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