JPEG compression for tiff file

My goal is to read tif file with jpeg compression with vtk (I use 9.1). I was not able to do it with vtkTIFFReader so I tried from the studying a possibility to write tif with jpeg compression first to see how it works.
So, I have the following code:

	vtkSmartPointer<vtkPNGReader> pngReader = vtkSmartPointer<vtkPNGReader>::New(); // 1
	pngReader->SetFileName(myPNGFileName); // 2
	pngReader->Update(); // 3

	vtkSmartPointer<vtkTIFFWriter> writer = vtkSmartPointer<vtkTIFFWriter>::New(); // 4
	writer->SetCompressionToJPEG(); // 5
	writer->SetInputConnection(pngReader->GetOutputPort()); // 6
	writer->SetFileName(myTIFFFileName); // 7
	writer->Write(); // 8

It doesn’t work on the line // 8 because of the line 140 in tif_write.c

		if (!(*tif->tif_preencode)(tif, sample))
			return (-1);

returns -1. I see under debugger that tif->tif_preencode is {vtktiff-9.1d.dll!JPEGPreEncode(tiff *, unsigned short)}.
Writer writes tiff file in case of the line 7 is commented.

So, what should I do to use JPEG compression for writing tiff file?
And if it is possible there is the second question - how to read such kind of saved tiff file later?

It’s possible that the JPEG encoder requires RGB pixels, 8 bits per channel. What is the pixel format of your PNG image? You might have to convert the pixel format before you can apply JPEG compression to it.

You are right, thanks. I created a bitmap image with 24 bits per pixel, changed vtkPNGReader into vtkBMPReader and now this code works. I can read output tif file with vtkTIFFReader as well.
Unfortunately, I did not find a solution for reading my another tiff file in the same way.

I did not find the root of problem, but I can explain the structure of my tiff file.
It has the standard header (see TIFF: Summary from the Encyclopedia of Graphics File Formats as well) (49 49 2a 00), then First IFD Offset. In This IFD there are all needed parameters, e.g.
image_width = 37888
image_length = 49152
tile_width = 256
tile_length = 256
compression = 7
planar_configuration = 1
tile_offsets, tile_bytes
jpeg_table with jpeg_tables_size = 574

JPEG Table has the following structure:
FF DB (DQT) with some data
FF DB (DQT) with some data
FF C4 (DHT) with some data
FF C4 (DHT) with some data
FF C4 (DHT) with some data
FF C4 (DHT) with some data

Data for offset 0 has 1831 bytes and has the following structure:
FF C0 (SOF0) with some data
FF DA (SOS) with some data
image data (1796 bytes)

Other data has the similar structures.

The problem is that vtkTIFFReader 8.1 and 9.1 can’t read this my file. Version 8.1 reads it but displays a wrong image and 9.1 fails in tif_jpeg.c on the line 1255

			TIFFErrorExt(tif->tif_clientdata, module,
				       "Improper JPEG sampling factors %d,%d\n"
				       "Apparently should be %"PRIu16",%"PRIu16".",
				       sp->h_sampling, sp->v_sampling);

because sp->cinfo.d.comp_info[0].h_samp_factor and sp->cinfo.d.comp_info[0].v_samp_factor are equal to 1 while sp->h_sampling and sp->v_sampling are 2. The last two parameters have been set to 2 in the same file on the line 1039:

	switch (sp->photometric) {
		sp->h_sampling = td->td_ycbcrsubsampling[0];
		sp->v_sampling = td->td_ycbcrsubsampling[1];

I have to add that GIMP opens this tiff file but its structure if violated. But Windows Photo Viewer displays this file good. Matlab open this tiff file good as well.

I wrote a python script that creates a buffer, copy there jpeg table without the last two bytes and then copy there image by index without the first two bytes. I get the right jpeg file in this way. Then I combine all those images and have the right result.

Unfortunately I can’t attach this image because of its size and non-disclosure agreement, but I hope you could point me out the right place where I can get some help.

And old web page for TIFFReadScanline() indicates that it doesn’t work with JPEG subsampled data.

The same webpage also offers a solution: instead of the scanline-based interface, use the strip- and tile-based interfaces to read these images.

So it sounds like that’s the direction to go, if you want to try patching vtkTIFFReader so that it can read your file.