ChunkPNG (chunk_png v1.0.347)

Library for manipulating metadata in PNG files.

PNG files consist of a magic number, a mandatory header chunk, then a list of optional chunks. This library splits the file into a list of chunks with a cursor and allows you to insert and remove chunks.

A standard PNG may consist of:

  1. magic number (8 bytes)
  2. IHDR - mandatory first chunk of a PNG datastream
  3. PLTE - pallete for indexed PNG images <--
  4. IDAT - image data chunk(s)
  5. IEND - image trailer, the last in a PNG datastream

Consider the use case of inserting copyright metadata. After parsing your image you receive a list of chunks in the form of a list-zipper which is a tuple of the list of chunks up until IDHR, then the focus (probably PLTE), then the remaining list of chunks after the focus. You may immediately insert your new chunks to the left of the focus and after the IHDR, or navigate to the end and append new chunks there.

There are three forms of textual chunks:

  • tEXt - simple key-value using the Latin-1 character set
  • iTXt - simple key-value using UTF-8 encoding with optional value compression
  • zTXt - equivalent to tEXt but using deflate compression for large text blocks

When finished the list of chunks can be written out to a file or a buffer.

Link to this section Summary

Functions

Insert a chunk to the left of the focus

Parse a PNG from in-memory buffer

Parse a PNG file into a zipper of a list of chunks

Write a chunk zipper as a binary PNG datastream

Write a chunk zipper to a PNG file

Link to this section Types

Link to this section Functions

Link to this function

insert_left(arg, chunk)

Insert a chunk to the left of the focus

Link to this function

parse_buffer(data)

@spec parse_buffer(binary()) :: {:ok, zipper()} | {:error, any()}

Parse a PNG from in-memory buffer

Link to this function

parse_file!(path)

Parse a PNG file into a zipper of a list of chunks

Link to this function

write_buffer(arg)

Write a chunk zipper as a binary PNG datastream

Link to this function

write_file(arg, path)

Write a chunk zipper to a PNG file