C++ Library for creating PNGs or JPEGs

H

hamishd

Does anyone know of a good library (free or otherwise) for creating
PNG or JPEG images?

Apologies if OT.
 
J

Juha Nieminen

hamishd said:
Does anyone know of a good library (free or otherwise) for creating
PNG or JPEG images?

Well, you could try to use libpng and libjpeg (both of which are C
libraries) directly, but they are quite complicated to use and require
a significant amount of documentation reading before you can do anything
with them.

Of course you could also try googling for some easy-to-use straightforward
C++ wrappers for those. In my personal experience, though, these wrappers
often tend to be really huge, trying to do *everything* (supporting every
possible file format, supporting tons and tons of image creation and
manipulation functions, and some even supporting viewing the image on
screen). While some/many of them are relatively easy to use, their sheer
humongous size often makes them somewhat impractical in very small projects
where all you want is just to write a bitmap to a png or jpeg file.

I once wrote for myself an extremely simplistic and small class for reading
and writing png files, which can be used with simple one-liners, without all
the fuss. You can find it here: http://warp.povusers.org/PngImage.zip
(Note that it requires libpng. If that's out of the question, then I'm
afraid I can't be of much help.)
 
J

Jorgen Grahn

Well, you could try to use libpng and libjpeg (both of which are C
libraries) directly,

(Side note: these are the more or less the reference implementation of
those file formats, in very wide use at least on Unix systems.)
but they are quite complicated to use and require
a significant amount of documentation reading before you can do anything
with them.

I haven't used them (just libpbm), but can it really be that bad? I
look at e.g. John Bradley's 'xv', and see his WritePNG() function,
which uses libpng, is at just below 400 lines ... but 100 of those are
for text comments and many of the others are for various image depths.

I suspect that much of the trouble will come from converting your
internal representation of the image to what the library wants.

Which brings another question: does hamish have the internal part done
already, or is he maybe even asking for a complete 2D drawing package,
not just the encoding?

/Jorgen
 
R

Richard

[Please do not mail me a copy of your followup]

Juha Nieminen <[email protected]> spake the secret code
Well, you could try to use libpng and libjpeg (both of which are C
libraries) directly, but they are quite complicated to use and require
a significant amount of documentation reading before you can do anything
with them.

As noted elsewhere, these are the defacto standard libraries for these
formats. Both are C libraries, but you can use them from C++ with no
problems.

libjpeg: <http://www.ijg.org/>
libpng: <http://www.libpng.org/pub/png/libpng.html>

I disagree with your assertion that it takes reading significant
documentation before doing anything with them. I've used them both,
and they are both structured roughly the same:

- initialize
- for each scanline in image:
- call function to read/write a scanline
- cleanup

PNG is a rich file format and there is lots of additional metadata
you can process or ignore. Its easy to just ignore it, but when you
need that addtional metadata (say you actually *care* about image
gamma), its important to have it there. JPEG has metadata associated
with the file format as well and the same applies: use it or ignore
it.

Its fairly simple to read and write PNG and JPEG files using these
libraries and even beginner programmers have literally been doing just
that for over a decade now.
 
H

hamishd

(Side note: these are the more or less the reference implementation of
those file formats, in very wide use at least on Unix systems.)


I haven't used them (just libpbm), but can it really be that bad?  I
look at e.g. John Bradley's 'xv', and see his WritePNG() function,
which uses libpng, is at just below 400 lines ... but 100 of those are
for text comments and many of the others are for various image depths.

I suspect that much of the trouble will come from converting your
internal representation of the image to what the library wants.

Which brings another question: does hamish have the internal part done
already, or is he maybe even asking for a complete 2D drawing package,
not just the encoding?

I have a set of polygons, which I draw to the screen. But I also want
to draw these to a PNG file. I want to be able to create line-
segments, and possibly fill the polygon with a colour.

These images will be displayed in a web-browser, and it has been
suggested that PNG is the best format for scaling / adjusting to
window size.
 
I

Ian Collins

I have a set of polygons, which I draw to the screen. But I also want
to draw these to a PNG file. I want to be able to create line-
segments, and possibly fill the polygon with a colour.

These images will be displayed in a web-browser, and it has been
suggested that PNG is the best format for scaling / adjusting to
window size.

libGD might be what you are looking for.

http://www.libgd.org/Main_Page
 
R

Richard

[Please do not mail me a copy of your followup]

hamishd <[email protected]> spake the secret code
I have a set of polygons, which I draw to the screen. But I also want
to draw these to a PNG file. I want to be able to create line-
segments, and possibly fill the polygon with a colour.

Its really straightforward to capture the contents of the screen into
a PNG file.
These images will be displayed in a web-browser, and it has been
suggested that PNG is the best format for scaling / adjusting to
window size.

If you're drawing vector art and want to display that in a web
browser, SVG output might be better (although you'll need a javascript
SVG library to deal with IE).
 
M

Miles Bader

Juha Nieminen said:
Well, you could try to use libpng and libjpeg (both of which are C
libraries) directly, but they are quite complicated to use and require
a significant amount of documentation reading before you can do anything
with them.

I've used libpng and libjpeg for reading/writing images from C++, and
they didn't seem particularly hard to use, though there's some quirks
(it's pretty easy to just copy the appropriate boilerplate from the
provided example code though).

[What little bits of complexity there were generally seemed to be
related to _reading_ (e.g., handling multiple image format variants);
just _writing_ images is quite simple with both libraries.]

-Miles
 
R

Richard Kettlewell

hamishd said:
I have a set of polygons, which I draw to the screen. But I also want
to draw these to a PNG file. I want to be able to create line-
segments, and possibly fill the polygon with a colour.

These images will be displayed in a web-browser, and it has been
suggested that PNG is the best format for scaling / adjusting to
window size.

http://cairographics.org/cairomm/
 
B

BGB / cr88192

hamishd said:
Does anyone know of a good library (free or otherwise) for creating
PNG or JPEG images?

Apologies if OT.

for my own uses, I wrote my own code (in plain C) for reading/writing both
PNG and JPEG files.

main reason:
personally I don't much like external library dependencies (they are a
hassle to deal with, for example, when building on Windows), and would
rather avoid dragging around these libraries.

so, for my uses, I wrote my own code.


however, I have noted a few issues:
my JPEG reader seems to occasionally foul things up in one of the apps I use
it in, I suspect because it is overflowing buffers somewhere or similar;
my PNG writer seems to occasionally mess up when encoding images (leaving
images which are partly fouled up), I suspect because there is a bug in my
deflater somewhere (I suspect in the LZ77 string match function or similar),
but have not thus far identified it...

also, they tend to only really handle simple cases, and don't really bother
with more advanced features (like metadata, or supporting anything other
than 24/32 bpp RGB/RGBA images...).

also, both assume that the entire image fits in memory, and so aren't really
usable for "absurd" image sizes (mostly I use them for loading texturemaps
and similar...).


so, I can't entirely recommend my own code unless one is willing to dig
around in it and try to fix bugs if needed, but oh well...

or such...
 
J

Juha Nieminen

Jorgen Grahn said:
I haven't used them (just libpbm), but can it really be that bad? I
look at e.g. John Bradley's 'xv', and see his WritePNG() function,
which uses libpng, is at just below 400 lines ... but 100 of those are
for text comments and many of the others are for various image depths.

That's the problem. If you are just writing a quick&small program where
you want to read or write PNG files, you don't want to write 300 lines of
code just to do that. You don't even want to write 50 lines of code.
Optimally, you can do it with one or two lines of code. In such a small
program you usually don't care about bith depths, palettes, scanlines and
whatnot. You simply have a RGB bitmap and you want to read or write it to
a PNG file, that's all.

Yes, it *is* perfectly possible to implement a wrapper around libpng
which allows you to do that. There's a small example program in the wrapper
I made and which I posted a link to early that demonstrates the principle:

#include "PngImage.hh"

int main()
{
PngImage image(256, 256);

for(int y = 0; y < 256; ++y)
for(int x = 0; x < 256; ++x)
image.setPixel(x, y, x, y, 0);

image.saveImage("example.png");
}

Note that exactly 1 line was used to instantiate a bitmap object, and
1 line was used to write its contents to a PNG file. (The other three
lines of code are not related to the PNG writing itself, as they simply
create some colored pixel data to be written.)

It doesn't have to be more complicated than that.
 
J

Juha Nieminen

Richard said:
I disagree with your assertion that it takes reading significant
documentation before doing anything with them.

Well, you don't have to take my word for it. You can check the
documentation yourself:

http://www.libpng.org/pub/png/libpng-manual.txt

Estimate how much of it you have to read before you are able to create
a PNG file.

The code itself which is needed to use libpng to create a PNG file is
also quite lengthy. At the very minimum we are talking about something
like 50 lines of code.

Most people don't want to write 50 lines of code just to write a bitmap
to a PNG file. They want to write 1 line of code.
 
R

Richard

[Please do not mail me a copy of your followup]

Juha Nieminen <[email protected]> spake the secret code
Well, you don't have to take my word for it. You can check the
documentation yourself:

Been there, done that. As I said, I've used both libraries for over a
decade now.

My only conclusion is that you think anything longer than a couple
80x24 screenfuls is "significant documentation".
 
R

Robert Hairgrove

hamishd said:
Does anyone know of a good library (free or otherwise) for creating
PNG or JPEG images?

I have worked with the following libraries, and they are very easy to
use. Both are cross-platform, open source and provide a C++ API.
ImageMagick can also be run from the command line and scripted:

http://www.imagemagick.org
http://www.paintlib.de

The Qt libraries also provide built-in support for reading, writing and
transforming a wide variety of image formats on different OS platforms:
http://qt.nokia.com
 
R

Richard

[Please do not mail me a copy of your followup]

Stuart Golodetz <[email protected]> spake the secret code
[...] (the fact that
you sat down and figured it out a decade ago is commendable, but it
doesn't change the basic point - that you shouldn't have *had* to really
figure it out).

Spending a few minutes reading a text file is hardly anything commendable.

Really, if that is the sort of thing you find too difficult or
challenging, I suggest you investigate other career paths.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top