Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / image / pnm.c
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  * Portable anymap format (PNM)
25  *
26  */
27
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <ipxe/image.h>
32 #include <ipxe/pixbuf.h>
33 #include <ipxe/pnm.h>
34
35 /**
36  * Extract PNM ASCII value
37  *
38  * @v image             PNM image
39  * @v pnm               PNM context
40  * @ret value           Value, or negative error
41  */
42 static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
43         char buf[ pnm->ascii_len + 1 /* NUL */ ];
44         char *endp;
45         size_t len;
46         int value;
47         int in_comment = 0;
48
49         /* Skip any leading whitespace and comments */
50         for ( ; pnm->offset < image->len ; pnm->offset++ ) {
51                 copy_from_user ( &buf[0], image->data, pnm->offset,
52                                  sizeof ( buf[0] ) );
53                 if ( in_comment ) {
54                         if ( buf[0] == '\n' )
55                                 in_comment = 0;
56                 } else {
57                         if ( buf[0] == '#' ) {
58                                 in_comment = 1;
59                         } else if ( ! isspace ( buf[0] ) ) {
60                                 break;
61                         }
62                 }
63         }
64
65         /* Fail if no value is present */
66         len = ( image->len - pnm->offset );
67         if ( len == 0 ) {
68                 DBGC ( image, "PNM %s ran out of ASCII data\n", image->name );
69                 return -EINVAL;
70         }
71
72         /* Copy ASCII value to buffer and ensure string is NUL-terminated */
73         if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
74                 len = ( sizeof ( buf ) - 1 /* NUL */ );
75         copy_from_user ( buf, image->data, pnm->offset, len );
76         buf[len] = '\0';
77
78         /* Parse value and update offset */
79         value = strtoul ( buf, &endp, 0 );
80         pnm->offset += ( endp - buf );
81
82         /* Check and skip terminating whitespace character, if present */
83         if ( ( pnm->offset != image->len ) && ( *endp != '\0' ) ) {
84                 if ( ! isspace ( *endp ) ) {
85                         DBGC ( image, "PNM %s invalid ASCII integer\n",
86                                image->name );
87                         return -EINVAL;
88                 }
89                 pnm->offset++;
90         }
91
92         return value;
93 }
94
95 /**
96  * Extract PNM binary value
97  *
98  * @v image             PNM image
99  * @v pnm               PNM context
100  * @ret value           Value, or negative error
101  */
102 static int pnm_binary ( struct image *image, struct pnm_context *pnm ) {
103         uint8_t value;
104
105         /* Sanity check */
106         if ( pnm->offset == image->len ) {
107                 DBGC ( image, "PNM %s ran out of binary data\n",
108                        image->name );
109                 return -EINVAL;
110         }
111
112         /* Extract value */
113         copy_from_user ( &value, image->data, pnm->offset, sizeof ( value ) );
114         pnm->offset++;
115
116         return value;
117 }
118
119 /**
120  * Scale PNM scalar value
121  *
122  * @v image             PNM image
123  * @v pnm               PNM context
124  * @v value             Raw value
125  * @ret value           Scaled value (in range 0-255)
126  */
127 static int pnm_scale ( struct image *image, struct pnm_context *pnm,
128                        unsigned int value ) {
129
130         if ( value > pnm->max ) {
131                 DBGC ( image, "PNM %s has out-of-range value %d (max %d)\n",
132                        image->name, value, pnm->max );
133                 return -EINVAL;
134         }
135         return ( ( 255 * value ) / pnm->max );
136 }
137
138 /**
139  * Convert PNM bitmap composite value to RGB
140  *
141  * @v composite         Composite value
142  * @v index             Pixel index within this composite value
143  * @ret rgb             24-bit RGB value
144  */
145 static uint32_t pnm_bitmap ( uint32_t composite, unsigned int index ) {
146
147         /* Composite value is an 8-bit bitmask */
148         return ( ( ( composite << index ) & 0x80 ) ? 0x000000 : 0xffffff );
149 }
150
151 /**
152  * Convert PNM greymap composite value to RGB
153  *
154  * @v composite         Composite value
155  * @v index             Pixel index within this composite value
156  * @ret rgb             24-bit RGB value
157  */
158 static uint32_t pnm_greymap ( uint32_t composite, unsigned int index __unused ){
159
160         /* Composite value is an 8-bit greyscale value */
161         return ( ( composite << 16 ) | ( composite << 8 ) | composite );
162 }
163
164 /**
165  * Convert PNM pixmap composite value to RGB
166  *
167  * @v composite         Composite value
168  * @v index             Pixel index within this composite value
169  * @ret rgb             24-bit RGB value
170  */
171 static uint32_t pnm_pixmap ( uint32_t composite, unsigned int index __unused ) {
172
173         /* Composite value is already an RGB value */
174         return composite;
175 }
176
177 /**
178  * Extract PNM pixel data
179  *
180  * @v image             PNM image
181  * @v pnm               PNM context
182  * @v pixbuf            Pixel buffer
183  * @ret rc              Return status code
184  */
185 static int pnm_data ( struct image *image, struct pnm_context *pnm,
186                       struct pixel_buffer *pixbuf ) {
187         struct pnm_type *type = pnm->type;
188         size_t offset = 0;
189         unsigned int xpos = 0;
190         int scalar;
191         uint32_t composite;
192         uint32_t rgb;
193         unsigned int i;
194
195         /* Fill pixel buffer */
196         while ( offset < pixbuf->len ) {
197
198                 /* Extract a scaled composite scalar value from the file */
199                 composite = 0;
200                 for ( i = 0 ; i < type->depth ; i++ ) {
201                         scalar = type->scalar ( image, pnm );
202                         if ( scalar < 0 )
203                                 return scalar;
204                         scalar = pnm_scale ( image, pnm, scalar );
205                         if ( scalar < 0 )
206                                 return scalar;
207                         composite = ( ( composite << 8 ) | scalar );
208                 }
209
210                 /* Extract 24-bit RGB values from composite value */
211                 for ( i = 0 ; i < type->packing ; i++ ) {
212                         if ( offset >= pixbuf->len ) {
213                                 DBGC ( image, "PNM %s has too many pixels\n",
214                                        image->name );
215                                 return -EINVAL;
216                         }
217                         rgb = type->rgb ( composite, i );
218                         copy_to_user ( pixbuf->data, offset, &rgb,
219                                        sizeof ( rgb ) );
220                         offset += sizeof ( rgb );
221                         if ( ++xpos == pixbuf->width ) {
222                                 xpos = 0;
223                                 break;
224                         }
225                 }
226         }
227
228         return 0;
229 }
230
231 /** PNM image types */
232 static struct pnm_type pnm_types[] = {
233         {
234                 .type = '1',
235                 .depth = 1,
236                 .packing = 1,
237                 .flags = PNM_BITMAP,
238                 .scalar = pnm_ascii,
239                 .rgb = pnm_bitmap,
240         },
241         {
242                 .type = '2',
243                 .depth = 1,
244                 .packing = 1,
245                 .scalar = pnm_ascii,
246                 .rgb = pnm_greymap,
247         },
248         {
249                 .type = '3',
250                 .depth = 3,
251                 .packing = 1,
252                 .scalar = pnm_ascii,
253                 .rgb = pnm_pixmap,
254         },
255         {
256                 .type = '4',
257                 .depth = 1,
258                 .packing = 8,
259                 .flags = PNM_BITMAP,
260                 .scalar = pnm_binary,
261                 .rgb = pnm_bitmap,
262         },
263         {
264                 .type = '5',
265                 .depth = 1,
266                 .packing = 1,
267                 .scalar = pnm_binary,
268                 .rgb = pnm_greymap,
269         },
270         {
271                 .type = '6',
272                 .depth = 3,
273                 .packing = 1,
274                 .scalar = pnm_binary,
275                 .rgb = pnm_pixmap,
276         },
277 };
278
279 /**
280  * Determine PNM image type
281  *
282  * @v image             PNM image
283  * @ret type            PNM image type, or NULL if not found
284  */
285 static struct pnm_type * pnm_type ( struct image *image ) {
286         struct pnm_signature signature;
287         struct pnm_type *type;
288         unsigned int i;
289
290         /* Extract signature */
291         assert ( image->len >= sizeof ( signature ) );
292         copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
293
294         /* Check for supported types */
295         for ( i = 0 ; i < ( sizeof ( pnm_types ) /
296                             sizeof ( pnm_types[0] ) ) ; i++ ) {
297                 type = &pnm_types[i];
298                 if ( type->type == signature.type )
299                         return type;
300         }
301         return NULL;
302 }
303
304 /**
305  * Convert PNM image to pixel buffer
306  *
307  * @v image             PNM image
308  * @v pixbuf            Pixel buffer to fill in
309  * @ret rc              Return status code
310  */
311 static int pnm_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
312         struct pnm_context pnm;
313         int width;
314         int height;
315         int max;
316         int rc;
317
318         /* Initialise PNM context */
319         pnm.type = pnm_type ( image );
320         if ( ! pnm.type ) {
321                 rc = -ENOTSUP;
322                 goto err_type;
323         }
324         pnm.offset = sizeof ( struct pnm_signature );
325         pnm.ascii_len = PNM_ASCII_LEN;
326
327         /* Extract width */
328         if ( ( width = pnm_ascii ( image, &pnm ) ) < 0 ) {
329                 rc = width;
330                 goto err_width;
331         }
332
333         /* Extract height */
334         if ( ( height = pnm_ascii ( image, &pnm ) ) < 0 ) {
335                 rc = height;
336                 goto err_height;
337         }
338
339         /* Extract maximum scalar value, if not predefined */
340         if ( pnm.type->flags & PNM_BITMAP ) {
341                 pnm.max = ( ( 1 << pnm.type->packing ) - 1 );
342                 pnm.ascii_len = 1;
343         } else {
344                 if ( ( max = pnm_ascii ( image, &pnm ) ) < 0 ) {
345                         rc = max;
346                         goto err_max;
347                 }
348                 pnm.max = max;
349         }
350         if ( pnm.max == 0 ) {
351                 DBGC ( image, "PNM %s has invalid maximum value 0\n",
352                        image->name );
353                 rc = -EINVAL;
354                 goto err_max;
355         }
356         DBGC ( image, "PNM %s is type %c width %d height %d max %d\n",
357                image->name, pnm.type->type, width, height, pnm.max );
358
359         /* Allocate pixel buffer */
360         *pixbuf = alloc_pixbuf ( width, height );
361         if ( ! *pixbuf ) {
362                 rc = -ENOMEM;
363                 goto err_alloc_pixbuf;
364         }
365
366         /* Extract pixel data */
367         if ( ( rc = pnm_data ( image, &pnm, *pixbuf ) ) != 0 )
368                 goto err_data;
369
370         return 0;
371
372  err_data:
373         pixbuf_put ( *pixbuf );
374  err_alloc_pixbuf:
375  err_max:
376  err_height:
377  err_width:
378  err_type:
379         return rc;
380 }
381
382 /**
383  * Probe PNM image
384  *
385  * @v image             PNM image
386  * @ret rc              Return status code
387  */
388 static int pnm_probe ( struct image *image ) {
389         struct pnm_signature signature;
390
391         /* Sanity check */
392         if ( image->len < sizeof ( signature ) ) {
393                 DBGC ( image, "PNM %s is too short\n", image->name );
394                 return -ENOEXEC;
395         }
396
397         /* Check signature */
398         copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
399         if ( ! ( ( signature.magic == PNM_MAGIC ) &&
400                  ( isdigit ( signature.type ) ) &&
401                  ( isspace ( signature.space ) ) ) ) {
402                 DBGC ( image, "PNM %s has invalid signature\n", image->name );
403                 return -ENOEXEC;
404         }
405         DBGC ( image, "PNM %s is type %c\n", image->name, signature.type );
406
407         return 0;
408 }
409
410 /** PNM image type */
411 struct image_type pnm_image_type __image_type ( PROBE_NORMAL ) = {
412         .name = "PNM",
413         .probe = pnm_probe,
414         .pixbuf = pnm_pixbuf,
415 };