/src/libjpeg-turbo.main/src/turbojpeg-mp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2009-2026 D. R. Commander |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions are met: |
6 | | * |
7 | | * - Redistributions of source code must retain the above copyright notice, |
8 | | * this list of conditions and the following disclaimer. |
9 | | * - Redistributions in binary form must reproduce the above copyright notice, |
10 | | * this list of conditions and the following disclaimer in the documentation |
11 | | * and/or other materials provided with the distribution. |
12 | | * - Neither the name of the libjpeg-turbo Project nor the names of its |
13 | | * contributors may be used to endorse or promote products derived from this |
14 | | * software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
17 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
20 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | | * POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | /* TurboJPEG API functions that must be compiled for multiple data |
30 | | precisions */ |
31 | | |
32 | | #if BITS_IN_JSAMPLE == 8 |
33 | 0 | #define _JSAMPLE JSAMPLE |
34 | 0 | #define _JSAMPROW JSAMPROW |
35 | 0 | #define _buffer buffer |
36 | 0 | #define _jinit_read_png jinit_read_png |
37 | 0 | #define _jinit_write_png jinit_write_png |
38 | 0 | #define _jinit_read_ppm jinit_read_ppm |
39 | 0 | #define _jinit_write_ppm jinit_write_ppm |
40 | 0 | #define _jpeg_crop_scanline jpeg_crop_scanline |
41 | 0 | #define _jpeg_read_scanlines jpeg_read_scanlines |
42 | 0 | #define _jpeg_skip_scanlines jpeg_skip_scanlines |
43 | 0 | #define _jpeg_write_scanlines jpeg_write_scanlines |
44 | | #elif BITS_IN_JSAMPLE == 12 |
45 | 0 | #define _JSAMPLE J12SAMPLE |
46 | 0 | #define _JSAMPROW J12SAMPROW |
47 | 0 | #define _buffer buffer12 |
48 | 0 | #define _jinit_read_png j12init_read_png |
49 | 0 | #define _jinit_write_png j12init_write_png |
50 | 0 | #define _jinit_read_ppm j12init_read_ppm |
51 | 0 | #define _jinit_write_ppm j12init_write_ppm |
52 | 0 | #define _jpeg_crop_scanline jpeg12_crop_scanline |
53 | 0 | #define _jpeg_read_scanlines jpeg12_read_scanlines |
54 | 0 | #define _jpeg_skip_scanlines jpeg12_skip_scanlines |
55 | 0 | #define _jpeg_write_scanlines jpeg12_write_scanlines |
56 | | #elif BITS_IN_JSAMPLE == 16 |
57 | 0 | #define _JSAMPLE J16SAMPLE |
58 | 0 | #define _JSAMPROW J16SAMPROW |
59 | 0 | #define _buffer buffer16 |
60 | 0 | #define _jinit_read_png j16init_read_png |
61 | 0 | #define _jinit_write_png j16init_write_png |
62 | 0 | #define _jinit_read_ppm j16init_read_ppm |
63 | 0 | #define _jinit_write_ppm j16init_write_ppm |
64 | 0 | #define _jpeg_read_scanlines jpeg16_read_scanlines |
65 | 0 | #define _jpeg_write_scanlines jpeg16_write_scanlines |
66 | | #endif |
67 | | |
68 | 0 | #define _GET_NAME(name, suffix) name##suffix |
69 | 0 | #define GET_NAME(name, suffix) _GET_NAME(name, suffix) |
70 | 0 | #define _GET_STRING(name, suffix) #name #suffix |
71 | 0 | #define GET_STRING(name, suffix) _GET_STRING(name, suffix) |
72 | | |
73 | | |
74 | | /******************************** Compressor *********************************/ |
75 | | |
76 | | /* TurboJPEG 3.0+ */ |
77 | | DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE) |
78 | | (tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height, |
79 | | int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize) |
80 | 0 | { |
81 | 0 | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); |
82 | 0 | int i, retval = 0; |
83 | 0 | boolean alloc = TRUE; |
84 | 0 | _JSAMPROW *row_pointer = NULL; |
85 | |
|
86 | 0 | GET_CINSTANCE(handle) |
87 | 0 | if ((this->init & COMPRESS) == 0) |
88 | 0 | THROW("Instance has not been initialized for compression"); |
89 | |
|
90 | 0 | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
91 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || |
92 | 0 | jpegSize == NULL) |
93 | 0 | THROW("Invalid argument"); |
94 | |
|
95 | 0 | if (!this->lossless && this->quality == -1) |
96 | 0 | THROW("TJPARAM_QUALITY must be specified"); |
97 | 0 | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) |
98 | 0 | THROW("TJPARAM_SUBSAMP must be specified"); |
99 | |
|
100 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
101 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
102 | 0 | THROW("Invalid argument"); |
103 | |
|
104 | 0 | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) |
105 | 0 | THROW("Memory allocation failure"); |
106 | |
|
107 | 0 | CATCH_LIBJPEG(this); |
108 | |
|
109 | 0 | cinfo->image_width = width; |
110 | 0 | cinfo->image_height = height; |
111 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
112 | | #if BITS_IN_JSAMPLE == 8 |
113 | 0 | if (this->lossless && this->precision >= 2 && |
114 | 0 | this->precision <= BITS_IN_JSAMPLE) |
115 | | #else |
116 | 0 | if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 && |
117 | 0 | this->precision <= BITS_IN_JSAMPLE) |
118 | 0 | #endif |
119 | 0 | cinfo->data_precision = this->precision; |
120 | |
|
121 | 0 | setCompDefaults(this, pixelFormat, FALSE); |
122 | 0 | if (this->noRealloc) alloc = FALSE; |
123 | 0 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
124 | |
|
125 | 0 | jpeg_start_compress(cinfo, TRUE); |
126 | 0 | if (this->iccBuf != NULL && this->iccSize != 0) |
127 | 0 | jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize); |
128 | 0 | for (i = 0; i < height; i++) { |
129 | 0 | if (this->bottomUp) |
130 | 0 | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; |
131 | 0 | else |
132 | 0 | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; |
133 | 0 | } |
134 | 0 | while (cinfo->next_scanline < cinfo->image_height) |
135 | 0 | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
136 | 0 | cinfo->image_height - cinfo->next_scanline); |
137 | 0 | jpeg_finish_compress(cinfo); |
138 | |
|
139 | 0 | bailout: |
140 | 0 | if (cinfo->global_state > CSTATE_START && alloc) |
141 | 0 | (*cinfo->dest->term_destination) (cinfo); |
142 | 0 | if (cinfo->global_state > CSTATE_START || retval == -1) |
143 | 0 | jpeg_abort_compress(cinfo); |
144 | 0 | free(row_pointer); |
145 | 0 | if (this->jerr.warning) retval = -1; |
146 | 0 | return retval; |
147 | 0 | } Unexecuted instantiation: tj3Compress8 Unexecuted instantiation: tj3Compress12 Unexecuted instantiation: tj3Compress16 |
148 | | |
149 | | |
150 | | /******************************* Decompressor ********************************/ |
151 | | |
152 | | /* TurboJPEG 3.0+ */ |
153 | | DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE) |
154 | | (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize, |
155 | | _JSAMPLE *dstBuf, int pitch, int pixelFormat) |
156 | 0 | { |
157 | 0 | static const char FUNCTION_NAME[] = |
158 | 0 | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
159 | 0 | _JSAMPROW *row_pointer = NULL; |
160 | 0 | int croppedHeight, i, retval = 0; |
161 | | #if BITS_IN_JSAMPLE != 16 |
162 | | int scaledWidth; |
163 | | #endif |
164 | 0 | struct my_progress_mgr progress; |
165 | |
|
166 | 0 | GET_DINSTANCE(handle); |
167 | 0 | if ((this->init & DECOMPRESS) == 0) |
168 | 0 | THROW("Instance has not been initialized for decompression"); |
169 | |
|
170 | 0 | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
171 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
172 | 0 | THROW("Invalid argument"); |
173 | |
|
174 | 0 | if (this->scanLimit) { |
175 | 0 | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
176 | 0 | progress.pub.progress_monitor = my_progress_monitor; |
177 | 0 | progress.this = this; |
178 | 0 | dinfo->progress = &progress.pub; |
179 | 0 | } else |
180 | 0 | dinfo->progress = NULL; |
181 | |
|
182 | 0 | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
183 | |
|
184 | 0 | CATCH_LIBJPEG(this); |
185 | |
|
186 | 0 | if (dinfo->global_state <= DSTATE_INHEADER) { |
187 | 0 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
188 | 0 | jpeg_read_header(dinfo, TRUE); |
189 | 0 | } |
190 | 0 | setDecompParameters(this); |
191 | | #if BITS_IN_JSAMPLE == 12 |
192 | 0 | if (this->precision == 8 && !this->lossless) |
193 | 0 | dinfo->data_precision = 12; |
194 | | #endif |
195 | 0 | if (this->maxPixels && |
196 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > |
197 | 0 | (unsigned long long)this->maxPixels) |
198 | 0 | THROW("Image is too large"); |
199 | 0 | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
200 | | #if BITS_IN_JSAMPLE != 16 |
201 | 0 | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
202 | | #endif |
203 | 0 | dinfo->do_fancy_upsampling = !this->fastUpsample; |
204 | 0 | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
205 | |
|
206 | 0 | dinfo->scale_num = this->scalingFactor.num; |
207 | 0 | dinfo->scale_denom = this->scalingFactor.denom; |
208 | |
|
209 | 0 | jpeg_start_decompress(dinfo); |
210 | |
|
211 | | #if BITS_IN_JSAMPLE != 16 |
212 | 0 | if (this->croppingRegion.x != 0 || |
213 | 0 | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
214 | 0 | JDIMENSION crop_x = this->croppingRegion.x; |
215 | 0 | JDIMENSION crop_w = this->croppingRegion.w; |
216 | |
|
217 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
218 | 0 | if ((int)crop_x != this->croppingRegion.x) |
219 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
220 | 0 | "actual (%d) cropping region left boundary", |
221 | 0 | this->croppingRegion.x, (int)crop_x); |
222 | 0 | if ((int)crop_w != this->croppingRegion.w) |
223 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
224 | 0 | "actual (%d) cropping region width", |
225 | 0 | this->croppingRegion.w, (int)crop_w); |
226 | 0 | } |
227 | 0 | #endif |
228 | | |
229 | 0 | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
230 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) |
231 | 0 | THROW("Invalid argument"); |
232 | |
|
233 | 0 | croppedHeight = dinfo->output_height; |
234 | | #if BITS_IN_JSAMPLE != 16 |
235 | 0 | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
236 | 0 | croppedHeight = this->croppingRegion.h; |
237 | | #endif |
238 | 0 | if ((row_pointer = |
239 | 0 | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
240 | 0 | THROW("Memory allocation failure"); |
241 | 0 | CATCH_LIBJPEG(this); |
242 | 0 | for (i = 0; i < (int)croppedHeight; i++) { |
243 | 0 | if (this->bottomUp) |
244 | 0 | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
245 | 0 | else |
246 | 0 | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
247 | 0 | } |
248 | |
|
249 | | #if BITS_IN_JSAMPLE != 16 |
250 | 0 | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
251 | 0 | if (this->croppingRegion.y != 0) { |
252 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
253 | | |
254 | 0 | if ((int)lines != this->croppingRegion.y) |
255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
256 | | "actual (%d) cropping region upper boundary", |
257 | 0 | this->croppingRegion.y, (int)lines); |
258 | 0 | } |
259 | 0 | while ((int)dinfo->output_scanline < |
260 | 0 | this->croppingRegion.y + this->croppingRegion.h) |
261 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
262 | 0 | this->croppingRegion.y], |
263 | 0 | this->croppingRegion.y + this->croppingRegion.h - |
264 | 0 | dinfo->output_scanline); |
265 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != |
266 | 0 | (int)dinfo->output_height) { |
267 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
268 | | this->croppingRegion.y - |
269 | | this->croppingRegion.h); |
270 | |
|
271 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - |
272 | 0 | this->croppingRegion.h) |
273 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
274 | 0 | "actual (%d) cropping region lower boundary", |
275 | 0 | this->croppingRegion.y + this->croppingRegion.h, |
276 | 0 | (int)(dinfo->output_height - lines)); |
277 | 0 | } |
278 | 0 | } else |
279 | 0 | #endif |
280 | 0 | { |
281 | 0 | while (dinfo->output_scanline < dinfo->output_height) |
282 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
283 | 0 | dinfo->output_height - dinfo->output_scanline); |
284 | 0 | } |
285 | 0 | jpeg_finish_decompress(dinfo); |
286 | |
|
287 | 0 | bailout: |
288 | 0 | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
289 | 0 | free(row_pointer); |
290 | 0 | if (this->jerr.warning) retval = -1; |
291 | 0 | return retval; |
292 | 0 | } Unexecuted instantiation: tj3Decompress8 Unexecuted instantiation: tj3Decompress12 Unexecuted instantiation: tj3Decompress16 |
293 | | |
294 | | |
295 | | /*************************** Packed-Pixel Image I/O **************************/ |
296 | | |
297 | | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
298 | | |
299 | | /* TurboJPEG 3.0+ */ |
300 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
301 | | static |
302 | | #endif |
303 | | _JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
304 | | (tjhandle handle, FILE *file, int *width, int align, int *height, |
305 | | int *pixelFormat) |
306 | 0 | { |
307 | 0 | static const char FUNCTION_NAME[] = |
308 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
309 | |
|
310 | 0 | int retval = 0, tempc; |
311 | 0 | size_t pitch; |
312 | 0 | tjhandle handle2 = NULL; |
313 | 0 | tjinstance *this2; |
314 | 0 | j_compress_ptr cinfo = NULL; |
315 | 0 | cjpeg_source_ptr src = NULL; |
316 | 0 | _JSAMPLE *dstBuf = NULL; |
317 | 0 | boolean invert; |
318 | |
|
319 | 0 | GET_TJINSTANCE(handle, NULL) |
320 | |
|
321 | 0 | if (!file || !width || align < 1 || !height || !pixelFormat || |
322 | 0 | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
323 | 0 | THROW("Invalid argument"); |
324 | 0 | if ((align & (align - 1)) != 0) |
325 | 0 | THROW("Alignment must be a power of 2"); |
326 | | |
327 | | /* The instance handle passed to this function is used only for parameter |
328 | | retrieval. Create a new temporary instance to avoid interfering with the |
329 | | libjpeg state of the primary instance. */ |
330 | 0 | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; |
331 | 0 | this2 = (tjinstance *)handle2; |
332 | 0 | cinfo = &this2->cinfo; |
333 | |
|
334 | 0 | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
335 | 0 | THROW_UNIX("Could not read input file") |
336 | 0 | else if (tempc == EOF) |
337 | 0 | THROW("Input file contains no data"); |
338 | |
|
339 | 0 | CATCH_LIBJPEG(this2); |
340 | |
|
341 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
342 | 0 | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
343 | 0 | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
344 | 0 | if (tempc == 'B') { |
345 | 0 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
346 | 0 | THROW("Could not initialize bitmap loader"); |
347 | 0 | invert = !this->bottomUp; |
348 | 0 | } else if (tempc == 0x89) { |
349 | | #if BITS_IN_JSAMPLE == 8 |
350 | 0 | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
351 | | #else |
352 | 0 | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
353 | 0 | this->precision <= BITS_IN_JSAMPLE) |
354 | 0 | #endif |
355 | 0 | cinfo->data_precision = this->precision; |
356 | 0 | if ((src = _jinit_read_png(cinfo)) == NULL) |
357 | 0 | THROW("Could not initialize PNG loader"); |
358 | 0 | invert = this->bottomUp; |
359 | 0 | } else if (tempc == 'P') { |
360 | | #if BITS_IN_JSAMPLE == 8 |
361 | 0 | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
362 | | #else |
363 | 0 | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
364 | 0 | this->precision <= BITS_IN_JSAMPLE) |
365 | 0 | #endif |
366 | 0 | cinfo->data_precision = this->precision; |
367 | 0 | if ((src = _jinit_read_ppm(cinfo)) == NULL) |
368 | 0 | THROW("Could not initialize PPM loader"); |
369 | 0 | invert = this->bottomUp; |
370 | 0 | } else |
371 | 0 | THROW("Unsupported file type"); |
372 | |
|
373 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
374 | | /* If we get here, the JPEG code has signaled an error. */ |
375 | 0 | retval = -1; goto bailout; |
376 | 0 | } |
377 | | |
378 | 0 | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
379 | |
|
380 | 0 | src->input_file = file; |
381 | | /* Refuse to load images larger than the specified size. */ |
382 | 0 | src->max_pixels = this->maxPixels; |
383 | 0 | (*src->start_input) (cinfo, src); |
384 | 0 | if (tempc == 'B') { |
385 | 0 | if (cinfo->X_density && cinfo->Y_density) { |
386 | 0 | this->xDensity = cinfo->X_density; |
387 | 0 | this->yDensity = cinfo->Y_density; |
388 | 0 | this->densityUnits = cinfo->density_unit; |
389 | 0 | } |
390 | 0 | } else if (tempc == 0x89 && (this->init & COMPRESS) && |
391 | 0 | (this->saveMarkers == 2 || this->saveMarkers == 4)) { |
392 | 0 | JOCTET *iccBuf = NULL; |
393 | 0 | unsigned int iccLen = 0; |
394 | |
|
395 | 0 | if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf && |
396 | 0 | iccLen) |
397 | 0 | tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen); |
398 | 0 | } |
399 | 0 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
400 | |
|
401 | 0 | *width = cinfo->image_width; *height = cinfo->image_height; |
402 | 0 | *pixelFormat = cs2pf[cinfo->in_color_space]; |
403 | |
|
404 | 0 | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
405 | 0 | if ( |
406 | | #if ULLONG_MAX > SIZE_MAX |
407 | | (unsigned long long)pitch * (unsigned long long)(*height) > |
408 | | (unsigned long long)((size_t)-1) || |
409 | | #endif |
410 | 0 | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * |
411 | 0 | sizeof(_JSAMPLE))) == NULL) |
412 | 0 | THROW("Memory allocation failure"); |
413 | |
|
414 | 0 | CATCH_LIBJPEG(this2); |
415 | |
|
416 | 0 | while (cinfo->next_scanline < cinfo->image_height) { |
417 | 0 | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
418 | |
|
419 | 0 | for (i = 0; i < nlines; i++) { |
420 | 0 | _JSAMPLE *dstptr; |
421 | 0 | int row; |
422 | |
|
423 | 0 | row = cinfo->next_scanline + i; |
424 | 0 | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
425 | 0 | else dstptr = &dstBuf[row * pitch]; |
426 | 0 | memcpy(dstptr, src->_buffer[i], |
427 | 0 | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); |
428 | 0 | } |
429 | 0 | cinfo->next_scanline += nlines; |
430 | 0 | } |
431 | |
|
432 | 0 | bailout: |
433 | 0 | if (src) |
434 | 0 | (*src->finish_input) (cinfo, src); |
435 | 0 | tj3Destroy(handle2); |
436 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
437 | 0 | return dstBuf; |
438 | 0 | } Unexecuted instantiation: _tj3LoadImageFromFileHandle8 Unexecuted instantiation: _tj3LoadImageFromFileHandle12 Unexecuted instantiation: _tj3LoadImageFromFileHandle16 |
439 | | |
440 | | #endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
441 | | |
442 | | DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE) |
443 | | (tjhandle handle, const char *filename, int *width, int align, int *height, |
444 | | int *pixelFormat) |
445 | 0 | { |
446 | 0 | static const char FUNCTION_NAME[] = |
447 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
448 | |
|
449 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
450 | |
|
451 | 0 | int retval = 0; |
452 | 0 | _JSAMPLE *dstBuf = NULL; |
453 | 0 | FILE *file = NULL; |
454 | |
|
455 | 0 | GET_TJINSTANCE(handle, NULL) |
456 | |
|
457 | 0 | if (!filename) |
458 | 0 | THROW("Invalid argument"); |
459 | |
|
460 | | #ifdef _MSC_VER |
461 | | if (fopen_s(&file, filename, "rb") || file == NULL) |
462 | | #else |
463 | 0 | if ((file = fopen(filename, "rb")) == NULL) |
464 | 0 | #endif |
465 | 0 | THROW_UNIX("Cannot open input file"); |
466 | |
|
467 | 0 | dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
468 | 0 | (handle, file, width, align, height, pixelFormat); |
469 | |
|
470 | 0 | bailout: |
471 | 0 | if (file) fclose(file); |
472 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
473 | 0 | return dstBuf; |
474 | |
|
475 | | #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
476 | | |
477 | | static const char ERROR_MSG[] = |
478 | | "16-bit data precision requires lossless JPEG,\n" |
479 | | "which was disabled at build time."; |
480 | | _JSAMPLE *retval = NULL; |
481 | | |
482 | | GET_TJINSTANCE(handle, NULL) |
483 | | SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME, |
484 | | ERROR_MSG); |
485 | | this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL) |
486 | | |
487 | | bailout: |
488 | | return retval; |
489 | | |
490 | | #endif |
491 | 0 | } Unexecuted instantiation: tj3LoadImage8 Unexecuted instantiation: tj3LoadImage12 Unexecuted instantiation: tj3LoadImage16 |
492 | | |
493 | | |
494 | | /* TurboJPEG 3.0+ */ |
495 | | DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE) |
496 | | (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width, |
497 | | int pitch, int height, int pixelFormat) |
498 | 0 | { |
499 | 0 | static const char FUNCTION_NAME[] = |
500 | 0 | GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE); |
501 | 0 | int retval = 0; |
502 | |
|
503 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) |
504 | |
|
505 | 0 | tjhandle handle2 = NULL; |
506 | 0 | tjinstance *this2; |
507 | 0 | j_decompress_ptr dinfo = NULL; |
508 | 0 | djpeg_dest_ptr dst; |
509 | 0 | FILE *file = NULL; |
510 | 0 | const char *ptr = NULL; |
511 | 0 | boolean invert; |
512 | |
|
513 | 0 | GET_TJINSTANCE(handle, -1) |
514 | |
|
515 | 0 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
516 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
517 | 0 | THROW("Invalid argument"); |
518 | | |
519 | | /* The instance handle passed to this function is used only for parameter |
520 | | retrieval. Create a new temporary instance to avoid interfering with the |
521 | | libjpeg state of the primary instance. */ |
522 | 0 | if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
523 | 0 | return -1; |
524 | 0 | this2 = (tjinstance *)handle2; |
525 | 0 | dinfo = &this2->dinfo; |
526 | |
|
527 | | #ifdef _MSC_VER |
528 | | if (fopen_s(&file, filename, "wb") || file == NULL) |
529 | | #else |
530 | 0 | if ((file = fopen(filename, "wb")) == NULL) |
531 | 0 | #endif |
532 | 0 | THROW_UNIX("Cannot open output file"); |
533 | |
|
534 | 0 | CATCH_LIBJPEG(this2); |
535 | |
|
536 | 0 | this2->dinfo.out_color_space = pf2cs[pixelFormat]; |
537 | 0 | dinfo->image_width = width; dinfo->image_height = height; |
538 | 0 | dinfo->global_state = DSTATE_READY; |
539 | 0 | dinfo->scale_num = dinfo->scale_denom = 1; |
540 | 0 | dinfo->data_precision = BITS_IN_JSAMPLE; |
541 | |
|
542 | 0 | ptr = strrchr(filename, '.'); |
543 | 0 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
544 | 0 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
545 | 0 | THROW("Could not initialize bitmap writer"); |
546 | 0 | invert = !this->bottomUp; |
547 | 0 | dinfo->X_density = (UINT16)this->xDensity; |
548 | 0 | dinfo->Y_density = (UINT16)this->yDensity; |
549 | 0 | dinfo->density_unit = (UINT8)this->densityUnits; |
550 | 0 | } else if (ptr && !strcasecmp(ptr, ".png")) { |
551 | | #if BITS_IN_JSAMPLE == 8 |
552 | 0 | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
553 | | #else |
554 | 0 | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
555 | 0 | this->precision <= BITS_IN_JSAMPLE) |
556 | 0 | #endif |
557 | 0 | dinfo->data_precision = this->precision; |
558 | 0 | if ((dst = _jinit_write_png(dinfo)) == NULL) |
559 | 0 | THROW("Could not initialize PNG writer"); |
560 | 0 | invert = this->bottomUp; |
561 | |
|
562 | 0 | if ((this->init & DECOMPRESS) && this->decompICCBuf && |
563 | 0 | this->decompICCSize) { |
564 | 0 | unsigned char *iccBuf = (unsigned char *)malloc(this->decompICCSize); |
565 | |
|
566 | 0 | if (!iccBuf) |
567 | 0 | THROW("Memory allocation failure"); |
568 | 0 | memcpy(iccBuf, this->decompICCBuf, this->decompICCSize); |
569 | 0 | (*dst->write_icc_profile) (dinfo, dst, iccBuf, |
570 | 0 | (unsigned int)this->decompICCSize); |
571 | 0 | } |
572 | 0 | } else { |
573 | | #if BITS_IN_JSAMPLE == 8 |
574 | 0 | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
575 | | #else |
576 | 0 | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
577 | 0 | this->precision <= BITS_IN_JSAMPLE) |
578 | 0 | #endif |
579 | 0 | dinfo->data_precision = this->precision; |
580 | 0 | if ((dst = _jinit_write_ppm(dinfo)) == NULL) |
581 | 0 | THROW("Could not initialize PPM writer"); |
582 | 0 | invert = this->bottomUp; |
583 | 0 | } |
584 | | |
585 | 0 | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
586 | |
|
587 | 0 | dst->output_file = file; |
588 | 0 | (*dst->start_output) (dinfo, dst); |
589 | 0 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
590 | |
|
591 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
592 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
593 | 0 | THROW("Invalid argument"); |
594 | |
|
595 | 0 | while (dinfo->output_scanline < dinfo->output_height) { |
596 | 0 | _JSAMPLE *rowptr; |
597 | |
|
598 | 0 | if (invert) |
599 | 0 | rowptr = |
600 | 0 | (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch]; |
601 | 0 | else |
602 | 0 | rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch]; |
603 | 0 | memcpy(dst->_buffer[0], rowptr, |
604 | 0 | width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE)); |
605 | 0 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
606 | 0 | dinfo->output_scanline++; |
607 | 0 | } |
608 | |
|
609 | 0 | (*dst->finish_output) (dinfo, dst); |
610 | |
|
611 | 0 | bailout: |
612 | 0 | tj3Destroy(handle2); |
613 | 0 | if (file) fclose(file); |
614 | 0 | return retval; |
615 | |
|
616 | | #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */ |
617 | | |
618 | | GET_TJINSTANCE(handle, -1) |
619 | | THROW("16-bit data precision requires lossless JPEG,\n" |
620 | | "which was disabled at build time.") |
621 | | bailout: |
622 | | return retval; |
623 | | |
624 | | #endif |
625 | 0 | } Unexecuted instantiation: tj3SaveImage8 Unexecuted instantiation: tj3SaveImage12 Unexecuted instantiation: tj3SaveImage16 |
626 | | |
627 | | |
628 | | #undef _JSAMPLE |
629 | | #undef _JSAMPROW |
630 | | #undef _buffer |
631 | | #undef _jinit_read_png |
632 | | #undef _jinit_write_png |
633 | | #undef _jinit_read_ppm |
634 | | #undef _jinit_write_ppm |
635 | | #undef _jpeg_crop_scanline |
636 | | #undef _jpeg_read_scanlines |
637 | | #undef _jpeg_skip_scanlines |
638 | | #undef _jpeg_write_scanlines |