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.
This commit is contained in:
DRC
2024-09-14 10:29:56 -04:00
parent bcbca8b9bc
commit f29eda6485

View File

@@ -2738,22 +2738,21 @@ DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
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 @@ DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
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 @@ DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
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);
}