OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * jdatadst.c |
| 3 * |
| 4 * Copyright (C) 1994-1996, Thomas G. Lane. |
| 5 * Modified 2009 by Guido Vollbeding. |
| 6 * This file is part of the Independent JPEG Group's software. |
| 7 * For conditions of distribution and use, see the accompanying README file. |
| 8 * |
| 9 * This file contains compression data destination routines for the case of |
| 10 * emitting JPEG data to memory or to a file (or any stdio stream). |
| 11 * While these routines are sufficient for most applications, |
| 12 * some will want to use a different destination manager. |
| 13 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of |
| 14 * JOCTETs into 8-bit-wide elements on external storage. If char is wider |
| 15 * than 8 bits on your machine, you may need to do some tweaking. |
| 16 */ |
| 17 |
| 18 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ |
| 19 #include "jinclude.h" |
| 20 #include "jpeglib.h" |
| 21 #include "jerror.h" |
| 22 |
| 23 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
| 24 extern void * malloc JPP((size_t size)); |
| 25 extern void free JPP((void *ptr)); |
| 26 #endif |
| 27 |
| 28 |
| 29 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
| 30 |
| 31 |
| 32 /* Expanded data destination object for memory output */ |
| 33 |
| 34 typedef struct { |
| 35 struct jpeg_destination_mgr pub; /* public fields */ |
| 36 |
| 37 unsigned char ** outbuffer; /* target buffer */ |
| 38 unsigned long * outsize; |
| 39 unsigned char * newbuffer; /* newly allocated buffer */ |
| 40 JOCTET * buffer; /* start of buffer */ |
| 41 size_t bufsize; |
| 42 boolean alloc; |
| 43 } my_mem_destination_mgr; |
| 44 |
| 45 typedef my_mem_destination_mgr * my_mem_dest_ptr; |
| 46 |
| 47 |
| 48 /* |
| 49 * Initialize destination --- called by jpeg_start_compress |
| 50 * before any data is actually written. |
| 51 */ |
| 52 |
| 53 METHODDEF(void) |
| 54 init_mem_destination (j_compress_ptr cinfo) |
| 55 { |
| 56 /* no work necessary here */ |
| 57 } |
| 58 |
| 59 |
| 60 /* |
| 61 * Empty the output buffer --- called whenever buffer fills up. |
| 62 * |
| 63 * In typical applications, this should write the entire output buffer |
| 64 * (ignoring the current state of next_output_byte & free_in_buffer), |
| 65 * reset the pointer & count to the start of the buffer, and return TRUE |
| 66 * indicating that the buffer has been dumped. |
| 67 * |
| 68 * In applications that need to be able to suspend compression due to output |
| 69 * overrun, a FALSE return indicates that the buffer cannot be emptied now. |
| 70 * In this situation, the compressor will return to its caller (possibly with |
| 71 * an indication that it has not accepted all the supplied scanlines). The |
| 72 * application should resume compression after it has made more room in the |
| 73 * output buffer. Note that there are substantial restrictions on the use of |
| 74 * suspension --- see the documentation. |
| 75 * |
| 76 * When suspending, the compressor will back up to a convenient restart point |
| 77 * (typically the start of the current MCU). next_output_byte & free_in_buffer |
| 78 * indicate where the restart point will be if the current call returns FALSE. |
| 79 * Data beyond this point will be regenerated after resumption, so do not |
| 80 * write it out when emptying the buffer externally. |
| 81 */ |
| 82 |
| 83 METHODDEF(boolean) |
| 84 empty_mem_output_buffer (j_compress_ptr cinfo) |
| 85 { |
| 86 size_t nextsize; |
| 87 JOCTET * nextbuffer; |
| 88 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
| 89 |
| 90 if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 91 |
| 92 /* Try to allocate new buffer with double size */ |
| 93 nextsize = dest->bufsize * 2; |
| 94 nextbuffer = malloc(nextsize); |
| 95 |
| 96 if (nextbuffer == NULL) |
| 97 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 98 |
| 99 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); |
| 100 |
| 101 if (dest->newbuffer != NULL) |
| 102 free(dest->newbuffer); |
| 103 |
| 104 dest->newbuffer = nextbuffer; |
| 105 |
| 106 dest->pub.next_output_byte = nextbuffer + dest->bufsize; |
| 107 dest->pub.free_in_buffer = dest->bufsize; |
| 108 |
| 109 dest->buffer = nextbuffer; |
| 110 dest->bufsize = nextsize; |
| 111 |
| 112 return TRUE; |
| 113 } |
| 114 |
| 115 |
| 116 /* |
| 117 * Terminate destination --- called by jpeg_finish_compress |
| 118 * after all data has been written. Usually needs to flush buffer. |
| 119 * |
| 120 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding |
| 121 * application must deal with any cleanup that should happen even |
| 122 * for error exit. |
| 123 */ |
| 124 |
| 125 METHODDEF(void) |
| 126 term_mem_destination (j_compress_ptr cinfo) |
| 127 { |
| 128 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
| 129 |
| 130 if(dest->alloc) *dest->outbuffer = dest->buffer; |
| 131 *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer); |
| 132 } |
| 133 |
| 134 |
| 135 /* |
| 136 * Prepare for output to a memory buffer. |
| 137 * The caller may supply an own initial buffer with appropriate size. |
| 138 * Otherwise, or when the actual data output exceeds the given size, |
| 139 * the library adapts the buffer size as necessary. |
| 140 * The standard library functions malloc/free are used for allocating |
| 141 * larger memory, so the buffer is available to the application after |
| 142 * finishing compression, and then the application is responsible for |
| 143 * freeing the requested memory. |
| 144 */ |
| 145 |
| 146 GLOBAL(void) |
| 147 jpeg_mem_dest_tj (j_compress_ptr cinfo, |
| 148 unsigned char ** outbuffer, unsigned long * outsize, |
| 149 boolean alloc) |
| 150 { |
| 151 my_mem_dest_ptr dest; |
| 152 |
| 153 if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
| 154 ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 155 |
| 156 /* The destination object is made permanent so that multiple JPEG images |
| 157 * can be written to the same buffer without re-executing jpeg_mem_dest. |
| 158 */ |
| 159 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
| 160 cinfo->dest = (struct jpeg_destination_mgr *) |
| 161 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 162 SIZEOF(my_mem_destination_mgr)); |
| 163 dest = (my_mem_dest_ptr) cinfo->dest; |
| 164 dest->newbuffer = NULL; |
| 165 } |
| 166 |
| 167 dest = (my_mem_dest_ptr) cinfo->dest; |
| 168 dest->pub.init_destination = init_mem_destination; |
| 169 dest->pub.empty_output_buffer = empty_mem_output_buffer; |
| 170 dest->pub.term_destination = term_mem_destination; |
| 171 dest->outbuffer = outbuffer; |
| 172 dest->outsize = outsize; |
| 173 dest->alloc = alloc; |
| 174 |
| 175 if (*outbuffer == NULL || *outsize == 0) { |
| 176 if (alloc) { |
| 177 /* Allocate initial buffer */ |
| 178 dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE); |
| 179 if (dest->newbuffer == NULL) |
| 180 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 181 *outsize = OUTPUT_BUF_SIZE; |
| 182 } |
| 183 else ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 184 } |
| 185 |
| 186 dest->pub.next_output_byte = dest->buffer = *outbuffer; |
| 187 dest->pub.free_in_buffer = dest->bufsize = *outsize; |
| 188 } |
OLD | NEW |