iPXE
png.h
Go to the documentation of this file.
00001 #ifndef _IPXE_PNG_H
00002 #define _IPXE_PNG_H
00003 
00004 /** @file
00005  *
00006  * Portable Network Graphics (PNG) format
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <byteswap.h>
00014 #include <ipxe/image.h>
00015 
00016 /** A PNG file signature */
00017 struct png_signature {
00018         /** Signature bytes */
00019         uint8_t bytes[8];
00020 } __attribute__ (( packed ));
00021 
00022 /** PNG file signature */
00023 #define PNG_SIGNATURE { { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n' } }
00024 
00025 /** A PNG chunk header */
00026 struct png_chunk_header {
00027         /** Length of the chunk (excluding header and footer) */
00028         uint32_t len;
00029         /** Chunk type */
00030         uint32_t type;
00031 } __attribute__ (( packed ));
00032 
00033 /** A PNG chunk footer */
00034 struct png_chunk_footer {
00035         /** CRC */
00036         uint32_t crc;
00037 } __attribute__ (( packed ));
00038 
00039 /** PNG chunk type property bits */
00040 enum png_chunk_type_bits {
00041         /** Chunk is ancillary */
00042         PNG_CHUNK_ANCILLARY = 0x20000000UL,
00043         /** Chunk is private */
00044         PNG_CHUNK_PRIVATE = 0x00200000UL,
00045         /** Reserved */
00046         PNG_CHUNK_RESERVED = 0x00002000UL,
00047         /** Chunk is safe to copy */
00048         PNG_CHUNK_SAFE = 0x00000020UL,
00049 };
00050 
00051 /**
00052  * Canonicalise PNG chunk type
00053  *
00054  * @v type              Raw chunk type
00055  * @ret type            Canonicalised chunk type (excluding property bits)
00056  */
00057 static inline __attribute__ (( always_inline )) uint32_t
00058 png_canonical_type ( uint32_t type ) {
00059         return ( type & ~( htonl ( PNG_CHUNK_ANCILLARY | PNG_CHUNK_PRIVATE |
00060                                    PNG_CHUNK_RESERVED | PNG_CHUNK_SAFE ) ) );
00061 }
00062 
00063 /**
00064  * Define a canonical PNG chunk type
00065  *
00066  * @v first             First letter (in upper case)
00067  * @v second            Second letter (in upper case)
00068  * @v third             Third letter (in upper case)
00069  * @v fourth            Fourth letter (in upper case)
00070  * @ret type            Canonical chunk type
00071  */
00072 #define PNG_TYPE( first, second, third, fourth ) \
00073         ( ( (first) << 24 ) | ( (second) << 16 ) | ( (third) << 8 ) | (fourth) )
00074 
00075 /** PNG image header chunk type */
00076 #define PNG_TYPE_IHDR PNG_TYPE ( 'I', 'H', 'D', 'R' )
00077 
00078 /** A PNG image header */
00079 struct png_image_header {
00080         /** Width */
00081         uint32_t width;
00082         /** Height */
00083         uint32_t height;
00084         /** Bit depth */
00085         uint8_t depth;
00086         /** Colour type */
00087         uint8_t colour_type;
00088         /** Compression method */
00089         uint8_t compression;
00090         /** Filter method */
00091         uint8_t filter;
00092         /** Interlace method */
00093         uint8_t interlace;
00094 } __attribute__ (( packed ));
00095 
00096 /** PNG colour type bits */
00097 enum png_colour_type {
00098         /** Palette is used */
00099         PNG_COLOUR_TYPE_PALETTE = 0x01,
00100         /** RGB colour is used */
00101         PNG_COLOUR_TYPE_RGB = 0x02,
00102         /** Alpha channel is used */
00103         PNG_COLOUR_TYPE_ALPHA = 0x04,
00104 };
00105 
00106 /** PNG colour type mask */
00107 #define PNG_COLOUR_TYPE_MASK 0x07
00108 
00109 /** PNG compression methods */
00110 enum png_compression_method {
00111         /** DEFLATE compression with 32kB sliding window */
00112         PNG_COMPRESSION_DEFLATE = 0x00,
00113         /** First unknown compression method */
00114         PNG_COMPRESSION_UNKNOWN = 0x01,
00115 };
00116 
00117 /** PNG filter methods */
00118 enum png_filter_method {
00119         /** Adaptive filtering with five basic types */
00120         PNG_FILTER_BASIC = 0x00,
00121         /** First unknown filter method */
00122         PNG_FILTER_UNKNOWN = 0x01,
00123 };
00124 
00125 /** PNG interlace methods */
00126 enum png_interlace_method {
00127         /** No interlacing */
00128         PNG_INTERLACE_NONE = 0x00,
00129         /** Adam7 interlacing */
00130         PNG_INTERLACE_ADAM7 = 0x01,
00131         /** First unknown interlace method */
00132         PNG_INTERLACE_UNKNOWN = 0x02,
00133 };
00134 
00135 /** PNG palette chunk type */
00136 #define PNG_TYPE_PLTE PNG_TYPE ( 'P', 'L', 'T', 'E' )
00137 
00138 /** A PNG palette entry */
00139 struct png_palette_entry {
00140         /** Red */
00141         uint8_t red;
00142         /** Green */
00143         uint8_t green;
00144         /** Blue */
00145         uint8_t blue;
00146 } __attribute__ (( packed ));
00147 
00148 /** A PNG palette chunk */
00149 struct png_palette {
00150         /** Palette entries */
00151         struct png_palette_entry entries[0];
00152 } __attribute__ (( packed ));
00153 
00154 /** Maximum number of PNG palette entries */
00155 #define PNG_PALETTE_COUNT 256
00156 
00157 /** PNG image data chunk type */
00158 #define PNG_TYPE_IDAT PNG_TYPE ( 'I', 'D', 'A', 'T' )
00159 
00160 /** PNG basic filter types */
00161 enum png_basic_filter_type {
00162         /** No filtering */
00163         PNG_FILTER_BASIC_NONE = 0,
00164         /** Left byte used as predictor */
00165         PNG_FILTER_BASIC_SUB = 1,
00166         /** Above byte used as predictor */
00167         PNG_FILTER_BASIC_UP = 2,
00168         /** Above and left bytes used as predictors */
00169         PNG_FILTER_BASIC_AVERAGE = 3,
00170         /** Paeth filter */
00171         PNG_FILTER_BASIC_PAETH = 4,
00172 };
00173 
00174 /** PNG image end chunk type */
00175 #define PNG_TYPE_IEND PNG_TYPE ( 'I', 'E', 'N', 'D' )
00176 
00177 extern struct image_type png_image_type __image_type ( PROBE_NORMAL );
00178 
00179 #endif /* _IPXE_PNG_H */