Skip to main content
GameDev.net gamedev.net
🔒 Locked

tiff library?

Started by corrington_j Nov 17, 2004 at 8:19 PM 2 replies 450+ views
Original Post
corrington_j
corrington_j
I am looking for a small tiff image library that can load and save tiff files. The one requirement i have is that it be able to give me the image data in an array, because i am working with existing code that takes as input an array of tif image data. Any suggestions. Thanks
dotproduct
dotproduct
libtiff perhaps? :p

Write to tiff image file:
	unsigned char* buf = img->getPixelData();    tsize_t linebytes;    TIFF* out;	int width = img->getCols();	int height = img->getRows();	int pixelSize = img->getPixelSize();	    out = TIFFOpen(filename.c_str(), "w");	if(!out)		throw std::runtime_error("Unable to open file '" + filename + "' for writing");	    TIFFSetField(out, TIFFTAG_IMAGEWIDTH,  width);    TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, pixelSize);    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);    TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE);    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1);    linebytes = pixelSize * width;	    for (uint32 row = height-1; row > 0; row--)	{		TIFFWriteScanline(out, buf + (row * width * pixelSize), row, 0);	}	    TIFFClose(out);


Read from image file:
	int width = 0;	int height = 0;	int pixelSize = 0;		TIFF* tif = TIFFOpen(filename.c_str(), "r");	if(!tif)		throw std::runtime_error("Could not open file '" + filename + "', or it is not a valid TIFF file");		TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);	TIFFGetField(tif,  TIFFTAG_SAMPLESPERPIXEL, &pixelSize);	unsigned char* data = (unsigned char*)_TIFFmalloc((tsize_t)(TIFFNumberOfStrips(tif) * TIFFStripSize(tif)));	for(tstrip_t strip = 0; strip < TIFFNumberOfStrips(tif); strip++)		TIFFReadEncodedStrip(tif, strip, data + strip * TIFFStripSize(tif), (tsize_t)-1);	TIFFClose(tif);		if(pixelSize == 4)		img->setFromBuffer(data, width, height, Image::RGBA);	else if(pixelSize == 3)		img->setFromBuffer(data, width, height, Image::RGB);		_TIFFfree(data);


Ok... you obviously can't just cut and paste this. (I just copied it from my (fairly simple) image lib).. but you get the idea...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.