/src/libjpeg-turbo.main/turbojpeg-mp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C)2009-2024 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 | 65.0k | #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 | 18.8M | #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 | 41.8k | #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 | 26.4M | #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 | 14.5k | #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 | 8.74M | #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 | 121k | #define _GET_STRING(name, suffix) #name #suffix |
65 | 121k | #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 && alloc) |
129 | 0 | (*cinfo->dest->term_destination) (cinfo); |
130 | 0 | if (cinfo->global_state > CSTATE_START || retval == -1) |
131 | 0 | jpeg_abort_compress(cinfo); |
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 | 121k | { |
145 | 121k | static const char FUNCTION_NAME[] = |
146 | 121k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
147 | 121k | _JSAMPROW *row_pointer = NULL; |
148 | 121k | int croppedHeight, i, retval = 0; |
149 | | #if BITS_IN_JSAMPLE != 16 |
150 | | int scaledWidth; |
151 | | #endif |
152 | 121k | struct my_progress_mgr progress; |
153 | | |
154 | 121k | GET_DINSTANCE(handle); |
155 | 121k | if ((this->init & DECOMPRESS) == 0) |
156 | 121k | THROW("Instance has not been initialized for decompression"); |
157 | | |
158 | 121k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
159 | 121k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
160 | 121k | THROW("Invalid argument"); |
161 | | |
162 | 121k | if (this->scanLimit) { |
163 | 121k | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
164 | 121k | progress.pub.progress_monitor = my_progress_monitor; |
165 | 121k | progress.this = this; |
166 | 121k | dinfo->progress = &progress.pub; |
167 | 121k | } else |
168 | 0 | dinfo->progress = NULL; |
169 | | |
170 | 121k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
171 | | |
172 | 121k | if (setjmp(this->jerr.setjmp_buffer)) { |
173 | | /* If we get here, the JPEG code has signaled an error. */ |
174 | 34.0k | retval = -1; goto bailout; |
175 | 34.0k | } |
176 | | |
177 | 121k | if (dinfo->global_state <= DSTATE_INHEADER) { |
178 | 121k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
179 | 121k | jpeg_read_header(dinfo, TRUE); |
180 | 121k | } |
181 | 87.3k | setDecompParameters(this); |
182 | 87.3k | if (this->maxPixels && |
183 | 87.3k | (unsigned long long)this->jpegWidth * this->jpegHeight > |
184 | 0 | (unsigned long long)this->maxPixels) |
185 | 87.3k | THROW("Image is too large"); |
186 | 87.3k | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
187 | | #if BITS_IN_JSAMPLE != 16 |
188 | 74.2k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
189 | | #endif |
190 | 87.3k | dinfo->do_fancy_upsampling = !this->fastUpsample; |
191 | 87.3k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
192 | | |
193 | 87.3k | dinfo->scale_num = this->scalingFactor.num; |
194 | 87.3k | dinfo->scale_denom = this->scalingFactor.denom; |
195 | | |
196 | 87.3k | jpeg_start_decompress(dinfo); |
197 | | |
198 | | #if BITS_IN_JSAMPLE != 16 |
199 | 74.2k | if (this->croppingRegion.x != 0 || |
200 | 74.2k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
201 | 0 | JDIMENSION crop_x = this->croppingRegion.x; |
202 | 0 | JDIMENSION crop_w = this->croppingRegion.w; |
203 | |
|
204 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
205 | 0 | if ((int)crop_x != this->croppingRegion.x) |
206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
207 | 0 | "actual (%d) cropping region left boundary", |
208 | 0 | this->croppingRegion.x, (int)crop_x); |
209 | 0 | if ((int)crop_w != this->croppingRegion.w) |
210 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
211 | 0 | "actual (%d) cropping region width", |
212 | 0 | this->croppingRegion.w, (int)crop_w); |
213 | 0 | } |
214 | 74.2k | #endif |
215 | | |
216 | 87.3k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
217 | | |
218 | 74.2k | croppedHeight = dinfo->output_height; |
219 | | #if BITS_IN_JSAMPLE != 16 |
220 | 74.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
221 | 0 | croppedHeight = this->croppingRegion.h; |
222 | | #endif |
223 | 87.3k | if ((row_pointer = |
224 | 87.3k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
225 | 87.3k | THROW("Memory allocation failure"); |
226 | 87.3k | if (setjmp(this->jerr.setjmp_buffer)) { |
227 | | /* If we get here, the JPEG code has signaled an error. */ |
228 | 34.6k | retval = -1; goto bailout; |
229 | 34.6k | } |
230 | 105M | for (i = 0; i < (int)croppedHeight; i++) { |
231 | 105M | if (this->bottomUp) |
232 | 90.4M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
233 | 15.3M | else |
234 | 15.3M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
235 | 105M | } |
236 | | |
237 | | #if BITS_IN_JSAMPLE != 16 |
238 | 74.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
239 | 0 | if (this->croppingRegion.y != 0) { |
240 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
241 | | |
242 | 0 | if ((int)lines != this->croppingRegion.y) |
243 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
244 | | "actual (%d) cropping region upper boundary", |
245 | 0 | this->croppingRegion.y, (int)lines); |
246 | 0 | } |
247 | 0 | while ((int)dinfo->output_scanline < |
248 | 0 | this->croppingRegion.y + this->croppingRegion.h) |
249 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
250 | 0 | this->croppingRegion.y], |
251 | 0 | this->croppingRegion.y + this->croppingRegion.h - |
252 | 0 | dinfo->output_scanline); |
253 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != |
254 | 0 | (int)dinfo->output_height) { |
255 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
256 | | this->croppingRegion.y - |
257 | | this->croppingRegion.h); |
258 | |
|
259 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - |
260 | 0 | this->croppingRegion.h) |
261 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
262 | 0 | "actual (%d) cropping region lower boundary", |
263 | 0 | this->croppingRegion.y + this->croppingRegion.h, |
264 | 0 | (int)(dinfo->output_height - lines)); |
265 | 0 | } |
266 | 0 | } else |
267 | 50.4k | #endif |
268 | 50.4k | { |
269 | 54.1M | while (dinfo->output_scanline < dinfo->output_height) |
270 | 54.0M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
271 | 54.0M | dinfo->output_height - dinfo->output_scanline); |
272 | 50.4k | } |
273 | 50.4k | jpeg_finish_decompress(dinfo); |
274 | | |
275 | 121k | bailout: |
276 | 121k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
277 | 121k | free(row_pointer); |
278 | 121k | if (this->jerr.warning) retval = -1; |
279 | 121k | return retval; |
280 | 50.4k | } Line | Count | Source | 144 | 65.0k | { | 145 | 65.0k | static const char FUNCTION_NAME[] = | 146 | 65.0k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 65.0k | _JSAMPROW *row_pointer = NULL; | 148 | 65.0k | int croppedHeight, i, retval = 0; | 149 | 65.0k | #if BITS_IN_JSAMPLE != 16 | 150 | 65.0k | int scaledWidth; | 151 | 65.0k | #endif | 152 | 65.0k | struct my_progress_mgr progress; | 153 | | | 154 | 65.0k | GET_DINSTANCE(handle); | 155 | 65.0k | if ((this->init & DECOMPRESS) == 0) | 156 | 65.0k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 65.0k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 65.0k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 65.0k | THROW("Invalid argument"); | 161 | | | 162 | 65.0k | if (this->scanLimit) { | 163 | 65.0k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 65.0k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 65.0k | progress.this = this; | 166 | 65.0k | dinfo->progress = &progress.pub; | 167 | 65.0k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 65.0k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 171 | | | 172 | 65.0k | if (setjmp(this->jerr.setjmp_buffer)) { | 173 | | /* If we get here, the JPEG code has signaled an error. */ | 174 | 25.5k | retval = -1; goto bailout; | 175 | 25.5k | } | 176 | | | 177 | 65.0k | if (dinfo->global_state <= DSTATE_INHEADER) { | 178 | 65.0k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 179 | 65.0k | jpeg_read_header(dinfo, TRUE); | 180 | 65.0k | } | 181 | 39.4k | setDecompParameters(this); | 182 | 39.4k | if (this->maxPixels && | 183 | 39.4k | (unsigned long long)this->jpegWidth * this->jpegHeight > | 184 | 0 | (unsigned long long)this->maxPixels) | 185 | 39.4k | THROW("Image is too large"); | 186 | 39.4k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 187 | 39.4k | #if BITS_IN_JSAMPLE != 16 | 188 | 39.4k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 189 | 39.4k | #endif | 190 | 39.4k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 191 | 18.4E | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 192 | | | 193 | 39.4k | dinfo->scale_num = this->scalingFactor.num; | 194 | 39.4k | dinfo->scale_denom = this->scalingFactor.denom; | 195 | | | 196 | 39.4k | jpeg_start_decompress(dinfo); | 197 | | | 198 | 39.4k | #if BITS_IN_JSAMPLE != 16 | 199 | 39.4k | if (this->croppingRegion.x != 0 || | 200 | 39.4k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 201 | 0 | JDIMENSION crop_x = this->croppingRegion.x; | 202 | 0 | JDIMENSION crop_w = this->croppingRegion.w; | 203 | |
| 204 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 205 | 0 | if ((int)crop_x != this->croppingRegion.x) | 206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | 0 | "actual (%d) cropping region left boundary", | 208 | 0 | this->croppingRegion.x, (int)crop_x); | 209 | 0 | if ((int)crop_w != this->croppingRegion.w) | 210 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 211 | 0 | "actual (%d) cropping region width", | 212 | 0 | this->croppingRegion.w, (int)crop_w); | 213 | 0 | } | 214 | 39.4k | #endif | 215 | | | 216 | 39.4k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 217 | | | 218 | 39.4k | croppedHeight = dinfo->output_height; | 219 | 39.4k | #if BITS_IN_JSAMPLE != 16 | 220 | 39.4k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 221 | 0 | croppedHeight = this->croppingRegion.h; | 222 | 39.4k | #endif | 223 | 39.4k | if ((row_pointer = | 224 | 39.4k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 225 | 39.4k | THROW("Memory allocation failure"); | 226 | 39.4k | if (setjmp(this->jerr.setjmp_buffer)) { | 227 | | /* If we get here, the JPEG code has signaled an error. */ | 228 | 11.2k | retval = -1; goto bailout; | 229 | 11.2k | } | 230 | 38.3M | for (i = 0; i < (int)croppedHeight; i++) { | 231 | 38.3M | if (this->bottomUp) | 232 | 33.8M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 233 | 4.48M | else | 234 | 4.48M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 235 | 38.3M | } | 236 | | | 237 | 28.2k | #if BITS_IN_JSAMPLE != 16 | 238 | 39.4k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 239 | 0 | if (this->croppingRegion.y != 0) { | 240 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 241 | |
| 242 | 0 | if ((int)lines != this->croppingRegion.y) | 243 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 244 | 0 | "actual (%d) cropping region upper boundary", | 245 | 0 | this->croppingRegion.y, (int)lines); | 246 | 0 | } | 247 | 0 | while ((int)dinfo->output_scanline < | 248 | 0 | this->croppingRegion.y + this->croppingRegion.h) | 249 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 250 | 0 | this->croppingRegion.y], | 251 | 0 | this->croppingRegion.y + this->croppingRegion.h - | 252 | 0 | dinfo->output_scanline); | 253 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != | 254 | 0 | (int)dinfo->output_height) { | 255 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 256 | 0 | this->croppingRegion.y - | 257 | 0 | this->croppingRegion.h); | 258 | |
| 259 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - | 260 | 0 | this->croppingRegion.h) | 261 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 262 | 0 | "actual (%d) cropping region lower boundary", | 263 | 0 | this->croppingRegion.y + this->croppingRegion.h, | 264 | 0 | (int)(dinfo->output_height - lines)); | 265 | 0 | } | 266 | 0 | } else | 267 | 28.2k | #endif | 268 | 28.2k | { | 269 | 18.8M | while (dinfo->output_scanline < dinfo->output_height) | 270 | 18.8M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 271 | 18.8M | dinfo->output_height - dinfo->output_scanline); | 272 | 28.2k | } | 273 | 28.2k | jpeg_finish_decompress(dinfo); | 274 | | | 275 | 65.0k | bailout: | 276 | 65.0k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 277 | 65.0k | free(row_pointer); | 278 | 65.0k | if (this->jerr.warning) retval = -1; | 279 | 65.0k | return retval; | 280 | 28.2k | } |
Line | Count | Source | 144 | 41.8k | { | 145 | 41.8k | static const char FUNCTION_NAME[] = | 146 | 41.8k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 41.8k | _JSAMPROW *row_pointer = NULL; | 148 | 41.8k | int croppedHeight, i, retval = 0; | 149 | 41.8k | #if BITS_IN_JSAMPLE != 16 | 150 | 41.8k | int scaledWidth; | 151 | 41.8k | #endif | 152 | 41.8k | struct my_progress_mgr progress; | 153 | | | 154 | 41.8k | GET_DINSTANCE(handle); | 155 | 41.8k | if ((this->init & DECOMPRESS) == 0) | 156 | 41.8k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 41.8k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 41.8k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 41.8k | THROW("Invalid argument"); | 161 | | | 162 | 41.8k | if (this->scanLimit) { | 163 | 41.8k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 41.8k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 41.8k | progress.this = this; | 166 | 41.8k | dinfo->progress = &progress.pub; | 167 | 41.8k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 41.8k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 171 | | | 172 | 41.8k | if (setjmp(this->jerr.setjmp_buffer)) { | 173 | | /* If we get here, the JPEG code has signaled an error. */ | 174 | 7.03k | retval = -1; goto bailout; | 175 | 7.03k | } | 176 | | | 177 | 41.8k | if (dinfo->global_state <= DSTATE_INHEADER) { | 178 | 41.8k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 179 | 41.8k | jpeg_read_header(dinfo, TRUE); | 180 | 41.8k | } | 181 | 34.7k | setDecompParameters(this); | 182 | 34.7k | if (this->maxPixels && | 183 | 34.7k | (unsigned long long)this->jpegWidth * this->jpegHeight > | 184 | 0 | (unsigned long long)this->maxPixels) | 185 | 34.7k | THROW("Image is too large"); | 186 | 34.7k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 187 | 34.7k | #if BITS_IN_JSAMPLE != 16 | 188 | 34.7k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 189 | 34.7k | #endif | 190 | 34.7k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 191 | 34.7k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 192 | | | 193 | 34.7k | dinfo->scale_num = this->scalingFactor.num; | 194 | 34.7k | dinfo->scale_denom = this->scalingFactor.denom; | 195 | | | 196 | 34.7k | jpeg_start_decompress(dinfo); | 197 | | | 198 | 34.7k | #if BITS_IN_JSAMPLE != 16 | 199 | 34.7k | if (this->croppingRegion.x != 0 || | 200 | 34.7k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 201 | 0 | JDIMENSION crop_x = this->croppingRegion.x; | 202 | 0 | JDIMENSION crop_w = this->croppingRegion.w; | 203 | |
| 204 | 0 | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 205 | 0 | if ((int)crop_x != this->croppingRegion.x) | 206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | 0 | "actual (%d) cropping region left boundary", | 208 | 0 | this->croppingRegion.x, (int)crop_x); | 209 | 0 | if ((int)crop_w != this->croppingRegion.w) | 210 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 211 | 0 | "actual (%d) cropping region width", | 212 | 0 | this->croppingRegion.w, (int)crop_w); | 213 | 0 | } | 214 | 34.7k | #endif | 215 | | | 216 | 34.7k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 217 | | | 218 | 34.7k | croppedHeight = dinfo->output_height; | 219 | 34.7k | #if BITS_IN_JSAMPLE != 16 | 220 | 34.7k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 221 | 0 | croppedHeight = this->croppingRegion.h; | 222 | 34.7k | #endif | 223 | 34.7k | if ((row_pointer = | 224 | 34.7k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 225 | 34.7k | THROW("Memory allocation failure"); | 226 | 34.7k | if (setjmp(this->jerr.setjmp_buffer)) { | 227 | | /* If we get here, the JPEG code has signaled an error. */ | 228 | 12.5k | retval = -1; goto bailout; | 229 | 12.5k | } | 230 | 50.0M | for (i = 0; i < (int)croppedHeight; i++) { | 231 | 50.0M | if (this->bottomUp) | 232 | 39.1M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 233 | 10.8M | else | 234 | 10.8M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 235 | 50.0M | } | 236 | | | 237 | 22.2k | #if BITS_IN_JSAMPLE != 16 | 238 | 34.7k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 239 | 0 | if (this->croppingRegion.y != 0) { | 240 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 241 | |
| 242 | 0 | if ((int)lines != this->croppingRegion.y) | 243 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 244 | 0 | "actual (%d) cropping region upper boundary", | 245 | 0 | this->croppingRegion.y, (int)lines); | 246 | 0 | } | 247 | 0 | while ((int)dinfo->output_scanline < | 248 | 0 | this->croppingRegion.y + this->croppingRegion.h) | 249 | 0 | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 250 | 0 | this->croppingRegion.y], | 251 | 0 | this->croppingRegion.y + this->croppingRegion.h - | 252 | 0 | dinfo->output_scanline); | 253 | 0 | if (this->croppingRegion.y + this->croppingRegion.h != | 254 | 0 | (int)dinfo->output_height) { | 255 | 0 | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 256 | 0 | this->croppingRegion.y - | 257 | 0 | this->croppingRegion.h); | 258 | |
| 259 | 0 | if (lines != dinfo->output_height - this->croppingRegion.y - | 260 | 0 | this->croppingRegion.h) | 261 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 262 | 0 | "actual (%d) cropping region lower boundary", | 263 | 0 | this->croppingRegion.y + this->croppingRegion.h, | 264 | 0 | (int)(dinfo->output_height - lines)); | 265 | 0 | } | 266 | 0 | } else | 267 | 22.2k | #endif | 268 | 22.2k | { | 269 | 26.5M | while (dinfo->output_scanline < dinfo->output_height) | 270 | 26.4M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 271 | 26.4M | dinfo->output_height - dinfo->output_scanline); | 272 | 22.2k | } | 273 | 22.2k | jpeg_finish_decompress(dinfo); | 274 | | | 275 | 41.8k | bailout: | 276 | 41.8k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 277 | 41.8k | free(row_pointer); | 278 | 41.8k | if (this->jerr.warning) retval = -1; | 279 | 41.8k | return retval; | 280 | 22.2k | } |
Line | Count | Source | 144 | 14.5k | { | 145 | 14.5k | static const char FUNCTION_NAME[] = | 146 | 14.5k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 147 | 14.5k | _JSAMPROW *row_pointer = NULL; | 148 | 14.5k | int croppedHeight, i, retval = 0; | 149 | | #if BITS_IN_JSAMPLE != 16 | 150 | | int scaledWidth; | 151 | | #endif | 152 | 14.5k | struct my_progress_mgr progress; | 153 | | | 154 | 14.5k | GET_DINSTANCE(handle); | 155 | 14.5k | if ((this->init & DECOMPRESS) == 0) | 156 | 14.5k | THROW("Instance has not been initialized for decompression"); | 157 | | | 158 | 14.5k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 159 | 14.5k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 160 | 14.5k | THROW("Invalid argument"); | 161 | | | 162 | 14.5k | if (this->scanLimit) { | 163 | 14.5k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 164 | 14.5k | progress.pub.progress_monitor = my_progress_monitor; | 165 | 14.5k | progress.this = this; | 166 | 14.5k | dinfo->progress = &progress.pub; | 167 | 14.5k | } else | 168 | 0 | dinfo->progress = NULL; | 169 | | | 170 | 14.5k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 171 | | | 172 | 14.5k | if (setjmp(this->jerr.setjmp_buffer)) { | 173 | | /* If we get here, the JPEG code has signaled an error. */ | 174 | 1.39k | retval = -1; goto bailout; | 175 | 1.39k | } | 176 | | | 177 | 14.5k | if (dinfo->global_state <= DSTATE_INHEADER) { | 178 | 14.5k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 179 | 14.5k | jpeg_read_header(dinfo, TRUE); | 180 | 14.5k | } | 181 | 13.1k | setDecompParameters(this); | 182 | 13.1k | if (this->maxPixels && | 183 | 13.1k | (unsigned long long)this->jpegWidth * this->jpegHeight > | 184 | 0 | (unsigned long long)this->maxPixels) | 185 | 13.1k | THROW("Image is too large"); | 186 | 13.1k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 187 | | #if BITS_IN_JSAMPLE != 16 | 188 | | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 189 | | #endif | 190 | 13.1k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 191 | 13.1k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 192 | | | 193 | 13.1k | dinfo->scale_num = this->scalingFactor.num; | 194 | 13.1k | dinfo->scale_denom = this->scalingFactor.denom; | 195 | | | 196 | 13.1k | jpeg_start_decompress(dinfo); | 197 | | | 198 | | #if BITS_IN_JSAMPLE != 16 | 199 | | if (this->croppingRegion.x != 0 || | 200 | | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 201 | | JDIMENSION crop_x = this->croppingRegion.x; | 202 | | JDIMENSION crop_w = this->croppingRegion.w; | 203 | | | 204 | | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 205 | | if ((int)crop_x != this->croppingRegion.x) | 206 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | | "actual (%d) cropping region left boundary", | 208 | | this->croppingRegion.x, (int)crop_x); | 209 | | if ((int)crop_w != this->croppingRegion.w) | 210 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 211 | | "actual (%d) cropping region width", | 212 | | this->croppingRegion.w, (int)crop_w); | 213 | | } | 214 | | #endif | 215 | | | 216 | 13.1k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 217 | | | 218 | 13.1k | croppedHeight = dinfo->output_height; | 219 | | #if BITS_IN_JSAMPLE != 16 | 220 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 221 | | croppedHeight = this->croppingRegion.h; | 222 | | #endif | 223 | 13.1k | if ((row_pointer = | 224 | 13.1k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 225 | 13.1k | THROW("Memory allocation failure"); | 226 | 13.1k | if (setjmp(this->jerr.setjmp_buffer)) { | 227 | | /* If we get here, the JPEG code has signaled an error. */ | 228 | 10.9k | retval = -1; goto bailout; | 229 | 10.9k | } | 230 | 17.4M | for (i = 0; i < (int)croppedHeight; i++) { | 231 | 17.4M | if (this->bottomUp) | 232 | 17.4M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 233 | 22.2k | else | 234 | 22.2k | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 235 | 17.4M | } | 236 | | | 237 | | #if BITS_IN_JSAMPLE != 16 | 238 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 239 | | if (this->croppingRegion.y != 0) { | 240 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 241 | | | 242 | | if ((int)lines != this->croppingRegion.y) | 243 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 244 | | "actual (%d) cropping region upper boundary", | 245 | | this->croppingRegion.y, (int)lines); | 246 | | } | 247 | | while ((int)dinfo->output_scanline < | 248 | | this->croppingRegion.y + this->croppingRegion.h) | 249 | | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 250 | | this->croppingRegion.y], | 251 | | this->croppingRegion.y + this->croppingRegion.h - | 252 | | dinfo->output_scanline); | 253 | | if (this->croppingRegion.y + this->croppingRegion.h != | 254 | | (int)dinfo->output_height) { | 255 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 256 | | this->croppingRegion.y - | 257 | | this->croppingRegion.h); | 258 | | | 259 | | if (lines != dinfo->output_height - this->croppingRegion.y - | 260 | | this->croppingRegion.h) | 261 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 262 | | "actual (%d) cropping region lower boundary", | 263 | | this->croppingRegion.y + this->croppingRegion.h, | 264 | | (int)(dinfo->output_height - lines)); | 265 | | } | 266 | | } else | 267 | | #endif | 268 | 2.18k | { | 269 | 8.74M | while (dinfo->output_scanline < dinfo->output_height) | 270 | 8.74M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 271 | 8.74M | dinfo->output_height - dinfo->output_scanline); | 272 | 2.18k | } | 273 | 2.18k | jpeg_finish_decompress(dinfo); | 274 | | | 275 | 14.5k | bailout: | 276 | 14.5k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 277 | 14.5k | free(row_pointer); | 278 | 14.5k | if (this->jerr.warning) retval = -1; | 279 | 14.5k | return retval; | 280 | 2.18k | } |
|
281 | | |
282 | | |
283 | | /*************************** Packed-Pixel Image I/O **************************/ |
284 | | |
285 | | /* TurboJPEG 3+ */ |
286 | | DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE) |
287 | | (tjhandle handle, const char *filename, int *width, int align, int *height, |
288 | | int *pixelFormat) |
289 | 0 | { |
290 | 0 | static const char FUNCTION_NAME[] = |
291 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
292 | |
|
293 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
294 | |
|
295 | 0 | int retval = 0, tempc; |
296 | 0 | size_t pitch; |
297 | 0 | tjhandle handle2 = NULL; |
298 | 0 | tjinstance *this2; |
299 | 0 | j_compress_ptr cinfo = NULL; |
300 | 0 | cjpeg_source_ptr src; |
301 | 0 | _JSAMPLE *dstBuf = NULL; |
302 | 0 | FILE *file = NULL; |
303 | 0 | boolean invert; |
304 | |
|
305 | 0 | GET_TJINSTANCE(handle, NULL) |
306 | |
|
307 | 0 | if (!filename || !width || align < 1 || !height || !pixelFormat || |
308 | 0 | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
309 | 0 | THROW("Invalid argument"); |
310 | 0 | if ((align & (align - 1)) != 0) |
311 | 0 | THROW("Alignment must be a power of 2"); |
312 | | |
313 | | /* The instance handle passed to this function is used only for parameter |
314 | | retrieval. Create a new temporary instance to avoid interfering with the |
315 | | libjpeg state of the primary instance. */ |
316 | 0 | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; |
317 | 0 | this2 = (tjinstance *)handle2; |
318 | 0 | cinfo = &this2->cinfo; |
319 | |
|
320 | | #ifdef _MSC_VER |
321 | | if (fopen_s(&file, filename, "rb") || file == NULL) |
322 | | #else |
323 | 0 | if ((file = fopen(filename, "rb")) == NULL) |
324 | 0 | #endif |
325 | 0 | THROW_UNIX("Cannot open input file"); |
326 | |
|
327 | 0 | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
328 | 0 | THROW_UNIX("Could not read input file") |
329 | 0 | else if (tempc == EOF) |
330 | 0 | THROW("Input file contains no data"); |
331 | |
|
332 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
333 | | /* If we get here, the JPEG code has signaled an error. */ |
334 | 0 | retval = -1; goto bailout; |
335 | 0 | } |
336 | | |
337 | 0 | cinfo->data_precision = BITS_IN_JSAMPLE; |
338 | 0 | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
339 | 0 | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
340 | 0 | if (tempc == 'B') { |
341 | 0 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
342 | 0 | THROW("Could not initialize bitmap loader"); |
343 | 0 | invert = !this->bottomUp; |
344 | 0 | } else if (tempc == 'P') { |
345 | 0 | if ((src = _jinit_read_ppm(cinfo)) == NULL) |
346 | 0 | THROW("Could not initialize PPM loader"); |
347 | 0 | invert = this->bottomUp; |
348 | 0 | } else |
349 | 0 | THROW("Unsupported file type"); |
350 | |
|
351 | 0 | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
352 | |
|
353 | 0 | src->input_file = file; |
354 | | /* Refuse to load images larger than the specified size. */ |
355 | 0 | src->max_pixels = this->maxPixels; |
356 | 0 | (*src->start_input) (cinfo, src); |
357 | 0 | if (tempc == 'B') { |
358 | 0 | if (cinfo->X_density && cinfo->Y_density) { |
359 | 0 | this->xDensity = cinfo->X_density; |
360 | 0 | this->yDensity = cinfo->Y_density; |
361 | 0 | this->densityUnits = cinfo->density_unit; |
362 | 0 | } |
363 | 0 | } |
364 | 0 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
365 | |
|
366 | 0 | *width = cinfo->image_width; *height = cinfo->image_height; |
367 | 0 | *pixelFormat = cs2pf[cinfo->in_color_space]; |
368 | |
|
369 | 0 | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
370 | 0 | if ( |
371 | | #if ULLONG_MAX > SIZE_MAX |
372 | | (unsigned long long)pitch * (unsigned long long)(*height) > |
373 | | (unsigned long long)((size_t)-1) || |
374 | | #endif |
375 | 0 | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * |
376 | 0 | sizeof(_JSAMPLE))) == NULL) |
377 | 0 | THROW("Memory allocation failure"); |
378 | |
|
379 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
380 | | /* If we get here, the JPEG code has signaled an error. */ |
381 | 0 | retval = -1; goto bailout; |
382 | 0 | } |
383 | | |
384 | 0 | while (cinfo->next_scanline < cinfo->image_height) { |
385 | 0 | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
386 | |
|
387 | 0 | for (i = 0; i < nlines; i++) { |
388 | 0 | _JSAMPLE *dstptr; |
389 | 0 | int row; |
390 | |
|
391 | 0 | row = cinfo->next_scanline + i; |
392 | 0 | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
393 | 0 | else dstptr = &dstBuf[row * pitch]; |
394 | 0 | memcpy(dstptr, src->_buffer[i], |
395 | 0 | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); |
396 | 0 | } |
397 | 0 | cinfo->next_scanline += nlines; |
398 | 0 | } |
399 | |
|
400 | 0 | (*src->finish_input) (cinfo, src); |
401 | |
|
402 | 0 | bailout: |
403 | 0 | tj3Destroy(handle2); |
404 | 0 | if (file) fclose(file); |
405 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
406 | 0 | return dstBuf; |
407 | |
|
408 | | #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
409 | | |
410 | | static const char ERROR_MSG[] = |
411 | | "16-bit data precision requires lossless JPEG,\n" |
412 | | "which was disabled at build time."; |
413 | | _JSAMPLE *retval = NULL; |
414 | | |
415 | | GET_TJINSTANCE(handle, NULL) |
416 | | SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME, |
417 | | ERROR_MSG); |
418 | | this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL) |
419 | | |
420 | | bailout: |
421 | | return retval; |
422 | | |
423 | | #endif |
424 | 0 | } Unexecuted instantiation: tj3LoadImage8 Unexecuted instantiation: tj3LoadImage12 Unexecuted instantiation: tj3LoadImage16 |
425 | | |
426 | | |
427 | | /* TurboJPEG 3+ */ |
428 | | DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE) |
429 | | (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width, |
430 | | int pitch, int height, int pixelFormat) |
431 | 0 | { |
432 | 0 | static const char FUNCTION_NAME[] = |
433 | 0 | GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE); |
434 | 0 | int retval = 0; |
435 | |
|
436 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) |
437 | |
|
438 | 0 | tjhandle handle2 = NULL; |
439 | 0 | tjinstance *this2; |
440 | 0 | j_decompress_ptr dinfo = NULL; |
441 | 0 | djpeg_dest_ptr dst; |
442 | 0 | FILE *file = NULL; |
443 | 0 | char *ptr = NULL; |
444 | 0 | boolean invert; |
445 | |
|
446 | 0 | GET_TJINSTANCE(handle, -1) |
447 | |
|
448 | 0 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
449 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
450 | 0 | THROW("Invalid argument"); |
451 | | |
452 | | /* The instance handle passed to this function is used only for parameter |
453 | | retrieval. Create a new temporary instance to avoid interfering with the |
454 | | libjpeg state of the primary instance. */ |
455 | 0 | if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
456 | 0 | return -1; |
457 | 0 | this2 = (tjinstance *)handle2; |
458 | 0 | dinfo = &this2->dinfo; |
459 | |
|
460 | | #ifdef _MSC_VER |
461 | | if (fopen_s(&file, filename, "wb") || file == NULL) |
462 | | #else |
463 | 0 | if ((file = fopen(filename, "wb")) == NULL) |
464 | 0 | #endif |
465 | 0 | THROW_UNIX("Cannot open output file"); |
466 | |
|
467 | 0 | if (setjmp(this2->jerr.setjmp_buffer)) { |
468 | | /* If we get here, the JPEG code has signaled an error. */ |
469 | 0 | retval = -1; goto bailout; |
470 | 0 | } |
471 | | |
472 | 0 | this2->dinfo.out_color_space = pf2cs[pixelFormat]; |
473 | 0 | dinfo->image_width = width; dinfo->image_height = height; |
474 | 0 | dinfo->global_state = DSTATE_READY; |
475 | 0 | dinfo->scale_num = dinfo->scale_denom = 1; |
476 | 0 | dinfo->data_precision = BITS_IN_JSAMPLE; |
477 | |
|
478 | 0 | ptr = strrchr(filename, '.'); |
479 | 0 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
480 | 0 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
481 | 0 | THROW("Could not initialize bitmap writer"); |
482 | 0 | invert = !this->bottomUp; |
483 | 0 | dinfo->X_density = (UINT16)this->xDensity; |
484 | 0 | dinfo->Y_density = (UINT16)this->yDensity; |
485 | 0 | dinfo->density_unit = (UINT8)this->densityUnits; |
486 | 0 | } else { |
487 | 0 | if ((dst = _jinit_write_ppm(dinfo)) == NULL) |
488 | 0 | THROW("Could not initialize PPM writer"); |
489 | 0 | invert = this->bottomUp; |
490 | 0 | } |
491 | | |
492 | 0 | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
493 | |
|
494 | 0 | dst->output_file = file; |
495 | 0 | (*dst->start_output) (dinfo, dst); |
496 | 0 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
497 | |
|
498 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
499 | |
|
500 | 0 | while (dinfo->output_scanline < dinfo->output_height) { |
501 | 0 | _JSAMPLE *rowptr; |
502 | |
|
503 | 0 | if (invert) |
504 | 0 | rowptr = |
505 | 0 | (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch]; |
506 | 0 | else |
507 | 0 | rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch]; |
508 | 0 | memcpy(dst->_buffer[0], rowptr, |
509 | 0 | width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE)); |
510 | 0 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
511 | 0 | dinfo->output_scanline++; |
512 | 0 | } |
513 | |
|
514 | 0 | (*dst->finish_output) (dinfo, dst); |
515 | |
|
516 | 0 | bailout: |
517 | 0 | tj3Destroy(handle2); |
518 | 0 | if (file) fclose(file); |
519 | 0 | return retval; |
520 | |
|
521 | | #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */ |
522 | | |
523 | | GET_TJINSTANCE(handle, -1) |
524 | | THROW("16-bit data precision requires lossless JPEG,\n" |
525 | | "which was disabled at build time.") |
526 | | bailout: |
527 | | return retval; |
528 | | |
529 | | #endif |
530 | 0 | } Unexecuted instantiation: tj3SaveImage8 Unexecuted instantiation: tj3SaveImage12 Unexecuted instantiation: tj3SaveImage16 |
531 | | |
532 | | |
533 | | #undef _JSAMPLE |
534 | | #undef _JSAMPROW |
535 | | #undef _buffer |
536 | | #undef _jinit_read_ppm |
537 | | #undef _jinit_write_ppm |
538 | | #undef _jpeg_crop_scanline |
539 | | #undef _jpeg_read_scanlines |
540 | | #undef _jpeg_skip_scanlines |
541 | | #undef _jpeg_write_scanlines |