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