0

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?

alex88
  • 4,788
  • 4
  • 39
  • 60

2 Answers2

0

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.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • Let me check the tiff2pdf source code, btw the JPEG-in-TIFF is always made by me using the vips2tiff command so it's always converted in the same format. Actually i've just got it working with TIFFReadEncodedTile() to a vips image but i want if possible to skip the decoding/encoding part – alex88 Oct 18 '12 at 15:10
  • Unfortunately, I know nothing about vips2tiff, so I can't say if it's a better starting point than tiff2pdf. – Bobrovsky Oct 18 '12 at 15:22
  • Don't worry about that, it's just that TIFFReadEncodedTile decodes the JPEG, the RAW version returns the encoded JPEG but i think you need to add some jpeg metadata – alex88 Oct 18 '12 at 15:31
0

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.

alex88
  • 4,788
  • 4
  • 39
  • 60