arsd.png

PNG file read and write. Leverages color.d's MemoryImage interfaces for interop.

The main high-level functions you want are readPng, readPngFromBytes, writePng, and maybe writeImageToPngFile or writePngLazy for some circumstances.

The other functions are low-level implementations and helpers for dissecting the png file format.

Public Imports

arsd.color
public import arsd.color;
Undocumented in source.

Members

Enums

PngType
enum PngType

Represents the different types of png files, with numbers matching what the spec gives for filevalues.

Functions

blankPNG
PNG* blankPNG(PngHeader h)

Creates a new PNG object from the given header parameters, ready to receive data.

bytesPerPixel
int bytesPerPixel(PngHeader header)
fetchPalette
Color[] fetchPalette(PNG* p)

Extracts the palette chunk from a PNG object as an array of RGBA quads.

getANDMask
ubyte[] getANDMask(PNG* p)

Takes the transparency info and returns an AND mask suitable for use in a Windows ico

getHeader
PngHeader getHeader(PNG* p)

Gets the parsed PngHeader data out of the PNG object.

getHeaderFromFile
PngHeader getHeaderFromFile(string filename)

Opens a file and pulls the PngHeader out, leaving the rest of the data alone.

imageFromPng
MemoryImage imageFromPng(PNG* png)

The return value should be casted to indexed or truecolor depending on what the file is. You can also use getAsTrueColorImage to forcibly convert it if needed.

pngFromBytes
auto pngFromBytes(Range r)

Given an input range of bytes, return a lazy PNG file

pngFromImage
PNG* pngFromImage(IndexedImage i)

Creates the PNG data structure out of an IndexedImage. This structure will have the minimum number of colors needed to represent the image faithfully in the file and will be ready for writing to a file.

pngFromImage
PNG* pngFromImage(TrueColorImage i)

Creates the PNG data structure out of a TrueColorImage. This implementation currently always make the file a true color with alpha png type.

readPng
MemoryImage readPng(string filename)

Easily reads a png file into a MemoryImage

readPng
PNG* readPng(ubyte[] data)

Given an in-memory array of bytes from a PNG file, returns the parsed out PNG object.

readPngChunks
LazyPngChunks!(Range) readPngChunks(Range r)

Lazily breaks the buffered input range into png chunks, as defined in the PNG spec

readPngChunks
auto readPngChunks(Range r)

Same as above, but takes a regular input range instead of a buffered one. Provided for easier compatibility with standard input ranges (for example, std.stdio.File.byChunk)

readPngFromBytes
MemoryImage readPngFromBytes(const(ubyte)[] bytes)

Easily reads a png from a data array into a MemoryImage.

replacePalette
void replacePalette(PNG* p, Color[] colors)

Replaces the palette data in a PNG object.

unfilter
immutable(ubyte)[] unfilter(ubyte filterType, ubyte[] data, ubyte[] previousLine, int bpp)

Png files apply a filter to each line in the datastream, hoping to aid in compression. This undoes that as you load.

writeImageToPngFile
void writeImageToPngFile(char[] filename, TrueColorImage image)

this is just like writePng(filename, pngFromImage(image)), but it manages its own memory and writes straight to the file instead of using intermediate buffers that might not get gc'd right

writePng
void writePng(string filename, MemoryImage mi)

Saves a MemoryImage to a png file. See also: writeImageToPngFile which uses memory a little more efficiently

writePng
void writePng(string filename, ubyte[] data, int width, int height, PngType type, ubyte depth)

Saves an image from an existing array of pixel data. Note that depth other than 8 may not be implemented yet. Also note depth of 16 must be stored big endian

writePng
ubyte[] writePng(PNG* p)

Turns a PNG structure into an array of bytes, ready to be written to a file.

writePngLazy
void writePngLazy(OutputRange where, InputRange image)

turns a range of png scanlines into a png file in the output range. really weird

Static variables

PNG_MAGIC_NUMBER
immutable(ubyte[]) PNG_MAGIC_NUMBER;

All PNG files are supposed to open with these bytes according to the spec

Structs

BufferedInputRange
struct BufferedInputRange(Range)

Allows appending to front on a regular input range, if that range is an array. It appends to the array rather than creating an array of arrays; it's meant to make the illusion of one continuous front rather than simply adding capability to walk backward to an existing input range.

Chunk
struct Chunk

A PNG file consists of the magic number then a stream of chunks. This struct represents those chunks.

LazyPngChunks
struct LazyPngChunks(T)

See: readPngChunks

LazyPngFile
struct LazyPngFile(LazyPngChunksProvider)

Lazily reads out basic info from a png (header, palette, image data) It will only allocate memory to read a palette, and only copies on the header and the palette. It ignores everything else.

PNG
struct PNG

Represents the PNG file's data. This struct is intended to be passed around by pointer.

PngHeader
struct PngHeader

The first chunk in a PNG file is a header that contains this info

Templates

isBufferedInputRange
template isBufferedInputRange(R)

* Buffered input range - generic, non-image code Is the given range a buffered input range? That is, an input range that also provides consumeFromFront(int) and appendToFront()

See Also

  • arsd.image has generic load interfaces that can handle multiple file formats, including png.
  • arsd.apng handles the animated png extensions.

Meta

History

Originally written in 2009. This is why some of it is still written in a C-like style!