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