DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1 | /* |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2 | * Copyright (C)2009-2018 D. R. Commander. All Rights Reserved. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 3 | * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 6 | * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 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. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
DRC | bdfcb74 | 2013-01-22 13:56:34 +0000 | [diff] [blame] | 29 | /* TurboJPEG/LJT: this implements the TurboJPEG API using libjpeg or |
| 30 | libjpeg-turbo */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
DRC | 0713c1b | 2014-08-22 13:43:33 +0000 | [diff] [blame] | 34 | #include <ctype.h> |
DRC | 296c71b | 2011-05-25 04:12:52 +0000 | [diff] [blame] | 35 | #include <jinclude.h> |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 36 | #define JPEG_INTERNALS |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 37 | #include <jpeglib.h> |
| 38 | #include <jerror.h> |
| 39 | #include <setjmp.h> |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 40 | #include <errno.h> |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 41 | #include "./turbojpeg.h" |
DRC | 29e453f | 2018-03-16 14:09:53 -0500 | [diff] [blame^] | 42 | #include "./tjutil.h" |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 43 | #include "transupp.h" |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 44 | #include "./jpegcomp.h" |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 45 | #include "./cdjpeg.h" |
DRC | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 46 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 47 | extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **, unsigned long *, |
| 48 | boolean); |
DRC | 6fa14b3 | 2015-08-13 20:06:03 -0500 | [diff] [blame] | 49 | extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *, |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 50 | unsigned long); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 51 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 52 | #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1))) |
| 53 | #define isPow2(x) (((x) & (x - 1)) == 0) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 54 | |
| 55 | |
DRC | 8c40ac8 | 2017-11-16 18:46:01 -0600 | [diff] [blame] | 56 | /* Error handling (based on example in example.txt) */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 57 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 58 | static char errStr[JMSG_LENGTH_MAX] = "No error"; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 59 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 60 | struct my_error_mgr { |
| 61 | struct jpeg_error_mgr pub; |
| 62 | jmp_buf setjmp_buffer; |
| 63 | void (*emit_message) (j_common_ptr, int); |
| 64 | boolean warning, stopOnWarning; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 65 | }; |
| 66 | typedef struct my_error_mgr *my_error_ptr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 67 | |
| 68 | static void my_error_exit(j_common_ptr cinfo) |
| 69 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 70 | my_error_ptr myerr = (my_error_ptr)cinfo->err; |
| 71 | |
| 72 | (*cinfo->err->output_message) (cinfo); |
| 73 | longjmp(myerr->setjmp_buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 76 | /* Based on output_message() in jerror.c */ |
| 77 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 78 | static void my_output_message(j_common_ptr cinfo) |
| 79 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 80 | (*cinfo->err->format_message) (cinfo, errStr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
DRC | 1f79c7c | 2015-06-01 19:22:41 +0000 | [diff] [blame] | 83 | static void my_emit_message(j_common_ptr cinfo, int msg_level) |
| 84 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 85 | my_error_ptr myerr = (my_error_ptr)cinfo->err; |
| 86 | |
| 87 | myerr->emit_message(cinfo, msg_level); |
| 88 | if (msg_level < 0) { |
| 89 | myerr->warning = TRUE; |
| 90 | if (myerr->stopOnWarning) longjmp(myerr->setjmp_buffer, 1); |
| 91 | } |
DRC | 1f79c7c | 2015-06-01 19:22:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 94 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 95 | /* Global structures, macros, etc. */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 96 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 97 | enum { COMPRESS = 1, DECOMPRESS = 2 }; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 98 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 99 | typedef struct _tjinstance { |
| 100 | struct jpeg_compress_struct cinfo; |
| 101 | struct jpeg_decompress_struct dinfo; |
| 102 | struct my_error_mgr jerr; |
| 103 | int init, headerRead; |
| 104 | char errStr[JMSG_LENGTH_MAX]; |
| 105 | boolean isInstanceError; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 106 | } tjinstance; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 107 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 108 | static const int pixelsize[TJ_NUMSAMP] = { 3, 3, 3, 1, 3, 3 }; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 109 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 110 | static const JXFORM_CODE xformtypes[TJ_NUMXOP] = { |
| 111 | JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE, |
| 112 | JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270 |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 113 | }; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 114 | |
DRC | ab2df6e | 2012-01-28 06:49:56 +0000 | [diff] [blame] | 115 | #define NUMSF 16 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 116 | static const tjscalingfactor sf[NUMSF] = { |
| 117 | { 2, 1 }, |
| 118 | { 15, 8 }, |
| 119 | { 7, 4 }, |
| 120 | { 13, 8 }, |
| 121 | { 3, 2 }, |
| 122 | { 11, 8 }, |
| 123 | { 5, 4 }, |
| 124 | { 9, 8 }, |
| 125 | { 1, 1 }, |
| 126 | { 7, 8 }, |
| 127 | { 3, 4 }, |
| 128 | { 5, 8 }, |
| 129 | { 1, 2 }, |
| 130 | { 3, 8 }, |
| 131 | { 1, 4 }, |
| 132 | { 1, 8 } |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 133 | }; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 134 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 135 | static J_COLOR_SPACE pf2cs[TJ_NUMPF] = { |
| 136 | JCS_EXT_RGB, JCS_EXT_BGR, JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR, |
| 137 | JCS_EXT_XRGB, JCS_GRAYSCALE, JCS_EXT_RGBA, JCS_EXT_BGRA, JCS_EXT_ABGR, |
| 138 | JCS_EXT_ARGB, JCS_CMYK |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 139 | }; |
| 140 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 141 | static int cs2pf[JPEG_NUMCS] = { |
| 142 | TJPF_UNKNOWN, TJPF_GRAY, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 143 | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 144 | TJPF_RGB, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 145 | #elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 3 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 146 | TJPF_BGR, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 147 | #elif RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 4 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 148 | TJPF_RGBX, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 149 | #elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 4 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 150 | TJPF_BGRX, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 151 | #elif RGB_RED == 3 && RGB_GREEN == 2 && RGB_BLUE == 1 && RGB_PIXELSIZE == 4 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 152 | TJPF_XBGR, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 153 | #elif RGB_RED == 1 && RGB_GREEN == 2 && RGB_BLUE == 3 && RGB_PIXELSIZE == 4 |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 154 | TJPF_XRGB, |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 155 | #endif |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 156 | TJPF_UNKNOWN, TJPF_CMYK, TJPF_UNKNOWN, TJPF_RGB, TJPF_RGBX, TJPF_BGR, |
| 157 | TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_RGBA, TJPF_BGRA, TJPF_ABGR, TJPF_ARGB, |
| 158 | TJPF_UNKNOWN |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 159 | }; |
| 160 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 161 | #define _throwg(m) { \ |
| 162 | snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \ |
| 163 | retval = -1; goto bailout; \ |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 164 | } |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 165 | #define _throwunix(m) { \ |
| 166 | snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerror(errno)); \ |
| 167 | retval = -1; goto bailout; \ |
| 168 | } |
| 169 | #define _throw(m) { \ |
| 170 | snprintf(this->errStr, JMSG_LENGTH_MAX, "%s", m); \ |
| 171 | this->isInstanceError = TRUE; _throwg(m); \ |
| 172 | } |
| 173 | |
| 174 | #define getinstance(handle) \ |
| 175 | tjinstance *this = (tjinstance *)handle; \ |
| 176 | j_compress_ptr cinfo = NULL; \ |
| 177 | j_decompress_ptr dinfo = NULL; \ |
| 178 | \ |
| 179 | if (!this) { \ |
| 180 | snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \ |
| 181 | return -1; \ |
| 182 | } \ |
| 183 | cinfo = &this->cinfo; dinfo = &this->dinfo; \ |
| 184 | this->jerr.warning = FALSE; \ |
| 185 | this->isInstanceError = FALSE; |
| 186 | |
| 187 | #define getcinstance(handle) \ |
| 188 | tjinstance *this = (tjinstance *)handle; \ |
| 189 | j_compress_ptr cinfo = NULL; \ |
| 190 | \ |
| 191 | if (!this) { \ |
| 192 | snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \ |
| 193 | return -1; \ |
| 194 | } \ |
| 195 | cinfo = &this->cinfo; \ |
| 196 | this->jerr.warning = FALSE; \ |
| 197 | this->isInstanceError = FALSE; |
| 198 | |
| 199 | #define getdinstance(handle) \ |
| 200 | tjinstance *this = (tjinstance *)handle; \ |
| 201 | j_decompress_ptr dinfo = NULL; \ |
| 202 | \ |
| 203 | if (!this) { \ |
| 204 | snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \ |
| 205 | return -1; \ |
| 206 | } \ |
| 207 | dinfo = &this->dinfo; \ |
| 208 | this->jerr.warning = FALSE; \ |
| 209 | this->isInstanceError = FALSE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 210 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 211 | static int getPixelFormat(int pixelSize, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 212 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 213 | if (pixelSize == 1) return TJPF_GRAY; |
| 214 | if (pixelSize == 3) { |
| 215 | if (flags & TJ_BGR) return TJPF_BGR; |
| 216 | else return TJPF_RGB; |
| 217 | } |
| 218 | if (pixelSize == 4) { |
| 219 | if (flags & TJ_ALPHAFIRST) { |
| 220 | if (flags & TJ_BGR) return TJPF_XBGR; |
| 221 | else return TJPF_XRGB; |
| 222 | } else { |
| 223 | if (flags & TJ_BGR) return TJPF_BGRX; |
| 224 | else return TJPF_RGBX; |
| 225 | } |
| 226 | } |
| 227 | return -1; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 230 | static int setCompDefaults(struct jpeg_compress_struct *cinfo, int pixelFormat, |
| 231 | int subsamp, int jpegQual, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 232 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 233 | int retval = 0; |
| 234 | char *env = NULL; |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 235 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 236 | cinfo->in_color_space = pf2cs[pixelFormat]; |
| 237 | cinfo->input_components = tjPixelSize[pixelFormat]; |
| 238 | jpeg_set_defaults(cinfo); |
DRC | 0713c1b | 2014-08-22 13:43:33 +0000 | [diff] [blame] | 239 | |
DRC | feccdcf | 2015-02-23 19:19:40 +0000 | [diff] [blame] | 240 | #ifndef NO_GETENV |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 241 | if ((env = getenv("TJ_OPTIMIZE")) != NULL && strlen(env) > 0 && |
| 242 | !strcmp(env, "1")) |
| 243 | cinfo->optimize_coding = TRUE; |
| 244 | if ((env = getenv("TJ_ARITHMETIC")) != NULL && strlen(env) > 0 && |
| 245 | !strcmp(env, "1")) |
| 246 | cinfo->arith_code = TRUE; |
| 247 | if ((env = getenv("TJ_RESTART")) != NULL && strlen(env) > 0) { |
| 248 | int temp = -1; |
| 249 | char tempc = 0; |
| 250 | |
| 251 | if (sscanf(env, "%d%c", &temp, &tempc) >= 1 && temp >= 0 && |
| 252 | temp <= 65535) { |
| 253 | if (toupper(tempc) == 'B') { |
| 254 | cinfo->restart_interval = temp; |
| 255 | cinfo->restart_in_rows = 0; |
| 256 | } else |
| 257 | cinfo->restart_in_rows = temp; |
| 258 | } |
| 259 | } |
DRC | feccdcf | 2015-02-23 19:19:40 +0000 | [diff] [blame] | 260 | #endif |
DRC | 0713c1b | 2014-08-22 13:43:33 +0000 | [diff] [blame] | 261 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 262 | if (jpegQual >= 0) { |
| 263 | jpeg_set_quality(cinfo, jpegQual, TRUE); |
| 264 | if (jpegQual >= 96 || flags & TJFLAG_ACCURATEDCT) |
| 265 | cinfo->dct_method = JDCT_ISLOW; |
| 266 | else |
| 267 | cinfo->dct_method = JDCT_FASTEST; |
| 268 | } |
| 269 | if (subsamp == TJSAMP_GRAY) |
| 270 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 271 | else if (pixelFormat == TJPF_CMYK) |
| 272 | jpeg_set_colorspace(cinfo, JCS_YCCK); |
| 273 | else |
| 274 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 275 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 276 | if (flags & TJFLAG_PROGRESSIVE) |
| 277 | jpeg_simple_progression(cinfo); |
DRC | feccdcf | 2015-02-23 19:19:40 +0000 | [diff] [blame] | 278 | #ifndef NO_GETENV |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 279 | else if ((env = getenv("TJ_PROGRESSIVE")) != NULL && strlen(env) > 0 && |
| 280 | !strcmp(env, "1")) |
| 281 | jpeg_simple_progression(cinfo); |
DRC | feccdcf | 2015-02-23 19:19:40 +0000 | [diff] [blame] | 282 | #endif |
DRC | 0713c1b | 2014-08-22 13:43:33 +0000 | [diff] [blame] | 283 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 284 | cinfo->comp_info[0].h_samp_factor = tjMCUWidth[subsamp] / 8; |
| 285 | cinfo->comp_info[1].h_samp_factor = 1; |
| 286 | cinfo->comp_info[2].h_samp_factor = 1; |
| 287 | if (cinfo->num_components > 3) |
| 288 | cinfo->comp_info[3].h_samp_factor = tjMCUWidth[subsamp] / 8; |
| 289 | cinfo->comp_info[0].v_samp_factor = tjMCUHeight[subsamp] / 8; |
| 290 | cinfo->comp_info[1].v_samp_factor = 1; |
| 291 | cinfo->comp_info[2].v_samp_factor = 1; |
| 292 | if (cinfo->num_components > 3) |
| 293 | cinfo->comp_info[3].v_samp_factor = tjMCUHeight[subsamp] / 8; |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 294 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 295 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 296 | } |
| 297 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 298 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 299 | static int getSubsamp(j_decompress_ptr dinfo) |
| 300 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 301 | int retval = -1, i, k; |
DRC | ea1eea4 | 2014-11-19 00:55:28 +0000 | [diff] [blame] | 302 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 303 | /* The sampling factors actually have no meaning with grayscale JPEG files, |
| 304 | and in fact it's possible to generate grayscale JPEGs with sampling |
| 305 | factors > 1 (even though those sampling factors are ignored by the |
| 306 | decompressor.) Thus, we need to treat grayscale as a special case. */ |
| 307 | if (dinfo->num_components == 1 && dinfo->jpeg_color_space == JCS_GRAYSCALE) |
| 308 | return TJSAMP_GRAY; |
DRC | ea1eea4 | 2014-11-19 00:55:28 +0000 | [diff] [blame] | 309 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 310 | for (i = 0; i < NUMSUBOPT; i++) { |
| 311 | if (dinfo->num_components == pixelsize[i] || |
| 312 | ((dinfo->jpeg_color_space == JCS_YCCK || |
| 313 | dinfo->jpeg_color_space == JCS_CMYK) && |
| 314 | pixelsize[i] == 3 && dinfo->num_components == 4)) { |
| 315 | if (dinfo->comp_info[0].h_samp_factor == tjMCUWidth[i] / 8 && |
| 316 | dinfo->comp_info[0].v_samp_factor == tjMCUHeight[i] / 8) { |
| 317 | int match = 0; |
| 318 | |
| 319 | for (k = 1; k < dinfo->num_components; k++) { |
| 320 | int href = 1, vref = 1; |
| 321 | |
| 322 | if (dinfo->jpeg_color_space == JCS_YCCK && k == 3) { |
| 323 | href = tjMCUWidth[i] / 8; vref = tjMCUHeight[i] / 8; |
| 324 | } |
| 325 | if (dinfo->comp_info[k].h_samp_factor == href && |
| 326 | dinfo->comp_info[k].v_samp_factor == vref) |
| 327 | match++; |
| 328 | } |
| 329 | if (match == dinfo->num_components - 1) { |
| 330 | retval = i; break; |
| 331 | } |
| 332 | } |
| 333 | /* Handle 4:2:2 and 4:4:0 images whose sampling factors are specified |
| 334 | in non-standard ways. */ |
| 335 | if (dinfo->comp_info[0].h_samp_factor == 2 && |
| 336 | dinfo->comp_info[0].v_samp_factor == 2 && |
| 337 | (i == TJSAMP_422 || i == TJSAMP_440)) { |
| 338 | int match = 0; |
| 339 | |
| 340 | for (k = 1; k < dinfo->num_components; k++) { |
| 341 | int href = tjMCUHeight[i] / 8, vref = tjMCUWidth[i] / 8; |
| 342 | |
| 343 | if (dinfo->jpeg_color_space == JCS_YCCK && k == 3) { |
| 344 | href = vref = 2; |
| 345 | } |
| 346 | if (dinfo->comp_info[k].h_samp_factor == href && |
| 347 | dinfo->comp_info[k].v_samp_factor == vref) |
| 348 | match++; |
| 349 | } |
| 350 | if (match == dinfo->num_components - 1) { |
| 351 | retval = i; break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | return retval; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 360 | /* General API functions */ |
| 361 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 362 | DLLEXPORT char *tjGetErrorStr2(tjhandle handle) |
DRC | b9ab64d | 2017-05-11 21:02:29 -0500 | [diff] [blame] | 363 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 364 | tjinstance *this = (tjinstance *)handle; |
| 365 | |
| 366 | if (this && this->isInstanceError) { |
| 367 | this->isInstanceError = FALSE; |
| 368 | return this->errStr; |
| 369 | } else |
| 370 | return errStr; |
DRC | b9ab64d | 2017-05-11 21:02:29 -0500 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 374 | DLLEXPORT char *tjGetErrorStr(void) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 375 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 376 | return errStr; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 380 | DLLEXPORT int tjGetErrorCode(tjhandle handle) |
DRC | d4092f6 | 2017-06-27 10:54:21 -0500 | [diff] [blame] | 381 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 382 | tjinstance *this = (tjinstance *)handle; |
| 383 | |
| 384 | if (this && this->jerr.warning) return TJERR_WARNING; |
| 385 | else return TJERR_FATAL; |
DRC | d4092f6 | 2017-06-27 10:54:21 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 389 | DLLEXPORT int tjDestroy(tjhandle handle) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 390 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 391 | getinstance(handle); |
| 392 | |
| 393 | if (setjmp(this->jerr.setjmp_buffer)) return -1; |
| 394 | if (this->init & COMPRESS) jpeg_destroy_compress(cinfo); |
| 395 | if (this->init & DECOMPRESS) jpeg_destroy_decompress(dinfo); |
| 396 | free(this); |
| 397 | return 0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 401 | /* These are exposed mainly because Windows can't malloc() and free() across |
| 402 | DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL |
| 403 | with turbojpeg.dll for compatibility reasons. However, these functions |
| 404 | can potentially be used for other purposes by different implementations. */ |
| 405 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 406 | DLLEXPORT void tjFree(unsigned char *buf) |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 407 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 408 | if (buf) free(buf); |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 412 | DLLEXPORT unsigned char *tjAlloc(int bytes) |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 413 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 414 | return (unsigned char *)malloc(bytes); |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 418 | /* Compressor */ |
| 419 | |
| 420 | static tjhandle _tjInitCompress(tjinstance *this) |
| 421 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 422 | static unsigned char buffer[1]; |
| 423 | unsigned char *buf = buffer; |
| 424 | unsigned long size = 1; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 425 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 426 | /* This is also straight out of example.txt */ |
| 427 | this->cinfo.err = jpeg_std_error(&this->jerr.pub); |
| 428 | this->jerr.pub.error_exit = my_error_exit; |
| 429 | this->jerr.pub.output_message = my_output_message; |
| 430 | this->jerr.emit_message = this->jerr.pub.emit_message; |
| 431 | this->jerr.pub.emit_message = my_emit_message; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 432 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 433 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 434 | /* If we get here, the JPEG code has signaled an error. */ |
| 435 | if (this) free(this); |
| 436 | return NULL; |
| 437 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 438 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 439 | jpeg_create_compress(&this->cinfo); |
| 440 | /* Make an initial call so it will create the destination manager */ |
| 441 | jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 442 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 443 | this->init |= COMPRESS; |
| 444 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 445 | } |
| 446 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 447 | DLLEXPORT tjhandle tjInitCompress(void) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 448 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 449 | tjinstance *this = NULL; |
| 450 | |
| 451 | if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) { |
| 452 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 453 | "tjInitCompress(): Memory allocation failure"); |
| 454 | return NULL; |
| 455 | } |
| 456 | MEMZERO(this, sizeof(tjinstance)); |
| 457 | snprintf(this->errStr, JMSG_LENGTH_MAX, "No error"); |
| 458 | return _tjInitCompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 459 | } |
| 460 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 461 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 462 | DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 463 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 464 | unsigned long retval = 0; |
| 465 | int mcuw, mcuh, chromasf; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 466 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 467 | if (width < 1 || height < 1 || jpegSubsamp < 0 || jpegSubsamp >= NUMSUBOPT) |
| 468 | _throwg("tjBufSize(): Invalid argument"); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 469 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 470 | /* This allows for rare corner cases in which a JPEG image can actually be |
| 471 | larger than the uncompressed input (we wouldn't mention it if it hadn't |
| 472 | happened before.) */ |
| 473 | mcuw = tjMCUWidth[jpegSubsamp]; |
| 474 | mcuh = tjMCUHeight[jpegSubsamp]; |
| 475 | chromasf = jpegSubsamp == TJSAMP_GRAY ? 0 : 4 * 64 / (mcuw * mcuh); |
| 476 | retval = PAD(width, mcuw) * PAD(height, mcuh) * (2 + chromasf) + 2048; |
| 477 | |
| 478 | bailout: |
| 479 | return retval; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 480 | } |
| 481 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 482 | DLLEXPORT unsigned long TJBUFSIZE(int width, int height) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 483 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 484 | unsigned long retval = 0; |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 485 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 486 | if (width < 1 || height < 1) |
| 487 | _throwg("TJBUFSIZE(): Invalid argument"); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 488 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 489 | /* This allows for rare corner cases in which a JPEG image can actually be |
| 490 | larger than the uncompressed input (we wouldn't mention it if it hadn't |
| 491 | happened before.) */ |
| 492 | retval = PAD(width, 16) * PAD(height, 16) * 6 + 2048; |
| 493 | |
| 494 | bailout: |
| 495 | return retval; |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 496 | } |
| 497 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 498 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 499 | DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height, |
| 500 | int subsamp) |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 501 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 502 | int retval = 0, nc, i; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 503 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 504 | if (subsamp < 0 || subsamp >= NUMSUBOPT) |
| 505 | _throwg("tjBufSizeYUV2(): Invalid argument"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 506 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 507 | nc = (subsamp == TJSAMP_GRAY ? 1 : 3); |
| 508 | for (i = 0; i < nc; i++) { |
| 509 | int pw = tjPlaneWidth(i, width, subsamp); |
| 510 | int stride = PAD(pw, pad); |
| 511 | int ph = tjPlaneHeight(i, height, subsamp); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 512 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 513 | if (pw < 0 || ph < 0) return -1; |
| 514 | else retval += stride * ph; |
| 515 | } |
| 516 | |
| 517 | bailout: |
| 518 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 519 | } |
| 520 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 521 | DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 522 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 523 | return tjBufSizeYUV2(width, 4, height, subsamp); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 524 | } |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 525 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 526 | DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int subsamp) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 527 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 528 | return tjBufSizeYUV(width, height, subsamp); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 532 | DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp) |
| 533 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 534 | int pw, nc, retval = 0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 535 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 536 | if (width < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP) |
| 537 | _throwg("tjPlaneWidth(): Invalid argument"); |
| 538 | nc = (subsamp == TJSAMP_GRAY ? 1 : 3); |
| 539 | if (componentID < 0 || componentID >= nc) |
| 540 | _throwg("tjPlaneWidth(): Invalid argument"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 541 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 542 | pw = PAD(width, tjMCUWidth[subsamp] / 8); |
| 543 | if (componentID == 0) |
| 544 | retval = pw; |
| 545 | else |
| 546 | retval = pw * 8 / tjMCUWidth[subsamp]; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 547 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 548 | bailout: |
| 549 | return retval; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp) |
| 554 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 555 | int ph, nc, retval = 0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 556 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 557 | if (height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP) |
| 558 | _throwg("tjPlaneHeight(): Invalid argument"); |
| 559 | nc = (subsamp == TJSAMP_GRAY ? 1 : 3); |
| 560 | if (componentID < 0 || componentID >= nc) |
| 561 | _throwg("tjPlaneHeight(): Invalid argument"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 562 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 563 | ph = PAD(height, tjMCUHeight[subsamp] / 8); |
| 564 | if (componentID == 0) |
| 565 | retval = ph; |
| 566 | else |
| 567 | retval = ph * 8 / tjMCUHeight[subsamp]; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 568 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 569 | bailout: |
| 570 | return retval; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 574 | DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride, |
| 575 | int height, int subsamp) |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 576 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 577 | unsigned long retval = 0; |
| 578 | int pw, ph; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 579 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 580 | if (width < 1 || height < 1 || subsamp < 0 || subsamp >= NUMSUBOPT) |
| 581 | _throwg("tjPlaneSizeYUV(): Invalid argument"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 582 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 583 | pw = tjPlaneWidth(componentID, width, subsamp); |
| 584 | ph = tjPlaneHeight(componentID, height, subsamp); |
| 585 | if (pw < 0 || ph < 0) return -1; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 586 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 587 | if (stride == 0) stride = pw; |
| 588 | else stride = abs(stride); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 589 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 590 | retval = stride * (ph - 1) + pw; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 591 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 592 | bailout: |
| 593 | return retval; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 597 | DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf, |
| 598 | int width, int pitch, int height, int pixelFormat, |
| 599 | unsigned char **jpegBuf, unsigned long *jpegSize, |
| 600 | int jpegSubsamp, int jpegQual, int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 601 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 602 | int i, retval = 0, alloc = 1; |
| 603 | JSAMPROW *row_pointer = NULL; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 604 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 605 | getcinstance(handle) |
| 606 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
| 607 | if ((this->init & COMPRESS) == 0) |
| 608 | _throw("tjCompress2(): Instance has not been initialized for compression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 609 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 610 | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
| 611 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL || |
| 612 | jpegSize == NULL || jpegSubsamp < 0 || jpegSubsamp >= NUMSUBOPT || |
| 613 | jpegQual < 0 || jpegQual > 100) |
| 614 | _throw("tjCompress2(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 615 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 616 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 617 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 618 | if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * height)) == NULL) |
| 619 | _throw("tjCompress2(): Memory allocation failure"); |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 620 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 621 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 622 | /* If we get here, the JPEG code has signaled an error. */ |
| 623 | retval = -1; goto bailout; |
| 624 | } |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 625 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 626 | cinfo->image_width = width; |
| 627 | cinfo->image_height = height; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 628 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 629 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 630 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 631 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 632 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 633 | if (flags & TJFLAG_NOREALLOC) { |
| 634 | alloc = 0; *jpegSize = tjBufSize(width, height, jpegSubsamp); |
| 635 | } |
| 636 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
| 637 | if (setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags) == -1) |
| 638 | return -1; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 639 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 640 | jpeg_start_compress(cinfo, TRUE); |
| 641 | for (i = 0; i < height; i++) { |
| 642 | if (flags & TJFLAG_BOTTOMUP) |
| 643 | row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * pitch]; |
| 644 | else |
| 645 | row_pointer[i] = (JSAMPROW)&srcBuf[i * pitch]; |
| 646 | } |
| 647 | while (cinfo->next_scanline < cinfo->image_height) |
| 648 | jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
| 649 | cinfo->image_height - cinfo->next_scanline); |
| 650 | jpeg_finish_compress(cinfo); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 651 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 652 | bailout: |
| 653 | if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo); |
| 654 | if (row_pointer) free(row_pointer); |
| 655 | if (this->jerr.warning) retval = -1; |
| 656 | this->jerr.stopOnWarning = FALSE; |
| 657 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 658 | } |
| 659 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 660 | DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width, |
| 661 | int pitch, int height, int pixelSize, |
| 662 | unsigned char *jpegBuf, unsigned long *jpegSize, |
| 663 | int jpegSubsamp, int jpegQual, int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 664 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 665 | int retval = 0; |
| 666 | unsigned long size; |
| 667 | |
| 668 | if (flags & TJ_YUV) { |
| 669 | size = tjBufSizeYUV(width, height, jpegSubsamp); |
| 670 | retval = tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 671 | getPixelFormat(pixelSize, flags), jpegBuf, |
| 672 | jpegSubsamp, flags); |
| 673 | } else { |
| 674 | retval = tjCompress2(handle, srcBuf, width, pitch, height, |
| 675 | getPixelFormat(pixelSize, flags), &jpegBuf, &size, |
| 676 | jpegSubsamp, jpegQual, flags | TJFLAG_NOREALLOC); |
| 677 | } |
| 678 | *jpegSize = size; |
| 679 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 683 | DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf, |
| 684 | int width, int pitch, int height, |
| 685 | int pixelFormat, unsigned char **dstPlanes, |
| 686 | int *strides, int subsamp, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 687 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 688 | JSAMPROW *row_pointer = NULL; |
| 689 | JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS]; |
| 690 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS]; |
| 691 | JSAMPROW *outbuf[MAX_COMPONENTS]; |
| 692 | int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS]; |
| 693 | JSAMPLE *ptr; |
| 694 | jpeg_component_info *compptr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 695 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 696 | getcinstance(handle); |
| 697 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
DRC | b51ee89 | 2013-10-31 05:00:19 +0000 | [diff] [blame] | 698 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 699 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 700 | tmpbuf[i] = NULL; _tmpbuf[i] = NULL; |
| 701 | tmpbuf2[i] = NULL; _tmpbuf2[i] = NULL; outbuf[i] = NULL; |
| 702 | } |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 703 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 704 | if ((this->init & COMPRESS) == 0) |
| 705 | _throw("tjEncodeYUVPlanes(): Instance has not been initialized for compression"); |
DRC | e2f8e69 | 2013-10-30 22:21:06 +0000 | [diff] [blame] | 706 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 707 | if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
| 708 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF || !dstPlanes || |
| 709 | !dstPlanes[0] || subsamp < 0 || subsamp >= NUMSUBOPT) |
| 710 | _throw("tjEncodeYUVPlanes(): Invalid argument"); |
| 711 | if (subsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2])) |
| 712 | _throw("tjEncodeYUVPlanes(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 713 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 714 | if (pixelFormat == TJPF_CMYK) |
| 715 | _throw("tjEncodeYUVPlanes(): Cannot generate YUV images from CMYK pixels"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 716 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 717 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 718 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 719 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 720 | /* If we get here, the JPEG code has signaled an error. */ |
| 721 | retval = -1; goto bailout; |
| 722 | } |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 723 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 724 | cinfo->image_width = width; |
| 725 | cinfo->image_height = height; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 726 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 727 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 728 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 729 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 730 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 731 | if (setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags) == -1) return -1; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 732 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 733 | /* Execute only the parts of jpeg_start_compress() that we need. If we |
| 734 | were to call the whole jpeg_start_compress() function, then it would try |
| 735 | to write the file headers, which could overflow the output buffer if the |
| 736 | YUV image were very small. */ |
| 737 | if (cinfo->global_state != CSTATE_START) |
| 738 | _throw("tjEncodeYUVPlanes(): libjpeg API is in the wrong state"); |
| 739 | (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo); |
| 740 | jinit_c_master_control(cinfo, FALSE); |
| 741 | jinit_color_converter(cinfo); |
| 742 | jinit_downsampler(cinfo); |
| 743 | (*cinfo->cconvert->start_pass) (cinfo); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 744 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 745 | pw0 = PAD(width, cinfo->max_h_samp_factor); |
| 746 | ph0 = PAD(height, cinfo->max_v_samp_factor); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 747 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 748 | if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL) |
| 749 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 750 | for (i = 0; i < height; i++) { |
| 751 | if (flags & TJFLAG_BOTTOMUP) |
| 752 | row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * pitch]; |
| 753 | else |
| 754 | row_pointer[i] = (JSAMPROW)&srcBuf[i * pitch]; |
| 755 | } |
| 756 | if (height < ph0) |
| 757 | for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1]; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 758 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 759 | for (i = 0; i < cinfo->num_components; i++) { |
| 760 | compptr = &cinfo->comp_info[i]; |
| 761 | _tmpbuf[i] = (JSAMPLE *)malloc( |
| 762 | PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) / |
| 763 | compptr->h_samp_factor, 32) * |
| 764 | cinfo->max_v_samp_factor + 32); |
| 765 | if (!_tmpbuf[i]) |
| 766 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 767 | tmpbuf[i] = |
| 768 | (JSAMPROW *)malloc(sizeof(JSAMPROW) * cinfo->max_v_samp_factor); |
| 769 | if (!tmpbuf[i]) |
| 770 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 771 | for (row = 0; row < cinfo->max_v_samp_factor; row++) { |
| 772 | unsigned char *_tmpbuf_aligned = |
| 773 | (unsigned char *)PAD((size_t)_tmpbuf[i], 32); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 774 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 775 | tmpbuf[i][row] = &_tmpbuf_aligned[ |
| 776 | PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) / |
| 777 | compptr->h_samp_factor, 32) * row]; |
| 778 | } |
| 779 | _tmpbuf2[i] = |
| 780 | (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) * |
| 781 | compptr->v_samp_factor + 32); |
| 782 | if (!_tmpbuf2[i]) |
| 783 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 784 | tmpbuf2[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor); |
| 785 | if (!tmpbuf2[i]) |
| 786 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 787 | for (row = 0; row < compptr->v_samp_factor; row++) { |
| 788 | unsigned char *_tmpbuf2_aligned = |
| 789 | (unsigned char *)PAD((size_t)_tmpbuf2[i], 32); |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 790 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 791 | tmpbuf2[i][row] = |
| 792 | &_tmpbuf2_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row]; |
| 793 | } |
| 794 | pw[i] = pw0 * compptr->h_samp_factor / cinfo->max_h_samp_factor; |
| 795 | ph[i] = ph0 * compptr->v_samp_factor / cinfo->max_v_samp_factor; |
| 796 | outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]); |
| 797 | if (!outbuf[i]) |
| 798 | _throw("tjEncodeYUVPlanes(): Memory allocation failure"); |
| 799 | ptr = dstPlanes[i]; |
| 800 | for (row = 0; row < ph[i]; row++) { |
| 801 | outbuf[i][row] = ptr; |
| 802 | ptr += (strides && strides[i] != 0) ? strides[i] : pw[i]; |
| 803 | } |
| 804 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 805 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 806 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 807 | /* If we get here, the JPEG code has signaled an error. */ |
| 808 | retval = -1; goto bailout; |
| 809 | } |
| 810 | |
| 811 | for (row = 0; row < ph0; row += cinfo->max_v_samp_factor) { |
| 812 | (*cinfo->cconvert->color_convert) (cinfo, &row_pointer[row], tmpbuf, 0, |
| 813 | cinfo->max_v_samp_factor); |
| 814 | (cinfo->downsample->downsample) (cinfo, tmpbuf, 0, tmpbuf2, 0); |
| 815 | for (i = 0, compptr = cinfo->comp_info; i < cinfo->num_components; |
| 816 | i++, compptr++) |
| 817 | jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i], |
| 818 | row * compptr->v_samp_factor / cinfo->max_v_samp_factor, |
| 819 | compptr->v_samp_factor, pw[i]); |
| 820 | } |
| 821 | cinfo->next_scanline += height; |
| 822 | jpeg_abort_compress(cinfo); |
| 823 | |
| 824 | bailout: |
| 825 | if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo); |
| 826 | if (row_pointer) free(row_pointer); |
| 827 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 828 | if (tmpbuf[i] != NULL) free(tmpbuf[i]); |
| 829 | if (_tmpbuf[i] != NULL) free(_tmpbuf[i]); |
| 830 | if (tmpbuf2[i] != NULL) free(tmpbuf2[i]); |
| 831 | if (_tmpbuf2[i] != NULL) free(_tmpbuf2[i]); |
| 832 | if (outbuf[i] != NULL) free(outbuf[i]); |
| 833 | } |
| 834 | if (this->jerr.warning) retval = -1; |
| 835 | this->jerr.stopOnWarning = FALSE; |
| 836 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 837 | } |
| 838 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 839 | DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf, |
| 840 | int width, int pitch, int height, int pixelFormat, |
| 841 | unsigned char *dstBuf, int pad, int subsamp, |
| 842 | int flags) |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 843 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 844 | unsigned char *dstPlanes[3]; |
| 845 | int pw0, ph0, strides[3], retval = -1; |
| 846 | tjinstance *this = (tjinstance *)handle; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 847 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 848 | if (!this) _throwg("tjEncodeYUV3(): Invalid handle"); |
| 849 | this->isInstanceError = FALSE; |
DRC | b9ab64d | 2017-05-11 21:02:29 -0500 | [diff] [blame] | 850 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 851 | if (width <= 0 || height <= 0 || dstBuf == NULL || pad < 0 || !isPow2(pad) || |
| 852 | subsamp < 0 || subsamp >= NUMSUBOPT) |
| 853 | _throw("tjEncodeYUV3(): Invalid argument"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 854 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 855 | pw0 = tjPlaneWidth(0, width, subsamp); |
| 856 | ph0 = tjPlaneHeight(0, height, subsamp); |
| 857 | dstPlanes[0] = dstBuf; |
| 858 | strides[0] = PAD(pw0, pad); |
| 859 | if (subsamp == TJSAMP_GRAY) { |
| 860 | strides[1] = strides[2] = 0; |
| 861 | dstPlanes[1] = dstPlanes[2] = NULL; |
| 862 | } else { |
| 863 | int pw1 = tjPlaneWidth(1, width, subsamp); |
| 864 | int ph1 = tjPlaneHeight(1, height, subsamp); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 865 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 866 | strides[1] = strides[2] = PAD(pw1, pad); |
| 867 | dstPlanes[1] = dstPlanes[0] + strides[0] * ph0; |
| 868 | dstPlanes[2] = dstPlanes[1] + strides[1] * ph1; |
| 869 | } |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 870 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 871 | return tjEncodeYUVPlanes(handle, srcBuf, width, pitch, height, pixelFormat, |
| 872 | dstPlanes, strides, subsamp, flags); |
| 873 | |
| 874 | bailout: |
| 875 | return retval; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 876 | } |
| 877 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 878 | DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width, |
| 879 | int pitch, int height, int pixelFormat, |
| 880 | unsigned char *dstBuf, int subsamp, int flags) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 881 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 882 | return tjEncodeYUV3(handle, srcBuf, width, pitch, height, pixelFormat, |
| 883 | dstBuf, 4, subsamp, flags); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 884 | } |
| 885 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 886 | DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width, |
| 887 | int pitch, int height, int pixelSize, |
| 888 | unsigned char *dstBuf, int subsamp, int flags) |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 889 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 890 | return tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 891 | getPixelFormat(pixelSize, flags), dstBuf, subsamp, |
| 892 | flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 896 | DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle, |
| 897 | const unsigned char **srcPlanes, |
| 898 | int width, const int *strides, |
| 899 | int height, int subsamp, |
| 900 | unsigned char **jpegBuf, |
| 901 | unsigned long *jpegSize, int jpegQual, |
| 902 | int flags) |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 903 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 904 | int i, row, retval = 0, alloc = 1; |
| 905 | int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS], |
| 906 | tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS]; |
| 907 | JSAMPLE *_tmpbuf = NULL, *ptr; |
| 908 | JSAMPROW *inbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS]; |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 909 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 910 | getcinstance(handle) |
| 911 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
DRC | b51ee89 | 2013-10-31 05:00:19 +0000 | [diff] [blame] | 912 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 913 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 914 | tmpbuf[i] = NULL; inbuf[i] = NULL; |
| 915 | } |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 916 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 917 | if ((this->init & COMPRESS) == 0) |
| 918 | _throw("tjCompressFromYUVPlanes(): Instance has not been initialized for compression"); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 919 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 920 | if (!srcPlanes || !srcPlanes[0] || width <= 0 || height <= 0 || |
| 921 | subsamp < 0 || subsamp >= NUMSUBOPT || jpegBuf == NULL || |
| 922 | jpegSize == NULL || jpegQual < 0 || jpegQual > 100) |
| 923 | _throw("tjCompressFromYUVPlanes(): Invalid argument"); |
| 924 | if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2])) |
| 925 | _throw("tjCompressFromYUVPlanes(): Invalid argument"); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 926 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 927 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 928 | /* If we get here, the JPEG code has signaled an error. */ |
| 929 | retval = -1; goto bailout; |
| 930 | } |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 931 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 932 | cinfo->image_width = width; |
| 933 | cinfo->image_height = height; |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 934 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 935 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 936 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 937 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 938 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 939 | if (flags & TJFLAG_NOREALLOC) { |
| 940 | alloc = 0; *jpegSize = tjBufSize(width, height, subsamp); |
| 941 | } |
| 942 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
| 943 | if (setCompDefaults(cinfo, TJPF_RGB, subsamp, jpegQual, flags) == -1) |
| 944 | return -1; |
| 945 | cinfo->raw_data_in = TRUE; |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 946 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 947 | jpeg_start_compress(cinfo, TRUE); |
| 948 | for (i = 0; i < cinfo->num_components; i++) { |
| 949 | jpeg_component_info *compptr = &cinfo->comp_info[i]; |
| 950 | int ih; |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 951 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 952 | iw[i] = compptr->width_in_blocks * DCTSIZE; |
| 953 | ih = compptr->height_in_blocks * DCTSIZE; |
| 954 | pw[i] = PAD(cinfo->image_width, cinfo->max_h_samp_factor) * |
| 955 | compptr->h_samp_factor / cinfo->max_h_samp_factor; |
| 956 | ph[i] = PAD(cinfo->image_height, cinfo->max_v_samp_factor) * |
| 957 | compptr->v_samp_factor / cinfo->max_v_samp_factor; |
| 958 | if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1; |
| 959 | th[i] = compptr->v_samp_factor * DCTSIZE; |
| 960 | tmpbufsize += iw[i] * th[i]; |
| 961 | if ((inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL) |
| 962 | _throw("tjCompressFromYUVPlanes(): Memory allocation failure"); |
| 963 | ptr = (JSAMPLE *)srcPlanes[i]; |
| 964 | for (row = 0; row < ph[i]; row++) { |
| 965 | inbuf[i][row] = ptr; |
| 966 | ptr += (strides && strides[i] != 0) ? strides[i] : pw[i]; |
| 967 | } |
| 968 | } |
| 969 | if (usetmpbuf) { |
| 970 | if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL) |
| 971 | _throw("tjCompressFromYUVPlanes(): Memory allocation failure"); |
| 972 | ptr = _tmpbuf; |
| 973 | for (i = 0; i < cinfo->num_components; i++) { |
| 974 | if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL) |
| 975 | _throw("tjCompressFromYUVPlanes(): Memory allocation failure"); |
| 976 | for (row = 0; row < th[i]; row++) { |
| 977 | tmpbuf[i][row] = ptr; |
| 978 | ptr += iw[i]; |
| 979 | } |
| 980 | } |
| 981 | } |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 982 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 983 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 984 | /* If we get here, the JPEG code has signaled an error. */ |
| 985 | retval = -1; goto bailout; |
| 986 | } |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 987 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 988 | for (row = 0; row < (int)cinfo->image_height; |
| 989 | row += cinfo->max_v_samp_factor * DCTSIZE) { |
| 990 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
| 991 | int crow[MAX_COMPONENTS]; |
| 992 | |
| 993 | for (i = 0; i < cinfo->num_components; i++) { |
| 994 | jpeg_component_info *compptr = &cinfo->comp_info[i]; |
| 995 | |
| 996 | crow[i] = row * compptr->v_samp_factor / cinfo->max_v_samp_factor; |
| 997 | if (usetmpbuf) { |
| 998 | int j, k; |
| 999 | |
| 1000 | for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) { |
| 1001 | memcpy(tmpbuf[i][j], inbuf[i][crow[i] + j], pw[i]); |
| 1002 | /* Duplicate last sample in row to fill out MCU */ |
| 1003 | for (k = pw[i]; k < iw[i]; k++) |
| 1004 | tmpbuf[i][j][k] = tmpbuf[i][j][pw[i] - 1]; |
| 1005 | } |
| 1006 | /* Duplicate last row to fill out MCU */ |
| 1007 | for (j = ph[i] - crow[i]; j < th[i]; j++) |
| 1008 | memcpy(tmpbuf[i][j], tmpbuf[i][ph[i] - crow[i] - 1], iw[i]); |
| 1009 | yuvptr[i] = tmpbuf[i]; |
| 1010 | } else |
| 1011 | yuvptr[i] = &inbuf[i][crow[i]]; |
| 1012 | } |
| 1013 | jpeg_write_raw_data(cinfo, yuvptr, cinfo->max_v_samp_factor * DCTSIZE); |
| 1014 | } |
| 1015 | jpeg_finish_compress(cinfo); |
| 1016 | |
| 1017 | bailout: |
| 1018 | if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo); |
| 1019 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 1020 | if (tmpbuf[i]) free(tmpbuf[i]); |
| 1021 | if (inbuf[i]) free(inbuf[i]); |
| 1022 | } |
| 1023 | if (_tmpbuf) free(_tmpbuf); |
| 1024 | if (this->jerr.warning) retval = -1; |
| 1025 | this->jerr.stopOnWarning = FALSE; |
| 1026 | return retval; |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1029 | DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf, |
| 1030 | int width, int pad, int height, int subsamp, |
| 1031 | unsigned char **jpegBuf, |
| 1032 | unsigned long *jpegSize, int jpegQual, |
| 1033 | int flags) |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1034 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1035 | const unsigned char *srcPlanes[3]; |
| 1036 | int pw0, ph0, strides[3], retval = -1; |
| 1037 | tjinstance *this = (tjinstance *)handle; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1038 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1039 | if (!this) _throwg("tjCompressFromYUV(): Invalid handle"); |
| 1040 | this->isInstanceError = FALSE; |
DRC | b9ab64d | 2017-05-11 21:02:29 -0500 | [diff] [blame] | 1041 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1042 | if (srcBuf == NULL || width <= 0 || pad < 1 || height <= 0 || subsamp < 0 || |
| 1043 | subsamp >= NUMSUBOPT) |
| 1044 | _throw("tjCompressFromYUV(): Invalid argument"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1045 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1046 | pw0 = tjPlaneWidth(0, width, subsamp); |
| 1047 | ph0 = tjPlaneHeight(0, height, subsamp); |
| 1048 | srcPlanes[0] = srcBuf; |
| 1049 | strides[0] = PAD(pw0, pad); |
| 1050 | if (subsamp == TJSAMP_GRAY) { |
| 1051 | strides[1] = strides[2] = 0; |
| 1052 | srcPlanes[1] = srcPlanes[2] = NULL; |
| 1053 | } else { |
| 1054 | int pw1 = tjPlaneWidth(1, width, subsamp); |
| 1055 | int ph1 = tjPlaneHeight(1, height, subsamp); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1056 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1057 | strides[1] = strides[2] = PAD(pw1, pad); |
| 1058 | srcPlanes[1] = srcPlanes[0] + strides[0] * ph0; |
| 1059 | srcPlanes[2] = srcPlanes[1] + strides[1] * ph1; |
| 1060 | } |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1061 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1062 | return tjCompressFromYUVPlanes(handle, srcPlanes, width, strides, height, |
| 1063 | subsamp, jpegBuf, jpegSize, jpegQual, flags); |
| 1064 | |
| 1065 | bailout: |
| 1066 | return retval; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 1069 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1070 | /* Decompressor */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1071 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1072 | static tjhandle _tjInitDecompress(tjinstance *this) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1073 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1074 | static unsigned char buffer[1]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1075 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1076 | /* This is also straight out of example.txt */ |
| 1077 | this->dinfo.err = jpeg_std_error(&this->jerr.pub); |
| 1078 | this->jerr.pub.error_exit = my_error_exit; |
| 1079 | this->jerr.pub.output_message = my_output_message; |
| 1080 | this->jerr.emit_message = this->jerr.pub.emit_message; |
| 1081 | this->jerr.pub.emit_message = my_emit_message; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1082 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1083 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1084 | /* If we get here, the JPEG code has signaled an error. */ |
| 1085 | if (this) free(this); |
| 1086 | return NULL; |
| 1087 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1088 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1089 | jpeg_create_decompress(&this->dinfo); |
| 1090 | /* Make an initial call so it will create the source manager */ |
| 1091 | jpeg_mem_src_tj(&this->dinfo, buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1092 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1093 | this->init |= DECOMPRESS; |
| 1094 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1097 | DLLEXPORT tjhandle tjInitDecompress(void) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1098 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1099 | tjinstance *this; |
| 1100 | |
| 1101 | if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) { |
| 1102 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 1103 | "tjInitDecompress(): Memory allocation failure"); |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | MEMZERO(this, sizeof(tjinstance)); |
| 1107 | snprintf(this->errStr, JMSG_LENGTH_MAX, "No error"); |
| 1108 | return _tjInitDecompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1111 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1112 | DLLEXPORT int tjDecompressHeader3(tjhandle handle, |
| 1113 | const unsigned char *jpegBuf, |
| 1114 | unsigned long jpegSize, int *width, |
| 1115 | int *height, int *jpegSubsamp, |
| 1116 | int *jpegColorspace) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1117 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1118 | int retval = 0; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1119 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1120 | getdinstance(handle); |
| 1121 | if ((this->init & DECOMPRESS) == 0) |
| 1122 | _throw("tjDecompressHeader3(): Instance has not been initialized for decompression"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1123 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1124 | if (jpegBuf == NULL || jpegSize <= 0 || width == NULL || height == NULL || |
| 1125 | jpegSubsamp == NULL || jpegColorspace == NULL) |
| 1126 | _throw("tjDecompressHeader3(): Invalid argument"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1127 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1128 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1129 | /* If we get here, the JPEG code has signaled an error. */ |
| 1130 | return -1; |
| 1131 | } |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1132 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1133 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1134 | jpeg_read_header(dinfo, TRUE); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1135 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1136 | *width = dinfo->image_width; |
| 1137 | *height = dinfo->image_height; |
| 1138 | *jpegSubsamp = getSubsamp(dinfo); |
| 1139 | switch (dinfo->jpeg_color_space) { |
| 1140 | case JCS_GRAYSCALE: *jpegColorspace = TJCS_GRAY; break; |
| 1141 | case JCS_RGB: *jpegColorspace = TJCS_RGB; break; |
| 1142 | case JCS_YCbCr: *jpegColorspace = TJCS_YCbCr; break; |
| 1143 | case JCS_CMYK: *jpegColorspace = TJCS_CMYK; break; |
| 1144 | case JCS_YCCK: *jpegColorspace = TJCS_YCCK; break; |
| 1145 | default: *jpegColorspace = -1; break; |
| 1146 | } |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1147 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1148 | jpeg_abort_decompress(dinfo); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1149 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1150 | if (*jpegSubsamp < 0) |
| 1151 | _throw("tjDecompressHeader3(): Could not determine subsampling type for JPEG image"); |
| 1152 | if (*jpegColorspace < 0) |
| 1153 | _throw("tjDecompressHeader3(): Could not determine colorspace of JPEG image"); |
| 1154 | if (*width < 1 || *height < 1) |
| 1155 | _throw("tjDecompressHeader3(): Invalid data returned in header"); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1156 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1157 | bailout: |
| 1158 | if (this->jerr.warning) retval = -1; |
| 1159 | return retval; |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1162 | DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf, |
| 1163 | unsigned long jpegSize, int *width, |
| 1164 | int *height, int *jpegSubsamp) |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 1165 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1166 | int jpegColorspace; |
| 1167 | |
| 1168 | return tjDecompressHeader3(handle, jpegBuf, jpegSize, width, height, |
| 1169 | jpegSubsamp, &jpegColorspace); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1172 | DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf, |
| 1173 | unsigned long jpegSize, int *width, |
| 1174 | int *height) |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1175 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1176 | int jpegSubsamp; |
| 1177 | |
| 1178 | return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height, |
| 1179 | &jpegSubsamp); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1183 | DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 1184 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1185 | if (numscalingfactors == NULL) { |
| 1186 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 1187 | "tjGetScalingFactors(): Invalid argument"); |
| 1188 | return NULL; |
| 1189 | } |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 1190 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1191 | *numscalingfactors = NUMSF; |
| 1192 | return (tjscalingfactor *)sf; |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1196 | DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf, |
| 1197 | unsigned long jpegSize, unsigned char *dstBuf, |
| 1198 | int width, int pitch, int height, int pixelFormat, |
| 1199 | int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1200 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1201 | JSAMPROW *row_pointer = NULL; |
| 1202 | int i, retval = 0, jpegwidth, jpegheight, scaledw, scaledh; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1203 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1204 | getdinstance(handle); |
| 1205 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
| 1206 | if ((this->init & DECOMPRESS) == 0) |
| 1207 | _throw("tjDecompress2(): Instance has not been initialized for decompression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1208 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1209 | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 || |
| 1210 | pitch < 0 || height < 0 || pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
| 1211 | _throw("tjDecompress2(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1212 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1213 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1214 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1215 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1216 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1217 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1218 | /* If we get here, the JPEG code has signaled an error. */ |
| 1219 | retval = -1; goto bailout; |
| 1220 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1221 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1222 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1223 | jpeg_read_header(dinfo, TRUE); |
| 1224 | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
| 1225 | if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST; |
| 1226 | if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1227 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1228 | jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height; |
| 1229 | if (width == 0) width = jpegwidth; |
| 1230 | if (height == 0) height = jpegheight; |
| 1231 | for (i = 0; i < NUMSF; i++) { |
| 1232 | scaledw = TJSCALED(jpegwidth, sf[i]); |
| 1233 | scaledh = TJSCALED(jpegheight, sf[i]); |
| 1234 | if (scaledw <= width && scaledh <= height) |
| 1235 | break; |
| 1236 | } |
| 1237 | if (i >= NUMSF) |
| 1238 | _throw("tjDecompress2(): Could not scale down to desired image dimensions"); |
| 1239 | width = scaledw; height = scaledh; |
| 1240 | dinfo->scale_num = sf[i].num; |
| 1241 | dinfo->scale_denom = sf[i].denom; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1242 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1243 | jpeg_start_decompress(dinfo); |
| 1244 | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 1245 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1246 | if ((row_pointer = |
| 1247 | (JSAMPROW *)malloc(sizeof(JSAMPROW) * dinfo->output_height)) == NULL) |
| 1248 | _throw("tjDecompress2(): Memory allocation failure"); |
| 1249 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1250 | /* If we get here, the JPEG code has signaled an error. */ |
| 1251 | retval = -1; goto bailout; |
| 1252 | } |
| 1253 | for (i = 0; i < (int)dinfo->output_height; i++) { |
| 1254 | if (flags & TJFLAG_BOTTOMUP) |
| 1255 | row_pointer[i] = &dstBuf[(dinfo->output_height - i - 1) * pitch]; |
| 1256 | else |
| 1257 | row_pointer[i] = &dstBuf[i * pitch]; |
| 1258 | } |
| 1259 | while (dinfo->output_scanline < dinfo->output_height) |
| 1260 | jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
| 1261 | dinfo->output_height - dinfo->output_scanline); |
| 1262 | jpeg_finish_decompress(dinfo); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1263 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1264 | bailout: |
| 1265 | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
| 1266 | if (row_pointer) free(row_pointer); |
| 1267 | if (this->jerr.warning) retval = -1; |
| 1268 | this->jerr.stopOnWarning = FALSE; |
| 1269 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1272 | DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf, |
| 1273 | unsigned long jpegSize, unsigned char *dstBuf, |
| 1274 | int width, int pitch, int height, int pixelSize, |
| 1275 | int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1276 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1277 | if (flags & TJ_YUV) |
| 1278 | return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags); |
| 1279 | else |
| 1280 | return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch, |
| 1281 | height, getPixelFormat(pixelSize, flags), flags); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1285 | static int setDecodeDefaults(struct jpeg_decompress_struct *dinfo, |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1286 | int pixelFormat, int subsamp, int flags) |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1287 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1288 | int i; |
DRC | 895fd6d | 2014-02-28 09:35:34 +0000 | [diff] [blame] | 1289 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1290 | dinfo->scale_num = dinfo->scale_denom = 1; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1291 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1292 | if (subsamp == TJSAMP_GRAY) { |
| 1293 | dinfo->num_components = dinfo->comps_in_scan = 1; |
| 1294 | dinfo->jpeg_color_space = JCS_GRAYSCALE; |
| 1295 | } else { |
| 1296 | dinfo->num_components = dinfo->comps_in_scan = 3; |
| 1297 | dinfo->jpeg_color_space = JCS_YCbCr; |
| 1298 | } |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1299 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1300 | dinfo->comp_info = (jpeg_component_info *) |
| 1301 | (*dinfo->mem->alloc_small) ((j_common_ptr)dinfo, JPOOL_IMAGE, |
| 1302 | dinfo->num_components * |
| 1303 | sizeof(jpeg_component_info)); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1304 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1305 | for (i = 0; i < dinfo->num_components; i++) { |
| 1306 | jpeg_component_info *compptr = &dinfo->comp_info[i]; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1307 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1308 | compptr->h_samp_factor = (i == 0) ? tjMCUWidth[subsamp] / 8 : 1; |
| 1309 | compptr->v_samp_factor = (i == 0) ? tjMCUHeight[subsamp] / 8 : 1; |
| 1310 | compptr->component_index = i; |
| 1311 | compptr->component_id = i + 1; |
| 1312 | compptr->quant_tbl_no = compptr->dc_tbl_no = |
| 1313 | compptr->ac_tbl_no = (i == 0) ? 0 : 1; |
| 1314 | dinfo->cur_comp_info[i] = compptr; |
| 1315 | } |
| 1316 | dinfo->data_precision = 8; |
| 1317 | for (i = 0; i < 2; i++) { |
| 1318 | if (dinfo->quant_tbl_ptrs[i] == NULL) |
| 1319 | dinfo->quant_tbl_ptrs[i] = jpeg_alloc_quant_table((j_common_ptr)dinfo); |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | int my_read_markers(j_decompress_ptr dinfo) |
| 1327 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1328 | return JPEG_REACHED_SOS; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | void my_reset_marker_reader(j_decompress_ptr dinfo) |
| 1332 | { |
| 1333 | } |
| 1334 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1335 | DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle, |
| 1336 | const unsigned char **srcPlanes, |
| 1337 | const int *strides, int subsamp, |
| 1338 | unsigned char *dstBuf, int width, int pitch, |
| 1339 | int height, int pixelFormat, int flags) |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1340 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1341 | JSAMPROW *row_pointer = NULL; |
| 1342 | JSAMPLE *_tmpbuf[MAX_COMPONENTS]; |
| 1343 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *inbuf[MAX_COMPONENTS]; |
| 1344 | int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS]; |
| 1345 | JSAMPLE *ptr; |
| 1346 | jpeg_component_info *compptr; |
| 1347 | int (*old_read_markers) (j_decompress_ptr); |
| 1348 | void (*old_reset_marker_reader) (j_decompress_ptr); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1349 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1350 | getdinstance(handle); |
| 1351 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1352 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1353 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 1354 | tmpbuf[i] = NULL; _tmpbuf[i] = NULL; inbuf[i] = NULL; |
| 1355 | } |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1356 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1357 | if ((this->init & DECOMPRESS) == 0) |
| 1358 | _throw("tjDecodeYUVPlanes(): Instance has not been initialized for decompression"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1359 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1360 | if (!srcPlanes || !srcPlanes[0] || subsamp < 0 || subsamp >= NUMSUBOPT || |
| 1361 | dstBuf == NULL || width <= 0 || pitch < 0 || height <= 0 || |
| 1362 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
| 1363 | _throw("tjDecodeYUVPlanes(): Invalid argument"); |
| 1364 | if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2])) |
| 1365 | _throw("tjDecodeYUVPlanes(): Invalid argument"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1366 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1367 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1368 | /* If we get here, the JPEG code has signaled an error. */ |
| 1369 | retval = -1; goto bailout; |
| 1370 | } |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1371 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1372 | if (pixelFormat == TJPF_CMYK) |
| 1373 | _throw("tjDecodeYUVPlanes(): Cannot decode YUV images into CMYK pixels."); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1374 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1375 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
| 1376 | dinfo->image_width = width; |
| 1377 | dinfo->image_height = height; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1378 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1379 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1380 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1381 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1382 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1383 | if (setDecodeDefaults(dinfo, pixelFormat, subsamp, flags) == -1) { |
| 1384 | retval = -1; goto bailout; |
| 1385 | } |
| 1386 | old_read_markers = dinfo->marker->read_markers; |
| 1387 | dinfo->marker->read_markers = my_read_markers; |
| 1388 | old_reset_marker_reader = dinfo->marker->reset_marker_reader; |
| 1389 | dinfo->marker->reset_marker_reader = my_reset_marker_reader; |
| 1390 | jpeg_read_header(dinfo, TRUE); |
| 1391 | dinfo->marker->read_markers = old_read_markers; |
| 1392 | dinfo->marker->reset_marker_reader = old_reset_marker_reader; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1393 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1394 | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
| 1395 | if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST; |
| 1396 | dinfo->do_fancy_upsampling = FALSE; |
| 1397 | dinfo->Se = DCTSIZE2 - 1; |
| 1398 | jinit_master_decompress(dinfo); |
| 1399 | (*dinfo->upsample->start_pass) (dinfo); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1400 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1401 | pw0 = PAD(width, dinfo->max_h_samp_factor); |
| 1402 | ph0 = PAD(height, dinfo->max_v_samp_factor); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1403 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1404 | if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat]; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1405 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1406 | if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL) |
| 1407 | _throw("tjDecodeYUVPlanes(): Memory allocation failure"); |
| 1408 | for (i = 0; i < height; i++) { |
| 1409 | if (flags & TJFLAG_BOTTOMUP) |
| 1410 | row_pointer[i] = &dstBuf[(height - i - 1) * pitch]; |
| 1411 | else |
| 1412 | row_pointer[i] = &dstBuf[i * pitch]; |
| 1413 | } |
| 1414 | if (height < ph0) |
| 1415 | for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1]; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1416 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1417 | for (i = 0; i < dinfo->num_components; i++) { |
| 1418 | compptr = &dinfo->comp_info[i]; |
| 1419 | _tmpbuf[i] = |
| 1420 | (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) * |
| 1421 | compptr->v_samp_factor + 32); |
| 1422 | if (!_tmpbuf[i]) |
| 1423 | _throw("tjDecodeYUVPlanes(): Memory allocation failure"); |
| 1424 | tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor); |
| 1425 | if (!tmpbuf[i]) |
| 1426 | _throw("tjDecodeYUVPlanes(): Memory allocation failure"); |
| 1427 | for (row = 0; row < compptr->v_samp_factor; row++) { |
| 1428 | unsigned char *_tmpbuf_aligned = |
| 1429 | (unsigned char *)PAD((size_t)_tmpbuf[i], 32); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1430 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1431 | tmpbuf[i][row] = |
| 1432 | &_tmpbuf_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row]; |
| 1433 | } |
| 1434 | pw[i] = pw0 * compptr->h_samp_factor / dinfo->max_h_samp_factor; |
| 1435 | ph[i] = ph0 * compptr->v_samp_factor / dinfo->max_v_samp_factor; |
| 1436 | inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]); |
| 1437 | if (!inbuf[i]) |
| 1438 | _throw("tjDecodeYUVPlanes(): Memory allocation failure"); |
| 1439 | ptr = (JSAMPLE *)srcPlanes[i]; |
| 1440 | for (row = 0; row < ph[i]; row++) { |
| 1441 | inbuf[i][row] = ptr; |
| 1442 | ptr += (strides && strides[i] != 0) ? strides[i] : pw[i]; |
| 1443 | } |
| 1444 | } |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 1445 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1446 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1447 | /* If we get here, the JPEG code has signaled an error. */ |
| 1448 | retval = -1; goto bailout; |
| 1449 | } |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1450 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1451 | for (row = 0; row < ph0; row += dinfo->max_v_samp_factor) { |
| 1452 | JDIMENSION inrow = 0, outrow = 0; |
| 1453 | |
| 1454 | for (i = 0, compptr = dinfo->comp_info; i < dinfo->num_components; |
| 1455 | i++, compptr++) |
| 1456 | jcopy_sample_rows(inbuf[i], |
| 1457 | row * compptr->v_samp_factor / dinfo->max_v_samp_factor, tmpbuf[i], 0, |
| 1458 | compptr->v_samp_factor, pw[i]); |
| 1459 | (dinfo->upsample->upsample) (dinfo, tmpbuf, &inrow, |
| 1460 | dinfo->max_v_samp_factor, &row_pointer[row], |
| 1461 | &outrow, dinfo->max_v_samp_factor); |
| 1462 | } |
| 1463 | jpeg_abort_decompress(dinfo); |
| 1464 | |
| 1465 | bailout: |
| 1466 | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
| 1467 | if (row_pointer) free(row_pointer); |
| 1468 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 1469 | if (tmpbuf[i] != NULL) free(tmpbuf[i]); |
| 1470 | if (_tmpbuf[i] != NULL) free(_tmpbuf[i]); |
| 1471 | if (inbuf[i] != NULL) free(inbuf[i]); |
| 1472 | } |
| 1473 | if (this->jerr.warning) retval = -1; |
| 1474 | this->jerr.stopOnWarning = FALSE; |
| 1475 | return retval; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1478 | DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf, |
| 1479 | int pad, int subsamp, unsigned char *dstBuf, |
| 1480 | int width, int pitch, int height, int pixelFormat, |
| 1481 | int flags) |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1482 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1483 | const unsigned char *srcPlanes[3]; |
| 1484 | int pw0, ph0, strides[3], retval = -1; |
| 1485 | tjinstance *this = (tjinstance *)handle; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 1486 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1487 | if (!this) _throwg("tjDecodeYUV(): Invalid handle"); |
| 1488 | this->isInstanceError = FALSE; |
DRC | b9ab64d | 2017-05-11 21:02:29 -0500 | [diff] [blame] | 1489 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1490 | if (srcBuf == NULL || pad < 0 || !isPow2(pad) || subsamp < 0 || |
| 1491 | subsamp >= NUMSUBOPT || width <= 0 || height <= 0) |
| 1492 | _throw("tjDecodeYUV(): Invalid argument"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1493 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1494 | pw0 = tjPlaneWidth(0, width, subsamp); |
| 1495 | ph0 = tjPlaneHeight(0, height, subsamp); |
| 1496 | srcPlanes[0] = srcBuf; |
| 1497 | strides[0] = PAD(pw0, pad); |
| 1498 | if (subsamp == TJSAMP_GRAY) { |
| 1499 | strides[1] = strides[2] = 0; |
| 1500 | srcPlanes[1] = srcPlanes[2] = NULL; |
| 1501 | } else { |
| 1502 | int pw1 = tjPlaneWidth(1, width, subsamp); |
| 1503 | int ph1 = tjPlaneHeight(1, height, subsamp); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1504 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1505 | strides[1] = strides[2] = PAD(pw1, pad); |
| 1506 | srcPlanes[1] = srcPlanes[0] + strides[0] * ph0; |
| 1507 | srcPlanes[2] = srcPlanes[1] + strides[1] * ph1; |
| 1508 | } |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1509 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1510 | return tjDecodeYUVPlanes(handle, srcPlanes, strides, subsamp, dstBuf, width, |
| 1511 | pitch, height, pixelFormat, flags); |
| 1512 | |
| 1513 | bailout: |
| 1514 | return retval; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1517 | DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle, |
| 1518 | const unsigned char *jpegBuf, |
| 1519 | unsigned long jpegSize, |
| 1520 | unsigned char **dstPlanes, int width, |
| 1521 | int *strides, int height, int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1522 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1523 | int i, sfi, row, retval = 0; |
| 1524 | int jpegwidth, jpegheight, jpegSubsamp, scaledw, scaledh; |
| 1525 | int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS], |
| 1526 | tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS]; |
| 1527 | JSAMPLE *_tmpbuf = NULL, *ptr; |
| 1528 | JSAMPROW *outbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS]; |
| 1529 | int dctsize; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1530 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1531 | getdinstance(handle); |
| 1532 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
DRC | b51ee89 | 2013-10-31 05:00:19 +0000 | [diff] [blame] | 1533 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1534 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 1535 | tmpbuf[i] = NULL; outbuf[i] = NULL; |
| 1536 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1537 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1538 | if ((this->init & DECOMPRESS) == 0) |
| 1539 | _throw("tjDecompressToYUVPlanes(): Instance has not been initialized for decompression"); |
DRC | e2f8e69 | 2013-10-30 22:21:06 +0000 | [diff] [blame] | 1540 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1541 | if (jpegBuf == NULL || jpegSize <= 0 || !dstPlanes || !dstPlanes[0] || |
| 1542 | width < 0 || height < 0) |
| 1543 | _throw("tjDecompressToYUVPlanes(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1544 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1545 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1546 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1547 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 1548 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1549 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1550 | /* If we get here, the JPEG code has signaled an error. */ |
| 1551 | retval = -1; goto bailout; |
| 1552 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1553 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1554 | if (!this->headerRead) { |
| 1555 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1556 | jpeg_read_header(dinfo, TRUE); |
| 1557 | } |
| 1558 | this->headerRead = 0; |
| 1559 | jpegSubsamp = getSubsamp(dinfo); |
| 1560 | if (jpegSubsamp < 0) |
| 1561 | _throw("tjDecompressToYUVPlanes(): Could not determine subsampling type for JPEG image"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1562 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1563 | if (jpegSubsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2])) |
| 1564 | _throw("tjDecompressToYUVPlanes(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1565 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1566 | jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height; |
| 1567 | if (width == 0) width = jpegwidth; |
| 1568 | if (height == 0) height = jpegheight; |
| 1569 | for (i = 0; i < NUMSF; i++) { |
| 1570 | scaledw = TJSCALED(jpegwidth, sf[i]); |
| 1571 | scaledh = TJSCALED(jpegheight, sf[i]); |
| 1572 | if (scaledw <= width && scaledh <= height) |
| 1573 | break; |
| 1574 | } |
| 1575 | if (i >= NUMSF) |
| 1576 | _throw("tjDecompressToYUVPlanes(): Could not scale down to desired image dimensions"); |
| 1577 | if (dinfo->num_components > 3) |
| 1578 | _throw("tjDecompressToYUVPlanes(): JPEG image must have 3 or fewer components"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 1579 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1580 | width = scaledw; height = scaledh; |
| 1581 | dinfo->scale_num = sf[i].num; |
| 1582 | dinfo->scale_denom = sf[i].denom; |
| 1583 | sfi = i; |
| 1584 | jpeg_calc_output_dimensions(dinfo); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1585 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1586 | dctsize = DCTSIZE * sf[sfi].num / sf[sfi].denom; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1587 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1588 | for (i = 0; i < dinfo->num_components; i++) { |
| 1589 | jpeg_component_info *compptr = &dinfo->comp_info[i]; |
| 1590 | int ih; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1591 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1592 | iw[i] = compptr->width_in_blocks * dctsize; |
| 1593 | ih = compptr->height_in_blocks * dctsize; |
| 1594 | pw[i] = PAD(dinfo->output_width, dinfo->max_h_samp_factor) * |
| 1595 | compptr->h_samp_factor / dinfo->max_h_samp_factor; |
| 1596 | ph[i] = PAD(dinfo->output_height, dinfo->max_v_samp_factor) * |
| 1597 | compptr->v_samp_factor / dinfo->max_v_samp_factor; |
| 1598 | if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1; |
| 1599 | th[i] = compptr->v_samp_factor * dctsize; |
| 1600 | tmpbufsize += iw[i] * th[i]; |
| 1601 | if ((outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL) |
| 1602 | _throw("tjDecompressToYUVPlanes(): Memory allocation failure"); |
| 1603 | ptr = dstPlanes[i]; |
| 1604 | for (row = 0; row < ph[i]; row++) { |
| 1605 | outbuf[i][row] = ptr; |
| 1606 | ptr += (strides && strides[i] != 0) ? strides[i] : pw[i]; |
| 1607 | } |
| 1608 | } |
| 1609 | if (usetmpbuf) { |
| 1610 | if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL) |
| 1611 | _throw("tjDecompressToYUVPlanes(): Memory allocation failure"); |
| 1612 | ptr = _tmpbuf; |
| 1613 | for (i = 0; i < dinfo->num_components; i++) { |
| 1614 | if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL) |
| 1615 | _throw("tjDecompressToYUVPlanes(): Memory allocation failure"); |
| 1616 | for (row = 0; row < th[i]; row++) { |
| 1617 | tmpbuf[i][row] = ptr; |
| 1618 | ptr += iw[i]; |
| 1619 | } |
| 1620 | } |
| 1621 | } |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 1622 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1623 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1624 | /* If we get here, the JPEG code has signaled an error. */ |
| 1625 | retval = -1; goto bailout; |
| 1626 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1627 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1628 | if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE; |
| 1629 | if (flags & TJFLAG_FASTDCT) dinfo->dct_method = JDCT_FASTEST; |
| 1630 | dinfo->raw_data_out = TRUE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1631 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1632 | jpeg_start_decompress(dinfo); |
| 1633 | for (row = 0; row < (int)dinfo->output_height; |
| 1634 | row += dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size) { |
| 1635 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
| 1636 | int crow[MAX_COMPONENTS]; |
| 1637 | |
| 1638 | for (i = 0; i < dinfo->num_components; i++) { |
| 1639 | jpeg_component_info *compptr = &dinfo->comp_info[i]; |
| 1640 | |
| 1641 | if (jpegSubsamp == TJ_420) { |
| 1642 | /* When 4:2:0 subsampling is used with IDCT scaling, libjpeg will try |
| 1643 | to be clever and use the IDCT to perform upsampling on the U and V |
| 1644 | planes. For instance, if the output image is to be scaled by 1/2 |
| 1645 | relative to the JPEG image, then the scaling factor and upsampling |
| 1646 | effectively cancel each other, so a normal 8x8 IDCT can be used. |
| 1647 | However, this is not desirable when using the decompress-to-YUV |
| 1648 | functionality in TurboJPEG, since we want to output the U and V |
| 1649 | planes in their subsampled form. Thus, we have to override some |
| 1650 | internal libjpeg parameters to force it to use the "scaled" IDCT |
| 1651 | functions on the U and V planes. */ |
| 1652 | compptr->_DCT_scaled_size = dctsize; |
| 1653 | compptr->MCU_sample_width = tjMCUWidth[jpegSubsamp] * |
| 1654 | sf[sfi].num / sf[sfi].denom * |
| 1655 | compptr->v_samp_factor / dinfo->max_v_samp_factor; |
| 1656 | dinfo->idct->inverse_DCT[i] = dinfo->idct->inverse_DCT[0]; |
| 1657 | } |
| 1658 | crow[i] = row * compptr->v_samp_factor / dinfo->max_v_samp_factor; |
| 1659 | if (usetmpbuf) yuvptr[i] = tmpbuf[i]; |
| 1660 | else yuvptr[i] = &outbuf[i][crow[i]]; |
| 1661 | } |
| 1662 | jpeg_read_raw_data(dinfo, yuvptr, |
| 1663 | dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size); |
| 1664 | if (usetmpbuf) { |
| 1665 | int j; |
| 1666 | |
| 1667 | for (i = 0; i < dinfo->num_components; i++) { |
| 1668 | for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) { |
| 1669 | memcpy(outbuf[i][crow[i] + j], tmpbuf[i][j], pw[i]); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | jpeg_finish_decompress(dinfo); |
| 1675 | |
| 1676 | bailout: |
| 1677 | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
| 1678 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 1679 | if (tmpbuf[i]) free(tmpbuf[i]); |
| 1680 | if (outbuf[i]) free(outbuf[i]); |
| 1681 | } |
| 1682 | if (_tmpbuf) free(_tmpbuf); |
| 1683 | if (this->jerr.warning) retval = -1; |
| 1684 | this->jerr.stopOnWarning = FALSE; |
| 1685 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1688 | DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf, |
| 1689 | unsigned long jpegSize, unsigned char *dstBuf, |
| 1690 | int width, int pad, int height, int flags) |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1691 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1692 | unsigned char *dstPlanes[3]; |
| 1693 | int pw0, ph0, strides[3], retval = -1, jpegSubsamp = -1; |
| 1694 | int i, jpegwidth, jpegheight, scaledw, scaledh; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1695 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1696 | getdinstance(handle); |
| 1697 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1698 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1699 | if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 || |
| 1700 | pad < 1 || !isPow2(pad) || height < 0) |
| 1701 | _throw("tjDecompressToYUV2(): Invalid argument"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1702 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1703 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1704 | /* If we get here, the JPEG code has signaled an error. */ |
| 1705 | return -1; |
| 1706 | } |
DRC | dec7995 | 2016-04-20 11:27:42 -0500 | [diff] [blame] | 1707 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1708 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1709 | jpeg_read_header(dinfo, TRUE); |
| 1710 | jpegSubsamp = getSubsamp(dinfo); |
| 1711 | if (jpegSubsamp < 0) |
| 1712 | _throw("tjDecompressToYUV2(): Could not determine subsampling type for JPEG image"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1713 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1714 | jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height; |
| 1715 | if (width == 0) width = jpegwidth; |
| 1716 | if (height == 0) height = jpegheight; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1717 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1718 | for (i = 0; i < NUMSF; i++) { |
| 1719 | scaledw = TJSCALED(jpegwidth, sf[i]); |
| 1720 | scaledh = TJSCALED(jpegheight, sf[i]); |
| 1721 | if (scaledw <= width && scaledh <= height) |
| 1722 | break; |
| 1723 | } |
| 1724 | if (i >= NUMSF) |
| 1725 | _throw("tjDecompressToYUV2(): Could not scale down to desired image dimensions"); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1726 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1727 | pw0 = tjPlaneWidth(0, width, jpegSubsamp); |
| 1728 | ph0 = tjPlaneHeight(0, height, jpegSubsamp); |
| 1729 | dstPlanes[0] = dstBuf; |
| 1730 | strides[0] = PAD(pw0, pad); |
| 1731 | if (jpegSubsamp == TJSAMP_GRAY) { |
| 1732 | strides[1] = strides[2] = 0; |
| 1733 | dstPlanes[1] = dstPlanes[2] = NULL; |
| 1734 | } else { |
| 1735 | int pw1 = tjPlaneWidth(1, width, jpegSubsamp); |
| 1736 | int ph1 = tjPlaneHeight(1, height, jpegSubsamp); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1737 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1738 | strides[1] = strides[2] = PAD(pw1, pad); |
| 1739 | dstPlanes[1] = dstPlanes[0] + strides[0] * ph0; |
| 1740 | dstPlanes[2] = dstPlanes[1] + strides[1] * ph1; |
| 1741 | } |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1742 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1743 | this->headerRead = 1; |
| 1744 | return tjDecompressToYUVPlanes(handle, jpegBuf, jpegSize, dstPlanes, width, |
| 1745 | strides, height, flags); |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1746 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1747 | bailout: |
| 1748 | this->jerr.stopOnWarning = FALSE; |
| 1749 | return retval; |
DRC | aecea38 | 2014-08-11 18:05:41 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1752 | DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf, |
| 1753 | unsigned long jpegSize, unsigned char *dstBuf, |
| 1754 | int flags) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1755 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1756 | return tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, 0, 4, 0, flags); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1759 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1760 | /* Transformer */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1761 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1762 | DLLEXPORT tjhandle tjInitTransform(void) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1763 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1764 | tjinstance *this = NULL; |
| 1765 | tjhandle handle = NULL; |
| 1766 | |
| 1767 | if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) { |
| 1768 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 1769 | "tjInitTransform(): Memory allocation failure"); |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | MEMZERO(this, sizeof(tjinstance)); |
| 1773 | snprintf(this->errStr, JMSG_LENGTH_MAX, "No error"); |
| 1774 | handle = _tjInitCompress(this); |
| 1775 | if (!handle) return NULL; |
| 1776 | handle = _tjInitDecompress(this); |
| 1777 | return handle; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1781 | DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf, |
| 1782 | unsigned long jpegSize, int n, |
| 1783 | unsigned char **dstBufs, unsigned long *dstSizes, |
| 1784 | tjtransform *t, int flags) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1785 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1786 | jpeg_transform_info *xinfo = NULL; |
| 1787 | jvirt_barray_ptr *srccoefs, *dstcoefs; |
| 1788 | int retval = 0, i, jpegSubsamp, saveMarkers = 0; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1789 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1790 | getinstance(handle); |
| 1791 | this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE; |
| 1792 | if ((this->init & COMPRESS) == 0 || (this->init & DECOMPRESS) == 0) |
| 1793 | _throw("tjTransform(): Instance has not been initialized for transformation"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1794 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1795 | if (jpegBuf == NULL || jpegSize <= 0 || n < 1 || dstBufs == NULL || |
| 1796 | dstSizes == NULL || t == NULL || flags < 0) |
| 1797 | _throw("tjTransform(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1798 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1799 | if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1800 | else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1801 | else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1802 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1803 | if ((xinfo = |
| 1804 | (jpeg_transform_info *)malloc(sizeof(jpeg_transform_info) * n)) == NULL) |
| 1805 | _throw("tjTransform(): Memory allocation failure"); |
| 1806 | MEMZERO(xinfo, sizeof(jpeg_transform_info) * n); |
DRC | d4c41fe | 2017-03-18 12:56:36 -0500 | [diff] [blame] | 1807 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1808 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1809 | /* If we get here, the JPEG code has signaled an error. */ |
| 1810 | retval = -1; goto bailout; |
| 1811 | } |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1812 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1813 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1814 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1815 | for (i = 0; i < n; i++) { |
| 1816 | xinfo[i].transform = xformtypes[t[i].op]; |
| 1817 | xinfo[i].perfect = (t[i].options & TJXOPT_PERFECT) ? 1 : 0; |
| 1818 | xinfo[i].trim = (t[i].options & TJXOPT_TRIM) ? 1 : 0; |
| 1819 | xinfo[i].force_grayscale = (t[i].options & TJXOPT_GRAY) ? 1 : 0; |
| 1820 | xinfo[i].crop = (t[i].options & TJXOPT_CROP) ? 1 : 0; |
| 1821 | if (n != 1 && t[i].op == TJXOP_HFLIP) xinfo[i].slow_hflip = 1; |
| 1822 | else xinfo[i].slow_hflip = 0; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1823 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1824 | if (xinfo[i].crop) { |
| 1825 | xinfo[i].crop_xoffset = t[i].r.x; xinfo[i].crop_xoffset_set = JCROP_POS; |
| 1826 | xinfo[i].crop_yoffset = t[i].r.y; xinfo[i].crop_yoffset_set = JCROP_POS; |
| 1827 | if (t[i].r.w != 0) { |
| 1828 | xinfo[i].crop_width = t[i].r.w; xinfo[i].crop_width_set = JCROP_POS; |
| 1829 | } else |
| 1830 | xinfo[i].crop_width = JCROP_UNSET; |
| 1831 | if (t[i].r.h != 0) { |
| 1832 | xinfo[i].crop_height = t[i].r.h; xinfo[i].crop_height_set = JCROP_POS; |
| 1833 | } else |
| 1834 | xinfo[i].crop_height = JCROP_UNSET; |
| 1835 | } |
| 1836 | if (!(t[i].options & TJXOPT_COPYNONE)) saveMarkers = 1; |
| 1837 | } |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1838 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1839 | jcopy_markers_setup(dinfo, saveMarkers ? JCOPYOPT_ALL : JCOPYOPT_NONE); |
| 1840 | jpeg_read_header(dinfo, TRUE); |
| 1841 | jpegSubsamp = getSubsamp(dinfo); |
| 1842 | if (jpegSubsamp < 0) |
| 1843 | _throw("tjTransform(): Could not determine subsampling type for JPEG image"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1844 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1845 | for (i = 0; i < n; i++) { |
| 1846 | if (!jtransform_request_workspace(dinfo, &xinfo[i])) |
| 1847 | _throw("tjTransform(): Transform is not perfect"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1848 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1849 | if (xinfo[i].crop) { |
| 1850 | if ((t[i].r.x % xinfo[i].iMCU_sample_width) != 0 || |
| 1851 | (t[i].r.y % xinfo[i].iMCU_sample_height) != 0) { |
| 1852 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 1853 | "To crop this JPEG image, x must be a multiple of %d\n" |
| 1854 | "and y must be a multiple of %d.\n", |
| 1855 | xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height); |
| 1856 | retval = -1; goto bailout; |
| 1857 | } |
| 1858 | } |
| 1859 | } |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1860 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1861 | srccoefs = jpeg_read_coefficients(dinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1862 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1863 | for (i = 0; i < n; i++) { |
| 1864 | int w, h, alloc = 1; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1865 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1866 | if (!xinfo[i].crop) { |
| 1867 | w = dinfo->image_width; h = dinfo->image_height; |
| 1868 | } else { |
| 1869 | w = xinfo[i].crop_width; h = xinfo[i].crop_height; |
| 1870 | } |
| 1871 | if (flags & TJFLAG_NOREALLOC) { |
| 1872 | alloc = 0; dstSizes[i] = tjBufSize(w, h, jpegSubsamp); |
| 1873 | } |
| 1874 | if (!(t[i].options & TJXOPT_NOOUTPUT)) |
| 1875 | jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc); |
| 1876 | jpeg_copy_critical_parameters(dinfo, cinfo); |
| 1877 | dstcoefs = jtransform_adjust_parameters(dinfo, cinfo, srccoefs, &xinfo[i]); |
| 1878 | if (flags & TJFLAG_PROGRESSIVE || t[i].options & TJXOPT_PROGRESSIVE) |
| 1879 | jpeg_simple_progression(cinfo); |
| 1880 | if (!(t[i].options & TJXOPT_NOOUTPUT)) { |
| 1881 | jpeg_write_coefficients(cinfo, dstcoefs); |
| 1882 | jcopy_markers_execute(dinfo, cinfo, t[i].options & TJXOPT_COPYNONE ? |
| 1883 | JCOPYOPT_NONE : JCOPYOPT_ALL); |
| 1884 | } else |
| 1885 | jinit_c_master_control(cinfo, TRUE); |
| 1886 | jtransform_execute_transformation(dinfo, cinfo, srccoefs, &xinfo[i]); |
| 1887 | if (t[i].customFilter) { |
| 1888 | int ci, y; |
| 1889 | JDIMENSION by; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1890 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1891 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 1892 | jpeg_component_info *compptr = &cinfo->comp_info[ci]; |
| 1893 | tjregion arrayRegion = { |
| 1894 | 0, 0, compptr->width_in_blocks * DCTSIZE, DCTSIZE |
| 1895 | }; |
| 1896 | tjregion planeRegion = { |
| 1897 | 0, 0, compptr->width_in_blocks * DCTSIZE, |
| 1898 | compptr->height_in_blocks * DCTSIZE |
| 1899 | }; |
| 1900 | |
| 1901 | for (by = 0; by < compptr->height_in_blocks; |
| 1902 | by += compptr->v_samp_factor) { |
| 1903 | JBLOCKARRAY barray = (dinfo->mem->access_virt_barray) |
| 1904 | ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor, |
| 1905 | TRUE); |
| 1906 | |
| 1907 | for (y = 0; y < compptr->v_samp_factor; y++) { |
| 1908 | if (t[i].customFilter(barray[y][0], arrayRegion, planeRegion, ci, |
| 1909 | i, &t[i]) == -1) |
| 1910 | _throw("tjTransform(): Error in custom filter"); |
| 1911 | arrayRegion.y += DCTSIZE; |
| 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | if (!(t[i].options & TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo); |
| 1917 | } |
| 1918 | |
| 1919 | jpeg_finish_decompress(dinfo); |
| 1920 | |
| 1921 | bailout: |
| 1922 | if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo); |
| 1923 | if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo); |
| 1924 | if (xinfo) free(xinfo); |
| 1925 | if (this->jerr.warning) retval = -1; |
| 1926 | this->jerr.stopOnWarning = FALSE; |
| 1927 | return retval; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1928 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1929 | |
| 1930 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1931 | DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width, |
| 1932 | int align, int *height, int *pixelFormat, |
| 1933 | int flags) |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1934 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1935 | int retval = 0, tempc, pitch; |
| 1936 | tjhandle handle = NULL; |
| 1937 | tjinstance *this; |
| 1938 | j_compress_ptr cinfo = NULL; |
| 1939 | cjpeg_source_ptr src; |
| 1940 | unsigned char *dstBuf = NULL; |
| 1941 | FILE *file = NULL; |
| 1942 | boolean invert; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1943 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1944 | if (!filename || !width || align < 1 || !height || !pixelFormat || |
| 1945 | *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF) |
| 1946 | _throwg("tjLoadImage(): Invalid argument"); |
| 1947 | if ((align & (align - 1)) != 0) |
| 1948 | _throwg("tjLoadImage(): Alignment must be a power of 2"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1949 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1950 | if ((handle = tjInitCompress()) == NULL) return NULL; |
| 1951 | this = (tjinstance *)handle; |
| 1952 | cinfo = &this->cinfo; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1953 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1954 | if ((file = fopen(filename, "rb")) == NULL) |
| 1955 | _throwunix("tjLoadImage(): Cannot open input file"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1956 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1957 | if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) |
| 1958 | _throwunix("tjLoadImage(): Could not read input file") |
| 1959 | else if (tempc == EOF) |
| 1960 | _throwg("tjLoadImage(): Input file contains no data"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1961 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1962 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1963 | /* If we get here, the JPEG code has signaled an error. */ |
| 1964 | retval = -1; goto bailout; |
| 1965 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1966 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1967 | if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN; |
| 1968 | else cinfo->in_color_space = pf2cs[*pixelFormat]; |
| 1969 | if (tempc == 'B') { |
| 1970 | if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL) |
| 1971 | _throwg("tjLoadImage(): Could not initialize bitmap loader"); |
| 1972 | invert = (flags & TJFLAG_BOTTOMUP) == 0; |
| 1973 | } else if (tempc == 'P') { |
| 1974 | if ((src = jinit_read_ppm(cinfo)) == NULL) |
| 1975 | _throwg("tjLoadImage(): Could not initialize bitmap loader"); |
| 1976 | invert = (flags & TJFLAG_BOTTOMUP) != 0; |
| 1977 | } else |
| 1978 | _throwg("tjLoadImage(): Unsupported file type"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1979 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1980 | src->input_file = file; |
| 1981 | (*src->start_input) (cinfo, src); |
| 1982 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1983 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1984 | *width = cinfo->image_width; *height = cinfo->image_height; |
| 1985 | *pixelFormat = cs2pf[cinfo->in_color_space]; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1986 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1987 | pitch = PAD((*width) * tjPixelSize[*pixelFormat], align); |
| 1988 | if ((dstBuf = (unsigned char *)malloc(pitch * (*height))) == NULL) |
| 1989 | _throwg("tjLoadImage(): Memory allocation failure"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1990 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1991 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 1992 | /* If we get here, the JPEG code has signaled an error. */ |
| 1993 | retval = -1; goto bailout; |
| 1994 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1995 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1996 | while (cinfo->next_scanline < cinfo->image_height) { |
| 1997 | int i, nlines = (*src->get_pixel_rows) (cinfo, src); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 1998 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 1999 | for (i = 0; i < nlines; i++) { |
| 2000 | unsigned char *dstptr; |
| 2001 | int row; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2002 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2003 | row = cinfo->next_scanline + i; |
| 2004 | if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch]; |
| 2005 | else dstptr = &dstBuf[row * pitch]; |
| 2006 | memcpy(dstptr, src->buffer[i], (*width) * tjPixelSize[*pixelFormat]); |
| 2007 | } |
| 2008 | cinfo->next_scanline += nlines; |
| 2009 | } |
| 2010 | |
| 2011 | (*src->finish_input) (cinfo, src); |
| 2012 | |
| 2013 | bailout: |
| 2014 | if (handle) tjDestroy(handle); |
| 2015 | if (file) fclose(file); |
| 2016 | if (retval < 0 && dstBuf) { free(dstBuf); dstBuf = NULL; } |
| 2017 | return dstBuf; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2021 | DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer, |
| 2022 | int width, int pitch, int height, int pixelFormat, |
| 2023 | int flags) |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2024 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2025 | int retval = 0; |
| 2026 | tjhandle handle = NULL; |
| 2027 | tjinstance *this; |
| 2028 | j_decompress_ptr dinfo = NULL; |
| 2029 | djpeg_dest_ptr dst; |
| 2030 | FILE *file = NULL; |
| 2031 | char *ptr = NULL; |
| 2032 | boolean invert; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2033 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2034 | if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 || |
| 2035 | pixelFormat < 0 || pixelFormat >= TJ_NUMPF) |
| 2036 | _throwg("tjSaveImage(): Invalid argument"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2037 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2038 | if ((handle = tjInitDecompress()) == NULL) |
| 2039 | return -1; |
| 2040 | this = (tjinstance *)handle; |
| 2041 | dinfo = &this->dinfo; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2042 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2043 | if ((file = fopen(filename, "wb")) == NULL) |
| 2044 | _throwunix("tjSaveImage(): Cannot open output file"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2045 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2046 | if (setjmp(this->jerr.setjmp_buffer)) { |
| 2047 | /* If we get here, the JPEG code has signaled an error. */ |
| 2048 | retval = -1; goto bailout; |
| 2049 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2050 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2051 | this->dinfo.out_color_space = pf2cs[pixelFormat]; |
| 2052 | dinfo->image_width = width; dinfo->image_height = height; |
| 2053 | dinfo->global_state = DSTATE_READY; |
| 2054 | dinfo->scale_num = dinfo->scale_denom = 1; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2055 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2056 | ptr = strrchr(filename, '.'); |
| 2057 | if (ptr && !strcasecmp(ptr, ".bmp")) { |
| 2058 | if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL) |
| 2059 | _throwg("tjSaveImage(): Could not initialize bitmap writer"); |
| 2060 | invert = (flags & TJFLAG_BOTTOMUP) == 0; |
| 2061 | } else { |
| 2062 | if ((dst = jinit_write_ppm(dinfo)) == NULL) |
| 2063 | _throwg("tjSaveImage(): Could not initialize PPM writer"); |
| 2064 | invert = (flags & TJFLAG_BOTTOMUP) != 0; |
| 2065 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2066 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2067 | dst->output_file = file; |
| 2068 | (*dst->start_output) (dinfo, dst); |
| 2069 | (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2070 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2071 | if (pitch == 0) pitch = width * tjPixelSize[pixelFormat]; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2072 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2073 | while (dinfo->output_scanline < dinfo->output_height) { |
| 2074 | unsigned char *rowptr; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2075 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2076 | if (invert) |
| 2077 | rowptr = &buffer[(height - dinfo->output_scanline - 1) * pitch]; |
| 2078 | else |
| 2079 | rowptr = &buffer[dinfo->output_scanline * pitch]; |
| 2080 | memcpy(dst->buffer[0], rowptr, width * tjPixelSize[pixelFormat]); |
| 2081 | (*dst->put_pixel_rows) (dinfo, dst, 1); |
| 2082 | dinfo->output_scanline++; |
| 2083 | } |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2084 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 2085 | (*dst->finish_output) (dinfo, dst); |
| 2086 | |
| 2087 | bailout: |
| 2088 | if (handle) tjDestroy(handle); |
| 2089 | if (file) fclose(file); |
| 2090 | return retval; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame] | 2091 | } |