iPXE
Data Structures | Functions | Variables
png.c File Reference

Portable Network Graphics (PNG) format. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/umalloc.h>
#include <ipxe/pixbuf.h>
#include <ipxe/deflate.h>
#include <ipxe/png.h>

Go to the source code of this file.

Data Structures

struct  png_context
 PNG context. More...
 
struct  png_interlace
 A PNG interlace pass. More...
 
struct  png_filter
 A PNG filter. More...
 
struct  png_chunk_handler
 A PNG chunk handler. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static const char * png_type_name (uint32_t type)
 Transcribe PNG chunk type name (for debugging) More...
 
static void png_interlace (struct png_context *png, unsigned int pass, struct png_interlace *interlace)
 Calculate PNG interlace pass parameters. More...
 
static unsigned int png_pixel_len (struct png_context *png)
 Calculate PNG pixel length. More...
 
static size_t png_scanline_len (struct png_context *png, struct png_interlace *interlace)
 Calculate PNG scanline length. More...
 
static int png_image_header (struct image *image, struct png_context *png, size_t len)
 Handle PNG image header chunk. More...
 
static int png_palette (struct image *image, struct png_context *png, size_t len)
 Handle PNG palette chunk. More...
 
static int png_image_data (struct image *image, struct png_context *png, size_t len)
 Handle PNG image data chunk. More...
 
static unsigned int png_unfilter_none (unsigned int current, unsigned int left __unused, unsigned int above __unused, unsigned int above_left __unused)
 Unfilter byte using the "None" filter. More...
 
static unsigned int png_unfilter_sub (unsigned int current, unsigned int left, unsigned int above __unused, unsigned int above_left __unused)
 Unfilter byte using the "Sub" filter. More...
 
static unsigned int png_unfilter_up (unsigned int current, unsigned int left __unused, unsigned int above, unsigned int above_left __unused)
 Unfilter byte using the "Up" filter. More...
 
static unsigned int png_unfilter_average (unsigned int current, unsigned int left, unsigned int above, unsigned int above_left __unused)
 Unfilter byte using the "Average" filter. More...
 
static unsigned int png_paeth_predictor (unsigned int a, unsigned int b, unsigned int c)
 Paeth predictor function (defined in RFC 2083) More...
 
static unsigned int png_unfilter_paeth (unsigned int current, unsigned int left, unsigned int above, unsigned int above_left)
 Unfilter byte using the "Paeth" filter. More...
 
static int png_unfilter_pass (struct image *image, struct png_context *png, struct png_interlace *interlace)
 Unfilter one interlace pass of PNG raw data. More...
 
static int png_unfilter (struct image *image, struct png_context *png)
 Unfilter PNG raw data. More...
 
static unsigned int png_pixel (unsigned int raw, unsigned int alpha, unsigned int max)
 Calculate PNG pixel component value. More...
 
static void png_pixels_pass (struct image *image, struct png_context *png, struct png_interlace *interlace)
 Fill one interlace pass of PNG pixels. More...
 
static void png_pixels (struct image *image, struct png_context *png)
 Fill PNG pixels. More...
 
static int png_image_end (struct image *image, struct png_context *png, size_t len)
 Handle PNG image end chunk. More...
 
static int png_chunk (struct image *image, struct png_context *png, uint32_t type, size_t len)
 Handle PNG chunk. More...
 
static int png_pixbuf (struct image *image, struct pixel_buffer **pixbuf)
 Convert PNG image to pixel buffer. More...
 
static int png_probe (struct image *image)
 Probe PNG image. More...
 
struct image_type png_image_type __image_type (PROBE_NORMAL)
 PNG image type. More...
 

Variables

static struct png_signature png_signature = PNG_SIGNATURE
 PNG file signature. More...
 
static uint8_t png_interlace_passes []
 Number of interlacing passes. More...
 
static struct png_filter png_filters []
 PNG filter types. More...
 
static struct png_chunk_handler png_chunk_handlers []
 PNG chunk handlers. More...
 

Detailed Description

Portable Network Graphics (PNG) format.

The PNG format is defined in RFC 2083.

Definition in file png.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ png_type_name()

static const char* png_type_name ( uint32_t  type)
static

Transcribe PNG chunk type name (for debugging)

Parameters
typeChunk type
Return values
nameChunk type name

Definition at line 101 of file png.c.

101  {
102  static union {
103  uint32_t type;
104  char name[ sizeof ( uint32_t ) + 1 /* NUL */ ];
105  } u;
106 
107  u.type = type;
108  return u.name;
109 }
const char * name
Definition: ath9k_hw.c:1984
uint32_t type
Operating system type.
Definition: ena.h:12
union @18 u
union zimg_context::@637 type
Compression type.
unsigned int uint32_t
Definition: stdint.h:12

References name, type, and u.

Referenced by png_chunk().

◆ png_interlace()

static void png_interlace ( struct png_context png,
unsigned int  pass,
struct png_interlace interlace 
)
static

Calculate PNG interlace pass parameters.

Parameters
pngPNG context
passPass number (0=first pass)
interlaceInterlace pass to fill in

Definition at line 118 of file png.c.

119  {
120  unsigned int grid_width_log2;
121  unsigned int grid_height_log2;
122  unsigned int x_indent;
123  unsigned int y_indent;
124  unsigned int x_stride_log2;
125  unsigned int y_stride_log2;
126  unsigned int x_stride;
127  unsigned int y_stride;
128  unsigned int width;
129  unsigned int height;
130 
131  /* Sanity check */
132  assert ( png->passes > 0 );
133 
134  /* Store pass number */
135  interlace->pass = pass;
136 
137  /* Calculate interlace grid dimensions */
138  grid_width_log2 = ( png->passes / 2 );
139  grid_height_log2 = ( ( png->passes - 1 ) / 2 );
140 
141  /* Calculate starting indents */
142  interlace->x_indent = x_indent =
143  ( ( pass & 1 ) ?
144  ( 1 << ( grid_width_log2 - ( pass / 2 ) - 1 ) ) : 0 );
145  interlace->y_indent = y_indent =
146  ( ( pass && ! ( pass & 1 ) ) ?
147  ( 1 << ( grid_height_log2 - ( ( pass - 1 ) / 2 ) - 1 ) ) : 0);
148 
149  /* Calculate strides */
150  x_stride_log2 = ( grid_width_log2 - ( pass / 2 ) );
151  y_stride_log2 =
152  ( grid_height_log2 - ( pass ? ( ( pass - 1 ) / 2 ) : 0 ) );
153  interlace->x_stride = x_stride = ( 1 << x_stride_log2 );
154  interlace->y_stride = y_stride = ( 1 << y_stride_log2 );
155 
156  /* Calculate pass dimensions */
157  width = png->pixbuf->width;
158  height = png->pixbuf->height;
159  interlace->width =
160  ( ( width - x_indent + x_stride - 1 ) >> x_stride_log2 );
161  interlace->height =
162  ( ( height - y_indent + y_stride - 1 ) >> y_stride_log2 );
163 }
unsigned int x_indent
X starting indent.
Definition: png.c:73
unsigned int height
Height.
Definition: pixbuf.h:22
unsigned int y_stride
Y stride.
Definition: png.c:79
unsigned int pass
Pass number.
Definition: png.c:71
unsigned int width
Width.
Definition: png.c:81
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int x_stride
X stride.
Definition: png.c:77
struct pixel_buffer * pixbuf
Pixel buffer.
Definition: png.c:49
unsigned int passes
Number of interlace passes.
Definition: png.c:58
unsigned int height
Height.
Definition: png.c:83
unsigned int y_indent
Y starting indent.
Definition: png.c:75
unsigned int width
Width.
Definition: pixbuf.h:20

References assert(), pixel_buffer::height, png_interlace::height, png_interlace::pass, png_context::passes, png_context::pixbuf, pixel_buffer::width, png_interlace::width, png_interlace::x_indent, png_interlace::x_stride, png_interlace::y_indent, and png_interlace::y_stride.

Referenced by png_image_header(), png_pixels(), and png_unfilter().

◆ png_pixel_len()

static unsigned int png_pixel_len ( struct png_context png)
static

Calculate PNG pixel length.

Parameters
pngPNG context
Return values
pixel_lenPixel length

Definition at line 171 of file png.c.

171  {
172 
173  return ( ( ( png->channels * png->depth ) + 7 ) / 8 );
174 }
unsigned int depth
Bit depth.
Definition: png.c:52
unsigned int channels
Number of channels.
Definition: png.c:56

References png_context::channels, and png_context::depth.

Referenced by png_unfilter_pass().

◆ png_scanline_len()

static size_t png_scanline_len ( struct png_context png,
struct png_interlace interlace 
)
static

Calculate PNG scanline length.

Parameters
pngPNG context
interlaceInterlace pass
Return values
scanline_lenScanline length (including filter byte)

Definition at line 183 of file png.c.

184  {
185 
186  return ( 1 /* Filter byte */ +
187  ( ( interlace->width * png->channels * png->depth ) + 7 ) / 8);
188 }
unsigned int width
Width.
Definition: png.c:81
unsigned int depth
Bit depth.
Definition: png.c:52
unsigned int channels
Number of channels.
Definition: png.c:56

References png_context::channels, png_context::depth, and png_interlace::width.

Referenced by png_image_header(), and png_unfilter_pass().

◆ png_image_header()

static int png_image_header ( struct image image,
struct png_context png,
size_t  len 
)
static

Handle PNG image header chunk.

Parameters
imagePNG image
pngPNG context
lenChunk length
Return values
rcReturn status code

Definition at line 198 of file png.c.

199  {
200  const struct png_image_header *ihdr;
201  struct png_interlace interlace;
202  unsigned int pass;
203 
204  /* Sanity check */
205  if ( len != sizeof ( *ihdr ) ) {
206  DBGC ( image, "PNG %s invalid IHDR length %zd\n",
207  image->name, len );
208  return -EINVAL;
209  }
210  if ( png->pixbuf ) {
211  DBGC ( image, "PNG %s duplicate IHDR\n", image->name );
212  return -EINVAL;
213  }
214 
215  /* Extract image header */
216  ihdr = ( image->data + png->offset );
217  DBGC ( image, "PNG %s %dx%d depth %d type %d compression %d filter %d "
218  "interlace %d\n", image->name, ntohl ( ihdr->width ),
219  ntohl ( ihdr->height ), ihdr->depth, ihdr->colour_type,
220  ihdr->compression, ihdr->filter, ihdr->interlace );
221 
222  /* Sanity checks */
223  if ( ihdr->compression >= PNG_COMPRESSION_UNKNOWN ) {
224  DBGC ( image, "PNG %s unknown compression method %d\n",
225  image->name, ihdr->compression );
226  return -ENOTSUP;
227  }
228  if ( ihdr->filter >= PNG_FILTER_UNKNOWN ) {
229  DBGC ( image, "PNG %s unknown filter method %d\n",
230  image->name, ihdr->filter );
231  return -ENOTSUP;
232  }
233  if ( ihdr->interlace >= PNG_INTERLACE_UNKNOWN ) {
234  DBGC ( image, "PNG %s unknown interlace method %d\n",
235  image->name, ihdr->interlace );
236  return -ENOTSUP;
237  }
238 
239  /* Allocate pixel buffer */
240  png->pixbuf = alloc_pixbuf ( ntohl ( ihdr->width ),
241  ntohl ( ihdr->height ) );
242  if ( ! png->pixbuf ) {
243  DBGC ( image, "PNG %s could not allocate pixel buffer\n",
244  image->name );
245  return -ENOMEM;
246  }
247 
248  /* Extract bit depth */
249  png->depth = ihdr->depth;
250  if ( ( png->depth == 0 ) ||
251  ( ( png->depth & ( png->depth - 1 ) ) != 0 ) ) {
252  DBGC ( image, "PNG %s invalid depth %d\n",
253  image->name, png->depth );
254  return -EINVAL;
255  }
256 
257  /* Calculate number of channels */
258  png->colour_type = ihdr->colour_type;
259  png->channels = 1;
260  if ( ! ( ihdr->colour_type & PNG_COLOUR_TYPE_PALETTE ) ) {
261  if ( ihdr->colour_type & PNG_COLOUR_TYPE_RGB )
262  png->channels += 2;
263  if ( ihdr->colour_type & PNG_COLOUR_TYPE_ALPHA )
264  png->channels += 1;
265  }
266 
267  /* Calculate number of interlace passes */
268  png->passes = png_interlace_passes[ihdr->interlace];
269 
270  /* Calculate length of raw data buffer */
271  for ( pass = 0 ; pass < png->passes ; pass++ ) {
272  png_interlace ( png, pass, &interlace );
273  if ( interlace.width == 0 )
274  continue;
275  png->raw.len += ( interlace.height *
276  png_scanline_len ( png, &interlace ) );
277  }
278 
279  /* Allocate raw data buffer */
280  png->raw.data = umalloc ( png->raw.len );
281  if ( ! png->raw.data ) {
282  DBGC ( image, "PNG %s could not allocate data buffer\n",
283  image->name );
284  return -ENOMEM;
285  }
286 
287  return 0;
288 }
void * data
Data.
Definition: deflate.h:247
#define EINVAL
Invalid argument.
Definition: errno.h:428
RGB colour is used.
Definition: png.h:101
struct pixel_buffer * alloc_pixbuf(unsigned int width, unsigned int height)
Allocate pixel buffer.
Definition: pixbuf.c:59
static size_t png_scanline_len(struct png_context *png, struct png_interlace *interlace)
Calculate PNG scanline length.
Definition: png.c:183
uint32_t height
Height.
Definition: png.h:83
const void * data
Read-only data.
Definition: image.h:50
Alpha channel is used.
Definition: png.h:103
size_t offset
Offset within image.
Definition: png.c:46
#define DBGC(...)
Definition: compiler.h:505
uint32_t width
Width.
Definition: png.h:81
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
#define ntohl(value)
Definition: byteswap.h:134
An executable image.
Definition: image.h:23
unsigned int pass
Pass number.
Definition: png.c:71
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
unsigned int depth
Bit depth.
Definition: png.c:52
First unknown filter method.
Definition: png.h:122
A PNG image header.
Definition: png.h:79
#define ENOMEM
Not enough space.
Definition: errno.h:534
uint8_t colour_type
Colour type.
Definition: png.h:87
ring len
Length.
Definition: dwmac.h:231
A PNG interlace pass.
Definition: png.c:69
Palette is used.
Definition: png.h:99
unsigned int colour_type
Colour type.
Definition: png.c:54
struct pixel_buffer * pixbuf
Pixel buffer.
Definition: png.c:49
uint8_t compression
Compression method.
Definition: png.h:89
static uint8_t png_interlace_passes[]
Number of interlacing passes.
Definition: png.c:90
First unknown interlace method.
Definition: png.h:132
static __always_inline void * umalloc(size_t size)
Allocate external memory.
Definition: umalloc.h:56
First unknown compression method.
Definition: png.h:114
unsigned int passes
Number of interlace passes.
Definition: png.c:58
uint8_t interlace
Interlace method.
Definition: png.h:93
uint8_t filter
Filter method.
Definition: png.h:91
uint8_t depth
Bit depth.
Definition: png.h:85
static void png_interlace(struct png_context *png, unsigned int pass, struct png_interlace *interlace)
Calculate PNG interlace pass parameters.
Definition: png.c:118
unsigned int channels
Number of channels.
Definition: png.c:56
char * name
Name.
Definition: image.h:37
size_t len
Length of data.
Definition: deflate.h:251

References alloc_pixbuf(), png_context::channels, png_context::colour_type, png_image_header::colour_type, png_image_header::compression, image::data, deflate_chunk::data, DBGC, png_context::depth, png_image_header::depth, EINVAL, ENOMEM, ENOTSUP, png_image_header::filter, png_interlace::height, png_image_header::height, png_image_header::interlace, len, deflate_chunk::len, image::name, ntohl, png_context::offset, png_interlace::pass, png_context::passes, png_context::pixbuf, PNG_COLOUR_TYPE_ALPHA, PNG_COLOUR_TYPE_PALETTE, PNG_COLOUR_TYPE_RGB, PNG_COMPRESSION_UNKNOWN, PNG_FILTER_UNKNOWN, png_interlace(), png_interlace_passes, PNG_INTERLACE_UNKNOWN, png_scanline_len(), png_context::raw, umalloc(), png_interlace::width, and png_image_header::width.

◆ png_palette()

static int png_palette ( struct image image,
struct png_context png,
size_t  len 
)
static

Handle PNG palette chunk.

Parameters
imagePNG image
pngPNG context
lenChunk length
Return values
rcReturn status code

Definition at line 298 of file png.c.

299  {
300  const struct png_palette_entry *palette;
301  unsigned int i;
302 
303  /* Populate palette */
304  palette = ( image->data + png->offset );
305  for ( i = 0 ; i < ( sizeof ( png->palette ) /
306  sizeof ( png->palette[0] ) ) ; i++ ) {
307 
308  /* Stop when we run out of palette data */
309  if ( len < sizeof ( *palette ) )
310  break;
311 
312  /* Extract palette entry */
313  png->palette[i] = ( ( palette->red << 16 ) |
314  ( palette->green << 8 ) |
315  ( palette->blue << 0 ) );
316  DBGC2 ( image, "PNG %s palette entry %d is %#06x\n",
317  image->name, i, png->palette[i] );
318 
319  /* Move to next entry */
320  palette++;
321  len -= sizeof ( *palette );
322  }
323 
324  return 0;
325 }
uint32_t palette[PNG_PALETTE_COUNT]
Palette, in iPXE's pixel buffer format.
Definition: png.c:60
const void * data
Read-only data.
Definition: image.h:50
uint8_t blue
Blue.
Definition: png.h:145
size_t offset
Offset within image.
Definition: png.c:46
An executable image.
Definition: image.h:23
ring len
Length.
Definition: dwmac.h:231
#define DBGC2(...)
Definition: compiler.h:522
A PNG palette entry.
Definition: png.h:139
char * name
Name.
Definition: image.h:37
uint8_t green
Green.
Definition: png.h:143
uint8_t red
Red.
Definition: png.h:141

References png_palette_entry::blue, image::data, DBGC2, png_palette_entry::green, len, image::name, png_context::offset, png_context::palette, and png_palette_entry::red.

◆ png_image_data()

static int png_image_data ( struct image image,
struct png_context png,
size_t  len 
)
static

Handle PNG image data chunk.

Parameters
imagePNG image
pngPNG context
lenChunk length
Return values
rcReturn status code

Definition at line 335 of file png.c.

336  {
337  int rc;
338 
339  /* Deflate this chunk */
340  if ( ( rc = deflate_inflate ( &png->deflate,
341  ( image->data + png->offset ),
342  len, &png->raw ) ) != 0 ) {
343  DBGC ( image, "PNG %s could not decompress: %s\n",
344  image->name, strerror ( rc ) );
345  return rc;
346  }
347 
348  return 0;
349 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Read-only data.
Definition: image.h:50
size_t offset
Offset within image.
Definition: png.c:46
#define DBGC(...)
Definition: compiler.h:505
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
An executable image.
Definition: image.h:23
ring len
Length.
Definition: dwmac.h:231
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition: deflate.c:483
struct deflate deflate
Decompressor.
Definition: png.c:65
char * name
Name.
Definition: image.h:37

References image::data, DBGC, png_context::deflate, deflate_inflate(), len, image::name, png_context::offset, png_context::raw, rc, and strerror().

◆ png_unfilter_none()

static unsigned int png_unfilter_none ( unsigned int  current,
unsigned int left  __unused,
unsigned int above  __unused,
unsigned int above_left  __unused 
)
static

Unfilter byte using the "None" filter.

Parameters
currentFiltered current byte
leftUnfiltered left byte
aboveUnfiltered above byte
above_leftUnfiltered above-left byte
Return values
currentUnfiltered current byte

Definition at line 360 of file png.c.

363  {
364 
365  return current;
366 }

◆ png_unfilter_sub()

static unsigned int png_unfilter_sub ( unsigned int  current,
unsigned int  left,
unsigned int above  __unused,
unsigned int above_left  __unused 
)
static

Unfilter byte using the "Sub" filter.

Parameters
currentFiltered current byte
leftUnfiltered left byte
aboveUnfiltered above byte
above_leftUnfiltered above-left byte
Return values
currentUnfiltered current byte

Definition at line 377 of file png.c.

380  {
381 
382  return ( current + left );
383 }

◆ png_unfilter_up()

static unsigned int png_unfilter_up ( unsigned int  current,
unsigned int left  __unused,
unsigned int  above,
unsigned int above_left  __unused 
)
static

Unfilter byte using the "Up" filter.

Parameters
currentFiltered current byte
leftUnfiltered left byte
aboveUnfiltered above byte
above_leftUnfiltered above-left byte
Return values
currentUnfiltered current byte

Definition at line 394 of file png.c.

397  {
398 
399  return ( current + above );
400 }

◆ png_unfilter_average()

static unsigned int png_unfilter_average ( unsigned int  current,
unsigned int  left,
unsigned int  above,
unsigned int above_left  __unused 
)
static

Unfilter byte using the "Average" filter.

Parameters
currentFiltered current byte
leftUnfiltered left byte
aboveUnfiltered above byte
above_leftUnfiltered above-left byte
Return values
currentUnfiltered current byte

Definition at line 411 of file png.c.

414  {
415 
416  return ( current + ( ( above + left ) >> 1 ) );
417 }

◆ png_paeth_predictor()

static unsigned int png_paeth_predictor ( unsigned int  a,
unsigned int  b,
unsigned int  c 
)
static

Paeth predictor function (defined in RFC 2083)

Parameters
aPixel A
bPixel B
cPixel C
Return values
predictorPredictor pixel

Definition at line 427 of file png.c.

428  {
429  unsigned int p;
430  unsigned int pa;
431  unsigned int pb;
432  unsigned int pc;
433 
434  /* Algorithm as defined in RFC 2083 section 6.6 */
435  p = ( a + b - c );
436  pa = abs ( p - a );
437  pb = abs ( p - b );
438  pc = abs ( p - c );
439  if ( ( pa <= pb ) && ( pa <= pc ) ) {
440  return a;
441  } else if ( pb <= pc ) {
442  return b;
443  } else {
444  return c;
445  }
446 }
#define abs(x)
Definition: ath.h:45

References abs.

Referenced by png_unfilter_paeth().

◆ png_unfilter_paeth()

static unsigned int png_unfilter_paeth ( unsigned int  current,
unsigned int  left,
unsigned int  above,
unsigned int  above_left 
)
static

Unfilter byte using the "Paeth" filter.

Parameters
currentFiltered current byte
above_leftUnfiltered above-left byte
aboveUnfiltered above byte
leftUnfiltered left byte
Return values
currentUnfiltered current byte

Definition at line 457 of file png.c.

460  {
461 
462  return ( current + png_paeth_predictor ( left, above, above_left ) );
463 }
static unsigned int png_paeth_predictor(unsigned int a, unsigned int b, unsigned int c)
Paeth predictor function (defined in RFC 2083)
Definition: png.c:427

References png_paeth_predictor().

◆ png_unfilter_pass()

static int png_unfilter_pass ( struct image image,
struct png_context png,
struct png_interlace interlace 
)
static

Unfilter one interlace pass of PNG raw data.

Parameters
imagePNG image
pngPNG context
interlaceInterlace pass
Return values
rcReturn status code

This routine may assume that it is impossible to overrun the raw data buffer, since the size is determined by the image dimensions.

Definition at line 502 of file png.c.

503  {
504  size_t pixel_len = png_pixel_len ( png );
505  size_t scanline_len = png_scanline_len ( png, interlace );
506  uint8_t *data = ( png->raw.data + png->raw.offset );
507  struct png_filter *filter;
508  unsigned int scanline;
509  unsigned int byte;
510  unsigned int filter_type;
511  unsigned int left;
512  unsigned int above;
513  unsigned int above_left;
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  filter_type = *(data++);
525  if ( filter_type >= ( sizeof ( png_filters ) /
526  sizeof ( png_filters[0] ) ) ) {
527  DBGC ( image, "PNG %s unknown filter type %d\n",
528  image->name, filter_type );
529  return -ENOTSUP;
530  }
531  filter = &png_filters[filter_type];
532  assert ( filter->unfilter != NULL );
533  DBGC2 ( image, "PNG %s pass %d scanline %d filter type %d\n",
534  image->name, interlace->pass, scanline, filter_type );
535 
536  /* At the start of a line, both above-left and left
537  * bytes are taken to be zero.
538  */
539  left = 0;
540  above_left = 0;
541 
542  /* Iterate over each byte (not pixel) in turn */
543  for ( byte = 0 ; byte < ( scanline_len - 1 ) ; byte++ ) {
544 
545  /* Extract predictor bytes, if applicable */
546  if ( byte >= pixel_len )
547  left = *( data - pixel_len );
548  if ( scanline > 0 )
549  above = *( data - scanline_len );
550  if ( ( scanline > 0 ) && ( byte >= pixel_len ) ) {
551  above_left = *( data - scanline_len -
552  pixel_len );
553  }
554 
555  /* Unfilter current byte */
556  *data = filter->unfilter ( *data, left, above,
557  above_left );
558  data++;
559  }
560  }
561 
562  /* Update offset */
563  png->raw.offset = ( ( ( void * ) data ) - png->raw.data );
564 
565  return 0;
566 }
void * data
Data.
Definition: deflate.h:247
static size_t png_scanline_len(struct png_context *png, struct png_interlace *interlace)
Calculate PNG scanline length.
Definition: png.c:183
UINT8_t filter
Receive packet filter.
Definition: pxe_api.h:68
#define DBGC(...)
Definition: compiler.h:505
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
An executable image.
Definition: image.h:23
unsigned int pass
Pass number.
Definition: png.c:71
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A PNG filter.
Definition: png.c:466
unsigned char uint8_t
Definition: stdint.h:10
size_t offset
Current offset.
Definition: deflate.h:249
unsigned char byte
Definition: smc9000.h:38
static struct png_filter png_filters[]
PNG filter types.
Definition: png.c:483
#define DBGC2(...)
Definition: compiler.h:522
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static unsigned int png_pixel_len(struct png_context *png)
Calculate PNG pixel length.
Definition: png.c:171
char * name
Name.
Definition: image.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
unsigned int height
Height.
Definition: png.c:83

References assert(), data, deflate_chunk::data, DBGC, DBGC2, ENOTSUP, filter, png_interlace::height, image::name, NULL, deflate_chunk::offset, png_interlace::pass, png_filters, png_pixel_len(), png_scanline_len(), and png_context::raw.

Referenced by png_unfilter().

◆ png_unfilter()

static int png_unfilter ( struct image image,
struct png_context png 
)
static

Unfilter PNG raw data.

Parameters
imagePNG image
pngPNG context
Return values
rcReturn status code

This routine may assume that it is impossible to overrun the raw data buffer, since the size is determined by the image dimensions.

Definition at line 578 of file png.c.

578  {
579  struct png_interlace interlace;
580  unsigned int pass;
581  int rc;
582 
583  /* Process each interlace pass */
584  png->raw.offset = 0;
585  for ( pass = 0 ; pass < png->passes ; pass++ ) {
586 
587  /* Calculate interlace pass parameters */
588  png_interlace ( png, pass, &interlace );
589 
590  /* Skip zero-width rows (which have no filter bytes) */
591  if ( interlace.width == 0 )
592  continue;
593 
594  /* Unfilter this pass */
595  if ( ( rc = png_unfilter_pass ( image, png,
596  &interlace ) ) != 0 )
597  return rc;
598  }
599  assert ( png->raw.offset == png->raw.len );
600 
601  return 0;
602 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
An executable image.
Definition: image.h:23
unsigned int pass
Pass number.
Definition: png.c:71
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A PNG interlace pass.
Definition: png.c:69
size_t offset
Current offset.
Definition: deflate.h:249
static int png_unfilter_pass(struct image *image, struct png_context *png, struct png_interlace *interlace)
Unfilter one interlace pass of PNG raw data.
Definition: png.c:502
unsigned int passes
Number of interlace passes.
Definition: png.c:58
static void png_interlace(struct png_context *png, unsigned int pass, struct png_interlace *interlace)
Calculate PNG interlace pass parameters.
Definition: png.c:118
size_t len
Length of data.
Definition: deflate.h:251

References assert(), deflate_chunk::len, deflate_chunk::offset, png_interlace::pass, png_context::passes, png_interlace(), png_unfilter_pass(), png_context::raw, rc, and png_interlace::width.

Referenced by png_image_end().

◆ png_pixel()

static unsigned int png_pixel ( unsigned int  raw,
unsigned int  alpha,
unsigned int  max 
)
inlinestatic

Calculate PNG pixel component value.

Parameters
rawRaw component value
alphaAlpha value
maxMaximum raw/alpha value
Return values
valueComponent value in range 0-255

Definition at line 612 of file png.c.

613  {
614 
615  /* The basic calculation is 255*(raw/max)*(value/max). We use
616  * fixed-point arithmetic (scaling up to the maximum range for
617  * a 32-bit integer), in order to get the same results for
618  * alpha blending as the test cases (produced using
619  * ImageMagick).
620  */
621  return ( ( ( ( ( 0xff00 * raw * alpha ) / max ) / max ) + 0x80 ) >> 8 );
622 }
#define max(x, y)
Definition: ath.h:40
__be32 raw[7]
Definition: CIB_PRM.h:28

References max, and raw.

Referenced by png_pixels_pass().

◆ png_pixels_pass()

static void png_pixels_pass ( struct image image,
struct png_context png,
struct png_interlace interlace 
)
static

Fill one interlace pass of PNG pixels.

Parameters
imagePNG image
pngPNG context
interlaceInterlace pass

This routine may assume that it is impossible to overrun either the raw data buffer or the pixel buffer, since the sizes of both are determined by the image dimensions.

Definition at line 635 of file png.c.

637  {
638  uint8_t channel[png->channels];
639  int is_indexed = ( png->colour_type & PNG_COLOUR_TYPE_PALETTE );
640  int is_rgb = ( png->colour_type & PNG_COLOUR_TYPE_RGB );
641  int has_alpha = ( png->colour_type & PNG_COLOUR_TYPE_ALPHA );
642  const uint8_t *data = ( png->raw.data + png->raw.offset );
643  size_t data_stride;
644  unsigned int pixbuf_y_index;
645  unsigned int pixbuf_index;
646  unsigned int pixbuf_x_stride;
647  unsigned int pixbuf_y_stride;
648  unsigned int y;
649  unsigned int x;
650  unsigned int c;
651  unsigned int bits;
652  unsigned int depth;
653  unsigned int max;
654  unsigned int alpha;
655  unsigned int raw;
656  unsigned int value;
657  uint8_t current = 0;
658  uint32_t pixel;
659 
660  /* We only ever use the top byte of 16-bit pixels. Model this
661  * as a bit depth of 8 with a stride of more than one.
662  */
663  depth = png->depth;
664  data_stride = ( ( depth + 7 ) / 8 );
665  if ( depth > 8 )
666  depth = 8;
667  max = ( ( 1 << depth ) - 1 );
668 
669  /* Calculate pixel buffer offset and strides */
670  pixbuf_y_index = ( ( ( interlace->y_indent * png->pixbuf->width ) +
671  interlace->x_indent ) );
672  pixbuf_x_stride = interlace->x_stride;
673  pixbuf_y_stride = ( interlace->y_stride * png->pixbuf->width );
674  DBGC2 ( image, "PNG %s pass %d %dx%d at (%d,%d) stride (%d,%d)\n",
675  image->name, interlace->pass, interlace->width,
676  interlace->height, interlace->x_indent, interlace->y_indent,
677  interlace->x_stride, interlace->y_stride );
678 
679  /* Iterate over each scanline in turn */
680  for ( y = 0 ; y < interlace->height ; y++ ) {
681 
682  /* Skip filter byte */
683  data++;
684 
685  /* Iterate over each pixel in turn */
686  bits = depth;
687  pixbuf_index = pixbuf_y_index;
688  for ( x = 0 ; x < interlace->width ; x++ ) {
689 
690  /* Extract sample value */
691  for ( c = 0 ; c < png->channels ; c++ ) {
692 
693  /* Get sample value into high bits of current */
694  current <<= depth;
695  bits -= depth;
696  if ( ! bits ) {
697  current = *data;
698  data += data_stride;
699  bits = 8;
700  }
701 
702  /* Extract sample value */
703  channel[c] = ( current >> ( 8 - depth ) );
704  }
705 
706  /* Convert to native pixel format */
707  if ( is_indexed ) {
708 
709  /* Indexed */
710  pixel = png->palette[channel[0]];
711 
712  } else {
713 
714  /* Determine alpha value */
715  alpha = ( has_alpha ?
716  channel[ png->channels - 1 ] : max );
717 
718  /* Convert to RGB value */
719  pixel = 0;
720  for ( c = 0 ; c < 3 ; c++ ) {
721  raw = channel[ is_rgb ? c : 0 ];
722  value = png_pixel ( raw, alpha, max );
723  assert ( value <= 255 );
724  pixel = ( ( pixel << 8 ) | value );
725  }
726  }
727 
728  /* Store pixel */
729  png->pixbuf->data[pixbuf_index] = pixel;
730  pixbuf_index += pixbuf_x_stride;
731  }
732 
733  /* Move to next output row */
734  pixbuf_y_index += pixbuf_y_stride;
735  }
736 
737  /* Update offset */
738  png->raw.offset = ( ( ( const void * ) data ) - png->raw.data );
739 }
void * data
Data.
Definition: deflate.h:247
RGB colour is used.
Definition: png.h:101
uint32_t palette[PNG_PALETTE_COUNT]
Palette, in iPXE's pixel buffer format.
Definition: png.c:60
#define max(x, y)
Definition: ath.h:40
unsigned int x_indent
X starting indent.
Definition: png.c:73
Alpha channel is used.
Definition: png.h:103
unsigned int y_stride
Y stride.
Definition: png.c:79
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
static unsigned int x
Definition: pixbuf.h:62
An executable image.
Definition: image.h:23
unsigned int pass
Pass number.
Definition: png.c:71
unsigned int width
Width.
Definition: png.c:81
unsigned int depth
Bit depth.
Definition: png.c:52
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
Palette is used.
Definition: png.h:99
uint32_t channel
RNDIS channel.
Definition: netvsc.h:14
uint32_t * data
32-bit (8:8:8:8) xRGB pixel data, in host-endian order
Definition: pixbuf.h:24
unsigned char uint8_t
Definition: stdint.h:10
unsigned int x_stride
X stride.
Definition: png.c:77
size_t offset
Current offset.
Definition: deflate.h:249
unsigned int uint32_t
Definition: stdint.h:12
unsigned int colour_type
Colour type.
Definition: png.c:54
struct pixel_buffer * pixbuf
Pixel buffer.
Definition: png.c:49
static volatile void * bits
Definition: bitops.h:27
#define DBGC2(...)
Definition: compiler.h:522
static unsigned int unsigned int y
Definition: pixbuf.h:62
uint8_t data[48]
Additional event data.
Definition: ena.h:22
__be32 raw[7]
Definition: CIB_PRM.h:28
unsigned int channels
Number of channels.
Definition: png.c:56
char * name
Name.
Definition: image.h:37
unsigned int height
Height.
Definition: png.c:83
unsigned int y_indent
Y starting indent.
Definition: png.c:75
unsigned int width
Width.
Definition: pixbuf.h:20
static unsigned int png_pixel(unsigned int raw, unsigned int alpha, unsigned int max)
Calculate PNG pixel component value.
Definition: png.c:612

References assert(), bits, channel, png_context::channels, png_context::colour_type, data, pixel_buffer::data, deflate_chunk::data, DBGC2, png_context::depth, png_interlace::height, max, image::name, deflate_chunk::offset, png_context::palette, png_interlace::pass, png_context::pixbuf, PNG_COLOUR_TYPE_ALPHA, PNG_COLOUR_TYPE_PALETTE, PNG_COLOUR_TYPE_RGB, png_pixel(), raw, png_context::raw, value, pixel_buffer::width, png_interlace::width, x, png_interlace::x_indent, png_interlace::x_stride, y, png_interlace::y_indent, and png_interlace::y_stride.

Referenced by png_pixels().

◆ png_pixels()

static void png_pixels ( struct image image,
struct png_context png 
)
static

Fill PNG pixels.

Parameters
imagePNG image
pngPNG context

This routine may assume that it is impossible to overrun either the raw data buffer or the pixel buffer, since the sizes of both are determined by the image dimensions.

Definition at line 751 of file png.c.

751  {
752  struct png_interlace interlace;
753  unsigned int pass;
754 
755  /* Process each interlace pass */
756  png->raw.offset = 0;
757  for ( pass = 0 ; pass < png->passes ; pass++ ) {
758 
759  /* Calculate interlace pass parameters */
760  png_interlace ( png, pass, &interlace );
761 
762  /* Skip zero-width rows (which have no filter bytes) */
763  if ( interlace.width == 0 )
764  continue;
765 
766  /* Unfilter this pass */
767  png_pixels_pass ( image, png, &interlace );
768  }
769  assert ( png->raw.offset == png->raw.len );
770 }
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
An executable image.
Definition: image.h:23
unsigned int pass
Pass number.
Definition: png.c:71
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A PNG interlace pass.
Definition: png.c:69
size_t offset
Current offset.
Definition: deflate.h:249
static void png_pixels_pass(struct image *image, struct png_context *png, struct png_interlace *interlace)
Fill one interlace pass of PNG pixels.
Definition: png.c:635
unsigned int passes
Number of interlace passes.
Definition: png.c:58
static void png_interlace(struct png_context *png, unsigned int pass, struct png_interlace *interlace)
Calculate PNG interlace pass parameters.
Definition: png.c:118
size_t len
Length of data.
Definition: deflate.h:251

References assert(), deflate_chunk::len, deflate_chunk::offset, png_interlace::pass, png_context::passes, png_interlace(), png_pixels_pass(), png_context::raw, and png_interlace::width.

Referenced by png_image_end().

◆ png_image_end()

static int png_image_end ( struct image image,
struct png_context png,
size_t  len 
)
static

Handle PNG image end chunk.

Parameters
imagePNG image
pngPNG context
lenChunk length
Return values
rcReturn status code

Definition at line 780 of file png.c.

781  {
782  int rc;
783 
784  /* Sanity checks */
785  if ( len != 0 ) {
786  DBGC ( image, "PNG %s invalid IEND length %zd\n",
787  image->name, len );
788  return -EINVAL;
789  }
790  if ( ! png->pixbuf ) {
791  DBGC ( image, "PNG %s missing pixel buffer (no IHDR?)\n",
792  image->name );
793  return -EINVAL;
794  }
795  if ( ! deflate_finished ( &png->deflate ) ) {
796  DBGC ( image, "PNG %s decompression not complete\n",
797  image->name );
798  return -EINVAL;
799  }
800  if ( png->raw.offset != png->raw.len ) {
801  DBGC ( image, "PNG %s incorrect decompressed length (expected "
802  "%zd, got %zd)\n", image->name, png->raw.len,
803  png->raw.offset );
804  return -EINVAL;
805  }
806 
807  /* Unfilter raw data */
808  if ( ( rc = png_unfilter ( image, png ) ) != 0 )
809  return rc;
810 
811  /* Fill pixel buffer */
812  png_pixels ( image, png );
813 
814  return 0;
815 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void png_pixels(struct image *image, struct png_context *png)
Fill PNG pixels.
Definition: png.c:751
static int png_unfilter(struct image *image, struct png_context *png)
Unfilter PNG raw data.
Definition: png.c:578
#define DBGC(...)
Definition: compiler.h:505
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
An executable image.
Definition: image.h:23
static int deflate_finished(struct deflate *deflate)
Check if decompression has finished.
Definition: deflate.h:277
ring len
Length.
Definition: dwmac.h:231
size_t offset
Current offset.
Definition: deflate.h:249
struct deflate deflate
Decompressor.
Definition: png.c:65
struct pixel_buffer * pixbuf
Pixel buffer.
Definition: png.c:49
char * name
Name.
Definition: image.h:37
size_t len
Length of data.
Definition: deflate.h:251

References DBGC, png_context::deflate, deflate_finished(), EINVAL, len, deflate_chunk::len, image::name, deflate_chunk::offset, png_context::pixbuf, png_pixels(), png_unfilter(), png_context::raw, and rc.

◆ png_chunk()

static int png_chunk ( struct image image,
struct png_context png,
uint32_t  type,
size_t  len 
)
static

Handle PNG chunk.

Parameters
imagePNG image
pngPNG context
typeChunk type
lenChunk length
Return values
rcReturn status code

Definition at line 850 of file png.c.

851  {
852  struct png_chunk_handler *handler;
853  unsigned int i;
854 
855  DBGC ( image, "PNG %s chunk type %s offset %zd length %zd\n",
856  image->name, png_type_name ( type ), png->offset, len );
857 
858  /* Handle according to chunk type */
859  for ( i = 0 ; i < ( sizeof ( png_chunk_handlers ) /
860  sizeof ( png_chunk_handlers[0] ) ) ; i++ ) {
861  handler = &png_chunk_handlers[i];
862  if ( handler->type == type )
863  return handler->handle ( image, png, len );
864  }
865 
866  /* Fail if unknown chunk type is critical */
867  if ( ! ( type & htonl ( PNG_CHUNK_ANCILLARY ) ) ) {
868  DBGC ( image, "PNG %s unknown critical chunk type %s\n",
869  image->name, png_type_name ( type ) );
870  return -ENOTSUP;
871  }
872 
873  /* Ignore non-critical unknown chunk types */
874  return 0;
875 }
Chunk is ancillary.
Definition: png.h:42
static const char * png_type_name(uint32_t type)
Transcribe PNG chunk type name (for debugging)
Definition: png.c:101
size_t offset
Offset within image.
Definition: png.c:46
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
int(* handle)(struct image *image, struct png_context *png, size_t len)
Handle chunk.
Definition: png.c:829
An executable image.
Definition: image.h:23
static struct png_chunk_handler png_chunk_handlers[]
PNG chunk handlers.
Definition: png.c:834
#define htonl(value)
Definition: byteswap.h:133
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
ring len
Length.
Definition: dwmac.h:231
A PNG chunk handler.
Definition: png.c:818
uint32_t type
Chunk type.
Definition: png.c:820
char * name
Name.
Definition: image.h:37

References DBGC, ENOTSUP, png_chunk_handler::handle, htonl, len, image::name, png_context::offset, PNG_CHUNK_ANCILLARY, png_chunk_handlers, png_type_name(), type, and png_chunk_handler::type.

Referenced by png_pixbuf().

◆ png_pixbuf()

static int png_pixbuf ( struct image image,
struct pixel_buffer **  pixbuf 
)
static

Convert PNG image to pixel buffer.

Parameters
imagePNG image
pixbufPixel buffer to fill in
Return values
rcReturn status code

Definition at line 884 of file png.c.

884  {
885  struct png_context *png;
886  const struct png_chunk_header *header;
887  const struct png_chunk_footer *footer;
888  size_t remaining;
889  size_t chunk_len;
890  int rc;
891 
892  /* Allocate and initialise context */
893  png = zalloc ( sizeof ( *png ) );
894  if ( ! png ) {
895  rc = -ENOMEM;
896  goto err_alloc;
897  }
898  png->offset = sizeof ( struct png_signature );
899  deflate_init ( &png->deflate, DEFLATE_ZLIB );
900 
901  /* Process chunks */
902  do {
903 
904  /* Extract chunk header */
905  remaining = ( image->len - png->offset );
906  if ( remaining < ( sizeof ( *header ) + sizeof ( *footer ) ) ){
907  DBGC ( image, "PNG %s truncated chunk header/footer "
908  "at offset %zd\n", image->name, png->offset );
909  rc = -EINVAL;
910  goto err_truncated;
911  }
912  header = ( image->data + png->offset );
913  png->offset += sizeof ( *header );
914 
915  /* Validate chunk length */
916  chunk_len = ntohl ( header->len );
917  if ( chunk_len > ( remaining - sizeof ( *header ) -
918  sizeof ( *footer ) ) ) {
919  DBGC ( image, "PNG %s truncated chunk data at offset "
920  "%zd\n", image->name, png->offset );
921  rc = -EINVAL;
922  goto err_truncated;
923  }
924 
925  /* Handle chunk */
926  if ( ( rc = png_chunk ( image, png, header->type,
927  chunk_len ) ) != 0 )
928  goto err_chunk;
929 
930  /* Move to next chunk */
931  png->offset += ( chunk_len + sizeof ( *footer ) );
932 
933  } while ( png->offset < image->len );
934 
935  /* Check that we finished with an IEND chunk */
936  if ( header->type != htonl ( PNG_TYPE_IEND ) ) {
937  DBGC ( image, "PNG %s did not finish with IEND\n",
938  image->name );
939  rc = -EINVAL;
940  goto err_iend;
941  }
942 
943  /* Return pixel buffer */
944  *pixbuf = pixbuf_get ( png->pixbuf );
945 
946  /* Success */
947  rc = 0;
948 
949  err_iend:
950  err_chunk:
951  err_truncated:
952  pixbuf_put ( png->pixbuf );
953  ufree ( png->raw.data );
954  free ( png );
955  err_alloc:
956  return rc;
957 }
void * data
Data.
Definition: deflate.h:247
static __always_inline void ufree(void *ptr)
Free external memory.
Definition: umalloc.h:67
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int png_chunk(struct image *image, struct png_context *png, uint32_t type, size_t len)
Handle PNG chunk.
Definition: png.c:850
const void * data
Read-only data.
Definition: image.h:50
size_t offset
Offset within image.
Definition: png.c:46
#define DBGC(...)
Definition: compiler.h:505
struct deflate_chunk raw
Decompression buffer for raw PNG data.
Definition: png.c:63
#define ntohl(value)
Definition: byteswap.h:134
An executable image.
Definition: image.h:23
#define htonl(value)
Definition: byteswap.h:133
#define ENOMEM
Not enough space.
Definition: errno.h:534
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition: deflate.c:991
PNG context.
Definition: png.c:44
#define PNG_TYPE_IEND
PNG image end chunk type.
Definition: png.h:175
ZLIB header and footer.
Definition: deflate.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
size_t len
Length of raw file image.
Definition: image.h:55
struct deflate deflate
Decompressor.
Definition: png.c:65
struct pixel_buffer * pixbuf
Pixel buffer.
Definition: png.c:49
struct ena_llq_option header
Header locations.
Definition: ena.h:16
A PNG file signature.
Definition: png.h:17
A PNG chunk header.
Definition: png.h:26
char * name
Name.
Definition: image.h:37

References image::data, deflate_chunk::data, DBGC, png_context::deflate, deflate_init(), DEFLATE_ZLIB, EINVAL, ENOMEM, free, header, htonl, image::len, image::name, ntohl, png_context::offset, png_context::pixbuf, png_chunk(), PNG_TYPE_IEND, png_context::raw, rc, ufree(), and zalloc().

◆ png_probe()

static int png_probe ( struct image image)
static

Probe PNG image.

Parameters
imagePNG image
Return values
rcReturn status code

Definition at line 965 of file png.c.

965  {
966  const struct png_signature *signature;
967 
968  /* Sanity check */
969  if ( image->len < sizeof ( *signature ) ) {
970  DBGC ( image, "PNG %s is too short\n", image->name );
971  return -ENOEXEC;
972  }
973 
974  /* Check signature */
975  signature = image->data;
976  if ( memcmp ( signature, &png_signature, sizeof ( *signature ) ) != 0 ){
977  DBGC ( image, "PNG %s has invalid signature\n", image->name );
978  return -ENOEXEC;
979  }
980 
981  return 0;
982 }
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
size_t len
Length of raw file image.
Definition: image.h:55
A PNG file signature.
Definition: png.h:17
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
char * name
Name.
Definition: image.h:37

References image::data, DBGC, ENOEXEC, image::len, memcmp(), image::name, and signature.

◆ __image_type()

struct image_type png_image_type __image_type ( PROBE_NORMAL  )

PNG image type.

Variable Documentation

◆ png_signature

PNG file signature.

Definition at line 87 of file png.c.

◆ png_interlace_passes

uint8_t png_interlace_passes[]
static
Initial value:
= {
}
Adam7 interlacing.
Definition: png.h:130
No interlacing.
Definition: png.h:128

Number of interlacing passes.

Definition at line 90 of file png.c.

Referenced by png_image_header().

◆ png_filters

struct png_filter png_filters[]
static
Initial value:
= {
}
static unsigned int png_unfilter_average(unsigned int current, unsigned int left, unsigned int above, unsigned int above_left __unused)
Unfilter byte using the "Average" filter.
Definition: png.c:411
No filtering.
Definition: png.h:163
Above byte used as predictor.
Definition: png.h:167
static unsigned int png_unfilter_sub(unsigned int current, unsigned int left, unsigned int above __unused, unsigned int above_left __unused)
Unfilter byte using the "Sub" filter.
Definition: png.c:377
static unsigned int png_unfilter_paeth(unsigned int current, unsigned int left, unsigned int above, unsigned int above_left)
Unfilter byte using the "Paeth" filter.
Definition: png.c:457
Above and left bytes used as predictors.
Definition: png.h:169
Paeth filter.
Definition: png.h:171
static unsigned int png_unfilter_none(unsigned int current, unsigned int left __unused, unsigned int above __unused, unsigned int above_left __unused)
Unfilter byte using the "None" filter.
Definition: png.c:360
Left byte used as predictor.
Definition: png.h:165
static unsigned int png_unfilter_up(unsigned int current, unsigned int left __unused, unsigned int above, unsigned int above_left __unused)
Unfilter byte using the "Up" filter.
Definition: png.c:394

PNG filter types.

Definition at line 483 of file png.c.

Referenced by png_unfilter_pass().

◆ png_chunk_handlers

struct png_chunk_handler png_chunk_handlers[]
static
Initial value:
= {
}
static int png_image_data(struct image *image, struct png_context *png, size_t len)
Handle PNG image data chunk.
Definition: png.c:335
#define htonl(value)
Definition: byteswap.h:133
#define PNG_TYPE_IDAT
PNG image data chunk type.
Definition: png.h:158
A PNG image header.
Definition: png.h:79
#define PNG_TYPE_IEND
PNG image end chunk type.
Definition: png.h:175
A PNG palette chunk.
Definition: png.h:149
#define PNG_TYPE_IHDR
PNG image header chunk type.
Definition: png.h:76
static int png_image_end(struct image *image, struct png_context *png, size_t len)
Handle PNG image end chunk.
Definition: png.c:780
#define PNG_TYPE_PLTE
PNG palette chunk type.
Definition: png.h:136

PNG chunk handlers.

Definition at line 834 of file png.c.

Referenced by png_chunk().