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