DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2009-2011 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 | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 29 | /* TurboJPEG/OSS: this implements the TurboJPEG API using libjpeg-turbo */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 33 | #define JPEG_INTERNALS |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 34 | #include <jpeglib.h> |
| 35 | #include <jerror.h> |
| 36 | #include <setjmp.h> |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 37 | #include <jinclude.h> |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 38 | #include "./turbojpeg.h" |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 39 | #include "./rrutil.h" |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 40 | #include "transupp.h" |
DRC | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 41 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 42 | extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **, |
| 43 | unsigned long *, boolean); |
| 44 | extern void jpeg_mem_src_tj(j_decompress_ptr, unsigned char *, unsigned long); |
| 45 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 46 | #ifndef min |
| 47 | #define min(a,b) ((a)<(b)?(a):(b)) |
| 48 | #endif |
| 49 | |
| 50 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 51 | |
| 52 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 53 | /* Error handling (based on example in example.c) */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 54 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 55 | static char errStr[JMSG_LENGTH_MAX]="No error"; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 56 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 57 | struct my_error_mgr |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 58 | { |
| 59 | struct jpeg_error_mgr pub; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 60 | jmp_buf setjmp_buffer; |
| 61 | }; |
| 62 | typedef struct my_error_mgr *my_error_ptr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 63 | |
| 64 | static void my_error_exit(j_common_ptr cinfo) |
| 65 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 66 | my_error_ptr myerr=(my_error_ptr)cinfo->err; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 67 | (*cinfo->err->output_message)(cinfo); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 68 | longjmp(myerr->setjmp_buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 71 | /* Based on output_message() in jerror.c */ |
| 72 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 73 | static void my_output_message(j_common_ptr cinfo) |
| 74 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 75 | (*cinfo->err->format_message)(cinfo, errStr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 79 | /* Global structures, macros, etc. */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 80 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 81 | enum {COMPRESS=1, DECOMPRESS=2}; |
| 82 | |
| 83 | typedef struct _tjinstance |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 84 | { |
| 85 | struct jpeg_compress_struct cinfo; |
| 86 | struct jpeg_decompress_struct dinfo; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 87 | struct my_error_mgr jerr; |
| 88 | int init; |
| 89 | } tjinstance; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 90 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 91 | static const int pixelsize[TJ_NUMSAMP]={3, 3, 3, 1, 3}; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 92 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 93 | static const JXFORM_CODE xformtypes[TJ_NUMXOP]= |
| 94 | { |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 95 | JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE, |
| 96 | JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270 |
| 97 | }; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 98 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 99 | #define NUMSF 4 |
| 100 | static const tjscalingfactor sf[NUMSF]={ |
| 101 | {1, 1}, |
| 102 | {1, 2}, |
| 103 | {1, 4}, |
| 104 | {1, 8} |
| 105 | }; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 106 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 107 | #define _throw(c) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", c); \ |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 108 | retval=-1; goto bailout;} |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 109 | #define getinstance(handle) tjinstance *this=(tjinstance *)handle; \ |
| 110 | j_compress_ptr cinfo=NULL; j_decompress_ptr dinfo=NULL; \ |
| 111 | if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \ |
| 112 | return -1;} \ |
| 113 | cinfo=&this->cinfo; dinfo=&this->dinfo; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 114 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 115 | static int getPixelFormat(int pixelSize, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 116 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 117 | if(pixelSize==1) return TJPF_GRAY; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 118 | if(pixelSize==3) |
| 119 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 120 | if(flags&TJ_BGR) return TJPF_BGR; |
| 121 | else return TJPF_RGB; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 122 | } |
| 123 | if(pixelSize==4) |
| 124 | { |
| 125 | if(flags&TJ_ALPHAFIRST) |
| 126 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 127 | if(flags&TJ_BGR) return TJPF_XBGR; |
| 128 | else return TJPF_XRGB; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 132 | if(flags&TJ_BGR) return TJPF_BGRX; |
| 133 | else return TJPF_RGBX; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | return -1; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 137 | } |
| 138 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 139 | static void setCompDefaults(struct jpeg_compress_struct *cinfo, |
| 140 | int pixelFormat, int subsamp, int jpegQual) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 141 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 142 | switch(pixelFormat) |
| 143 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 144 | case TJPF_GRAY: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 145 | cinfo->in_color_space=JCS_GRAYSCALE; break; |
| 146 | #if JCS_EXTENSIONS==1 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 147 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 148 | cinfo->in_color_space=JCS_EXT_RGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 149 | case TJPF_BGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 150 | cinfo->in_color_space=JCS_EXT_BGR; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 151 | case TJPF_RGBX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 152 | cinfo->in_color_space=JCS_EXT_RGBX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 153 | case TJPF_BGRX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 154 | cinfo->in_color_space=JCS_EXT_BGRX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 155 | case TJPF_XRGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 156 | cinfo->in_color_space=JCS_EXT_XRGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 157 | case TJPF_XBGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 158 | cinfo->in_color_space=JCS_EXT_XBGR; break; |
| 159 | #else |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 160 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 161 | if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3) |
| 162 | { |
| 163 | cinfo->in_color_space=JCS_RGB; break; |
| 164 | } |
| 165 | default: |
| 166 | _throw("Unsupported pixel format"); |
| 167 | #endif |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 168 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 169 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 170 | cinfo->input_components=tjPixelSize[pixelFormat]; |
| 171 | jpeg_set_defaults(cinfo); |
| 172 | if(jpegQual>=0) |
| 173 | { |
| 174 | jpeg_set_quality(cinfo, jpegQual, TRUE); |
| 175 | if(jpegQual>=96) cinfo->dct_method=JDCT_ISLOW; |
| 176 | else cinfo->dct_method=JDCT_FASTEST; |
| 177 | } |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 178 | if(subsamp==TJSAMP_GRAY) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 179 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 180 | else |
| 181 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 182 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 183 | cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8; |
| 184 | cinfo->comp_info[1].h_samp_factor=1; |
| 185 | cinfo->comp_info[2].h_samp_factor=1; |
| 186 | cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8; |
| 187 | cinfo->comp_info[1].v_samp_factor=1; |
| 188 | cinfo->comp_info[2].v_samp_factor=1; |
| 189 | } |
| 190 | |
| 191 | static void setDecompDefaults(struct jpeg_decompress_struct *dinfo, |
| 192 | int pixelFormat) |
| 193 | { |
| 194 | switch(pixelFormat) |
| 195 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 196 | case TJPF_GRAY: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 197 | dinfo->out_color_space=JCS_GRAYSCALE; break; |
| 198 | #if JCS_EXTENSIONS==1 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 199 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 200 | dinfo->out_color_space=JCS_EXT_RGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 201 | case TJPF_BGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 202 | dinfo->out_color_space=JCS_EXT_BGR; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 203 | case TJPF_RGBX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 204 | dinfo->out_color_space=JCS_EXT_RGBX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 205 | case TJPF_BGRX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 206 | dinfo->out_color_space=JCS_EXT_BGRX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 207 | case TJPF_XRGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 208 | dinfo->out_color_space=JCS_EXT_XRGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 209 | case TJPF_XBGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 210 | dinfo->out_color_space=JCS_EXT_XBGR; break; |
| 211 | #else |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 212 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 213 | if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3) |
| 214 | { |
| 215 | dinfo->out_color_space=JCS_RGB; break; |
| 216 | } |
| 217 | default: |
| 218 | _throw("Unsupported pixel format"); |
| 219 | #endif |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* General API functions */ |
| 225 | |
| 226 | DLLEXPORT char* DLLCALL tjGetErrorStr(void) |
| 227 | { |
| 228 | return errStr; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | DLLEXPORT int DLLCALL tjDestroy(tjhandle handle) |
| 233 | { |
| 234 | getinstance(handle); |
| 235 | if(setjmp(this->jerr.setjmp_buffer)) return -1; |
| 236 | if(this->init&COMPRESS) jpeg_destroy_compress(cinfo); |
| 237 | if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo); |
| 238 | free(this); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* Compressor */ |
| 244 | |
| 245 | static tjhandle _tjInitCompress(tjinstance *this) |
| 246 | { |
| 247 | unsigned char buffer[1], *buf=buffer; unsigned long size=1; |
| 248 | |
| 249 | /* This is also straight out of example.c */ |
| 250 | this->cinfo.err=jpeg_std_error(&this->jerr.pub); |
| 251 | this->jerr.pub.error_exit=my_error_exit; |
| 252 | this->jerr.pub.output_message=my_output_message; |
| 253 | |
| 254 | if(setjmp(this->jerr.setjmp_buffer)) |
| 255 | { |
| 256 | /* If we get here, the JPEG code has signaled an error. */ |
| 257 | if(this) free(this); return NULL; |
| 258 | } |
| 259 | |
| 260 | jpeg_create_compress(&this->cinfo); |
| 261 | /* Make an initial call so it will create the destination manager */ |
| 262 | jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0); |
| 263 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 264 | this->init|=COMPRESS; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 265 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 266 | } |
| 267 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 268 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void) |
| 269 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 270 | tjinstance *this=NULL; |
| 271 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 272 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 273 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 274 | "tjInitCompress(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 277 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 278 | return _tjInitCompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 279 | } |
| 280 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 281 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 282 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height) |
| 283 | { |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 284 | unsigned long retval=0; |
| 285 | if(width<1 || height<1) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 286 | _throw("TJBUFSIZE(): Invalid argument"); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 287 | |
| 288 | // This allows for rare corner cases in which a JPEG image can actually be |
| 289 | // larger than the uncompressed input (we wouldn't mention it if it hadn't |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 290 | // happened before.) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 291 | retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048; |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 292 | |
| 293 | bailout: |
| 294 | return retval; |
| 295 | } |
| 296 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 297 | |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 298 | DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height, |
| 299 | int subsamp) |
| 300 | { |
| 301 | unsigned long retval=0; |
| 302 | int pw, ph, cw, ch; |
| 303 | if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 304 | _throw("TJBUFSIZEYUV(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 305 | pw=PAD(width, tjMCUWidth[subsamp]/8); |
| 306 | ph=PAD(height, tjMCUHeight[subsamp]/8); |
| 307 | cw=pw*8/tjMCUWidth[subsamp]; ch=ph*8/tjMCUHeight[subsamp]; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 308 | retval=PAD(pw, 4)*ph + (subsamp==TJSAMP_GRAY? 0:PAD(cw, 4)*ch*2); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 309 | |
| 310 | bailout: |
| 311 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 312 | } |
| 313 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 314 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 315 | DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, |
| 316 | int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf, |
| 317 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags) |
| 318 | { |
| 319 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
| 320 | |
| 321 | getinstance(handle) |
| 322 | if((this->init&COMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 323 | _throw("tjCompress2(): Instance has not been initialized for compression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 324 | |
| 325 | if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0 |
| 326 | || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL |
| 327 | || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 328 | _throw("tjCompress2(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 329 | |
| 330 | if(setjmp(this->jerr.setjmp_buffer)) |
| 331 | { |
| 332 | /* If we get here, the JPEG code has signaled an error. */ |
| 333 | retval=-1; |
| 334 | goto bailout; |
| 335 | } |
| 336 | |
| 337 | if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; |
| 338 | |
| 339 | cinfo->image_width=width; |
| 340 | cinfo->image_height=height; |
| 341 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 342 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 343 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 344 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 345 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 346 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, (flags&TJFLAG_NOREALLOC)==0); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 347 | setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual); |
| 348 | |
| 349 | jpeg_start_compress(cinfo, TRUE); |
| 350 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 351 | _throw("tjCompress2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 352 | for(i=0; i<height; i++) |
| 353 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 354 | if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 355 | else row_pointer[i]=&srcBuf[i*pitch]; |
| 356 | } |
| 357 | while(cinfo->next_scanline<cinfo->image_height) |
| 358 | { |
| 359 | jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
| 360 | cinfo->image_height-cinfo->next_scanline); |
| 361 | } |
| 362 | jpeg_finish_compress(cinfo); |
| 363 | |
| 364 | bailout: |
| 365 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
| 366 | if(row_pointer) free(row_pointer); |
| 367 | return retval; |
| 368 | } |
| 369 | |
| 370 | DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf, |
| 371 | int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf, |
| 372 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags) |
| 373 | { |
| 374 | int retval=0; unsigned long size; |
| 375 | if(flags&TJ_YUV) |
| 376 | { |
| 377 | size=TJBUFSIZEYUV(width, height, jpegSubsamp); |
| 378 | retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 379 | getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags); |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | size=TJBUFSIZE(width, height); |
| 384 | retval=tjCompress2(handle, srcBuf, width, pitch, height, |
| 385 | getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual, |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 386 | flags|TJFLAG_NOREALLOC); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 387 | } |
| 388 | *jpegSize=size; |
| 389 | return retval; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, |
| 394 | int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf, |
| 395 | int subsamp, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 396 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 397 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 398 | JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS]; |
| 399 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS]; |
| 400 | JSAMPROW *outbuf[MAX_COMPONENTS]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 401 | int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS]; |
| 402 | JSAMPLE *ptr=dstBuf; |
| 403 | unsigned long yuvsize=0; |
| 404 | jpeg_component_info *compptr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 405 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 406 | getinstance(handle); |
| 407 | if((this->init&COMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 408 | _throw("tjEncodeYUV2(): Instance has not been initialized for compression"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 409 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 410 | for(i=0; i<MAX_COMPONENTS; i++) |
| 411 | { |
| 412 | tmpbuf[i]=NULL; _tmpbuf[i]=NULL; |
| 413 | tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL; |
| 414 | } |
| 415 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 416 | if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0 |
| 417 | || pixelFormat>=TJ_NUMPF || dstBuf==NULL || subsamp<0 |
| 418 | || subsamp>=NUMSUBOPT) |
| 419 | _throw("tjEncodeYUV2(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 420 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 421 | if(setjmp(this->jerr.setjmp_buffer)) |
| 422 | { |
| 423 | /* If we get here, the JPEG code has signaled an error. */ |
| 424 | retval=-1; |
| 425 | goto bailout; |
| 426 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 427 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 428 | if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 429 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 430 | cinfo->image_width=width; |
| 431 | cinfo->image_height=height; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 432 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 433 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 434 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 435 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 436 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 437 | yuvsize=TJBUFSIZEYUV(width, height, subsamp); |
| 438 | jpeg_mem_dest_tj(cinfo, &dstBuf, &yuvsize, 0); |
| 439 | setCompDefaults(cinfo, pixelFormat, subsamp, -1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 440 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 441 | jpeg_start_compress(cinfo, TRUE); |
| 442 | pw=PAD(width, cinfo->max_h_samp_factor); |
| 443 | ph=PAD(height, cinfo->max_v_samp_factor); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 444 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 445 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 446 | _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 447 | for(i=0; i<height; i++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 448 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 449 | if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 450 | else row_pointer[i]=&srcBuf[i*pitch]; |
| 451 | } |
| 452 | if(height<ph) |
| 453 | for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1]; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 454 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 455 | for(i=0; i<cinfo->num_components; i++) |
| 456 | { |
| 457 | compptr=&cinfo->comp_info[i]; |
| 458 | _tmpbuf[i]=(JSAMPLE *)malloc( |
| 459 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
| 460 | /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 461 | if(!_tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 462 | tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 463 | if(!tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 464 | for(row=0; row<cinfo->max_v_samp_factor; row++) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 465 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 466 | unsigned char *_tmpbuf_aligned= |
| 467 | (unsigned char *)PAD((size_t)_tmpbuf[i], 16); |
| 468 | tmpbuf[i][row]=&_tmpbuf_aligned[ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 469 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 470 | /compptr->h_samp_factor, 16) * row]; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 471 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 472 | _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16) |
| 473 | * compptr->v_samp_factor + 16); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 474 | if(!_tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 475 | tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 476 | if(!tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 477 | for(row=0; row<compptr->v_samp_factor; row++) |
| 478 | { |
| 479 | unsigned char *_tmpbuf2_aligned= |
| 480 | (unsigned char *)PAD((size_t)_tmpbuf2[i], 16); |
| 481 | tmpbuf2[i][row]=&_tmpbuf2_aligned[ |
| 482 | PAD(compptr->width_in_blocks*DCTSIZE, 16) * row]; |
| 483 | } |
| 484 | cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor; |
| 485 | ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor; |
| 486 | outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 487 | if(!outbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 488 | for(row=0; row<ch[i]; row++) |
| 489 | { |
| 490 | outbuf[i][row]=ptr; |
| 491 | ptr+=PAD(cw[i], 4); |
| 492 | } |
| 493 | } |
| 494 | if(yuvsize!=(unsigned long)(ptr-dstBuf)) |
| 495 | _throw("tjEncodeYUV2(): Generated image is not the correct size"); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 496 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 497 | for(row=0; row<ph; row+=cinfo->max_v_samp_factor) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 498 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 499 | (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0, |
| 500 | cinfo->max_v_samp_factor); |
| 501 | (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0); |
| 502 | for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++) |
| 503 | jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i], |
| 504 | row*compptr->v_samp_factor/cinfo->max_v_samp_factor, |
| 505 | compptr->v_samp_factor, cw[i]); |
DRC | 6ee5459 | 2011-03-01 08:18:30 +0000 | [diff] [blame] | 506 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 507 | cinfo->next_scanline+=height; |
| 508 | jpeg_abort_compress(cinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 509 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 510 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 511 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 512 | if(row_pointer) free(row_pointer); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 513 | for(i=0; i<MAX_COMPONENTS; i++) |
| 514 | { |
| 515 | if(tmpbuf[i]!=NULL) free(tmpbuf[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 516 | if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 517 | if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 518 | if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 519 | if(outbuf[i]!=NULL) free(outbuf[i]); |
| 520 | } |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 521 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 522 | } |
| 523 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 524 | DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, |
| 525 | int width, int pitch, int height, int pixelSize, unsigned char *dstBuf, |
| 526 | int subsamp, int flags) |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 527 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 528 | return tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 529 | getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 533 | /* Decompressor */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 534 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 535 | static tjhandle _tjInitDecompress(tjinstance *this) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 536 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 537 | unsigned char buffer[1]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 538 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 539 | /* This is also straight out of example.c */ |
| 540 | this->dinfo.err=jpeg_std_error(&this->jerr.pub); |
| 541 | this->jerr.pub.error_exit=my_error_exit; |
| 542 | this->jerr.pub.output_message=my_output_message; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 543 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 544 | if(setjmp(this->jerr.setjmp_buffer)) |
| 545 | { |
| 546 | /* If we get here, the JPEG code has signaled an error. */ |
| 547 | if(this) free(this); return NULL; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 548 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 549 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 550 | jpeg_create_decompress(&this->dinfo); |
| 551 | /* Make an initial call so it will create the source manager */ |
| 552 | jpeg_mem_src_tj(&this->dinfo, buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 553 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 554 | this->init|=DECOMPRESS; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 555 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 556 | } |
| 557 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 558 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void) |
| 559 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 560 | tjinstance *this; |
| 561 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 562 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 563 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 564 | "tjInitDecompress(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 565 | return NULL; |
| 566 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 567 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 568 | return _tjInitDecompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 569 | } |
| 570 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 571 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 572 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle, |
| 573 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height, |
| 574 | int *jpegSubsamp) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 575 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 576 | int i, k, retval=0; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 577 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 578 | getinstance(handle); |
| 579 | if((this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 580 | _throw("tjDecompressHeader2(): Instance has not been initialized for decompression"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 581 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 582 | if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL |
| 583 | || jpegSubsamp==NULL) |
| 584 | _throw("tjDecompressHeader2(): Invalid argument"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 585 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 586 | if(setjmp(this->jerr.setjmp_buffer)) |
| 587 | { |
| 588 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 592 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 593 | jpeg_read_header(dinfo, TRUE); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 594 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 595 | *width=dinfo->image_width; |
| 596 | *height=dinfo->image_height; |
| 597 | *jpegSubsamp=-1; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 598 | for(i=0; i<NUMSUBOPT; i++) |
| 599 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 600 | if(dinfo->num_components==pixelsize[i]) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 601 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 602 | if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8 |
| 603 | && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 604 | { |
| 605 | int match=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 606 | for(k=1; k<dinfo->num_components; k++) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 607 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 608 | if(dinfo->comp_info[k].h_samp_factor==1 |
| 609 | && dinfo->comp_info[k].v_samp_factor==1) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 610 | match++; |
| 611 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 612 | if(match==dinfo->num_components-1) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 613 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 614 | *jpegSubsamp=i; break; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 620 | jpeg_abort_decompress(dinfo); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 621 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 622 | if(*jpegSubsamp<0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 623 | _throw("tjDecompressHeader2(): Could not determine subsampling type for JPEG image"); |
| 624 | if(*width<1 || *height<1) |
| 625 | _throw("tjDecompressHeader2(): Invalid data returned in header"); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 626 | |
| 627 | bailout: |
| 628 | return retval; |
| 629 | } |
| 630 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 631 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle, |
| 632 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height) |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 633 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 634 | int jpegSubsamp; |
| 635 | return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height, |
| 636 | &jpegSubsamp); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 640 | DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 641 | { |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 642 | if(numscalingfactors==NULL) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 643 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 644 | snprintf(errStr, JMSG_LENGTH_MAX, |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 645 | "tjGetScalingFactors(): Invalid argument"); |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 646 | return NULL; |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 647 | } |
| 648 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 649 | *numscalingfactors=NUMSF; |
| 650 | return (tjscalingfactor *)sf; |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 654 | DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf, |
| 655 | unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch, |
| 656 | int height, int pixelFormat, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 657 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 658 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 659 | int jpegwidth, jpegheight, scaledw, scaledh; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 660 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 661 | getinstance(handle); |
| 662 | if((this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 663 | _throw("tjDecompress2(): Instance has not been initialized for decompression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 664 | |
| 665 | if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0 |
| 666 | || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF) |
| 667 | _throw("tjDecompress2(): Invalid argument"); |
| 668 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 669 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 670 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 671 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 672 | |
| 673 | if(setjmp(this->jerr.setjmp_buffer)) |
| 674 | { |
| 675 | /* If we get here, the JPEG code has signaled an error. */ |
| 676 | retval=-1; |
| 677 | goto bailout; |
| 678 | } |
| 679 | |
| 680 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 681 | jpeg_read_header(dinfo, TRUE); |
| 682 | setDecompDefaults(dinfo, pixelFormat); |
| 683 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 684 | if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 685 | |
| 686 | jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height; |
| 687 | if(width==0) width=jpegwidth; |
| 688 | if(height==0) height=jpegheight; |
| 689 | for(i=0; i<NUMSF; i++) |
| 690 | { |
| 691 | scaledw=TJSCALED(jpegwidth, sf[i]); |
| 692 | scaledh=TJSCALED(jpegheight, sf[i]); |
| 693 | if(scaledw<=width && scaledh<=height) |
| 694 | break; |
| 695 | } |
| 696 | if(scaledw>width || scaledh>height) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 697 | _throw("tjDecompress2(): Could not scale down to desired image dimensions"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 698 | width=scaledw; height=scaledh; |
| 699 | dinfo->scale_num=sf[i].num; |
| 700 | dinfo->scale_denom=sf[i].denom; |
| 701 | |
| 702 | jpeg_start_decompress(dinfo); |
| 703 | if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat]; |
| 704 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW) |
| 705 | *dinfo->output_height))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 706 | _throw("tjDecompress2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 707 | for(i=0; i<(int)dinfo->output_height; i++) |
| 708 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 709 | if(flags&TJFLAG_BOTTOMUP) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 710 | row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch]; |
| 711 | else row_pointer[i]=&dstBuf[i*pitch]; |
| 712 | } |
| 713 | while(dinfo->output_scanline<dinfo->output_height) |
| 714 | { |
| 715 | jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
| 716 | dinfo->output_height-dinfo->output_scanline); |
| 717 | } |
| 718 | jpeg_finish_decompress(dinfo); |
| 719 | |
| 720 | bailout: |
| 721 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
| 722 | if(row_pointer) free(row_pointer); |
| 723 | return retval; |
| 724 | } |
| 725 | |
| 726 | DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf, |
| 727 | unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch, |
| 728 | int height, int pixelSize, int flags) |
| 729 | { |
| 730 | if(flags&TJ_YUV) |
| 731 | return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags); |
| 732 | else |
| 733 | return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch, |
| 734 | height, getPixelFormat(pixelSize, flags), flags); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle, |
| 739 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 740 | int flags) |
| 741 | { |
| 742 | int i, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS]; |
| 743 | int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS], |
| 744 | tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS]; |
| 745 | JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf; JSAMPROW *tmpbuf[MAX_COMPONENTS]; |
| 746 | |
| 747 | getinstance(handle); |
| 748 | if((this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 749 | _throw("tjDecompressToYUV(): Instance has not been initialized for decompression"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 750 | |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 751 | for(i=0; i<MAX_COMPONENTS; i++) |
| 752 | { |
| 753 | tmpbuf[i]=NULL; outbuf[i]=NULL; |
| 754 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 755 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 756 | if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL) |
| 757 | _throw("tjDecompressToYUV(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 758 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 759 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 760 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 761 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 762 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 763 | if(setjmp(this->jerr.setjmp_buffer)) |
| 764 | { |
| 765 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 766 | retval=-1; |
| 767 | goto bailout; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 768 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 769 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 770 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 771 | jpeg_read_header(dinfo, TRUE); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 772 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 773 | for(i=0; i<dinfo->num_components; i++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 774 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 775 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
| 776 | int ih; |
| 777 | iw[i]=compptr->width_in_blocks*DCTSIZE; |
| 778 | ih=compptr->height_in_blocks*DCTSIZE; |
| 779 | cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor) |
| 780 | *compptr->h_samp_factor/dinfo->max_h_samp_factor; |
| 781 | ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor) |
| 782 | *compptr->v_samp_factor/dinfo->max_v_samp_factor; |
| 783 | if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1; |
| 784 | th[i]=compptr->v_samp_factor*DCTSIZE; |
| 785 | tmpbufsize+=iw[i]*th[i]; |
| 786 | if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 787 | _throw("tjDecompressToYUV(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 788 | for(row=0; row<ch[i]; row++) |
| 789 | { |
| 790 | outbuf[i][row]=ptr; |
| 791 | ptr+=PAD(cw[i], 4); |
| 792 | } |
| 793 | } |
| 794 | if(usetmpbuf) |
| 795 | { |
| 796 | if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 797 | _throw("tjDecompressToYUV(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 798 | ptr=_tmpbuf; |
| 799 | for(i=0; i<dinfo->num_components; i++) |
| 800 | { |
| 801 | if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 802 | _throw("tjDecompressToYUV(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 803 | for(row=0; row<th[i]; row++) |
| 804 | { |
| 805 | tmpbuf[i][row]=ptr; |
| 806 | ptr+=iw[i]; |
| 807 | } |
| 808 | } |
| 809 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 810 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 811 | if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 812 | dinfo->raw_data_out=TRUE; |
| 813 | |
| 814 | jpeg_start_decompress(dinfo); |
| 815 | for(row=0; row<(int)dinfo->output_height; |
| 816 | row+=dinfo->max_v_samp_factor*DCTSIZE) |
| 817 | { |
| 818 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
| 819 | int crow[MAX_COMPONENTS]; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 820 | for(i=0; i<dinfo->num_components; i++) |
| 821 | { |
| 822 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 823 | crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor; |
| 824 | if(usetmpbuf) yuvptr[i]=tmpbuf[i]; |
| 825 | else yuvptr[i]=&outbuf[i][crow[i]]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 826 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 827 | jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 828 | if(usetmpbuf) |
| 829 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 830 | int j; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 831 | for(i=0; i<dinfo->num_components; i++) |
| 832 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 833 | for(j=0; j<min(th[i], ch[i]-crow[i]); j++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 834 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 835 | memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 836 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 840 | jpeg_finish_decompress(dinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 841 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 842 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 843 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 844 | for(i=0; i<MAX_COMPONENTS; i++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 845 | { |
| 846 | if(tmpbuf[i]) free(tmpbuf[i]); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 847 | if(outbuf[i]) free(outbuf[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 848 | } |
| 849 | if(_tmpbuf) free(_tmpbuf); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 850 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 854 | /* Transformer */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 855 | |
| 856 | DLLEXPORT tjhandle DLLCALL tjInitTransform(void) |
| 857 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 858 | tjinstance *this=NULL; tjhandle handle=NULL; |
| 859 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 860 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 861 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 862 | "tjInitTransform(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 863 | return NULL; |
| 864 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 865 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 866 | handle=_tjInitCompress(this); |
| 867 | if(!handle) return NULL; |
| 868 | handle=_tjInitDecompress(this); |
| 869 | return handle; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 873 | DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf, |
| 874 | unsigned long jpegSize, int n, unsigned char **dstBufs, |
| 875 | unsigned long *dstSizes, tjtransform *t, int flags) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 876 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 877 | jpeg_transform_info *xinfo=NULL; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 878 | jvirt_barray_ptr *srccoefs, *dstcoefs; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 879 | int retval=0, i; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 880 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 881 | getinstance(handle); |
| 882 | if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 883 | _throw("tjTransform(): Instance has not been initialized for transformation"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 884 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 885 | if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL |
| 886 | || t==NULL || flags<0) |
| 887 | _throw("tjTransform(): Invalid argument"); |
| 888 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 889 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 890 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 891 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 892 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 893 | if(setjmp(this->jerr.setjmp_buffer)) |
| 894 | { |
| 895 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 896 | retval=-1; |
| 897 | goto bailout; |
| 898 | } |
| 899 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 900 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 901 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 902 | if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n)) |
| 903 | ==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 904 | _throw("tjTransform(): Memory allocation failure"); |
| 905 | MEMZERO(xinfo, sizeof(jpeg_transform_info)*n); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 906 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 907 | for(i=0; i<n; i++) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 908 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 909 | xinfo[i].transform=xformtypes[t[i].op]; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 910 | xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0; |
| 911 | xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0; |
| 912 | xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0; |
| 913 | xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0; |
| 914 | if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1; |
DRC | ba5ea51 | 2011-03-04 03:20:34 +0000 | [diff] [blame] | 915 | else xinfo[i].slow_hflip=0; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 916 | |
| 917 | if(xinfo[i].crop) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 918 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 919 | xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS; |
| 920 | xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS; |
| 921 | if(t[i].r.w!=0) |
| 922 | { |
| 923 | xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS; |
| 924 | } |
DRC | d932e58 | 2011-03-15 20:09:47 +0000 | [diff] [blame] | 925 | else xinfo[i].crop_width=JCROP_UNSET; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 926 | if(t[i].r.h!=0) |
| 927 | { |
| 928 | xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS; |
| 929 | } |
DRC | d932e58 | 2011-03-15 20:09:47 +0000 | [diff] [blame] | 930 | else xinfo[i].crop_height=JCROP_UNSET; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 934 | jcopy_markers_setup(dinfo, JCOPYOPT_ALL); |
| 935 | jpeg_read_header(dinfo, TRUE); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 936 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 937 | for(i=0; i<n; i++) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 938 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 939 | if(!jtransform_request_workspace(dinfo, &xinfo[i])) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame^] | 940 | _throw("tjTransform(): Transform is not perfect"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 941 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 942 | if(xinfo[i].crop) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 943 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 944 | if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0 |
| 945 | || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0) |
| 946 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 947 | snprintf(errStr, JMSG_LENGTH_MAX, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 948 | "To crop this JPEG image, x must be a multiple of %d\n" |
| 949 | "and y must be a multiple of %d.\n", |
| 950 | xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height); |
| 951 | retval=-1; goto bailout; |
| 952 | } |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 956 | srccoefs=jpeg_read_coefficients(dinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 957 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 958 | for(i=0; i<n; i++) |
| 959 | { |
| 960 | int w, h; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 961 | jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 962 | (flags&TJFLAG_NOREALLOC)==0); |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 963 | if(!xinfo[i].crop) |
| 964 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 965 | w=dinfo->image_width; h=dinfo->image_height; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 966 | } |
| 967 | else |
| 968 | { |
| 969 | w=xinfo[i].crop_width; h=xinfo[i].crop_height; |
| 970 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 971 | jpeg_copy_critical_parameters(dinfo, cinfo); |
| 972 | dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 973 | &xinfo[i]); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 974 | jpeg_write_coefficients(cinfo, dstcoefs); |
| 975 | jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL); |
| 976 | jtransform_execute_transformation(dinfo, cinfo, srccoefs, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 977 | &xinfo[i]); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 978 | jpeg_finish_compress(cinfo); |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 979 | } |
| 980 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 981 | jpeg_finish_decompress(dinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 982 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 983 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 984 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
| 985 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 986 | if(xinfo) free(xinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 987 | return retval; |
| 988 | } |