realloc image buffer in turbojpeg cpp

584 views Asked by At

I'm using turbojpeg for reading images.

I'm doing something along these lines:

m_data = tjAlloc(MAX_RAW_SIZE);
c_jpeg_buffer = tjAlloc(MAX_RAW_SIZE);
tjDecompressHeader2(jpegDecompressor, c_jpeg_buffer, _jpegSize, &tj_width, &tj_height, &jpegSubsamp);
tjDecompress2(c_jpegDecompressor, m_data, _jpegSize, c_jpeg_buffer, tj_width, 0/*pitch*/, tj_height, TJPF_GRAY, TJFLAG_FASTDCT);
tjFree(c_jpeg_buffer);
tjFree(m_data);

I want to realloc m_data and c_jpeg_buffer in case the _jpegSize or (tj_width*tj_height) are too large. The turbojpeg documentation suggests that this would happen automatically if I'm using tjCompress2(...) or tjTransform(...). It says nothing about what happens in this case. Suppose I realloc the buffers - would it still be safe to use tjFree()?

1

There are 1 answers

0
mousomer On BEST ANSWER

For future users of turboJpeg... RTFM...

If you send a pre-allocated buffer, you need to pass it's size. Both buffer the itself and the size might be changed.