tj3Transform: Don't calc dst subsamp unless needed
This just improves code readability by emphasizing that we don't care
about the destination image's level of subsampling unless
TJPARAM_NOREALLOC is set or lossless cropping will be performed.
diff --git a/turbojpeg.c b/turbojpeg.c
index 9e622ae..766a6d1 100644
--- a/turbojpeg.c
+++ b/turbojpeg.c
@@ -2738,22 +2738,21 @@
srcSubsamp = getSubsamp(&this->dinfo);
for (i = 0; i < n; i++) {
- int dstSubsamp = (t[i].options & TJXOPT_GRAY) ? TJSAMP_GRAY : srcSubsamp;
-
- if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
- t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
- if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440;
- else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422;
- else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441;
- else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411;
- }
-
if (!jtransform_request_workspace(dinfo, &xinfo[i]))
THROW("Transform is not perfect");
if (xinfo[i].crop) {
+ int dstSubsamp = (t[i].options & TJXOPT_GRAY) ? TJSAMP_GRAY : srcSubsamp;
+
+ if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
+ t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
+ if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440;
+ else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422;
+ else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441;
+ else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411;
+ }
if (dstSubsamp == TJSAMP_UNKNOWN)
- THROW("Could not determine subsampling level of JPEG image");
+ THROW("Could not determine subsampling level of destination image");
if ((t[i].r.x % tjMCUWidth[dstSubsamp]) != 0 ||
(t[i].r.y % tjMCUHeight[dstSubsamp]) != 0)
THROWI("To crop this JPEG image, x must be a multiple of %d\n"
@@ -2766,15 +2765,10 @@
for (i = 0; i < n; i++) {
JDIMENSION dstWidth = dinfo->image_width, dstHeight = dinfo->image_height;
- int dstSubsamp = (t[i].options & TJXOPT_GRAY) ? TJSAMP_GRAY : srcSubsamp;
if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
dstWidth = dinfo->image_height; dstHeight = dinfo->image_width;
- if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440;
- else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422;
- else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441;
- else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411;
}
if (xinfo[i].crop) {
@@ -2786,6 +2780,17 @@
dstWidth = xinfo[i].crop_width; dstHeight = xinfo[i].crop_height;
}
if (this->noRealloc) {
+ int dstSubsamp = (t[i].options & TJXOPT_GRAY) ? TJSAMP_GRAY : srcSubsamp;
+
+ if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
+ t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
+ if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440;
+ else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422;
+ else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441;
+ else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411;
+ }
+ if (dstSubsamp == TJSAMP_UNKNOWN)
+ THROW("Could not determine subsampling level of destination image");
alloc = FALSE;
dstSizes[i] = tj3JPEGBufSize(dstWidth, dstHeight, dstSubsamp);
}