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