/src/libjpeg-turbo.3.1.x/src/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 | 76.4M | #define _JSAMPLE JSAMPLE |
34 | 114k | #define _JSAMPROW JSAMPROW |
35 | 76.3M | #define _buffer buffer |
36 | 49.3k | #define _jinit_read_ppm jinit_read_ppm |
37 | 0 | #define _jinit_write_ppm jinit_write_ppm |
38 | 2.23k | #define _jpeg_crop_scanline jpeg_crop_scanline |
39 | 30.3M | #define _jpeg_read_scanlines jpeg_read_scanlines |
40 | 4.45k | #define _jpeg_skip_scanlines jpeg_skip_scanlines |
41 | 71.5k | #define _jpeg_write_scanlines jpeg_write_scanlines |
42 | | #elif BITS_IN_JSAMPLE == 12 |
43 | 49.6M | #define _JSAMPLE J12SAMPLE |
44 | 80.7k | #define _JSAMPROW J12SAMPROW |
45 | 49.5M | #define _buffer buffer12 |
46 | 33.9k | #define _jinit_read_ppm j12init_read_ppm |
47 | 0 | #define _jinit_write_ppm j12init_write_ppm |
48 | 1.24k | #define _jpeg_crop_scanline jpeg12_crop_scanline |
49 | 16.6M | #define _jpeg_read_scanlines jpeg12_read_scanlines |
50 | 2.46k | #define _jpeg_skip_scanlines jpeg12_skip_scanlines |
51 | 57.3k | #define _jpeg_write_scanlines jpeg12_write_scanlines |
52 | | #elif BITS_IN_JSAMPLE == 16 |
53 | 15.2M | #define _JSAMPLE J16SAMPLE |
54 | 32.2k | #define _JSAMPROW J16SAMPROW |
55 | 15.2M | #define _buffer buffer16 |
56 | 13.6k | #define _jinit_read_ppm j16init_read_ppm |
57 | 0 | #define _jinit_write_ppm j16init_write_ppm |
58 | 4.86M | #define _jpeg_read_scanlines jpeg16_read_scanlines |
59 | 21.0k | #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 | 347k | #define _GET_STRING(name, suffix) #name #suffix |
65 | 347k | #define GET_STRING(name, suffix) _GET_STRING(name, suffix) |
66 | | |
67 | | |
68 | | /******************************** Compressor *********************************/ |
69 | | |
70 | | /* TurboJPEG 3.0+ */ |
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 | 149k | { |
75 | 149k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); |
76 | 149k | int i, retval = 0; |
77 | 149k | boolean alloc = TRUE; |
78 | 149k | _JSAMPROW *row_pointer = NULL; |
79 | | |
80 | 149k | GET_CINSTANCE(handle) |
81 | 149k | if ((this->init & COMPRESS) == 0) |
82 | 149k | THROW("Instance has not been initialized for compression"); |
83 | | |
84 | 149k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
85 | 149k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || |
86 | 149k | jpegSize == NULL) |
87 | 149k | THROW("Invalid argument"); |
88 | | |
89 | 149k | if (!this->lossless && this->quality == -1) |
90 | 149k | THROW("TJPARAM_QUALITY must be specified"); |
91 | 149k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) |
92 | 149k | THROW("TJPARAM_SUBSAMP must be specified"); |
93 | | |
94 | 149k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
96 | 149k | THROW("Invalid argument"); |
97 | | |
98 | 149k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) |
99 | 149k | THROW("Memory allocation failure"); |
100 | | |
101 | 149k | CATCH_LIBJPEG(this); |
102 | | |
103 | 149k | cinfo->image_width = width; |
104 | 149k | cinfo->image_height = height; |
105 | 149k | cinfo->data_precision = BITS_IN_JSAMPLE; |
106 | | #if BITS_IN_JSAMPLE == 8 |
107 | 71.5k | if (this->lossless && this->precision >= 2 && |
108 | 28.7k | this->precision <= BITS_IN_JSAMPLE) |
109 | | #else |
110 | 78.3k | if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 && |
111 | 42.9k | this->precision <= BITS_IN_JSAMPLE) |
112 | 42.9k | #endif |
113 | 71.7k | cinfo->data_precision = this->precision; |
114 | | |
115 | 149k | setCompDefaults(this, pixelFormat, FALSE); |
116 | 149k | if (this->noRealloc) alloc = FALSE; |
117 | 149k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
118 | | |
119 | 149k | jpeg_start_compress(cinfo, TRUE); |
120 | 149k | if (this->iccBuf != NULL && this->iccSize != 0) |
121 | 127k | jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize); |
122 | 209M | for (i = 0; i < height; i++) { |
123 | 209M | if (this->bottomUp) |
124 | 30.8M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; |
125 | 178M | else |
126 | 178M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; |
127 | 209M | } |
128 | 299k | while (cinfo->next_scanline < cinfo->image_height) |
129 | 149k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
130 | 149k | cinfo->image_height - cinfo->next_scanline); |
131 | 149k | jpeg_finish_compress(cinfo); |
132 | | |
133 | 149k | bailout: |
134 | 149k | if (cinfo->global_state > CSTATE_START && alloc) |
135 | 136 | (*cinfo->dest->term_destination) (cinfo); |
136 | 149k | if (cinfo->global_state > CSTATE_START || retval == -1) |
137 | 136 | jpeg_abort_compress(cinfo); |
138 | 149k | free(row_pointer); |
139 | 149k | if (this->jerr.warning) retval = -1; |
140 | 149k | return retval; |
141 | 149k | } Line | Count | Source | 74 | 71.5k | { | 75 | 71.5k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 71.5k | int i, retval = 0; | 77 | 71.5k | boolean alloc = TRUE; | 78 | 71.5k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 71.5k | GET_CINSTANCE(handle) | 81 | 71.5k | if ((this->init & COMPRESS) == 0) | 82 | 71.5k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 71.5k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 71.5k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 71.5k | jpegSize == NULL) | 87 | 71.5k | THROW("Invalid argument"); | 88 | | | 89 | 71.5k | if (!this->lossless && this->quality == -1) | 90 | 71.5k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 71.5k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 71.5k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 71.5k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 71.5k | THROW("Invalid argument"); | 97 | | | 98 | 71.5k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 71.5k | THROW("Memory allocation failure"); | 100 | | | 101 | 71.5k | CATCH_LIBJPEG(this); | 102 | | | 103 | 71.5k | cinfo->image_width = width; | 104 | 71.5k | cinfo->image_height = height; | 105 | 71.5k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | 71.5k | #if BITS_IN_JSAMPLE == 8 | 107 | 71.5k | if (this->lossless && this->precision >= 2 && | 108 | 28.7k | this->precision <= BITS_IN_JSAMPLE) | 109 | | #else | 110 | | if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 && | 111 | | this->precision <= BITS_IN_JSAMPLE) | 112 | | #endif | 113 | 28.7k | cinfo->data_precision = this->precision; | 114 | | | 115 | 71.5k | setCompDefaults(this, pixelFormat, FALSE); | 116 | 71.5k | if (this->noRealloc) alloc = FALSE; | 117 | 71.5k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 118 | | | 119 | 71.5k | jpeg_start_compress(cinfo, TRUE); | 120 | 71.5k | if (this->iccBuf != NULL && this->iccSize != 0) | 121 | 61.9k | jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize); | 122 | 89.2M | for (i = 0; i < height; i++) { | 123 | 89.1M | if (this->bottomUp) | 124 | 13.4M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 125 | 75.6M | else | 126 | 75.6M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 127 | 89.1M | } | 128 | 143k | while (cinfo->next_scanline < cinfo->image_height) | 129 | 71.5k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 130 | 71.5k | cinfo->image_height - cinfo->next_scanline); | 131 | 71.5k | jpeg_finish_compress(cinfo); | 132 | | | 133 | 71.5k | bailout: | 134 | 71.5k | if (cinfo->global_state > CSTATE_START && alloc) | 135 | 0 | (*cinfo->dest->term_destination) (cinfo); | 136 | 71.5k | if (cinfo->global_state > CSTATE_START || retval == -1) | 137 | 0 | jpeg_abort_compress(cinfo); | 138 | 71.5k | free(row_pointer); | 139 | 71.5k | if (this->jerr.warning) retval = -1; | 140 | 71.5k | return retval; | 141 | 71.5k | } |
Line | Count | Source | 74 | 57.3k | { | 75 | 57.3k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 57.3k | int i, retval = 0; | 77 | 57.3k | boolean alloc = TRUE; | 78 | 57.3k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 57.3k | GET_CINSTANCE(handle) | 81 | 57.3k | if ((this->init & COMPRESS) == 0) | 82 | 57.3k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 57.3k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 57.3k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 57.3k | jpegSize == NULL) | 87 | 57.3k | THROW("Invalid argument"); | 88 | | | 89 | 57.3k | if (!this->lossless && this->quality == -1) | 90 | 57.3k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 57.3k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 57.3k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 57.3k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 57.3k | THROW("Invalid argument"); | 97 | | | 98 | 57.3k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 57.3k | THROW("Memory allocation failure"); | 100 | | | 101 | 57.3k | CATCH_LIBJPEG(this); | 102 | | | 103 | 57.2k | cinfo->image_width = width; | 104 | 57.2k | cinfo->image_height = height; | 105 | 57.2k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | | #if BITS_IN_JSAMPLE == 8 | 107 | | if (this->lossless && this->precision >= 2 && | 108 | | this->precision <= BITS_IN_JSAMPLE) | 109 | | #else | 110 | 57.2k | if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 && | 111 | 21.8k | this->precision <= BITS_IN_JSAMPLE) | 112 | 21.8k | #endif | 113 | 21.8k | cinfo->data_precision = this->precision; | 114 | | | 115 | 57.2k | setCompDefaults(this, pixelFormat, FALSE); | 116 | 57.2k | if (this->noRealloc) alloc = FALSE; | 117 | 57.2k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 118 | | | 119 | 57.2k | jpeg_start_compress(cinfo, TRUE); | 120 | 57.2k | if (this->iccBuf != NULL && this->iccSize != 0) | 121 | 47.7k | jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize); | 122 | 92.4M | for (i = 0; i < height; i++) { | 123 | 92.4M | if (this->bottomUp) | 124 | 13.3M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 125 | 79.0M | else | 126 | 79.0M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 127 | 92.4M | } | 128 | 114k | while (cinfo->next_scanline < cinfo->image_height) | 129 | 57.3k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 130 | 57.3k | cinfo->image_height - cinfo->next_scanline); | 131 | 57.2k | jpeg_finish_compress(cinfo); | 132 | | | 133 | 57.3k | bailout: | 134 | 57.3k | if (cinfo->global_state > CSTATE_START && alloc) | 135 | 50 | (*cinfo->dest->term_destination) (cinfo); | 136 | 57.3k | if (cinfo->global_state > CSTATE_START || retval == -1) | 137 | 50 | jpeg_abort_compress(cinfo); | 138 | 57.3k | free(row_pointer); | 139 | 57.3k | if (this->jerr.warning) retval = -1; | 140 | 57.3k | return retval; | 141 | 57.2k | } |
Line | Count | Source | 74 | 21.0k | { | 75 | 21.0k | static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE); | 76 | 21.0k | int i, retval = 0; | 77 | 21.0k | boolean alloc = TRUE; | 78 | 21.0k | _JSAMPROW *row_pointer = NULL; | 79 | | | 80 | 21.0k | GET_CINSTANCE(handle) | 81 | 21.0k | if ((this->init & COMPRESS) == 0) | 82 | 21.0k | THROW("Instance has not been initialized for compression"); | 83 | | | 84 | 21.0k | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || | 85 | 21.0k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || | 86 | 21.0k | jpegSize == NULL) | 87 | 21.0k | THROW("Invalid argument"); | 88 | | | 89 | 21.0k | if (!this->lossless && this->quality == -1) | 90 | 21.0k | THROW("TJPARAM_QUALITY must be specified"); | 91 | 21.0k | if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN) | 92 | 21.0k | THROW("TJPARAM_SUBSAMP must be specified"); | 93 | | | 94 | 21.0k | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; | 95 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) | 96 | 21.0k | THROW("Invalid argument"); | 97 | | | 98 | 21.0k | if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL) | 99 | 21.0k | THROW("Memory allocation failure"); | 100 | | | 101 | 21.0k | CATCH_LIBJPEG(this); | 102 | | | 103 | 21.0k | cinfo->image_width = width; | 104 | 21.0k | cinfo->image_height = height; | 105 | 21.0k | cinfo->data_precision = BITS_IN_JSAMPLE; | 106 | | #if BITS_IN_JSAMPLE == 8 | 107 | | if (this->lossless && this->precision >= 2 && | 108 | | this->precision <= BITS_IN_JSAMPLE) | 109 | | #else | 110 | 21.0k | if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 && | 111 | 21.0k | this->precision <= BITS_IN_JSAMPLE) | 112 | 21.0k | #endif | 113 | 21.0k | cinfo->data_precision = this->precision; | 114 | | | 115 | 21.0k | setCompDefaults(this, pixelFormat, FALSE); | 116 | 21.0k | if (this->noRealloc) alloc = FALSE; | 117 | 21.0k | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); | 118 | | | 119 | 21.0k | jpeg_start_compress(cinfo, TRUE); | 120 | 21.0k | if (this->iccBuf != NULL && this->iccSize != 0) | 121 | 18.0k | jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize); | 122 | 28.1M | for (i = 0; i < height; i++) { | 123 | 28.1M | if (this->bottomUp) | 124 | 4.04M | row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch]; | 125 | 24.0M | else | 126 | 24.0M | row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch]; | 127 | 28.1M | } | 128 | 42.1k | while (cinfo->next_scanline < cinfo->image_height) | 129 | 21.0k | _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], | 130 | 21.0k | cinfo->image_height - cinfo->next_scanline); | 131 | 21.0k | jpeg_finish_compress(cinfo); | 132 | | | 133 | 21.0k | bailout: | 134 | 21.0k | if (cinfo->global_state > CSTATE_START && alloc) | 135 | 86 | (*cinfo->dest->term_destination) (cinfo); | 136 | 21.0k | if (cinfo->global_state > CSTATE_START || retval == -1) | 137 | 86 | jpeg_abort_compress(cinfo); | 138 | 21.0k | free(row_pointer); | 139 | 21.0k | if (this->jerr.warning) retval = -1; | 140 | 21.0k | return retval; | 141 | 21.0k | } |
|
142 | | |
143 | | |
144 | | /******************************* Decompressor ********************************/ |
145 | | |
146 | | /* TurboJPEG 3.0+ */ |
147 | | DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE) |
148 | | (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize, |
149 | | _JSAMPLE *dstBuf, int pitch, int pixelFormat) |
150 | 77.7k | { |
151 | 77.7k | static const char FUNCTION_NAME[] = |
152 | 77.7k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); |
153 | 77.7k | _JSAMPROW *row_pointer = NULL; |
154 | 77.7k | int croppedHeight, i, retval = 0; |
155 | | #if BITS_IN_JSAMPLE != 16 |
156 | | int scaledWidth; |
157 | | #endif |
158 | 77.7k | struct my_progress_mgr progress; |
159 | | |
160 | 77.7k | GET_DINSTANCE(handle); |
161 | 77.7k | if ((this->init & DECOMPRESS) == 0) |
162 | 77.7k | THROW("Instance has not been initialized for decompression"); |
163 | | |
164 | 77.7k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || |
165 | 77.7k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
166 | 77.7k | THROW("Invalid argument"); |
167 | | |
168 | 77.7k | if (this->scanLimit) { |
169 | 77.7k | memset(&progress, 0, sizeof(struct my_progress_mgr)); |
170 | 77.7k | progress.pub.progress_monitor = my_progress_monitor; |
171 | 77.7k | progress.this = this; |
172 | 77.7k | dinfo->progress = &progress.pub; |
173 | 77.7k | } else |
174 | 0 | dinfo->progress = NULL; |
175 | | |
176 | 77.7k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
177 | | |
178 | 77.7k | CATCH_LIBJPEG(this); |
179 | | |
180 | 77.7k | if (dinfo->global_state <= DSTATE_INHEADER) { |
181 | 77.7k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
182 | 77.7k | jpeg_read_header(dinfo, TRUE); |
183 | 77.7k | } |
184 | 33.6k | setDecompParameters(this); |
185 | 33.6k | if (this->maxPixels && |
186 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > |
187 | 0 | (unsigned long long)this->maxPixels) |
188 | 33.6k | THROW("Image is too large"); |
189 | 33.6k | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
190 | | #if BITS_IN_JSAMPLE != 16 |
191 | 30.8k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); |
192 | | #endif |
193 | 33.6k | dinfo->do_fancy_upsampling = !this->fastUpsample; |
194 | 33.6k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; |
195 | | |
196 | 33.6k | dinfo->scale_num = this->scalingFactor.num; |
197 | 33.6k | dinfo->scale_denom = this->scalingFactor.denom; |
198 | | |
199 | 33.6k | jpeg_start_decompress(dinfo); |
200 | | |
201 | | #if BITS_IN_JSAMPLE != 16 |
202 | 30.8k | if (this->croppingRegion.x != 0 || |
203 | 27.3k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { |
204 | 3.48k | JDIMENSION crop_x = this->croppingRegion.x; |
205 | 3.48k | JDIMENSION crop_w = this->croppingRegion.w; |
206 | | |
207 | 3.48k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); |
208 | 3.48k | if ((int)crop_x != this->croppingRegion.x) |
209 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" |
210 | 3.48k | "actual (%d) cropping region left boundary", |
211 | 3.48k | this->croppingRegion.x, (int)crop_x); |
212 | 3.47k | if ((int)crop_w != this->croppingRegion.w) |
213 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
214 | 3.47k | "actual (%d) cropping region width", |
215 | 3.47k | this->croppingRegion.w, (int)crop_w); |
216 | 3.47k | } |
217 | 30.8k | #endif |
218 | | |
219 | 33.6k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
220 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) |
221 | 33.6k | THROW("Invalid argument"); |
222 | | |
223 | 33.6k | croppedHeight = dinfo->output_height; |
224 | | #if BITS_IN_JSAMPLE != 16 |
225 | 30.8k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) |
226 | 3.47k | croppedHeight = this->croppingRegion.h; |
227 | | #endif |
228 | 33.6k | if ((row_pointer = |
229 | 33.6k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) |
230 | 33.6k | THROW("Memory allocation failure"); |
231 | 33.6k | CATCH_LIBJPEG(this); |
232 | 128M | for (i = 0; i < (int)croppedHeight; i++) { |
233 | 128M | if (this->bottomUp) |
234 | 43.7M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; |
235 | 84.9M | else |
236 | 84.9M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; |
237 | 128M | } |
238 | | |
239 | | #if BITS_IN_JSAMPLE != 16 |
240 | 27.3k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { |
241 | 3.47k | if (this->croppingRegion.y != 0) { |
242 | 3.47k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); |
243 | | |
244 | 3.47k | if ((int)lines != this->croppingRegion.y) |
245 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
246 | | "actual (%d) cropping region upper boundary", |
247 | 3.47k | this->croppingRegion.y, (int)lines); |
248 | 3.47k | } |
249 | 127k | while ((int)dinfo->output_scanline < |
250 | 127k | this->croppingRegion.y + this->croppingRegion.h) |
251 | 123k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - |
252 | 123k | this->croppingRegion.y], |
253 | 123k | this->croppingRegion.y + this->croppingRegion.h - |
254 | 123k | dinfo->output_scanline); |
255 | 3.47k | if (this->croppingRegion.y + this->croppingRegion.h != |
256 | 3.47k | (int)dinfo->output_height) { |
257 | 3.44k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - |
258 | | this->croppingRegion.y - |
259 | | this->croppingRegion.h); |
260 | | |
261 | 3.44k | if (lines != dinfo->output_height - this->croppingRegion.y - |
262 | 3.44k | this->croppingRegion.h) |
263 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" |
264 | 3.44k | "actual (%d) cropping region lower boundary", |
265 | 3.44k | this->croppingRegion.y + this->croppingRegion.h, |
266 | 3.44k | (int)(dinfo->output_height - lines)); |
267 | 3.44k | } |
268 | 3.47k | } else |
269 | 22.8k | #endif |
270 | 22.8k | { |
271 | 51.7M | while (dinfo->output_scanline < dinfo->output_height) |
272 | 51.7M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
273 | 51.7M | dinfo->output_height - dinfo->output_scanline); |
274 | 22.8k | } |
275 | 26.3k | jpeg_finish_decompress(dinfo); |
276 | | |
277 | 77.7k | bailout: |
278 | 77.7k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
279 | 77.7k | free(row_pointer); |
280 | 77.7k | if (this->jerr.warning) retval = -1; |
281 | 77.7k | return retval; |
282 | 26.3k | } Line | Count | Source | 150 | 43.2k | { | 151 | 43.2k | static const char FUNCTION_NAME[] = | 152 | 43.2k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 153 | 43.2k | _JSAMPROW *row_pointer = NULL; | 154 | 43.2k | int croppedHeight, i, retval = 0; | 155 | 43.2k | #if BITS_IN_JSAMPLE != 16 | 156 | 43.2k | int scaledWidth; | 157 | 43.2k | #endif | 158 | 43.2k | struct my_progress_mgr progress; | 159 | | | 160 | 43.2k | GET_DINSTANCE(handle); | 161 | 43.2k | if ((this->init & DECOMPRESS) == 0) | 162 | 43.2k | THROW("Instance has not been initialized for decompression"); | 163 | | | 164 | 43.2k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 165 | 43.2k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 166 | 43.2k | THROW("Invalid argument"); | 167 | | | 168 | 43.2k | if (this->scanLimit) { | 169 | 43.2k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 170 | 43.2k | progress.pub.progress_monitor = my_progress_monitor; | 171 | 43.2k | progress.this = this; | 172 | 43.2k | dinfo->progress = &progress.pub; | 173 | 43.2k | } else | 174 | 0 | dinfo->progress = NULL; | 175 | | | 176 | 43.2k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 177 | | | 178 | 43.2k | CATCH_LIBJPEG(this); | 179 | | | 180 | 43.2k | if (dinfo->global_state <= DSTATE_INHEADER) { | 181 | 43.2k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 182 | 43.2k | jpeg_read_header(dinfo, TRUE); | 183 | 43.2k | } | 184 | 19.5k | setDecompParameters(this); | 185 | 19.5k | if (this->maxPixels && | 186 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 187 | 0 | (unsigned long long)this->maxPixels) | 188 | 19.5k | THROW("Image is too large"); | 189 | 19.5k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 190 | 19.5k | #if BITS_IN_JSAMPLE != 16 | 191 | 19.5k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 192 | 19.5k | #endif | 193 | 19.5k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 194 | 19.5k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 195 | | | 196 | 19.5k | dinfo->scale_num = this->scalingFactor.num; | 197 | 19.5k | dinfo->scale_denom = this->scalingFactor.denom; | 198 | | | 199 | 19.5k | jpeg_start_decompress(dinfo); | 200 | | | 201 | 19.5k | #if BITS_IN_JSAMPLE != 16 | 202 | 19.5k | if (this->croppingRegion.x != 0 || | 203 | 17.2k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 204 | 2.23k | JDIMENSION crop_x = this->croppingRegion.x; | 205 | 2.23k | JDIMENSION crop_w = this->croppingRegion.w; | 206 | | | 207 | 2.23k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 208 | 2.23k | if ((int)crop_x != this->croppingRegion.x) | 209 | 3 | THROWI("Unexplained mismatch between specified (%d) and\n" | 210 | 2.23k | "actual (%d) cropping region left boundary", | 211 | 2.23k | this->croppingRegion.x, (int)crop_x); | 212 | 2.23k | if ((int)crop_w != this->croppingRegion.w) | 213 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 214 | 2.23k | "actual (%d) cropping region width", | 215 | 2.23k | this->croppingRegion.w, (int)crop_w); | 216 | 2.23k | } | 217 | 19.5k | #endif | 218 | | | 219 | 19.5k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 220 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 221 | 19.5k | THROW("Invalid argument"); | 222 | | | 223 | 19.5k | croppedHeight = dinfo->output_height; | 224 | 19.5k | #if BITS_IN_JSAMPLE != 16 | 225 | 19.5k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 226 | 2.23k | croppedHeight = this->croppingRegion.h; | 227 | 19.5k | #endif | 228 | 19.5k | if ((row_pointer = | 229 | 19.5k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 230 | 19.5k | THROW("Memory allocation failure"); | 231 | 19.5k | CATCH_LIBJPEG(this); | 232 | 67.8M | for (i = 0; i < (int)croppedHeight; i++) { | 233 | 67.8M | if (this->bottomUp) | 234 | 23.7M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 235 | 44.0M | else | 236 | 44.0M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 237 | 67.8M | } | 238 | | | 239 | 16.6k | #if BITS_IN_JSAMPLE != 16 | 240 | 17.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 241 | 2.23k | if (this->croppingRegion.y != 0) { | 242 | 2.23k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 243 | | | 244 | 2.23k | if ((int)lines != this->croppingRegion.y) | 245 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 246 | 2.23k | "actual (%d) cropping region upper boundary", | 247 | 2.23k | this->croppingRegion.y, (int)lines); | 248 | 2.23k | } | 249 | 84.3k | while ((int)dinfo->output_scanline < | 250 | 84.3k | this->croppingRegion.y + this->croppingRegion.h) | 251 | 82.0k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 252 | 82.0k | this->croppingRegion.y], | 253 | 82.0k | this->croppingRegion.y + this->croppingRegion.h - | 254 | 82.0k | dinfo->output_scanline); | 255 | 2.23k | if (this->croppingRegion.y + this->croppingRegion.h != | 256 | 2.23k | (int)dinfo->output_height) { | 257 | 2.22k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 258 | 2.22k | this->croppingRegion.y - | 259 | 2.22k | this->croppingRegion.h); | 260 | | | 261 | 2.22k | if (lines != dinfo->output_height - this->croppingRegion.y - | 262 | 2.22k | this->croppingRegion.h) | 263 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 264 | 2.22k | "actual (%d) cropping region lower boundary", | 265 | 2.22k | this->croppingRegion.y + this->croppingRegion.h, | 266 | 2.22k | (int)(dinfo->output_height - lines)); | 267 | 2.22k | } | 268 | 2.23k | } else | 269 | 14.4k | #endif | 270 | 14.4k | { | 271 | 30.3M | while (dinfo->output_scanline < dinfo->output_height) | 272 | 30.2M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 273 | 30.2M | dinfo->output_height - dinfo->output_scanline); | 274 | 14.4k | } | 275 | 16.6k | jpeg_finish_decompress(dinfo); | 276 | | | 277 | 43.2k | bailout: | 278 | 43.2k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 279 | 43.2k | free(row_pointer); | 280 | 43.2k | if (this->jerr.warning) retval = -1; | 281 | 43.2k | return retval; | 282 | 16.6k | } |
Line | Count | Source | 150 | 23.3k | { | 151 | 23.3k | static const char FUNCTION_NAME[] = | 152 | 23.3k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 153 | 23.3k | _JSAMPROW *row_pointer = NULL; | 154 | 23.3k | int croppedHeight, i, retval = 0; | 155 | 23.3k | #if BITS_IN_JSAMPLE != 16 | 156 | 23.3k | int scaledWidth; | 157 | 23.3k | #endif | 158 | 23.3k | struct my_progress_mgr progress; | 159 | | | 160 | 23.3k | GET_DINSTANCE(handle); | 161 | 23.3k | if ((this->init & DECOMPRESS) == 0) | 162 | 23.3k | THROW("Instance has not been initialized for decompression"); | 163 | | | 164 | 23.3k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 165 | 23.3k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 166 | 23.3k | THROW("Invalid argument"); | 167 | | | 168 | 23.3k | if (this->scanLimit) { | 169 | 23.3k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 170 | 23.3k | progress.pub.progress_monitor = my_progress_monitor; | 171 | 23.3k | progress.this = this; | 172 | 23.3k | dinfo->progress = &progress.pub; | 173 | 23.3k | } else | 174 | 0 | dinfo->progress = NULL; | 175 | | | 176 | 23.3k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 177 | | | 178 | 23.3k | CATCH_LIBJPEG(this); | 179 | | | 180 | 23.3k | if (dinfo->global_state <= DSTATE_INHEADER) { | 181 | 23.3k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 182 | 23.3k | jpeg_read_header(dinfo, TRUE); | 183 | 23.3k | } | 184 | 11.2k | setDecompParameters(this); | 185 | 11.2k | if (this->maxPixels && | 186 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 187 | 0 | (unsigned long long)this->maxPixels) | 188 | 11.2k | THROW("Image is too large"); | 189 | 11.2k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 190 | 11.2k | #if BITS_IN_JSAMPLE != 16 | 191 | 11.2k | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 192 | 11.2k | #endif | 193 | 11.2k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 194 | 11.2k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 195 | | | 196 | 11.2k | dinfo->scale_num = this->scalingFactor.num; | 197 | 11.2k | dinfo->scale_denom = this->scalingFactor.denom; | 198 | | | 199 | 11.2k | jpeg_start_decompress(dinfo); | 200 | | | 201 | 11.2k | #if BITS_IN_JSAMPLE != 16 | 202 | 11.2k | if (this->croppingRegion.x != 0 || | 203 | 10.0k | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 204 | 1.24k | JDIMENSION crop_x = this->croppingRegion.x; | 205 | 1.24k | JDIMENSION crop_w = this->croppingRegion.w; | 206 | | | 207 | 1.24k | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 208 | 1.24k | if ((int)crop_x != this->croppingRegion.x) | 209 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 210 | 1.24k | "actual (%d) cropping region left boundary", | 211 | 1.24k | this->croppingRegion.x, (int)crop_x); | 212 | 1.24k | if ((int)crop_w != this->croppingRegion.w) | 213 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 214 | 1.24k | "actual (%d) cropping region width", | 215 | 1.24k | this->croppingRegion.w, (int)crop_w); | 216 | 1.24k | } | 217 | 11.2k | #endif | 218 | | | 219 | 11.2k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 220 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 221 | 11.2k | THROW("Invalid argument"); | 222 | | | 223 | 11.2k | croppedHeight = dinfo->output_height; | 224 | 11.2k | #if BITS_IN_JSAMPLE != 16 | 225 | 11.2k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 226 | 1.24k | croppedHeight = this->croppingRegion.h; | 227 | 11.2k | #endif | 228 | 11.2k | if ((row_pointer = | 229 | 11.2k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 230 | 11.2k | THROW("Memory allocation failure"); | 231 | 11.2k | CATCH_LIBJPEG(this); | 232 | 36.6M | for (i = 0; i < (int)croppedHeight; i++) { | 233 | 36.6M | if (this->bottomUp) | 234 | 13.2M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 235 | 23.4M | else | 236 | 23.4M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 237 | 36.6M | } | 238 | | | 239 | 9.69k | #if BITS_IN_JSAMPLE != 16 | 240 | 10.0k | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 241 | 1.24k | if (this->croppingRegion.y != 0) { | 242 | 1.24k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 243 | | | 244 | 1.24k | if ((int)lines != this->croppingRegion.y) | 245 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 246 | 1.24k | "actual (%d) cropping region upper boundary", | 247 | 1.24k | this->croppingRegion.y, (int)lines); | 248 | 1.24k | } | 249 | 42.7k | while ((int)dinfo->output_scanline < | 250 | 42.7k | this->croppingRegion.y + this->croppingRegion.h) | 251 | 41.5k | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 252 | 41.5k | this->croppingRegion.y], | 253 | 41.5k | this->croppingRegion.y + this->croppingRegion.h - | 254 | 41.5k | dinfo->output_scanline); | 255 | 1.24k | if (this->croppingRegion.y + this->croppingRegion.h != | 256 | 1.24k | (int)dinfo->output_height) { | 257 | 1.22k | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 258 | 1.22k | this->croppingRegion.y - | 259 | 1.22k | this->croppingRegion.h); | 260 | | | 261 | 1.22k | if (lines != dinfo->output_height - this->croppingRegion.y - | 262 | 1.22k | this->croppingRegion.h) | 263 | 0 | THROWI("Unexplained mismatch between specified (%d) and\n" | 264 | 1.22k | "actual (%d) cropping region lower boundary", | 265 | 1.22k | this->croppingRegion.y + this->croppingRegion.h, | 266 | 1.22k | (int)(dinfo->output_height - lines)); | 267 | 1.22k | } | 268 | 1.24k | } else | 269 | 8.45k | #endif | 270 | 8.45k | { | 271 | 16.6M | while (dinfo->output_scanline < dinfo->output_height) | 272 | 16.5M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 273 | 16.5M | dinfo->output_height - dinfo->output_scanline); | 274 | 8.45k | } | 275 | 9.69k | jpeg_finish_decompress(dinfo); | 276 | | | 277 | 23.3k | bailout: | 278 | 23.3k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 279 | 23.3k | free(row_pointer); | 280 | 23.3k | if (this->jerr.warning) retval = -1; | 281 | 23.3k | return retval; | 282 | 9.69k | } |
Line | Count | Source | 150 | 11.1k | { | 151 | 11.1k | static const char FUNCTION_NAME[] = | 152 | 11.1k | GET_STRING(tj3Decompress, BITS_IN_JSAMPLE); | 153 | 11.1k | _JSAMPROW *row_pointer = NULL; | 154 | 11.1k | int croppedHeight, i, retval = 0; | 155 | | #if BITS_IN_JSAMPLE != 16 | 156 | | int scaledWidth; | 157 | | #endif | 158 | 11.1k | struct my_progress_mgr progress; | 159 | | | 160 | 11.1k | GET_DINSTANCE(handle); | 161 | 11.1k | if ((this->init & DECOMPRESS) == 0) | 162 | 11.1k | THROW("Instance has not been initialized for decompression"); | 163 | | | 164 | 11.1k | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 || | 165 | 11.1k | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) | 166 | 11.1k | THROW("Invalid argument"); | 167 | | | 168 | 11.1k | if (this->scanLimit) { | 169 | 11.1k | memset(&progress, 0, sizeof(struct my_progress_mgr)); | 170 | 11.1k | progress.pub.progress_monitor = my_progress_monitor; | 171 | 11.1k | progress.this = this; | 172 | 11.1k | dinfo->progress = &progress.pub; | 173 | 11.1k | } else | 174 | 0 | dinfo->progress = NULL; | 175 | | | 176 | 11.1k | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 177 | | | 178 | 11.1k | CATCH_LIBJPEG(this); | 179 | | | 180 | 11.1k | if (dinfo->global_state <= DSTATE_INHEADER) { | 181 | 11.1k | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); | 182 | 11.1k | jpeg_read_header(dinfo, TRUE); | 183 | 11.1k | } | 184 | 2.88k | setDecompParameters(this); | 185 | 2.88k | if (this->maxPixels && | 186 | 0 | (unsigned long long)this->jpegWidth * this->jpegHeight > | 187 | 0 | (unsigned long long)this->maxPixels) | 188 | 2.88k | THROW("Image is too large"); | 189 | 2.88k | this->dinfo.out_color_space = pf2cs[pixelFormat]; | 190 | | #if BITS_IN_JSAMPLE != 16 | 191 | | scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor); | 192 | | #endif | 193 | 2.88k | dinfo->do_fancy_upsampling = !this->fastUpsample; | 194 | 2.88k | this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW; | 195 | | | 196 | 2.88k | dinfo->scale_num = this->scalingFactor.num; | 197 | 2.88k | dinfo->scale_denom = this->scalingFactor.denom; | 198 | | | 199 | 2.88k | jpeg_start_decompress(dinfo); | 200 | | | 201 | | #if BITS_IN_JSAMPLE != 16 | 202 | | if (this->croppingRegion.x != 0 || | 203 | | (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) { | 204 | | JDIMENSION crop_x = this->croppingRegion.x; | 205 | | JDIMENSION crop_w = this->croppingRegion.w; | 206 | | | 207 | | _jpeg_crop_scanline(dinfo, &crop_x, &crop_w); | 208 | | if ((int)crop_x != this->croppingRegion.x) | 209 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 210 | | "actual (%d) cropping region left boundary", | 211 | | this->croppingRegion.x, (int)crop_x); | 212 | | if ((int)crop_w != this->croppingRegion.w) | 213 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 214 | | "actual (%d) cropping region width", | 215 | | this->croppingRegion.w, (int)crop_w); | 216 | | } | 217 | | #endif | 218 | | | 219 | 2.88k | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; | 220 | 0 | else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat]) | 221 | 2.88k | THROW("Invalid argument"); | 222 | | | 223 | 2.88k | croppedHeight = dinfo->output_height; | 224 | | #if BITS_IN_JSAMPLE != 16 | 225 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) | 226 | | croppedHeight = this->croppingRegion.h; | 227 | | #endif | 228 | 2.88k | if ((row_pointer = | 229 | 2.88k | (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL) | 230 | 2.88k | THROW("Memory allocation failure"); | 231 | 2.88k | CATCH_LIBJPEG(this); | 232 | 24.2M | for (i = 0; i < (int)croppedHeight; i++) { | 233 | 24.2M | if (this->bottomUp) | 234 | 6.80M | row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch]; | 235 | 17.4M | else | 236 | 17.4M | row_pointer[i] = &dstBuf[i * (size_t)pitch]; | 237 | 24.2M | } | 238 | | | 239 | | #if BITS_IN_JSAMPLE != 16 | 240 | | if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) { | 241 | | if (this->croppingRegion.y != 0) { | 242 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y); | 243 | | | 244 | | if ((int)lines != this->croppingRegion.y) | 245 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 246 | | "actual (%d) cropping region upper boundary", | 247 | | this->croppingRegion.y, (int)lines); | 248 | | } | 249 | | while ((int)dinfo->output_scanline < | 250 | | this->croppingRegion.y + this->croppingRegion.h) | 251 | | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline - | 252 | | this->croppingRegion.y], | 253 | | this->croppingRegion.y + this->croppingRegion.h - | 254 | | dinfo->output_scanline); | 255 | | if (this->croppingRegion.y + this->croppingRegion.h != | 256 | | (int)dinfo->output_height) { | 257 | | JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height - | 258 | | this->croppingRegion.y - | 259 | | this->croppingRegion.h); | 260 | | | 261 | | if (lines != dinfo->output_height - this->croppingRegion.y - | 262 | | this->croppingRegion.h) | 263 | | THROWI("Unexplained mismatch between specified (%d) and\n" | 264 | | "actual (%d) cropping region lower boundary", | 265 | | this->croppingRegion.y + this->croppingRegion.h, | 266 | | (int)(dinfo->output_height - lines)); | 267 | | } | 268 | | } else | 269 | | #endif | 270 | 1.27k | { | 271 | 4.86M | while (dinfo->output_scanline < dinfo->output_height) | 272 | 4.86M | _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], | 273 | 4.86M | dinfo->output_height - dinfo->output_scanline); | 274 | 1.27k | } | 275 | 1.27k | jpeg_finish_decompress(dinfo); | 276 | | | 277 | 11.1k | bailout: | 278 | 11.1k | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); | 279 | 11.1k | free(row_pointer); | 280 | 11.1k | if (this->jerr.warning) retval = -1; | 281 | 11.1k | return retval; | 282 | 1.27k | } |
|
283 | | |
284 | | |
285 | | /*************************** Packed-Pixel Image I/O **************************/ |
286 | | |
287 | | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
288 | | |
289 | | /* TurboJPEG 3.0+ */ |
290 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
291 | | static |
292 | | #endif |
293 | | _JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
294 | | (tjhandle handle, FILE *file, int *width, int align, int *height, |
295 | | int *pixelFormat) |
296 | 119k | { |
297 | 119k | static const char FUNCTION_NAME[] = |
298 | 119k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
299 | | |
300 | 119k | int retval = 0, tempc; |
301 | 119k | size_t pitch; |
302 | 119k | tjhandle handle2 = NULL; |
303 | 119k | tjinstance *this2; |
304 | 119k | j_compress_ptr cinfo = NULL; |
305 | 119k | cjpeg_source_ptr src; |
306 | 119k | _JSAMPLE *dstBuf = NULL; |
307 | 119k | boolean invert; |
308 | | |
309 | 119k | GET_TJINSTANCE(handle, NULL) |
310 | | |
311 | 119k | if (!file || !width || align < 1 || !height || !pixelFormat || |
312 | 119k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
313 | 119k | THROW("Invalid argument"); |
314 | 119k | if ((align & (align - 1)) != 0) |
315 | 119k | THROW("Alignment must be a power of 2"); |
316 | | |
317 | | /* The instance handle passed to this function is used only for parameter |
318 | | retrieval. Create a new temporary instance to avoid interfering with the |
319 | | libjpeg state of the primary instance. */ |
320 | 119k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; |
321 | 119k | this2 = (tjinstance *)handle2; |
322 | 119k | cinfo = &this2->cinfo; |
323 | | |
324 | 119k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
325 | 0 | THROW_UNIX("Could not read input file") |
326 | 119k | else if (tempc == EOF) |
327 | 119k | THROW("Input file contains no data"); |
328 | | |
329 | 119k | CATCH_LIBJPEG(this2); |
330 | | |
331 | 92.7k | cinfo->data_precision = BITS_IN_JSAMPLE; |
332 | 92.7k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
333 | 92.7k | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
334 | 92.7k | if (tempc == 'B') { |
335 | 19.3k | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
336 | 19.3k | THROW("Could not initialize bitmap loader"); |
337 | 19.3k | invert = !this->bottomUp; |
338 | 96.8k | } else if (tempc == 'P') { |
339 | | #if BITS_IN_JSAMPLE == 8 |
340 | 49.3k | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
341 | | #else |
342 | 47.5k | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
343 | 27.3k | this->precision <= BITS_IN_JSAMPLE) |
344 | 27.3k | #endif |
345 | 76.6k | cinfo->data_precision = this->precision; |
346 | 96.8k | if ((src = _jinit_read_ppm(cinfo)) == NULL) |
347 | 96.8k | THROW("Could not initialize PPM loader"); |
348 | 96.8k | invert = this->bottomUp; |
349 | 96.8k | } else |
350 | 18.4E | THROW("Unsupported file type"); |
351 | | |
352 | 116k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
353 | | |
354 | 116k | src->input_file = file; |
355 | | /* Refuse to load images larger than the specified size. */ |
356 | 116k | src->max_pixels = this->maxPixels; |
357 | 116k | (*src->start_input) (cinfo, src); |
358 | 116k | if (tempc == 'B') { |
359 | 5.70k | if (cinfo->X_density && cinfo->Y_density) { |
360 | 2.10k | this->xDensity = cinfo->X_density; |
361 | 2.10k | this->yDensity = cinfo->Y_density; |
362 | 2.10k | this->densityUnits = cinfo->density_unit; |
363 | 2.10k | } |
364 | 5.70k | } |
365 | 116k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
366 | | |
367 | 116k | *width = cinfo->image_width; *height = cinfo->image_height; |
368 | 116k | *pixelFormat = cs2pf[cinfo->in_color_space]; |
369 | | |
370 | 116k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
371 | 116k | if ( |
372 | | #if ULLONG_MAX > SIZE_MAX |
373 | | (unsigned long long)pitch * (unsigned long long)(*height) > |
374 | | (unsigned long long)((size_t)-1) || |
375 | | #endif |
376 | 116k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * |
377 | 116k | sizeof(_JSAMPLE))) == NULL) |
378 | 116k | THROW("Memory allocation failure"); |
379 | | |
380 | 116k | CATCH_LIBJPEG(this2); |
381 | | |
382 | 141M | while (cinfo->next_scanline < cinfo->image_height) { |
383 | 141M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
384 | | |
385 | 282M | for (i = 0; i < nlines; i++) { |
386 | 141M | _JSAMPLE *dstptr; |
387 | 141M | int row; |
388 | | |
389 | 141M | row = cinfo->next_scanline + i; |
390 | 141M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
391 | 110M | else dstptr = &dstBuf[row * pitch]; |
392 | 141M | memcpy(dstptr, src->_buffer[i], |
393 | 141M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); |
394 | 141M | } |
395 | 141M | cinfo->next_scanline += nlines; |
396 | 141M | } |
397 | | |
398 | 76.9k | (*src->finish_input) (cinfo, src); |
399 | | |
400 | 119k | bailout: |
401 | 119k | tj3Destroy(handle2); |
402 | 119k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
403 | 119k | return dstBuf; |
404 | 76.9k | } _tj3LoadImageFromFileHandle8 Line | Count | Source | 296 | 70.0k | { | 297 | 70.0k | static const char FUNCTION_NAME[] = | 298 | 70.0k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 299 | | | 300 | 70.0k | int retval = 0, tempc; | 301 | 70.0k | size_t pitch; | 302 | 70.0k | tjhandle handle2 = NULL; | 303 | 70.0k | tjinstance *this2; | 304 | 70.0k | j_compress_ptr cinfo = NULL; | 305 | 70.0k | cjpeg_source_ptr src; | 306 | 70.0k | _JSAMPLE *dstBuf = NULL; | 307 | 70.0k | boolean invert; | 308 | | | 309 | 70.0k | GET_TJINSTANCE(handle, NULL) | 310 | | | 311 | 70.0k | if (!file || !width || align < 1 || !height || !pixelFormat || | 312 | 70.0k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 313 | 70.0k | THROW("Invalid argument"); | 314 | 70.0k | if ((align & (align - 1)) != 0) | 315 | 70.0k | THROW("Alignment must be a power of 2"); | 316 | | | 317 | | /* The instance handle passed to this function is used only for parameter | 318 | | retrieval. Create a new temporary instance to avoid interfering with the | 319 | | libjpeg state of the primary instance. */ | 320 | 70.0k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 321 | 70.0k | this2 = (tjinstance *)handle2; | 322 | 70.0k | cinfo = &this2->cinfo; | 323 | | | 324 | 70.0k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 325 | 0 | THROW_UNIX("Could not read input file") | 326 | 70.0k | else if (tempc == EOF) | 327 | 70.0k | THROW("Input file contains no data"); | 328 | | | 329 | 70.0k | CATCH_LIBJPEG(this2); | 330 | | | 331 | 50.2k | cinfo->data_precision = BITS_IN_JSAMPLE; | 332 | 50.2k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 333 | 50.2k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 334 | 50.2k | if (tempc == 'B') { | 335 | 19.2k | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 336 | 19.2k | THROW("Could not initialize bitmap loader"); | 337 | 19.2k | invert = !this->bottomUp; | 338 | 49.3k | } else if (tempc == 'P') { | 339 | 49.3k | #if BITS_IN_JSAMPLE == 8 | 340 | 49.3k | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) | 341 | | #else | 342 | | if (this->precision >= BITS_IN_JSAMPLE - 3 && | 343 | | this->precision <= BITS_IN_JSAMPLE) | 344 | | #endif | 345 | 49.3k | cinfo->data_precision = this->precision; | 346 | 49.3k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 347 | 49.3k | THROW("Could not initialize PPM loader"); | 348 | 49.3k | invert = this->bottomUp; | 349 | 49.3k | } else | 350 | 18.4E | THROW("Unsupported file type"); | 351 | | | 352 | 68.5k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 353 | | | 354 | 68.5k | src->input_file = file; | 355 | | /* Refuse to load images larger than the specified size. */ | 356 | 68.5k | src->max_pixels = this->maxPixels; | 357 | 68.5k | (*src->start_input) (cinfo, src); | 358 | 68.5k | if (tempc == 'B') { | 359 | 5.70k | if (cinfo->X_density && cinfo->Y_density) { | 360 | 2.10k | this->xDensity = cinfo->X_density; | 361 | 2.10k | this->yDensity = cinfo->Y_density; | 362 | 2.10k | this->densityUnits = cinfo->density_unit; | 363 | 2.10k | } | 364 | 5.70k | } | 365 | 68.5k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 366 | | | 367 | 68.5k | *width = cinfo->image_width; *height = cinfo->image_height; | 368 | 68.5k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 369 | | | 370 | 68.5k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 371 | 68.5k | if ( | 372 | | #if ULLONG_MAX > SIZE_MAX | 373 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 374 | | (unsigned long long)((size_t)-1) || | 375 | | #endif | 376 | 68.5k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 377 | 68.5k | sizeof(_JSAMPLE))) == NULL) | 378 | 68.5k | THROW("Memory allocation failure"); | 379 | | | 380 | 68.5k | CATCH_LIBJPEG(this2); | 381 | | | 382 | 76.4M | while (cinfo->next_scanline < cinfo->image_height) { | 383 | 76.4M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 384 | | | 385 | 152M | for (i = 0; i < nlines; i++) { | 386 | 76.3M | _JSAMPLE *dstptr; | 387 | 76.3M | int row; | 388 | | | 389 | 76.3M | row = cinfo->next_scanline + i; | 390 | 76.3M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 391 | 54.5M | else dstptr = &dstBuf[row * pitch]; | 392 | 76.3M | memcpy(dstptr, src->_buffer[i], | 393 | 76.3M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 394 | 76.3M | } | 395 | 76.4M | cinfo->next_scanline += nlines; | 396 | 76.4M | } | 397 | | | 398 | 48.1k | (*src->finish_input) (cinfo, src); | 399 | | | 400 | 70.0k | bailout: | 401 | 70.0k | tj3Destroy(handle2); | 402 | 70.0k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 403 | 70.0k | return dstBuf; | 404 | 48.1k | } |
_tj3LoadImageFromFileHandle12 Line | Count | Source | 296 | 35.5k | { | 297 | 35.5k | static const char FUNCTION_NAME[] = | 298 | 35.5k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 299 | | | 300 | 35.5k | int retval = 0, tempc; | 301 | 35.5k | size_t pitch; | 302 | 35.5k | tjhandle handle2 = NULL; | 303 | 35.5k | tjinstance *this2; | 304 | 35.5k | j_compress_ptr cinfo = NULL; | 305 | 35.5k | cjpeg_source_ptr src; | 306 | 35.5k | _JSAMPLE *dstBuf = NULL; | 307 | 35.5k | boolean invert; | 308 | | | 309 | 35.5k | GET_TJINSTANCE(handle, NULL) | 310 | | | 311 | 35.5k | if (!file || !width || align < 1 || !height || !pixelFormat || | 312 | 35.5k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 313 | 35.5k | THROW("Invalid argument"); | 314 | 35.5k | if ((align & (align - 1)) != 0) | 315 | 35.5k | THROW("Alignment must be a power of 2"); | 316 | | | 317 | | /* The instance handle passed to this function is used only for parameter | 318 | | retrieval. Create a new temporary instance to avoid interfering with the | 319 | | libjpeg state of the primary instance. */ | 320 | 35.5k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 321 | 35.5k | this2 = (tjinstance *)handle2; | 322 | 35.5k | cinfo = &this2->cinfo; | 323 | | | 324 | 35.5k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 325 | 0 | THROW_UNIX("Could not read input file") | 326 | 35.5k | else if (tempc == EOF) | 327 | 35.5k | THROW("Input file contains no data"); | 328 | | | 329 | 35.5k | CATCH_LIBJPEG(this2); | 330 | | | 331 | 30.6k | cinfo->data_precision = BITS_IN_JSAMPLE; | 332 | 30.6k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 333 | 30.6k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 334 | 30.6k | if (tempc == 'B') { | 335 | 84 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 336 | 84 | THROW("Could not initialize bitmap loader"); | 337 | 84 | invert = !this->bottomUp; | 338 | 33.9k | } else if (tempc == 'P') { | 339 | | #if BITS_IN_JSAMPLE == 8 | 340 | | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) | 341 | | #else | 342 | 33.9k | if (this->precision >= BITS_IN_JSAMPLE - 3 && | 343 | 13.6k | this->precision <= BITS_IN_JSAMPLE) | 344 | 13.6k | #endif | 345 | 13.6k | cinfo->data_precision = this->precision; | 346 | 33.9k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 347 | 33.9k | THROW("Could not initialize PPM loader"); | 348 | 33.9k | invert = this->bottomUp; | 349 | 33.9k | } else | 350 | 18.4E | THROW("Unsupported file type"); | 351 | | | 352 | 33.9k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 353 | | | 354 | 33.9k | src->input_file = file; | 355 | | /* Refuse to load images larger than the specified size. */ | 356 | 33.9k | src->max_pixels = this->maxPixels; | 357 | 33.9k | (*src->start_input) (cinfo, src); | 358 | 33.9k | if (tempc == 'B') { | 359 | 0 | if (cinfo->X_density && cinfo->Y_density) { | 360 | 0 | this->xDensity = cinfo->X_density; | 361 | 0 | this->yDensity = cinfo->Y_density; | 362 | 0 | this->densityUnits = cinfo->density_unit; | 363 | 0 | } | 364 | 0 | } | 365 | 33.9k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 366 | | | 367 | 33.9k | *width = cinfo->image_width; *height = cinfo->image_height; | 368 | 33.9k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 369 | | | 370 | 33.9k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 371 | 33.9k | if ( | 372 | | #if ULLONG_MAX > SIZE_MAX | 373 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 374 | | (unsigned long long)((size_t)-1) || | 375 | | #endif | 376 | 33.9k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 377 | 33.9k | sizeof(_JSAMPLE))) == NULL) | 378 | 33.9k | THROW("Memory allocation failure"); | 379 | | | 380 | 33.9k | CATCH_LIBJPEG(this2); | 381 | | | 382 | 49.6M | while (cinfo->next_scanline < cinfo->image_height) { | 383 | 49.5M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 384 | | | 385 | 99.1M | for (i = 0; i < nlines; i++) { | 386 | 49.5M | _JSAMPLE *dstptr; | 387 | 49.5M | int row; | 388 | | | 389 | 49.5M | row = cinfo->next_scanline + i; | 390 | 49.5M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 391 | 42.4M | else dstptr = &dstBuf[row * pitch]; | 392 | 49.5M | memcpy(dstptr, src->_buffer[i], | 393 | 49.5M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 394 | 49.5M | } | 395 | 49.5M | cinfo->next_scanline += nlines; | 396 | 49.5M | } | 397 | | | 398 | 21.4k | (*src->finish_input) (cinfo, src); | 399 | | | 400 | 35.5k | bailout: | 401 | 35.5k | tj3Destroy(handle2); | 402 | 35.5k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 403 | 35.5k | return dstBuf; | 404 | 21.4k | } |
_tj3LoadImageFromFileHandle16 Line | Count | Source | 296 | 14.2k | { | 297 | 14.2k | static const char FUNCTION_NAME[] = | 298 | 14.2k | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); | 299 | | | 300 | 14.2k | int retval = 0, tempc; | 301 | 14.2k | size_t pitch; | 302 | 14.2k | tjhandle handle2 = NULL; | 303 | 14.2k | tjinstance *this2; | 304 | 14.2k | j_compress_ptr cinfo = NULL; | 305 | 14.2k | cjpeg_source_ptr src; | 306 | 14.2k | _JSAMPLE *dstBuf = NULL; | 307 | 14.2k | boolean invert; | 308 | | | 309 | 14.2k | GET_TJINSTANCE(handle, NULL) | 310 | | | 311 | 14.2k | if (!file || !width || align < 1 || !height || !pixelFormat || | 312 | 14.2k | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) | 313 | 14.2k | THROW("Invalid argument"); | 314 | 14.2k | if ((align & (align - 1)) != 0) | 315 | 14.2k | THROW("Alignment must be a power of 2"); | 316 | | | 317 | | /* The instance handle passed to this function is used only for parameter | 318 | | retrieval. Create a new temporary instance to avoid interfering with the | 319 | | libjpeg state of the primary instance. */ | 320 | 14.2k | if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL; | 321 | 14.2k | this2 = (tjinstance *)handle2; | 322 | 14.2k | cinfo = &this2->cinfo; | 323 | | | 324 | 14.2k | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) | 325 | 0 | THROW_UNIX("Could not read input file") | 326 | 14.2k | else if (tempc == EOF) | 327 | 14.2k | THROW("Input file contains no data"); | 328 | | | 329 | 14.2k | CATCH_LIBJPEG(this2); | 330 | | | 331 | 11.8k | cinfo->data_precision = BITS_IN_JSAMPLE; | 332 | 11.8k | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; | 333 | 11.8k | else cinfo->in_color_space = pf2cs[*pixelFormat]; | 334 | 11.8k | if (tempc == 'B') { | 335 | 35 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) | 336 | 35 | THROW("Could not initialize bitmap loader"); | 337 | 35 | invert = !this->bottomUp; | 338 | 13.6k | } else if (tempc == 'P') { | 339 | | #if BITS_IN_JSAMPLE == 8 | 340 | | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) | 341 | | #else | 342 | 13.6k | if (this->precision >= BITS_IN_JSAMPLE - 3 && | 343 | 13.6k | this->precision <= BITS_IN_JSAMPLE) | 344 | 13.6k | #endif | 345 | 13.6k | cinfo->data_precision = this->precision; | 346 | 13.6k | if ((src = _jinit_read_ppm(cinfo)) == NULL) | 347 | 13.6k | THROW("Could not initialize PPM loader"); | 348 | 13.6k | invert = this->bottomUp; | 349 | 13.6k | } else | 350 | 18.4E | THROW("Unsupported file type"); | 351 | | | 352 | 13.6k | cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; | 353 | | | 354 | 13.6k | src->input_file = file; | 355 | | /* Refuse to load images larger than the specified size. */ | 356 | 13.6k | src->max_pixels = this->maxPixels; | 357 | 13.6k | (*src->start_input) (cinfo, src); | 358 | 13.6k | if (tempc == 'B') { | 359 | 0 | if (cinfo->X_density && cinfo->Y_density) { | 360 | 0 | this->xDensity = cinfo->X_density; | 361 | 0 | this->yDensity = cinfo->Y_density; | 362 | 0 | this->densityUnits = cinfo->density_unit; | 363 | 0 | } | 364 | 0 | } | 365 | 13.6k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); | 366 | | | 367 | 13.6k | *width = cinfo->image_width; *height = cinfo->image_height; | 368 | 13.6k | *pixelFormat = cs2pf[cinfo->in_color_space]; | 369 | | | 370 | 13.6k | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); | 371 | 13.6k | if ( | 372 | | #if ULLONG_MAX > SIZE_MAX | 373 | | (unsigned long long)pitch * (unsigned long long)(*height) > | 374 | | (unsigned long long)((size_t)-1) || | 375 | | #endif | 376 | 13.6k | (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) * | 377 | 13.6k | sizeof(_JSAMPLE))) == NULL) | 378 | 13.6k | THROW("Memory allocation failure"); | 379 | | | 380 | 13.6k | CATCH_LIBJPEG(this2); | 381 | | | 382 | 15.2M | while (cinfo->next_scanline < cinfo->image_height) { | 383 | 15.2M | int i, nlines = (*src->get_pixel_rows) (cinfo, src); | 384 | | | 385 | 30.5M | for (i = 0; i < nlines; i++) { | 386 | 15.2M | _JSAMPLE *dstptr; | 387 | 15.2M | int row; | 388 | | | 389 | 15.2M | row = cinfo->next_scanline + i; | 390 | 15.2M | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; | 391 | 13.0M | else dstptr = &dstBuf[row * pitch]; | 392 | 15.2M | memcpy(dstptr, src->_buffer[i], | 393 | 15.2M | (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE)); | 394 | 15.2M | } | 395 | 15.2M | cinfo->next_scanline += nlines; | 396 | 15.2M | } | 397 | | | 398 | 7.36k | (*src->finish_input) (cinfo, src); | 399 | | | 400 | 14.2k | bailout: | 401 | 14.2k | tj3Destroy(handle2); | 402 | 14.2k | if (retval < 0) { free(dstBuf); dstBuf = NULL; } | 403 | 14.2k | return dstBuf; | 404 | 7.36k | } |
|
405 | | |
406 | | #endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
407 | | |
408 | | DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE) |
409 | | (tjhandle handle, const char *filename, int *width, int align, int *height, |
410 | | int *pixelFormat) |
411 | 0 | { |
412 | 0 | static const char FUNCTION_NAME[] = |
413 | 0 | GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE); |
414 | |
|
415 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) |
416 | |
|
417 | 0 | int retval = 0; |
418 | 0 | _JSAMPLE *dstBuf = NULL; |
419 | 0 | FILE *file = NULL; |
420 | |
|
421 | 0 | GET_TJINSTANCE(handle, NULL) |
422 | |
|
423 | 0 | if (!filename) |
424 | 0 | THROW("Invalid argument"); |
425 | |
|
426 | | #ifdef _MSC_VER |
427 | | if (fopen_s(&file, filename, "rb") || file == NULL) |
428 | | #else |
429 | 0 | if ((file = fopen(filename, "rb")) == NULL) |
430 | 0 | #endif |
431 | 0 | THROW_UNIX("Cannot open input file"); |
432 | |
|
433 | 0 | dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE) |
434 | 0 | (handle, file, width, align, height, pixelFormat); |
435 | |
|
436 | 0 | bailout: |
437 | 0 | if (file) fclose(file); |
438 | 0 | if (retval < 0) { free(dstBuf); dstBuf = NULL; } |
439 | 0 | return dstBuf; |
440 | |
|
441 | | #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */ |
442 | | |
443 | | static const char ERROR_MSG[] = |
444 | | "16-bit data precision requires lossless JPEG,\n" |
445 | | "which was disabled at build time."; |
446 | | _JSAMPLE *retval = NULL; |
447 | | |
448 | | GET_TJINSTANCE(handle, NULL) |
449 | | SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME, |
450 | | ERROR_MSG); |
451 | | this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL) |
452 | | |
453 | | bailout: |
454 | | return retval; |
455 | | |
456 | | #endif |
457 | 0 | } Unexecuted instantiation: tj3LoadImage8 Unexecuted instantiation: tj3LoadImage12 Unexecuted instantiation: tj3LoadImage16 |
458 | | |
459 | | |
460 | | /* TurboJPEG 3.0+ */ |
461 | | DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE) |
462 | | (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width, |
463 | | int pitch, int height, int pixelFormat) |
464 | 0 | { |
465 | 0 | static const char FUNCTION_NAME[] = |
466 | 0 | GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE); |
467 | 0 | int retval = 0; |
468 | |
|
469 | 0 | #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) |
470 | |
|
471 | 0 | tjhandle handle2 = NULL; |
472 | 0 | tjinstance *this2; |
473 | 0 | j_decompress_ptr dinfo = NULL; |
474 | 0 | djpeg_dest_ptr dst; |
475 | 0 | FILE *file = NULL; |
476 | 0 | const char *ptr = NULL; |
477 | 0 | boolean invert; |
478 | |
|
479 | 0 | GET_TJINSTANCE(handle, -1) |
480 | |
|
481 | 0 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
482 | 0 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
483 | 0 | THROW("Invalid argument"); |
484 | | |
485 | | /* The instance handle passed to this function is used only for parameter |
486 | | retrieval. Create a new temporary instance to avoid interfering with the |
487 | | libjpeg state of the primary instance. */ |
488 | 0 | if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
489 | 0 | return -1; |
490 | 0 | this2 = (tjinstance *)handle2; |
491 | 0 | dinfo = &this2->dinfo; |
492 | |
|
493 | | #ifdef _MSC_VER |
494 | | if (fopen_s(&file, filename, "wb") || file == NULL) |
495 | | #else |
496 | 0 | if ((file = fopen(filename, "wb")) == NULL) |
497 | 0 | #endif |
498 | 0 | THROW_UNIX("Cannot open output file"); |
499 | |
|
500 | 0 | CATCH_LIBJPEG(this2); |
501 | |
|
502 | 0 | this2->dinfo.out_color_space = pf2cs[pixelFormat]; |
503 | 0 | dinfo->image_width = width; dinfo->image_height = height; |
504 | 0 | dinfo->global_state = DSTATE_READY; |
505 | 0 | dinfo->scale_num = dinfo->scale_denom = 1; |
506 | 0 | dinfo->data_precision = BITS_IN_JSAMPLE; |
507 | |
|
508 | 0 | ptr = strrchr(filename, '.'); |
509 | 0 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
510 | 0 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
511 | 0 | THROW("Could not initialize bitmap writer"); |
512 | 0 | invert = !this->bottomUp; |
513 | 0 | dinfo->X_density = (UINT16)this->xDensity; |
514 | 0 | dinfo->Y_density = (UINT16)this->yDensity; |
515 | 0 | dinfo->density_unit = (UINT8)this->densityUnits; |
516 | 0 | } else { |
517 | | #if BITS_IN_JSAMPLE == 8 |
518 | 0 | if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE) |
519 | | #else |
520 | 0 | if (this->precision >= BITS_IN_JSAMPLE - 3 && |
521 | 0 | this->precision <= BITS_IN_JSAMPLE) |
522 | 0 | #endif |
523 | 0 | dinfo->data_precision = this->precision; |
524 | 0 | if ((dst = _jinit_write_ppm(dinfo)) == NULL) |
525 | 0 | THROW("Could not initialize PPM writer"); |
526 | 0 | invert = this->bottomUp; |
527 | 0 | } |
528 | | |
529 | 0 | dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L; |
530 | |
|
531 | 0 | dst->output_file = file; |
532 | 0 | (*dst->start_output) (dinfo, dst); |
533 | 0 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
534 | |
|
535 | 0 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
536 | 0 | else if (pitch < width * tjPixelSize[pixelFormat]) |
537 | 0 | THROW("Invalid argument"); |
538 | |
|
539 | 0 | while (dinfo->output_scanline < dinfo->output_height) { |
540 | 0 | _JSAMPLE *rowptr; |
541 | |
|
542 | 0 | if (invert) |
543 | 0 | rowptr = |
544 | 0 | (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch]; |
545 | 0 | else |
546 | 0 | rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch]; |
547 | 0 | memcpy(dst->_buffer[0], rowptr, |
548 | 0 | width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE)); |
549 | 0 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
550 | 0 | dinfo->output_scanline++; |
551 | 0 | } |
552 | |
|
553 | 0 | (*dst->finish_output) (dinfo, dst); |
554 | |
|
555 | 0 | bailout: |
556 | 0 | tj3Destroy(handle2); |
557 | 0 | if (file) fclose(file); |
558 | 0 | return retval; |
559 | |
|
560 | | #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */ |
561 | | |
562 | | GET_TJINSTANCE(handle, -1) |
563 | | THROW("16-bit data precision requires lossless JPEG,\n" |
564 | | "which was disabled at build time.") |
565 | | bailout: |
566 | | return retval; |
567 | | |
568 | | #endif |
569 | 0 | } Unexecuted instantiation: tj3SaveImage8 Unexecuted instantiation: tj3SaveImage12 Unexecuted instantiation: tj3SaveImage16 |
570 | | |
571 | | |
572 | | #undef _JSAMPLE |
573 | | #undef _JSAMPROW |
574 | | #undef _buffer |
575 | | #undef _jinit_read_ppm |
576 | | #undef _jinit_write_ppm |
577 | | #undef _jpeg_crop_scanline |
578 | | #undef _jpeg_read_scanlines |
579 | | #undef _jpeg_skip_scanlines |
580 | | #undef _jpeg_write_scanlines |