|
@@ -0,0 +1,296 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
17
|
+ * 02110-1301, USA.
|
|
18
|
+ */
|
|
19
|
+
|
|
20
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
21
|
+
|
|
22
|
+/** @file
|
|
23
|
+ *
|
|
24
|
+ * PNM self-tests
|
|
25
|
+ *
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+/* Forcibly enable assertions */
|
|
29
|
+#undef NDEBUG
|
|
30
|
+
|
|
31
|
+#include <string.h>
|
|
32
|
+#include <assert.h>
|
|
33
|
+#include <ipxe/pixbuf.h>
|
|
34
|
+#include <ipxe/pnm.h>
|
|
35
|
+#include <ipxe/test.h>
|
|
36
|
+
|
|
37
|
+/** Define inline pixel data */
|
|
38
|
+#define DATA(...) { __VA_ARGS__ }
|
|
39
|
+
|
|
40
|
+/** A PNM test */
|
|
41
|
+struct pnm_test {
|
|
42
|
+ /** Source image */
|
|
43
|
+ struct image *image;
|
|
44
|
+ /** Pixel data */
|
|
45
|
+ const uint32_t *data;
|
|
46
|
+ /** Length of pixel data */
|
|
47
|
+ size_t len;
|
|
48
|
+ /** Width */
|
|
49
|
+ unsigned int width;
|
|
50
|
+ /** Height */
|
|
51
|
+ unsigned int height;
|
|
52
|
+};
|
|
53
|
+
|
|
54
|
+/** Define a PNM test */
|
|
55
|
+#define PNM( NAME, FILE, WIDTH, HEIGHT, DATA ) \
|
|
56
|
+ static const char NAME ## _file[] = FILE; \
|
|
57
|
+ static const uint32_t NAME ## _data[] = DATA; \
|
|
58
|
+ static struct image NAME ## _image = { \
|
|
59
|
+ .refcnt = REF_INIT ( ref_no_free ), \
|
|
60
|
+ .name = #NAME, \
|
|
61
|
+ .data = ( userptr_t ) ( NAME ## _file ), \
|
|
62
|
+ .len = sizeof ( NAME ## _file ), \
|
|
63
|
+ }; \
|
|
64
|
+ static struct pnm_test NAME = { \
|
|
65
|
+ .image = & NAME ## _image, \
|
|
66
|
+ .data = NAME ## _data, \
|
|
67
|
+ .len = sizeof ( NAME ## _data ), \
|
|
68
|
+ .width = WIDTH, \
|
|
69
|
+ .height = HEIGHT, \
|
|
70
|
+ };
|
|
71
|
+
|
|
72
|
+/** PBM ASCII example (from Wikipedia) */
|
|
73
|
+PNM ( pbm_ascii,
|
|
74
|
+ "P1\n"
|
|
75
|
+ "# This is an example bitmap of the letter \"J\"\n"
|
|
76
|
+ "6 10\n"
|
|
77
|
+ "0 0 0 0 1 0\n"
|
|
78
|
+ "0 0 0 0 1 0\n"
|
|
79
|
+ "0 0 0 0 1 0\n"
|
|
80
|
+ "0 0 0 0 1 0\n"
|
|
81
|
+ "0 0 0 0 1 0\n"
|
|
82
|
+ "0 0 0 0 1 0\n"
|
|
83
|
+ "1 0 0 0 1 0\n"
|
|
84
|
+ "0 1 1 1 0 0\n"
|
|
85
|
+ "0 0 0 0 0 0\n"
|
|
86
|
+ "0 0 0 0 0 0\n",
|
|
87
|
+ 6, 10,
|
|
88
|
+ DATA ( 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
89
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
90
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
91
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
92
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
93
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
94
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
95
|
+ 0xffffff, 0x000000, 0x000000, 0x000000, 0xffffff, 0xffffff,
|
|
96
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
|
|
97
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
|
|
98
|
+
|
|
99
|
+/** PGM ASCII example (from Wikipedia) */
|
|
100
|
+PNM ( pgm_ascii,
|
|
101
|
+ "P2\n"
|
|
102
|
+ "# Shows the word \"FEEP\" (example from Netpbm man page on PGM)\n"
|
|
103
|
+ "24 7\n"
|
|
104
|
+ "15\n"
|
|
105
|
+ "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"
|
|
106
|
+ "0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0\n"
|
|
107
|
+ "0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0\n"
|
|
108
|
+ "0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0\n"
|
|
109
|
+ "0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0\n"
|
|
110
|
+ "0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0\n"
|
|
111
|
+ "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"
|
|
112
|
+ , 24, 7,
|
|
113
|
+ DATA ( 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
114
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
115
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
116
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
117
|
+ 0x000000, 0x333333, 0x333333, 0x333333, 0x333333, 0x000000,
|
|
118
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
|
|
119
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
|
|
120
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
|
|
121
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
122
|
+ 0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
123
|
+ 0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
124
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0xffffff, 0x000000,
|
|
125
|
+ 0x000000, 0x333333, 0x333333, 0x333333, 0x000000, 0x000000,
|
|
126
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x000000, 0x000000,
|
|
127
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000, 0x000000,
|
|
128
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
|
|
129
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
130
|
+ 0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
131
|
+ 0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
132
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
133
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
134
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
|
|
135
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
|
|
136
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
137
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
138
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
139
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
140
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
|
|
141
|
+
|
|
142
|
+/** PPM ASCII example (from Wikipedia) */
|
|
143
|
+PNM ( ppm_ascii,
|
|
144
|
+ "P3\n"
|
|
145
|
+ "# The P3 means colors are in ASCII, then 3 columns and 2 rows,\n"
|
|
146
|
+ "# then 255 for max color, then RGB triplets\n"
|
|
147
|
+ "3 2\n"
|
|
148
|
+ "255\n"
|
|
149
|
+ "255 0 0 0 255 0 0 0 255\n"
|
|
150
|
+ "255 255 0 255 255 255 0 0 0\n",
|
|
151
|
+ 3, 2,
|
|
152
|
+ DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
|
|
153
|
+
|
|
154
|
+/** PBM ASCII with no space between pixel values */
|
|
155
|
+PNM ( pbm_ascii_no_space,
|
|
156
|
+ "P1\n"
|
|
157
|
+ "3 3\n"
|
|
158
|
+ "001\n"
|
|
159
|
+ "010\n"
|
|
160
|
+ "111\n",
|
|
161
|
+ 3, 3,
|
|
162
|
+ DATA ( 0xffffff, 0xffffff, 0x000000, 0xffffff, 0x000000, 0xffffff,
|
|
163
|
+ 0x000000, 0x000000, 0x000000 ) );
|
|
164
|
+
|
|
165
|
+/** PBM binary example (converted from Wikipedia) */
|
|
166
|
+PNM ( pbm_binary,
|
|
167
|
+ DATA ( 0x50, 0x34, 0x0a, 0x23, 0x20, 0x43, 0x52, 0x45, 0x41, 0x54, 0x4f,
|
|
168
|
+ 0x52, 0x3a, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x20, 0x50, 0x4e, 0x4d,
|
|
169
|
+ 0x20, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x20, 0x56, 0x65, 0x72,
|
|
170
|
+ 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x2e, 0x31, 0x0a, 0x36, 0x20,
|
|
171
|
+ 0x31, 0x30, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x88, 0x70,
|
|
172
|
+ 0x00, 0x00 ),
|
|
173
|
+ 6, 10,
|
|
174
|
+ DATA ( 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
175
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
176
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
177
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
178
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
179
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
180
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
|
|
181
|
+ 0xffffff, 0x000000, 0x000000, 0x000000, 0xffffff, 0xffffff,
|
|
182
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
|
|
183
|
+ 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
|
|
184
|
+
|
|
185
|
+/** PGM binary example (converted from Wikipedia) */
|
|
186
|
+PNM ( pgm_binary,
|
|
187
|
+ DATA ( 0x50, 0x35, 0x0a, 0x32, 0x34, 0x20, 0x37, 0x0a, 0x31, 0x35, 0x0a,
|
|
188
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
189
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
190
|
+ 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x07, 0x07,
|
|
191
|
+ 0x07, 0x07, 0x00, 0x00, 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x00, 0x0f,
|
|
192
|
+ 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
193
|
+ 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00,
|
|
194
|
+ 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x03, 0x03, 0x03, 0x00,
|
|
195
|
+ 0x00, 0x00, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x0b, 0x0b,
|
|
196
|
+ 0x00, 0x00, 0x00, 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x03, 0x00,
|
|
197
|
+ 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
|
|
198
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
199
|
+ 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x07, 0x07, 0x00,
|
|
200
|
+ 0x00, 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
|
|
201
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
202
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
203
|
+ 0x00, 0x00, 0x00 ),
|
|
204
|
+ 24, 7,
|
|
205
|
+ DATA ( 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
206
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
207
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
208
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
209
|
+ 0x000000, 0x333333, 0x333333, 0x333333, 0x333333, 0x000000,
|
|
210
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
|
|
211
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
|
|
212
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
|
|
213
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
214
|
+ 0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
215
|
+ 0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
216
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0xffffff, 0x000000,
|
|
217
|
+ 0x000000, 0x333333, 0x333333, 0x333333, 0x000000, 0x000000,
|
|
218
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x000000, 0x000000,
|
|
219
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000, 0x000000,
|
|
220
|
+ 0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
|
|
221
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
222
|
+ 0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
223
|
+ 0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
224
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
225
|
+ 0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
226
|
+ 0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
|
|
227
|
+ 0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
|
|
228
|
+ 0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
229
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
230
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
231
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
|
|
232
|
+ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
|
|
233
|
+
|
|
234
|
+/** PPM binary example (converted from Wikipedia) */
|
|
235
|
+PNM ( ppm_binary,
|
|
236
|
+ DATA ( 0x50, 0x36, 0x0a, 0x33, 0x20, 0x32, 0x0a, 0x32, 0x35, 0x35, 0x0a,
|
|
237
|
+ 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
|
|
238
|
+ 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 ),
|
|
239
|
+ 3, 2,
|
|
240
|
+ DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
|
|
241
|
+
|
|
242
|
+/**
|
|
243
|
+ * Report PNM test result
|
|
244
|
+ *
|
|
245
|
+ * @v test PNM test
|
|
246
|
+ */
|
|
247
|
+#define pnm_ok( test ) do { \
|
|
248
|
+ struct pixel_buffer *pixbuf; \
|
|
249
|
+ uint8_t data[ (test)->len ]; \
|
|
250
|
+ int rc; \
|
|
251
|
+ \
|
|
252
|
+ /* Sanity check */ \
|
|
253
|
+ assert ( ( (test)->width * (test)->height * \
|
|
254
|
+ sizeof ( (test)->data[0] ) ) == (test)->len ); \
|
|
255
|
+ \
|
|
256
|
+ /* Correct image data pointer */ \
|
|
257
|
+ (test)->image->data = \
|
|
258
|
+ virt_to_user ( ( void * ) (test)->image->data ); \
|
|
259
|
+ \
|
|
260
|
+ /* Perform tests */ \
|
|
261
|
+ ok ( image_probe ( (test)->image ) == 0 ); \
|
|
262
|
+ ok ( (test)->image->type == &pnm_image_type ); \
|
|
263
|
+ ok ( ( rc = image_pixbuf ( (test)->image, &pixbuf ) ) == 0 ); \
|
|
264
|
+ if ( rc == 0 ) { \
|
|
265
|
+ ok ( pixbuf->width == (test)->width ); \
|
|
266
|
+ ok ( pixbuf->height == (test)->height ); \
|
|
267
|
+ ok ( pixbuf->len == (test)->len ); \
|
|
268
|
+ copy_from_user ( data, pixbuf->data, 0, \
|
|
269
|
+ sizeof ( data ) ); \
|
|
270
|
+ ok ( memcmp ( data, (test)->data, \
|
|
271
|
+ sizeof ( data ) ) == 0 ); \
|
|
272
|
+ DBGC_HDA ( (test)->image, 0, data, sizeof ( data ) ); \
|
|
273
|
+ pixbuf_put ( pixbuf ); \
|
|
274
|
+ } \
|
|
275
|
+ } while ( 0 )
|
|
276
|
+
|
|
277
|
+/**
|
|
278
|
+ * Perform PNM self-test
|
|
279
|
+ *
|
|
280
|
+ */
|
|
281
|
+static void pnm_test_exec ( void ) {
|
|
282
|
+
|
|
283
|
+ pnm_ok ( &pbm_ascii );
|
|
284
|
+ pnm_ok ( &pgm_ascii );
|
|
285
|
+ pnm_ok ( &ppm_ascii );
|
|
286
|
+ pnm_ok ( &pbm_ascii_no_space );
|
|
287
|
+ pnm_ok ( &pbm_binary );
|
|
288
|
+ pnm_ok ( &pgm_binary );
|
|
289
|
+ pnm_ok ( &ppm_binary );
|
|
290
|
+}
|
|
291
|
+
|
|
292
|
+/** PNM self-test */
|
|
293
|
+struct self_test pnm_test __self_test = {
|
|
294
|
+ .name = "pnm",
|
|
295
|
+ .exec = pnm_test_exec,
|
|
296
|
+};
|