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