# JPEG compression for tiff file

**URL:** https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424
**Category:** Support
**Created:** [May 4, 2022, 8:15am UTC](https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424 "2022-05-04T08:15:32Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Leo\_Abalckin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/leo_abalckin/32/5106_2.png) [@Leo\_Abalckin](https://discourse.vtk.org/u/Leo_Abalckin)
#### Post date: [May 4, 2022, 8:15am UTC](https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424/1 "2022-05-04T08:15:32Z")

</div>

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:

```auto
	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

```auto
		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?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 4, 2022, 1:01pm UTC](https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424/2 "2022-05-04T13:01:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Leo\_Abalckin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/leo_abalckin/32/5106_2.png) [@Leo\_Abalckin](https://discourse.vtk.org/u/Leo_Abalckin)
#### Post date: [May 18, 2022, 10:45am UTC](https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424/3 "2022-05-18T10:45:54Z")

</div>

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](https://www.fileformat.info/format/tiff/egff.htm) 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 D8 (SOI)  
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  
FF D9 (EOI)

Data for offset 0 has 1831 bytes and has the following structure:  
FF D8 (SOI)  
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

```auto
			TIFFErrorExt(tif->tif_clientdata, module,
				       "Improper JPEG sampling factors %d,%d\n"
				       "Apparently should be %"PRIu16",%"PRIu16".",
				       sp->cinfo.d.comp_info[0].h_samp_factor,
				       sp->cinfo.d.comp_info[0].v_samp_factor,
				       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:

```auto
	switch (sp->photometric) {
	case PHOTOMETRIC_YCBCR:
		sp->h_sampling = td->td_ycbcrsubsampling[0];
		sp->v_sampling = td->td_ycbcrsubsampling[1];
		break;

```

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.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 19, 2022, 1:55pm UTC](https://discourse.vtk.org/t/jpeg-compression-for-tiff-file/8424/4 "2022-05-19T13:55:50Z")

</div>

And [old web page](http://www.libtiff.org/man/TIFFReadScanline.3t.html) 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.
