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