Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / pack.cpp
blob11617ef4767e9db80095ed8cebc8aed6faaf683c
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 // Data packing utilities. Note that these map component values per
33 // the usual OpenGL conversions. Also, see comments in unpack.cpp.
35 #include "image.h"
37 namespace {
39 #define SCALE (static_cast<double>(num) / static_cast<double>(denom))
40 #define BIAS (static_cast<double>(bias) / static_cast<double>(denom))
42 // The original implementation of packing functions (using function
43 // templates) wouldn't compile under VC6, but this slight variant
44 // using static member functions in a class template will compile.
46 template<class component, int num, unsigned int denom, int bias>
47 class Pack
49 public :
50 // pack_l
51 static void pack_l(GLsizei n, char* dst, double* rgba)
53 component* out = reinterpret_cast<component*>(dst);
54 double* end = rgba + 4 * n;
55 for (; rgba != end; rgba += 4) {
56 if (bias)
57 out[0] = static_cast<component>(SCALE * rgba[0] - BIAS);
58 else
59 out[0] = static_cast<component>(SCALE * rgba[0]);
60 out += 1;
64 // pack_la
65 static void pack_la(GLsizei n, char* dst, double* rgba)
67 component* out = reinterpret_cast<component*>(dst);
68 double* end = rgba + 4 * n;
69 for (; rgba != end; rgba += 4) {
70 if (bias) {
71 out[0] = static_cast<component>(SCALE * rgba[0] - BIAS);
72 out[1] = static_cast<component>(SCALE * rgba[3] - BIAS);
73 } else {
74 out[0] = static_cast<component>(SCALE * rgba[0]);
75 out[1] = static_cast<component>(SCALE * rgba[3]);
77 out += 2;
81 // pack_rga
82 static void pack_rgb(GLsizei n, char* dst, double* rgba)
84 component* out = reinterpret_cast<component*>(dst);
85 double* end = rgba + 4 * n;
86 for (; rgba != end; rgba += 4) {
87 if (bias) {
88 out[0] = static_cast<component>(SCALE * rgba[0] - BIAS);
89 out[1] = static_cast<component>(SCALE * rgba[1] - BIAS);
90 out[2] = static_cast<component>(SCALE * rgba[2] - BIAS);
91 } else {
92 out[0] = static_cast<component>(SCALE * rgba[0]);
93 out[1] = static_cast<component>(SCALE * rgba[1]);
94 out[2] = static_cast<component>(SCALE * rgba[2]);
96 out += 3;
100 // pack_rgba
101 static void pack_rgba(GLsizei n, char* dst, double* rgba)
103 component* out = reinterpret_cast<component*>(dst);
104 double* end = rgba + 4 * n;
105 for (; rgba != end; rgba += 4) {
106 if (bias) {
107 out[0] = static_cast<component>(SCALE * rgba[0] - BIAS);
108 out[1] = static_cast<component>(SCALE * rgba[1] - BIAS);
109 out[2] = static_cast<component>(SCALE * rgba[2] - BIAS);
110 out[3] = static_cast<component>(SCALE * rgba[3] - BIAS);
111 } else {
112 out[0] = static_cast<component>(SCALE * rgba[0]);
113 out[1] = static_cast<component>(SCALE * rgba[1]);
114 out[2] = static_cast<component>(SCALE * rgba[2]);
115 out[3] = static_cast<component>(SCALE * rgba[3]);
117 out += 4;
121 }; // class Pack
123 #undef SCALE
124 #undef BIAS
126 }; // anonymous namespace
129 namespace GLEAN {
131 ///////////////////////////////////////////////////////////////////////////////
132 // Public interface
133 ///////////////////////////////////////////////////////////////////////////////
134 void
135 Image::pack(GLsizei n, char* nextPixel, double* rgba) {
136 (*(valid(vbPacker)? _packer: validatePacker())) (n, nextPixel, rgba);
139 ///////////////////////////////////////////////////////////////////////////////
140 // validatePacker - select appropriate pixel-packing utility
141 ///////////////////////////////////////////////////////////////////////////////
143 Image::Packer*
144 Image::validatePacker() {
145 switch (format()) {
146 case GL_LUMINANCE:
147 switch (type()) {
148 case GL_BYTE:
149 _packer = Pack<GLbyte, 255, 2, 1>::pack_l;
150 break;
151 case GL_UNSIGNED_BYTE:
152 _packer = Pack<GLubyte, 255, 1, 0>::pack_l;
153 break;
154 case GL_SHORT:
155 _packer = Pack<GLshort, 65535, 2, 1>::pack_l;
156 break;
157 case GL_UNSIGNED_SHORT:
158 _packer = Pack<GLushort, 65535, 1, 0>::pack_l;
159 break;
160 case GL_INT:
161 _packer = Pack<GLint, 4294967295U, 2, 1>::pack_l;
162 break;
163 case GL_UNSIGNED_INT:
164 _packer = Pack<GLuint, 4294967295U, 1, 0>::pack_l;
165 break;
166 case GL_FLOAT:
167 _packer = Pack<GLfloat, 1, 1, 0>::pack_l;
168 break;
169 default:
170 throw BadType(type());
172 break;
173 case GL_LUMINANCE_ALPHA:
174 switch (type()) {
175 case GL_BYTE:
176 _packer = Pack<GLbyte, 255, 2, 1>::pack_la;
177 break;
178 case GL_UNSIGNED_BYTE:
179 _packer = Pack<GLubyte, 255, 1, 0>::pack_la;
180 break;
181 case GL_SHORT:
182 _packer = Pack<GLshort, 65535, 2, 1>::pack_la;
183 break;
184 case GL_UNSIGNED_SHORT:
185 _packer = Pack<GLushort, 65535, 1, 0>::pack_la;
186 break;
187 case GL_INT:
188 _packer = Pack<GLint, 4294967295U, 2, 1>::pack_la;
189 break;
190 case GL_UNSIGNED_INT:
191 _packer = Pack<GLuint, 4294967295U, 1, 0>::pack_la;
192 break;
193 case GL_FLOAT:
194 _packer = Pack<GLfloat, 1, 1, 0>::pack_la;
195 break;
196 default:
197 throw BadType(type());
199 break;
200 case GL_RGB:
201 switch (type()) {
202 case GL_BYTE:
203 _packer = Pack<GLbyte, 255, 2, 1>::pack_rgb;
204 break;
205 case GL_UNSIGNED_BYTE:
206 _packer = Pack<GLubyte, 255, 1, 0>::pack_rgb;
207 break;
208 case GL_SHORT:
209 _packer = Pack<GLshort, 65535, 2, 1>::pack_rgb;
210 break;
211 case GL_UNSIGNED_SHORT:
212 _packer = Pack<GLushort, 65535, 1, 0>::pack_rgb;
213 break;
214 case GL_INT:
215 _packer = Pack<GLint, 4294967295U, 2, 1>::pack_rgb;
216 break;
217 case GL_UNSIGNED_INT:
218 _packer = Pack<GLuint, 4294967295U, 1, 0>::pack_rgb;
219 break;
220 case GL_FLOAT:
221 _packer = Pack<GLfloat, 1, 1, 0>::pack_rgb;
222 break;
223 default:
224 throw BadType(type());
226 break;
227 case GL_RGBA:
228 switch (type()) {
229 case GL_BYTE:
230 _packer = Pack<GLbyte, 255, 2, 1>::pack_rgba;
231 break;
232 case GL_UNSIGNED_BYTE:
233 _packer = Pack<GLubyte, 255, 1, 0>::pack_rgba;
234 break;
235 case GL_SHORT:
236 _packer = Pack<GLshort, 65535, 2, 1>::pack_rgba;
237 break;
238 case GL_UNSIGNED_SHORT:
239 _packer = Pack<GLushort, 65535, 1, 0>::pack_rgba;
240 break;
241 case GL_INT:
242 _packer = Pack<GLint, 4294967295U, 2, 1>::pack_rgba;
243 break;
244 case GL_UNSIGNED_INT:
245 _packer = Pack<GLuint, 4294967295U, 1, 0>::pack_rgba;
246 break;
247 case GL_FLOAT:
248 _packer = Pack<GLfloat, 1, 1, 0>::pack_rgba;
249 break;
250 default:
251 throw BadType(type());
253 break;
254 default:
255 throw BadFormat(format());
258 validate(vbPacker);
259 return _packer;
262 }; // namespace GLEAN