in turbojpeg.c [1969:2015]
DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
unsigned long jpegSize, unsigned char *dstBuf,
int width, int pitch, int height, int pixelFormat,
int flags)
{
static const char FUNCTION_NAME[] = "tjDecompress2";
int i, retval = 0, jpegwidth, jpegheight, scaledw, scaledh;
GET_DINSTANCE(handle);
if ((this->init & DECOMPRESS) == 0)
THROW("Instance has not been initialized for decompression");
if (jpegBuf == NULL || jpegSize <= 0 || width < 0 || height < 0)
THROW("Invalid argument");
if (setjmp(this->jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
retval = -1; goto bailout;
}
jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
jpeg_read_header(dinfo, TRUE);
jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height;
if (width == 0) width = jpegwidth;
if (height == 0) height = jpegheight;
for (i = 0; i < NUMSF; i++) {
scaledw = TJSCALED(jpegwidth, sf[i]);
scaledh = TJSCALED(jpegheight, sf[i]);
if (scaledw <= width && scaledh <= height)
break;
}
if (i >= NUMSF)
THROW("Could not scale down to desired image dimensions");
processFlags(handle, flags, DECOMPRESS);
if (tj3SetScalingFactor(handle, sf[i]) == -1)
return -1;
if (tj3SetCroppingRegion(handle, TJUNCROPPED) == -1)
return -1;
return tj3Decompress8(handle, jpegBuf, jpegSize, dstBuf, pitch, pixelFormat);
bailout:
if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
if (this->jerr.warning) retval = -1;
return retval;
}