Using libtiff's TIFFReadRawTile to get a jpeg tile without decompression/compression

StackOverflow https://stackoverflow.com/questions/12949061

  •  08-07-2021
  •  | 
  •  

سؤال

i've a pyramidal tiled tiff file and I want to extract the tiles without decoding and re-encoding the jpeg, i've seen that using TIFFReadRawTile() function you can extract the raw tile without decoding, how can i write the extracted buffer to a readable jpeg file?

هل كانت مفيدة؟

المحلول 2

I've found that actually there is no way to get the encoded tile without directly messing with the huffmann tables of the tiff, which is pretty tricky.

The only way I've found is to read the decoded tile and then do some magic with vips to output to jpeg directly.

tdata_t buf;
tsize_t len;

buf = _TIFFmalloc( TIFFTileSize( tif ) );
len = TIFFReadEncodedTile(tif, tile, buf, (tsize_t) -1);

VImage result ((void *) buf, 256, 256, 3, VImage::FMTUCHAR);

void *outBuffer;
unsigned long len;
vips_jpegsave_buffer(result, &outBuffer, &len, "Q", 90, NULL);

and the use cout to output the image after some headers.

نصائح أخرى

The task you are up to is not a trivial one. You might want to take a closer look at tiff2pdf utility's source code. The utility does what you need and you might extract relevant parts from it.

The problem is, the utility does many other things you will have to discard. Also, not any JPEG-in-TIFF could be successfully processed by the utility. Basically, because there is enough semi-broken TIFFs out there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top