|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C)2011-2012, 2014-2015, 2017, 2019 D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* All Rights Reserved.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Redistribution and use in source and binary forms, with or without
|
|
shun-iwasawa |
82a8f5 |
* modification, are permitted provided that the following conditions are met:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* - Redistributions of source code must retain the above copyright notice,
|
|
shun-iwasawa |
82a8f5 |
* this list of conditions and the following disclaimer.
|
|
shun-iwasawa |
82a8f5 |
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
shun-iwasawa |
82a8f5 |
* this list of conditions and the following disclaimer in the documentation
|
|
shun-iwasawa |
82a8f5 |
* and/or other materials provided with the distribution.
|
|
shun-iwasawa |
82a8f5 |
* - Neither the name of the libjpeg-turbo Project nor the names of its
|
|
shun-iwasawa |
82a8f5 |
* contributors may be used to endorse or promote products derived from this
|
|
shun-iwasawa |
82a8f5 |
* software without specific prior written permission.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
|
|
shun-iwasawa |
82a8f5 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
shun-iwasawa |
82a8f5 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
shun-iwasawa |
82a8f5 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
|
shun-iwasawa |
82a8f5 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
shun-iwasawa |
82a8f5 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
shun-iwasawa |
82a8f5 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
shun-iwasawa |
82a8f5 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
shun-iwasawa |
82a8f5 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
shun-iwasawa |
82a8f5 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
shun-iwasawa |
82a8f5 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* This program demonstrates how to compress, decompress, and transform JPEG
|
|
shun-iwasawa |
82a8f5 |
* images using the TurboJPEG C API
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#include <stdio.h></stdio.h>
|
|
shun-iwasawa |
82a8f5 |
#include <stdlib.h></stdlib.h>
|
|
shun-iwasawa |
82a8f5 |
#include <string.h></string.h>
|
|
shun-iwasawa |
82a8f5 |
#include <errno.h></errno.h>
|
|
shun-iwasawa |
82a8f5 |
#include <turbojpeg.h></turbojpeg.h>
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef _WIN32
|
|
shun-iwasawa |
82a8f5 |
#define strcasecmp stricmp
|
|
shun-iwasawa |
82a8f5 |
#define strncasecmp strnicmp
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define THROW(action, message) { \
|
|
shun-iwasawa |
82a8f5 |
printf("ERROR in line %d while %s:\n%s\n", __LINE__, action, message); \
|
|
shun-iwasawa |
82a8f5 |
retval = -1; goto bailout; \
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define THROW_TJ(action) THROW(action, tjGetErrorStr2(tjInstance))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define THROW_UNIX(action) THROW(action, strerror(errno))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define DEFAULT_SUBSAMP TJSAMP_444
|
|
shun-iwasawa |
82a8f5 |
#define DEFAULT_QUALITY 95
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
const char *subsampName[TJ_NUMSAMP] = {
|
|
shun-iwasawa |
82a8f5 |
"4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1"
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
const char *colorspaceName[TJ_NUMCS] = {
|
|
shun-iwasawa |
82a8f5 |
"RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
tjscalingfactor *scalingFactors = NULL;
|
|
shun-iwasawa |
82a8f5 |
int numScalingFactors = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* DCT filter example. This produces a negative of the image. */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static int customFilter(short *coeffs, tjregion arrayRegion,
|
|
shun-iwasawa |
82a8f5 |
tjregion planeRegion, int componentIndex,
|
|
shun-iwasawa |
82a8f5 |
int transformIndex, tjtransform *transform)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
|
|
shun-iwasawa |
82a8f5 |
coeffs[i] = -coeffs[i];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static void usage(char *programName)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("\nUSAGE: %s <input image=""> <output image=""> [options]\n\n",</output>
|
|
shun-iwasawa |
82a8f5 |
programName);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("Input and output images can be in Windows BMP or PBMPLUS (PPM/PGM) format. If\n");
|
|
shun-iwasawa |
82a8f5 |
printf("either filename ends in a .jpg extension, then the TurboJPEG API will be used\n");
|
|
shun-iwasawa |
82a8f5 |
printf("to compress or decompress the image.\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("Compression Options (used if the output image is a JPEG image)\n");
|
|
shun-iwasawa |
82a8f5 |
printf("--------------------------------------------------------------\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-subsamp <444|422|420|gray> = Apply this level of chrominance subsampling when\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" compressing the output image. The default is to use the same level of\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" subsampling as in the input image, if the input image is also a JPEG\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" image, or to use grayscale if the input image is a grayscale non-JPEG\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" image, or to use %s subsampling otherwise.\n\n",
|
|
shun-iwasawa |
82a8f5 |
subsampName[DEFAULT_SUBSAMP]);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-q <1-100> = Compress the output image with this JPEG quality level\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" (default = %d).\n\n", DEFAULT_QUALITY);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("Decompression Options (used if the input image is a JPEG image)\n");
|
|
shun-iwasawa |
82a8f5 |
printf("---------------------------------------------------------------\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-scale M/N = Scale the input image by a factor of M/N when decompressing it.\n");
|
|
shun-iwasawa |
82a8f5 |
printf("(M/N = ");
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < numScalingFactors; i++) {
|
|
shun-iwasawa |
82a8f5 |
printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
|
|
shun-iwasawa |
82a8f5 |
if (numScalingFactors == 2 && i != numScalingFactors - 1)
|
|
shun-iwasawa |
82a8f5 |
printf(" or ");
|
|
shun-iwasawa |
82a8f5 |
else if (numScalingFactors > 2) {
|
|
shun-iwasawa |
82a8f5 |
if (i != numScalingFactors - 1)
|
|
shun-iwasawa |
82a8f5 |
printf(", ");
|
|
shun-iwasawa |
82a8f5 |
if (i == numScalingFactors - 2)
|
|
shun-iwasawa |
82a8f5 |
printf("or ");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
printf(")\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" Perform one of these lossless transform operations on the input image\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" prior to decompressing it (these options are mutually exclusive.)\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-grayscale = Perform lossless grayscale conversion on the input image prior\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" to decompressing it (can be combined with the other transform operations\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" above.)\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-crop WxH+X+Y = Perform lossless cropping on the input image prior to\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" decompressing it. X and Y specify the upper left corner of the cropping\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" region, and W and H specify the width and height of the cropping region.\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" X and Y must be evenly divible by the MCU block size (8x8 if the input\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" image was compressed using no subsampling or grayscale, 16x8 if it was\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" compressed using 4:2:2 subsampling, or 16x16 if it was compressed using\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" 4:2:0 subsampling.)\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("General Options\n");
|
|
shun-iwasawa |
82a8f5 |
printf("---------------\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" the underlying codec.\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" codec.\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
|
|
shun-iwasawa |
82a8f5 |
printf(" underlying codec.\n\n");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
exit(1);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
int main(int argc, char **argv)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
tjscalingfactor scalingFactor = { 1, 1 };
|
|
shun-iwasawa |
82a8f5 |
int outSubsamp = -1, outQual = -1;
|
|
shun-iwasawa |
82a8f5 |
tjtransform xform;
|
|
shun-iwasawa |
82a8f5 |
int flags = 0;
|
|
shun-iwasawa |
82a8f5 |
int width, height;
|
|
shun-iwasawa |
82a8f5 |
char *inFormat, *outFormat;
|
|
shun-iwasawa |
82a8f5 |
FILE *jpegFile = NULL;
|
|
shun-iwasawa |
82a8f5 |
unsigned char *imgBuf = NULL, *jpegBuf = NULL;
|
|
shun-iwasawa |
82a8f5 |
int retval = 0, i, pixelFormat = TJPF_UNKNOWN;
|
|
shun-iwasawa |
82a8f5 |
tjhandle tjInstance = NULL;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if ((scalingFactors = tjGetScalingFactors(&numScalingFactors)) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("getting scaling factors");
|
|
shun-iwasawa |
82a8f5 |
memset(&xform, 0, sizeof(tjtransform));
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (argc < 3)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Parse arguments. */
|
|
shun-iwasawa |
82a8f5 |
for (i = 3; i < argc; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (!strncasecmp(argv[i], "-sc", 3) && i < argc - 1) {
|
|
shun-iwasawa |
82a8f5 |
int match = 0, temp1 = 0, temp2 = 0, j;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) < 2)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
for (j = 0; j < numScalingFactors; j++) {
|
|
shun-iwasawa |
82a8f5 |
if ((double)temp1 / (double)temp2 == (double)scalingFactors[j].num /
|
|
shun-iwasawa |
82a8f5 |
(double)scalingFactors[j].denom) {
|
|
shun-iwasawa |
82a8f5 |
scalingFactor = scalingFactors[j];
|
|
shun-iwasawa |
82a8f5 |
match = 1;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (match != 1)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
} else if (!strncasecmp(argv[i], "-su", 3) && i < argc - 1) {
|
|
shun-iwasawa |
82a8f5 |
i++;
|
|
shun-iwasawa |
82a8f5 |
if (!strncasecmp(argv[i], "g", 1))
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_GRAY;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "444"))
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_444;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "422"))
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_422;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "420"))
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_420;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
} else if (!strncasecmp(argv[i], "-q", 2) && i < argc - 1) {
|
|
shun-iwasawa |
82a8f5 |
outQual = atoi(argv[++i]);
|
|
shun-iwasawa |
82a8f5 |
if (outQual < 1 || outQual > 100)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
} else if (!strncasecmp(argv[i], "-g", 2))
|
|
shun-iwasawa |
82a8f5 |
xform.options |= TJXOPT_GRAY;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-hflip"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_HFLIP;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-vflip"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_VFLIP;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-transpose"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_TRANSPOSE;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-transverse"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_TRANSVERSE;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-rot90"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_ROT90;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-rot180"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_ROT180;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-rot270"))
|
|
shun-iwasawa |
82a8f5 |
xform.op = TJXOP_ROT270;
|
|
shun-iwasawa |
82a8f5 |
else if (!strcasecmp(argv[i], "-custom"))
|
|
shun-iwasawa |
82a8f5 |
xform.customFilter = customFilter;
|
|
shun-iwasawa |
82a8f5 |
else if (!strncasecmp(argv[i], "-c", 2) && i < argc - 1) {
|
|
shun-iwasawa |
82a8f5 |
if (sscanf(argv[++i], "%dx%d+%d+%d", &xform.r.w, &xform.r.h, &xform.r.x,
|
|
shun-iwasawa |
82a8f5 |
&xform.r.y) < 4 ||
|
|
shun-iwasawa |
82a8f5 |
xform.r.x < 0 || xform.r.y < 0 || xform.r.w < 1 || xform.r.h < 1)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
xform.options |= TJXOPT_CROP;
|
|
shun-iwasawa |
82a8f5 |
} else if (!strcasecmp(argv[i], "-fastupsample")) {
|
|
shun-iwasawa |
82a8f5 |
printf("Using fast upsampling code\n");
|
|
shun-iwasawa |
82a8f5 |
flags |= TJFLAG_FASTUPSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
} else if (!strcasecmp(argv[i], "-fastdct")) {
|
|
shun-iwasawa |
82a8f5 |
printf("Using fastest DCT/IDCT algorithm\n");
|
|
shun-iwasawa |
82a8f5 |
flags |= TJFLAG_FASTDCT;
|
|
shun-iwasawa |
82a8f5 |
} else if (!strcasecmp(argv[i], "-accuratedct")) {
|
|
shun-iwasawa |
82a8f5 |
printf("Using most accurate DCT/IDCT algorithm\n");
|
|
shun-iwasawa |
82a8f5 |
flags |= TJFLAG_ACCURATEDCT;
|
|
shun-iwasawa |
82a8f5 |
} else usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Determine input and output image formats based on file extensions. */
|
|
shun-iwasawa |
82a8f5 |
inFormat = strrchr(argv[1], '.');
|
|
shun-iwasawa |
82a8f5 |
outFormat = strrchr(argv[2], '.');
|
|
shun-iwasawa |
82a8f5 |
if (inFormat == NULL || outFormat == NULL || strlen(inFormat) < 2 ||
|
|
shun-iwasawa |
82a8f5 |
strlen(outFormat) < 2)
|
|
shun-iwasawa |
82a8f5 |
usage(argv[0]);
|
|
shun-iwasawa |
82a8f5 |
inFormat = &inFormat[1];
|
|
shun-iwasawa |
82a8f5 |
outFormat = &outFormat[1];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!strcasecmp(inFormat, "jpg")) {
|
|
shun-iwasawa |
82a8f5 |
/* Input image is a JPEG image. Decompress and/or transform it. */
|
|
shun-iwasawa |
82a8f5 |
long size;
|
|
shun-iwasawa |
82a8f5 |
int inSubsamp, inColorspace;
|
|
shun-iwasawa |
82a8f5 |
int doTransform = (xform.op != TJXOP_NONE || xform.options != 0 ||
|
|
shun-iwasawa |
82a8f5 |
xform.customFilter != NULL);
|
|
shun-iwasawa |
82a8f5 |
unsigned long jpegSize;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Read the JPEG file into memory. */
|
|
shun-iwasawa |
82a8f5 |
if ((jpegFile = fopen(argv[1], "rb")) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("opening input file");
|
|
shun-iwasawa |
82a8f5 |
if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) ||
|
|
shun-iwasawa |
82a8f5 |
fseek(jpegFile, 0, SEEK_SET) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("determining input file size");
|
|
shun-iwasawa |
82a8f5 |
if (size == 0)
|
|
shun-iwasawa |
82a8f5 |
THROW("determining input file size", "Input file contains no data");
|
|
shun-iwasawa |
82a8f5 |
jpegSize = (unsigned long)size;
|
|
shun-iwasawa |
82a8f5 |
if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("allocating JPEG buffer");
|
|
shun-iwasawa |
82a8f5 |
if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("reading input file");
|
|
shun-iwasawa |
82a8f5 |
fclose(jpegFile); jpegFile = NULL;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (doTransform) {
|
|
shun-iwasawa |
82a8f5 |
/* Transform it. */
|
|
shun-iwasawa |
82a8f5 |
unsigned char *dstBuf = NULL; /* Dynamically allocate the JPEG buffer */
|
|
shun-iwasawa |
82a8f5 |
unsigned long dstSize = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if ((tjInstance = tjInitTransform()) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("initializing transformer");
|
|
shun-iwasawa |
82a8f5 |
xform.options |= TJXOPT_TRIM;
|
|
shun-iwasawa |
82a8f5 |
if (tjTransform(tjInstance, jpegBuf, jpegSize, 1, &dstBuf, &dstSize,
|
|
shun-iwasawa |
82a8f5 |
&xform, flags) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("transforming input image");
|
|
shun-iwasawa |
82a8f5 |
tjFree(jpegBuf);
|
|
shun-iwasawa |
82a8f5 |
jpegBuf = dstBuf;
|
|
shun-iwasawa |
82a8f5 |
jpegSize = dstSize;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if ((tjInstance = tjInitDecompress()) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("initializing decompressor");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (tjDecompressHeader3(tjInstance, jpegBuf, jpegSize, &width, &height,
|
|
shun-iwasawa |
82a8f5 |
&inSubsamp, &inColorspace) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("reading JPEG header");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("%s Image: %d x %d pixels, %s subsampling, %s colorspace\n",
|
|
shun-iwasawa |
82a8f5 |
(doTransform ? "Transformed" : "Input"), width, height,
|
|
shun-iwasawa |
82a8f5 |
subsampName[inSubsamp], colorspaceName[inColorspace]);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!strcasecmp(outFormat, "jpg") && doTransform &&
|
|
shun-iwasawa |
82a8f5 |
scalingFactor.num == 1 && scalingFactor.denom == 1 && outSubsamp < 0 &&
|
|
shun-iwasawa |
82a8f5 |
outQual < 0) {
|
|
shun-iwasawa |
82a8f5 |
/* Input image has been transformed, and no re-compression options
|
|
shun-iwasawa |
82a8f5 |
have been selected. Write the transformed image to disk and exit. */
|
|
shun-iwasawa |
82a8f5 |
if ((jpegFile = fopen(argv[2], "wb")) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("opening output file");
|
|
shun-iwasawa |
82a8f5 |
if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("writing output file");
|
|
shun-iwasawa |
82a8f5 |
fclose(jpegFile); jpegFile = NULL;
|
|
shun-iwasawa |
82a8f5 |
goto bailout;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Scaling and/or a non-JPEG output image format and/or compression options
|
|
shun-iwasawa |
82a8f5 |
have been selected, so we need to decompress the input/transformed
|
|
shun-iwasawa |
82a8f5 |
image. */
|
|
shun-iwasawa |
82a8f5 |
width = TJSCALED(width, scalingFactor);
|
|
shun-iwasawa |
82a8f5 |
height = TJSCALED(height, scalingFactor);
|
|
shun-iwasawa |
82a8f5 |
if (outSubsamp < 0)
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = inSubsamp;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
pixelFormat = TJPF_BGRX;
|
|
shun-iwasawa |
82a8f5 |
if ((imgBuf = (unsigned char *)tjAlloc(width * height *
|
|
shun-iwasawa |
82a8f5 |
tjPixelSize[pixelFormat])) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("allocating uncompressed image buffer");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height,
|
|
shun-iwasawa |
82a8f5 |
pixelFormat, flags) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("decompressing JPEG image");
|
|
shun-iwasawa |
82a8f5 |
tjFree(jpegBuf); jpegBuf = NULL;
|
|
shun-iwasawa |
82a8f5 |
tjDestroy(tjInstance); tjInstance = NULL;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* Input image is not a JPEG image. Load it into memory. */
|
|
shun-iwasawa |
82a8f5 |
if ((imgBuf = tjLoadImage(argv[1], &width, 1, &height, &pixelFormat,
|
|
shun-iwasawa |
82a8f5 |
0)) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("loading input image");
|
|
shun-iwasawa |
82a8f5 |
if (outSubsamp < 0) {
|
|
shun-iwasawa |
82a8f5 |
if (pixelFormat == TJPF_GRAY)
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_GRAY;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
outSubsamp = TJSAMP_444;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
printf("Input Image: %d x %d pixels\n", width, height);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
printf("Output Image (%s): %d x %d pixels", outFormat, width, height);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!strcasecmp(outFormat, "jpg")) {
|
|
shun-iwasawa |
82a8f5 |
/* Output image format is JPEG. Compress the uncompressed image. */
|
|
shun-iwasawa |
82a8f5 |
unsigned long jpegSize = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
jpegBuf = NULL; /* Dynamically allocate the JPEG buffer */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (outQual < 0)
|
|
shun-iwasawa |
82a8f5 |
outQual = DEFAULT_QUALITY;
|
|
shun-iwasawa |
82a8f5 |
printf(", %s subsampling, quality = %d\n", subsampName[outSubsamp],
|
|
shun-iwasawa |
82a8f5 |
outQual);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if ((tjInstance = tjInitCompress()) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("initializing compressor");
|
|
shun-iwasawa |
82a8f5 |
if (tjCompress2(tjInstance, imgBuf, width, 0, height, pixelFormat,
|
|
shun-iwasawa |
82a8f5 |
&jpegBuf, &jpegSize, outSubsamp, outQual, flags) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("compressing image");
|
|
shun-iwasawa |
82a8f5 |
tjDestroy(tjInstance); tjInstance = NULL;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Write the JPEG image to disk. */
|
|
shun-iwasawa |
82a8f5 |
if ((jpegFile = fopen(argv[2], "wb")) == NULL)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("opening output file");
|
|
shun-iwasawa |
82a8f5 |
if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
|
|
shun-iwasawa |
82a8f5 |
THROW_UNIX("writing output file");
|
|
shun-iwasawa |
82a8f5 |
tjDestroy(tjInstance); tjInstance = NULL;
|
|
shun-iwasawa |
82a8f5 |
fclose(jpegFile); jpegFile = NULL;
|
|
shun-iwasawa |
82a8f5 |
tjFree(jpegBuf); jpegBuf = NULL;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* Output image format is not JPEG. Save the uncompressed image
|
|
shun-iwasawa |
82a8f5 |
directly to disk. */
|
|
shun-iwasawa |
82a8f5 |
printf("\n");
|
|
shun-iwasawa |
82a8f5 |
if (tjSaveImage(argv[2], imgBuf, width, 0, height, pixelFormat, 0) < 0)
|
|
shun-iwasawa |
82a8f5 |
THROW_TJ("saving output image");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
bailout:
|
|
shun-iwasawa |
82a8f5 |
tjFree(imgBuf);
|
|
shun-iwasawa |
82a8f5 |
if (tjInstance) tjDestroy(tjInstance);
|
|
shun-iwasawa |
82a8f5 |
tjFree(jpegBuf);
|
|
shun-iwasawa |
82a8f5 |
if (jpegFile) fclose(jpegFile);
|
|
shun-iwasawa |
82a8f5 |
return retval;
|
|
shun-iwasawa |
82a8f5 |
}
|