xref: /haiku/src/tests/kits/interface/bbitmap/BBitmapTester.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 //------------------------------------------------------------------------------
2 //	BBitmapTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <Message.h>
11 #include <OS.h>
12 #include <Application.h>
13 #include <Bitmap.h>
14 #include <String.h>
15 
16 // Project Includes ------------------------------------------------------------
17 #include <TestShell.h>
18 #include <TestUtils.h>
19 #include <cppunit/TestAssert.h>
20 
21 // Local Includes --------------------------------------------------------------
22 #include "BBitmapTester.h"
23 
24 // Local Defines ---------------------------------------------------------------
25 
26 // Globals ---------------------------------------------------------------------
27 
28 //------------------------------------------------------------------------------
29 
30 // get_bytes_per_row
31 static inline
32 int32
33 get_bytes_per_row(color_space colorSpace, int32 width)
34 {
35 	int32 bpr = 0;
36 	switch (colorSpace) {
37 		// supported
38 		case B_RGBA64: case B_RGBA64_BIG:
39 			bpr = 8 * width;
40 			break;
41 		case B_RGB48: case B_RGB48_BIG:
42 			bpr = 6 * width;
43 			break;
44 		case B_RGB32: case B_RGBA32:
45 		case B_RGB32_BIG: case B_RGBA32_BIG:
46 		case B_UVL32: case B_UVLA32:
47 		case B_LAB32: case B_LABA32:
48 		case B_HSI32: case B_HSIA32:
49 		case B_HSV32: case B_HSVA32:
50 		case B_HLS32: case B_HLSA32:
51 		case B_CMY32: case B_CMYA32: case B_CMYK32:
52 			bpr = 4 * width;
53 			break;
54 		case B_RGB24: case B_RGB24_BIG:
55 		case B_UVL24: case B_LAB24: case B_HSI24:
56 		case B_HSV24: case B_HLS24: case B_CMY24:
57 			bpr = 3 * width;
58 			break;
59 		case B_RGB16:		case B_RGB15:		case B_RGBA15:
60 		case B_RGB16_BIG:	case B_RGB15_BIG:	case B_RGBA15_BIG:
61 			bpr = 2 * width;
62 			break;
63 		case B_CMAP8: case B_GRAY8:
64 			bpr = width;
65 			break;
66 		case B_GRAY1:
67 			bpr = (width + 7) / 8;
68 			break;
69 		case B_YCbCr422: case B_YUV422:
70 			bpr = (width + 3) / 4 * 8;
71 			break;
72 		case B_YCbCr411: case B_YUV411:
73 			bpr = (width + 3) / 4 * 6;
74 			break;
75 		case B_YCbCr444: case B_YUV444:
76 			bpr = (width + 3) / 4 * 12;
77 			break;
78 		case B_YCbCr420: case B_YUV420:
79 			bpr = (width + 3) / 4 * 6;
80 			break;
81 		// unsupported
82 		case B_NO_COLOR_SPACE:
83 		case B_YUV9: case B_YUV12:
84 			break;
85 	}
86 	// align to int32
87 	bpr = (bpr + 3) & 0x7ffffffc;
88 	return bpr;
89 }
90 
91 
92 /*
93 	BBitmap(BRect bounds, color_space colorSpace, bool acceptsViews,
94 			bool needsContiguous);
95 	@case 1			acceptsViews = false, needsContiguous = false, valid bounds,
96 					different color spaces
97 	@results		Bits() should be valid, BitsLength(), BytesPerRow() etc.
98 					should return the correct values.
99  */
100 void TBBitmapTester::BBitmap1()
101 {
102 	BApplication app("application/x-vnd.obos.bitmap-constructor-test");
103 	struct test_case {
104 		BRect		bounds;
105 		color_space	space;
106 	} testCases[] = {
107 		{ BRect(0, 0, 39, 9),	B_RGBA64 },
108 		{ BRect(0, 0, 39, 9),	B_RGBA64_BIG },
109 		{ BRect(0, 0, 39, 9),	B_RGB48 },
110 		{ BRect(0, 0, 39, 9),	B_RGB48_BIG },
111 
112 		{ BRect(0, 0, 39, 9),	B_RGB32 },
113 		{ BRect(0, 0, 39, 9),	B_RGBA32 },
114 		{ BRect(0, 0, 39, 9),	B_RGB32_BIG },
115 		{ BRect(0, 0, 39, 9),	B_RGBA32_BIG },
116 
117 		{ BRect(0, 0, 39, 9),	B_UVL32 },
118 		{ BRect(0, 0, 39, 9),	B_UVLA32 },
119 		{ BRect(0, 0, 39, 9),	B_LAB32 },
120 		{ BRect(0, 0, 39, 9),	B_LABA32 },
121 		{ BRect(0, 0, 39, 9),	B_HSI32 },
122 		{ BRect(0, 0, 39, 9),	B_HSIA32 },
123 		{ BRect(0, 0, 39, 9),	B_HSV32 },
124 		{ BRect(0, 0, 39, 9),	B_HSVA32 },
125 		{ BRect(0, 0, 39, 9),	B_HLS32 },
126 		{ BRect(0, 0, 39, 9),	B_HLSA32 },
127 		{ BRect(0, 0, 39, 9),	B_CMY32 },
128 		{ BRect(0, 0, 39, 9),	B_CMYA32 },
129 		{ BRect(0, 0, 39, 9),	B_CMYK32 },
130 		{ BRect(0, 0, 39, 9),	B_RGB24 },
131 		{ BRect(0, 0, 18, 9),	B_RGB24 },
132 		{ BRect(0, 0, 39, 9),	B_RGB24_BIG },
133 		{ BRect(0, 0, 18, 9),	B_RGB24_BIG },
134 		{ BRect(0, 0, 39, 9),	B_UVL24 },
135 		{ BRect(0, 0, 18, 9),	B_UVL24 },
136 		{ BRect(0, 0, 39, 9),	B_LAB24 },
137 		{ BRect(0, 0, 18, 9),	B_LAB24 },
138 		{ BRect(0, 0, 39, 9),	B_HSI24 },
139 		{ BRect(0, 0, 18, 9),	B_HSI24 },
140 		{ BRect(0, 0, 39, 9),	B_HSV24 },
141 		{ BRect(0, 0, 18, 9),	B_HSV24 },
142 		{ BRect(0, 0, 39, 9),	B_HLS24 },
143 		{ BRect(0, 0, 18, 9),	B_HLS24 },
144 		{ BRect(0, 0, 39, 9),	B_CMY24 },
145 		{ BRect(0, 0, 18, 9),	B_CMY24 },
146 		{ BRect(0, 0, 39, 9),	B_RGB16 },
147 		{ BRect(0, 0, 18, 9),	B_RGB16 },
148 		{ BRect(0, 0, 39, 9),	B_RGB16_BIG },
149 		{ BRect(0, 0, 18, 9),	B_RGB16_BIG },
150 		{ BRect(0, 0, 39, 9),	B_RGB15 },
151 		{ BRect(0, 0, 18, 9),	B_RGB15 },
152 		{ BRect(0, 0, 39, 9),	B_RGB15_BIG },
153 		{ BRect(0, 0, 18, 9),	B_RGB15_BIG },
154 		{ BRect(0, 0, 39, 9),	B_RGBA15 },
155 		{ BRect(0, 0, 18, 9),	B_RGBA15 },
156 		{ BRect(0, 0, 39, 9),	B_RGBA15_BIG },
157 		{ BRect(0, 0, 18, 9),	B_RGBA15_BIG },
158 		{ BRect(0, 0, 39, 9),	B_CMAP8 },
159 		{ BRect(0, 0, 18, 9),	B_CMAP8 },
160 		{ BRect(0, 0, 39, 9),	B_GRAY8 },
161 		{ BRect(0, 0, 18, 9),	B_GRAY8 },
162 		{ BRect(0, 0, 39, 9),	B_GRAY1 },
163 		{ BRect(0, 0, 31, 9),	B_GRAY1 },
164 		{ BRect(0, 0, 18, 9),	B_GRAY1 },
165 		{ BRect(0, 0, 39, 9),	B_YCbCr422 },
166 		{ BRect(0, 0, 18, 9),	B_YCbCr422 },
167 		{ BRect(0, 0, 39, 9),	B_YUV422 },
168 		{ BRect(0, 0, 18, 9),	B_YUV422 },
169 // R5: calculates weird BPR values for B_YCbCr411, B_YUV411 and B_YUV420.
170 // TODO: Investigate!
171 #ifndef TEST_R5
172 		{ BRect(0, 0, 39, 9),	B_YCbCr411 },
173 		{ BRect(0, 0, 31, 9),	B_YCbCr411 },
174 		{ BRect(0, 0, 18, 9),	B_YCbCr411 },
175 		{ BRect(0, 0, 39, 9),	B_YUV411 },
176 		{ BRect(0, 0, 31, 9),	B_YUV411 },
177 		{ BRect(0, 0, 18, 9),	B_YUV411 },
178 #endif
179 		{ BRect(0, 0, 39, 9),	B_YCbCr444 },
180 		{ BRect(0, 0, 18, 9),	B_YCbCr444 },
181 		{ BRect(0, 0, 39, 9),	B_YUV444 },
182 		{ BRect(0, 0, 18, 9),	B_YUV444 },
183 		{ BRect(0, 0, 39, 9),	B_YCbCr420 },
184 		{ BRect(0, 0, 31, 9),	B_YCbCr420 },
185 		{ BRect(0, 0, 18, 9),	B_YCbCr420 },
186 #ifndef TEST_R5
187 		{ BRect(0, 0, 39, 9),	B_YUV420 },
188 		{ BRect(0, 0, 31, 9),	B_YUV420 },
189 		{ BRect(0, 0, 18, 9),	B_YUV420 },
190 #endif
191 	};
192 	int32 testCaseCount = sizeof(testCases) / sizeof(test_case);
193 	for (int32 i = 0; i < testCaseCount; i++) {
194 		// get test case
195 		const test_case &testCase = testCases[i];
196 		int32 width = testCase.bounds.IntegerWidth() + 1;
197 		int32 height = testCase.bounds.IntegerHeight() + 1;
198 		int32 bpr = get_bytes_per_row(testCase.space, width);
199 		// create and check bitmap
200 		BBitmap bitmap(testCase.bounds, testCase.space);
201 		CHK(bitmap.InitCheck() == B_OK);
202 		CHK(bitmap.IsValid() == true);
203 		CHK(bitmap.Bits() != NULL);
204 		CHK(bitmap.Bounds() == testCase.bounds);
205 		CHK(bitmap.ColorSpace() == testCase.space);
206 if (bitmap.BytesPerRow() != bpr) {
207 printf("space: %x: bpr: %ld (%ld)\n", testCase.space, bitmap.BytesPerRow(),
208 bpr);
209 }
210 		CHK(bitmap.BytesPerRow() == bpr);
211 		CHK(bitmap.BitsLength() == bpr * height);
212 	}
213 }
214 
215 
216 Test* TBBitmapTester::Suite()
217 {
218 	TestSuite* SuiteOfTests = new TestSuite;
219 
220 	ADD_TEST4(BBitmap, SuiteOfTests, TBBitmapTester, BBitmap1);
221 
222 	return SuiteOfTests;
223 }
224 
225