in turbojpeg-mp.c [71:135]
DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE)
(tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height,
int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize)
{
static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
int i, retval = 0;
boolean alloc = TRUE;
_JSAMPROW *row_pointer = NULL;
GET_CINSTANCE(handle)
if ((this->init & COMPRESS) == 0)
THROW("Instance has not been initialized for compression");
if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
jpegSize == NULL)
THROW("Invalid argument");
if (!this->lossless && this->quality == -1)
THROW("TJPARAM_QUALITY must be specified");
if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
THROW("TJPARAM_SUBSAMP must be specified");
if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
THROW("Memory allocation failure");
if (setjmp(this->jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
retval = -1; goto bailout;
}
cinfo->image_width = width;
cinfo->image_height = height;
cinfo->data_precision = BITS_IN_JSAMPLE;
setCompDefaults(this, pixelFormat);
if (this->noRealloc) {
alloc = FALSE;
*jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
}
jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
jpeg_start_compress(cinfo, TRUE);
for (i = 0; i < height; i++) {
if (this->bottomUp)
row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
else
row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
}
while (cinfo->next_scanline < cinfo->image_height)
_jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
cinfo->image_height - cinfo->next_scanline);
jpeg_finish_compress(cinfo);
bailout:
if (cinfo->global_state > CSTATE_START && alloc)
(*cinfo->dest->term_destination) (cinfo);
if (cinfo->global_state > CSTATE_START || retval == -1)
jpeg_abort_compress(cinfo);
free(row_pointer);
if (this->jerr.warning) retval = -1;
return retval;
}