SAMV71 Xplained Ultra Software Package 1.4

jdatadst_mem.c

00001 /*
00002  * jdatadst_mem.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * Modified 2009 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains compression data destination routines for the case of
00010  * emitting JPEG data to memory or to a file (or any stdio stream).
00011  * While these routines are sufficient for most applications,
00012  * some will want to use a different destination manager.
00013  * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
00014  * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
00015  * than 8 bits on your machine, you may need to do some tweaking.
00016  */
00017 
00018 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
00019 #include "jinclude.h"
00020 #include "jpeglib.h"
00021 #include "jerror.h"
00022 
00023 #ifndef HAVE_STDLIB_H       /* <stdlib.h> should declare malloc(),free() */
00024 extern void * malloc JPP((size_t size));
00025 extern void free JPP((void *ptr));
00026 #endif
00027 
00028 #define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
00029 
00030 /* Expanded data destination object for memory output */
00031 
00032 typedef struct {
00033   struct jpeg_destination_mgr pub; /* public fields */
00034 
00035   unsigned char ** outbuffer;   /* target buffer */
00036   unsigned long * outsize;
00037   unsigned char * newbuffer;    /* newly allocated buffer */
00038   JOCTET * buffer;      /* start of buffer */
00039   size_t bufsize;
00040 } my_mem_destination_mgr;
00041 
00042 typedef my_mem_destination_mgr * my_mem_dest_ptr;
00043 
00044 METHODDEF(void)
00045 init_mem_destination (j_compress_ptr cinfo)
00046 {
00047   /* no work necessary here */
00048 }
00049 
00050 METHODDEF(boolean)
00051 empty_mem_output_buffer (j_compress_ptr cinfo)
00052 {
00053   size_t nextsize;
00054   JOCTET * nextbuffer;
00055   my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
00056 
00057   /* Try to allocate new buffer with double size */
00058   nextsize = dest->bufsize * 2;
00059   nextbuffer = malloc(nextsize);
00060 
00061   if (nextbuffer == NULL)
00062     ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
00063 
00064   MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
00065 
00066   if (dest->newbuffer != NULL)
00067     free(dest->newbuffer);
00068 
00069   dest->newbuffer = nextbuffer;
00070 
00071   dest->pub.next_output_byte = nextbuffer + dest->bufsize;
00072   dest->pub.free_in_buffer = dest->bufsize;
00073 
00074   dest->buffer = nextbuffer;
00075   dest->bufsize = nextsize;
00076 
00077   return TRUE;
00078 }
00079 
00080 METHODDEF(void)
00081 term_mem_destination (j_compress_ptr cinfo)
00082 {
00083   my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
00084 
00085   *dest->outbuffer = dest->buffer;
00086   *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
00087 }
00088 
00089 /*
00090  * Prepare for output to a memory buffer.
00091  * The caller may supply an own initial buffer with appropriate size.
00092  * Otherwise, or when the actual data output exceeds the given size,
00093  * the library adapts the buffer size as necessary.
00094  * The standard library functions malloc/free are used for allocating
00095  * larger memory, so the buffer is available to the application after
00096  * finishing compression, and then the application is responsible for
00097  * freeing the requested memory.
00098  */
00099 
00100 GLOBAL(void)
00101 jpeg_mem_dest (j_compress_ptr cinfo,
00102            unsigned char ** outbuffer, unsigned long * outsize)
00103 {
00104   my_mem_dest_ptr dest;
00105 
00106   if (outbuffer == NULL || outsize == NULL) /* sanity check */
00107     ERREXIT(cinfo, JERR_BUFFER_SIZE);
00108 
00109   /* The destination object is made permanent so that multiple JPEG images
00110    * can be written to the same buffer without re-executing jpeg_mem_dest.
00111    */
00112   if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
00113     cinfo->dest = (struct jpeg_destination_mgr *)
00114       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00115                   SIZEOF(my_mem_destination_mgr));
00116   }
00117 
00118   dest = (my_mem_dest_ptr) cinfo->dest;
00119   dest->pub.init_destination = init_mem_destination;
00120   dest->pub.empty_output_buffer = empty_mem_output_buffer;
00121   dest->pub.term_destination = term_mem_destination;
00122   dest->outbuffer = outbuffer;
00123   dest->outsize = outsize;
00124   dest->newbuffer = NULL;
00125 
00126   if (*outbuffer == NULL || *outsize == 0) {
00127     /* Allocate initial buffer */
00128     dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE);
00129     if (dest->newbuffer == NULL)
00130       ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
00131     *outsize = OUTPUT_BUF_SIZE;
00132   }
00133 
00134   dest->pub.next_output_byte = dest->buffer = *outbuffer;
00135   dest->pub.free_in_buffer = dest->bufsize = *outsize;
00136 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines