You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

png.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef _IPXE_PNG_H
  2. #define _IPXE_PNG_H
  3. /** @file
  4. *
  5. * Portable Network Graphics (PNG) format
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <byteswap.h>
  11. #include <ipxe/image.h>
  12. /** A PNG file signature */
  13. struct png_signature {
  14. /** Signature bytes */
  15. uint8_t bytes[8];
  16. } __attribute__ (( packed ));
  17. /** PNG file signature */
  18. #define PNG_SIGNATURE { { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n' } }
  19. /** A PNG chunk header */
  20. struct png_chunk_header {
  21. /** Length of the chunk (excluding header and footer) */
  22. uint32_t len;
  23. /** Chunk type */
  24. uint32_t type;
  25. } __attribute__ (( packed ));
  26. /** A PNG chunk footer */
  27. struct png_chunk_footer {
  28. /** CRC */
  29. uint32_t crc;
  30. } __attribute__ (( packed ));
  31. /** PNG chunk type property bits */
  32. enum png_chunk_type_bits {
  33. /** Chunk is ancillary */
  34. PNG_CHUNK_ANCILLARY = 0x20000000UL,
  35. /** Chunk is private */
  36. PNG_CHUNK_PRIVATE = 0x00200000UL,
  37. /** Reserved */
  38. PNG_CHUNK_RESERVED = 0x00002000UL,
  39. /** Chunk is safe to copy */
  40. PNG_CHUNK_SAFE = 0x00000020UL,
  41. };
  42. /**
  43. * Canonicalise PNG chunk type
  44. *
  45. * @v type Raw chunk type
  46. * @ret type Canonicalised chunk type (excluding property bits)
  47. */
  48. static inline __attribute__ (( always_inline )) uint32_t
  49. png_canonical_type ( uint32_t type ) {
  50. return ( type & ~( htonl ( PNG_CHUNK_ANCILLARY | PNG_CHUNK_PRIVATE |
  51. PNG_CHUNK_RESERVED | PNG_CHUNK_SAFE ) ) );
  52. }
  53. /**
  54. * Define a canonical PNG chunk type
  55. *
  56. * @v first First letter (in upper case)
  57. * @v second Second letter (in upper case)
  58. * @v third Third letter (in upper case)
  59. * @v fourth Fourth letter (in upper case)
  60. * @ret type Canonical chunk type
  61. */
  62. #define PNG_TYPE( first, second, third, fourth ) \
  63. ( ( (first) << 24 ) | ( (second) << 16 ) | ( (third) << 8 ) | (fourth) )
  64. /** PNG image header chunk type */
  65. #define PNG_TYPE_IHDR PNG_TYPE ( 'I', 'H', 'D', 'R' )
  66. /** A PNG image header */
  67. struct png_image_header {
  68. /** Width */
  69. uint32_t width;
  70. /** Height */
  71. uint32_t height;
  72. /** Bit depth */
  73. uint8_t depth;
  74. /** Colour type */
  75. uint8_t colour_type;
  76. /** Compression method */
  77. uint8_t compression;
  78. /** Filter method */
  79. uint8_t filter;
  80. /** Interlace method */
  81. uint8_t interlace;
  82. } __attribute__ (( packed ));
  83. /** PNG colour type bits */
  84. enum png_colour_type {
  85. /** Palette is used */
  86. PNG_COLOUR_TYPE_PALETTE = 0x01,
  87. /** RGB colour is used */
  88. PNG_COLOUR_TYPE_RGB = 0x02,
  89. /** Alpha channel is used */
  90. PNG_COLOUR_TYPE_ALPHA = 0x04,
  91. };
  92. /** PNG colour type mask */
  93. #define PNG_COLOUR_TYPE_MASK 0x07
  94. /** PNG compression methods */
  95. enum png_compression_method {
  96. /** DEFLATE compression with 32kB sliding window */
  97. PNG_COMPRESSION_DEFLATE = 0x00,
  98. /** First unknown compression method */
  99. PNG_COMPRESSION_UNKNOWN = 0x01,
  100. };
  101. /** PNG filter methods */
  102. enum png_filter_method {
  103. /** Adaptive filtering with five basic types */
  104. PNG_FILTER_BASIC = 0x00,
  105. /** First unknown filter method */
  106. PNG_FILTER_UNKNOWN = 0x01,
  107. };
  108. /** PNG interlace methods */
  109. enum png_interlace_method {
  110. /** No interlacing */
  111. PNG_INTERLACE_NONE = 0x00,
  112. /** Adam7 interlacing */
  113. PNG_INTERLACE_ADAM7 = 0x01,
  114. /** First unknown interlace method */
  115. PNG_INTERLACE_UNKNOWN = 0x02,
  116. };
  117. /** PNG palette chunk type */
  118. #define PNG_TYPE_PLTE PNG_TYPE ( 'P', 'L', 'T', 'E' )
  119. /** A PNG palette entry */
  120. struct png_palette_entry {
  121. /** Red */
  122. uint8_t red;
  123. /** Green */
  124. uint8_t green;
  125. /** Blue */
  126. uint8_t blue;
  127. } __attribute__ (( packed ));
  128. /** A PNG palette chunk */
  129. struct png_palette {
  130. /** Palette entries */
  131. struct png_palette_entry entries[0];
  132. } __attribute__ (( packed ));
  133. /** Maximum number of PNG palette entries */
  134. #define PNG_PALETTE_COUNT 256
  135. /** PNG image data chunk type */
  136. #define PNG_TYPE_IDAT PNG_TYPE ( 'I', 'D', 'A', 'T' )
  137. /** PNG basic filter types */
  138. enum png_basic_filter_type {
  139. /** No filtering */
  140. PNG_FILTER_BASIC_NONE = 0,
  141. /** Left byte used as predictor */
  142. PNG_FILTER_BASIC_SUB = 1,
  143. /** Above byte used as predictor */
  144. PNG_FILTER_BASIC_UP = 2,
  145. /** Above and left bytes used as predictors */
  146. PNG_FILTER_BASIC_AVERAGE = 3,
  147. /** Paeth filter */
  148. PNG_FILTER_BASIC_PAETH = 4,
  149. };
  150. /** PNG image end chunk type */
  151. #define PNG_TYPE_IEND PNG_TYPE ( 'I', 'E', 'N', 'D' )
  152. extern struct image_type png_image_type __image_type ( PROBE_NORMAL );
  153. #endif /* _IPXE_PNG_H */