3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
32 // Implementation of image data, attribute, and I/O
39 ///////////////////////////////////////////////////////////////////////////////
40 // writeTIFF - write image to TIFF file
41 ///////////////////////////////////////////////////////////////////////////////
43 Image::writeTIFF(const char* filename
) {
44 static uint16 unassocAlpha
[] = {EXTRASAMPLE_UNASSALPHA
};
45 GLsizei rowStep
= rowSizeInBytes();
47 TIFF
* tf
= TIFFOpen(filename
, "w");
49 throw CantOpen(filename
);
51 TIFFSetField(tf
, TIFFTAG_IMAGELENGTH
, height());
52 TIFFSetField(tf
, TIFFTAG_IMAGEWIDTH
, width());
53 TIFFSetField(tf
, TIFFTAG_XRESOLUTION
, 100.0);
54 TIFFSetField(tf
, TIFFTAG_YRESOLUTION
, 100.0);
55 TIFFSetField(tf
, TIFFTAG_RESOLUTIONUNIT
, RESUNIT_INCH
);
56 TIFFSetField(tf
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
57 TIFFSetField(tf
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
58 TIFFSetField(tf
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
59 // LZW would have been acceptable, were it not for patent
61 TIFFSetField(tf
, TIFFTAG_ROWSPERSTRIP
, height());
65 TIFFSetField(tf
, TIFFTAG_SAMPLESPERPIXEL
, 1);
66 TIFFSetField(tf
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_MINISBLACK
);
68 case GL_LUMINANCE_ALPHA
:
69 TIFFSetField(tf
, TIFFTAG_SAMPLESPERPIXEL
, 2);
70 TIFFSetField(tf
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_MINISBLACK
);
71 TIFFSetField(tf
, TIFFTAG_EXTRASAMPLES
, 1, unassocAlpha
);
74 TIFFSetField(tf
, TIFFTAG_SAMPLESPERPIXEL
, 3);
75 TIFFSetField(tf
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_RGB
);
78 TIFFSetField(tf
, TIFFTAG_SAMPLESPERPIXEL
, 4);
79 TIFFSetField(tf
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_RGB
);
80 TIFFSetField(tf
, TIFFTAG_EXTRASAMPLES
, 1, unassocAlpha
);
84 throw BadFormat(format());
89 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 8);
90 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_INT
);
92 case GL_UNSIGNED_BYTE
:
93 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 8);
94 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
97 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 16);
98 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_INT
);
100 case GL_UNSIGNED_SHORT
:
101 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 16);
102 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
105 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 32);
106 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_INT
);
108 case GL_UNSIGNED_INT
:
109 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 32);
110 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
113 TIFFSetField(tf
, TIFFTAG_BITSPERSAMPLE
, 32);
114 TIFFSetField(tf
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_IEEEFP
);
118 throw BadType(type());
122 // Write rows in reverse order, so that the usual OpenGL
123 // orientation won't result in an upside-down image for
124 // naive TIFF readers:
125 char* row
= pixels() + (height() - 1) * rowStep
;
126 for (GLsizei r
= 0; r
< height(); ++r
, row
-= rowStep
)
127 TIFFWriteScanline(tf
, row
, r
, 0);
131 }; // Image::writeTIFF
134 }; // namespace GLEAN