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