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 // Constructors/Destructor
41 ///////////////////////////////////////////////////////////////////////////////
46 _type
= GL_UNSIGNED_BYTE
;
54 // An unitialized image of the given type and size:
55 Image::Image(int aWidth
, int aHeight
, GLenum aFormat
, GLenum aType
) {
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
) {
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
;
93 for (/*int */i
= 0; i
< height(); ++i
) {
94 pack(width(), row
, solidColor
);
95 row
+= rowSizeInBytes();
97 } // Image::Image(aWidth, aHeight, aFormat, aType)
100 Image::Image(Image
& i
) {
102 _height
= i
.height();
103 _format
= i
.format();
105 _alignment
= i
.alignment();
111 memcpy(pixels(), i
.pixels(), height() * rowSizeInBytes());
112 } // Image::Image(Image&)
115 Image::operator= (Image
& i
) {
122 alignment(i
.alignment());
125 memcpy(pixels(), i
.pixels(), height() * rowSizeInBytes());
127 } // Image::operator=
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())
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)
160 ///////////////////////////////////////////////////////////////////////////////
161 // pixels - set pointer to pixel array
162 ///////////////////////////////////////////////////////////////////////////////
164 Image::pixels(char* p
) {
165 // We always own our pixels, so delete the old ones (if any) before
166 // installing new ones:
172 ///////////////////////////////////////////////////////////////////////////////
173 // reserve - reserve memory for image (assuming current type, format, and size)
174 ///////////////////////////////////////////////////////////////////////////////
177 pixels(0); // deallocate old pixel array
178 pixels(new char[height() * rowSizeInBytes()]);
181 ///////////////////////////////////////////////////////////////////////////////
182 // validateRowSizeInBytes - compute image row size, measured in bytes
183 ///////////////////////////////////////////////////////////////////////////////
185 Image::validateRowSizeInBytes() {
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 ///////////////////////////////////////////////////////////////////////////////
197 Image::validatePixelSizeInBytes() {
200 _pixelSizeInBytes
= 1;
202 case GL_LUMINANCE_ALPHA
:
203 _pixelSizeInBytes
= 2;
206 _pixelSizeInBytes
= 3;
209 _pixelSizeInBytes
= 4;
212 throw BadFormat(format());
217 case GL_UNSIGNED_BYTE
:
220 case GL_UNSIGNED_SHORT
:
221 _pixelSizeInBytes
<<= 1;
224 case GL_UNSIGNED_INT
:
226 _pixelSizeInBytes
<<= 2;
229 throw BadType(type());
232 validate(vbPixelSizeInBytes
);
233 return _pixelSizeInBytes
;
236 }; // namespace GLEAN