/src/libjpeg-turbo.3.0.x/turbojpeg-mp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2009-2026 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 | 98.7M | #define _JSAMPLE JSAMPLE |
34 | 60.3k | #define _JSAMPROW JSAMPROW |
35 | 98.6M | #define _buffer buffer |
36 | 49.0k | #define _jinit_read_ppm jinit_read_ppm |
37 | 0 | #define _jinit_write_ppm jinit_write_ppm |
38 | 1.88k | #define _jpeg_crop_scanline jpeg_crop_scanline |
39 | 28.5M | #define _jpeg_read_scanlines jpeg_read_scanlines |
40 | 3.74k | #define _jpeg_skip_scanlines jpeg_skip_scanlines |
41 | 18.3k | #define _jpeg_write_scanlines jpeg_write_scanlines |
42 | | #elif BITS_IN_JSAMPLE == 12 |
43 | 65.4M | #define _JSAMPLE J12SAMPLE |
44 | 40.2k | #define _JSAMPROW J12SAMPROW |
45 | 65.4M | #define _buffer buffer12 |
46 | 31.8k | #define _jinit_read_ppm j12init_read_ppm |
47 | 0 | #define _jinit_write_ppm j12init_write_ppm |
48 | 1.26k | #define _jpeg_crop_scanline jpeg12_crop_scanline |
49 | 13.6M | #define _jpeg_read_scanlines jpeg12_read_scanlines |
50 | 2.50k | #define _jpeg_skip_scanlines jpeg12_skip_scanlines |
51 | 18.2k | #define _jpeg_write_scanlines jpeg12_write_scanlines |
52 | | #elif BITS_IN_JSAMPLE == 16 |
53 | 14.3M | #define _JSAMPLE J16SAMPLE |
54 | 14.7k | #define _JSAMPROW J16SAMPROW |
55 | 14.2M | #define _buffer buffer16 |
56 | 11.5k | #define _jinit_read_ppm j16init_read_ppm |
57 | 0 | #define _jinit_write_ppm j16init_write_ppm |
58 | 3.11M | #define _jpeg_read_scanlines jpeg16_read_scanlines |
59 | 4.72k | #define _jpeg_write_scanlines jpeg16_write_scanlines |
60 | | #endif |
61 | | |
62 | 0 | #define _GET_NAME(name, suffix) name##suffix |
63 | 0 | #define GET_NAME(name, suffix) _GET_NAME(name, suffix) |
64 | 226k | #define _GET_STRING(name, suffix) #name #suffix |
65 | 226k | #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 | 41.3k | { |
75 | 41.3k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); |
76 | 41.3k | int i, retval = 0; |
77 | 41.3k | boolean alloc = TRUE; |
78 | 41.3k | _JSAMPROW *row_pointer = NULL; |
79 | | |
80 | 41.3k | GET_CINSTANCE(handle) |
81 | 41.3k | if ((this->init & COMPRESS) == 0) |
82 | 41.3k | THROW("Instance has not been initialized for compression"); |
83 | | |
84 | 41.3k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
85 | 41.3k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || |
86 | 41.3k | jpegSize == NULL) |
87 | 41.3k | THROW("Invalid argument"); |
88 | | |
89 | 41.3k | if (!this->lossless && this->quality == -1) |
90 | 41.3k | THROW("TJPARAM_QUALITY must be specified"); |
91 | 41.3k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) |
92 | 41.3k | THROW("TJPARAM_SUBSAMP must be specified"); |
93 | | |
94 | 41.3k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
96 | 41.3k | THROW("Invalid argument"); |
97 | | |
98 | 41.3k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) |
99 | 41.3k | THROW("Memory allocation failure"); |
100 | | |
101 | 41.3k | CATCH_LIBJPEG(this); |
102 | | |
103 | 41.2k | cinfo->image_width = width; |
104 | 41.2k | cinfo->image_height = height; |
105 | 41.2k | cinfo->data_precision = BITS_IN_JSAMPLE; |
106 | | |
107 | 41.2k | setCompDefaults(this, pixelFormat, FALSE); |
108 | 41.2k | if (this->noRealloc) { |
109 | 35.2k | alloc = FALSE; |
110 | 35.2k | *jpegSize = tj3JPEGBufSize(width, height, this->subsamp); |
111 | 35.2k | } |
112 | 41.2k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
113 | | |
114 | 41.2k | jpeg_start_compress(cinfo, TRUE); |
115 | 140M | for (i = 0; i < height; i++) { |
116 | 140M | if (this->bottomUp) |
117 | 20.2M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; |
118 | 120M | else |
119 | 120M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; |
120 | 140M | } |
121 | 82.5k | while (cinfo->next_scanline < cinfo->image_height) |
122 | 41.3k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
123 | 41.3k | cinfo->image_height - cinfo->next_scanline); |
124 | 41.2k | jpeg_finish_compress(cinfo); |
125 | | |
126 | 41.3k | bailout: |
127 | 41.3k | if (cinfo->global_state > CSTATE_START && alloc) |
128 | 32 | (*cinfo->dest->term_destination) (cinfo); |
129 | 41.3k | if (cinfo->global_state > CSTATE_START || retval == -1) |
130 | 32 | jpeg_abort_compress(cinfo); |
131 | 41.3k | free(row_pointer); |
132 | 41.3k | if (this->jerr.warning) retval = -1; |
133 | 41.3k | return retval; |
134 | 41.2k | } Line | Count | Source | 74 | 18.3k | { | 75 | 18.3k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 18.3k | int i, retval = 0; | 77 | 18.3k | boolean alloc = TRUE; | 78 | 18.3k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 18.3k | GET_CINSTANCE(handle) | 81 | 18.3k | if ((this->init & COMPRESS) == 0) | 82 | 18.3k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 18.3k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 18.3k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 18.3k | jpegSize == NULL) | 87 | 18.3k | THROW("Invalid argument"); | 88 | | | 89 | 18.3k | if (!this->lossless && this->quality == -1) | 90 | 18.3k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 18.3k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 18.3k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 18.3k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 18.3k | THROW("Invalid argument"); | 97 | | | 98 | 18.3k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 18.3k | THROW("Memory allocation failure"); | 100 | | | 101 | 18.3k | CATCH_LIBJPEG(this); | 102 | | | 103 | 18.3k | cinfo->image_width = width; | 104 | 18.3k | cinfo->image_height = height; | 105 | 18.3k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | | | 107 | 18.3k | setCompDefaults(this, pixelFormat, FALSE); | 108 | 18.3k | if (this->noRealloc) { | 109 | 15.6k | alloc = FALSE; | 110 | 15.6k | *jpegSize = tj3JPEGBufSize(width, height, this->subsamp); | 111 | 15.6k | } | 112 | 18.3k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 113 | | | 114 | 18.3k | jpeg_start_compress(cinfo, TRUE); | 115 | 60.9M | for (i = 0; i < height; i++) { | 116 | 60.8M | if (this->bottomUp) | 117 | 8.84M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 118 | 52.0M | else | 119 | 52.0M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 120 | 60.8M | } | 121 | 36.7k | while (cinfo->next_scanline < cinfo->image_height) | 122 | 18.3k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 123 | 18.3k | cinfo->image_height - cinfo->next_scanline); | 124 | 18.3k | jpeg_finish_compress(cinfo); | 125 | | | 126 | 18.3k | bailout: | 127 | 18.3k | if (cinfo->global_state > CSTATE_START && alloc) | 128 | 0 | (*cinfo->dest->term_destination) (cinfo); | 129 | 18.3k | if (cinfo->global_state > CSTATE_START || retval == -1) | 130 | 0 | jpeg_abort_compress(cinfo); | 131 | 18.3k | free(row_pointer); | 132 | 18.3k | if (this->jerr.warning) retval = -1; | 133 | 18.3k | return retval; | 134 | 18.3k | } |
Line | Count | Source | 74 | 18.2k | { | 75 | 18.2k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 18.2k | int i, retval = 0; | 77 | 18.2k | boolean alloc = TRUE; | 78 | 18.2k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 18.2k | GET_CINSTANCE(handle) | 81 | 18.2k | if ((this->init & COMPRESS) == 0) | 82 | 18.2k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 18.2k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 18.2k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 18.2k | jpegSize == NULL) | 87 | 18.2k | THROW("Invalid argument"); | 88 | | | 89 | 18.2k | if (!this->lossless && this->quality == -1) | 90 | 18.2k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 18.2k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 18.2k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 18.2k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 18.2k | THROW("Invalid argument"); | 97 | | | 98 | 18.2k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 18.2k | THROW("Memory allocation failure"); | 100 | | | 101 | 18.2k | CATCH_LIBJPEG(this); | 102 | | | 103 | 18.2k | cinfo->image_width = width; | 104 | 18.2k | cinfo->image_height = height; | 105 | 18.2k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | | | 107 | 18.2k | setCompDefaults(this, pixelFormat, FALSE); | 108 | 18.2k | if (this->noRealloc) { | 109 | 15.5k | alloc = FALSE; | 110 | 15.5k | *jpegSize = tj3JPEGBufSize(width, height, this->subsamp); | 111 | 15.5k | } | 112 | 18.2k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 113 | | | 114 | 18.2k | jpeg_start_compress(cinfo, TRUE); | 115 | 65.3M | for (i = 0; i < height; i++) { | 116 | 65.2M | if (this->bottomUp) | 117 | 9.33M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 118 | 55.9M | else | 119 | 55.9M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 120 | 65.2M | } | 121 | 36.4k | while (cinfo->next_scanline < cinfo->image_height) | 122 | 18.2k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 123 | 18.2k | cinfo->image_height - cinfo->next_scanline); | 124 | 18.2k | jpeg_finish_compress(cinfo); | 125 | | | 126 | 18.2k | bailout: | 127 | 18.2k | if (cinfo->global_state > CSTATE_START && alloc) | 128 | 0 | (*cinfo->dest->term_destination) (cinfo); | 129 | 18.2k | if (cinfo->global_state > CSTATE_START || retval == -1) | 130 | 0 | jpeg_abort_compress(cinfo); | 131 | 18.2k | free(row_pointer); | 132 | 18.2k | if (this->jerr.warning) retval = -1; | 133 | 18.2k | return retval; | 134 | 18.2k | } |
Line | Count | Source | 74 | 4.72k | { | 75 | 4.72k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 4.72k | int i, retval = 0; | 77 | 4.72k | boolean alloc = TRUE; | 78 | 4.72k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 4.72k | GET_CINSTANCE(handle) | 81 | 4.72k | if ((this->init & COMPRESS) == 0) | 82 | 4.72k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 4.72k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 4.72k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 4.72k | jpegSize == NULL) | 87 | 4.72k | THROW("Invalid argument"); | 88 | | | 89 | 4.72k | if (!this->lossless && this->quality == -1) | 90 | 4.72k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 4.72k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 4.72k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 4.72k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 4.72k | THROW("Invalid argument"); | 97 | | | 98 | 4.72k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 4.72k | THROW("Memory allocation failure"); | 100 | | | 101 | 4.72k | CATCH_LIBJPEG(this); | 102 | | | 103 | 4.69k | cinfo->image_width = width; | 104 | 4.69k | cinfo->image_height = height; | 105 | 4.69k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | | | 107 | 4.69k | setCompDefaults(this, pixelFormat, FALSE); | 108 | 4.69k | if (this->noRealloc) { | 109 | 4.03k | alloc = FALSE; | 110 | 4.03k | *jpegSize = tj3JPEGBufSize(width, height, this->subsamp); | 111 | 4.03k | } | 112 | 4.69k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 113 | | | 114 | 4.69k | jpeg_start_compress(cinfo, TRUE); | 115 | 14.2M | for (i = 0; i < height; i++) { | 116 | 14.2M | if (this->bottomUp) | 117 | 2.03M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 118 | 12.2M | else | 119 | 12.2M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 120 | 14.2M | } | 121 | 9.42k | while (cinfo->next_scanline < cinfo->image_height) | 122 | 4.72k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 123 | 4.72k | cinfo->image_height - cinfo->next_scanline); | 124 | 4.69k | jpeg_finish_compress(cinfo); | 125 | | | 126 | 4.72k | bailout: | 127 | 4.72k | if (cinfo->global_state > CSTATE_START && alloc) | 128 | 32 | (*cinfo->dest->term_destination) (cinfo); | 129 | 4.72k | if (cinfo->global_state > CSTATE_START || retval == -1) | 130 | 32 | jpeg_abort_compress(cinfo); | 131 | 4.72k | free(row_pointer); | 132 | 4.72k | if (this->jerr.warning) retval = -1; | 133 | 4.72k | return retval; | 134 | 4.69k | } |
|
135 | | |
136 | | |
137 | | /******************************* Decompressor ********************************/ |
138 | | |
139 | | /* TurboJPEG 3+ */ |
140 | | DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE) |
141 | | (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize, |
142 | | _JSAMPLE *dstBuf, int pitch, int pixelFormat) |
143 | 73.9k | { |
144 | 73.9k | static const char FUNCTION_NAME[] = |
145 | 73.9k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
146 | 73.9k | _JSAMPROW *row_pointer = NULL; |
147 | 73.9k | int croppedHeight, i, retval = 0; |
148 | | #if BITS_IN_JSAMPLE != 16 |
149 | | int scaledWidth; |
150 | | #endif |
151 | 73.9k | struct my_progress_mgr progress; |
152 | | |
153 | 73.9k | GET_DINSTANCE(handle); |
154 | 73.9k | if ((this->init & DECOMPRESS) == 0) |
155 | 73.9k | THROW("Instance has not been initialized for decompression"); |
156 | | |
157 | 73.9k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
158 | 73.9k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
159 | 73.9k | THROW("Invalid argument"); |
160 | | |
161 | 73.9k | if (this->scanLimit) { |
162 | 73.9k | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
163 | 73.9k | progress.pub.progress_monitor = my_progress_monitor; |
164 | 73.9k | progress.this = this; |
165 | 73.9k | dinfo->progress = &progress.pub; |
166 | 73.9k | } else |
167 | 0 | dinfo->progress = NULL; |
168 | | |
169 | 73.9k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
170 | | |
171 | 73.9k | CATCH_LIBJPEG(this); |
172 | | |
173 | 73.9k | if (dinfo->global_state <= DSTATE_INHEADER) { |
174 | 73.9k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
175 | 73.9k | jpeg_read_header(dinfo, TRUE); |
176 | 73.9k | } |
177 | 32.6k | setDecompParameters(this); |
178 | 32.6k | if (this->maxPixels && |
179 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > |
180 | 0 | (unsigned long long)this->maxPixels) |
181 | 32.6k | THROW("Image is too large"); |
182 | 32.6k | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
183 | | #if BITS_IN_JSAMPLE != 16 |
184 | 29.6k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
185 | | #endif |
186 | 32.6k | dinfo->do_fancy_upsampling = !this->fastUpsample; |
187 | 32.6k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
188 | | |
189 | 32.6k | dinfo->scale_num = this->scalingFactor.num; |
190 | 32.6k | dinfo->scale_denom = this->scalingFactor.denom; |
191 | | |
192 | 32.6k | jpeg_start_decompress(dinfo); |
193 | | |
194 | | #if BITS_IN_JSAMPLE != 16 |
195 | 29.6k | if (this->croppingRegion.x != 0 || |
196 | 26.5k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
197 | 3.14k | JDIMENSION crop_x = this->croppingRegion.x; |
198 | 3.14k | JDIMENSION crop_w = this->croppingRegion.w; |
199 | | |
200 | 3.14k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
201 | 3.14k | if ((int)crop_x != this->croppingRegion.x) |
202 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" |
203 | 3.14k | "actual (%d) cropping region left boundary", |
204 | 3.14k | this->croppingRegion.x, (int)crop_x); |
205 | 3.14k | if ((int)crop_w != this->croppingRegion.w) |
206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
207 | 3.14k | "actual (%d) cropping region width", |
208 | 3.14k | this->croppingRegion.w, (int)crop_w); |
209 | 3.14k | } |
210 | 29.6k | #endif |
211 | | |
212 | 32.6k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
213 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) |
214 | 32.6k | THROW("Invalid argument"); |
215 | | |
216 | 32.6k | croppedHeight = dinfo->output_height; |
217 | | #if BITS_IN_JSAMPLE != 16 |
218 | 29.6k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
219 | 3.14k | croppedHeight = this->croppingRegion.h; |
220 | | #endif |
221 | 32.6k | if ((row_pointer = |
222 | 32.6k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
223 | 32.6k | THROW("Memory allocation failure"); |
224 | 32.6k | CATCH_LIBJPEG(this); |
225 | 118M | for (i = 0; i < (int)croppedHeight; i++) { |
226 | 118M | if (this->bottomUp) |
227 | 43.0M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
228 | 75.8M | else |
229 | 75.8M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
230 | 118M | } |
231 | | |
232 | | #if BITS_IN_JSAMPLE != 16 |
233 | 26.5k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
234 | 3.14k | if (this->croppingRegion.y != 0) { |
235 | 3.14k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
236 | | |
237 | 3.14k | if ((int)lines != this->croppingRegion.y) |
238 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
239 | | "actual (%d) cropping region upper boundary", |
240 | 3.14k | this->croppingRegion.y, (int)lines); |
241 | 3.14k | } |
242 | 119k | while ((int)dinfo->output_scanline < |
243 | 119k | this->croppingRegion.y + this->croppingRegion.h) |
244 | 116k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
245 | 116k | this->croppingRegion.y], |
246 | 116k | this->croppingRegion.y + this->croppingRegion.h - |
247 | 116k | dinfo->output_scanline); |
248 | 3.14k | if (this->croppingRegion.y + this->croppingRegion.h != |
249 | 3.14k | (int)dinfo->output_height) { |
250 | 3.10k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
251 | | this->croppingRegion.y - |
252 | | this->croppingRegion.h); |
253 | | |
254 | 3.10k | if (lines != dinfo->output_height - this->croppingRegion.y - |
255 | 3.10k | this->croppingRegion.h) |
256 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
257 | 3.10k | "actual (%d) cropping region lower boundary", |
258 | 3.10k | this->croppingRegion.y + this->croppingRegion.h, |
259 | 3.10k | (int)(dinfo->output_height - lines)); |
260 | 3.10k | } |
261 | 3.14k | } else |
262 | 21.5k | #endif |
263 | 21.5k | { |
264 | 45.2M | while (dinfo->output_scanline < dinfo->output_height) |
265 | 45.2M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
266 | 45.2M | dinfo->output_height - dinfo->output_scanline); |
267 | 21.5k | } |
268 | 24.6k | jpeg_finish_decompress(dinfo); |
269 | | |
270 | 73.9k | bailout: |
271 | 73.9k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
272 | 73.9k | free(row_pointer); |
273 | 73.9k | if (this->jerr.warning) retval = -1; |
274 | 73.9k | return retval; |
275 | 24.6k | } Line | Count | Source | 143 | 41.9k | { | 144 | 41.9k | static const char FUNCTION_NAME[] = | 145 | 41.9k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 146 | 41.9k | _JSAMPROW *row_pointer = NULL; | 147 | 41.9k | int croppedHeight, i, retval = 0; | 148 | 41.9k | #if BITS_IN_JSAMPLE != 16 | 149 | 41.9k | int scaledWidth; | 150 | 41.9k | #endif | 151 | 41.9k | struct my_progress_mgr progress; | 152 | | | 153 | 41.9k | GET_DINSTANCE(handle); | 154 | 41.9k | if ((this->init & DECOMPRESS) == 0) | 155 | 41.9k | THROW("Instance has not been initialized for decompression"); | 156 | | | 157 | 41.9k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 158 | 41.9k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 159 | 41.9k | THROW("Invalid argument"); | 160 | | | 161 | 41.9k | if (this->scanLimit) { | 162 | 41.9k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 163 | 41.9k | progress.pub.progress_monitor = my_progress_monitor; | 164 | 41.9k | progress.this = this; | 165 | 41.9k | dinfo->progress = &progress.pub; | 166 | 41.9k | } else | 167 | 0 | dinfo->progress = NULL; | 168 | | | 169 | 41.9k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 170 | | | 171 | 41.9k | CATCH_LIBJPEG(this); | 172 | | | 173 | 41.9k | if (dinfo->global_state <= DSTATE_INHEADER) { | 174 | 41.9k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 175 | 41.9k | jpeg_read_header(dinfo, TRUE); | 176 | 41.9k | } | 177 | 18.5k | setDecompParameters(this); | 178 | 18.5k | if (this->maxPixels && | 179 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 180 | 0 | (unsigned long long)this->maxPixels) | 181 | 18.5k | THROW("Image is too large"); | 182 | 18.5k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 183 | 18.5k | #if BITS_IN_JSAMPLE != 16 | 184 | 18.5k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 185 | 18.5k | #endif | 186 | 18.5k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 187 | 18.5k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 188 | | | 189 | 18.5k | dinfo->scale_num = this->scalingFactor.num; | 190 | 18.5k | dinfo->scale_denom = this->scalingFactor.denom; | 191 | | | 192 | 18.5k | jpeg_start_decompress(dinfo); | 193 | | | 194 | 18.5k | #if BITS_IN_JSAMPLE != 16 | 195 | 18.5k | if (this->croppingRegion.x != 0 || | 196 | 16.7k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 197 | 1.88k | JDIMENSION crop_x = this->croppingRegion.x; | 198 | 1.88k | JDIMENSION crop_w = this->croppingRegion.w; | 199 | | | 200 | 1.88k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 201 | 1.88k | if ((int)crop_x != this->croppingRegion.x) | 202 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" | 203 | 1.88k | "actual (%d) cropping region left boundary", | 204 | 1.88k | this->croppingRegion.x, (int)crop_x); | 205 | 1.87k | if ((int)crop_w != this->croppingRegion.w) | 206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | 1.87k | "actual (%d) cropping region width", | 208 | 1.87k | this->croppingRegion.w, (int)crop_w); | 209 | 1.87k | } | 210 | 18.5k | #endif | 211 | | | 212 | 18.5k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 213 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 214 | 18.5k | THROW("Invalid argument"); | 215 | | | 216 | 18.5k | croppedHeight = dinfo->output_height; | 217 | 18.5k | #if BITS_IN_JSAMPLE != 16 | 218 | 18.5k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 219 | 1.87k | croppedHeight = this->croppingRegion.h; | 220 | 18.5k | #endif | 221 | 18.5k | if ((row_pointer = | 222 | 18.5k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 223 | 18.5k | THROW("Memory allocation failure"); | 224 | 18.5k | CATCH_LIBJPEG(this); | 225 | 64.5M | for (i = 0; i < (int)croppedHeight; i++) { | 226 | 64.5M | if (this->bottomUp) | 227 | 23.2M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 228 | 41.2M | else | 229 | 41.2M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 230 | 64.5M | } | 231 | | | 232 | 15.3k | #if BITS_IN_JSAMPLE != 16 | 233 | 16.7k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 234 | 1.87k | if (this->croppingRegion.y != 0) { | 235 | 1.87k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 236 | | | 237 | 1.87k | if ((int)lines != this->croppingRegion.y) | 238 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 239 | 1.87k | "actual (%d) cropping region upper boundary", | 240 | 1.87k | this->croppingRegion.y, (int)lines); | 241 | 1.87k | } | 242 | 79.2k | while ((int)dinfo->output_scanline < | 243 | 79.2k | this->croppingRegion.y + this->croppingRegion.h) | 244 | 77.3k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 245 | 77.3k | this->croppingRegion.y], | 246 | 77.3k | this->croppingRegion.y + this->croppingRegion.h - | 247 | 77.3k | dinfo->output_scanline); | 248 | 1.87k | if (this->croppingRegion.y + this->croppingRegion.h != | 249 | 1.87k | (int)dinfo->output_height) { | 250 | 1.86k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 251 | 1.86k | this->croppingRegion.y - | 252 | 1.86k | this->croppingRegion.h); | 253 | | | 254 | 1.86k | if (lines != dinfo->output_height - this->croppingRegion.y - | 255 | 1.86k | this->croppingRegion.h) | 256 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 257 | 1.86k | "actual (%d) cropping region lower boundary", | 258 | 1.86k | this->croppingRegion.y + this->croppingRegion.h, | 259 | 1.86k | (int)(dinfo->output_height - lines)); | 260 | 1.86k | } | 261 | 1.87k | } else | 262 | 13.4k | #endif | 263 | 13.4k | { | 264 | 28.5M | while (dinfo->output_scanline < dinfo->output_height) | 265 | 28.5M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 266 | 28.5M | dinfo->output_height - dinfo->output_scanline); | 267 | 13.4k | } | 268 | 15.3k | jpeg_finish_decompress(dinfo); | 269 | | | 270 | 41.9k | bailout: | 271 | 41.9k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 272 | 41.9k | free(row_pointer); | 273 | 41.9k | if (this->jerr.warning) retval = -1; | 274 | 41.9k | return retval; | 275 | 15.3k | } |
Line | Count | Source | 143 | 21.9k | { | 144 | 21.9k | static const char FUNCTION_NAME[] = | 145 | 21.9k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 146 | 21.9k | _JSAMPROW *row_pointer = NULL; | 147 | 21.9k | int croppedHeight, i, retval = 0; | 148 | 21.9k | #if BITS_IN_JSAMPLE != 16 | 149 | 21.9k | int scaledWidth; | 150 | 21.9k | #endif | 151 | 21.9k | struct my_progress_mgr progress; | 152 | | | 153 | 21.9k | GET_DINSTANCE(handle); | 154 | 21.9k | if ((this->init & DECOMPRESS) == 0) | 155 | 21.9k | THROW("Instance has not been initialized for decompression"); | 156 | | | 157 | 21.9k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 158 | 21.9k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 159 | 21.9k | THROW("Invalid argument"); | 160 | | | 161 | 21.9k | if (this->scanLimit) { | 162 | 21.9k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 163 | 21.9k | progress.pub.progress_monitor = my_progress_monitor; | 164 | 21.9k | progress.this = this; | 165 | 21.9k | dinfo->progress = &progress.pub; | 166 | 21.9k | } else | 167 | 0 | dinfo->progress = NULL; | 168 | | | 169 | 21.9k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 170 | | | 171 | 21.9k | CATCH_LIBJPEG(this); | 172 | | | 173 | 21.9k | if (dinfo->global_state <= DSTATE_INHEADER) { | 174 | 21.9k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 175 | 21.9k | jpeg_read_header(dinfo, TRUE); | 176 | 21.9k | } | 177 | 11.0k | setDecompParameters(this); | 178 | 11.0k | if (this->maxPixels && | 179 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 180 | 0 | (unsigned long long)this->maxPixels) | 181 | 11.0k | THROW("Image is too large"); | 182 | 11.0k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 183 | 11.0k | #if BITS_IN_JSAMPLE != 16 | 184 | 11.0k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 185 | 11.0k | #endif | 186 | 11.0k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 187 | 11.0k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 188 | | | 189 | 11.0k | dinfo->scale_num = this->scalingFactor.num; | 190 | 11.0k | dinfo->scale_denom = this->scalingFactor.denom; | 191 | | | 192 | 11.0k | jpeg_start_decompress(dinfo); | 193 | | | 194 | 11.0k | #if BITS_IN_JSAMPLE != 16 | 195 | 11.0k | if (this->croppingRegion.x != 0 || | 196 | 9.82k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 197 | 1.26k | JDIMENSION crop_x = this->croppingRegion.x; | 198 | 1.26k | JDIMENSION crop_w = this->croppingRegion.w; | 199 | | | 200 | 1.26k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 201 | 1.26k | if ((int)crop_x != this->croppingRegion.x) | 202 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 203 | 1.26k | "actual (%d) cropping region left boundary", | 204 | 1.26k | this->croppingRegion.x, (int)crop_x); | 205 | 1.26k | if ((int)crop_w != this->croppingRegion.w) | 206 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | 1.26k | "actual (%d) cropping region width", | 208 | 1.26k | this->croppingRegion.w, (int)crop_w); | 209 | 1.26k | } | 210 | 11.0k | #endif | 211 | | | 212 | 11.0k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 213 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 214 | 11.0k | THROW("Invalid argument"); | 215 | | | 216 | 11.0k | croppedHeight = dinfo->output_height; | 217 | 11.0k | #if BITS_IN_JSAMPLE != 16 | 218 | 11.0k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 219 | 1.26k | croppedHeight = this->croppingRegion.h; | 220 | 11.0k | #endif | 221 | 11.0k | if ((row_pointer = | 222 | 11.0k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 223 | 11.0k | THROW("Memory allocation failure"); | 224 | 11.0k | CATCH_LIBJPEG(this); | 225 | 32.3M | for (i = 0; i < (int)croppedHeight; i++) { | 226 | 32.3M | if (this->bottomUp) | 227 | 12.4M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 228 | 19.8M | else | 229 | 19.8M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 230 | 32.3M | } | 231 | | | 232 | 9.37k | #if BITS_IN_JSAMPLE != 16 | 233 | 9.82k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 234 | 1.26k | if (this->croppingRegion.y != 0) { | 235 | 1.26k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 236 | | | 237 | 1.26k | if ((int)lines != this->croppingRegion.y) | 238 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 239 | 1.26k | "actual (%d) cropping region upper boundary", | 240 | 1.26k | this->croppingRegion.y, (int)lines); | 241 | 1.26k | } | 242 | 40.7k | while ((int)dinfo->output_scanline < | 243 | 40.7k | this->croppingRegion.y + this->croppingRegion.h) | 244 | 39.4k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 245 | 39.4k | this->croppingRegion.y], | 246 | 39.4k | this->croppingRegion.y + this->croppingRegion.h - | 247 | 39.4k | dinfo->output_scanline); | 248 | 1.26k | if (this->croppingRegion.y + this->croppingRegion.h != | 249 | 1.26k | (int)dinfo->output_height) { | 250 | 1.23k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 251 | 1.23k | this->croppingRegion.y - | 252 | 1.23k | this->croppingRegion.h); | 253 | | | 254 | 1.23k | if (lines != dinfo->output_height - this->croppingRegion.y - | 255 | 1.23k | this->croppingRegion.h) | 256 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 257 | 1.23k | "actual (%d) cropping region lower boundary", | 258 | 1.23k | this->croppingRegion.y + this->croppingRegion.h, | 259 | 1.23k | (int)(dinfo->output_height - lines)); | 260 | 1.23k | } | 261 | 1.26k | } else | 262 | 8.11k | #endif | 263 | 8.11k | { | 264 | 13.6M | while (dinfo->output_scanline < dinfo->output_height) | 265 | 13.6M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 266 | 13.6M | dinfo->output_height - dinfo->output_scanline); | 267 | 8.11k | } | 268 | 9.37k | jpeg_finish_decompress(dinfo); | 269 | | | 270 | 21.9k | bailout: | 271 | 21.9k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 272 | 21.9k | free(row_pointer); | 273 | 21.9k | if (this->jerr.warning) retval = -1; | 274 | 21.9k | return retval; | 275 | 9.37k | } |
Line | Count | Source | 143 | 10.0k | { | 144 | 10.0k | static const char FUNCTION_NAME[] = | 145 | 10.0k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 146 | 10.0k | _JSAMPROW *row_pointer = NULL; | 147 | 10.0k | int croppedHeight, i, retval = 0; | 148 | | #if BITS_IN_JSAMPLE != 16 | 149 | | int scaledWidth; | 150 | | #endif | 151 | 10.0k | struct my_progress_mgr progress; | 152 | | | 153 | 10.0k | GET_DINSTANCE(handle); | 154 | 10.0k | if ((this->init & DECOMPRESS) == 0) | 155 | 10.0k | THROW("Instance has not been initialized for decompression"); | 156 | | | 157 | 10.0k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 158 | 10.0k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 159 | 10.0k | THROW("Invalid argument"); | 160 | | | 161 | 10.0k | if (this->scanLimit) { | 162 | 10.0k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 163 | 10.0k | progress.pub.progress_monitor = my_progress_monitor; | 164 | 10.0k | progress.this = this; | 165 | 10.0k | dinfo->progress = &progress.pub; | 166 | 10.0k | } else | 167 | 0 | dinfo->progress = NULL; | 168 | | | 169 | 10.0k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 170 | | | 171 | 10.0k | CATCH_LIBJPEG(this); | 172 | | | 173 | 10.0k | if (dinfo->global_state <= DSTATE_INHEADER) { | 174 | 10.0k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 175 | 10.0k | jpeg_read_header(dinfo, TRUE); | 176 | 10.0k | } | 177 | 2.94k | setDecompParameters(this); | 178 | 2.94k | if (this->maxPixels && | 179 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 180 | 0 | (unsigned long long)this->maxPixels) | 181 | 2.94k | THROW("Image is too large"); | 182 | 2.94k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 183 | | #if BITS_IN_JSAMPLE != 16 | 184 | | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 185 | | #endif | 186 | 2.94k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 187 | 2.94k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 188 | | | 189 | 2.94k | dinfo->scale_num = this->scalingFactor.num; | 190 | 2.94k | dinfo->scale_denom = this->scalingFactor.denom; | 191 | | | 192 | 2.94k | jpeg_start_decompress(dinfo); | 193 | | | 194 | | #if BITS_IN_JSAMPLE != 16 | 195 | | if (this->croppingRegion.x != 0 || | 196 | | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 197 | | JDIMENSION crop_x = this->croppingRegion.x; | 198 | | JDIMENSION crop_w = this->croppingRegion.w; | 199 | | | 200 | | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 201 | | if ((int)crop_x != this->croppingRegion.x) | 202 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 203 | | "actual (%d) cropping region left boundary", | 204 | | this->croppingRegion.x, (int)crop_x); | 205 | | if ((int)crop_w != this->croppingRegion.w) | 206 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 207 | | "actual (%d) cropping region width", | 208 | | this->croppingRegion.w, (int)crop_w); | 209 | | } | 210 | | #endif | 211 | | | 212 | 2.94k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 213 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 214 | 2.94k | THROW("Invalid argument"); | 215 | | | 216 | 2.94k | croppedHeight = dinfo->output_height; | 217 | | #if BITS_IN_JSAMPLE != 16 | 218 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 219 | | croppedHeight = this->croppingRegion.h; | 220 | | #endif | 221 | 2.94k | if ((row_pointer = | 222 | 2.94k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 223 | 2.94k | THROW("Memory allocation failure"); | 224 | 2.94k | CATCH_LIBJPEG(this); | 225 | 21.9M | for (i = 0; i < (int)croppedHeight; i++) { | 226 | 21.9M | if (this->bottomUp) | 227 | 7.31M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 228 | 14.6M | else | 229 | 14.6M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 230 | 21.9M | } | 231 | | | 232 | | #if BITS_IN_JSAMPLE != 16 | 233 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 234 | | if (this->croppingRegion.y != 0) { | 235 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 236 | | | 237 | | if ((int)lines != this->croppingRegion.y) | 238 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 239 | | "actual (%d) cropping region upper boundary", | 240 | | this->croppingRegion.y, (int)lines); | 241 | | } | 242 | | while ((int)dinfo->output_scanline < | 243 | | this->croppingRegion.y + this->croppingRegion.h) | 244 | | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 245 | | this->croppingRegion.y], | 246 | | this->croppingRegion.y + this->croppingRegion.h - | 247 | | dinfo->output_scanline); | 248 | | if (this->croppingRegion.y + this->croppingRegion.h != | 249 | | (int)dinfo->output_height) { | 250 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 251 | | this->croppingRegion.y - | 252 | | this->croppingRegion.h); | 253 | | | 254 | | if (lines != dinfo->output_height - this->croppingRegion.y - | 255 | | this->croppingRegion.h) | 256 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 257 | | "actual (%d) cropping region lower boundary", | 258 | | this->croppingRegion.y + this->croppingRegion.h, | 259 | | (int)(dinfo->output_height - lines)); | 260 | | } | 261 | | } else | 262 | | #endif | 263 | 931 | { | 264 | 3.11M | while (dinfo->output_scanline < dinfo->output_height) | 265 | 3.11M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 266 | 3.11M | dinfo->output_height - dinfo->output_scanline); | 267 | 931 | } | 268 | 931 | jpeg_finish_decompress(dinfo); | 269 | | | 270 | 10.0k | bailout: | 271 | 10.0k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 272 | 10.0k | free(row_pointer); | 273 | 10.0k | if (this->jerr.warning) retval = -1; | 274 | 10.0k | return retval; | 275 | 931 | } |
|
276 | | |
277 | | |
278 | | /*************************** Packed-Pixel Image I/O **************************/ |
279 | | |
280 | | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
281 | | |
282 | | /* TurboJPEG 3+ */ |
283 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
284 | | static |
285 | | #endif |
286 | | _JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
287 | | (tjhandle handle, FILE *file, int *width, int align, int *height, |
288 | | int *pixelFormat) |
289 | 111k | { |
290 | 111k | static const char FUNCTION_NAME[] = |
291 | 111k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
292 | | |
293 | 111k | int retval = 0, tempc; |
294 | 111k | size_t pitch; |
295 | 111k | tjhandle handle2 = NULL; |
296 | 111k | tjinstance *this2; |
297 | 111k | j_compress_ptr cinfo = NULL; |
298 | 111k | cjpeg_source_ptr src; |
299 | 111k | _JSAMPLE *dstBuf = NULL; |
300 | 111k | boolean invert; |
301 | | |
302 | 111k | GET_TJINSTANCE(handle, NULL) |
303 | | |
304 | 111k | if (!file || !width || align < 1 || !height || !pixelFormat || |
305 | 111k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
306 | 111k | THROW("Invalid argument"); |
307 | 111k | if ((align & (align - 1)) != 0) |
308 | 111k | THROW("Alignment must be a power of 2"); |
309 | | |
310 | | /* The instance handle passed to this function is used only for parameter |
311 | | retrieval. Create a new temporary instance to avoid interfering with the |
312 | | libjpeg state of the primary instance. */ |
313 | 111k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; |
314 | 111k | this2 = (tjinstance *)handle2; |
315 | 111k | cinfo = &this2->cinfo; |
316 | | |
317 | 111k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
318 | 0 | THROW_UNIX("Could not read input file") |
319 | 111k | else if (tempc == EOF) |
320 | 111k | THROW("Input file contains no data"); |
321 | | |
322 | 111k | CATCH_LIBJPEG(this2); |
323 | | |
324 | 85.9k | cinfo->data_precision = BITS_IN_JSAMPLE; |
325 | 85.9k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
326 | 85.9k | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
327 | 85.9k | if (tempc == 'B') { |
328 | 18.3k | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
329 | 18.3k | THROW("Could not initialize bitmap loader"); |
330 | 18.3k | invert = !this->bottomUp; |
331 | 92.4k | } else if (tempc == 'P') { |
332 | 92.4k | if ((src = _jinit_read_ppm(cinfo)) == NULL) |
333 | 92.4k | THROW("Could not initialize PPM loader"); |
334 | 92.4k | invert = this->bottomUp; |
335 | 92.4k | } else |
336 | 18.4E | THROW("Unsupported file type"); |
337 | | |
338 | 110k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
339 | | |
340 | 110k | src->input_file = file; |
341 | | /* Refuse to load images larger than the specified size. */ |
342 | 110k | src->max_pixels = this->maxPixels; |
343 | 110k | (*src->start_input) (cinfo, src); |
344 | 110k | if (tempc == 'B') { |
345 | 5.24k | if (cinfo->X_density && cinfo->Y_density) { |
346 | 1.35k | this->xDensity = cinfo->X_density; |
347 | 1.35k | this->yDensity = cinfo->Y_density; |
348 | 1.35k | this->densityUnits = cinfo->density_unit; |
349 | 1.35k | } |
350 | 5.24k | } |
351 | 110k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
352 | | |
353 | 110k | *width = cinfo->image_width; *height = cinfo->image_height; |
354 | 110k | *pixelFormat = cs2pf[cinfo->in_color_space]; |
355 | | |
356 | 110k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
357 | 110k | if ( |
358 | | #if ULLONG_MAX > SIZE_MAX |
359 | | (unsigned long long)pitch * (unsigned long long)(*height) > |
360 | | (unsigned long long)((size_t)-1) || |
361 | | #endif |
362 | 110k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * |
363 | 110k | sizeof(_JSAMPLE))) == NULL) |
364 | 110k | THROW("Memory allocation failure"); |
365 | | |
366 | 110k | CATCH_LIBJPEG(this2); |
367 | | |
368 | 178M | while (cinfo->next_scanline < cinfo->image_height) { |
369 | 178M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
370 | | |
371 | 356M | for (i = 0; i < nlines; i++) { |
372 | 178M | _JSAMPLE *dstptr; |
373 | 178M | int row; |
374 | | |
375 | 178M | row = cinfo->next_scanline + i; |
376 | 178M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
377 | 147M | else dstptr = &dstBuf[row * pitch]; |
378 | 178M | memcpy(dstptr, src->_buffer[i], |
379 | 178M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); |
380 | 178M | } |
381 | 178M | cinfo->next_scanline += nlines; |
382 | 178M | } |
383 | | |
384 | 78.5k | (*src->finish_input) (cinfo, src); |
385 | | |
386 | 111k | bailout: |
387 | 111k | tj3Destroy(handle2); |
388 | 111k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
389 | 111k | return dstBuf; |
390 | 78.5k | } _tj3LoadImageFromFileHandle8 Line | Count | Source | 289 | 67.7k | { | 290 | 67.7k | static const char FUNCTION_NAME[] = | 291 | 67.7k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 292 | | | 293 | 67.7k | int retval = 0, tempc; | 294 | 67.7k | size_t pitch; | 295 | 67.7k | tjhandle handle2 = NULL; | 296 | 67.7k | tjinstance *this2; | 297 | 67.7k | j_compress_ptr cinfo = NULL; | 298 | 67.7k | cjpeg_source_ptr src; | 299 | 67.7k | _JSAMPLE *dstBuf = NULL; | 300 | 67.7k | boolean invert; | 301 | | | 302 | 67.7k | GET_TJINSTANCE(handle, NULL) | 303 | | | 304 | 67.7k | if (!file || !width || align < 1 || !height || !pixelFormat || | 305 | 67.7k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 306 | 67.7k | THROW("Invalid argument"); | 307 | 67.7k | if ((align & (align - 1)) != 0) | 308 | 67.7k | THROW("Alignment must be a power of 2"); | 309 | | | 310 | | /* The instance handle passed to this function is used only for parameter | 311 | | retrieval. Create a new temporary instance to avoid interfering with the | 312 | | libjpeg state of the primary instance. */ | 313 | 67.7k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 314 | 67.7k | this2 = (tjinstance *)handle2; | 315 | 67.7k | cinfo = &this2->cinfo; | 316 | | | 317 | 67.7k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 318 | 0 | THROW_UNIX("Could not read input file") | 319 | 67.7k | else if (tempc == EOF) | 320 | 67.7k | THROW("Input file contains no data"); | 321 | | | 322 | 67.7k | CATCH_LIBJPEG(this2); | 323 | | | 324 | 48.4k | cinfo->data_precision = BITS_IN_JSAMPLE; | 325 | 48.4k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 326 | 48.4k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 327 | 48.4k | if (tempc == 'B') { | 328 | 18.3k | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 329 | 18.3k | THROW("Could not initialize bitmap loader"); | 330 | 18.3k | invert = !this->bottomUp; | 331 | 49.0k | } else if (tempc == 'P') { | 332 | 49.0k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 333 | 49.0k | THROW("Could not initialize PPM loader"); | 334 | 49.0k | invert = this->bottomUp; | 335 | 49.0k | } else | 336 | 18.4E | THROW("Unsupported file type"); | 337 | | | 338 | 67.3k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 339 | | | 340 | 67.3k | src->input_file = file; | 341 | | /* Refuse to load images larger than the specified size. */ | 342 | 67.3k | src->max_pixels = this->maxPixels; | 343 | 67.3k | (*src->start_input) (cinfo, src); | 344 | 67.3k | if (tempc == 'B') { | 345 | 5.24k | if (cinfo->X_density && cinfo->Y_density) { | 346 | 1.35k | this->xDensity = cinfo->X_density; | 347 | 1.35k | this->yDensity = cinfo->Y_density; | 348 | 1.35k | this->densityUnits = cinfo->density_unit; | 349 | 1.35k | } | 350 | 5.24k | } | 351 | 67.3k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 352 | | | 353 | 67.3k | *width = cinfo->image_width; *height = cinfo->image_height; | 354 | 67.3k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 355 | | | 356 | 67.3k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 357 | 67.3k | if ( | 358 | | #if ULLONG_MAX > SIZE_MAX | 359 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 360 | | (unsigned long long)((size_t)-1) || | 361 | | #endif | 362 | 67.3k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 363 | 67.3k | sizeof(_JSAMPLE))) == NULL) | 364 | 67.3k | THROW("Memory allocation failure"); | 365 | | | 366 | 67.3k | CATCH_LIBJPEG(this2); | 367 | | | 368 | 98.7M | while (cinfo->next_scanline < cinfo->image_height) { | 369 | 98.6M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 370 | | | 371 | 197M | for (i = 0; i < nlines; i++) { | 372 | 98.6M | _JSAMPLE *dstptr; | 373 | 98.6M | int row; | 374 | | | 375 | 98.6M | row = cinfo->next_scanline + i; | 376 | 98.6M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 377 | 79.3M | else dstptr = &dstBuf[row * pitch]; | 378 | 98.6M | memcpy(dstptr, src->_buffer[i], | 379 | 98.6M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 380 | 98.6M | } | 381 | 98.6M | cinfo->next_scanline += nlines; | 382 | 98.6M | } | 383 | | | 384 | 49.1k | (*src->finish_input) (cinfo, src); | 385 | | | 386 | 67.7k | bailout: | 387 | 67.7k | tj3Destroy(handle2); | 388 | 67.7k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 389 | 67.7k | return dstBuf; | 390 | 49.1k | } |
_tj3LoadImageFromFileHandle12 Line | Count | Source | 289 | 32.0k | { | 290 | 32.0k | static const char FUNCTION_NAME[] = | 291 | 32.0k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 292 | | | 293 | 32.0k | int retval = 0, tempc; | 294 | 32.0k | size_t pitch; | 295 | 32.0k | tjhandle handle2 = NULL; | 296 | 32.0k | tjinstance *this2; | 297 | 32.0k | j_compress_ptr cinfo = NULL; | 298 | 32.0k | cjpeg_source_ptr src; | 299 | 32.0k | _JSAMPLE *dstBuf = NULL; | 300 | 32.0k | boolean invert; | 301 | | | 302 | 32.0k | GET_TJINSTANCE(handle, NULL) | 303 | | | 304 | 32.0k | if (!file || !width || align < 1 || !height || !pixelFormat || | 305 | 32.0k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 306 | 32.0k | THROW("Invalid argument"); | 307 | 32.0k | if ((align & (align - 1)) != 0) | 308 | 32.0k | THROW("Alignment must be a power of 2"); | 309 | | | 310 | | /* The instance handle passed to this function is used only for parameter | 311 | | retrieval. Create a new temporary instance to avoid interfering with the | 312 | | libjpeg state of the primary instance. */ | 313 | 32.0k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 314 | 32.0k | this2 = (tjinstance *)handle2; | 315 | 32.0k | cinfo = &this2->cinfo; | 316 | | | 317 | 32.0k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 318 | 0 | THROW_UNIX("Could not read input file") | 319 | 32.0k | else if (tempc == EOF) | 320 | 32.0k | THROW("Input file contains no data"); | 321 | | | 322 | 32.0k | CATCH_LIBJPEG(this2); | 323 | | | 324 | 27.8k | cinfo->data_precision = BITS_IN_JSAMPLE; | 325 | 27.8k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 326 | 27.8k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 327 | 27.8k | if (tempc == 'B') { | 328 | 14 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 329 | 14 | THROW("Could not initialize bitmap loader"); | 330 | 14 | invert = !this->bottomUp; | 331 | 31.8k | } else if (tempc == 'P') { | 332 | 31.8k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 333 | 31.8k | THROW("Could not initialize PPM loader"); | 334 | 31.8k | invert = this->bottomUp; | 335 | 31.8k | } else | 336 | 18.4E | THROW("Unsupported file type"); | 337 | | | 338 | 31.8k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 339 | | | 340 | 31.8k | src->input_file = file; | 341 | | /* Refuse to load images larger than the specified size. */ | 342 | 31.8k | src->max_pixels = this->maxPixels; | 343 | 31.8k | (*src->start_input) (cinfo, src); | 344 | 31.8k | if (tempc == 'B') { | 345 | 0 | if (cinfo->X_density && cinfo->Y_density) { | 346 | 0 | this->xDensity = cinfo->X_density; | 347 | 0 | this->yDensity = cinfo->Y_density; | 348 | 0 | this->densityUnits = cinfo->density_unit; | 349 | 0 | } | 350 | 0 | } | 351 | 31.8k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 352 | | | 353 | 31.8k | *width = cinfo->image_width; *height = cinfo->image_height; | 354 | 31.8k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 355 | | | 356 | 31.8k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 357 | 31.8k | if ( | 358 | | #if ULLONG_MAX > SIZE_MAX | 359 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 360 | | (unsigned long long)((size_t)-1) || | 361 | | #endif | 362 | 31.8k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 363 | 31.8k | sizeof(_JSAMPLE))) == NULL) | 364 | 31.8k | THROW("Memory allocation failure"); | 365 | | | 366 | 31.8k | CATCH_LIBJPEG(this2); | 367 | | | 368 | 65.4M | while (cinfo->next_scanline < cinfo->image_height) { | 369 | 65.4M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 370 | | | 371 | 130M | for (i = 0; i < nlines; i++) { | 372 | 65.4M | _JSAMPLE *dstptr; | 373 | 65.4M | int row; | 374 | | | 375 | 65.4M | row = cinfo->next_scanline + i; | 376 | 65.4M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 377 | 56.0M | else dstptr = &dstBuf[row * pitch]; | 378 | 65.4M | memcpy(dstptr, src->_buffer[i], | 379 | 65.4M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 380 | 65.4M | } | 381 | 65.4M | cinfo->next_scanline += nlines; | 382 | 65.4M | } | 383 | | | 384 | 22.5k | (*src->finish_input) (cinfo, src); | 385 | | | 386 | 32.0k | bailout: | 387 | 32.0k | tj3Destroy(handle2); | 388 | 32.0k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 389 | 32.0k | return dstBuf; | 390 | 22.5k | } |
_tj3LoadImageFromFileHandle16 Line | Count | Source | 289 | 11.7k | { | 290 | 11.7k | static const char FUNCTION_NAME[] = | 291 | 11.7k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 292 | | | 293 | 11.7k | int retval = 0, tempc; | 294 | 11.7k | size_t pitch; | 295 | 11.7k | tjhandle handle2 = NULL; | 296 | 11.7k | tjinstance *this2; | 297 | 11.7k | j_compress_ptr cinfo = NULL; | 298 | 11.7k | cjpeg_source_ptr src; | 299 | 11.7k | _JSAMPLE *dstBuf = NULL; | 300 | 11.7k | boolean invert; | 301 | | | 302 | 11.7k | GET_TJINSTANCE(handle, NULL) | 303 | | | 304 | 11.7k | if (!file || !width || align < 1 || !height || !pixelFormat || | 305 | 11.7k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 306 | 11.7k | THROW("Invalid argument"); | 307 | 11.7k | if ((align & (align - 1)) != 0) | 308 | 11.7k | THROW("Alignment must be a power of 2"); | 309 | | | 310 | | /* The instance handle passed to this function is used only for parameter | 311 | | retrieval. Create a new temporary instance to avoid interfering with the | 312 | | libjpeg state of the primary instance. */ | 313 | 11.7k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 314 | 11.7k | this2 = (tjinstance *)handle2; | 315 | 11.7k | cinfo = &this2->cinfo; | 316 | | | 317 | 11.7k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 318 | 0 | THROW_UNIX("Could not read input file") | 319 | 11.7k | else if (tempc == EOF) | 320 | 11.7k | THROW("Input file contains no data"); | 321 | | | 322 | 11.7k | CATCH_LIBJPEG(this2); | 323 | | | 324 | 9.66k | cinfo->data_precision = BITS_IN_JSAMPLE; | 325 | 9.66k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 326 | 9.66k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 327 | 9.66k | if (tempc == 'B') { | 328 | 7 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 329 | 7 | THROW("Could not initialize bitmap loader"); | 330 | 7 | invert = !this->bottomUp; | 331 | 11.5k | } else if (tempc == 'P') { | 332 | 11.5k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 333 | 11.5k | THROW("Could not initialize PPM loader"); | 334 | 11.5k | invert = this->bottomUp; | 335 | 11.5k | } else | 336 | 18.4E | THROW("Unsupported file type"); | 337 | | | 338 | 11.5k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 339 | | | 340 | 11.5k | src->input_file = file; | 341 | | /* Refuse to load images larger than the specified size. */ | 342 | 11.5k | src->max_pixels = this->maxPixels; | 343 | 11.5k | (*src->start_input) (cinfo, src); | 344 | 11.5k | if (tempc == 'B') { | 345 | 0 | if (cinfo->X_density && cinfo->Y_density) { | 346 | 0 | this->xDensity = cinfo->X_density; | 347 | 0 | this->yDensity = cinfo->Y_density; | 348 | 0 | this->densityUnits = cinfo->density_unit; | 349 | 0 | } | 350 | 0 | } | 351 | 11.5k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 352 | | | 353 | 11.5k | *width = cinfo->image_width; *height = cinfo->image_height; | 354 | 11.5k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 355 | | | 356 | 11.5k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 357 | 11.5k | if ( | 358 | | #if ULLONG_MAX > SIZE_MAX | 359 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 360 | | (unsigned long long)((size_t)-1) || | 361 | | #endif | 362 | 11.5k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 363 | 11.5k | sizeof(_JSAMPLE))) == NULL) | 364 | 11.5k | THROW("Memory allocation failure"); | 365 | | | 366 | 11.5k | CATCH_LIBJPEG(this2); | 367 | | | 368 | 14.3M | while (cinfo->next_scanline < cinfo->image_height) { | 369 | 14.2M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 370 | | | 371 | 28.5M | for (i = 0; i < nlines; i++) { | 372 | 14.2M | _JSAMPLE *dstptr; | 373 | 14.2M | int row; | 374 | | | 375 | 14.2M | row = cinfo->next_scanline + i; | 376 | 14.2M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 377 | 12.2M | else dstptr = &dstBuf[row * pitch]; | 378 | 14.2M | memcpy(dstptr, src->_buffer[i], | 379 | 14.2M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 380 | 14.2M | } | 381 | 14.2M | cinfo->next_scanline += nlines; | 382 | 14.2M | } | 383 | | | 384 | 6.78k | (*src->finish_input) (cinfo, src); | 385 | | | 386 | 11.7k | bailout: | 387 | 11.7k | tj3Destroy(handle2); | 388 | 11.7k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 389 | 11.7k | return dstBuf; | 390 | 6.78k | } |
|
391 | | |
392 | | #endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
393 | | |
394 | | DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE) |
395 | | (tjhandle handle, const char *filename, int *width, int align, int *height, |
396 | | int *pixelFormat) |
397 | 0 | { |
398 | 0 | static const char FUNCTION_NAME[] = |
399 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
400 | |
|
401 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
402 | |
|
403 | 0 | int retval = 0; |
404 | 0 | _JSAMPLE *dstBuf = NULL; |
405 | 0 | FILE *file = NULL; |
406 | |
|
407 | 0 | GET_TJINSTANCE(handle, NULL) |
408 | |
|
409 | 0 | if (!filename) |
410 | 0 | THROW("Invalid argument"); |
411 | |
|
412 | | #ifdef _MSC_VER |
413 | | if (fopen_s(&file, filename, "rb") || file == NULL) |
414 | | #else |
415 | 0 | if ((file = fopen(filename, "rb")) == NULL) |
416 | 0 | #endif |
417 | 0 | THROW_UNIX("Cannot open input file"); |
418 | |
|
419 | 0 | dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
420 | 0 | (handle, file, width, align, height, pixelFormat); |
421 | |
|
422 | 0 | bailout: |
423 | 0 | if (file) fclose(file); |
424 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
425 | 0 | return dstBuf; |
426 | |
|
427 | | #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
428 | | |
429 | | static const char ERROR_MSG[] = |
430 | | "16-bit data precision requires lossless JPEG,\n" |
431 | | "which was disabled at build time."; |
432 | | _JSAMPLE *retval = NULL; |
433 | | |
434 | | GET_TJINSTANCE(handle, NULL) |
435 | | SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME, |
436 | | ERROR_MSG); |
437 | | this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL) |
438 | | |
439 | | bailout: |
440 | | return retval; |
441 | | |
442 | | #endif |
443 | 0 | } Unexecuted instantiation: tj3LoadImage8 Unexecuted instantiation: tj3LoadImage12 Unexecuted instantiation: tj3LoadImage16 |
444 | | |
445 | | |
446 | | /* TurboJPEG 3+ */ |
447 | | DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE) |
448 | | (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width, |
449 | | int pitch, int height, int pixelFormat) |
450 | 0 | { |
451 | 0 | static const char FUNCTION_NAME[] = |
452 | 0 | GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE); |
453 | 0 | int retval = 0; |
454 | |
|
455 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) |
456 | |
|
457 | 0 | tjhandle handle2 = NULL; |
458 | 0 | tjinstance *this2; |
459 | 0 | j_decompress_ptr dinfo = NULL; |
460 | 0 | djpeg_dest_ptr dst; |
461 | 0 | FILE *file = NULL; |
462 | 0 | const char *ptr = NULL; |
463 | 0 | boolean invert; |
464 | |
|
465 | 0 | GET_TJINSTANCE(handle, -1) |
466 | |
|
467 | 0 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
468 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
469 | 0 | THROW("Invalid argument"); |
470 | | |
471 | | /* The instance handle passed to this function is used only for parameter |
472 | | retrieval. Create a new temporary instance to avoid interfering with the |
473 | | libjpeg state of the primary instance. */ |
474 | 0 | if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
475 | 0 | return -1; |
476 | 0 | this2 = (tjinstance *)handle2; |
477 | 0 | dinfo = &this2->dinfo; |
478 | |
|
479 | | #ifdef _MSC_VER |
480 | | if (fopen_s(&file, filename, "wb") || file == NULL) |
481 | | #else |
482 | 0 | if ((file = fopen(filename, "wb")) == NULL) |
483 | 0 | #endif |
484 | 0 | THROW_UNIX("Cannot open output file"); |
485 | |
|
486 | 0 | CATCH_LIBJPEG(this2); |
487 | |
|
488 | 0 | this2->dinfo.out_color_space = pf2cs[pixelFormat]; |
489 | 0 | dinfo->image_width = width; dinfo->image_height = height; |
490 | 0 | dinfo->global_state = DSTATE_READY; |
491 | 0 | dinfo->scale_num = dinfo->scale_denom = 1; |
492 | 0 | dinfo->data_precision = BITS_IN_JSAMPLE; |
493 | |
|
494 | 0 | ptr = strrchr(filename, '.'); |
495 | 0 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
496 | 0 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
497 | 0 | THROW("Could not initialize bitmap writer"); |
498 | 0 | invert = !this->bottomUp; |
499 | 0 | dinfo->X_density = (UINT16)this->xDensity; |
500 | 0 | dinfo->Y_density = (UINT16)this->yDensity; |
501 | 0 | dinfo->density_unit = (UINT8)this->densityUnits; |
502 | 0 | } else { |
503 | 0 | if ((dst = _jinit_write_ppm(dinfo)) == NULL) |
504 | 0 | THROW("Could not initialize PPM writer"); |
505 | 0 | invert = this->bottomUp; |
506 | 0 | } |
507 | | |
508 | 0 | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
509 | |
|
510 | 0 | dst->output_file = file; |
511 | 0 | (*dst->start_output) (dinfo, dst); |
512 | 0 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
513 | |
|
514 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
515 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
516 | 0 | THROW("Invalid argument"); |
517 | |
|
518 | 0 | while (dinfo->output_scanline < dinfo->output_height) { |
519 | 0 | _JSAMPLE *rowptr; |
520 | |
|
521 | 0 | if (invert) |
522 | 0 | rowptr = |
523 | 0 | (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch]; |
524 | 0 | else |
525 | 0 | rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch]; |
526 | 0 | memcpy(dst->_buffer[0], rowptr, |
527 | 0 | width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE)); |
528 | 0 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
529 | 0 | dinfo->output_scanline++; |
530 | 0 | } |
531 | |
|
532 | 0 | (*dst->finish_output) (dinfo, dst); |
533 | |
|
534 | 0 | bailout: |
535 | 0 | tj3Destroy(handle2); |
536 | 0 | if (file) fclose(file); |
537 | 0 | return retval; |
538 | |
|
539 | | #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */ |
540 | | |
541 | | GET_TJINSTANCE(handle, -1) |
542 | | THROW("16-bit data precision requires lossless JPEG,\n" |
543 | | "which was disabled at build time.") |
544 | | bailout: |
545 | | return retval; |
546 | | |
547 | | #endif |
548 | 0 | } Unexecuted instantiation: tj3SaveImage8 Unexecuted instantiation: tj3SaveImage12 Unexecuted instantiation: tj3SaveImage16 |
549 | | |
550 | | |
551 | | #undef _JSAMPLE |
552 | | #undef _JSAMPROW |
553 | | #undef _buffer |
554 | | #undef _jinit_read_ppm |
555 | | #undef _jinit_write_ppm |
556 | | #undef _jpeg_crop_scanline |
557 | | #undef _jpeg_read_scanlines |
558 | | #undef _jpeg_skip_scanlines |
559 | | #undef _jpeg_write_scanlines |