| 1 |
This is a D port of the C library "Tiny Jpeg Decoder" by Luc Saillard. |
|---|
| 2 |
Conversion by Tomas Lindquist Olsen (tomas at famolsen.dk) |
|---|
| 3 |
|
|---|
| 4 |
The original C version is available at: |
|---|
| 5 |
http://www.saillard.org/programs_and_patches/tinyjpegdecoder/ |
|---|
| 6 |
|
|---|
| 7 |
The code in this package is based on 20070609 from that address. |
|---|
| 8 |
|
|---|
| 9 |
While the port is as direct as possible, the 'tinyjpeg_get_errorstring' function returns a D string, not a C string. |
|---|
| 10 |
Garbage should be minimal, and most memory is allocated using C's 'malloc', so proper cleanup is important! |
|---|
| 11 |
|
|---|
| 12 |
Take a look in the original C source for more information. |
|---|
| 13 |
|
|---|
| 14 |
// Example: |
|---|
| 15 |
// Shows how to decode a Jpeg image into a RGB24 buffer. |
|---|
| 16 |
|
|---|
| 17 |
import tinyjpeg.tinyjpeg; |
|---|
| 18 |
|
|---|
| 19 |
// init tinyjpeg |
|---|
| 20 |
jdec_private* jdec = tinyjpeg_init(); |
|---|
| 21 |
if (jdec is null) |
|---|
| 22 |
throw new Exception("Not enough memory to initialize JPEG decoder"); |
|---|
| 23 |
scope(exit) tinyjpeg_free(jdec); |
|---|
| 24 |
|
|---|
| 25 |
// load file into memory |
|---|
| 26 |
scope buffer = std.file.read("image.jpg"); |
|---|
| 27 |
|
|---|
| 28 |
// parse jpeg header |
|---|
| 29 |
if (tinyjpeg_parse_header(jdec, buffer.ptr, buffer.length) < 0) |
|---|
| 30 |
throw new Exception("Failed to parse JPEG header: "~tinyjpeg_get_errorstring(jdec)); |
|---|
| 31 |
|
|---|
| 32 |
// get dimensions |
|---|
| 33 |
uint w,h; |
|---|
| 34 |
tinyjpeg_get_size(jdec, &w, &h); |
|---|
| 35 |
|
|---|
| 36 |
// decode pixel data |
|---|
| 37 |
if (tinyjpeg_decode(jdec, TINYJPEG_FMT_RGB24) < 0) |
|---|
| 38 |
throw new Exception("Failed to decode JPEG: "~tinyjpeg_get_errorstring(jdec)); |
|---|
| 39 |
|
|---|
| 40 |
// copy the pixel data |
|---|
| 41 |
ubyte*[3] components; |
|---|
| 42 |
tinyjpeg_get_components(jdec, components.ptr); |
|---|
| 43 |
ubyte[] pixels = components[0][0..w*h*3].dup; |
|---|