/src/libjpeg-turbo.main/turbojpeg-mp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C)2009-2023 D. R. Commander. All Rights Reserved. |
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 | 50.7k | #define _JSAMPROW JSAMPROW |
35 | 0 | #define _buffer buffer |
36 | 0 | #define _jinit_read_ppm jinit_read_ppm |
37 | 0 | #define _jinit_write_ppm jinit_write_ppm |
38 | 0 | #define _jpeg_crop_scanline jpeg_crop_scanline |
39 | 20.3M | #define _jpeg_read_scanlines jpeg_read_scanlines |
40 | 0 | #define _jpeg_skip_scanlines jpeg_skip_scanlines |
41 | 0 | #define _jpeg_write_scanlines jpeg_write_scanlines |
42 | | #elif BITS_IN_JSAMPLE == 12 |
43 | 0 | #define _JSAMPLE J12SAMPLE |
44 | 51.9k | #define _JSAMPROW J12SAMPROW |
45 | 0 | #define _buffer buffer12 |
46 | 0 | #define _jinit_read_ppm j12init_read_ppm |
47 | 0 | #define _jinit_write_ppm j12init_write_ppm |
48 | 0 | #define _jpeg_crop_scanline jpeg12_crop_scanline |
49 | 24.9M | #define _jpeg_read_scanlines jpeg12_read_scanlines |
50 | 0 | #define _jpeg_skip_scanlines jpeg12_skip_scanlines |
51 | 0 | #define _jpeg_write_scanlines jpeg12_write_scanlines |
52 | | #elif BITS_IN_JSAMPLE == 16 |
53 | 0 | #define _JSAMPLE J16SAMPLE |
54 | 11.1k | #define _JSAMPROW J16SAMPROW |
55 | 0 | #define _buffer buffer16 |
56 | 0 | #define _jinit_read_ppm j16init_read_ppm |
57 | 0 | #define _jinit_write_ppm j16init_write_ppm |
58 | 7.54M | #define _jpeg_read_scanlines jpeg16_read_scanlines |
59 | 0 | #define _jpeg_write_scanlines jpeg16_write_scanlines |
60 | | #endif |
61 | | |
62 | | #define _GET_NAME(name, suffix) name##suffix |
63 | | #define GET_NAME(name, suffix) _GET_NAME(name, suffix) |
64 | 113k | #define _GET_STRING(name, suffix) #name #suffix |
65 | 113k | #define GET_STRING(name, suffix) _GET_STRING(name, suffix) |
66 | | |
67 | | |
68 | | /******************************** Compressor *********************************/ |
69 | | |
70 | | /* TurboJPEG 3+ */ |
71 | | DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE) |
72 | | (tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height, |
73 | | int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize) |
74 | 0 | { |
75 | 0 | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); |
76 | 0 | int i, retval = 0; |
77 | 0 | boolean alloc = TRUE; |
78 | 0 | _JSAMPROW *row_pointer = NULL; |
79 | |
|
80 | 0 | GET_CINSTANCE(handle) |
81 | 0 | if ((this->init & COMPRESS) == 0) |
82 | 0 | THROW("Instance has not been initialized for compression"); |
83 | |
|
84 | 0 | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
85 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || |
86 | 0 | jpegSize == NULL) |
87 | 0 | THROW("Invalid argument"); |
88 | |
|
89 | 0 | if (!this->lossless && this->quality == -1) |
90 | 0 | THROW("TJPARAM_QUALITY must be specified"); |
91 | 0 | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) |
92 | 0 | THROW("TJPARAM_SUBSAMP must be specified"); |
93 | |
|
94 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
95 | |
|
96 | 0 | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) |
97 | 0 | THROW("Memory allocation failure"); |
98 | |
|
99 | 0 | if (setjmp(this->jerr.setjmp_buffer)) { |
100 | | /* If we get here, the JPEG code has signaled an error. */ |
101 | 0 | retval = -1; goto bailout; |
102 | 0 | } |
103 | | |
104 | 0 | cinfo->image_width = width; |
105 | 0 | cinfo->image_height = height; |
106 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
107 | |
|
108 | 0 | setCompDefaults(this, pixelFormat); |
109 | 0 | if (this->noRealloc) { |
110 | 0 | alloc = FALSE; |
111 | 0 | *jpegSize = tj3JPEGBufSize(width, height, this->subsamp); |
112 | 0 | } |
113 | 0 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
114 | |
|
115 | 0 | jpeg_start_compress(cinfo, TRUE); |
116 | 0 | for (i = 0; i < height; i++) { |
117 | 0 | if (this->bottomUp) |
118 | 0 | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; |
119 | 0 | else |
120 | 0 | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; |
121 | 0 | } |
122 | 0 | while (cinfo->next_scanline < cinfo->image_height) |
123 | 0 | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
124 | 0 | cinfo->image_height - cinfo->next_scanline); |
125 | 0 | jpeg_finish_compress(cinfo); |
126 | |
|
127 | 0 | bailout: |
128 | 0 | if (cinfo->global_state > CSTATE_START) { |
129 | 0 | if (alloc) (*cinfo->dest->term_destination) (cinfo); |
130 | 0 | jpeg_abort_compress(cinfo); |
131 | 0 | } |
132 | 0 | free(row_pointer); |
133 | 0 | if (this->jerr.warning) retval = -1; |
134 | 0 | return retval; |
135 | 0 | } Unexecuted instantiation: tj3Compress8 Unexecuted instantiation: tj3Compress12 Unexecuted instantiation: tj3Compress16 |
136 | | |
137 | | |
138 | | /******************************* Decompressor ********************************/ |
139 | | |
140 | | /* TurboJPEG 3+ */ |
141 | | DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE) |
142 | | (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize, |
143 | | _JSAMPLE *dstBuf, int pitch, int pixelFormat) |
144 | 113k | { |
145 | 113k | static const char FUNCTION_NAME[] = |
146 | 113k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
147 | 113k | _JSAMPROW *row_pointer = NULL; |
148 | 113k | int croppedHeight, i, retval = 0; |
149 | | #if BITS_IN_JSAMPLE != 16 |
150 | | int scaledWidth; |
151 | | #endif |
152 | 113k | struct my_progress_mgr progress; |
153 | | |
154 | 113k | GET_DINSTANCE(handle); |
155 | 113k | if ((this->init & DECOMPRESS) == 0) |
156 | 113k | THROW("Instance has not been initialized for decompression"); |
157 | | |
158 | 113k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
159 | 113k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
160 | 113k | THROW("Invalid argument"); |
161 | | |
162 | 113k | if (this->scanLimit) { |
163 | 113k | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
164 | 113k | progress.pub.progress_monitor = my_progress_monitor; |
165 | 113k | progress.this = this; |
166 | 113k | dinfo->progress = &progress.pub; |
167 | 113k | } else |
168 | 0 | dinfo->progress = NULL; |
169 | | |
170 | 113k | if (setjmp(this->jerr.setjmp_buffer)) { |
171 | | /* If we get here, the JPEG code has signaled an error. */ |
172 | 25.9k | retval = -1; goto bailout; |
173 | 25.9k | } |
174 | | |
175 | 113k | if (dinfo->global_state <= DSTATE_START) { |
176 | 113k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
177 | 113k | jpeg_read_header(dinfo, TRUE); |
178 | 113k | } |
179 | 87.8k | setDecompParameters(this); |
180 | 87.8k | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
181 | | #if BITS_IN_JSAMPLE != 16 |
182 | 78.2k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
183 | | #endif |
184 | 87.8k | dinfo->do_fancy_upsampling = !this->fastUpsample; |
185 | 87.8k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
186 | | |
187 | 87.8k | dinfo->scale_num = this->scalingFactor.num; |
188 | 87.8k | dinfo->scale_denom = this->scalingFactor.denom; |
189 | | |
190 | 87.8k | jpeg_start_decompress(dinfo); |
191 | | |
192 | | #if BITS_IN_JSAMPLE != 16 |
193 | 78.2k | if (this->croppingRegion.x != 0 || |
194 | 78.2k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
195 | 0 | JDIMENSION crop_x = this->croppingRegion.x; |
196 | 0 | JDIMENSION crop_w = this->croppingRegion.w; |
197 | |
|
198 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
199 | 0 | if ((int)crop_x != this->croppingRegion.x) |
200 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
201 | 0 | "actual (%d) cropping region left boundary", |
202 | 0 | this->croppingRegion.x, (int)crop_x); |
203 | 0 | if ((int)crop_w != this->croppingRegion.w) |
204 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
205 | 0 | "actual (%d) cropping region width", |
206 | 0 | this->croppingRegion.w, (int)crop_w); |
207 | 0 | } |
208 | 78.2k | #endif |
209 | | |
210 | 87.8k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
211 | | |
212 | 78.2k | croppedHeight = dinfo->output_height; |
213 | | #if BITS_IN_JSAMPLE != 16 |
214 | 78.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
215 | 0 | croppedHeight = this->croppingRegion.h; |
216 | | #endif |
217 | 87.8k | if ((row_pointer = |
218 | 87.8k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
219 | 87.8k | THROW("Memory allocation failure"); |
220 | 87.8k | if (setjmp(this->jerr.setjmp_buffer)) { |
221 | | /* If we get here, the JPEG code has signaled an error. */ |
222 | 24.9k | retval = -1; goto bailout; |
223 | 24.9k | } |
224 | 118M | for (i = 0; i < (int)croppedHeight; i++) { |
225 | 118M | if (this->bottomUp) |
226 | 52.5M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
227 | 66.2M | else |
228 | 66.2M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
229 | 118M | } |
230 | | |
231 | | #if BITS_IN_JSAMPLE != 16 |
232 | 78.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
233 | 0 | if (this->croppingRegion.y != 0) { |
234 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
235 | | |
236 | 0 | if ((int)lines != this->croppingRegion.y) |
237 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
238 | | "actual (%d) cropping region upper boundary", |
239 | 0 | this->croppingRegion.y, (int)lines); |
240 | 0 | } |
241 | 0 | while ((int)dinfo->output_scanline < |
242 | 0 | this->croppingRegion.y + this->croppingRegion.h) |
243 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
244 | 0 | this->croppingRegion.y], |
245 | 0 | this->croppingRegion.y + this->croppingRegion.h - |
246 | 0 | dinfo->output_scanline); |
247 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != |
248 | 0 | (int)dinfo->output_height) { |
249 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
250 | | this->croppingRegion.y - |
251 | | this->croppingRegion.h); |
252 | |
|
253 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - |
254 | 0 | this->croppingRegion.h) |
255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
256 | 0 | "actual (%d) cropping region lower boundary", |
257 | 0 | this->croppingRegion.y + this->croppingRegion.h, |
258 | 0 | (int)(dinfo->output_height - lines)); |
259 | 0 | } |
260 | 0 | } else |
261 | 60.4k | #endif |
262 | 60.4k | { |
263 | 52.9M | while (dinfo->output_scanline < dinfo->output_height) |
264 | 52.8M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
265 | 52.8M | dinfo->output_height - dinfo->output_scanline); |
266 | 60.4k | } |
267 | 60.4k | jpeg_finish_decompress(dinfo); |
268 | | |
269 | 113k | bailout: |
270 | 113k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
271 | 113k | free(row_pointer); |
272 | 113k | if (this->jerr.warning) retval = -1; |
273 | 113k | return retval; |
274 | 60.4k | } Line | Count | Source | 144 | 50.7k | { | 145 | 50.7k | static const char FUNCTION_NAME[] = | 146 | 50.7k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 50.7k | _JSAMPROW *row_pointer = NULL; | 148 | 50.7k | int croppedHeight, i, retval = 0; | 149 | 50.7k | #if BITS_IN_JSAMPLE != 16 | 150 | 50.7k | int scaledWidth; | 151 | 50.7k | #endif | 152 | 50.7k | struct my_progress_mgr progress; | 153 | | | 154 | 50.7k | GET_DINSTANCE(handle); | 155 | 50.7k | if ((this->init & DECOMPRESS) == 0) | 156 | 50.7k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 50.7k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 50.7k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 50.7k | THROW("Invalid argument"); | 161 | | | 162 | 50.7k | if (this->scanLimit) { | 163 | 50.7k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 50.7k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 50.7k | progress.this = this; | 166 | 50.7k | dinfo->progress = &progress.pub; | 167 | 50.7k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 50.7k | if (setjmp(this->jerr.setjmp_buffer)) { | 171 | | /* If we get here, the JPEG code has signaled an error. */ | 172 | 14.2k | retval = -1; goto bailout; | 173 | 14.2k | } | 174 | | | 175 | 50.7k | if (dinfo->global_state <= DSTATE_START) { | 176 | 50.7k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 177 | 50.7k | jpeg_read_header(dinfo, TRUE); | 178 | 50.7k | } | 179 | 36.4k | setDecompParameters(this); | 180 | 36.4k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 181 | 36.4k | #if BITS_IN_JSAMPLE != 16 | 182 | 36.4k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 183 | 36.4k | #endif | 184 | 36.4k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 185 | 36.4k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 186 | | | 187 | 36.4k | dinfo->scale_num = this->scalingFactor.num; | 188 | 36.4k | dinfo->scale_denom = this->scalingFactor.denom; | 189 | | | 190 | 36.4k | jpeg_start_decompress(dinfo); | 191 | | | 192 | 36.4k | #if BITS_IN_JSAMPLE != 16 | 193 | 36.4k | if (this->croppingRegion.x != 0 || | 194 | 36.4k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 195 | 0 | JDIMENSION crop_x = this->croppingRegion.x; | 196 | 0 | JDIMENSION crop_w = this->croppingRegion.w; | 197 | |
| 198 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 199 | 0 | if ((int)crop_x != this->croppingRegion.x) | 200 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 201 | 0 | "actual (%d) cropping region left boundary", | 202 | 0 | this->croppingRegion.x, (int)crop_x); | 203 | 0 | if ((int)crop_w != this->croppingRegion.w) | 204 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 205 | 0 | "actual (%d) cropping region width", | 206 | 0 | this->croppingRegion.w, (int)crop_w); | 207 | 0 | } | 208 | 36.4k | #endif | 209 | | | 210 | 36.4k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 211 | | | 212 | 36.4k | croppedHeight = dinfo->output_height; | 213 | 36.4k | #if BITS_IN_JSAMPLE != 16 | 214 | 36.4k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 215 | 0 | croppedHeight = this->croppingRegion.h; | 216 | 36.4k | #endif | 217 | 36.4k | if ((row_pointer = | 218 | 36.4k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 219 | 36.4k | THROW("Memory allocation failure"); | 220 | 36.4k | if (setjmp(this->jerr.setjmp_buffer)) { | 221 | | /* If we get here, the JPEG code has signaled an error. */ | 222 | 8.55k | retval = -1; goto bailout; | 223 | 8.55k | } | 224 | 46.5M | for (i = 0; i < (int)croppedHeight; i++) { | 225 | 46.4M | if (this->bottomUp) | 226 | 23.5M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 227 | 22.8M | else | 228 | 22.8M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 229 | 46.4M | } | 230 | | | 231 | 27.9k | #if BITS_IN_JSAMPLE != 16 | 232 | 36.4k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 233 | 0 | if (this->croppingRegion.y != 0) { | 234 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 235 | |
| 236 | 0 | if ((int)lines != this->croppingRegion.y) | 237 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 238 | 0 | "actual (%d) cropping region upper boundary", | 239 | 0 | this->croppingRegion.y, (int)lines); | 240 | 0 | } | 241 | 0 | while ((int)dinfo->output_scanline < | 242 | 0 | this->croppingRegion.y + this->croppingRegion.h) | 243 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 244 | 0 | this->croppingRegion.y], | 245 | 0 | this->croppingRegion.y + this->croppingRegion.h - | 246 | 0 | dinfo->output_scanline); | 247 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != | 248 | 0 | (int)dinfo->output_height) { | 249 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 250 | 0 | this->croppingRegion.y - | 251 | 0 | this->croppingRegion.h); | 252 | |
| 253 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - | 254 | 0 | this->croppingRegion.h) | 255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | 0 | "actual (%d) cropping region lower boundary", | 257 | 0 | this->croppingRegion.y + this->croppingRegion.h, | 258 | 0 | (int)(dinfo->output_height - lines)); | 259 | 0 | } | 260 | 0 | } else | 261 | 27.9k | #endif | 262 | 27.9k | { | 263 | 20.3M | while (dinfo->output_scanline < dinfo->output_height) | 264 | 20.3M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 265 | 20.3M | dinfo->output_height - dinfo->output_scanline); | 266 | 27.9k | } | 267 | 27.9k | jpeg_finish_decompress(dinfo); | 268 | | | 269 | 50.7k | bailout: | 270 | 50.7k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 271 | 50.7k | free(row_pointer); | 272 | 50.7k | if (this->jerr.warning) retval = -1; | 273 | 50.7k | return retval; | 274 | 27.9k | } |
Line | Count | Source | 144 | 51.9k | { | 145 | 51.9k | static const char FUNCTION_NAME[] = | 146 | 51.9k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 51.9k | _JSAMPROW *row_pointer = NULL; | 148 | 51.9k | int croppedHeight, i, retval = 0; | 149 | 51.9k | #if BITS_IN_JSAMPLE != 16 | 150 | 51.9k | int scaledWidth; | 151 | 51.9k | #endif | 152 | 51.9k | struct my_progress_mgr progress; | 153 | | | 154 | 51.9k | GET_DINSTANCE(handle); | 155 | 51.9k | if ((this->init & DECOMPRESS) == 0) | 156 | 51.9k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 51.9k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 51.9k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 51.9k | THROW("Invalid argument"); | 161 | | | 162 | 51.9k | if (this->scanLimit) { | 163 | 51.9k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 51.9k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 51.9k | progress.this = this; | 166 | 51.9k | dinfo->progress = &progress.pub; | 167 | 51.9k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 51.9k | if (setjmp(this->jerr.setjmp_buffer)) { | 171 | | /* If we get here, the JPEG code has signaled an error. */ | 172 | 10.1k | retval = -1; goto bailout; | 173 | 10.1k | } | 174 | | | 175 | 51.9k | if (dinfo->global_state <= DSTATE_START) { | 176 | 51.9k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 177 | 51.9k | jpeg_read_header(dinfo, TRUE); | 178 | 51.9k | } | 179 | 41.7k | setDecompParameters(this); | 180 | 41.7k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 181 | 41.7k | #if BITS_IN_JSAMPLE != 16 | 182 | 41.7k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 183 | 41.7k | #endif | 184 | 41.7k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 185 | 41.7k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 186 | | | 187 | 41.7k | dinfo->scale_num = this->scalingFactor.num; | 188 | 41.7k | dinfo->scale_denom = this->scalingFactor.denom; | 189 | | | 190 | 41.7k | jpeg_start_decompress(dinfo); | 191 | | | 192 | 41.7k | #if BITS_IN_JSAMPLE != 16 | 193 | 41.7k | if (this->croppingRegion.x != 0 || | 194 | 41.7k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 195 | 0 | JDIMENSION crop_x = this->croppingRegion.x; | 196 | 0 | JDIMENSION crop_w = this->croppingRegion.w; | 197 | |
| 198 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 199 | 0 | if ((int)crop_x != this->croppingRegion.x) | 200 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 201 | 0 | "actual (%d) cropping region left boundary", | 202 | 0 | this->croppingRegion.x, (int)crop_x); | 203 | 0 | if ((int)crop_w != this->croppingRegion.w) | 204 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 205 | 0 | "actual (%d) cropping region width", | 206 | 0 | this->croppingRegion.w, (int)crop_w); | 207 | 0 | } | 208 | 41.7k | #endif | 209 | | | 210 | 41.7k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 211 | | | 212 | 41.7k | croppedHeight = dinfo->output_height; | 213 | 41.7k | #if BITS_IN_JSAMPLE != 16 | 214 | 41.7k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 215 | 0 | croppedHeight = this->croppingRegion.h; | 216 | 41.7k | #endif | 217 | 41.7k | if ((row_pointer = | 218 | 41.7k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 219 | 41.7k | THROW("Memory allocation failure"); | 220 | 41.7k | if (setjmp(this->jerr.setjmp_buffer)) { | 221 | | /* If we get here, the JPEG code has signaled an error. */ | 222 | 9.24k | retval = -1; goto bailout; | 223 | 9.24k | } | 224 | 53.3M | for (i = 0; i < (int)croppedHeight; i++) { | 225 | 53.2M | if (this->bottomUp) | 226 | 28.9M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 227 | 24.3M | else | 228 | 24.3M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 229 | 53.2M | } | 230 | | | 231 | 32.5k | #if BITS_IN_JSAMPLE != 16 | 232 | 41.7k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 233 | 0 | if (this->croppingRegion.y != 0) { | 234 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 235 | |
| 236 | 0 | if ((int)lines != this->croppingRegion.y) | 237 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 238 | 0 | "actual (%d) cropping region upper boundary", | 239 | 0 | this->croppingRegion.y, (int)lines); | 240 | 0 | } | 241 | 0 | while ((int)dinfo->output_scanline < | 242 | 0 | this->croppingRegion.y + this->croppingRegion.h) | 243 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 244 | 0 | this->croppingRegion.y], | 245 | 0 | this->croppingRegion.y + this->croppingRegion.h - | 246 | 0 | dinfo->output_scanline); | 247 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != | 248 | 0 | (int)dinfo->output_height) { | 249 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 250 | 0 | this->croppingRegion.y - | 251 | 0 | this->croppingRegion.h); | 252 | |
| 253 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - | 254 | 0 | this->croppingRegion.h) | 255 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | 0 | "actual (%d) cropping region lower boundary", | 257 | 0 | this->croppingRegion.y + this->croppingRegion.h, | 258 | 0 | (int)(dinfo->output_height - lines)); | 259 | 0 | } | 260 | 0 | } else | 261 | 32.5k | #endif | 262 | 32.5k | { | 263 | 24.9M | while (dinfo->output_scanline < dinfo->output_height) | 264 | 24.9M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 265 | 24.9M | dinfo->output_height - dinfo->output_scanline); | 266 | 32.5k | } | 267 | 32.5k | jpeg_finish_decompress(dinfo); | 268 | | | 269 | 51.9k | bailout: | 270 | 51.9k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 271 | 51.9k | free(row_pointer); | 272 | 51.9k | if (this->jerr.warning) retval = -1; | 273 | 51.9k | return retval; | 274 | 32.5k | } |
Line | Count | Source | 144 | 11.1k | { | 145 | 11.1k | static const char FUNCTION_NAME[] = | 146 | 11.1k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 11.1k | _JSAMPROW *row_pointer = NULL; | 148 | 11.1k | int croppedHeight, i, retval = 0; | 149 | | #if BITS_IN_JSAMPLE != 16 | 150 | | int scaledWidth; | 151 | | #endif | 152 | 11.1k | struct my_progress_mgr progress; | 153 | | | 154 | 11.1k | GET_DINSTANCE(handle); | 155 | 11.1k | if ((this->init & DECOMPRESS) == 0) | 156 | 11.1k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 11.1k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 11.1k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 11.1k | THROW("Invalid argument"); | 161 | | | 162 | 11.1k | if (this->scanLimit) { | 163 | 11.1k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 11.1k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 11.1k | progress.this = this; | 166 | 11.1k | dinfo->progress = &progress.pub; | 167 | 11.1k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 11.1k | if (setjmp(this->jerr.setjmp_buffer)) { | 171 | | /* If we get here, the JPEG code has signaled an error. */ | 172 | 1.46k | retval = -1; goto bailout; | 173 | 1.46k | } | 174 | | | 175 | 11.1k | if (dinfo->global_state <= DSTATE_START) { | 176 | 11.1k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 177 | 11.1k | jpeg_read_header(dinfo, TRUE); | 178 | 11.1k | } | 179 | 9.64k | setDecompParameters(this); | 180 | 9.64k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 181 | | #if BITS_IN_JSAMPLE != 16 | 182 | | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 183 | | #endif | 184 | 9.64k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 185 | 9.64k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 186 | | | 187 | 9.64k | dinfo->scale_num = this->scalingFactor.num; | 188 | 9.64k | dinfo->scale_denom = this->scalingFactor.denom; | 189 | | | 190 | 9.64k | jpeg_start_decompress(dinfo); | 191 | | | 192 | | #if BITS_IN_JSAMPLE != 16 | 193 | | if (this->croppingRegion.x != 0 || | 194 | | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 195 | | JDIMENSION crop_x = this->croppingRegion.x; | 196 | | JDIMENSION crop_w = this->croppingRegion.w; | 197 | | | 198 | | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 199 | | if ((int)crop_x != this->croppingRegion.x) | 200 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 201 | | "actual (%d) cropping region left boundary", | 202 | | this->croppingRegion.x, (int)crop_x); | 203 | | if ((int)crop_w != this->croppingRegion.w) | 204 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 205 | | "actual (%d) cropping region width", | 206 | | this->croppingRegion.w, (int)crop_w); | 207 | | } | 208 | | #endif | 209 | | | 210 | 9.64k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 211 | | | 212 | 9.64k | croppedHeight = dinfo->output_height; | 213 | | #if BITS_IN_JSAMPLE != 16 | 214 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 215 | | croppedHeight = this->croppingRegion.h; | 216 | | #endif | 217 | 9.64k | if ((row_pointer = | 218 | 9.64k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 219 | 9.64k | THROW("Memory allocation failure"); | 220 | 9.64k | if (setjmp(this->jerr.setjmp_buffer)) { | 221 | | /* If we get here, the JPEG code has signaled an error. */ | 222 | 7.19k | retval = -1; goto bailout; | 223 | 7.19k | } | 224 | 19.0M | for (i = 0; i < (int)croppedHeight; i++) { | 225 | 19.0M | if (this->bottomUp) | 226 | 0 | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 227 | 19.0M | else | 228 | 19.0M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 229 | 19.0M | } | 230 | | | 231 | | #if BITS_IN_JSAMPLE != 16 | 232 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 233 | | if (this->croppingRegion.y != 0) { | 234 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 235 | | | 236 | | if ((int)lines != this->croppingRegion.y) | 237 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 238 | | "actual (%d) cropping region upper boundary", | 239 | | this->croppingRegion.y, (int)lines); | 240 | | } | 241 | | while ((int)dinfo->output_scanline < | 242 | | this->croppingRegion.y + this->croppingRegion.h) | 243 | | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 244 | | this->croppingRegion.y], | 245 | | this->croppingRegion.y + this->croppingRegion.h - | 246 | | dinfo->output_scanline); | 247 | | if (this->croppingRegion.y + this->croppingRegion.h != | 248 | | (int)dinfo->output_height) { | 249 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 250 | | this->croppingRegion.y - | 251 | | this->croppingRegion.h); | 252 | | | 253 | | if (lines != dinfo->output_height - this->croppingRegion.y - | 254 | | this->croppingRegion.h) | 255 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 256 | | "actual (%d) cropping region lower boundary", | 257 | | this->croppingRegion.y + this->croppingRegion.h, | 258 | | (int)(dinfo->output_height - lines)); | 259 | | } | 260 | | } else | 261 | | #endif | 262 | 2.44k | { | 263 | 7.54M | while (dinfo->output_scanline < dinfo->output_height) | 264 | 7.54M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 265 | 7.54M | dinfo->output_height - dinfo->output_scanline); | 266 | 2.44k | } | 267 | 2.44k | jpeg_finish_decompress(dinfo); | 268 | | | 269 | 11.1k | bailout: | 270 | 11.1k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 271 | 11.1k | free(row_pointer); | 272 | 11.1k | if (this->jerr.warning) retval = -1; | 273 | 11.1k | return retval; | 274 | 2.44k | } |
|
275 | | |
276 | | |
277 | | /*************************** Packed-Pixel Image I/O **************************/ |
278 | | |
279 | | /* TurboJPEG 3+ */ |
280 | | DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE) |
281 | | (tjhandle handle, const char *filename, int *width, int align, int *height, |
282 | | int *pixelFormat) |
283 | 0 | { |
284 | 0 | static const char FUNCTION_NAME[] = |
285 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
286 | |
|
287 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
288 | |
|
289 | 0 | int retval = 0, tempc; |
290 | 0 | size_t pitch; |
291 | 0 | tjhandle handle2 = NULL; |
292 | 0 | tjinstance *this2; |
293 | 0 | j_compress_ptr cinfo = NULL; |
294 | 0 | cjpeg_source_ptr src; |
295 | 0 | _JSAMPLE *dstBuf = NULL; |
296 | 0 | FILE *file = NULL; |
297 | 0 | boolean invert; |
298 | |
|
299 | 0 | GET_TJINSTANCE(handle, NULL) |
300 | |
|
301 | 0 | if (!filename || !width || align < 1 || !height || !pixelFormat || |
302 | 0 | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
303 | 0 | THROW("Invalid argument"); |
304 | 0 | if ((align & (align - 1)) != 0) |
305 | 0 | THROW("Alignment must be a power of 2"); |
306 | | |
307 | | /* The instance handle passed to this function is used only for parameter |
308 | | retrieval. Create a new temporary instance to avoid interfering with the |
309 | | libjpeg state of the primary instance. */ |
310 | 0 | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; |
311 | 0 | this2 = (tjinstance *)handle2; |
312 | 0 | cinfo = &this2->cinfo; |
313 | |
|
314 | | #ifdef _MSC_VER |
315 | | if (fopen_s(&file, filename, "rb") || file == NULL) |
316 | | #else |
317 | 0 | if ((file = fopen(filename, "rb")) == NULL) |
318 | 0 | #endif |
319 | 0 | THROW_UNIX("Cannot open input file"); |
320 | |
|
321 | 0 | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
322 | 0 | THROW_UNIX("Could not read input file") |
323 | 0 | else if (tempc == EOF) |
324 | 0 | THROW("Input file contains no data"); |
325 | |
|
326 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
327 | | /* If we get here, the JPEG code has signaled an error. */ |
328 | 0 | retval = -1; goto bailout; |
329 | 0 | } |
330 | | |
331 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
332 | 0 | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
333 | 0 | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
334 | 0 | if (tempc == 'B') { |
335 | 0 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
336 | 0 | THROW("Could not initialize bitmap loader"); |
337 | 0 | invert = !this->bottomUp; |
338 | 0 | } else if (tempc == 'P') { |
339 | 0 | if ((src = _jinit_read_ppm(cinfo)) == NULL) |
340 | 0 | THROW("Could not initialize PPM loader"); |
341 | 0 | invert = this->bottomUp; |
342 | 0 | } else |
343 | 0 | THROW("Unsupported file type"); |
344 | |
|
345 | 0 | src->input_file = file; |
346 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
347 | | /* Refuse to load images larger than 1 Megapixel when fuzzing. */ |
348 | 0 | src->max_pixels = this->maxPixels; |
349 | 0 | #endif |
350 | 0 | (*src->start_input) (cinfo, src); |
351 | 0 | if (tempc == 'B') { |
352 | 0 | if (cinfo->X_density && cinfo->Y_density) { |
353 | 0 | this->xDensity = cinfo->X_density; |
354 | 0 | this->yDensity = cinfo->Y_density; |
355 | 0 | this->densityUnits = cinfo->density_unit; |
356 | 0 | } |
357 | 0 | } |
358 | 0 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
359 | |
|
360 | 0 | *width = cinfo->image_width; *height = cinfo->image_height; |
361 | 0 | *pixelFormat = cs2pf[cinfo->in_color_space]; |
362 | |
|
363 | 0 | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
364 | 0 | if ((unsigned long long)pitch * (unsigned long long)(*height) > |
365 | 0 | (unsigned long long)((size_t)-1) || |
366 | 0 | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * |
367 | 0 | sizeof(_JSAMPLE))) == NULL) |
368 | 0 | THROW("Memory allocation failure"); |
369 | |
|
370 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
371 | | /* If we get here, the JPEG code has signaled an error. */ |
372 | 0 | retval = -1; goto bailout; |
373 | 0 | } |
374 | | |
375 | 0 | while (cinfo->next_scanline < cinfo->image_height) { |
376 | 0 | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
377 | |
|
378 | 0 | for (i = 0; i < nlines; i++) { |
379 | 0 | _JSAMPLE *dstptr; |
380 | 0 | int row; |
381 | |
|
382 | 0 | row = cinfo->next_scanline + i; |
383 | 0 | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
384 | 0 | else dstptr = &dstBuf[row * pitch]; |
385 | 0 | memcpy(dstptr, src->_buffer[i], |
386 | 0 | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); |
387 | 0 | } |
388 | 0 | cinfo->next_scanline += nlines; |
389 | 0 | } |
390 | |
|
391 | 0 | (*src->finish_input) (cinfo, src); |
392 | |
|
393 | 0 | bailout: |
394 | 0 | tj3Destroy(handle2); |
395 | 0 | if (file) fclose(file); |
396 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
397 | 0 | return dstBuf; |
398 | |
|
399 | | #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
400 | | |
401 | | static const char ERROR_MSG[] = |
402 | | "16-bit data precision requires lossless JPEG,\n" |
403 | | "which was disabled at build time."; |
404 | | _JSAMPLE *retval = NULL; |
405 | | |
406 | | GET_TJINSTANCE(handle, NULL) |
407 | | SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME, |
408 | | ERROR_MSG); |
409 | | this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL) |
410 | | |
411 | | bailout: |
412 | | return retval; |
413 | | |
414 | | #endif |
415 | 0 | } Unexecuted instantiation: tj3LoadImage8 Unexecuted instantiation: tj3LoadImage12 Unexecuted instantiation: tj3LoadImage16 |
416 | | |
417 | | |
418 | | /* TurboJPEG 3+ */ |
419 | | DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE) |
420 | | (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width, |
421 | | int pitch, int height, int pixelFormat) |
422 | 0 | { |
423 | 0 | static const char FUNCTION_NAME[] = |
424 | 0 | GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE); |
425 | 0 | int retval = 0; |
426 | |
|
427 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) |
428 | |
|
429 | 0 | tjhandle handle2 = NULL; |
430 | 0 | tjinstance *this2; |
431 | 0 | j_decompress_ptr dinfo = NULL; |
432 | 0 | djpeg_dest_ptr dst; |
433 | 0 | FILE *file = NULL; |
434 | 0 | char *ptr = NULL; |
435 | 0 | boolean invert; |
436 | |
|
437 | 0 | GET_TJINSTANCE(handle, -1) |
438 | |
|
439 | 0 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
440 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
441 | 0 | THROW("Invalid argument"); |
442 | | |
443 | | /* The instance handle passed to this function is used only for parameter |
444 | | retrieval. Create a new temporary instance to avoid interfering with the |
445 | | libjpeg state of the primary instance. */ |
446 | 0 | if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
447 | 0 | return -1; |
448 | 0 | this2 = (tjinstance *)handle2; |
449 | 0 | dinfo = &this2->dinfo; |
450 | |
|
451 | | #ifdef _MSC_VER |
452 | | if (fopen_s(&file, filename, "wb") || file == NULL) |
453 | | #else |
454 | 0 | if ((file = fopen(filename, "wb")) == NULL) |
455 | 0 | #endif |
456 | 0 | THROW_UNIX("Cannot open output file"); |
457 | |
|
458 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
459 | | /* If we get here, the JPEG code has signaled an error. */ |
460 | 0 | retval = -1; goto bailout; |
461 | 0 | } |
462 | | |
463 | 0 | this2->dinfo.out_color_space = pf2cs[pixelFormat]; |
464 | 0 | dinfo->image_width = width; dinfo->image_height = height; |
465 | 0 | dinfo->global_state = DSTATE_READY; |
466 | 0 | dinfo->scale_num = dinfo->scale_denom = 1; |
467 | 0 | dinfo->data_precision = BITS_IN_JSAMPLE; |
468 | |
|
469 | 0 | ptr = strrchr(filename, '.'); |
470 | 0 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
471 | 0 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
472 | 0 | THROW("Could not initialize bitmap writer"); |
473 | 0 | invert = !this->bottomUp; |
474 | 0 | dinfo->X_density = (UINT16)this->xDensity; |
475 | 0 | dinfo->Y_density = (UINT16)this->yDensity; |
476 | 0 | dinfo->density_unit = (UINT8)this->densityUnits; |
477 | 0 | } else { |
478 | 0 | if ((dst = _jinit_write_ppm(dinfo)) == NULL) |
479 | 0 | THROW("Could not initialize PPM writer"); |
480 | 0 | invert = this->bottomUp; |
481 | 0 | } |
482 | | |
483 | 0 | dst->output_file = file; |
484 | 0 | (*dst->start_output) (dinfo, dst); |
485 | 0 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
486 | |
|
487 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
488 | |
|
489 | 0 | while (dinfo->output_scanline < dinfo->output_height) { |
490 | 0 | _JSAMPLE *rowptr; |
491 | |
|
492 | 0 | if (invert) |
493 | 0 | rowptr = |
494 | 0 | (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch]; |
495 | 0 | else |
496 | 0 | rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch]; |
497 | 0 | memcpy(dst->_buffer[0], rowptr, |
498 | 0 | width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE)); |
499 | 0 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
500 | 0 | dinfo->output_scanline++; |
501 | 0 | } |
502 | |
|
503 | 0 | (*dst->finish_output) (dinfo, dst); |
504 | |
|
505 | 0 | bailout: |
506 | 0 | tj3Destroy(handle2); |
507 | 0 | if (file) fclose(file); |
508 | 0 | return retval; |
509 | |
|
510 | | #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */ |
511 | | |
512 | | GET_TJINSTANCE(handle, -1) |
513 | | THROW("16-bit data precision requires lossless JPEG,\n" |
514 | | "which was disabled at build time.") |
515 | | bailout: |
516 | | return retval; |
517 | | |
518 | | #endif |
519 | 0 | } Unexecuted instantiation: tj3SaveImage8 Unexecuted instantiation: tj3SaveImage12 Unexecuted instantiation: tj3SaveImage16 |
520 | | |
521 | | |
522 | | #undef _JSAMPLE |
523 | | #undef _JSAMPROW |
524 | | #undef _buffer |
525 | | #undef _jinit_read_ppm |
526 | | #undef _jinit_write_ppm |
527 | | #undef _jpeg_crop_scanline |
528 | | #undef _jpeg_read_scanlines |
529 | | #undef _jpeg_skip_scanlines |
530 | | #undef _jpeg_write_scanlines |