Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / wrtiff.cpp
blobf4b95bacf2648a41bb20a317b1dc3b2349a65d85
1 // BEGIN_COPYRIGHT
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
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
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
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.
26 //
27 // END_COPYRIGHT
32 // Implementation of image data, attribute, and I/O
34 #include "image.h"
35 #include "tiffio.h"
37 namespace GLEAN {
39 ///////////////////////////////////////////////////////////////////////////////
40 // writeTIFF - write image to TIFF file
41 ///////////////////////////////////////////////////////////////////////////////
42 void
43 Image::writeTIFF(const char* filename) {
44 static uint16 unassocAlpha[] = {EXTRASAMPLE_UNASSALPHA};
45 GLsizei rowStep = rowSizeInBytes();
47 TIFF* tf = TIFFOpen(filename, "w");
48 if (!tf)
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
60 // issues.
61 TIFFSetField(tf, TIFFTAG_ROWSPERSTRIP, height());
63 switch (format()) {
64 case GL_LUMINANCE:
65 TIFFSetField(tf, TIFFTAG_SAMPLESPERPIXEL, 1);
66 TIFFSetField(tf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
67 break;
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);
72 break;
73 case GL_RGB:
74 TIFFSetField(tf, TIFFTAG_SAMPLESPERPIXEL, 3);
75 TIFFSetField(tf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
76 break;
77 case GL_RGBA:
78 TIFFSetField(tf, TIFFTAG_SAMPLESPERPIXEL, 4);
79 TIFFSetField(tf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
80 TIFFSetField(tf, TIFFTAG_EXTRASAMPLES, 1, unassocAlpha);
81 break;
82 default:
83 TIFFClose(tf);
84 throw BadFormat(format());
87 switch (type()) {
88 case GL_BYTE:
89 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 8);
90 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
91 break;
92 case GL_UNSIGNED_BYTE:
93 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 8);
94 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
95 break;
96 case GL_SHORT:
97 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 16);
98 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
99 break;
100 case GL_UNSIGNED_SHORT:
101 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 16);
102 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
103 break;
104 case GL_INT:
105 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 32);
106 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
107 break;
108 case GL_UNSIGNED_INT:
109 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 32);
110 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
111 break;
112 case GL_FLOAT:
113 TIFFSetField(tf, TIFFTAG_BITSPERSAMPLE, 32);
114 TIFFSetField(tf, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
115 break;
116 default:
117 TIFFClose(tf);
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);
130 TIFFClose(tf);
131 }; // Image::writeTIFF
134 }; // namespace GLEAN