Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/libjpeg/jdatadst.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* | |
| 2 * jdatadst.c | |
| 3 * | |
| 4 * Copyright (C) 1994-1996, Thomas G. Lane. | |
| 5 * Modified 2009-2022 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 /* Expanded data destination object for stdio output */ | |
| 30 | |
| 31 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ | |
| 32 | |
| 33 typedef struct { | |
| 34 struct jpeg_destination_mgr pub; /* public fields */ | |
| 35 | |
| 36 FILE * outfile; /* target stream */ | |
| 37 JOCTET buffer[OUTPUT_BUF_SIZE]; /* output buffer */ | |
| 38 } my_destination_mgr; | |
| 39 | |
| 40 typedef my_destination_mgr * my_dest_ptr; | |
| 41 | |
| 42 | |
| 43 /* Expanded data destination object for memory output */ | |
| 44 | |
| 45 typedef struct { | |
| 46 struct jpeg_destination_mgr pub; /* public fields */ | |
| 47 | |
| 48 unsigned char ** outbuffer; /* target buffer */ | |
| 49 size_t * outsize; | |
| 50 unsigned char * newbuffer; /* newly allocated buffer */ | |
| 51 JOCTET * buffer; /* start of buffer */ | |
| 52 size_t bufsize; | |
| 53 } my_mem_destination_mgr; | |
| 54 | |
| 55 typedef my_mem_destination_mgr * my_mem_dest_ptr; | |
| 56 | |
| 57 | |
| 58 /* | |
| 59 * Initialize destination --- called by jpeg_start_compress | |
| 60 * before any data is actually written. | |
| 61 */ | |
| 62 | |
| 63 METHODDEF(void) | |
| 64 init_destination (j_compress_ptr cinfo) | |
| 65 { | |
| 66 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
| 67 | |
| 68 dest->pub.next_output_byte = dest->buffer; | |
| 69 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
| 70 } | |
| 71 | |
| 72 METHODDEF(void) | |
| 73 init_mem_destination (j_compress_ptr cinfo) | |
| 74 { | |
| 75 /* no work necessary here */ | |
| 76 } | |
| 77 | |
| 78 | |
| 79 /* | |
| 80 * Empty the output buffer --- called whenever buffer fills up. | |
| 81 * | |
| 82 * In typical applications, this should write the entire output buffer | |
| 83 * (ignoring the current state of next_output_byte & free_in_buffer), | |
| 84 * reset the pointer & count to the start of the buffer, and return TRUE | |
| 85 * indicating that the buffer has been dumped. | |
| 86 * | |
| 87 * In applications that need to be able to suspend compression due to output | |
| 88 * overrun, a FALSE return indicates that the buffer cannot be emptied now. | |
| 89 * In this situation, the compressor will return to its caller (possibly with | |
| 90 * an indication that it has not accepted all the supplied scanlines). The | |
| 91 * application should resume compression after it has made more room in the | |
| 92 * output buffer. Note that there are substantial restrictions on the use of | |
| 93 * suspension --- see the documentation. | |
| 94 * | |
| 95 * When suspending, the compressor will back up to a convenient restart point | |
| 96 * (typically the start of the current MCU). next_output_byte & free_in_buffer | |
| 97 * indicate where the restart point will be if the current call returns FALSE. | |
| 98 * Data beyond this point will be regenerated after resumption, so do not | |
| 99 * write it out when emptying the buffer externally. | |
| 100 */ | |
| 101 | |
| 102 METHODDEF(boolean) | |
| 103 empty_output_buffer (j_compress_ptr cinfo) | |
| 104 { | |
| 105 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
| 106 | |
| 107 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != | |
| 108 (size_t) OUTPUT_BUF_SIZE) | |
| 109 ERREXIT(cinfo, JERR_FILE_WRITE); | |
| 110 | |
| 111 dest->pub.next_output_byte = dest->buffer; | |
| 112 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | |
| 113 | |
| 114 return TRUE; | |
| 115 } | |
| 116 | |
| 117 METHODDEF(boolean) | |
| 118 empty_mem_output_buffer (j_compress_ptr cinfo) | |
| 119 { | |
| 120 size_t nextsize; | |
| 121 JOCTET * nextbuffer; | |
| 122 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; | |
| 123 | |
| 124 /* Try to allocate new buffer with double size */ | |
| 125 nextsize = dest->bufsize * 2; | |
| 126 nextbuffer = (JOCTET *) malloc(nextsize); | |
| 127 | |
| 128 if (nextbuffer == NULL) | |
| 129 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11); | |
| 130 | |
| 131 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); | |
| 132 | |
| 133 if (dest->newbuffer != NULL) | |
| 134 free(dest->newbuffer); | |
| 135 | |
| 136 dest->newbuffer = nextbuffer; | |
| 137 | |
| 138 dest->pub.next_output_byte = nextbuffer + dest->bufsize; | |
| 139 dest->pub.free_in_buffer = dest->bufsize; | |
| 140 | |
| 141 dest->buffer = nextbuffer; | |
| 142 dest->bufsize = nextsize; | |
| 143 | |
| 144 return TRUE; | |
| 145 } | |
| 146 | |
| 147 | |
| 148 /* | |
| 149 * Terminate destination --- called by jpeg_finish_compress | |
| 150 * after all data has been written. Usually needs to flush buffer. | |
| 151 * | |
| 152 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding | |
| 153 * application must deal with any cleanup that should happen even | |
| 154 * for error exit. | |
| 155 */ | |
| 156 | |
| 157 METHODDEF(void) | |
| 158 term_destination (j_compress_ptr cinfo) | |
| 159 { | |
| 160 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | |
| 161 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; | |
| 162 | |
| 163 /* Write any data remaining in the buffer */ | |
| 164 if (datacount > 0) { | |
| 165 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) | |
| 166 ERREXIT(cinfo, JERR_FILE_WRITE); | |
| 167 } | |
| 168 JFFLUSH(dest->outfile); | |
| 169 /* Make sure we wrote the output file OK */ | |
| 170 if (JFERROR(dest->outfile)) | |
| 171 ERREXIT(cinfo, JERR_FILE_WRITE); | |
| 172 } | |
| 173 | |
| 174 METHODDEF(void) | |
| 175 term_mem_destination (j_compress_ptr cinfo) | |
| 176 { | |
| 177 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; | |
| 178 | |
| 179 *dest->outbuffer = dest->buffer; | |
| 180 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; | |
| 181 } | |
| 182 | |
| 183 | |
| 184 /* | |
| 185 * Prepare for output to a stdio stream. | |
| 186 * The caller must have already opened the stream, | |
| 187 * and is responsible for closing it after finishing compression. | |
| 188 */ | |
| 189 | |
| 190 GLOBAL(void) | |
| 191 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) | |
| 192 { | |
| 193 my_dest_ptr dest; | |
| 194 | |
| 195 /* The destination object is made permanent so that multiple JPEG images | |
| 196 * can be written to the same file without re-executing jpeg_stdio_dest. | |
| 197 * This makes it dangerous to use this manager and a different destination | |
| 198 * manager serially with the same JPEG object, because their private object | |
| 199 * sizes may be different. Caveat programmer. | |
| 200 */ | |
| 201 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ | |
| 202 cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) | |
| 203 ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_destination_mgr)); | |
| 204 } | |
| 205 | |
| 206 dest = (my_dest_ptr) cinfo->dest; | |
| 207 dest->pub.init_destination = init_destination; | |
| 208 dest->pub.empty_output_buffer = empty_output_buffer; | |
| 209 dest->pub.term_destination = term_destination; | |
| 210 dest->outfile = outfile; | |
| 211 } | |
| 212 | |
| 213 | |
| 214 /* | |
| 215 * Prepare for output to a memory buffer. | |
| 216 * The caller may supply an own initial buffer with appropriate size. | |
| 217 * Otherwise, or when the actual data output exceeds the given size, | |
| 218 * the library adapts the buffer size as necessary. | |
| 219 * The standard library functions malloc/free are used for allocating | |
| 220 * larger memory, so the buffer is available to the application after | |
| 221 * finishing compression, and then the application is responsible for | |
| 222 * freeing the requested memory. | |
| 223 * Note: An initial buffer supplied by the caller is expected to be | |
| 224 * managed by the application. The library does not free such buffer | |
| 225 * when allocating a larger buffer. | |
| 226 */ | |
| 227 | |
| 228 GLOBAL(void) | |
| 229 jpeg_mem_dest (j_compress_ptr cinfo, | |
| 230 unsigned char ** outbuffer, size_t * outsize) | |
| 231 { | |
| 232 my_mem_dest_ptr dest; | |
| 233 | |
| 234 if (outbuffer == NULL || outsize == NULL) /* sanity check */ | |
| 235 ERREXIT(cinfo, JERR_BUFFER_SIZE); | |
| 236 | |
| 237 /* The destination object is made permanent so that multiple JPEG images | |
| 238 * can be written to the same buffer without re-executing jpeg_mem_dest. | |
| 239 */ | |
| 240 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ | |
| 241 cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) | |
| 242 ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_mem_destination_mgr)); | |
| 243 } | |
| 244 | |
| 245 dest = (my_mem_dest_ptr) cinfo->dest; | |
| 246 dest->pub.init_destination = init_mem_destination; | |
| 247 dest->pub.empty_output_buffer = empty_mem_output_buffer; | |
| 248 dest->pub.term_destination = term_mem_destination; | |
| 249 dest->outbuffer = outbuffer; | |
| 250 dest->outsize = outsize; | |
| 251 dest->newbuffer = NULL; | |
| 252 | |
| 253 if (*outbuffer == NULL || *outsize == 0) { | |
| 254 /* Allocate initial buffer */ | |
| 255 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE); | |
| 256 if (dest->newbuffer == NULL) | |
| 257 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); | |
| 258 *outsize = OUTPUT_BUF_SIZE; | |
| 259 } | |
| 260 | |
| 261 dest->pub.next_output_byte = dest->buffer = *outbuffer; | |
| 262 dest->pub.free_in_buffer = dest->bufsize = *outsize; | |
| 263 } |
