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