|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jdatadst-tj.c
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file was part of the Independent JPEG Group's software:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1994-1996, Thomas G. Lane.
|
|
shun-iwasawa |
82a8f5 |
* Modified 2009-2012 by Guido Vollbeding.
|
|
shun-iwasawa |
82a8f5 |
* libjpeg-turbo Modifications:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2011, 2014, 2016, 2019, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* For conditions of distribution and use, see the accompanying README.ijg
|
|
shun-iwasawa |
82a8f5 |
* file.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file contains compression data destination routines for the case of
|
|
shun-iwasawa |
82a8f5 |
* emitting JPEG data to memory or to a file (or any stdio stream).
|
|
shun-iwasawa |
82a8f5 |
* While these routines are sufficient for most applications,
|
|
shun-iwasawa |
82a8f5 |
* some will want to use a different destination manager.
|
|
shun-iwasawa |
82a8f5 |
* IMPORTANT: we assume that fwrite() will correctly transcribe an array of
|
|
shun-iwasawa |
82a8f5 |
* JOCTETs into 8-bit-wide elements on external storage. If char is wider
|
|
shun-iwasawa |
82a8f5 |
* than 8 bits on your machine, you may need to do some tweaking.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
|
|
shun-iwasawa |
82a8f5 |
#include "jinclude.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jpeglib.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jerror.h"
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */</stdlib.h>
|
|
shun-iwasawa |
82a8f5 |
extern void *malloc(size_t size);
|
|
shun-iwasawa |
82a8f5 |
extern void free(void *ptr);
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
|
|
shun-iwasawa |
82a8f5 |
unsigned long *outsize, boolean alloc);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Expanded data destination object for memory output */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
shun-iwasawa |
82a8f5 |
struct jpeg_destination_mgr pub; /* public fields */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
unsigned char **outbuffer; /* target buffer */
|
|
shun-iwasawa |
82a8f5 |
unsigned long *outsize;
|
|
shun-iwasawa |
82a8f5 |
unsigned char *newbuffer; /* newly allocated buffer */
|
|
shun-iwasawa |
82a8f5 |
JOCTET *buffer; /* start of buffer */
|
|
shun-iwasawa |
82a8f5 |
size_t bufsize;
|
|
shun-iwasawa |
82a8f5 |
boolean alloc;
|
|
shun-iwasawa |
82a8f5 |
} my_mem_destination_mgr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef my_mem_destination_mgr *my_mem_dest_ptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize destination --- called by jpeg_start_compress
|
|
shun-iwasawa |
82a8f5 |
* before any data is actually written.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
init_mem_destination(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* no work necessary here */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Empty the output buffer --- called whenever buffer fills up.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* In typical applications, this should write the entire output buffer
|
|
shun-iwasawa |
82a8f5 |
* (ignoring the current state of next_output_byte & free_in_buffer),
|
|
shun-iwasawa |
82a8f5 |
* reset the pointer & count to the start of the buffer, and return TRUE
|
|
shun-iwasawa |
82a8f5 |
* indicating that the buffer has been dumped.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* In applications that need to be able to suspend compression due to output
|
|
shun-iwasawa |
82a8f5 |
* overrun, a FALSE return indicates that the buffer cannot be emptied now.
|
|
shun-iwasawa |
82a8f5 |
* In this situation, the compressor will return to its caller (possibly with
|
|
shun-iwasawa |
82a8f5 |
* an indication that it has not accepted all the supplied scanlines). The
|
|
shun-iwasawa |
82a8f5 |
* application should resume compression after it has made more room in the
|
|
shun-iwasawa |
82a8f5 |
* output buffer. Note that there are substantial restrictions on the use of
|
|
shun-iwasawa |
82a8f5 |
* suspension --- see the documentation.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* When suspending, the compressor will back up to a convenient restart point
|
|
shun-iwasawa |
82a8f5 |
* (typically the start of the current MCU). next_output_byte & free_in_buffer
|
|
shun-iwasawa |
82a8f5 |
* indicate where the restart point will be if the current call returns FALSE.
|
|
shun-iwasawa |
82a8f5 |
* Data beyond this point will be regenerated after resumption, so do not
|
|
shun-iwasawa |
82a8f5 |
* write it out when emptying the buffer externally.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
empty_mem_output_buffer(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
size_t nextsize;
|
|
shun-iwasawa |
82a8f5 |
JOCTET *nextbuffer;
|
|
shun-iwasawa |
82a8f5 |
my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Try to allocate new buffer with double size */
|
|
shun-iwasawa |
82a8f5 |
nextsize = dest->bufsize * 2;
|
|
shun-iwasawa |
82a8f5 |
nextbuffer = (JOCTET *)malloc(nextsize);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (nextbuffer == NULL)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
free(dest->newbuffer);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dest->newbuffer = nextbuffer;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dest->pub.next_output_byte = nextbuffer + dest->bufsize;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.free_in_buffer = dest->bufsize;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dest->buffer = nextbuffer;
|
|
shun-iwasawa |
82a8f5 |
dest->bufsize = nextsize;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Terminate destination --- called by jpeg_finish_compress
|
|
shun-iwasawa |
82a8f5 |
* after all data has been written. Usually needs to flush buffer.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
|
|
shun-iwasawa |
82a8f5 |
* application must deal with any cleanup that should happen even
|
|
shun-iwasawa |
82a8f5 |
* for error exit.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
term_mem_destination(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (dest->alloc) *dest->outbuffer = dest->buffer;
|
|
shun-iwasawa |
82a8f5 |
*dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Prepare for output to a memory buffer.
|
|
shun-iwasawa |
82a8f5 |
* The caller may supply an own initial buffer with appropriate size.
|
|
shun-iwasawa |
82a8f5 |
* Otherwise, or when the actual data output exceeds the given size,
|
|
shun-iwasawa |
82a8f5 |
* the library adapts the buffer size as necessary.
|
|
shun-iwasawa |
82a8f5 |
* The standard library functions malloc/free are used for allocating
|
|
shun-iwasawa |
82a8f5 |
* larger memory, so the buffer is available to the application after
|
|
shun-iwasawa |
82a8f5 |
* finishing compression, and then the application is responsible for
|
|
shun-iwasawa |
82a8f5 |
* freeing the requested memory.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
|
|
shun-iwasawa |
82a8f5 |
unsigned long *outsize, boolean alloc)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
boolean reused = FALSE;
|
|
shun-iwasawa |
82a8f5 |
my_mem_dest_ptr dest;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (outbuffer == NULL || outsize == NULL) /* sanity check */
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* The destination object is made permanent so that multiple JPEG images
|
|
shun-iwasawa |
82a8f5 |
* can be written to the same buffer without re-executing jpeg_mem_dest.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->dest == NULL) { /* first time for this JPEG object? */
|
|
shun-iwasawa |
82a8f5 |
cinfo->dest = (struct jpeg_destination_mgr *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
|
|
shun-iwasawa |
82a8f5 |
sizeof(my_mem_destination_mgr));
|
|
shun-iwasawa |
82a8f5 |
dest = (my_mem_dest_ptr)cinfo->dest;
|
|
shun-iwasawa |
82a8f5 |
dest->newbuffer = NULL;
|
|
shun-iwasawa |
82a8f5 |
dest->buffer = NULL;
|
|
shun-iwasawa |
82a8f5 |
} else if (cinfo->dest->init_destination != init_mem_destination) {
|
|
shun-iwasawa |
82a8f5 |
/* It is unsafe to reuse the existing destination manager unless it was
|
|
shun-iwasawa |
82a8f5 |
* created by this function.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dest = (my_mem_dest_ptr)cinfo->dest;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.init_destination = init_mem_destination;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.empty_output_buffer = empty_mem_output_buffer;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.term_destination = term_mem_destination;
|
|
shun-iwasawa |
82a8f5 |
if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
|
|
shun-iwasawa |
82a8f5 |
reused = TRUE;
|
|
shun-iwasawa |
82a8f5 |
dest->outbuffer = outbuffer;
|
|
shun-iwasawa |
82a8f5 |
dest->outsize = outsize;
|
|
shun-iwasawa |
82a8f5 |
dest->alloc = alloc;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (*outbuffer == NULL || *outsize == 0) {
|
|
shun-iwasawa |
82a8f5 |
if (alloc) {
|
|
shun-iwasawa |
82a8f5 |
/* Allocate initial buffer */
|
|
shun-iwasawa |
82a8f5 |
dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
|
|
shun-iwasawa |
82a8f5 |
if (dest->newbuffer == NULL)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
|
|
shun-iwasawa |
82a8f5 |
*outsize = OUTPUT_BUF_SIZE;
|
|
shun-iwasawa |
82a8f5 |
} else
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dest->pub.next_output_byte = dest->buffer = *outbuffer;
|
|
shun-iwasawa |
82a8f5 |
if (!reused)
|
|
shun-iwasawa |
82a8f5 |
dest->bufsize = *outsize;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.free_in_buffer = dest->bufsize;
|
|
shun-iwasawa |
82a8f5 |
}
|