1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "PixelFormatTests.h"
|
---|
26 | #include <cstdlib>
|
---|
27 |
|
---|
28 | // Register the suite
|
---|
29 | CPPUNIT_TEST_SUITE_REGISTRATION( PixelFormatTests );
|
---|
30 |
|
---|
31 | void PixelFormatTests::setUp()
|
---|
32 | {
|
---|
33 | size = 4096;
|
---|
34 | randomData = new uint8[size];
|
---|
35 | temp = new uint8[size];
|
---|
36 | temp2 = new uint8[size];
|
---|
37 | // Generate reproducable random data
|
---|
38 | srand(0);
|
---|
39 | for(unsigned int x=0; x<size; x++)
|
---|
40 | randomData[x] = (uint8)rand();
|
---|
41 | }
|
---|
42 |
|
---|
43 | void PixelFormatTests::tearDown()
|
---|
44 | {
|
---|
45 | delete [] randomData;
|
---|
46 | delete [] temp;
|
---|
47 | delete [] temp2;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | void PixelFormatTests::testIntegerPackUnpack()
|
---|
52 | {
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | void PixelFormatTests::testFloatPackUnpack()
|
---|
57 | {
|
---|
58 | // Float32
|
---|
59 | float data[4] = {1.0f, 2.0f, 3.0f, 4.0f};
|
---|
60 | float r,g,b,a;
|
---|
61 | PixelUtil::unpackColour(&r, &g, &b, &a, PF_FLOAT32_RGBA, data);
|
---|
62 | CPPUNIT_ASSERT_EQUAL(r, 1.0f);
|
---|
63 | CPPUNIT_ASSERT_EQUAL(g, 2.0f);
|
---|
64 | CPPUNIT_ASSERT_EQUAL(b, 3.0f);
|
---|
65 | CPPUNIT_ASSERT_EQUAL(a, 4.0f);
|
---|
66 |
|
---|
67 | // Float16
|
---|
68 | setupBoxes(PF_A8B8G8R8, PF_FLOAT16_RGBA);
|
---|
69 | dst2.format = PF_A8B8G8R8;
|
---|
70 | unsigned int eob = src.getWidth()*4;
|
---|
71 |
|
---|
72 | PixelUtil::bulkPixelConversion(src, dst1);
|
---|
73 | PixelUtil::bulkPixelConversion(dst1, dst2);
|
---|
74 |
|
---|
75 | // Locate errors
|
---|
76 | std::stringstream s;
|
---|
77 | int x;
|
---|
78 | for(x=0; x<eob; x++) {
|
---|
79 | if(temp2[x] != randomData[x])
|
---|
80 | s << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) randomData[x]
|
---|
81 | << "!= " << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) temp2[x] << " ";
|
---|
82 | }
|
---|
83 |
|
---|
84 | // src and dst2 should match
|
---|
85 | CPPUNIT_ASSERT_MESSAGE("PF_FLOAT16_RGBA<->PF_A8B8G8R8 conversion was not lossless "+s.str(),
|
---|
86 | memcmp(src.data, dst2.data, eob) == 0);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Pure 32 bit float precision brute force pixel conversion; for comparision
|
---|
90 | void naiveBulkPixelConversion(const PixelBox &src, const PixelBox &dst)
|
---|
91 | {
|
---|
92 | uint8 *srcptr = static_cast<uint8*>(src.data);
|
---|
93 | uint8 *dstptr = static_cast<uint8*>(dst.data);
|
---|
94 | unsigned int srcPixelSize = PixelUtil::getNumElemBytes(src.format);
|
---|
95 | unsigned int dstPixelSize = PixelUtil::getNumElemBytes(dst.format);
|
---|
96 |
|
---|
97 | // Calculate pitches+skips in bytes
|
---|
98 | int srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
|
---|
99 | int srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
|
---|
100 |
|
---|
101 | int dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
|
---|
102 | int dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
|
---|
103 |
|
---|
104 | // The brute force fallback
|
---|
105 | float r,g,b,a;
|
---|
106 | for(size_t z=src.front; z<src.back; z++)
|
---|
107 | {
|
---|
108 | for(size_t y=src.top; y<src.bottom; y++)
|
---|
109 | {
|
---|
110 | for(size_t x=src.left; x<src.right; x++)
|
---|
111 | {
|
---|
112 | PixelUtil::unpackColour(&r, &g, &b, &a, src.format, srcptr);
|
---|
113 | PixelUtil::packColour(r, g, b, a, dst.format, dstptr);
|
---|
114 | srcptr += srcPixelSize;
|
---|
115 | dstptr += dstPixelSize;
|
---|
116 | }
|
---|
117 | srcptr += srcRowSkipBytes;
|
---|
118 | dstptr += dstRowSkipBytes;
|
---|
119 | }
|
---|
120 | srcptr += srcSliceSkipBytes;
|
---|
121 | dstptr += dstSliceSkipBytes;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | void PixelFormatTests::setupBoxes(PixelFormat srcFormat, PixelFormat dstFormat)
|
---|
127 | {
|
---|
128 | unsigned int width = (size-4) / PixelUtil::getNumElemBytes(srcFormat);
|
---|
129 | unsigned int width2 = (size-4) / PixelUtil::getNumElemBytes(dstFormat);
|
---|
130 | if(width > width2)
|
---|
131 | width = width2;
|
---|
132 |
|
---|
133 | src = PixelBox(width, 1, 1, srcFormat, randomData);
|
---|
134 | dst1 = PixelBox(width, 1, 1, dstFormat, temp);
|
---|
135 | dst2 = PixelBox(width, 1, 1, dstFormat, temp2);
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | void PixelFormatTests::testCase(PixelFormat srcFormat, PixelFormat dstFormat)
|
---|
140 | {
|
---|
141 | setupBoxes(srcFormat, dstFormat);
|
---|
142 | // Check end of buffer
|
---|
143 | unsigned int eob = dst1.getWidth()*PixelUtil::getNumElemBytes(dstFormat);
|
---|
144 | temp[eob] = (unsigned char)0x56;
|
---|
145 | temp[eob+1] = (unsigned char)0x23;
|
---|
146 |
|
---|
147 | //std::cerr << "["+PixelUtil::getFormatName(srcFormat)+"->"+PixelUtil::getFormatName(dstFormat)+"]" << " " << eob << std::endl;
|
---|
148 |
|
---|
149 | // Do pack/unpacking with both naive and optimized version
|
---|
150 | PixelUtil::bulkPixelConversion(src, dst1);
|
---|
151 | naiveBulkPixelConversion(src, dst2);
|
---|
152 |
|
---|
153 | CPPUNIT_ASSERT_EQUAL(temp[eob], (unsigned char)0x56);
|
---|
154 | CPPUNIT_ASSERT_EQUAL(temp[eob+1], (unsigned char)0x23);
|
---|
155 |
|
---|
156 | std::stringstream s;
|
---|
157 | int x;
|
---|
158 | s << "src=";
|
---|
159 | for(x=0; x<16; x++)
|
---|
160 | s << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) randomData[x];
|
---|
161 | s << " dst=";
|
---|
162 | for(x=0; x<16; x++)
|
---|
163 | s << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) temp[x];
|
---|
164 | s << " dstRef=";
|
---|
165 | for(x=0; x<16; x++)
|
---|
166 | s << std::hex << std::setw(2) << std::setfill('0') << (unsigned int) temp2[x];
|
---|
167 | s << " ";
|
---|
168 |
|
---|
169 | // Compare result
|
---|
170 | CPPUNIT_ASSERT_MESSAGE("Conversion mismatch ["+PixelUtil::getFormatName(srcFormat)+"->"+PixelUtil::getFormatName(dstFormat)+"] "+s.str(),
|
---|
171 | memcmp(dst1.data, dst2.data, eob) == 0);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void PixelFormatTests::testBulkConversion()
|
---|
175 | {
|
---|
176 | // Self match
|
---|
177 | testCase(PF_A8R8G8B8, PF_A8R8G8B8);
|
---|
178 | // Optimized
|
---|
179 | testCase(PF_A8R8G8B8,PF_A8B8G8R8);
|
---|
180 | testCase(PF_A8R8G8B8,PF_B8G8R8A8);
|
---|
181 | testCase(PF_A8R8G8B8,PF_R8G8B8A8);
|
---|
182 | testCase(PF_A8B8G8R8,PF_A8R8G8B8);
|
---|
183 | testCase(PF_A8B8G8R8,PF_B8G8R8A8);
|
---|
184 | testCase(PF_A8B8G8R8,PF_R8G8B8A8);
|
---|
185 | testCase(PF_B8G8R8A8,PF_A8R8G8B8);
|
---|
186 | testCase(PF_B8G8R8A8,PF_A8B8G8R8);
|
---|
187 | testCase(PF_B8G8R8A8,PF_R8G8B8A8);
|
---|
188 | testCase(PF_R8G8B8A8,PF_A8R8G8B8);
|
---|
189 | testCase(PF_R8G8B8A8,PF_A8B8G8R8);
|
---|
190 | testCase(PF_R8G8B8A8,PF_B8G8R8A8);
|
---|
191 |
|
---|
192 | testCase(PF_A8B8G8R8, PF_L8);
|
---|
193 | testCase(PF_L8, PF_A8B8G8R8);
|
---|
194 | testCase(PF_A8R8G8B8, PF_L8);
|
---|
195 | testCase(PF_L8, PF_A8R8G8B8);
|
---|
196 | testCase(PF_B8G8R8A8, PF_L8);
|
---|
197 | testCase(PF_L8, PF_B8G8R8A8);
|
---|
198 | testCase(PF_L8, PF_L16);
|
---|
199 | testCase(PF_L16, PF_L8);
|
---|
200 | testCase(PF_R8G8B8, PF_B8G8R8);
|
---|
201 | testCase(PF_B8G8R8, PF_R8G8B8);
|
---|
202 | testCase(PF_B8G8R8, PF_R8G8B8);
|
---|
203 | testCase(PF_R8G8B8, PF_B8G8R8);
|
---|
204 | testCase(PF_R8G8B8, PF_A8R8G8B8);
|
---|
205 | testCase(PF_B8G8R8, PF_A8R8G8B8);
|
---|
206 | testCase(PF_R8G8B8, PF_A8B8G8R8);
|
---|
207 | testCase(PF_B8G8R8, PF_A8B8G8R8);
|
---|
208 | testCase(PF_R8G8B8, PF_B8G8R8A8);
|
---|
209 | testCase(PF_B8G8R8, PF_B8G8R8A8);
|
---|
210 | testCase(PF_A8R8G8B8, PF_R8G8B8);
|
---|
211 | testCase(PF_A8R8G8B8, PF_B8G8R8);
|
---|
212 | testCase(PF_X8R8G8B8, PF_A8R8G8B8);
|
---|
213 | testCase(PF_X8R8G8B8, PF_A8B8G8R8);
|
---|
214 | testCase(PF_X8R8G8B8, PF_B8G8R8A8);
|
---|
215 | testCase(PF_X8R8G8B8, PF_R8G8B8A8);
|
---|
216 | testCase(PF_X8B8G8R8, PF_A8R8G8B8);
|
---|
217 | testCase(PF_X8B8G8R8, PF_A8B8G8R8);
|
---|
218 | testCase(PF_X8B8G8R8, PF_B8G8R8A8);
|
---|
219 | testCase(PF_X8B8G8R8, PF_R8G8B8A8);
|
---|
220 |
|
---|
221 | //CPPUNIT_ASSERT_MESSAGE("Conversion mismatch", false);
|
---|
222 | }
|
---|
223 |
|
---|