0

I would like to create a large TIFF RGBA image using tiles. I wrote the following program which loops over the positions of the tiles, but the resulting image is always transparent. Why ?

#include <math.h>
#include <stdlib.h>
#include "tiffio.h"
int main(int argc,char** argv)
{
uint32 width=400, height=400;
uint32 tileWidth=16, tileHeight=16;
TIFF* tif = TIFFOpen("foo.tif", "w");
int i,j,x;

int spp=4;
int bps = 8;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,spp);//R G B A
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
TIFFSetField(tif, TIFFTAG_TILEWIDTH, tileWidth);
TIFFSetField(tif, TIFFTAG_TILELENGTH, tileHeight);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
int tilesize = tileWidth * tileHeight * bps/8 *  spp;
uint8 *buf = malloc(tilesize);

for ( j = 0; j < width; j += tileWidth) {
    for ( i = 0; i < height; i += tileHeight)
        {
           for ( x = 0; x < tilesize; x+=spp)
            {
                uint8 a = 100;
                buf[x+0] =a;
                buf[x+1] =a;
                buf[x+2] =a;
                buf[x+3] =a;
            }
        if(TIFFWriteTile(tif, buf, i, j, 0, 0)<0) exit(-1);
        }
    }
free(buf);
TIFFClose(tif);
return 0;
} 
Paul R
  • 208,748
  • 37
  • 389
  • 560
Pierre
  • 34,472
  • 31
  • 113
  • 192
  • 1
    Because you explicitly set the alpha to 100? Or is it *enitrely* transoarent - i.e., nothing at all? – Jongware Apr 27 '16 at 11:22
  • i've tried different values for 'a' : 0, 255, etc.. the image is always entirely transparent. – Pierre Apr 27 '16 at 12:35
  • 1
    Try taking transparency out of the equation by creating a 3BPP image. If that works, then transparency is the problem. – Jongware Apr 27 '16 at 13:05
  • 1
    You do not set what is the meaning of last byte: [see this](http://www.awaresystems.be/imaging/tiff/tifftags/samplesperpixel.html). Try adding `TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, EXTRASAMPLE_ASSOCALPHA);` – Mathieu Apr 27 '16 at 14:06
  • @Rad @purplepsycho : thanks , your comments led me to the solution: I was missing: ` TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);` – Pierre Apr 27 '16 at 15:09
  • Why not post an answer then – hello_there_andy Mar 11 '17 at 08:17

0 Answers0