mirror of
https://github.com/libjpeg-turbo/libjpeg-turbo.git
synced 2026-01-18 21:41:20 +01:00
tjexample.c: Fix error when decompressing
(regression introduced by300a344d65)300a344d65fixed the recompression code path but also broke the pure decompression code path, because the fix caused the TurboJPEG decompression instance to be destroyed before tj3SaveImage() could use it. Furthermore, the fix in300a344d65prevented pixel density information from being transferred from the input image to the output image when recompressing. This commit does the following: - Modify tjexample.c so that a single TurboJPEG instance is initialized for lossless transformation and shared by all code paths. In addition to fixing both of the aforementioned issues, this makes the code more readable. - Extend tjexampletest to test the recompression code path, thus ensuring that the issues fixed by this commit and300a344d65are not reintroduced. - Modify tjexample.c to remove redundant fclose(), tj3Destroy(), and tj3Free() calls.
This commit is contained in:
18
tjexample.c
18
tjexample.c
@@ -252,6 +252,9 @@ int main(int argc, char **argv)
|
||||
inFormat = &inFormat[1];
|
||||
outFormat = &outFormat[1];
|
||||
|
||||
if ((tjInstance = tj3Init(TJINIT_TRANSFORM)) == NULL)
|
||||
THROW_TJ("creating TurboJPEG instance");
|
||||
|
||||
if (!strcasecmp(inFormat, "jpg")) {
|
||||
/* Input image is a JPEG image. Decompress and/or transform it. */
|
||||
long size;
|
||||
@@ -280,8 +283,6 @@ int main(int argc, char **argv)
|
||||
unsigned char *dstBuf = NULL; /* Dynamically allocate the JPEG buffer */
|
||||
size_t dstSize = 0;
|
||||
|
||||
if ((tjInstance = tj3Init(TJINIT_TRANSFORM)) == NULL)
|
||||
THROW_TJ("initializing transformer");
|
||||
xform.options |= TJXOPT_TRIM;
|
||||
if (tj3Transform(tjInstance, jpegBuf, jpegSize, 1, &dstBuf, &dstSize,
|
||||
&xform) < 0) {
|
||||
@@ -291,9 +292,6 @@ int main(int argc, char **argv)
|
||||
tj3Free(jpegBuf);
|
||||
jpegBuf = dstBuf;
|
||||
jpegSize = dstSize;
|
||||
} else {
|
||||
if ((tjInstance = tj3Init(TJINIT_DECOMPRESS)) == NULL)
|
||||
THROW_TJ("initializing decompressor");
|
||||
}
|
||||
if (tj3Set(tjInstance, TJPARAM_FASTUPSAMPLE, fastUpsample) < 0)
|
||||
THROW_TJ("setting TJPARAM_FASTUPSAMPLE");
|
||||
@@ -323,7 +321,6 @@ int main(int argc, char **argv)
|
||||
THROW_UNIX("opening output file");
|
||||
if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
|
||||
THROW_UNIX("writing output file");
|
||||
fclose(jpegFile); jpegFile = NULL;
|
||||
goto bailout;
|
||||
}
|
||||
|
||||
@@ -350,11 +347,8 @@ int main(int argc, char **argv)
|
||||
pixelFormat) < 0)
|
||||
THROW_TJ("decompressing JPEG image");
|
||||
tj3Free(jpegBuf); jpegBuf = NULL;
|
||||
tj3Destroy(tjInstance); tjInstance = NULL;
|
||||
} else {
|
||||
/* Input image is not a JPEG image. Load it into memory. */
|
||||
if ((tjInstance = tj3Init(TJINIT_COMPRESS)) == NULL)
|
||||
THROW_TJ("initializing compressor");
|
||||
if ((imgBuf = tj3LoadImage8(tjInstance, argv[1], &width, 1, &height,
|
||||
&pixelFormat)) == NULL)
|
||||
THROW_TJ("loading input image");
|
||||
@@ -380,8 +374,6 @@ int main(int argc, char **argv)
|
||||
printf(", %s subsampling, quality = %d\n", subsampName[outSubsamp],
|
||||
outQual);
|
||||
|
||||
if (!tjInstance && (tjInstance = tj3Init(TJINIT_COMPRESS)) == NULL)
|
||||
THROW_TJ("initializing compressor");
|
||||
if (tj3Set(tjInstance, TJPARAM_SUBSAMP, outSubsamp) < 0)
|
||||
THROW_TJ("setting TJPARAM_SUBSAMP");
|
||||
if (tj3Set(tjInstance, TJPARAM_QUALITY, outQual) < 0)
|
||||
@@ -391,16 +383,12 @@ int main(int argc, char **argv)
|
||||
if (tj3Compress8(tjInstance, imgBuf, width, 0, height, pixelFormat,
|
||||
&jpegBuf, &jpegSize) < 0)
|
||||
THROW_TJ("compressing image");
|
||||
tj3Destroy(tjInstance); tjInstance = NULL;
|
||||
|
||||
/* Write the JPEG image to disk. */
|
||||
if ((jpegFile = fopen(argv[2], "wb")) == NULL)
|
||||
THROW_UNIX("opening output file");
|
||||
if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
|
||||
THROW_UNIX("writing output file");
|
||||
tj3Destroy(tjInstance); tjInstance = NULL;
|
||||
fclose(jpegFile); jpegFile = NULL;
|
||||
tj3Free(jpegBuf); jpegBuf = NULL;
|
||||
} else {
|
||||
/* Output image format is not JPEG. Save the uncompressed image
|
||||
directly to disk. */
|
||||
|
||||
@@ -68,6 +68,14 @@ for image in $IMAGES; do
|
||||
runme $EXEDIR/djpeg -dct fast -nosmooth -bmp -outfile $OUTDIR/${basename}_${samp}_fast_nosmooth_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg
|
||||
runme $EXEDIR/djpeg -dct int -nosmooth -bmp -outfile $OUTDIR/${basename}_${samp}_accurate_nosmooth_djpeg.bmp $OUTDIR/${basename}_${samp}_accurate_cjpeg.jpg
|
||||
done
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct fast -grayscale -outfile $OUTDIR/${basename}_GRAY_fast_cjpeg2.jpg $OUTDIR/${basename}_GRAY_fast_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 2x2 -outfile $OUTDIR/${basename}_420_fast_cjpeg2.jpg $OUTDIR/${basename}_420_fast_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 2x1 -outfile $OUTDIR/${basename}_422_fast_cjpeg2.jpg $OUTDIR/${basename}_422_fast_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 1x1 -outfile $OUTDIR/${basename}_444_fast_cjpeg2.jpg $OUTDIR/${basename}_444_fast_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct int -grayscale -outfile $OUTDIR/${basename}_GRAY_accurate_cjpeg2.jpg $OUTDIR/${basename}_GRAY_accurate_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct int -sample 2x2 -outfile $OUTDIR/${basename}_420_accurate_cjpeg2.jpg $OUTDIR/${basename}_420_accurate_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct int -sample 2x1 -outfile $OUTDIR/${basename}_422_accurate_cjpeg2.jpg $OUTDIR/${basename}_422_accurate_djpeg.bmp
|
||||
runme $EXEDIR/cjpeg -quality 95 -dct int -sample 1x1 -outfile $OUTDIR/${basename}_444_accurate_cjpeg2.jpg $OUTDIR/${basename}_444_accurate_djpeg.bmp
|
||||
|
||||
# Compression
|
||||
for dct in fast accurate; do
|
||||
@@ -81,6 +89,18 @@ for image in $IMAGES; do
|
||||
done
|
||||
done
|
||||
|
||||
# Recompression
|
||||
for dct in fast accurate; do
|
||||
dctarg=
|
||||
if [ "${dct}" = "fast" ]; then
|
||||
dctarg=-fastdct
|
||||
fi
|
||||
for samp in GRAY 420 422 444; do
|
||||
runme $TJEXAMPLE $OUTDIR/${basename}_${samp}_${dct}.jpg $OUTDIR/${basename}_${samp}_${dct}_recomp.jpg -q 95 -subsamp ${samp} ${dctarg}
|
||||
runme cmp $OUTDIR/${basename}_${samp}_${dct}_recomp.jpg $OUTDIR/${basename}_${samp}_${dct}_cjpeg2.jpg
|
||||
done
|
||||
done
|
||||
|
||||
# Decompression
|
||||
for dct in fast accurate; do
|
||||
dctarg=
|
||||
|
||||
Reference in New Issue
Block a user