DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1 | /* |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 2 | * Copyright (C)2009-2013 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 | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 125 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 126 | static int getPixelFormat(int pixelSize, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 127 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 128 | if(pixelSize==1) return TJPF_GRAY; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 129 | if(pixelSize==3) |
| 130 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 131 | if(flags&TJ_BGR) return TJPF_BGR; |
| 132 | else return TJPF_RGB; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 133 | } |
| 134 | if(pixelSize==4) |
| 135 | { |
| 136 | if(flags&TJ_ALPHAFIRST) |
| 137 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 138 | if(flags&TJ_BGR) return TJPF_XBGR; |
| 139 | else return TJPF_XRGB; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 143 | if(flags&TJ_BGR) return TJPF_BGRX; |
| 144 | else return TJPF_RGBX; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | return -1; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 150 | static int setCompDefaults(struct jpeg_compress_struct *cinfo, |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 151 | int pixelFormat, int subsamp, int jpegQual, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 152 | { |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 153 | int retval=0; |
| 154 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 155 | switch(pixelFormat) |
| 156 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 157 | case TJPF_GRAY: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 158 | cinfo->in_color_space=JCS_GRAYSCALE; break; |
| 159 | #if JCS_EXTENSIONS==1 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 160 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 161 | cinfo->in_color_space=JCS_EXT_RGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 162 | case TJPF_BGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 163 | cinfo->in_color_space=JCS_EXT_BGR; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 164 | case TJPF_RGBX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 165 | case TJPF_RGBA: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 166 | cinfo->in_color_space=JCS_EXT_RGBX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 167 | case TJPF_BGRX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 168 | case TJPF_BGRA: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 169 | cinfo->in_color_space=JCS_EXT_BGRX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 170 | case TJPF_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 171 | case TJPF_ARGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 172 | cinfo->in_color_space=JCS_EXT_XRGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 173 | case TJPF_XBGR: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 174 | case TJPF_ABGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 175 | cinfo->in_color_space=JCS_EXT_XBGR; break; |
| 176 | #else |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 177 | case TJPF_RGB: |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 178 | case TJPF_BGR: |
| 179 | case TJPF_RGBX: |
| 180 | case TJPF_BGRX: |
| 181 | case TJPF_XRGB: |
| 182 | case TJPF_XBGR: |
| 183 | case TJPF_RGBA: |
| 184 | case TJPF_BGRA: |
| 185 | case TJPF_ARGB: |
| 186 | case TJPF_ABGR: |
| 187 | cinfo->in_color_space=JCS_RGB; pixelFormat=TJPF_RGB; |
| 188 | break; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 189 | #endif |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 190 | case TJPF_CMYK: |
| 191 | cinfo->in_color_space=JCS_CMYK; break; |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 192 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 193 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 194 | cinfo->input_components=tjPixelSize[pixelFormat]; |
| 195 | jpeg_set_defaults(cinfo); |
| 196 | if(jpegQual>=0) |
| 197 | { |
| 198 | jpeg_set_quality(cinfo, jpegQual, TRUE); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 199 | if(jpegQual>=96 || flags&TJFLAG_ACCURATEDCT) cinfo->dct_method=JDCT_ISLOW; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 200 | else cinfo->dct_method=JDCT_FASTEST; |
| 201 | } |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 202 | if(subsamp==TJSAMP_GRAY) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 203 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 204 | else if(pixelFormat==TJPF_CMYK) |
| 205 | jpeg_set_colorspace(cinfo, JCS_YCCK); |
| 206 | else jpeg_set_colorspace(cinfo, JCS_YCbCr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 207 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 208 | cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8; |
| 209 | cinfo->comp_info[1].h_samp_factor=1; |
| 210 | cinfo->comp_info[2].h_samp_factor=1; |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 211 | if(cinfo->num_components>3) |
| 212 | cinfo->comp_info[3].h_samp_factor=tjMCUWidth[subsamp]/8; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 213 | cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8; |
| 214 | cinfo->comp_info[1].v_samp_factor=1; |
| 215 | cinfo->comp_info[2].v_samp_factor=1; |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 216 | if(cinfo->num_components>3) |
| 217 | cinfo->comp_info[3].v_samp_factor=tjMCUHeight[subsamp]/8; |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 218 | |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 219 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 220 | } |
| 221 | |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 222 | static int setDecompDefaults(struct jpeg_decompress_struct *dinfo, |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 223 | int pixelFormat, int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 224 | { |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 225 | int retval=0; |
| 226 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 227 | switch(pixelFormat) |
| 228 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 229 | case TJPF_GRAY: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 230 | dinfo->out_color_space=JCS_GRAYSCALE; break; |
| 231 | #if JCS_EXTENSIONS==1 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 232 | case TJPF_RGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 233 | dinfo->out_color_space=JCS_EXT_RGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 234 | case TJPF_BGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 235 | dinfo->out_color_space=JCS_EXT_BGR; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 236 | case TJPF_RGBX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 237 | dinfo->out_color_space=JCS_EXT_RGBX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 238 | case TJPF_BGRX: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 239 | dinfo->out_color_space=JCS_EXT_BGRX; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 240 | case TJPF_XRGB: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 241 | dinfo->out_color_space=JCS_EXT_XRGB; break; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 242 | case TJPF_XBGR: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 243 | dinfo->out_color_space=JCS_EXT_XBGR; break; |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 244 | #if JCS_ALPHA_EXTENSIONS==1 |
| 245 | case TJPF_RGBA: |
| 246 | dinfo->out_color_space=JCS_EXT_RGBA; break; |
| 247 | case TJPF_BGRA: |
| 248 | dinfo->out_color_space=JCS_EXT_BGRA; break; |
| 249 | case TJPF_ARGB: |
| 250 | dinfo->out_color_space=JCS_EXT_ARGB; break; |
| 251 | case TJPF_ABGR: |
| 252 | dinfo->out_color_space=JCS_EXT_ABGR; break; |
| 253 | #endif |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 254 | #else |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 255 | case TJPF_RGB: |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 256 | case TJPF_BGR: |
| 257 | case TJPF_RGBX: |
| 258 | case TJPF_BGRX: |
| 259 | case TJPF_XRGB: |
| 260 | case TJPF_XBGR: |
| 261 | case TJPF_RGBA: |
| 262 | case TJPF_BGRA: |
| 263 | case TJPF_ARGB: |
| 264 | case TJPF_ABGR: |
| 265 | dinfo->out_color_space=JCS_RGB; break; |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 266 | #endif |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 267 | case TJPF_CMYK: |
| 268 | dinfo->out_color_space=JCS_CMYK; break; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 269 | default: |
| 270 | _throw("Unsupported pixel format"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 271 | } |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 272 | |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 273 | if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST; |
| 274 | |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 275 | bailout: |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 276 | return retval; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 280 | static int getSubsamp(j_decompress_ptr dinfo) |
| 281 | { |
| 282 | int retval=-1, i, k; |
| 283 | for(i=0; i<NUMSUBOPT; i++) |
| 284 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 285 | if(dinfo->num_components==pixelsize[i] |
| 286 | || ((dinfo->jpeg_color_space==JCS_YCCK |
| 287 | || dinfo->jpeg_color_space==JCS_CMYK) |
| 288 | && pixelsize[i]==3 && dinfo->num_components==4)) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 289 | { |
| 290 | if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8 |
| 291 | && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8) |
| 292 | { |
| 293 | int match=0; |
| 294 | for(k=1; k<dinfo->num_components; k++) |
| 295 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 296 | int href=1, vref=1; |
| 297 | if(dinfo->jpeg_color_space==JCS_YCCK && k==3) |
| 298 | { |
| 299 | href=tjMCUWidth[i]/8; vref=tjMCUHeight[i]/8; |
| 300 | } |
| 301 | if(dinfo->comp_info[k].h_samp_factor==href |
| 302 | && dinfo->comp_info[k].v_samp_factor==vref) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 303 | match++; |
| 304 | } |
| 305 | if(match==dinfo->num_components-1) |
| 306 | { |
| 307 | retval=i; break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | return retval; |
| 313 | } |
| 314 | |
| 315 | |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 316 | #ifndef JCS_EXTENSIONS |
| 317 | |
| 318 | /* Conversion functions to emulate the colorspace extensions. This allows the |
| 319 | TurboJPEG wrapper to be used with libjpeg */ |
| 320 | |
| 321 | #define TORGB(PS, ROFFSET, GOFFSET, BOFFSET) { \ |
| 322 | int rowPad=pitch-width*PS; \ |
| 323 | while(height--) \ |
| 324 | { \ |
| 325 | unsigned char *endOfRow=src+width*PS; \ |
| 326 | while(src<endOfRow) \ |
| 327 | { \ |
| 328 | dst[RGB_RED]=src[ROFFSET]; \ |
| 329 | dst[RGB_GREEN]=src[GOFFSET]; \ |
| 330 | dst[RGB_BLUE]=src[BOFFSET]; \ |
| 331 | dst+=RGB_PIXELSIZE; src+=PS; \ |
| 332 | } \ |
| 333 | src+=rowPad; \ |
| 334 | } \ |
| 335 | } |
| 336 | |
| 337 | static unsigned char *toRGB(unsigned char *src, int width, int pitch, |
| 338 | int height, int pixelFormat, unsigned char *dst) |
| 339 | { |
| 340 | unsigned char *retval=src; |
| 341 | switch(pixelFormat) |
| 342 | { |
| 343 | case TJPF_RGB: |
| 344 | #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3 |
| 345 | retval=dst; TORGB(3, 0, 1, 2); |
| 346 | #endif |
| 347 | break; |
| 348 | case TJPF_BGR: |
| 349 | #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3 |
| 350 | retval=dst; TORGB(3, 2, 1, 0); |
| 351 | #endif |
| 352 | break; |
| 353 | case TJPF_RGBX: |
| 354 | case TJPF_RGBA: |
| 355 | #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4 |
| 356 | retval=dst; TORGB(4, 0, 1, 2); |
| 357 | #endif |
| 358 | break; |
| 359 | case TJPF_BGRX: |
| 360 | case TJPF_BGRA: |
| 361 | #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4 |
| 362 | retval=dst; TORGB(4, 2, 1, 0); |
| 363 | #endif |
| 364 | break; |
| 365 | case TJPF_XRGB: |
| 366 | case TJPF_ARGB: |
| 367 | #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4 |
| 368 | retval=dst; TORGB(4, 1, 2, 3); |
| 369 | #endif |
| 370 | break; |
| 371 | case TJPF_XBGR: |
| 372 | case TJPF_ABGR: |
| 373 | #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4 |
| 374 | retval=dst; TORGB(4, 3, 2, 1); |
| 375 | #endif |
| 376 | break; |
| 377 | } |
| 378 | return retval; |
| 379 | } |
| 380 | |
| 381 | #define FROMRGB(PS, ROFFSET, GOFFSET, BOFFSET, SETALPHA) { \ |
| 382 | int rowPad=pitch-width*PS; \ |
| 383 | while(height--) \ |
| 384 | { \ |
| 385 | unsigned char *endOfRow=dst+width*PS; \ |
| 386 | while(dst<endOfRow) \ |
| 387 | { \ |
| 388 | dst[ROFFSET]=src[RGB_RED]; \ |
| 389 | dst[GOFFSET]=src[RGB_GREEN]; \ |
| 390 | dst[BOFFSET]=src[RGB_BLUE]; \ |
| 391 | SETALPHA \ |
| 392 | dst+=PS; src+=RGB_PIXELSIZE; \ |
| 393 | } \ |
| 394 | dst+=rowPad; \ |
| 395 | } \ |
| 396 | } |
| 397 | |
| 398 | static void fromRGB(unsigned char *src, unsigned char *dst, int width, |
| 399 | int pitch, int height, int pixelFormat) |
| 400 | { |
| 401 | switch(pixelFormat) |
| 402 | { |
| 403 | case TJPF_RGB: |
| 404 | #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3 |
| 405 | FROMRGB(3, 0, 1, 2,); |
| 406 | #endif |
| 407 | break; |
| 408 | case TJPF_BGR: |
| 409 | #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3 |
| 410 | FROMRGB(3, 2, 1, 0,); |
| 411 | #endif |
| 412 | break; |
| 413 | case TJPF_RGBX: |
| 414 | #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4 |
| 415 | FROMRGB(4, 0, 1, 2,); |
| 416 | #endif |
| 417 | break; |
| 418 | case TJPF_RGBA: |
| 419 | #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4 |
| 420 | FROMRGB(4, 0, 1, 2, dst[3]=0xFF;); |
| 421 | #endif |
| 422 | break; |
| 423 | case TJPF_BGRX: |
| 424 | #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4 |
| 425 | FROMRGB(4, 2, 1, 0,); |
| 426 | #endif |
| 427 | break; |
| 428 | case TJPF_BGRA: |
| 429 | #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4 |
| 430 | FROMRGB(4, 2, 1, 0, dst[3]=0xFF;); return; |
| 431 | #endif |
| 432 | break; |
| 433 | case TJPF_XRGB: |
| 434 | #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4 |
| 435 | FROMRGB(4, 1, 2, 3,); return; |
| 436 | #endif |
| 437 | break; |
| 438 | case TJPF_ARGB: |
| 439 | #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4 |
| 440 | FROMRGB(4, 1, 2, 3, dst[0]=0xFF;); return; |
| 441 | #endif |
| 442 | break; |
| 443 | case TJPF_XBGR: |
| 444 | #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4 |
| 445 | FROMRGB(4, 3, 2, 1,); return; |
| 446 | #endif |
| 447 | break; |
| 448 | case TJPF_ABGR: |
| 449 | #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4 |
| 450 | FROMRGB(4, 3, 2, 1, dst[0]=0xFF;); return; |
| 451 | #endif |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | #endif |
| 457 | |
| 458 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 459 | /* General API functions */ |
| 460 | |
| 461 | DLLEXPORT char* DLLCALL tjGetErrorStr(void) |
| 462 | { |
| 463 | return errStr; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | DLLEXPORT int DLLCALL tjDestroy(tjhandle handle) |
| 468 | { |
| 469 | getinstance(handle); |
| 470 | if(setjmp(this->jerr.setjmp_buffer)) return -1; |
| 471 | if(this->init&COMPRESS) jpeg_destroy_compress(cinfo); |
| 472 | if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo); |
| 473 | free(this); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 478 | /* These are exposed mainly because Windows can't malloc() and free() across |
| 479 | DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL |
| 480 | with turbojpeg.dll for compatibility reasons. However, these functions |
| 481 | can potentially be used for other purposes by different implementations. */ |
| 482 | |
| 483 | DLLEXPORT void DLLCALL tjFree(unsigned char *buf) |
| 484 | { |
| 485 | if(buf) free(buf); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | DLLEXPORT unsigned char *DLLCALL tjAlloc(int bytes) |
| 490 | { |
| 491 | return (unsigned char *)malloc(bytes); |
| 492 | } |
| 493 | |
| 494 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 495 | /* Compressor */ |
| 496 | |
| 497 | static tjhandle _tjInitCompress(tjinstance *this) |
| 498 | { |
| 499 | unsigned char buffer[1], *buf=buffer; unsigned long size=1; |
| 500 | |
| 501 | /* This is also straight out of example.c */ |
| 502 | this->cinfo.err=jpeg_std_error(&this->jerr.pub); |
| 503 | this->jerr.pub.error_exit=my_error_exit; |
| 504 | this->jerr.pub.output_message=my_output_message; |
| 505 | |
| 506 | if(setjmp(this->jerr.setjmp_buffer)) |
| 507 | { |
| 508 | /* If we get here, the JPEG code has signaled an error. */ |
| 509 | if(this) free(this); return NULL; |
| 510 | } |
| 511 | |
| 512 | jpeg_create_compress(&this->cinfo); |
| 513 | /* Make an initial call so it will create the destination manager */ |
| 514 | jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0); |
| 515 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 516 | this->init|=COMPRESS; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 517 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 520 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void) |
| 521 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 522 | tjinstance *this=NULL; |
| 523 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 524 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 525 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 526 | "tjInitCompress(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 529 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 530 | return _tjInitCompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 531 | } |
| 532 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 533 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 534 | DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height, |
| 535 | int jpegSubsamp) |
| 536 | { |
| 537 | unsigned long retval=0; int mcuw, mcuh, chromasf; |
| 538 | if(width<1 || height<1 || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT) |
| 539 | _throw("tjBufSize(): Invalid argument"); |
| 540 | |
| 541 | // This allows for rare corner cases in which a JPEG image can actually be |
| 542 | // larger than the uncompressed input (we wouldn't mention it if it hadn't |
| 543 | // happened before.) |
| 544 | mcuw=tjMCUWidth[jpegSubsamp]; |
| 545 | mcuh=tjMCUHeight[jpegSubsamp]; |
| 546 | chromasf=jpegSubsamp==TJSAMP_GRAY? 0: 4*64/(mcuw*mcuh); |
| 547 | retval=PAD(width, mcuw) * PAD(height, mcuh) * (2 + chromasf) + 2048; |
| 548 | |
| 549 | bailout: |
| 550 | return retval; |
| 551 | } |
| 552 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 553 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height) |
| 554 | { |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 555 | unsigned long retval=0; |
| 556 | if(width<1 || height<1) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 557 | _throw("TJBUFSIZE(): Invalid argument"); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 558 | |
| 559 | // This allows for rare corner cases in which a JPEG image can actually be |
| 560 | // larger than the uncompressed input (we wouldn't mention it if it hadn't |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 561 | // happened before.) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 562 | retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048; |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 563 | |
| 564 | bailout: |
| 565 | return retval; |
| 566 | } |
| 567 | |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 568 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 569 | DLLEXPORT unsigned long DLLCALL tjBufSizeYUV2(int width, int pad, int height, |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 570 | int subsamp) |
| 571 | { |
| 572 | unsigned long retval=0; |
| 573 | int pw, ph, cw, ch; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 574 | if(width<1 || height<1 || pad<1 || !isPow2(pad) || subsamp<0 |
| 575 | || subsamp>=NUMSUBOPT) |
| 576 | _throw("tjBufSizeYUV2(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 577 | pw=PAD(width, tjMCUWidth[subsamp]/8); |
| 578 | ph=PAD(height, tjMCUHeight[subsamp]/8); |
| 579 | cw=pw*8/tjMCUWidth[subsamp]; ch=ph*8/tjMCUHeight[subsamp]; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 580 | 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] | 581 | |
| 582 | bailout: |
| 583 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 586 | DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height, |
| 587 | int subsamp) |
| 588 | { |
| 589 | return tjBufSizeYUV2(width, 4, height, subsamp); |
| 590 | } |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 591 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 592 | DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height, |
| 593 | int subsamp) |
| 594 | { |
| 595 | return tjBufSizeYUV(width, height, subsamp); |
| 596 | } |
| 597 | |
| 598 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 599 | DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, |
| 600 | int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf, |
| 601 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags) |
| 602 | { |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 603 | int i, retval=0, alloc=1; JSAMPROW *row_pointer=NULL; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 604 | #ifndef JCS_EXTENSIONS |
| 605 | unsigned char *rgbBuf=NULL; |
| 606 | #endif |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 607 | |
| 608 | getinstance(handle) |
| 609 | if((this->init&COMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 610 | _throw("tjCompress2(): Instance has not been initialized for compression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 611 | |
| 612 | if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0 |
| 613 | || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL |
| 614 | || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 615 | _throw("tjCompress2(): Invalid argument"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 616 | |
| 617 | if(setjmp(this->jerr.setjmp_buffer)) |
| 618 | { |
| 619 | /* If we get here, the JPEG code has signaled an error. */ |
| 620 | retval=-1; |
| 621 | goto bailout; |
| 622 | } |
| 623 | |
| 624 | if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; |
| 625 | |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 626 | #ifndef JCS_EXTENSIONS |
| 627 | if(pixelFormat!=TJPF_GRAY) |
| 628 | { |
| 629 | rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE); |
| 630 | if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure"); |
| 631 | srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf); |
| 632 | pitch=width*RGB_PIXELSIZE; |
| 633 | } |
| 634 | #endif |
| 635 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 636 | cinfo->image_width=width; |
| 637 | cinfo->image_height=height; |
| 638 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 639 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 640 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 641 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 642 | |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 643 | if(flags&TJFLAG_NOREALLOC) |
| 644 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 645 | alloc=0; *jpegSize=tjBufSize(width, height, jpegSubsamp); |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 646 | } |
| 647 | jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 648 | if(setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags)==-1) |
DRC | f12bb30 | 2011-09-07 05:03:18 +0000 | [diff] [blame] | 649 | return -1; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 650 | |
| 651 | jpeg_start_compress(cinfo, TRUE); |
| 652 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 653 | _throw("tjCompress2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 654 | for(i=0; i<height; i++) |
| 655 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 656 | if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 657 | else row_pointer[i]=&srcBuf[i*pitch]; |
| 658 | } |
| 659 | while(cinfo->next_scanline<cinfo->image_height) |
| 660 | { |
| 661 | jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline], |
| 662 | cinfo->image_height-cinfo->next_scanline); |
| 663 | } |
| 664 | jpeg_finish_compress(cinfo); |
| 665 | |
| 666 | bailout: |
| 667 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 668 | #ifndef JCS_EXTENSIONS |
DRC | ea3396a | 2012-04-26 03:18:49 +0000 | [diff] [blame] | 669 | if(rgbBuf) free(rgbBuf); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 670 | #endif |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 671 | if(row_pointer) free(row_pointer); |
| 672 | return retval; |
| 673 | } |
| 674 | |
| 675 | DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf, |
| 676 | int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf, |
| 677 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags) |
| 678 | { |
| 679 | int retval=0; unsigned long size; |
| 680 | if(flags&TJ_YUV) |
| 681 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 682 | size=tjBufSizeYUV(width, height, jpegSubsamp); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 683 | retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 684 | getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags); |
| 685 | } |
| 686 | else |
| 687 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 688 | retval=tjCompress2(handle, srcBuf, width, pitch, height, |
| 689 | getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual, |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 690 | flags|TJFLAG_NOREALLOC); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 691 | } |
| 692 | *jpegSize=size; |
| 693 | return retval; |
| 694 | } |
| 695 | |
| 696 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 697 | DLLEXPORT int DLLCALL tjEncodeYUV3(tjhandle handle, unsigned char *srcBuf, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 698 | int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf, |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 699 | int pad, int subsamp, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 700 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 701 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 702 | JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS]; |
| 703 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS]; |
| 704 | JSAMPROW *outbuf[MAX_COMPONENTS]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 705 | int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS]; |
| 706 | JSAMPLE *ptr=dstBuf; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 707 | unsigned long yuvsize=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 708 | jpeg_component_info *compptr; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 709 | #ifndef JCS_EXTENSIONS |
| 710 | unsigned char *rgbBuf=NULL; |
| 711 | #endif |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 712 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 713 | for(i=0; i<MAX_COMPONENTS; i++) |
| 714 | { |
| 715 | tmpbuf[i]=NULL; _tmpbuf[i]=NULL; |
| 716 | tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL; |
| 717 | } |
| 718 | |
DRC | e2f8e69 | 2013-10-30 22:21:06 +0000 | [diff] [blame^] | 719 | getinstance(handle); |
| 720 | if((this->init&COMPRESS)==0) |
| 721 | _throw("tjEncodeYUV3(): Instance has not been initialized for compression"); |
| 722 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 723 | if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0 |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 724 | || pixelFormat>=TJ_NUMPF || dstBuf==NULL || pad<0 || !isPow2(pad) |
| 725 | || subsamp<0 || subsamp>=NUMSUBOPT) |
| 726 | _throw("tjEncodeYUV3(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 727 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 728 | if(setjmp(this->jerr.setjmp_buffer)) |
| 729 | { |
| 730 | /* If we get here, the JPEG code has signaled an error. */ |
| 731 | retval=-1; |
| 732 | goto bailout; |
| 733 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 734 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 735 | if(pixelFormat==TJPF_CMYK) |
| 736 | _throw("tjEncodeYUV3(): Cannot generate YUV images from CMYK pixels"); |
| 737 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 738 | if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 739 | |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 740 | #ifndef JCS_EXTENSIONS |
| 741 | if(pixelFormat!=TJPF_GRAY) |
| 742 | { |
| 743 | rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 744 | if(!rgbBuf) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 745 | srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf); |
| 746 | pitch=width*RGB_PIXELSIZE; |
| 747 | } |
| 748 | #endif |
| 749 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 750 | cinfo->image_width=width; |
| 751 | cinfo->image_height=height; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 752 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 753 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 754 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 755 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 756 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 757 | yuvsize=tjBufSizeYUV2(width, pad, height, subsamp); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 758 | jpeg_mem_dest_tj(cinfo, &dstBuf, &yuvsize, 0); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 759 | if(setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags)==-1) return -1; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 760 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 761 | jpeg_start_compress(cinfo, TRUE); |
| 762 | pw=PAD(width, cinfo->max_h_samp_factor); |
| 763 | ph=PAD(height, cinfo->max_v_samp_factor); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 764 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 765 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 766 | _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 767 | for(i=0; i<height; i++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 768 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 769 | if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 770 | else row_pointer[i]=&srcBuf[i*pitch]; |
| 771 | } |
| 772 | if(height<ph) |
| 773 | for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1]; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 774 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 775 | for(i=0; i<cinfo->num_components; i++) |
| 776 | { |
| 777 | compptr=&cinfo->comp_info[i]; |
| 778 | _tmpbuf[i]=(JSAMPLE *)malloc( |
| 779 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
| 780 | /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 781 | if(!_tmpbuf[i]) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 782 | tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 783 | if(!tmpbuf[i]) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 784 | for(row=0; row<cinfo->max_v_samp_factor; row++) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 785 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 786 | unsigned char *_tmpbuf_aligned= |
| 787 | (unsigned char *)PAD((size_t)_tmpbuf[i], 16); |
| 788 | tmpbuf[i][row]=&_tmpbuf_aligned[ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 789 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 790 | /compptr->h_samp_factor, 16) * row]; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 791 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 792 | _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16) |
| 793 | * compptr->v_samp_factor + 16); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 794 | if(!_tmpbuf2[i]) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 795 | tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 796 | if(!tmpbuf2[i]) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 797 | for(row=0; row<compptr->v_samp_factor; row++) |
| 798 | { |
| 799 | unsigned char *_tmpbuf2_aligned= |
| 800 | (unsigned char *)PAD((size_t)_tmpbuf2[i], 16); |
| 801 | tmpbuf2[i][row]=&_tmpbuf2_aligned[ |
| 802 | PAD(compptr->width_in_blocks*DCTSIZE, 16) * row]; |
| 803 | } |
| 804 | cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor; |
| 805 | ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor; |
| 806 | outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 807 | if(!outbuf[i]) _throw("tjEncodeYUV3(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 808 | for(row=0; row<ch[i]; row++) |
| 809 | { |
| 810 | outbuf[i][row]=ptr; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 811 | ptr+=PAD(cw[i], pad); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | if(yuvsize!=(unsigned long)(ptr-dstBuf)) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 815 | _throw("tjEncodeYUV3(): Generated image is not the correct size"); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 816 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 817 | for(row=0; row<ph; row+=cinfo->max_v_samp_factor) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 818 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 819 | (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0, |
| 820 | cinfo->max_v_samp_factor); |
| 821 | (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0); |
| 822 | for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++) |
| 823 | jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i], |
| 824 | row*compptr->v_samp_factor/cinfo->max_v_samp_factor, |
| 825 | compptr->v_samp_factor, cw[i]); |
DRC | 6ee5459 | 2011-03-01 08:18:30 +0000 | [diff] [blame] | 826 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 827 | cinfo->next_scanline+=height; |
| 828 | jpeg_abort_compress(cinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 829 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 830 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 831 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 832 | #ifndef JCS_EXTENSIONS |
DRC | ea3396a | 2012-04-26 03:18:49 +0000 | [diff] [blame] | 833 | if(rgbBuf) free(rgbBuf); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 834 | #endif |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 835 | if(row_pointer) free(row_pointer); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 836 | for(i=0; i<MAX_COMPONENTS; i++) |
| 837 | { |
| 838 | if(tmpbuf[i]!=NULL) free(tmpbuf[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 839 | if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 840 | if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 841 | if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 842 | if(outbuf[i]!=NULL) free(outbuf[i]); |
| 843 | } |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 844 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 845 | } |
| 846 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 847 | DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, |
| 848 | int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf, |
| 849 | int subsamp, int flags) |
| 850 | { |
| 851 | return tjEncodeYUV3(handle, srcBuf, width, pitch, height, pixelFormat, |
| 852 | dstBuf, 4, subsamp, flags); |
| 853 | } |
| 854 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 855 | DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, |
| 856 | int width, int pitch, int height, int pixelSize, unsigned char *dstBuf, |
| 857 | int subsamp, int flags) |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 858 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 859 | return tjEncodeYUV2(handle, srcBuf, width, pitch, height, |
| 860 | getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 864 | /* Decompressor */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 865 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 866 | static tjhandle _tjInitDecompress(tjinstance *this) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 867 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 868 | unsigned char buffer[1]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 869 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 870 | /* This is also straight out of example.c */ |
| 871 | this->dinfo.err=jpeg_std_error(&this->jerr.pub); |
| 872 | this->jerr.pub.error_exit=my_error_exit; |
| 873 | this->jerr.pub.output_message=my_output_message; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 874 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 875 | if(setjmp(this->jerr.setjmp_buffer)) |
| 876 | { |
| 877 | /* If we get here, the JPEG code has signaled an error. */ |
| 878 | if(this) free(this); return NULL; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 879 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 880 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 881 | jpeg_create_decompress(&this->dinfo); |
| 882 | /* Make an initial call so it will create the source manager */ |
| 883 | jpeg_mem_src_tj(&this->dinfo, buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 884 | |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 885 | this->init|=DECOMPRESS; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 886 | return (tjhandle)this; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 887 | } |
| 888 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 889 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void) |
| 890 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 891 | tjinstance *this; |
| 892 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 893 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 894 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 895 | "tjInitDecompress(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 896 | return NULL; |
| 897 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 898 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 899 | return _tjInitDecompress(this); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 900 | } |
| 901 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 902 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 903 | DLLEXPORT int DLLCALL tjDecompressHeader3(tjhandle handle, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 904 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height, |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 905 | int *jpegSubsamp, int *jpegColorspace) |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 906 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 907 | int retval=0; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 908 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 909 | getinstance(handle); |
| 910 | if((this->init&DECOMPRESS)==0) |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 911 | _throw("tjDecompressHeader3(): Instance has not been initialized for decompression"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 912 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 913 | if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 914 | || jpegSubsamp==NULL || jpegColorspace==NULL) |
| 915 | _throw("tjDecompressHeader3(): Invalid argument"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 916 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 917 | if(setjmp(this->jerr.setjmp_buffer)) |
| 918 | { |
| 919 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 920 | return -1; |
| 921 | } |
| 922 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 923 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 924 | jpeg_read_header(dinfo, TRUE); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 925 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 926 | *width=dinfo->image_width; |
| 927 | *height=dinfo->image_height; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 928 | *jpegSubsamp=getSubsamp(dinfo); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 929 | switch(dinfo->jpeg_color_space) |
| 930 | { |
| 931 | case JCS_GRAYSCALE: *jpegColorspace=TJCS_GRAY; break; |
| 932 | case JCS_RGB: *jpegColorspace=TJCS_RGB; break; |
| 933 | case JCS_YCbCr: *jpegColorspace=TJCS_YCbCr; break; |
| 934 | case JCS_CMYK: *jpegColorspace=TJCS_CMYK; break; |
| 935 | case JCS_YCCK: *jpegColorspace=TJCS_YCCK; break; |
| 936 | default: *jpegColorspace=-1; break; |
| 937 | } |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 938 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 939 | jpeg_abort_decompress(dinfo); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 940 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 941 | if(*jpegSubsamp<0) |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 942 | _throw("tjDecompressHeader3(): Could not determine subsampling type for JPEG image"); |
| 943 | if(*jpegColorspace<0) |
| 944 | _throw("tjDecompressHeader3(): Could not determine colorspace of JPEG image"); |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 945 | if(*width<1 || *height<1) |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 946 | _throw("tjDecompressHeader3(): Invalid data returned in header"); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 947 | |
| 948 | bailout: |
| 949 | return retval; |
| 950 | } |
| 951 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 952 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle, |
| 953 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height, |
| 954 | int *jpegSubsamp) |
| 955 | { |
| 956 | int jpegColorspace; |
| 957 | return tjDecompressHeader3(handle, jpegBuf, jpegSize, width, height, |
| 958 | jpegSubsamp, &jpegColorspace); |
| 959 | } |
| 960 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 961 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle, |
| 962 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height) |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 963 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 964 | int jpegSubsamp; |
| 965 | return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height, |
| 966 | &jpegSubsamp); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 970 | DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 971 | { |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 972 | if(numscalingfactors==NULL) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 973 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 974 | snprintf(errStr, JMSG_LENGTH_MAX, |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 975 | "tjGetScalingFactors(): Invalid argument"); |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 976 | return NULL; |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 977 | } |
| 978 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 979 | *numscalingfactors=NUMSF; |
| 980 | return (tjscalingfactor *)sf; |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 984 | DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf, |
| 985 | unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch, |
| 986 | int height, int pixelFormat, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 987 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 988 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 989 | int jpegwidth, jpegheight, scaledw, scaledh; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 990 | #ifndef JCS_EXTENSIONS |
| 991 | unsigned char *rgbBuf=NULL; |
| 992 | unsigned char *_dstBuf=NULL; int _pitch=0; |
| 993 | #endif |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 994 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 995 | getinstance(handle); |
| 996 | if((this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 997 | _throw("tjDecompress2(): Instance has not been initialized for decompression"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 998 | |
| 999 | if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0 |
| 1000 | || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF) |
| 1001 | _throw("tjDecompress2(): Invalid argument"); |
| 1002 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1003 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1004 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1005 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1006 | |
| 1007 | if(setjmp(this->jerr.setjmp_buffer)) |
| 1008 | { |
| 1009 | /* If we get here, the JPEG code has signaled an error. */ |
| 1010 | retval=-1; |
| 1011 | goto bailout; |
| 1012 | } |
| 1013 | |
| 1014 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1015 | jpeg_read_header(dinfo, TRUE); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 1016 | if(setDecompDefaults(dinfo, pixelFormat, flags)==-1) |
DRC | 2eda821 | 2012-03-23 19:32:38 +0000 | [diff] [blame] | 1017 | { |
| 1018 | retval=-1; goto bailout; |
| 1019 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1020 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1021 | if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1022 | |
| 1023 | jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height; |
| 1024 | if(width==0) width=jpegwidth; |
| 1025 | if(height==0) height=jpegheight; |
| 1026 | for(i=0; i<NUMSF; i++) |
| 1027 | { |
| 1028 | scaledw=TJSCALED(jpegwidth, sf[i]); |
| 1029 | scaledh=TJSCALED(jpegheight, sf[i]); |
| 1030 | if(scaledw<=width && scaledh<=height) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1031 | break; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1032 | } |
| 1033 | if(scaledw>width || scaledh>height) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1034 | _throw("tjDecompress2(): Could not scale down to desired image dimensions"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1035 | width=scaledw; height=scaledh; |
| 1036 | dinfo->scale_num=sf[i].num; |
| 1037 | dinfo->scale_denom=sf[i].denom; |
| 1038 | |
| 1039 | jpeg_start_decompress(dinfo); |
| 1040 | if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat]; |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 1041 | |
| 1042 | #ifndef JCS_EXTENSIONS |
| 1043 | if(pixelFormat!=TJPF_GRAY && |
| 1044 | (RGB_RED!=tjRedOffset[pixelFormat] || |
| 1045 | RGB_GREEN!=tjGreenOffset[pixelFormat] || |
| 1046 | RGB_BLUE!=tjBlueOffset[pixelFormat] || |
| 1047 | RGB_PIXELSIZE!=tjPixelSize[pixelFormat])) |
| 1048 | { |
| 1049 | rgbBuf=(unsigned char *)malloc(width*height*3); |
| 1050 | if(!rgbBuf) _throw("tjDecompress2(): Memory allocation failure"); |
| 1051 | _pitch=pitch; pitch=width*3; |
| 1052 | _dstBuf=dstBuf; dstBuf=rgbBuf; |
| 1053 | } |
| 1054 | #endif |
| 1055 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1056 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW) |
| 1057 | *dinfo->output_height))==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1058 | _throw("tjDecompress2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1059 | for(i=0; i<(int)dinfo->output_height; i++) |
| 1060 | { |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1061 | if(flags&TJFLAG_BOTTOMUP) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1062 | row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch]; |
| 1063 | else row_pointer[i]=&dstBuf[i*pitch]; |
| 1064 | } |
| 1065 | while(dinfo->output_scanline<dinfo->output_height) |
| 1066 | { |
| 1067 | jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline], |
| 1068 | dinfo->output_height-dinfo->output_scanline); |
| 1069 | } |
| 1070 | jpeg_finish_decompress(dinfo); |
| 1071 | |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 1072 | #ifndef JCS_EXTENSIONS |
| 1073 | fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat); |
| 1074 | #endif |
| 1075 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1076 | bailout: |
| 1077 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 1078 | #ifndef JCS_EXTENSIONS |
DRC | ea3396a | 2012-04-26 03:18:49 +0000 | [diff] [blame] | 1079 | if(rgbBuf) free(rgbBuf); |
DRC | afc0692 | 2012-03-23 19:47:57 +0000 | [diff] [blame] | 1080 | #endif |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1081 | if(row_pointer) free(row_pointer); |
| 1082 | return retval; |
| 1083 | } |
| 1084 | |
| 1085 | DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf, |
| 1086 | unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch, |
| 1087 | int height, int pixelSize, int flags) |
| 1088 | { |
| 1089 | if(flags&TJ_YUV) |
| 1090 | return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags); |
| 1091 | else |
| 1092 | return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch, |
| 1093 | height, getPixelFormat(pixelSize, flags), flags); |
| 1094 | } |
| 1095 | |
| 1096 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1097 | DLLEXPORT int DLLCALL tjDecompressToYUV2(tjhandle handle, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1098 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1099 | int width, int pad, int height, int flags) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1100 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1101 | int i, sfi, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS]; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1102 | int jpegwidth, jpegheight, jpegSubsamp, scaledw, scaledh; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1103 | int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS], |
| 1104 | tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS]; |
| 1105 | JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf; JSAMPROW *tmpbuf[MAX_COMPONENTS]; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1106 | int dctsize; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1107 | |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1108 | for(i=0; i<MAX_COMPONENTS; i++) |
| 1109 | { |
| 1110 | tmpbuf[i]=NULL; outbuf[i]=NULL; |
| 1111 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1112 | |
DRC | e2f8e69 | 2013-10-30 22:21:06 +0000 | [diff] [blame^] | 1113 | getinstance(handle); |
| 1114 | if((this->init&DECOMPRESS)==0) |
| 1115 | _throw("tjDecompressToYUV2(): Instance has not been initialized for decompression"); |
| 1116 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1117 | if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pad<1 |
| 1118 | || !isPow2(pad) || height<0) |
| 1119 | _throw("tjDecompressToYUV2(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1120 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1121 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1122 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1123 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 1124 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1125 | if(setjmp(this->jerr.setjmp_buffer)) |
| 1126 | { |
| 1127 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1128 | retval=-1; |
| 1129 | goto bailout; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1130 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1131 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1132 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
| 1133 | jpeg_read_header(dinfo, TRUE); |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1134 | jpegSubsamp=getSubsamp(dinfo); |
| 1135 | if(jpegSubsamp<0) |
| 1136 | _throw("tjTransform(): Could not determine subsampling type for JPEG image"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1137 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1138 | jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height; |
| 1139 | if(width==0) width=jpegwidth; |
| 1140 | if(height==0) height=jpegheight; |
| 1141 | for(i=0; i<NUMSF; i++) |
| 1142 | { |
| 1143 | scaledw=TJSCALED(jpegwidth, sf[i]); |
| 1144 | scaledh=TJSCALED(jpegheight, sf[i]); |
| 1145 | if(scaledw<=width && scaledh<=height) |
| 1146 | break; |
| 1147 | } |
| 1148 | if(scaledw>width || scaledh>height) |
| 1149 | _throw("tjDecompressToYUV2(): Could not scale down to desired image dimensions"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 1150 | if(dinfo->num_components>3) |
| 1151 | _throw("tjDecompressToYUV2(): JPEG image must have 3 or fewer components"); |
| 1152 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1153 | width=scaledw; height=scaledh; |
| 1154 | dinfo->scale_num=sf[i].num; |
| 1155 | dinfo->scale_denom=sf[i].denom; |
| 1156 | sfi=i; |
| 1157 | jpeg_calc_output_dimensions(dinfo); |
| 1158 | |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1159 | dctsize=DCTSIZE*sf[sfi].num/sf[sfi].denom; |
| 1160 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1161 | for(i=0; i<dinfo->num_components; i++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1162 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1163 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
| 1164 | int ih; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1165 | iw[i]=compptr->width_in_blocks*dctsize; |
| 1166 | ih=compptr->height_in_blocks*dctsize; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1167 | cw[i]=PAD(dinfo->output_width, dinfo->max_h_samp_factor) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1168 | *compptr->h_samp_factor/dinfo->max_h_samp_factor; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1169 | ch[i]=PAD(dinfo->output_height, dinfo->max_v_samp_factor) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1170 | *compptr->v_samp_factor/dinfo->max_v_samp_factor; |
| 1171 | if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1172 | th[i]=compptr->v_samp_factor*dctsize; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1173 | tmpbufsize+=iw[i]*th[i]; |
| 1174 | if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1175 | _throw("tjDecompressToYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1176 | for(row=0; row<ch[i]; row++) |
| 1177 | { |
| 1178 | outbuf[i][row]=ptr; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1179 | ptr+=PAD(cw[i], pad); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | if(usetmpbuf) |
| 1183 | { |
| 1184 | if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1185 | _throw("tjDecompressToYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1186 | ptr=_tmpbuf; |
| 1187 | for(i=0; i<dinfo->num_components; i++) |
| 1188 | { |
| 1189 | if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL) |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1190 | _throw("tjDecompressToYUV2(): Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1191 | for(row=0; row<th[i]; row++) |
| 1192 | { |
| 1193 | tmpbuf[i][row]=ptr; |
| 1194 | ptr+=iw[i]; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1198 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1199 | if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE; |
DRC | e0419b5 | 2012-07-03 20:01:31 +0000 | [diff] [blame] | 1200 | if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1201 | dinfo->raw_data_out=TRUE; |
| 1202 | |
| 1203 | jpeg_start_decompress(dinfo); |
| 1204 | for(row=0; row<(int)dinfo->output_height; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1205 | row+=dinfo->max_v_samp_factor*dinfo->_min_DCT_scaled_size) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1206 | { |
| 1207 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
| 1208 | int crow[MAX_COMPONENTS]; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1209 | for(i=0; i<dinfo->num_components; i++) |
| 1210 | { |
| 1211 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1212 | if(jpegSubsamp==TJ_420) |
| 1213 | { |
| 1214 | /* When 4:2:0 subsampling is used with IDCT scaling, libjpeg will try |
| 1215 | to be clever and use the IDCT to perform upsampling on the U and V |
| 1216 | planes. For instance, if the output image is to be scaled by 1/2 |
| 1217 | relative to the JPEG image, then the scaling factor and upsampling |
| 1218 | effectively cancel each other, so a normal 8x8 IDCT can be used. |
| 1219 | However, this is not desirable when using the decompress-to-YUV |
| 1220 | functionality in TurboJPEG, since we want to output the U and V |
| 1221 | planes in their subsampled form. Thus, we have to override some |
| 1222 | internal libjpeg parameters to force it to use the "scaled" IDCT |
| 1223 | functions on the U and V planes. */ |
| 1224 | compptr->_DCT_scaled_size=dctsize; |
| 1225 | compptr->MCU_sample_width=tjMCUWidth[jpegSubsamp]* |
| 1226 | sf[sfi].num/sf[sfi].denom* |
| 1227 | compptr->v_samp_factor/dinfo->max_v_samp_factor; |
| 1228 | dinfo->idct->inverse_DCT[i] = dinfo->idct->inverse_DCT[0]; |
| 1229 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1230 | crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor; |
| 1231 | if(usetmpbuf) yuvptr[i]=tmpbuf[i]; |
| 1232 | else yuvptr[i]=&outbuf[i][crow[i]]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1233 | } |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1234 | jpeg_read_raw_data(dinfo, yuvptr, |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 1235 | dinfo->max_v_samp_factor*dinfo->_min_DCT_scaled_size); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1236 | if(usetmpbuf) |
| 1237 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1238 | int j; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1239 | for(i=0; i<dinfo->num_components; i++) |
| 1240 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1241 | for(j=0; j<min(th[i], ch[i]-crow[i]); j++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1242 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1243 | memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1244 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1248 | jpeg_finish_decompress(dinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1249 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1250 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1251 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1252 | for(i=0; i<MAX_COMPONENTS; i++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1253 | { |
| 1254 | if(tmpbuf[i]) free(tmpbuf[i]); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 1255 | if(outbuf[i]) free(outbuf[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 1256 | } |
| 1257 | if(_tmpbuf) free(_tmpbuf); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 1258 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 1261 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle, |
| 1262 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 1263 | int flags) |
| 1264 | { |
| 1265 | return tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, 0, 4, 0, flags); |
| 1266 | } |
| 1267 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1268 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1269 | /* Transformer */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1270 | |
| 1271 | DLLEXPORT tjhandle DLLCALL tjInitTransform(void) |
| 1272 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1273 | tjinstance *this=NULL; tjhandle handle=NULL; |
| 1274 | if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL) |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 1275 | { |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1276 | snprintf(errStr, JMSG_LENGTH_MAX, |
| 1277 | "tjInitTransform(): Memory allocation failure"); |
DRC | da5220a | 2011-03-02 02:17:30 +0000 | [diff] [blame] | 1278 | return NULL; |
| 1279 | } |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1280 | MEMZERO(this, sizeof(tjinstance)); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1281 | handle=_tjInitCompress(this); |
| 1282 | if(!handle) return NULL; |
| 1283 | handle=_tjInitDecompress(this); |
| 1284 | return handle; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1288 | DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf, |
| 1289 | unsigned long jpegSize, int n, unsigned char **dstBufs, |
| 1290 | unsigned long *dstSizes, tjtransform *t, int flags) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1291 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1292 | jpeg_transform_info *xinfo=NULL; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1293 | jvirt_barray_ptr *srccoefs, *dstcoefs; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 1294 | int retval=0, i, jpegSubsamp; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1295 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1296 | getinstance(handle); |
| 1297 | if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1298 | _throw("tjTransform(): Instance has not been initialized for transformation"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1299 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1300 | if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL |
| 1301 | || t==NULL || flags<0) |
| 1302 | _throw("tjTransform(): Invalid argument"); |
| 1303 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1304 | if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 1305 | else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 1306 | else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1307 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1308 | if(setjmp(this->jerr.setjmp_buffer)) |
| 1309 | { |
| 1310 | /* If we get here, the JPEG code has signaled an error. */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1311 | retval=-1; |
| 1312 | goto bailout; |
| 1313 | } |
| 1314 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1315 | jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1316 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1317 | if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n)) |
| 1318 | ==NULL) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1319 | _throw("tjTransform(): Memory allocation failure"); |
| 1320 | MEMZERO(xinfo, sizeof(jpeg_transform_info)*n); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1321 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1322 | for(i=0; i<n; i++) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1323 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1324 | xinfo[i].transform=xformtypes[t[i].op]; |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 1325 | xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0; |
| 1326 | xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0; |
| 1327 | xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0; |
| 1328 | xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0; |
| 1329 | if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1; |
DRC | ba5ea51 | 2011-03-04 03:20:34 +0000 | [diff] [blame] | 1330 | else xinfo[i].slow_hflip=0; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1331 | |
| 1332 | if(xinfo[i].crop) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1333 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1334 | xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS; |
| 1335 | xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS; |
| 1336 | if(t[i].r.w!=0) |
| 1337 | { |
| 1338 | xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS; |
| 1339 | } |
DRC | d932e58 | 2011-03-15 20:09:47 +0000 | [diff] [blame] | 1340 | else xinfo[i].crop_width=JCROP_UNSET; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1341 | if(t[i].r.h!=0) |
| 1342 | { |
| 1343 | xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS; |
| 1344 | } |
DRC | d932e58 | 2011-03-15 20:09:47 +0000 | [diff] [blame] | 1345 | else xinfo[i].crop_height=JCROP_UNSET; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1349 | jcopy_markers_setup(dinfo, JCOPYOPT_ALL); |
| 1350 | jpeg_read_header(dinfo, TRUE); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 1351 | jpegSubsamp=getSubsamp(dinfo); |
| 1352 | if(jpegSubsamp<0) |
| 1353 | _throw("tjTransform(): Could not determine subsampling type for JPEG image"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1354 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1355 | for(i=0; i<n; i++) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1356 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1357 | if(!jtransform_request_workspace(dinfo, &xinfo[i])) |
DRC | 007a42c | 2011-05-22 13:55:56 +0000 | [diff] [blame] | 1358 | _throw("tjTransform(): Transform is not perfect"); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1359 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1360 | if(xinfo[i].crop) |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1361 | { |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1362 | if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0 |
| 1363 | || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0) |
| 1364 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1365 | snprintf(errStr, JMSG_LENGTH_MAX, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1366 | "To crop this JPEG image, x must be a multiple of %d\n" |
| 1367 | "and y must be a multiple of %d.\n", |
| 1368 | xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height); |
| 1369 | retval=-1; goto bailout; |
| 1370 | } |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1374 | srccoefs=jpeg_read_coefficients(dinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1375 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1376 | for(i=0; i<n; i++) |
| 1377 | { |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 1378 | int w, h, alloc=1; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1379 | if(!xinfo[i].crop) |
| 1380 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1381 | w=dinfo->image_width; h=dinfo->image_height; |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1382 | } |
| 1383 | else |
| 1384 | { |
| 1385 | w=xinfo[i].crop_width; h=xinfo[i].crop_height; |
| 1386 | } |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 1387 | if(flags&TJFLAG_NOREALLOC) |
| 1388 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 1389 | alloc=0; dstSizes[i]=tjBufSize(w, h, jpegSubsamp); |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 1390 | } |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 1391 | if(!(t[i].options&TJXOPT_NOOUTPUT)) |
| 1392 | jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1393 | jpeg_copy_critical_parameters(dinfo, cinfo); |
| 1394 | dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1395 | &xinfo[i]); |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 1396 | if(!(t[i].options&TJXOPT_NOOUTPUT)) |
| 1397 | { |
| 1398 | jpeg_write_coefficients(cinfo, dstcoefs); |
| 1399 | jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL); |
| 1400 | } |
| 1401 | else jinit_c_master_control(cinfo, TRUE); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1402 | jtransform_execute_transformation(dinfo, cinfo, srccoefs, |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1403 | &xinfo[i]); |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 1404 | if(t[i].customFilter) |
| 1405 | { |
DRC | efe28ce | 2012-01-17 11:48:38 +0000 | [diff] [blame] | 1406 | int ci, y; JDIMENSION by; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 1407 | for(ci=0; ci<cinfo->num_components; ci++) |
| 1408 | { |
| 1409 | jpeg_component_info *compptr=&cinfo->comp_info[ci]; |
| 1410 | tjregion arrayRegion={0, 0, compptr->width_in_blocks*DCTSIZE, |
| 1411 | DCTSIZE}; |
| 1412 | tjregion planeRegion={0, 0, compptr->width_in_blocks*DCTSIZE, |
| 1413 | compptr->height_in_blocks*DCTSIZE}; |
| 1414 | for(by=0; by<compptr->height_in_blocks; by+=compptr->v_samp_factor) |
| 1415 | { |
| 1416 | JBLOCKARRAY barray=(dinfo->mem->access_virt_barray) |
| 1417 | ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor, |
| 1418 | TRUE); |
| 1419 | for(y=0; y<compptr->v_samp_factor; y++) |
| 1420 | { |
| 1421 | if(t[i].customFilter(barray[y][0], arrayRegion, planeRegion, |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1422 | ci, i, &t[i])==-1) |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 1423 | _throw("tjTransform(): Error in custom filter"); |
| 1424 | arrayRegion.y+=DCTSIZE; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | if(!(t[i].options&TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo); |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1432 | jpeg_finish_decompress(dinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1433 | |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1434 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1435 | if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); |
| 1436 | if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 1437 | if(xinfo) free(xinfo); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 1438 | return retval; |
| 1439 | } |