/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 | 16.4k | #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 | 979 | #define _jpeg_crop_scanline jpeg_crop_scanline |
41 | 11.0M | #define _jpeg_read_scanlines jpeg_read_scanlines |
42 | 1.94k | #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 | 12.5k | #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 | 918 | #define _jpeg_crop_scanline jpeg12_crop_scanline |
53 | 9.66M | #define _jpeg_read_scanlines jpeg12_read_scanlines |
54 | 1.82k | #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 | 4.00k | #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 | 1.18M | #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 | 33.0k | #define _GET_STRING(name, suffix) #name #suffix |
71 | 33.0k | #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 | 33.0k | { |
157 | 33.0k | static const char FUNCTION_NAME[] = |
158 | 33.0k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
159 | 33.0k | _JSAMPROW *row_pointer = NULL; |
160 | 33.0k | int croppedHeight, i, retval = 0; |
161 | | #if BITS_IN_JSAMPLE != 16 |
162 | | int scaledWidth; |
163 | | #endif |
164 | 33.0k | struct my_progress_mgr progress; |
165 | | |
166 | 33.0k | GET_DINSTANCE(handle); |
167 | 33.0k | if ((this->init & DECOMPRESS) == 0) |
168 | 33.0k | THROW("Instance has not been initialized for decompression"); |
169 | | |
170 | 33.0k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
171 | 33.0k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
172 | 33.0k | THROW("Invalid argument"); |
173 | | |
174 | 33.0k | if (this->scanLimit) { |
175 | 33.0k | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
176 | 33.0k | progress.pub.progress_monitor = my_progress_monitor; |
177 | 33.0k | progress.this = this; |
178 | 33.0k | dinfo->progress = &progress.pub; |
179 | 33.0k | } else |
180 | 0 | dinfo->progress = NULL; |
181 | | |
182 | 33.0k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
183 | | |
184 | 33.0k | CATCH_LIBJPEG(this); |
185 | | |
186 | 33.0k | if (dinfo->global_state <= DSTATE_INHEADER) { |
187 | 33.0k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
188 | 33.0k | jpeg_read_header(dinfo, TRUE); |
189 | 33.0k | } |
190 | 15.1k | setDecompParameters(this); |
191 | | #if BITS_IN_JSAMPLE == 12 |
192 | 6.72k | if (this->precision == 8 && !this->lossless) |
193 | 0 | dinfo->data_precision = 12; |
194 | | #endif |
195 | 15.1k | if (this->maxPixels && |
196 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > |
197 | 0 | (unsigned long long)this->maxPixels) |
198 | 15.1k | THROW("Image is too large"); |
199 | 15.1k | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
200 | | #if BITS_IN_JSAMPLE != 16 |
201 | 13.9k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
202 | | #endif |
203 | 15.1k | dinfo->do_fancy_upsampling = !this->fastUpsample; |
204 | 15.1k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
205 | | |
206 | 15.1k | dinfo->scale_num = this->scalingFactor.num; |
207 | 15.1k | dinfo->scale_denom = this->scalingFactor.denom; |
208 | | |
209 | 15.1k | jpeg_start_decompress(dinfo); |
210 | | |
211 | | #if BITS_IN_JSAMPLE != 16 |
212 | 13.9k | if (this->croppingRegion.x != 0 || |
213 | 12.0k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
214 | 1.89k | JDIMENSION crop_x = this->croppingRegion.x; |
215 | 1.89k | JDIMENSION crop_w = this->croppingRegion.w; |
216 | | |
217 | 1.89k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
218 | 1.89k | if ((int)crop_x != this->croppingRegion.x) |
219 | 6 | THROWI("Unexplained mismatch between specified (%d) and\n" |
220 | 1.89k | "actual (%d) cropping region left boundary", |
221 | 1.89k | this->croppingRegion.x, (int)crop_x); |
222 | 1.89k | if ((int)crop_w != this->croppingRegion.w) |
223 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
224 | 1.89k | "actual (%d) cropping region width", |
225 | 1.89k | this->croppingRegion.w, (int)crop_w); |
226 | 1.89k | } |
227 | 13.9k | #endif |
228 | | |
229 | 15.1k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
230 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) |
231 | 15.1k | THROW("Invalid argument"); |
232 | | |
233 | 15.1k | croppedHeight = dinfo->output_height; |
234 | | #if BITS_IN_JSAMPLE != 16 |
235 | 13.9k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
236 | 1.89k | croppedHeight = this->croppingRegion.h; |
237 | | #endif |
238 | 15.1k | if ((row_pointer = |
239 | 15.1k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
240 | 15.1k | THROW("Memory allocation failure"); |
241 | 15.1k | CATCH_LIBJPEG(this); |
242 | 54.2M | for (i = 0; i < (int)croppedHeight; i++) { |
243 | 54.1M | if (this->bottomUp) |
244 | 17.8M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
245 | 36.3M | else |
246 | 36.3M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
247 | 54.1M | } |
248 | | |
249 | | #if BITS_IN_JSAMPLE != 16 |
250 | 12.3k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
251 | 1.89k | if (this->croppingRegion.y != 0) { |
252 | 1.89k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
253 | | |
254 | 1.89k | if ((int)lines != this->croppingRegion.y) |
255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
256 | | "actual (%d) cropping region upper boundary", |
257 | 1.89k | this->croppingRegion.y, (int)lines); |
258 | 1.89k | } |
259 | 79.4k | while ((int)dinfo->output_scanline < |
260 | 79.4k | this->croppingRegion.y + this->croppingRegion.h) |
261 | 77.5k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
262 | 77.5k | this->croppingRegion.y], |
263 | 77.5k | this->croppingRegion.y + this->croppingRegion.h - |
264 | 77.5k | dinfo->output_scanline); |
265 | 1.89k | if (this->croppingRegion.y + this->croppingRegion.h != |
266 | 1.89k | (int)dinfo->output_height) { |
267 | 1.87k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
268 | | this->croppingRegion.y - |
269 | | this->croppingRegion.h); |
270 | | |
271 | 1.87k | if (lines != dinfo->output_height - this->croppingRegion.y - |
272 | 1.87k | this->croppingRegion.h) |
273 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
274 | 1.87k | "actual (%d) cropping region lower boundary", |
275 | 1.87k | this->croppingRegion.y + this->croppingRegion.h, |
276 | 1.87k | (int)(dinfo->output_height - lines)); |
277 | 1.87k | } |
278 | 1.89k | } else |
279 | 10.4k | #endif |
280 | 10.4k | { |
281 | 21.8M | while (dinfo->output_scanline < dinfo->output_height) |
282 | 21.8M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
283 | 21.8M | dinfo->output_height - dinfo->output_scanline); |
284 | 10.4k | } |
285 | 12.3k | jpeg_finish_decompress(dinfo); |
286 | | |
287 | 33.0k | bailout: |
288 | 33.0k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
289 | 33.0k | free(row_pointer); |
290 | 33.0k | if (this->jerr.warning) retval = -1; |
291 | 33.0k | return retval; |
292 | 12.3k | } Line | Count | Source | 156 | 16.4k | { | 157 | 16.4k | static const char FUNCTION_NAME[] = | 158 | 16.4k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 159 | 16.4k | _JSAMPROW *row_pointer = NULL; | 160 | 16.4k | int croppedHeight, i, retval = 0; | 161 | 16.4k | #if BITS_IN_JSAMPLE != 16 | 162 | 16.4k | int scaledWidth; | 163 | 16.4k | #endif | 164 | 16.4k | struct my_progress_mgr progress; | 165 | | | 166 | 16.4k | GET_DINSTANCE(handle); | 167 | 16.4k | if ((this->init & DECOMPRESS) == 0) | 168 | 16.4k | THROW("Instance has not been initialized for decompression"); | 169 | | | 170 | 16.4k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 171 | 16.4k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 172 | 16.4k | THROW("Invalid argument"); | 173 | | | 174 | 16.4k | if (this->scanLimit) { | 175 | 16.4k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 176 | 16.4k | progress.pub.progress_monitor = my_progress_monitor; | 177 | 16.4k | progress.this = this; | 178 | 16.4k | dinfo->progress = &progress.pub; | 179 | 16.4k | } else | 180 | 0 | dinfo->progress = NULL; | 181 | | | 182 | 16.4k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 183 | | | 184 | 16.4k | CATCH_LIBJPEG(this); | 185 | | | 186 | 16.4k | if (dinfo->global_state <= DSTATE_INHEADER) { | 187 | 16.4k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 188 | 16.4k | jpeg_read_header(dinfo, TRUE); | 189 | 16.4k | } | 190 | 7.20k | setDecompParameters(this); | 191 | | #if BITS_IN_JSAMPLE == 12 | 192 | | if (this->precision == 8 && !this->lossless) | 193 | | dinfo->data_precision = 12; | 194 | | #endif | 195 | 7.20k | if (this->maxPixels && | 196 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 197 | 0 | (unsigned long long)this->maxPixels) | 198 | 7.20k | THROW("Image is too large"); | 199 | 7.20k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 200 | 7.20k | #if BITS_IN_JSAMPLE != 16 | 201 | 7.20k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 202 | 7.20k | #endif | 203 | 7.20k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 204 | 7.20k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 205 | | | 206 | 7.20k | dinfo->scale_num = this->scalingFactor.num; | 207 | 7.20k | dinfo->scale_denom = this->scalingFactor.denom; | 208 | | | 209 | 7.20k | jpeg_start_decompress(dinfo); | 210 | | | 211 | 7.20k | #if BITS_IN_JSAMPLE != 16 | 212 | 7.20k | if (this->croppingRegion.x != 0 || | 213 | 6.22k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 214 | 979 | JDIMENSION crop_x = this->croppingRegion.x; | 215 | 979 | JDIMENSION crop_w = this->croppingRegion.w; | 216 | | | 217 | 979 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 218 | 979 | if ((int)crop_x != this->croppingRegion.x) | 219 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" | 220 | 979 | "actual (%d) cropping region left boundary", | 221 | 979 | this->croppingRegion.x, (int)crop_x); | 222 | 976 | if ((int)crop_w != this->croppingRegion.w) | 223 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 224 | 976 | "actual (%d) cropping region width", | 225 | 976 | this->croppingRegion.w, (int)crop_w); | 226 | 976 | } | 227 | 7.20k | #endif | 228 | | | 229 | 7.20k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 230 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 231 | 7.20k | THROW("Invalid argument"); | 232 | | | 233 | 7.20k | croppedHeight = dinfo->output_height; | 234 | 7.20k | #if BITS_IN_JSAMPLE != 16 | 235 | 7.20k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 236 | 976 | croppedHeight = this->croppingRegion.h; | 237 | 7.20k | #endif | 238 | 7.20k | if ((row_pointer = | 239 | 7.20k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 240 | 7.20k | THROW("Memory allocation failure"); | 241 | 7.20k | CATCH_LIBJPEG(this); | 242 | 22.5M | for (i = 0; i < (int)croppedHeight; i++) { | 243 | 22.5M | if (this->bottomUp) | 244 | 7.52M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 245 | 15.0M | else | 246 | 15.0M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 247 | 22.5M | } | 248 | | | 249 | 6.14k | #if BITS_IN_JSAMPLE != 16 | 250 | 6.22k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 251 | 976 | if (this->croppingRegion.y != 0) { | 252 | 976 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 253 | | | 254 | 976 | if ((int)lines != this->croppingRegion.y) | 255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | 976 | "actual (%d) cropping region upper boundary", | 257 | 976 | this->croppingRegion.y, (int)lines); | 258 | 976 | } | 259 | 42.0k | while ((int)dinfo->output_scanline < | 260 | 42.0k | this->croppingRegion.y + this->croppingRegion.h) | 261 | 41.0k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 262 | 41.0k | this->croppingRegion.y], | 263 | 41.0k | this->croppingRegion.y + this->croppingRegion.h - | 264 | 41.0k | dinfo->output_scanline); | 265 | 976 | if (this->croppingRegion.y + this->croppingRegion.h != | 266 | 976 | (int)dinfo->output_height) { | 267 | 967 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 268 | 967 | this->croppingRegion.y - | 269 | 967 | this->croppingRegion.h); | 270 | | | 271 | 967 | if (lines != dinfo->output_height - this->croppingRegion.y - | 272 | 967 | this->croppingRegion.h) | 273 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 274 | 967 | "actual (%d) cropping region lower boundary", | 275 | 967 | this->croppingRegion.y + this->croppingRegion.h, | 276 | 967 | (int)(dinfo->output_height - lines)); | 277 | 967 | } | 278 | 976 | } else | 279 | 5.17k | #endif | 280 | 5.17k | { | 281 | 11.0M | while (dinfo->output_scanline < dinfo->output_height) | 282 | 11.0M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 283 | 11.0M | dinfo->output_height - dinfo->output_scanline); | 284 | 5.17k | } | 285 | 6.14k | jpeg_finish_decompress(dinfo); | 286 | | | 287 | 16.4k | bailout: | 288 | 16.4k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 289 | 16.4k | free(row_pointer); | 290 | 16.4k | if (this->jerr.warning) retval = -1; | 291 | 16.4k | return retval; | 292 | 6.14k | } |
Line | Count | Source | 156 | 12.5k | { | 157 | 12.5k | static const char FUNCTION_NAME[] = | 158 | 12.5k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 159 | 12.5k | _JSAMPROW *row_pointer = NULL; | 160 | 12.5k | int croppedHeight, i, retval = 0; | 161 | 12.5k | #if BITS_IN_JSAMPLE != 16 | 162 | 12.5k | int scaledWidth; | 163 | 12.5k | #endif | 164 | 12.5k | struct my_progress_mgr progress; | 165 | | | 166 | 12.5k | GET_DINSTANCE(handle); | 167 | 12.5k | if ((this->init & DECOMPRESS) == 0) | 168 | 12.5k | THROW("Instance has not been initialized for decompression"); | 169 | | | 170 | 12.5k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 171 | 12.5k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 172 | 12.5k | THROW("Invalid argument"); | 173 | | | 174 | 12.5k | if (this->scanLimit) { | 175 | 12.5k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 176 | 12.5k | progress.pub.progress_monitor = my_progress_monitor; | 177 | 12.5k | progress.this = this; | 178 | 12.5k | dinfo->progress = &progress.pub; | 179 | 12.5k | } else | 180 | 0 | dinfo->progress = NULL; | 181 | | | 182 | 12.5k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 183 | | | 184 | 12.5k | CATCH_LIBJPEG(this); | 185 | | | 186 | 12.5k | if (dinfo->global_state <= DSTATE_INHEADER) { | 187 | 12.5k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 188 | 12.5k | jpeg_read_header(dinfo, TRUE); | 189 | 12.5k | } | 190 | 6.72k | setDecompParameters(this); | 191 | 6.72k | #if BITS_IN_JSAMPLE == 12 | 192 | 6.72k | if (this->precision == 8 && !this->lossless) | 193 | 0 | dinfo->data_precision = 12; | 194 | 6.72k | #endif | 195 | 6.72k | if (this->maxPixels && | 196 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 197 | 0 | (unsigned long long)this->maxPixels) | 198 | 6.72k | THROW("Image is too large"); | 199 | 6.72k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 200 | 6.72k | #if BITS_IN_JSAMPLE != 16 | 201 | 6.72k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 202 | 6.72k | #endif | 203 | 6.72k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 204 | 6.72k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 205 | | | 206 | 6.72k | dinfo->scale_num = this->scalingFactor.num; | 207 | 6.72k | dinfo->scale_denom = this->scalingFactor.denom; | 208 | | | 209 | 6.72k | jpeg_start_decompress(dinfo); | 210 | | | 211 | 6.72k | #if BITS_IN_JSAMPLE != 16 | 212 | 6.72k | if (this->croppingRegion.x != 0 || | 213 | 5.81k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 214 | 918 | JDIMENSION crop_x = this->croppingRegion.x; | 215 | 918 | JDIMENSION crop_w = this->croppingRegion.w; | 216 | | | 217 | 918 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 218 | 918 | if ((int)crop_x != this->croppingRegion.x) | 219 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" | 220 | 918 | "actual (%d) cropping region left boundary", | 221 | 918 | this->croppingRegion.x, (int)crop_x); | 222 | 915 | if ((int)crop_w != this->croppingRegion.w) | 223 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 224 | 915 | "actual (%d) cropping region width", | 225 | 915 | this->croppingRegion.w, (int)crop_w); | 226 | 915 | } | 227 | 6.72k | #endif | 228 | | | 229 | 6.72k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 230 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 231 | 6.72k | THROW("Invalid argument"); | 232 | | | 233 | 6.72k | croppedHeight = dinfo->output_height; | 234 | 6.72k | #if BITS_IN_JSAMPLE != 16 | 235 | 6.72k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 236 | 915 | croppedHeight = this->croppingRegion.h; | 237 | 6.72k | #endif | 238 | 6.72k | if ((row_pointer = | 239 | 6.72k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 240 | 6.72k | THROW("Memory allocation failure"); | 241 | 6.72k | CATCH_LIBJPEG(this); | 242 | 21.1M | for (i = 0; i < (int)croppedHeight; i++) { | 243 | 21.0M | if (this->bottomUp) | 244 | 7.01M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 245 | 14.0M | else | 246 | 14.0M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 247 | 21.0M | } | 248 | | | 249 | 6.19k | #if BITS_IN_JSAMPLE != 16 | 250 | 6.19k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 251 | 915 | if (this->croppingRegion.y != 0) { | 252 | 915 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 253 | | | 254 | 915 | if ((int)lines != this->croppingRegion.y) | 255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | 915 | "actual (%d) cropping region upper boundary", | 257 | 915 | this->croppingRegion.y, (int)lines); | 258 | 915 | } | 259 | 37.4k | while ((int)dinfo->output_scanline < | 260 | 37.4k | this->croppingRegion.y + this->croppingRegion.h) | 261 | 36.5k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 262 | 36.5k | this->croppingRegion.y], | 263 | 36.5k | this->croppingRegion.y + this->croppingRegion.h - | 264 | 36.5k | dinfo->output_scanline); | 265 | 915 | if (this->croppingRegion.y + this->croppingRegion.h != | 266 | 915 | (int)dinfo->output_height) { | 267 | 908 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 268 | 908 | this->croppingRegion.y - | 269 | 908 | this->croppingRegion.h); | 270 | | | 271 | 908 | if (lines != dinfo->output_height - this->croppingRegion.y - | 272 | 908 | this->croppingRegion.h) | 273 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 274 | 908 | "actual (%d) cropping region lower boundary", | 275 | 908 | this->croppingRegion.y + this->croppingRegion.h, | 276 | 908 | (int)(dinfo->output_height - lines)); | 277 | 908 | } | 278 | 915 | } else | 279 | 5.28k | #endif | 280 | 5.28k | { | 281 | 9.63M | while (dinfo->output_scanline < dinfo->output_height) | 282 | 9.62M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 283 | 9.62M | dinfo->output_height - dinfo->output_scanline); | 284 | 5.28k | } | 285 | 6.19k | jpeg_finish_decompress(dinfo); | 286 | | | 287 | 12.5k | bailout: | 288 | 12.5k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 289 | 12.5k | free(row_pointer); | 290 | 12.5k | if (this->jerr.warning) retval = -1; | 291 | 12.5k | return retval; | 292 | 6.19k | } |
Line | Count | Source | 156 | 4.00k | { | 157 | 4.00k | static const char FUNCTION_NAME[] = | 158 | 4.00k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 159 | 4.00k | _JSAMPROW *row_pointer = NULL; | 160 | 4.00k | int croppedHeight, i, retval = 0; | 161 | | #if BITS_IN_JSAMPLE != 16 | 162 | | int scaledWidth; | 163 | | #endif | 164 | 4.00k | struct my_progress_mgr progress; | 165 | | | 166 | 4.00k | GET_DINSTANCE(handle); | 167 | 4.00k | if ((this->init & DECOMPRESS) == 0) | 168 | 4.00k | THROW("Instance has not been initialized for decompression"); | 169 | | | 170 | 4.00k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 171 | 4.00k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 172 | 4.00k | THROW("Invalid argument"); | 173 | | | 174 | 4.00k | if (this->scanLimit) { | 175 | 4.00k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 176 | 4.00k | progress.pub.progress_monitor = my_progress_monitor; | 177 | 4.00k | progress.this = this; | 178 | 4.00k | dinfo->progress = &progress.pub; | 179 | 4.00k | } else | 180 | 0 | dinfo->progress = NULL; | 181 | | | 182 | 4.00k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 183 | | | 184 | 4.00k | CATCH_LIBJPEG(this); | 185 | | | 186 | 4.00k | if (dinfo->global_state <= DSTATE_INHEADER) { | 187 | 4.00k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 188 | 4.00k | jpeg_read_header(dinfo, TRUE); | 189 | 4.00k | } | 190 | 1.20k | setDecompParameters(this); | 191 | | #if BITS_IN_JSAMPLE == 12 | 192 | | if (this->precision == 8 && !this->lossless) | 193 | | dinfo->data_precision = 12; | 194 | | #endif | 195 | 1.20k | if (this->maxPixels && | 196 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 197 | 0 | (unsigned long long)this->maxPixels) | 198 | 1.20k | THROW("Image is too large"); | 199 | 1.20k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 200 | | #if BITS_IN_JSAMPLE != 16 | 201 | | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 202 | | #endif | 203 | 1.20k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 204 | 1.20k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 205 | | | 206 | 1.20k | dinfo->scale_num = this->scalingFactor.num; | 207 | 1.20k | dinfo->scale_denom = this->scalingFactor.denom; | 208 | | | 209 | 1.20k | jpeg_start_decompress(dinfo); | 210 | | | 211 | | #if BITS_IN_JSAMPLE != 16 | 212 | | if (this->croppingRegion.x != 0 || | 213 | | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 214 | | JDIMENSION crop_x = this->croppingRegion.x; | 215 | | JDIMENSION crop_w = this->croppingRegion.w; | 216 | | | 217 | | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 218 | | if ((int)crop_x != this->croppingRegion.x) | 219 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 220 | | "actual (%d) cropping region left boundary", | 221 | | this->croppingRegion.x, (int)crop_x); | 222 | | if ((int)crop_w != this->croppingRegion.w) | 223 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 224 | | "actual (%d) cropping region width", | 225 | | this->croppingRegion.w, (int)crop_w); | 226 | | } | 227 | | #endif | 228 | | | 229 | 1.20k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 230 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 231 | 1.20k | THROW("Invalid argument"); | 232 | | | 233 | 1.20k | croppedHeight = dinfo->output_height; | 234 | | #if BITS_IN_JSAMPLE != 16 | 235 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 236 | | croppedHeight = this->croppingRegion.h; | 237 | | #endif | 238 | 1.20k | if ((row_pointer = | 239 | 1.20k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 240 | 1.20k | THROW("Memory allocation failure"); | 241 | 1.20k | CATCH_LIBJPEG(this); | 242 | 10.5M | for (i = 0; i < (int)croppedHeight; i++) { | 243 | 10.5M | if (this->bottomUp) | 244 | 3.26M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 245 | 7.29M | else | 246 | 7.29M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 247 | 10.5M | } | 248 | | | 249 | | #if BITS_IN_JSAMPLE != 16 | 250 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 251 | | if (this->croppingRegion.y != 0) { | 252 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 253 | | | 254 | | if ((int)lines != this->croppingRegion.y) | 255 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | | "actual (%d) cropping region upper boundary", | 257 | | this->croppingRegion.y, (int)lines); | 258 | | } | 259 | | while ((int)dinfo->output_scanline < | 260 | | this->croppingRegion.y + this->croppingRegion.h) | 261 | | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 262 | | this->croppingRegion.y], | 263 | | this->croppingRegion.y + this->croppingRegion.h - | 264 | | dinfo->output_scanline); | 265 | | if (this->croppingRegion.y + this->croppingRegion.h != | 266 | | (int)dinfo->output_height) { | 267 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 268 | | this->croppingRegion.y - | 269 | | this->croppingRegion.h); | 270 | | | 271 | | if (lines != dinfo->output_height - this->croppingRegion.y - | 272 | | this->croppingRegion.h) | 273 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 274 | | "actual (%d) cropping region lower boundary", | 275 | | this->croppingRegion.y + this->croppingRegion.h, | 276 | | (int)(dinfo->output_height - lines)); | 277 | | } | 278 | | } else | 279 | | #endif | 280 | 409 | { | 281 | 1.18M | while (dinfo->output_scanline < dinfo->output_height) | 282 | 1.18M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 283 | 1.18M | dinfo->output_height - dinfo->output_scanline); | 284 | 409 | } | 285 | 409 | jpeg_finish_decompress(dinfo); | 286 | | | 287 | 4.00k | bailout: | 288 | 4.00k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 289 | 4.00k | free(row_pointer); | 290 | 4.00k | if (this->jerr.warning) retval = -1; | 291 | 4.00k | return retval; | 292 | 409 | } |
|
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 |