View mode: basic / threaded / horizontal-split · Log in · Help
June 16, 2012
JPG and PNG decoder
I've been working on decoders for simple (baseline) JPEG and 
PNG's, mostly for my own amusement, and they seem to work ok now, 
so if anyone has need of some simple D modules to load these 
formats you can grab them here:
https://github.com/callumenator/imaged

Notes:
- Will load 8-bit baseline sequential JPEG's only. All PNG image 
types are supported (only critical chunks are decoded however).
- Can load from a stream or a file. In the case of the PNG's, 
this allows progressive display with an interlaced image.
- I haven't put much effort into speeding up these routines, and 
have not tested the JPEG decoder extensively, so there are bound 
to be bugs and there is plenty room for improvement.
- The example shows stream usage, and uses Adam Ruppe's 
simpledisplay to make a little viewer, that can flick through all 
images in a directory.

Cheers,
cal
June 16, 2012
Re: JPG and PNG decoder
On 6/16/2012 12:10 PM, cal wrote:
> I've been working on decoders for simple (baseline) JPEG and PNG's, mostly for
> my own amusement, and they seem to work ok now, so if anyone has need of some
> simple D modules to load these formats you can grab them here:
> https://github.com/callumenator/imaged

Thank you!
June 17, 2012
Re: JPG and PNG decoder
> On 6/16/2012 12:10 PM, cal wrote:
>>
>> I've been working on decoders for simple (baseline) JPEG and PNG's, mostly
>> for
>> my own amusement, and they seem to work ok now, so if anyone has need of
>> some
>> simple D modules to load these formats you can grab them here:
>> https://github.com/callumenator/imaged

Nice one, thanks.

Is there a way to write an Image to disk? I didn't see one while
skimming the source.
June 17, 2012
Re: JPG and PNG decoder
On Sun, Jun 17, 2012 at 8:15 AM, Philippe Sigaud
<philippe.sigaud@gmail.com> wrote:

>> I've been working on decoders for simple (baseline) JPEG and PNG's, mostly
>>so if anyone has need of some simple D modules to load these formats you can grab them here:

> Is there a way to write an Image to disk? I didn't see one while
> skimming the source.

Oops, you*did* say these where for loading. Sorry for the noise :)

Still, I'm interested in writing a JPEG/PNG to disk from a
ubyte[3][][], or whatever.
June 17, 2012
Re: JPG and PNG decoder
On Sunday, 17 June 2012 at 07:07:35 UTC, Philippe Sigaud wrote:
> Still, I'm interested in writing a JPEG/PNG to disk from a
> ubyte[3][][], or whatever.

Do you mean that you want to encode a ubyte array to disk as 
JPEG/PNG? Encoding a JPEG would be a bit of work I think, the 
format's kind of a monster. PNG should be easier, depending on 
how good you want the compression to be. If you don't care too 
much about compression level, you simply zlib compress the data, 
write it out by image row/scanline, include appropriate header 
and chunk info, and you're done. I'll give a simple encoder a go 
if you think you could use it.

Cheers,
cal
June 17, 2012
Re: JPG and PNG decoder
I second the request for a PNG encoder.  You just saved me a lot 
of trouble, as I was about to implement my own PNG loader... and 
now I don't have to.  ^_^  I'll glance over your code in full 
sometime and see if I notice any readily apparent improvements.
June 17, 2012
Re: JPG and PNG decoder
cal:

> I've been working on decoders for simple (baseline) JPEG and 
> PNG's, mostly for my own amusement, and they seem to work ok 
> now, so if anyone has need of some simple D modules to load 
> these formats you can grab them here:
> https://github.com/callumenator/imaged

Suggestions on the code:
- Try to use final switches.
- switch cases don't need (), so instead of case(foo):  write 
case foo:
- Try to add const/immutable/pure/nothrow where possible.

Bye,
bearophile
June 17, 2012
Re: JPG and PNG decoder
On Sun, Jun 17, 2012 at 9:55 AM, cal <callumenator@gmail.com> wrote:
> On Sunday, 17 June 2012 at 07:07:35 UTC, Philippe Sigaud wrote:
>>
>> Still, I'm interested in writing a JPEG/PNG to disk from a
>> ubyte[3][][], or whatever.
>
>
> Do you mean that you want to encode a ubyte array to disk as JPEG/PNG?
> Encoding a JPEG would be a bit of work I think, the format's kind of a
> monster. PNG should be easier, depending on how good you want the
> compression to be. If you don't care too much about compression level, you
> simply zlib compress the data, write it out by image row/scanline, include
> appropriate header and chunk info, and you're done. I'll give a simple
> encoder a go if you think you could use it.

Years ago, one of the first programs I did in D was a raytracer. It
was a good test for OOP, structs, 3D vectors, foreach loops (which
were all the rage 4-5 years ago, even before ranges). It was nice and
fun, but I then discovered that, at the time, I had no easy way to
write my pixel 2D arrays to disk. I had to code a small BMP writer.
Nowdays, I'm playing with the idea to write a new tutorial for D,
using a raytracer and new D technics like CTFE / templates /
std.algorithms. This time, I would like to save the images as JPEG /
PNG.

I guess it's possible to wrap some C lib to do the work, but it's be
nice to have a D module to do that.

Anyway, nice work with the loader!
June 17, 2012
Re: JPG and PNG decoder
Am 16.06.2012 21:10, schrieb cal:
> I've been working on decoders for simple (baseline) JPEG and PNG's,
> mostly for my own amusement, and they seem to work ok now, so if anyone
> has need of some simple D modules to load these formats you can grab
> them here:
> https://github.com/callumenator/imaged
>
> Notes:
> - Will load 8-bit baseline sequential JPEG's only. All PNG image types
> are supported (only critical chunks are decoded however).
> - Can load from a stream or a file. In the case of the PNG's, this
> allows progressive display with an interlaced image.
> - I haven't put much effort into speeding up these routines, and have
> not tested the JPEG decoder extensively, so there are bound to be bugs
> and there is plenty room for improvement.
> - The example shows stream usage, and uses Adam Ruppe's simpledisplay to
> make a little viewer, that can flick through all images in a directory.
>
> Cheers,
> cal

Cool so I don't need to use my stb_image binding ( 
https://bitbucket.org/dav1d/gl-utils/src/0b97f77c14d7/stb_image ) anylonger!
June 17, 2012
Re: JPG and PNG decoder
On 17/06/2012 08:55, cal wrote:

<snip>
> If you don't care too much about compression level, you simply zlib
> compress the data, write it out by image row/scanline, include
> appropriate header and chunk info, and you're done.
<snip>

Not quite.  You need to encode it by scanline, add the filter byte at the beginning of 
each line, _then_ zlib compress it.  The filter byte can just be 0 throughout if you don't 
want to worry about filtering.

If you just want a basic truecolour or greyscale PNG encoder, then it's a matter of:
- write the PNG signature
- write the IHDR, being careful about byte order
- write the IDAT - zlib compressed image data
- write the IEND

You also need to compute the CRC of each chunk, but std.zlib has a function to do that as 
well.

FWIW a while ago I wrote a simple experimental program that generates an image and encodes 
it as a PNG.  And I've just tweaked it and updated it to D2 (attached).  It supports only 
truecolour with 8 bits per sample, but it supports filtering, though it isn't adaptive 
(you just specify the filter to use when you run the program).

Stewart.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home