Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / image / png.c
1 /*
2  * Copyright (C) 2014 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 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/umalloc.h>
28 #include <ipxe/pixbuf.h>
29 #include <ipxe/deflate.h>
30 #include <ipxe/png.h>
31
32 /** @file
33  *
34  * Portable Network Graphics (PNG) format
35  *
36  * The PNG format is defined in RFC 2083.
37  */
38
39 /** PNG context */
40 struct png_context {
41         /** Offset within image */
42         size_t offset;
43
44         /** Pixel buffer */
45         struct pixel_buffer *pixbuf;
46
47         /** Bit depth */
48         unsigned int depth;
49         /** Colour type */
50         unsigned int colour_type;
51         /** Number of channels */
52         unsigned int channels;
53         /** Number of interlace passes */
54         unsigned int passes;
55         /** Palette, in iPXE's pixel buffer format */
56         uint32_t palette[PNG_PALETTE_COUNT];
57
58         /** Decompression buffer for raw PNG data */
59         struct deflate_chunk raw;
60         /** Decompressor */
61         struct deflate deflate;
62 };
63
64 /** A PNG interlace pass */
65 struct png_interlace {
66         /** Pass number */
67         unsigned int pass;
68         /** X starting indent */
69         unsigned int x_indent;
70         /** Y starting indent */
71         unsigned int y_indent;
72         /** X stride */
73         unsigned int x_stride;
74         /** Y stride */
75         unsigned int y_stride;
76         /** Width */
77         unsigned int width;
78         /** Height */
79         unsigned int height;
80 };
81
82 /** PNG file signature */
83 static struct png_signature png_signature = PNG_SIGNATURE;
84
85 /** Number of interlacing passes */
86 static uint8_t png_interlace_passes[] = {
87         [PNG_INTERLACE_NONE] = 1,
88         [PNG_INTERLACE_ADAM7] = 7,
89 };
90
91 /**
92  * Transcribe PNG chunk type name (for debugging)
93  *
94  * @v type              Chunk type
95  * @ret name            Chunk type name
96  */
97 static const char * png_type_name ( uint32_t type ) {
98         static union {
99                 uint32_t type;
100                 char name[ sizeof ( uint32_t ) + 1 /* NUL */ ];
101         } u;
102
103         u.type = type;
104         return u.name;
105 }
106
107 /**
108  * Calculate PNG interlace pass parameters
109  *
110  * @v png               PNG context
111  * @v pass              Pass number (0=first pass)
112  * @v interlace         Interlace pass to fill in
113  */
114 static void png_interlace ( struct png_context *png, unsigned int pass,
115                             struct png_interlace *interlace ) {
116         unsigned int grid_width_log2;
117         unsigned int grid_height_log2;
118         unsigned int x_indent;
119         unsigned int y_indent;
120         unsigned int x_stride_log2;
121         unsigned int y_stride_log2;
122         unsigned int x_stride;
123         unsigned int y_stride;
124         unsigned int width;
125         unsigned int height;
126
127         /* Sanity check */
128         assert ( png->passes > 0 );
129
130         /* Store pass number */
131         interlace->pass = pass;
132
133         /* Calculate interlace grid dimensions */
134         grid_width_log2 = ( png->passes / 2 );
135         grid_height_log2 = ( ( png->passes - 1 ) / 2 );
136
137         /* Calculate starting indents */
138         interlace->x_indent = x_indent =
139                 ( ( pass & 1 ) ?
140                   ( 1 << ( grid_width_log2 - ( pass / 2 ) - 1 ) ) : 0 );
141         interlace->y_indent = y_indent =
142                 ( ( pass && ! ( pass & 1 ) ) ?
143                   ( 1 << ( grid_height_log2 - ( ( pass - 1 ) / 2 ) - 1 ) ) : 0);
144
145         /* Calculate strides */
146         x_stride_log2 = ( grid_width_log2 - ( pass / 2 ) );
147         y_stride_log2 =
148                 ( grid_height_log2 - ( pass ? ( ( pass - 1 ) / 2 ) : 0 ) );
149         interlace->x_stride = x_stride = ( 1 << x_stride_log2 );
150         interlace->y_stride = y_stride = ( 1 << y_stride_log2 );
151
152         /* Calculate pass dimensions */
153         width = png->pixbuf->width;
154         height = png->pixbuf->height;
155         interlace->width =
156                 ( ( width - x_indent + x_stride - 1 ) >> x_stride_log2 );
157         interlace->height =
158                 ( ( height - y_indent + y_stride - 1 ) >> y_stride_log2 );
159 }
160
161 /**
162  * Calculate PNG pixel length
163  *
164  * @v png               PNG context
165  * @ret pixel_len       Pixel length
166  */
167 static unsigned int png_pixel_len ( struct png_context *png ) {
168
169         return ( ( ( png->channels * png->depth ) + 7 ) / 8 );
170 }
171
172 /**
173  * Calculate PNG scanline length
174  *
175  * @v png               PNG context
176  * @v interlace         Interlace pass
177  * @ret scanline_len    Scanline length (including filter byte)
178  */
179 static size_t png_scanline_len ( struct png_context *png,
180                                  struct png_interlace *interlace ) {
181
182         return ( 1 /* Filter byte */ +
183                  ( ( interlace->width * png->channels * png->depth ) + 7 ) / 8);
184 }
185
186 /**
187  * Handle PNG image header chunk
188  *
189  * @v image             PNG image
190  * @v png               PNG context
191  * @v len               Chunk length
192  * @ret rc              Return status code
193  */
194 static int png_image_header ( struct image *image, struct png_context *png,
195                               size_t len ) {
196         struct png_image_header ihdr;
197         struct png_interlace interlace;
198         unsigned int pass;
199
200         /* Sanity check */
201         if ( len != sizeof ( ihdr ) ) {
202                 DBGC ( image, "PNG %s invalid IHDR length %zd\n",
203                        image->name, len );
204                 return -EINVAL;
205         }
206         if ( png->pixbuf ) {
207                 DBGC ( image, "PNG %s duplicate IHDR\n", image->name );
208                 return -EINVAL;
209         }
210
211         /* Extract image header */
212         copy_from_user ( &ihdr, image->data, png->offset, len );
213         DBGC ( image, "PNG %s %dx%d depth %d type %d compression %d filter %d "
214                "interlace %d\n", image->name, ntohl ( ihdr.width ),
215                ntohl ( ihdr.height ), ihdr.depth, ihdr.colour_type,
216                ihdr.compression, ihdr.filter, ihdr.interlace );
217
218         /* Sanity checks */
219         if ( ihdr.compression >= PNG_COMPRESSION_UNKNOWN ) {
220                 DBGC ( image, "PNG %s unknown compression method %d\n",
221                        image->name, ihdr.compression );
222                 return -ENOTSUP;
223         }
224         if ( ihdr.filter >= PNG_FILTER_UNKNOWN ) {
225                 DBGC ( image, "PNG %s unknown filter method %d\n",
226                        image->name, ihdr.filter );
227                 return -ENOTSUP;
228         }
229         if ( ihdr.interlace >= PNG_INTERLACE_UNKNOWN ) {
230                 DBGC ( image, "PNG %s unknown interlace method %d\n",
231                        image->name, ihdr.interlace );
232                 return -ENOTSUP;
233         }
234
235         /* Allocate pixel buffer */
236         png->pixbuf = alloc_pixbuf ( ntohl ( ihdr.width ),
237                                      ntohl ( ihdr.height ) );
238         if ( ! png->pixbuf ) {
239                 DBGC ( image, "PNG %s could not allocate pixel buffer\n",
240                        image->name );
241                 return -ENOMEM;
242         }
243
244         /* Extract bit depth */
245         png->depth = ihdr.depth;
246         if ( ( png->depth == 0 ) ||
247              ( ( png->depth & ( png->depth - 1 ) ) != 0 ) ) {
248                 DBGC ( image, "PNG %s invalid depth %d\n",
249                        image->name, png->depth );
250                 return -EINVAL;
251         }
252
253         /* Calculate number of channels */
254         png->colour_type = ihdr.colour_type;
255         png->channels = 1;
256         if ( ! ( ihdr.colour_type & PNG_COLOUR_TYPE_PALETTE ) ) {
257                 if ( ihdr.colour_type & PNG_COLOUR_TYPE_RGB )
258                         png->channels += 2;
259                 if ( ihdr.colour_type & PNG_COLOUR_TYPE_ALPHA )
260                         png->channels += 1;
261         }
262
263         /* Calculate number of interlace passes */
264         png->passes = png_interlace_passes[ihdr.interlace];
265
266         /* Calculate length of raw data buffer */
267         for ( pass = 0 ; pass < png->passes ; pass++ ) {
268                 png_interlace ( png, pass, &interlace );
269                 if ( interlace.width == 0 )
270                         continue;
271                 png->raw.len += ( interlace.height *
272                                   png_scanline_len ( png, &interlace ) );
273         }
274
275         /* Allocate raw data buffer */
276         png->raw.data = umalloc ( png->raw.len );
277         if ( ! png->raw.data ) {
278                 DBGC ( image, "PNG %s could not allocate data buffer\n",
279                        image->name );
280                 return -ENOMEM;
281         }
282
283         return 0;
284 }
285
286 /**
287  * Handle PNG palette chunk
288  *
289  * @v image             PNG image
290  * @v png               PNG context
291  * @v len               Chunk length
292  * @ret rc              Return status code
293  */
294 static int png_palette ( struct image *image, struct png_context *png,
295                          size_t len ) {
296         size_t offset = png->offset;
297         struct png_palette_entry palette;
298         unsigned int i;
299
300         /* Populate palette */
301         for ( i = 0 ; i < ( sizeof ( png->palette ) /
302                             sizeof ( png->palette[0] ) ) ; i++ ) {
303
304                 /* Stop when we run out of palette data */
305                 if ( len < sizeof ( palette ) )
306                         break;
307
308                 /* Extract palette entry */
309                 copy_from_user ( &palette, image->data, offset,
310                                  sizeof ( palette ) );
311                 png->palette[i] = ( ( palette.red << 16 ) |
312                                     ( palette.green << 8 ) |
313                                     ( palette.blue << 0 ) );
314                 DBGC2 ( image, "PNG %s palette entry %d is %#06x\n",
315                         image->name, i, png->palette[i] );
316
317                 /* Move to next entry */
318                 offset += sizeof ( palette );
319                 len -= sizeof ( palette );
320         }
321
322         return 0;
323 }
324
325 /**
326  * Handle PNG image data chunk
327  *
328  * @v image             PNG image
329  * @v png               PNG context
330  * @v len               Chunk length
331  * @ret rc              Return status code
332  */
333 static int png_image_data ( struct image *image, struct png_context *png,
334                             size_t len ) {
335         struct deflate_chunk in;
336         int rc;
337
338         /* Deflate this chunk */
339         deflate_chunk_init ( &in, image->data, png->offset,
340                              ( png->offset + len ) );
341         if ( ( rc = deflate_inflate ( &png->deflate, &in, &png->raw ) ) != 0 ) {
342                 DBGC ( image, "PNG %s could not decompress: %s\n",
343                        image->name, strerror ( rc ) );
344                 return rc;
345         }
346
347         return 0;
348 }
349
350 /**
351  * Unfilter byte using the "None" filter
352  *
353  * @v current           Filtered current byte
354  * @v left              Unfiltered left byte
355  * @v above             Unfiltered above byte
356  * @v above_left        Unfiltered above-left byte
357  * @ret current         Unfiltered current byte
358  */
359 static unsigned int png_unfilter_none ( unsigned int current,
360                                         unsigned int left __unused,
361                                         unsigned int above __unused,
362                                         unsigned int above_left __unused ) {
363
364         return current;
365 }
366
367 /**
368  * Unfilter byte using the "Sub" filter
369  *
370  * @v current           Filtered current byte
371  * @v left              Unfiltered left byte
372  * @v above             Unfiltered above byte
373  * @v above_left        Unfiltered above-left byte
374  * @ret current         Unfiltered current byte
375  */
376 static unsigned int png_unfilter_sub ( unsigned int current,
377                                        unsigned int left,
378                                        unsigned int above __unused,
379                                        unsigned int above_left __unused ) {
380
381         return ( current + left );
382 }
383
384 /**
385  * Unfilter byte using the "Up" filter
386  *
387  * @v current           Filtered current byte
388  * @v left              Unfiltered left byte
389  * @v above             Unfiltered above byte
390  * @v above_left        Unfiltered above-left byte
391  * @ret current         Unfiltered current byte
392  */
393 static unsigned int png_unfilter_up ( unsigned int current,
394                                       unsigned int left __unused,
395                                       unsigned int above,
396                                       unsigned int above_left __unused ) {
397
398         return ( current + above );
399 }
400
401 /**
402  * Unfilter byte using the "Average" filter
403  *
404  * @v current           Filtered current byte
405  * @v left              Unfiltered left byte
406  * @v above             Unfiltered above byte
407  * @v above_left        Unfiltered above-left byte
408  * @ret current         Unfiltered current byte
409  */
410 static unsigned int png_unfilter_average ( unsigned int current,
411                                            unsigned int left,
412                                            unsigned int above,
413                                            unsigned int above_left __unused ) {
414
415         return ( current + ( ( above + left ) >> 1 ) );
416 }
417
418 /**
419  * Paeth predictor function (defined in RFC 2083)
420  *
421  * @v a                 Pixel A
422  * @v b                 Pixel B
423  * @v c                 Pixel C
424  * @ret predictor       Predictor pixel
425  */
426 static unsigned int png_paeth_predictor ( unsigned int a, unsigned int b,
427                                           unsigned int c ) {
428         unsigned int p;
429         unsigned int pa;
430         unsigned int pb;
431         unsigned int pc;
432
433         /* Algorithm as defined in RFC 2083 section 6.6 */
434         p = ( a + b - c );
435         pa = abs ( p - a );
436         pb = abs ( p - b );
437         pc = abs ( p - c );
438         if ( ( pa <= pb ) && ( pa <= pc ) ) {
439                 return a;
440         } else if ( pb <= pc ) {
441                 return b;
442         } else {
443                 return c;
444         }
445 }
446
447 /**
448  * Unfilter byte using the "Paeth" filter
449  *
450  * @v current           Filtered current byte
451  * @v above_left        Unfiltered above-left byte
452  * @v above             Unfiltered above byte
453  * @v left              Unfiltered left byte
454  * @ret current         Unfiltered current byte
455  */
456 static unsigned int png_unfilter_paeth ( unsigned int current,
457                                          unsigned int left,
458                                          unsigned int above,
459                                          unsigned int above_left ) {
460
461         return ( current + png_paeth_predictor ( left, above, above_left ) );
462 }
463
464 /** A PNG filter */
465 struct png_filter {
466         /**
467          * Unfilter byte
468          *
469          * @v current           Filtered current byte
470          * @v left              Unfiltered left byte
471          * @v above             Unfiltered above byte
472          * @v above_left        Unfiltered above-left byte
473          * @ret current         Unfiltered current byte
474          */
475         unsigned int ( * unfilter ) ( unsigned int current,
476                                       unsigned int left,
477                                       unsigned int above,
478                                       unsigned int above_left );
479 };
480
481 /** PNG filter types */
482 static struct png_filter png_filters[] = {
483         [PNG_FILTER_BASIC_NONE] = { png_unfilter_none },
484         [PNG_FILTER_BASIC_SUB] = { png_unfilter_sub },
485         [PNG_FILTER_BASIC_UP] = { png_unfilter_up },
486         [PNG_FILTER_BASIC_AVERAGE] = { png_unfilter_average },
487         [PNG_FILTER_BASIC_PAETH] = { png_unfilter_paeth },
488 };
489
490 /**
491  * Unfilter one interlace pass of PNG raw data
492  *
493  * @v image             PNG image
494  * @v png               PNG context
495  * @v interlace         Interlace pass
496  * @ret rc              Return status code
497  *
498  * This routine may assume that it is impossible to overrun the raw
499  * data buffer, since the size is determined by the image dimensions.
500  */
501 static int png_unfilter_pass ( struct image *image, struct png_context *png,
502                                struct png_interlace *interlace ) {
503         size_t offset = png->raw.offset;
504         size_t pixel_len = png_pixel_len ( png );
505         size_t scanline_len = png_scanline_len ( png, interlace );
506         struct png_filter *filter;
507         unsigned int scanline;
508         unsigned int byte;
509         uint8_t filter_type;
510         uint8_t left;
511         uint8_t above;
512         uint8_t above_left;
513         uint8_t current;
514
515         /* On the first scanline of a pass, above bytes are assumed to
516          * be zero.
517          */
518         above = 0;
519
520         /* Iterate over each scanline in turn */
521         for ( scanline = 0 ; scanline < interlace->height ; scanline++ ) {
522
523                 /* Extract filter byte and determine filter type */
524                 copy_from_user ( &filter_type, png->raw.data, offset++,
525                                  sizeof ( filter_type ) );
526                 if ( filter_type >= ( sizeof ( png_filters ) /
527                                       sizeof ( png_filters[0] ) ) ) {
528                         DBGC ( image, "PNG %s unknown filter type %d\n",
529                                image->name, filter_type );
530                         return -ENOTSUP;
531                 }
532                 filter = &png_filters[filter_type];
533                 assert ( filter->unfilter != NULL );
534                 DBGC2 ( image, "PNG %s pass %d scanline %d filter type %d\n",
535                         image->name, interlace->pass, scanline, filter_type );
536
537                 /* At the start of a line, both above-left and left
538                  * bytes are taken to be zero.
539                  */
540                 left = 0;
541                 above_left = 0;
542
543                 /* Iterate over each byte (not pixel) in turn */
544                 for ( byte = 0 ; byte < ( scanline_len - 1 ) ; byte++ ) {
545
546                         /* Extract predictor bytes, if applicable */
547                         if ( byte >= pixel_len ) {
548                                 copy_from_user ( &left, png->raw.data,
549                                                  ( offset - pixel_len ),
550                                                  sizeof ( left ) );
551                         }
552                         if ( scanline > 0 ) {
553                                 copy_from_user ( &above, png->raw.data,
554                                                  ( offset - scanline_len ),
555                                                  sizeof ( above ) );
556                         }
557                         if ( ( scanline > 0 ) && ( byte >= pixel_len ) ) {
558                                 copy_from_user ( &above_left, png->raw.data,
559                                                  ( offset - scanline_len -
560                                                    pixel_len ),
561                                                  sizeof ( above_left ) );
562                         }
563
564                         /* Unfilter current byte */
565                         copy_from_user ( &current, png->raw.data,
566                                          offset, sizeof ( current ) );
567                         current = filter->unfilter ( current, left, above,
568                                                      above_left );
569                         copy_to_user ( png->raw.data, offset++,
570                                        &current, sizeof ( current ) );
571                 }
572         }
573
574         /* Update offset */
575         png->raw.offset = offset;
576
577         return 0;
578 }
579
580 /**
581  * Unfilter PNG raw data
582  *
583  * @v image             PNG image
584  * @v png               PNG context
585  * @ret rc              Return status code
586  *
587  * This routine may assume that it is impossible to overrun the raw
588  * data buffer, since the size is determined by the image dimensions.
589  */
590 static int png_unfilter ( struct image *image, struct png_context *png ) {
591         struct png_interlace interlace;
592         unsigned int pass;
593         int rc;
594
595         /* Process each interlace pass */
596         png->raw.offset = 0;
597         for ( pass = 0 ; pass < png->passes ; pass++ ) {
598
599                 /* Calculate interlace pass parameters */
600                 png_interlace ( png, pass, &interlace );
601
602                 /* Skip zero-width rows (which have no filter bytes) */
603                 if ( interlace.width == 0 )
604                         continue;
605
606                 /* Unfilter this pass */
607                 if ( ( rc = png_unfilter_pass ( image, png,
608                                                 &interlace ) ) != 0 )
609                         return rc;
610         }
611         assert ( png->raw.offset == png->raw.len );
612
613         return 0;
614 }
615
616 /**
617  * Calculate PNG pixel component value
618  *
619  * @v raw               Raw component value
620  * @v alpha             Alpha value
621  * @v max               Maximum raw/alpha value
622  * @ret value           Component value in range 0-255
623  */
624 static inline unsigned int png_pixel ( unsigned int raw, unsigned int alpha,
625                                        unsigned int max ) {
626
627         /* The basic calculation is 255*(raw/max)*(value/max).  We use
628          * fixed-point arithmetic (scaling up to the maximum range for
629          * a 32-bit integer), in order to get the same results for
630          * alpha blending as the test cases (produced using
631          * ImageMagick).
632          */
633         return ( ( ( ( ( 0xff00 * raw * alpha ) / max ) / max ) + 0x80 ) >> 8 );
634 }
635
636 /**
637  * Fill one interlace pass of PNG pixels
638  *
639  * @v image             PNG image
640  * @v png               PNG context
641  * @v interlace         Interlace pass
642  *
643  * This routine may assume that it is impossible to overrun either the
644  * raw data buffer or the pixel buffer, since the sizes of both are
645  * determined by the image dimensions.
646  */
647 static void png_pixels_pass ( struct image *image,
648                               struct png_context *png,
649                               struct png_interlace *interlace ) {
650         size_t raw_offset = png->raw.offset;
651         uint8_t channel[png->channels];
652         int is_indexed = ( png->colour_type & PNG_COLOUR_TYPE_PALETTE );
653         int is_rgb = ( png->colour_type & PNG_COLOUR_TYPE_RGB );
654         int has_alpha = ( png->colour_type & PNG_COLOUR_TYPE_ALPHA );
655         size_t pixbuf_y_offset;
656         size_t pixbuf_offset;
657         size_t pixbuf_x_stride;
658         size_t pixbuf_y_stride;
659         size_t raw_stride;
660         unsigned int y;
661         unsigned int x;
662         unsigned int c;
663         unsigned int bits;
664         unsigned int depth;
665         unsigned int max;
666         unsigned int alpha;
667         unsigned int raw;
668         unsigned int value;
669         uint8_t current = 0;
670         uint32_t pixel;
671
672         /* We only ever use the top byte of 16-bit pixels.  Model this
673          * as a bit depth of 8 with a stride of more than one.
674          */
675         depth = png->depth;
676         raw_stride = ( ( depth + 7 ) / 8 );
677         if ( depth > 8 )
678                 depth = 8;
679         max = ( ( 1 << depth ) - 1 );
680
681         /* Calculate pixel buffer offset and strides */
682         pixbuf_y_offset = ( ( ( interlace->y_indent * png->pixbuf->width ) +
683                               interlace->x_indent ) * sizeof ( pixel ) );
684         pixbuf_x_stride = ( interlace->x_stride * sizeof ( pixel ) );
685         pixbuf_y_stride = ( interlace->y_stride * png->pixbuf->width *
686                             sizeof ( pixel ) );
687         DBGC2 ( image, "PNG %s pass %d %dx%d at (%d,%d) stride (%d,%d)\n",
688                 image->name, interlace->pass, interlace->width,
689                 interlace->height, interlace->x_indent, interlace->y_indent,
690                 interlace->x_stride, interlace->y_stride );
691
692         /* Iterate over each scanline in turn */
693         for ( y = 0 ; y < interlace->height ; y++ ) {
694
695                 /* Skip filter byte */
696                 raw_offset++;
697
698                 /* Iterate over each pixel in turn */
699                 bits = depth;
700                 pixbuf_offset = pixbuf_y_offset;
701                 for ( x = 0 ; x < interlace->width ; x++ ) {
702
703                         /* Extract sample value */
704                         for ( c = 0 ; c < png->channels ; c++ ) {
705
706                                 /* Get sample value into high bits of current */
707                                 current <<= depth;
708                                 bits -= depth;
709                                 if ( ! bits ) {
710                                         copy_from_user ( &current,
711                                                          png->raw.data,
712                                                          raw_offset,
713                                                          sizeof ( current ) );
714                                         raw_offset += raw_stride;
715                                         bits = 8;
716                                 }
717
718                                 /* Extract sample value */
719                                 channel[c] = ( current >> ( 8 - depth ) );
720                         }
721
722                         /* Convert to native pixel format */
723                         if ( is_indexed ) {
724
725                                 /* Indexed */
726                                 pixel = png->palette[channel[0]];
727
728                         } else {
729
730                                 /* Determine alpha value */
731                                 alpha = ( has_alpha ?
732                                           channel[ png->channels - 1 ] : max );
733
734                                 /* Convert to RGB value */
735                                 pixel = 0;
736                                 for ( c = 0 ; c < 3 ; c++ ) {
737                                         raw = channel[ is_rgb ? c : 0 ];
738                                         value = png_pixel ( raw, alpha, max );
739                                         assert ( value <= 255 );
740                                         pixel = ( ( pixel << 8 ) | value );
741                                 }
742                         }
743
744                         /* Store pixel */
745                         copy_to_user ( png->pixbuf->data, pixbuf_offset,
746                                        &pixel, sizeof ( pixel ) );
747                         pixbuf_offset += pixbuf_x_stride;
748                 }
749
750                 /* Move to next output row */
751                 pixbuf_y_offset += pixbuf_y_stride;
752         }
753
754         /* Update offset */
755         png->raw.offset = raw_offset;
756 }
757
758 /**
759  * Fill PNG pixels
760  *
761  * @v image             PNG image
762  * @v png               PNG context
763  *
764  * This routine may assume that it is impossible to overrun either the
765  * raw data buffer or the pixel buffer, since the sizes of both are
766  * determined by the image dimensions.
767  */
768 static void png_pixels ( struct image *image, struct png_context *png ) {
769         struct png_interlace interlace;
770         unsigned int pass;
771
772         /* Process each interlace pass */
773         png->raw.offset = 0;
774         for ( pass = 0 ; pass < png->passes ; pass++ ) {
775
776                 /* Calculate interlace pass parameters */
777                 png_interlace ( png, pass, &interlace );
778
779                 /* Skip zero-width rows (which have no filter bytes) */
780                 if ( interlace.width == 0 )
781                         continue;
782
783                 /* Unfilter this pass */
784                 png_pixels_pass ( image, png, &interlace );
785         }
786         assert ( png->raw.offset == png->raw.len );
787 }
788
789 /**
790  * Handle PNG image end chunk
791  *
792  * @v image             PNG image
793  * @v png               PNG context
794  * @v len               Chunk length
795  * @ret rc              Return status code
796  */
797 static int png_image_end ( struct image *image, struct png_context *png,
798                            size_t len ) {
799         int rc;
800
801         /* Sanity checks */
802         if ( len != 0 ) {
803                 DBGC ( image, "PNG %s invalid IEND length %zd\n",
804                        image->name, len );
805                 return -EINVAL;
806         }
807         if ( ! png->pixbuf ) {
808                 DBGC ( image, "PNG %s missing pixel buffer (no IHDR?)\n",
809                        image->name );
810                 return -EINVAL;
811         }
812         if ( ! deflate_finished ( &png->deflate ) ) {
813                 DBGC ( image, "PNG %s decompression not complete\n",
814                        image->name );
815                 return -EINVAL;
816         }
817         if ( png->raw.offset != png->raw.len ) {
818                 DBGC ( image, "PNG %s incorrect decompressed length (expected "
819                        "%zd, got %zd)\n", image->name, png->raw.len,
820                        png->raw.offset );
821                 return -EINVAL;
822         }
823
824         /* Unfilter raw data */
825         if ( ( rc = png_unfilter ( image, png ) ) != 0 )
826                 return rc;
827
828         /* Fill pixel buffer */
829         png_pixels ( image, png );
830
831         return 0;
832 }
833
834 /** A PNG chunk handler */
835 struct png_chunk_handler {
836         /** Chunk type */
837         uint32_t type;
838         /**
839          * Handle chunk
840          *
841          * @v image             PNG image
842          * @v png               PNG context
843          * @v len               Chunk length
844          * @ret rc              Return status code
845          */
846         int ( * handle ) ( struct image *image, struct png_context *png,
847                            size_t len );
848 };
849
850 /** PNG chunk handlers */
851 static struct png_chunk_handler png_chunk_handlers[] = {
852         { htonl ( PNG_TYPE_IHDR ), png_image_header },
853         { htonl ( PNG_TYPE_PLTE ), png_palette },
854         { htonl ( PNG_TYPE_IDAT ), png_image_data },
855         { htonl ( PNG_TYPE_IEND ), png_image_end },
856 };
857
858 /**
859  * Handle PNG chunk
860  *
861  * @v image             PNG image
862  * @v png               PNG context
863  * @v type              Chunk type
864  * @v len               Chunk length
865  * @ret rc              Return status code
866  */
867 static int png_chunk ( struct image *image, struct png_context *png,
868                        uint32_t type, size_t len ) {
869         struct png_chunk_handler *handler;
870         unsigned int i;
871
872         DBGC ( image, "PNG %s chunk type %s offset %zd length %zd\n",
873                image->name, png_type_name ( type ), png->offset, len );
874
875         /* Handle according to chunk type */
876         for ( i = 0 ; i < ( sizeof ( png_chunk_handlers ) /
877                             sizeof ( png_chunk_handlers[0] ) ) ; i++ ) {
878                 handler = &png_chunk_handlers[i];
879                 if ( handler->type == type )
880                         return handler->handle ( image, png, len );
881         }
882
883         /* Fail if unknown chunk type is critical */
884         if ( ! ( type & htonl ( PNG_CHUNK_ANCILLARY ) ) ) {
885                 DBGC ( image, "PNG %s unknown critical chunk type %s\n",
886                        image->name, png_type_name ( type ) );
887                 return -ENOTSUP;
888         }
889
890         /* Ignore non-critical unknown chunk types */
891         return 0;
892 }
893
894 /**
895  * Convert PNG image to pixel buffer
896  *
897  * @v image             PNG image
898  * @v pixbuf            Pixel buffer to fill in
899  * @ret rc              Return status code
900  */
901 static int png_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
902         struct png_context *png;
903         struct png_chunk_header header;
904         struct png_chunk_footer footer;
905         size_t remaining;
906         size_t chunk_len;
907         int rc;
908
909         /* Allocate and initialise context */
910         png = zalloc ( sizeof ( *png ) );
911         if ( ! png ) {
912                 rc = -ENOMEM;
913                 goto err_alloc;
914         }
915         png->offset = sizeof ( struct png_signature );
916         deflate_init ( &png->deflate, DEFLATE_ZLIB );
917
918         /* Process chunks */
919         do {
920
921                 /* Extract chunk header */
922                 remaining = ( image->len - png->offset );
923                 if ( remaining < sizeof ( header ) ) {
924                         DBGC ( image, "PNG %s truncated chunk header at offset "
925                                "%zd\n", image->name, png->offset );
926                         rc = -EINVAL;
927                         goto err_truncated;
928                 }
929                 copy_from_user ( &header, image->data, png->offset,
930                                  sizeof ( header ) );
931                 png->offset += sizeof ( header );
932
933                 /* Validate chunk length */
934                 chunk_len = ntohl ( header.len );
935                 if ( remaining < ( sizeof ( header ) + chunk_len +
936                                    sizeof ( footer ) ) ) {
937                         DBGC ( image, "PNG %s truncated chunk data/footer at "
938                                "offset %zd\n", image->name, png->offset );
939                         rc = -EINVAL;
940                         goto err_truncated;
941                 }
942
943                 /* Handle chunk */
944                 if ( ( rc = png_chunk ( image, png, header.type,
945                                         chunk_len ) ) != 0 )
946                         goto err_chunk;
947
948                 /* Move to next chunk */
949                 png->offset += ( chunk_len + sizeof ( footer ) );
950
951         } while ( png->offset < image->len );
952
953         /* Check that we finished with an IEND chunk */
954         if ( header.type != htonl ( PNG_TYPE_IEND ) ) {
955                 DBGC ( image, "PNG %s did not finish with IEND\n",
956                        image->name );
957                 rc = -EINVAL;
958                 goto err_iend;
959         }
960
961         /* Return pixel buffer */
962         *pixbuf = pixbuf_get ( png->pixbuf );
963
964         /* Success */
965         rc = 0;
966
967  err_iend:
968  err_chunk:
969  err_truncated:
970         pixbuf_put ( png->pixbuf );
971         ufree ( png->raw.data );
972         free ( png );
973  err_alloc:
974         return rc;
975 }
976
977 /**
978  * Probe PNG image
979  *
980  * @v image             PNG image
981  * @ret rc              Return status code
982  */
983 static int png_probe ( struct image *image ) {
984         struct png_signature signature;
985
986         /* Sanity check */
987         if ( image->len < sizeof ( signature ) ) {
988                 DBGC ( image, "PNG %s is too short\n", image->name );
989                 return -ENOEXEC;
990         }
991
992         /* Check signature */
993         copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
994         if ( memcmp ( &signature, &png_signature, sizeof ( signature ) ) != 0 ){
995                 DBGC ( image, "PNG %s has invalid signature\n", image->name );
996                 return -ENOEXEC;
997         }
998
999         return 0;
1000 }
1001
1002 /** PNG image type */
1003 struct image_type png_image_type __image_type ( PROBE_NORMAL ) = {
1004         .name = "PNG",
1005         .probe = png_probe,
1006         .pixbuf = png_pixbuf,
1007 };