/*1* jdatadst.c2*3* Copyright (C) 1994-1996, Thomas G. Lane.4* Modified 2009-2012 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains compression data destination routines for the case of9* emitting JPEG data to memory or to a file (or any stdio stream).10* While these routines are sufficient for most applications,11* some will want to use a different destination manager.12* IMPORTANT: we assume that fwrite() will correctly transcribe an array of13* JOCTETs into 8-bit-wide elements on external storage. If char is wider14* than 8 bits on your machine, you may need to do some tweaking.15*/1617/* this is not a core library module, so it doesn't define JPEG_INTERNALS */18#include "jinclude.h"19#include "jpeglib.h"20#include "jerror.h"2122#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */23extern void * malloc JPP((size_t size));24extern void free JPP((void *ptr));25#endif262728/* Expanded data destination object for stdio output */2930typedef struct {31struct jpeg_destination_mgr pub; /* public fields */3233FILE * outfile; /* target stream */34JOCTET * buffer; /* start of buffer */35} my_destination_mgr;3637typedef my_destination_mgr * my_dest_ptr;3839#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */404142/* Expanded data destination object for memory output */4344typedef struct {45struct jpeg_destination_mgr pub; /* public fields */4647unsigned char ** outbuffer; /* target buffer */48unsigned long * outsize;49unsigned char * newbuffer; /* newly allocated buffer */50JOCTET * buffer; /* start of buffer */51size_t bufsize;52} my_mem_destination_mgr;5354typedef my_mem_destination_mgr * my_mem_dest_ptr;555657/*58* Initialize destination --- called by jpeg_start_compress59* before any data is actually written.60*/6162METHODDEF(void)63init_destination (j_compress_ptr cinfo)64{65my_dest_ptr dest = (my_dest_ptr) cinfo->dest;6667/* Allocate the output buffer --- it will be released when done with image */68dest->buffer = (JOCTET *)69(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,70OUTPUT_BUF_SIZE * SIZEOF(JOCTET));7172dest->pub.next_output_byte = dest->buffer;73dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;74}7576METHODDEF(void)77init_mem_destination (j_compress_ptr cinfo)78{79/* no work necessary here */80}818283/*84* Empty the output buffer --- called whenever buffer fills up.85*86* In typical applications, this should write the entire output buffer87* (ignoring the current state of next_output_byte & free_in_buffer),88* reset the pointer & count to the start of the buffer, and return TRUE89* indicating that the buffer has been dumped.90*91* In applications that need to be able to suspend compression due to output92* overrun, a FALSE return indicates that the buffer cannot be emptied now.93* In this situation, the compressor will return to its caller (possibly with94* an indication that it has not accepted all the supplied scanlines). The95* application should resume compression after it has made more room in the96* output buffer. Note that there are substantial restrictions on the use of97* suspension --- see the documentation.98*99* When suspending, the compressor will back up to a convenient restart point100* (typically the start of the current MCU). next_output_byte & free_in_buffer101* indicate where the restart point will be if the current call returns FALSE.102* Data beyond this point will be regenerated after resumption, so do not103* write it out when emptying the buffer externally.104*/105106METHODDEF(boolean)107empty_output_buffer (j_compress_ptr cinfo)108{109my_dest_ptr dest = (my_dest_ptr) cinfo->dest;110111if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=112(size_t) OUTPUT_BUF_SIZE)113ERREXIT(cinfo, JERR_FILE_WRITE);114115dest->pub.next_output_byte = dest->buffer;116dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;117118return TRUE;119}120121METHODDEF(boolean)122empty_mem_output_buffer (j_compress_ptr cinfo)123{124size_t nextsize;125JOCTET * nextbuffer;126my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;127128/* Try to allocate new buffer with double size */129nextsize = dest->bufsize * 2;130nextbuffer = (JOCTET *) malloc(nextsize);131132if (nextbuffer == NULL)133ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);134135MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);136137if (dest->newbuffer != NULL)138free(dest->newbuffer);139140dest->newbuffer = nextbuffer;141142dest->pub.next_output_byte = nextbuffer + dest->bufsize;143dest->pub.free_in_buffer = dest->bufsize;144145dest->buffer = nextbuffer;146dest->bufsize = nextsize;147148return TRUE;149}150151152/*153* Terminate destination --- called by jpeg_finish_compress154* after all data has been written. Usually needs to flush buffer.155*156* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding157* application must deal with any cleanup that should happen even158* for error exit.159*/160161METHODDEF(void)162term_destination (j_compress_ptr cinfo)163{164my_dest_ptr dest = (my_dest_ptr) cinfo->dest;165size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;166167/* Write any data remaining in the buffer */168if (datacount > 0) {169if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)170ERREXIT(cinfo, JERR_FILE_WRITE);171}172fflush(dest->outfile);173/* Make sure we wrote the output file OK */174if (ferror(dest->outfile))175ERREXIT(cinfo, JERR_FILE_WRITE);176}177178METHODDEF(void)179term_mem_destination (j_compress_ptr cinfo)180{181my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;182183*dest->outbuffer = dest->buffer;184*dest->outsize = dest->bufsize - dest->pub.free_in_buffer;185}186187188/*189* Prepare for output to a stdio stream.190* The caller must have already opened the stream, and is responsible191* for closing it after finishing compression.192*/193194GLOBAL(void)195jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)196{197my_dest_ptr dest;198199/* The destination object is made permanent so that multiple JPEG images200* can be written to the same file without re-executing jpeg_stdio_dest.201* This makes it dangerous to use this manager and a different destination202* manager serially with the same JPEG object, because their private object203* sizes may be different. Caveat programmer.204*/205if (cinfo->dest == NULL) { /* first time for this JPEG object? */206cinfo->dest = (struct jpeg_destination_mgr *)207(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,208SIZEOF(my_destination_mgr));209}210211dest = (my_dest_ptr) cinfo->dest;212dest->pub.init_destination = init_destination;213dest->pub.empty_output_buffer = empty_output_buffer;214dest->pub.term_destination = term_destination;215dest->outfile = outfile;216}217218219/*220* Prepare for output to a memory buffer.221* The caller may supply an own initial buffer with appropriate size.222* Otherwise, or when the actual data output exceeds the given size,223* the library adapts the buffer size as necessary.224* The standard library functions malloc/free are used for allocating225* larger memory, so the buffer is available to the application after226* finishing compression, and then the application is responsible for227* freeing the requested memory.228* Note: An initial buffer supplied by the caller is expected to be229* managed by the application. The library does not free such buffer230* when allocating a larger buffer.231*/232233GLOBAL(void)234jpeg_mem_dest (j_compress_ptr cinfo,235unsigned char ** outbuffer, unsigned long * outsize)236{237my_mem_dest_ptr dest;238239if (outbuffer == NULL || outsize == NULL) /* sanity check */240ERREXIT(cinfo, JERR_BUFFER_SIZE);241242/* The destination object is made permanent so that multiple JPEG images243* can be written to the same buffer without re-executing jpeg_mem_dest.244*/245if (cinfo->dest == NULL) { /* first time for this JPEG object? */246cinfo->dest = (struct jpeg_destination_mgr *)247(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,248SIZEOF(my_mem_destination_mgr));249}250251dest = (my_mem_dest_ptr) cinfo->dest;252dest->pub.init_destination = init_mem_destination;253dest->pub.empty_output_buffer = empty_mem_output_buffer;254dest->pub.term_destination = term_mem_destination;255dest->outbuffer = outbuffer;256dest->outsize = outsize;257dest->newbuffer = NULL;258259if (*outbuffer == NULL || *outsize == 0) {260/* Allocate initial buffer */261dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);262if (dest->newbuffer == NULL)263ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);264*outsize = OUTPUT_BUF_SIZE;265}266267dest->pub.next_output_byte = dest->buffer = *outbuffer;268dest->pub.free_in_buffer = dest->bufsize = *outsize;269}270271272