Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / image_misc.cpp
blob176267011945fcf3a27f44ce02dcd347247128ee
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 <string.h>
37 namespace GLEAN {
39 ///////////////////////////////////////////////////////////////////////////////
40 // Constructors/Destructor
41 ///////////////////////////////////////////////////////////////////////////////
42 // An empty image:
43 Image::Image() {
44 _width = _height = 0;
45 _format = GL_RGB;
46 _type = GL_UNSIGNED_BYTE;
47 _pixels = 0;
48 _alignment = 4;
49 _packer = 0;
50 _unpacker = 0;
51 _invalid = vbAll;
52 } // Image::Image
54 // An unitialized image of the given type and size:
55 Image::Image(int aWidth, int aHeight, GLenum aFormat, GLenum aType) {
56 _width = aWidth;
57 _height = aHeight;
58 _format = aFormat;
59 _type = aType;
60 _pixels = 0;
61 _alignment = 4;
62 _packer = 0;
63 _unpacker = 0;
64 _invalid = vbAll;
65 reserve();
66 } // Image::Image(aWidth, aHeight, aFormat, aType)
68 // An image of the given type and size, initialized to a solid color:
69 Image::Image(int aWidth, int aHeight, GLenum aFormat, GLenum aType,
70 double r, double g, double b, double a) {
71 _width = aWidth;
72 _height = aHeight;
73 _format = aFormat;
74 _type = aType;
75 _pixels = 0;
76 _alignment = 4;
77 _packer = 0;
78 _unpacker = 0;
79 _invalid = vbAll;
80 reserve();
81 int i; // VC++ 6 doesn't handle the definition of variables in a
82 // for-statement properly
84 double* solidColor = new double[4 * width()];
85 for (/*int */i = 0; i < 4 * width(); i += 4) {
86 solidColor[i + 0] = r;
87 solidColor[i + 1] = g;
88 solidColor[i + 2] = b;
89 solidColor[i + 3] = a;
92 char* row = pixels();
93 for (/*int */i = 0; i < height(); ++i) {
94 pack(width(), row, solidColor);
95 row += rowSizeInBytes();
97 } // Image::Image(aWidth, aHeight, aFormat, aType)
99 // Copy constructor:
100 Image::Image(Image& i) {
101 _width = i.width();
102 _height = i.height();
103 _format = i.format();
104 _type = i.type();
105 _alignment = i.alignment();
106 _pixels = 0;
107 _packer = 0;
108 _unpacker = 0;
109 _invalid = vbAll;
110 reserve();
111 memcpy(pixels(), i.pixels(), height() * rowSizeInBytes());
112 } // Image::Image(Image&)
114 /*Image::*/Image&
115 Image::operator= (Image& i) {
116 if (this == &i)
117 return *this;
118 width(i.width());
119 height(i.height());
120 format(i.format());
121 type(i.type());
122 alignment(i.alignment());
123 _invalid = vbAll;
124 reserve();
125 memcpy(pixels(), i.pixels(), height() * rowSizeInBytes());
126 return *this;
127 } // Image::operator=
129 Image::~Image() {
130 if (_pixels)
131 delete[] _pixels;
135 // Test if two images are identical
136 bool Image::operator==(const Image &img) const
138 // cast away const because of rowSizeInBytes()
139 Image &img1 = const_cast<Image&>(*this);
140 Image &img2 = const_cast<Image&>(img);
142 if (img1.width() != img2.width() ||
143 img1.height() != img2.height() ||
144 img1.format() != img2.format() ||
145 img1.type() != img2.type() ||
146 img1.alignment() != img2.alignment() ||
147 img1.rowSizeInBytes() != img2.rowSizeInBytes())
148 return false;
150 const char *p1 = img1.pixels();
151 const char *p2 = img2.pixels();
152 const int n = img1.rowSizeInBytes() * img1.height();
153 if (memcmp(p1, p2, n) != 0)
154 return false;
156 return true;
160 ///////////////////////////////////////////////////////////////////////////////
161 // pixels - set pointer to pixel array
162 ///////////////////////////////////////////////////////////////////////////////
163 void
164 Image::pixels(char* p) {
165 // We always own our pixels, so delete the old ones (if any) before
166 // installing new ones:
167 if (_pixels)
168 delete[] _pixels;
169 _pixels = p;
170 } // Image::pixels
172 ///////////////////////////////////////////////////////////////////////////////
173 // reserve - reserve memory for image (assuming current type, format, and size)
174 ///////////////////////////////////////////////////////////////////////////////
175 void
176 Image::reserve() {
177 pixels(0); // deallocate old pixel array
178 pixels(new char[height() * rowSizeInBytes()]);
179 } // Image::reserve
181 ///////////////////////////////////////////////////////////////////////////////
182 // validateRowSizeInBytes - compute image row size, measured in bytes
183 ///////////////////////////////////////////////////////////////////////////////
184 GLsizei
185 Image::validateRowSizeInBytes() {
186 _rowSizeInBytes =
187 (width() * pixelSizeInBytes() + alignment() - 1)
188 & ~(alignment() - 1);
189 validate(vbRowSizeInBytes);
190 return _rowSizeInBytes;
191 } // Image::calcRowSizeInBytes
193 ///////////////////////////////////////////////////////////////////////////////
194 // validatePixelSizeInBytes - compute pixel size, measured in bytes
195 ///////////////////////////////////////////////////////////////////////////////
196 GLsizei
197 Image::validatePixelSizeInBytes() {
198 switch (format()) {
199 case GL_LUMINANCE:
200 _pixelSizeInBytes = 1;
201 break;
202 case GL_LUMINANCE_ALPHA:
203 _pixelSizeInBytes = 2;
204 break;
205 case GL_RGB:
206 _pixelSizeInBytes = 3;
207 break;
208 case GL_RGBA:
209 _pixelSizeInBytes = 4;
210 break;
211 default:
212 throw BadFormat(format());
215 switch (type()) {
216 case GL_BYTE:
217 case GL_UNSIGNED_BYTE:
218 break;
219 case GL_SHORT:
220 case GL_UNSIGNED_SHORT:
221 _pixelSizeInBytes <<= 1;
222 break;
223 case GL_INT:
224 case GL_UNSIGNED_INT:
225 case GL_FLOAT:
226 _pixelSizeInBytes <<= 2;
227 break;
228 default:
229 throw BadType(type());
232 validate(vbPixelSizeInBytes);
233 return _pixelSizeInBytes;
236 }; // namespace GLEAN