image_pack 0.2.2 → 0.2.3

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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/README.md +23 -4
  4. data/ext/image_pack/extconf.rb +35 -124
  5. data/ext/image_pack/image_pack.c +638 -595
  6. data/ext/image_pack/mozjpeg_sources.rb +178 -0
  7. data/ext/image_pack/vendor/mozjpeg/BUILDING.md +744 -0
  8. data/ext/image_pack/vendor/mozjpeg/CODE_OF_CONDUCT.md +15 -0
  9. data/ext/image_pack/vendor/mozjpeg/ChangeLog.md +1996 -0
  10. data/lib/image_pack/configuration.rb +54 -8
  11. data/lib/image_pack/version.rb +1 -1
  12. data/lib/image_pack.rb +65 -18
  13. metadata +13 -78
  14. data/ext/image_pack/vendor/mozjpeg/README.ijg +0 -258
  15. data/ext/image_pack/vendor/mozjpeg/cdjpeg.c +0 -156
  16. data/ext/image_pack/vendor/mozjpeg/cjpeg.c +0 -961
  17. data/ext/image_pack/vendor/mozjpeg/djpeg.c +0 -855
  18. data/ext/image_pack/vendor/mozjpeg/jaricom.c +0 -157
  19. data/ext/image_pack/vendor/mozjpeg/jcarith.c +0 -972
  20. data/ext/image_pack/vendor/mozjpeg/jcstest.c +0 -126
  21. data/ext/image_pack/vendor/mozjpeg/jdarith.c +0 -782
  22. data/ext/image_pack/vendor/mozjpeg/jdatadst-tj.c +0 -198
  23. data/ext/image_pack/vendor/mozjpeg/jdatasrc-tj.c +0 -194
  24. data/ext/image_pack/vendor/mozjpeg/jpegtran.c +0 -827
  25. data/ext/image_pack/vendor/mozjpeg/jpegyuv.c +0 -172
  26. data/ext/image_pack/vendor/mozjpeg/rdbmp.c +0 -690
  27. data/ext/image_pack/vendor/mozjpeg/rdcolmap.c +0 -253
  28. data/ext/image_pack/vendor/mozjpeg/rdgif.c +0 -720
  29. data/ext/image_pack/vendor/mozjpeg/rdjpeg.c +0 -160
  30. data/ext/image_pack/vendor/mozjpeg/rdjpgcom.c +0 -494
  31. data/ext/image_pack/vendor/mozjpeg/rdpng.c +0 -194
  32. data/ext/image_pack/vendor/mozjpeg/rdppm.c +0 -781
  33. data/ext/image_pack/vendor/mozjpeg/rdswitch.c +0 -642
  34. data/ext/image_pack/vendor/mozjpeg/rdtarga.c +0 -508
  35. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jccolext-neon.c +0 -148
  36. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jchuff-neon.c +0 -334
  37. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jsimd.c +0 -976
  38. data/ext/image_pack/vendor/mozjpeg/simd/i386/jsimd.c +0 -1312
  39. data/ext/image_pack/vendor/mozjpeg/simd/mips/jsimd.c +0 -1143
  40. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jccolext-mmi.c +0 -455
  41. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jccolor-mmi.c +0 -148
  42. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcgray-mmi.c +0 -132
  43. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcgryext-mmi.c +0 -374
  44. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcsample-mmi.c +0 -98
  45. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdcolext-mmi.c +0 -415
  46. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdcolor-mmi.c +0 -139
  47. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdmerge-mmi.c +0 -149
  48. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdmrgext-mmi.c +0 -615
  49. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdsample-mmi.c +0 -304
  50. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jfdctfst-mmi.c +0 -255
  51. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jfdctint-mmi.c +0 -398
  52. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jidctfst-mmi.c +0 -395
  53. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jidctint-mmi.c +0 -571
  54. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jquanti-mmi.c +0 -124
  55. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jsimd.c +0 -866
  56. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jccolext-altivec.c +0 -269
  57. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jccolor-altivec.c +0 -116
  58. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcgray-altivec.c +0 -111
  59. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcgryext-altivec.c +0 -228
  60. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcsample-altivec.c +0 -159
  61. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdcolext-altivec.c +0 -276
  62. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdcolor-altivec.c +0 -106
  63. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdmerge-altivec.c +0 -130
  64. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdmrgext-altivec.c +0 -329
  65. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdsample-altivec.c +0 -400
  66. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jfdctfst-altivec.c +0 -154
  67. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jfdctint-altivec.c +0 -258
  68. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jidctfst-altivec.c +0 -255
  69. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jidctint-altivec.c +0 -357
  70. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jquanti-altivec.c +0 -250
  71. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jsimd.c +0 -884
  72. data/ext/image_pack/vendor/mozjpeg/strtest.c +0 -170
  73. data/ext/image_pack/vendor/mozjpeg/tjbench.c +0 -1044
  74. data/ext/image_pack/vendor/mozjpeg/tjexample.c +0 -406
  75. data/ext/image_pack/vendor/mozjpeg/tjunittest.c +0 -961
  76. data/ext/image_pack/vendor/mozjpeg/tjutil.c +0 -70
  77. data/ext/image_pack/vendor/mozjpeg/transupp.c +0 -2373
  78. data/ext/image_pack/vendor/mozjpeg/turbojpeg-jni.c +0 -1259
  79. data/ext/image_pack/vendor/mozjpeg/turbojpeg.c +0 -2320
  80. data/ext/image_pack/vendor/mozjpeg/wrbmp.c +0 -552
  81. data/ext/image_pack/vendor/mozjpeg/wrgif.c +0 -580
  82. data/ext/image_pack/vendor/mozjpeg/wrjpgcom.c +0 -577
  83. data/ext/image_pack/vendor/mozjpeg/wrppm.c +0 -366
  84. data/ext/image_pack/vendor/mozjpeg/wrtarga.c +0 -258
  85. data/ext/image_pack/vendor/mozjpeg/yuvjpeg.c +0 -268
@@ -1,406 +0,0 @@
1
- /*
2
- * Copyright (C)2011-2012, 2014-2015, 2017, 2019, 2021-2023
3
- * D. R. Commander. All Rights Reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * - Redistributions of source code must retain the above copyright notice,
9
- * this list of conditions and the following disclaimer.
10
- * - Redistributions in binary form must reproduce the above copyright notice,
11
- * this list of conditions and the following disclaimer in the documentation
12
- * and/or other materials provided with the distribution.
13
- * - Neither the name of the libjpeg-turbo Project nor the names of its
14
- * contributors may be used to endorse or promote products derived from this
15
- * software without specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- * POSSIBILITY OF SUCH DAMAGE.
28
- */
29
-
30
- /*
31
- * This program demonstrates how to compress, decompress, and transform JPEG
32
- * images using the TurboJPEG C API
33
- */
34
-
35
- #ifdef _MSC_VER
36
- #define _CRT_SECURE_NO_DEPRECATE
37
- #endif
38
-
39
- #include <limits.h>
40
- #include <stdio.h>
41
- #include <stdlib.h>
42
- #include <string.h>
43
- #include <errno.h>
44
- #include <turbojpeg.h>
45
-
46
-
47
- #ifdef _WIN32
48
- #define strcasecmp stricmp
49
- #define strncasecmp strnicmp
50
- #endif
51
-
52
- #define THROW(action, message) { \
53
- printf("ERROR in line %d while %s:\n%s\n", __LINE__, action, message); \
54
- retval = -1; goto bailout; \
55
- }
56
-
57
- #define THROW_TJ(action) THROW(action, tjGetErrorStr2(tjInstance))
58
-
59
- #define THROW_UNIX(action) THROW(action, strerror(errno))
60
-
61
- #define DEFAULT_SUBSAMP TJSAMP_444
62
- #define DEFAULT_QUALITY 95
63
-
64
-
65
- const char *subsampName[TJ_NUMSAMP] = {
66
- "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1"
67
- };
68
-
69
- const char *colorspaceName[TJ_NUMCS] = {
70
- "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
71
- };
72
-
73
- tjscalingfactor *scalingFactors = NULL;
74
- int numScalingFactors = 0;
75
-
76
-
77
- /* DCT filter example. This produces a negative of the image. */
78
-
79
- static int customFilter(short *coeffs, tjregion arrayRegion,
80
- tjregion planeRegion, int componentIndex,
81
- int transformIndex, tjtransform *transform)
82
- {
83
- int i;
84
-
85
- for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
86
- coeffs[i] = -coeffs[i];
87
-
88
- return 0;
89
- }
90
-
91
-
92
- static void usage(char *programName)
93
- {
94
- int i;
95
-
96
- printf("\nUSAGE: %s <Input image> <Output image> [options]\n\n",
97
- programName);
98
-
99
- printf("Input and output images can be in Windows BMP or PBMPLUS (PPM/PGM) format. If\n");
100
- printf("either filename ends in a .jpg extension, then the TurboJPEG API will be used\n");
101
- printf("to compress or decompress the image.\n\n");
102
-
103
- printf("Compression Options (used if the output image is a JPEG image)\n");
104
- printf("--------------------------------------------------------------\n\n");
105
-
106
- printf("-subsamp <444|422|420|gray> = Apply this level of chrominance subsampling when\n");
107
- printf(" compressing the output image. The default is to use the same level of\n");
108
- printf(" subsampling as in the input image, if the input image is also a JPEG\n");
109
- printf(" image, or to use grayscale if the input image is a grayscale non-JPEG\n");
110
- printf(" image, or to use %s subsampling otherwise.\n\n",
111
- subsampName[DEFAULT_SUBSAMP]);
112
-
113
- printf("-q <1-100> = Compress the output image with this JPEG quality level\n");
114
- printf(" (default = %d).\n\n", DEFAULT_QUALITY);
115
-
116
- printf("Decompression Options (used if the input image is a JPEG image)\n");
117
- printf("---------------------------------------------------------------\n\n");
118
-
119
- printf("-scale M/N = Scale the input image by a factor of M/N when decompressing it.\n");
120
- printf("(M/N = ");
121
- for (i = 0; i < numScalingFactors; i++) {
122
- printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
123
- if (numScalingFactors == 2 && i != numScalingFactors - 1)
124
- printf(" or ");
125
- else if (numScalingFactors > 2) {
126
- if (i != numScalingFactors - 1)
127
- printf(", ");
128
- if (i == numScalingFactors - 2)
129
- printf("or ");
130
- }
131
- }
132
- printf(")\n\n");
133
-
134
- printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
135
- printf(" Perform one of these lossless transform operations on the input image\n");
136
- printf(" prior to decompressing it (these options are mutually exclusive.)\n\n");
137
-
138
- printf("-grayscale = Perform lossless grayscale conversion on the input image prior\n");
139
- printf(" to decompressing it (can be combined with the other transform operations\n");
140
- printf(" above.)\n\n");
141
-
142
- printf("-crop WxH+X+Y = Perform lossless cropping on the input image prior to\n");
143
- printf(" decompressing it. X and Y specify the upper left corner of the cropping\n");
144
- printf(" region, and W and H specify the width and height of the cropping region.\n");
145
- printf(" X and Y must be evenly divible by the MCU block size (8x8 if the input\n");
146
- printf(" image was compressed using no subsampling or grayscale, 16x8 if it was\n");
147
- printf(" compressed using 4:2:2 subsampling, or 16x16 if it was compressed using\n");
148
- printf(" 4:2:0 subsampling.)\n\n");
149
-
150
- printf("General Options\n");
151
- printf("---------------\n\n");
152
-
153
- printf("-fastupsample = Use the fastest chrominance upsampling algorithm available\n\n");
154
-
155
- printf("-fastdct = Use the fastest DCT/IDCT algorithm available\n\n");
156
-
157
- printf("-accuratedct = Use the most accurate DCT/IDCT algorithm available\n\n");
158
-
159
- exit(1);
160
- }
161
-
162
-
163
- int main(int argc, char **argv)
164
- {
165
- tjscalingfactor scalingFactor = { 1, 1 };
166
- int outSubsamp = -1, outQual = -1;
167
- tjtransform xform;
168
- int flags = 0;
169
- int width, height;
170
- char *inFormat, *outFormat;
171
- FILE *jpegFile = NULL;
172
- unsigned char *imgBuf = NULL, *jpegBuf = NULL;
173
- int retval = 0, i, pixelFormat = TJPF_UNKNOWN;
174
- tjhandle tjInstance = NULL;
175
-
176
- if ((scalingFactors = tjGetScalingFactors(&numScalingFactors)) == NULL)
177
- THROW_TJ("getting scaling factors");
178
- memset(&xform, 0, sizeof(tjtransform));
179
-
180
- if (argc < 3)
181
- usage(argv[0]);
182
-
183
- /* Parse arguments. */
184
- for (i = 3; i < argc; i++) {
185
- if (!strncasecmp(argv[i], "-sc", 3) && i < argc - 1) {
186
- int match = 0, temp1 = 0, temp2 = 0, j;
187
-
188
- if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) < 2)
189
- usage(argv[0]);
190
- for (j = 0; j < numScalingFactors; j++) {
191
- if ((double)temp1 / (double)temp2 == (double)scalingFactors[j].num /
192
- (double)scalingFactors[j].denom) {
193
- scalingFactor = scalingFactors[j];
194
- match = 1;
195
- break;
196
- }
197
- }
198
- if (match != 1)
199
- usage(argv[0]);
200
- } else if (!strncasecmp(argv[i], "-su", 3) && i < argc - 1) {
201
- i++;
202
- if (!strncasecmp(argv[i], "g", 1))
203
- outSubsamp = TJSAMP_GRAY;
204
- else if (!strcasecmp(argv[i], "444"))
205
- outSubsamp = TJSAMP_444;
206
- else if (!strcasecmp(argv[i], "422"))
207
- outSubsamp = TJSAMP_422;
208
- else if (!strcasecmp(argv[i], "420"))
209
- outSubsamp = TJSAMP_420;
210
- else
211
- usage(argv[0]);
212
- } else if (!strncasecmp(argv[i], "-q", 2) && i < argc - 1) {
213
- outQual = atoi(argv[++i]);
214
- if (outQual < 1 || outQual > 100)
215
- usage(argv[0]);
216
- } else if (!strncasecmp(argv[i], "-g", 2))
217
- xform.options |= TJXOPT_GRAY;
218
- else if (!strcasecmp(argv[i], "-hflip"))
219
- xform.op = TJXOP_HFLIP;
220
- else if (!strcasecmp(argv[i], "-vflip"))
221
- xform.op = TJXOP_VFLIP;
222
- else if (!strcasecmp(argv[i], "-transpose"))
223
- xform.op = TJXOP_TRANSPOSE;
224
- else if (!strcasecmp(argv[i], "-transverse"))
225
- xform.op = TJXOP_TRANSVERSE;
226
- else if (!strcasecmp(argv[i], "-rot90"))
227
- xform.op = TJXOP_ROT90;
228
- else if (!strcasecmp(argv[i], "-rot180"))
229
- xform.op = TJXOP_ROT180;
230
- else if (!strcasecmp(argv[i], "-rot270"))
231
- xform.op = TJXOP_ROT270;
232
- else if (!strcasecmp(argv[i], "-custom"))
233
- xform.customFilter = customFilter;
234
- else if (!strncasecmp(argv[i], "-c", 2) && i < argc - 1) {
235
- if (sscanf(argv[++i], "%dx%d+%d+%d", &xform.r.w, &xform.r.h, &xform.r.x,
236
- &xform.r.y) < 4 ||
237
- xform.r.x < 0 || xform.r.y < 0 || xform.r.w < 1 || xform.r.h < 1)
238
- usage(argv[0]);
239
- xform.options |= TJXOPT_CROP;
240
- } else if (!strcasecmp(argv[i], "-fastupsample")) {
241
- printf("Using fast upsampling code\n");
242
- flags |= TJFLAG_FASTUPSAMPLE;
243
- } else if (!strcasecmp(argv[i], "-fastdct")) {
244
- printf("Using fastest DCT/IDCT algorithm\n");
245
- flags |= TJFLAG_FASTDCT;
246
- } else if (!strcasecmp(argv[i], "-accuratedct")) {
247
- printf("Using most accurate DCT/IDCT algorithm\n");
248
- flags |= TJFLAG_ACCURATEDCT;
249
- } else usage(argv[0]);
250
- }
251
-
252
- /* Determine input and output image formats based on file extensions. */
253
- inFormat = strrchr(argv[1], '.');
254
- outFormat = strrchr(argv[2], '.');
255
- if (inFormat == NULL || outFormat == NULL || strlen(inFormat) < 2 ||
256
- strlen(outFormat) < 2)
257
- usage(argv[0]);
258
- inFormat = &inFormat[1];
259
- outFormat = &outFormat[1];
260
-
261
- if (!strcasecmp(inFormat, "jpg")) {
262
- /* Input image is a JPEG image. Decompress and/or transform it. */
263
- long size;
264
- int inSubsamp, inColorspace;
265
- int doTransform = (xform.op != TJXOP_NONE || xform.options != 0 ||
266
- xform.customFilter != NULL);
267
- unsigned long jpegSize;
268
-
269
- /* Read the JPEG file into memory. */
270
- if ((jpegFile = fopen(argv[1], "rb")) == NULL)
271
- THROW_UNIX("opening input file");
272
- if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) ||
273
- fseek(jpegFile, 0, SEEK_SET) < 0)
274
- THROW_UNIX("determining input file size");
275
- if (size == 0)
276
- THROW("determining input file size", "Input file contains no data");
277
- jpegSize = (unsigned long)size;
278
- if (jpegSize > (unsigned long)INT_MAX)
279
- THROW("allocating JPEG buffer", "Input file is too large");
280
- if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL)
281
- THROW_UNIX("allocating JPEG buffer");
282
- if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
283
- THROW_UNIX("reading input file");
284
- fclose(jpegFile); jpegFile = NULL;
285
-
286
- if (doTransform) {
287
- /* Transform it. */
288
- unsigned char *dstBuf = NULL; /* Dynamically allocate the JPEG buffer */
289
- unsigned long dstSize = 0;
290
-
291
- if ((tjInstance = tjInitTransform()) == NULL)
292
- THROW_TJ("initializing transformer");
293
- xform.options |= TJXOPT_TRIM;
294
- if (tjTransform(tjInstance, jpegBuf, jpegSize, 1, &dstBuf, &dstSize,
295
- &xform, flags) < 0) {
296
- tjFree(dstBuf);
297
- THROW_TJ("transforming input image");
298
- }
299
- tjFree(jpegBuf);
300
- jpegBuf = dstBuf;
301
- jpegSize = dstSize;
302
- } else {
303
- if ((tjInstance = tjInitDecompress()) == NULL)
304
- THROW_TJ("initializing decompressor");
305
- }
306
-
307
- if (tjDecompressHeader3(tjInstance, jpegBuf, jpegSize, &width, &height,
308
- &inSubsamp, &inColorspace) < 0)
309
- THROW_TJ("reading JPEG header");
310
-
311
- printf("%s Image: %d x %d pixels, %s subsampling, %s colorspace\n",
312
- (doTransform ? "Transformed" : "Input"), width, height,
313
- subsampName[inSubsamp], colorspaceName[inColorspace]);
314
-
315
- if (!strcasecmp(outFormat, "jpg") && doTransform &&
316
- scalingFactor.num == 1 && scalingFactor.denom == 1 && outSubsamp < 0 &&
317
- outQual < 0) {
318
- /* Input image has been transformed, and no re-compression options
319
- have been selected. Write the transformed image to disk and exit. */
320
- if ((jpegFile = fopen(argv[2], "wb")) == NULL)
321
- THROW_UNIX("opening output file");
322
- if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
323
- THROW_UNIX("writing output file");
324
- fclose(jpegFile); jpegFile = NULL;
325
- goto bailout;
326
- }
327
-
328
- /* Scaling and/or a non-JPEG output image format and/or compression options
329
- have been selected, so we need to decompress the input/transformed
330
- image. */
331
- width = TJSCALED(width, scalingFactor);
332
- height = TJSCALED(height, scalingFactor);
333
- if (outSubsamp < 0)
334
- outSubsamp = inSubsamp;
335
-
336
- pixelFormat = TJPF_BGRX;
337
- if ((unsigned long long)width * height * tjPixelSize[pixelFormat] >
338
- (unsigned long long)((size_t)-1))
339
- THROW("allocating uncompressed image buffer", "Image is too large");
340
- if ((imgBuf =
341
- (unsigned char *)malloc(sizeof(unsigned char) * width * height *
342
- tjPixelSize[pixelFormat])) == NULL)
343
- THROW_UNIX("allocating uncompressed image buffer");
344
-
345
- if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height,
346
- pixelFormat, flags) < 0)
347
- THROW_TJ("decompressing JPEG image");
348
- tjFree(jpegBuf); jpegBuf = NULL;
349
- tjDestroy(tjInstance); tjInstance = NULL;
350
- } else {
351
- /* Input image is not a JPEG image. Load it into memory. */
352
- if ((imgBuf = tjLoadImage(argv[1], &width, 1, &height, &pixelFormat,
353
- 0)) == NULL)
354
- THROW_TJ("loading input image");
355
- if (outSubsamp < 0) {
356
- if (pixelFormat == TJPF_GRAY)
357
- outSubsamp = TJSAMP_GRAY;
358
- else
359
- outSubsamp = TJSAMP_444;
360
- }
361
- printf("Input Image: %d x %d pixels\n", width, height);
362
- }
363
-
364
- printf("Output Image (%s): %d x %d pixels", outFormat, width, height);
365
-
366
- if (!strcasecmp(outFormat, "jpg")) {
367
- /* Output image format is JPEG. Compress the uncompressed image. */
368
- unsigned long jpegSize = 0;
369
-
370
- jpegBuf = NULL; /* Dynamically allocate the JPEG buffer */
371
-
372
- if (outQual < 0)
373
- outQual = DEFAULT_QUALITY;
374
- printf(", %s subsampling, quality = %d\n", subsampName[outSubsamp],
375
- outQual);
376
-
377
- if ((tjInstance = tjInitCompress()) == NULL)
378
- THROW_TJ("initializing compressor");
379
- if (tjCompress2(tjInstance, imgBuf, width, 0, height, pixelFormat,
380
- &jpegBuf, &jpegSize, outSubsamp, outQual, flags) < 0)
381
- THROW_TJ("compressing image");
382
- tjDestroy(tjInstance); tjInstance = NULL;
383
-
384
- /* Write the JPEG image to disk. */
385
- if ((jpegFile = fopen(argv[2], "wb")) == NULL)
386
- THROW_UNIX("opening output file");
387
- if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
388
- THROW_UNIX("writing output file");
389
- tjDestroy(tjInstance); tjInstance = NULL;
390
- fclose(jpegFile); jpegFile = NULL;
391
- tjFree(jpegBuf); jpegBuf = NULL;
392
- } else {
393
- /* Output image format is not JPEG. Save the uncompressed image
394
- directly to disk. */
395
- printf("\n");
396
- if (tjSaveImage(argv[2], imgBuf, width, 0, height, pixelFormat, 0) < 0)
397
- THROW_TJ("saving output image");
398
- }
399
-
400
- bailout:
401
- tjFree(imgBuf);
402
- if (tjInstance) tjDestroy(tjInstance);
403
- tjFree(jpegBuf);
404
- if (jpegFile) fclose(jpegFile);
405
- return retval;
406
- }