2007.10.29 15:21 "[Tiff] rookie question", by Ron Croonenberg

2007.10.31 00:01 "[Tiff] Re: Rookie question", by Leonardo Serni

I am new to libTiff. I am looking for a way to create TIF image files (or any other graphics format) from "raw" simulation data. (basically it is 3-d data projected on a 2-D plane. What it comes down to is that I have a bunch of "pixels", a discrete coordinate (that translates to a pixel in an image) and RGB values. (I basically just want to create an image by doing something like "this pixel has these RGB values" and "stick them in an image file.

It probably can be done with libTiff, right? are there any examples that I can take a look at?

You can do this quite easily (no error checking included, see man pages):

#include <stdint.h>
#include "tiffio.h"

int main(int argc, char **argv)
{
         TIFF *tif;

         char    buffer[3*1024];

         uint32_t        width = 1024, height = 768, row;

 if (2 != argc)
{
        fprintf(stderr, "syntax: %s outfile.tif\n", argv[0]);
        return -1;
}

tif = TIFFOpen(argv[1], "w");

TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, width);

         TIFFSetField (tif, TIFFTAG_IMAGELENGTH, height);
         TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
         TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL,  3);
         TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE,    8);
         TIFFSetField (tif, TIFFTAG_EXTRASAMPLES,     0, NULL);
         TIFFSetField (tif, TIFFTAG_ORIENTATION,      ORIENTATION_TOPLEFT);
         TIFFSetField (tif, TIFFTAG_PHOTOMETRIC,      PHOTOMETRIC_RGB);
         TIFFSetField (tif, TIFFTAG_IMAGEDESCRIPTION, "3D Simulation");
         /* Other compressions exist, of course */
         TIFFSetField (tif, TIFFTAG_COMPRESSION,      COMPRESSION_DEFLATE);
         TIFFSetField (tif, TIFFTAG_PREDICTOR,        1 /* experiment */);

/* Suggest rps = 20000/width so that a strip is around 64K and possibly try to keep rps to a power of 2 (unless width is itself a power of 2, of course)

*/

         TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP,     16);

         for (row = 0; row < 768; row++)
         {
                 int i;
                 for (i = 0; i < 1024; i++)
                 {
                         buffer[3*i] = row % 255;
                         buffer[3*i+1] = i % 255;
                         buffer[3*i+2] = (row >> 2) % 255;
                 }
                 TIFFWriteScanline(tif, buffer, row, 0);
         }
         TIFFClose(tif); tif = NULL;
         return 0;
}

Leonardo