Branch
Hash :
f74989d8
Author :
Date :
2025-09-25T11:32:45
Clean up #include directives
This is subtle, but #include <header.h> searches directories specified
with -I, then system include directories. #include "header.h" searches
the current source directory, then directories specified with -I, then
system include directories.
Using bracketed #include directives for jpeglib.h, jinclude.h, jerror.h,
cdjpeg.h, and turbojpeg.h only worked because the build system
explicitly passed -I{source_directory}/src/ to the compiler. Referring
to 51cee0362998ec6f1eabac1e795f3b6e3ee639ea, it's better for the source
code to have as few dependencies on our specific build system as
possible.
Since jpeglib.h, jinclude.h, jerror.h, and turbojpeg.h can be installed
in system include directories, it's also better for internal references
to those headers to resolve internally first, to avoid potential
conflicts between the system-installed version of libjpeg-turbo and the
version being built. (Such conflicts would never have occurred with our
build system, but they might have occurred due to misintegration with a
downstream build system.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
* Copyright (C)2011-2012, 2014-2015, 2017, 2019, 2021-2025
* D. R. Commander. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the libjpeg-turbo Project nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program demonstrates how to use the TurboJPEG C API to approximate the
* functionality of the IJG's djpeg program. djpeg features that are not
* covered:
*
* - OS/2 BMP, GIF, and Targa output file formats [legacy feature]
* - Color quantization and dithering [legacy feature]
* - The floating-point IDCT method [legacy feature]
* - Extracting an ICC color management profile
* - Progress reporting
* - Skipping rows (i.e. exclusive rather than inclusive partial decompression)
* - Debug output
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#if !defined(_MSC_VER) || _MSC_VER > 1600
#include <stdint.h>
#endif
#include "turbojpeg.h"
#ifdef _WIN32
#define strncasecmp strnicmp
#endif
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#define MATCH_ARG(arg, string, minChars) \
!strncasecmp(arg, string, max(strlen(arg), minChars))
#define IS_CROPPED(cr) (cr.x != 0 || cr.y != 0 || cr.w != 0 || cr.h != 0)
#define THROW(action, message) { \
printf("ERROR in line %d while %s:\n%s\n", __LINE__, action, message); \
retval = -1; goto bailout; \
}
#define THROW_TJ(action) { \
int errorCode = tj3GetErrorCode(tjInstance); \
printf("%s in line %d while %s:\n%s\n", \
errorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, action, \
tj3GetErrorStr(tjInstance)); \
if (errorCode == TJERR_FATAL || stopOnWarning == 1) { \
retval = -1; goto bailout; \
} \
}
#define THROW_UNIX(action) THROW(action, strerror(errno))
static tjscalingfactor *scalingFactors = NULL;
static int numScalingFactors = 0;
static void usage(char *programName)
{
int i;
printf("\nUSAGE: %s [options] <JPEG image> <Output image>\n\n", programName);
printf("The output image will be in Windows BMP or PBMPLUS (PPM/PGM) format, depending\n");
printf("on the file extension.\n\n");
printf("GENERAL OPTIONS (CAN BE ABBREVBIATED)\n");
printf("-------------------------------------\n");
printf("-icc FILE\n");
printf(" Extract the ICC (International Color Consortium) color profile from the\n");
printf(" JPEG image to the specified file\n");
printf("-strict\n");
printf(" Treat all warnings as fatal; abort immediately if incomplete or corrupt\n");
printf(" data is encountered in the JPEG image, rather than trying to salvage the\n");
printf(" rest of the image\n\n");
printf("LOSSY JPEG OPTIONS (CAN BE ABBREVIATED)\n");
printf("---------------------------------------\n");
printf("-crop WxH+X+Y\n");
printf(" Decompress only the specified region of the JPEG image. (W, H, X, and Y\n");
printf(" are the width, height, left boundary, and upper boundary of the region, all\n");
printf(" specified relative to the scaled image dimensions.) If necessary, X will\n");
printf(" be shifted left to the nearest iMCU boundary, and W will be increased\n");
printf(" accordingly.\n");
printf("-dct fast\n");
printf(" Use less accurate IDCT algorithm [legacy feature]\n");
printf("-dct int\n");
printf(" Use more accurate IDCT algorithm [default]\n");
printf("-grayscale\n");
printf(" Decompress a full-color JPEG image into a grayscale output image\n");
printf("-maxmemory N\n");
printf(" Memory limit (in megabytes) for intermediate buffers used with progressive\n");
printf(" JPEG decompression [default = no limit]\n");
printf("-maxscans N\n");
printf(" Refuse to decompress progressive JPEG images that have more than N scans\n");
printf("-nosmooth\n");
printf(" Use the fastest chrominance upsampling algorithm available\n");
printf("-rgb\n");
printf(" Decompress a grayscale JPEG image into a full-color output image\n");
printf("-scale M/N\n");
printf(" Scale the width/height of the JPEG image by a factor of M/N when\n");
printf(" decompressing it (M/N = ");
for (i = 0; i < numScalingFactors; i++) {
printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
if (numScalingFactors == 2 && i != numScalingFactors - 1)
printf(" or ");
else if (numScalingFactors > 2) {
if (i != numScalingFactors - 1)
printf(", ");
if (i == numScalingFactors - 2)
printf("or ");
}
if (i % 8 == 0 && i != 0) printf("\n ");
}
printf(")\n\n");
exit(1);
}
int main(int argc, char **argv)
{
int i, retval = 0;
int colorspace, fastDCT = -1, fastUpsample = -1, maxMemory = -1,
maxScans = -1, pixelFormat = TJPF_UNKNOWN, precision, stopOnWarning = -1,
subsamp;
tjregion croppingRegion = TJUNCROPPED;
tjscalingfactor scalingFactor = TJUNSCALED;
char *iccFilename = NULL;
tjhandle tjInstance = NULL;
FILE *jpegFile = NULL, *iccFile = NULL;
long size = 0;
size_t jpegSize, sampleSize, iccSize;
int width, height;
unsigned char *jpegBuf = NULL, *iccBuf = NULL;
void *dstBuf = NULL;
if ((scalingFactors = tj3GetScalingFactors(&numScalingFactors)) == NULL)
THROW_TJ("getting scaling factors");
for (i = 1; i < argc; i++) {
if (MATCH_ARG(argv[i], "-crop", 2) && i < argc - 1) {
char tempc = -1;
if (sscanf(argv[++i], "%d%c%d+%d+%d", &croppingRegion.w, &tempc,
&croppingRegion.h, &croppingRegion.x,
&croppingRegion.y) != 5 || croppingRegion.w < 1 ||
(tempc != 'x' && tempc != 'X') || croppingRegion.h < 1 ||
croppingRegion.x < 0 || croppingRegion.y < 0)
usage(argv[0]);
} else if (MATCH_ARG(argv[i], "-dct", 2) && i < argc - 1) {
i++;
if (MATCH_ARG(argv[i], "fast", 1))
fastDCT = 1;
else if (!MATCH_ARG(argv[i], "int", 1))
usage(argv[0]);
} else if (MATCH_ARG(argv[i], "-grayscale", 2) ||
MATCH_ARG(argv[i], "-greyscale", 2))
pixelFormat = TJPF_GRAY;
else if (MATCH_ARG(argv[i], "-icc", 2) && i < argc - 1)
iccFilename = argv[++i];
else if (MATCH_ARG(argv[i], "-maxscans", 5) && i < argc - 1) {
int tempi = atoi(argv[++i]);
if (tempi < 0) usage(argv[0]);
maxScans = tempi;
} else if (MATCH_ARG(argv[i], "-maxmemory", 2) && i < argc - 1) {
int tempi = atoi(argv[++i]);
if (tempi < 0) usage(argv[0]);
maxMemory = tempi;
} else if (MATCH_ARG(argv[i], "-nosmooth", 2))
fastUpsample = 1;
else if (MATCH_ARG(argv[i], "-rgb", 2))
pixelFormat = TJPF_RGB;
else if (MATCH_ARG(argv[i], "-strict", 3))
stopOnWarning = 1;
else if (MATCH_ARG(argv[i], "-scale", 2) && i < argc - 1) {
int match = 0, temp_num = 0, temp_denom = 0, j;
if (sscanf(argv[++i], "%d/%d", &temp_num, &temp_denom) < 2)
usage(argv[0]);
if (temp_num < 1 || temp_denom < 1)
usage(argv[0]);
for (j = 0; j < numScalingFactors; j++) {
if ((double)temp_num / (double)temp_denom ==
(double)scalingFactors[j].num / (double)scalingFactors[j].denom) {
scalingFactor = scalingFactors[j];
match = 1;
break;
}
}
if (match != 1)
usage(argv[0]);
} else break;
}
if (i != argc - 2)
usage(argv[0]);
if ((tjInstance = tj3Init(TJINIT_DECOMPRESS)) == NULL)
THROW_TJ("creating TurboJPEG instance");
if (stopOnWarning >= 0 &&
tj3Set(tjInstance, TJPARAM_STOPONWARNING, stopOnWarning) < 0)
THROW_TJ("setting TJPARAM_STOPONWARNING");
if (fastUpsample >= 0 &&
tj3Set(tjInstance, TJPARAM_FASTUPSAMPLE, fastUpsample) < 0)
THROW_TJ("setting TJPARAM_FASTUPSAMPLE");
if (fastDCT >= 0 && tj3Set(tjInstance, TJPARAM_FASTDCT, fastDCT) < 0)
THROW_TJ("setting TJPARAM_FASTDCT");
if (maxScans >= 0 && tj3Set(tjInstance, TJPARAM_SCANLIMIT, maxScans) < 0)
THROW_TJ("setting TJPARAM_SCANLIMIT");
if (maxMemory >= 0 && tj3Set(tjInstance, TJPARAM_MAXMEMORY, maxMemory) < 0)
THROW_TJ("setting TJPARAM_MAXMEMORY");
if ((jpegFile = fopen(argv[i++], "rb")) == NULL)
THROW_UNIX("opening input file");
if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) ||
fseek(jpegFile, 0, SEEK_SET) < 0)
THROW_UNIX("determining input file size");
if (size == 0)
THROW("determining input file size", "Input file contains no data");
jpegSize = size;
if ((jpegBuf = (unsigned char *)malloc(jpegSize)) == NULL)
THROW_UNIX("allocating JPEG buffer");
if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
THROW_UNIX("reading input file");
fclose(jpegFile); jpegFile = NULL;
if (tj3DecompressHeader(tjInstance, jpegBuf, jpegSize) < 0)
THROW_TJ("reading JPEG header");
subsamp = tj3Get(tjInstance, TJPARAM_SUBSAMP);
width = tj3Get(tjInstance, TJPARAM_JPEGWIDTH);
height = tj3Get(tjInstance, TJPARAM_JPEGHEIGHT);
precision = tj3Get(tjInstance, TJPARAM_PRECISION);
sampleSize = (precision <= 8 ? sizeof(unsigned char) : sizeof(short));
colorspace = tj3Get(tjInstance, TJPARAM_COLORSPACE);
if (iccFilename) {
if (tj3GetICCProfile(tjInstance, &iccBuf, &iccSize) < 0) {
THROW_TJ("getting ICC profile");
} else {
if ((iccFile = fopen(iccFilename, "wb")) == NULL)
THROW_UNIX("opening ICC file");
if (fwrite(iccBuf, iccSize, 1, iccFile) < 1)
THROW_UNIX("writing ICC profile");
tj3Free(iccBuf); iccBuf = NULL;
fclose(iccFile); iccFile = NULL;
}
}
if (pixelFormat == TJPF_UNKNOWN) {
if (colorspace == TJCS_GRAY)
pixelFormat = TJPF_GRAY;
else if (colorspace == TJCS_CMYK || colorspace == TJCS_YCCK)
pixelFormat = TJPF_CMYK;
else
pixelFormat = TJPF_RGB;
}
if (!tj3Get(tjInstance, TJPARAM_LOSSLESS)) {
if (tj3SetScalingFactor(tjInstance, scalingFactor) < 0)
THROW_TJ("setting scaling factor");
width = TJSCALED(width, scalingFactor);
height = TJSCALED(height, scalingFactor);
if (IS_CROPPED(croppingRegion)) {
int adjustment;
if (subsamp == TJSAMP_UNKNOWN)
THROW("adjusting cropping region",
"Could not determine subsampling level of JPEG image");
adjustment =
croppingRegion.x % TJSCALED(tjMCUWidth[subsamp], scalingFactor);
croppingRegion.x -= adjustment;
croppingRegion.w += adjustment;
if (tj3SetCroppingRegion(tjInstance, croppingRegion) < 0)
THROW_TJ("setting cropping region");
width = croppingRegion.w;
height = croppingRegion.h;
}
}
#if ULLONG_MAX > SIZE_MAX
if ((unsigned long long)width * height * tjPixelSize[pixelFormat] *
sampleSize > (unsigned long long)((size_t)-1))
THROW("allocating uncompressed image buffer", "Image is too large");
#endif
if ((dstBuf =
(unsigned char *)malloc(sizeof(unsigned char) * width * height *
tjPixelSize[pixelFormat] * sampleSize)) == NULL)
THROW_UNIX("allocating uncompressed image buffer");
if (precision <= 8) {
if (tj3Decompress8(tjInstance, jpegBuf, jpegSize, dstBuf, 0,
pixelFormat) < 0)
THROW_TJ("decompressing JPEG image");
} else if (precision <= 12) {
if (tj3Decompress12(tjInstance, jpegBuf, jpegSize, dstBuf, 0,
pixelFormat) < 0)
THROW_TJ("decompressing JPEG image");
} else {
if (tj3Decompress16(tjInstance, jpegBuf, jpegSize, dstBuf, 0,
pixelFormat) < 0)
THROW_TJ("decompressing JPEG image");
}
tj3Free(jpegBuf); jpegBuf = NULL;
if (precision <= 8) {
if (tj3SaveImage8(tjInstance, argv[i], dstBuf, width, 0, height,
pixelFormat) < 0)
THROW_TJ("saving output image");
} else if (precision <= 12) {
if (tj3SaveImage12(tjInstance, argv[i], dstBuf, width, 0, height,
pixelFormat) < 0)
THROW_TJ("saving output image");
} else {
if (tj3SaveImage16(tjInstance, argv[i], dstBuf, width, 0, height,
pixelFormat) < 0)
THROW_TJ("saving output image");
}
bailout:
tj3Destroy(tjInstance);
if (jpegFile) fclose(jpegFile);
tj3Free(jpegBuf);
tj3Free(iccBuf);
if (iccFile) fclose(iccFile);
free(dstBuf);
return retval;
}