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