[cairo] TIFF files

James Cloos cloos at jhcloos.com
Fri Mar 16 08:11:59 PDT 2012


>>>>> "NA" == Nadeem Afana <nadeemafana at live.com> writes:

NA> How can I generate TIFF files using Cairo and C/C++ in Linux?

LibTiff.

Libtiff is well documented (start with >>man libtiff<<.  Each of the
functions listed under LIST OF ROUTINES has its own man page) and comes
with some example apps which help document the api.

As an example, I added this to a test app to write out said app's
internal CIELAB+Alpha buffer (if cairo had support for colour spaces I
would have used it to write a pdf...):

    if (labtif) {
	TIFFDefaultStripSize (labtif, height);
        TIFFSetField (labtif, TIFFTAG_IMAGEWIDTH, width);
        TIFFSetField (labtif, TIFFTAG_IMAGELENGTH, height);
        TIFFSetField (labtif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
        TIFFSetField (labtif, TIFFTAG_SAMPLESPERPIXEL, 4);
        TIFFSetField (labtif, TIFFTAG_BITSPERSAMPLE, TIFFDataWidth(TIFF_DOUBLE) * 8);
        TIFFSetField (labtif, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
        TIFFSetField (labtif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
        TIFFSetField (labtif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
	TIFFSetField (labtif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
	TIFFSetField (labtif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
	TIFFSetField (labtif, TIFFTAG_ROWSPERSTRIP, height );
	if (dpi > 0.) {
		TIFFSetField (labtif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
		TIFFSetField (labtif, TIFFTAG_XRESOLUTION, dpi );
		TIFFSetField (labtif, TIFFTAG_YRESOLUTION, dpi );
	}
	TIFFWriteRawStrip (labtif, 0, labbuf, len * 4 * sizeof(double));
	TIFFClose (labtif);
    }

(labtiff is opened with:

	labtif = TIFFOpen(optarg, "w");

inside of a getopt_long(3) block)

For a more portable app you'd want either to base fillorder on the
hosts's endianness or ensure the buffer has matching order.  You also
may want to use some form of compression, etc.

There are a bunch of fields to set, but the api is straitforward.

-JimC
-- 
James Cloos <cloos at jhcloos.com>         OpenPGP: 1024D/ED7DAEA6


More information about the cairo mailing list