image_intensities 1.0.3 → 1.0.4

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