Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Tiny Jpeg Decoder

Tiny Jpeg Decoder is a small library written in C by Luc Saillard. Converted to D by Tomas Lindquist Olsen.

The original source can be found at http://www.saillard.org/programs_and_patches/tinyjpegdecoder/

The version used for the conversion has been updated to 20070609:

Example

// Example:
// Shows how to decode a Jpeg image into a RGB24 buffer.

import tinyjpeg.tinyjpeg;

// init tinyjpeg
jdec_private* jdec = tinyjpeg_init();
if (jdec is null)
    throw new Exception("Not enough memory to initialize JPEG decoder");
scope(exit) tinyjpeg_free(jdec);

// load file into memory
scope buffer = std.file.read("image.jpg");

// parse jpeg header
if (tinyjpeg_parse_header(jdec, buffer.ptr, buffer.length) < 0)
    throw new Exception("Failed to parse JPEG header: "~tinyjpeg_get_errorstring(jdec));

// get dimensions
uint w,h;
tinyjpeg_get_size(jdec, &w, &h);

// decode pixel data
if (tinyjpeg_decode(jdec, TINYJPEG_FMT_RGB24) < 0)
    throw new Exception("Failed to decode JPEG: "~tinyjpeg_get_errorstring(jdec));

// copy the pixel data
ubyte*[3] components;
tinyjpeg_get_components(jdec, components.ptr);
ubyte[] pixels = components[0][0..w*h*3].dup;