How can I read/load images in C++? - c++

i am more a java developer and there is a standard way of reading images :
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.png"));
} catch (IOException e) {
}
but what is the c++ way of loading images? I want to load all images in a specific directory into an array or so.

Personally, I prefer the ImageMagick library.
There are many available graphics processing libraries, and there is not a single choice that stands out as clearly superior to the others. My advice is to make a short list of 3 or 4, take a look at the documentation for each, and try to write a simple half-page program with each. Use whichever one you personally find easiest to use.

There is no standard "way" in C++ to load images or files of any other sort. That feature is provided by (usually third-party) libraries.
On Windows, you can use the GDI or DirectX APIs to load images to memory.
You can also use any of many different libraries. Some that come to mind:
SDL-Image
ImageMagick
Qt's QImageReader
wxWidgets
cImg (Which will try to read files if the appropriate filetype-specific library is available.)
Boost.GIL (Which, apparently, has support for JPEG, PNG, and TIFF files.)
There are many, many others to look at, and some may be more appropriate than others depending on what you're trying to do.
For example, if you're only going to be working with JPEG files, then you'll want to use libIJG. Or if you'll only be using PNG, you might find libPNG or cairo to be more appropriate.

The library you will want to use to load images will depend on what you intend to do with it. If you are using a framework such as QT or wxWidgets, it will provide image loading routines.
Another possibility is to use the the SDL Image library, and to work on SDL surfaces, which will allow you to work down to the pixel level if you need.

Take a look at DevIL

Qt has good support for images, and is free and cross-platform.
Check out the qimage class

I would say that the closest you'll get to a standard way of doing this is with the Boost/Adobe Generic Image Library.

Related

How can I pass TIFF image data to JUCE (which does not support TIFF)?

I am using learning gui programming using c++ JUCE library. That library have supports for image file format(png, jpg). But I wants to learn how can I use other file format for example tiff.
After google I got libtiff.
My question is what will be the accurate approach for displaying this. Should I need to convert .tiff file into jpeg/png from tiff for doing this.
But I think this will require double processing.
Can anyone explain the raw/native/basic image file format so that I need to convert all images into that type and use it directly.
As I find something in winAPI for dealing with images in which they use image data from file format.
It will be very helpful if someone can let me know the approach for handling images data and displaying it.
Can anyone explain the raw/native/basic image file format so that I need to convert all images into that type and use it directly.
There is no "native" image file format, but RGB comes close (especially if you strip the headers to give just a Width×Height×Channels array of pixel values). You probably wouldn't want to use this for storing everything though as your buffers will be very large. Let your libraries handle storage.
It will be very helpful if someone can let me know the approach for handling images data and displaying it.
There is no "the" approach. C++ itself doesn't say anything about images, and there are loads of ways you can go about working with them. Your design will depend on your functional requirements specification and on what libraries you have available.
I am using learning gui programming using c++ JUCE library. That
library have supports for image file format(png, jpg). But I wants to
learn how can I use other file format for example tiff.
After google I got libtiff.
My question is what will be the accurate approach for displaying this.
Should I need to convert .tiff file into jpeg/png from tiff for doing
this.
But I think this will require double processing.
If you mean using libtiff to convert TIFF-format images to formats that JUCE supports, you're right in saying that this introduces an extra initial processing step. However, as far as you've said, it sounds like any possible performance hit through this will be vastly, wildly and hugely outweighed by the benefit of simplicity and clarity. So I'd just do that.
In order to do something like read *.tiff images and using them in an application build with the JUCE framework, I would suggest to create a new class derived from the base interface ImageFileFormat.
class MyTiffFormat : public ImageFileFormat
{
private:
MyTiffFormat( const MyTiffFormat& );
MyTiffFormat& operator=( const MyTiffFormat& );
public:
MyTiffFormat();
~MyTiffformat();
const String getFormatName();
bool canUnderStand();
Image decodeImage( InputStream& input );
bool writeImageToStream( const Image& source, OuptputStream& dest );
};
Implementing the function "Image decodeImage( InputSTeram& input )" is the point were you need something like libtiff. In the JUCE source tree you will find the implementation for PNG and the other supported formats in the folder: \juce\src\gui\graphics\imaging
More information on extending JUCE features can be found in the JUCE user forum.
Juce works great with pngs, jpgs, and gifs (not animated), and they can be read from file, or even "compiled" with the BinaryBuilder.
For example to load it from compiled c++ with BinaryBuilder.
someImage = ImageFileFormat::loadFrom (AppResources::image_png, AppResources::image_pngSize);
Check out the doxygen docs, they are quite helpful. to compile your images with BinaryBuilder the syntax is:
./BinaryBuilder someFolder otherFolder ClassName

Get dimensions of JPEG in C++

I need to get the image dimensions of a JPEG in C++. I'm looking for either a fairly simple way to do it or a smallish library that provides that functionality. I'm working in C++ on OpenVMS, so any external libraries may have to be adapted to compile on our systems - so please don't post me links to big, closed source libraries!
Has anyone come across anything that might do the trick, or understand the JPEG file format (I think I probably mean the JFIF file format here) to tell me how I might go about rolling my own solution?
You have this C function which may extract the relevant data for you.
This is a C routine but should compile fine with C++.
Pass it a normal FILE pointer (from fopen) to the beginning of a jpeg file and two int pointers to be set with the image height and width.
Or you may find in the Boost library a jpeg class which has the right function (From Adobe Generic Image Library).
jpeg_read_dimensions
boost::gil::jpeg_read_dimensions (const char *filename)
Returns the width and height of the JPEG file at the specified location. Throws std::ios_base::failure if the location does not correspond to a valid JPEG file.
libjpeg is reasonably small, open source and available on OpenVMS. It's probably quicker to install it than to handle JPEG yourself.
Maybe libjpeg?
You should be able to use this jpeg lib with this patch for OpenVMS
No need for full libjpeg library just to get this information (unless you need to do something else with the images). ImageInfo might help you. It is a Java class, but there are ports for other languages, including C++.
As pointed out, Exif might change these information (eg. with orientation setting).
You may want to try GDAL library which serves as an abstraction layer for large number of raster data formats, mostly used in geospatial applications for GIS/RS.
GDAL provides number of APIs, for C, C++ and scripting languages. Of course, it supports JPEG images and its variants like JPEG2000 and more.
Here is a very simple example how to open JPEG image and query its dimensions:
#include <gdal_priv.h>
GDALAllRegister(); // call ones in your application
GDALDataset* ds = (GDALDataset*)GDALOpen("my.jpeg", GA_ReadOnly);
int width = ds->GetRasterXSize();
int height = ds->GetRasterYSize(),
int nbands = ds->GetRasterCount();
Check GDAL API tutorial for more complete example.

Decode JPEG to obtain uncompressed data

I want to decode JPEG files and obtain uncompressed decoded output in BMP/RGB format.I am using GNU/Linux, and C/C++.
I had a look at libjpeg, but there seemed not to be any good documentation available.
So my questions are:
Where is documentation on libjpeg?
Can you suggest other C-based jpeg-decompression libraries?
The documentation for libjpeg comes with the source-code. Since you haven't found it yet:
Download the source-code archive and open the file libjpeg.doc. It's a plain ASCII file, not a word document, so better open it in notepad or another ASCII editor.
There are some other .doc files as well. Most of them aren't that interesting though.
Unfortunately I cannot recommend any other library besides libjpeg. I tried a couple of alternatives, but Libjpeg always won. Is pretty easy to work with once you have the basics done. Also it's the most complete and most stable jpeg library out there.
MagickWand is the C API for ImageMagick:
http://imagemagick.org/script/magick-wand.php
I have not used it, but the documentation looks quite extensive.
You should check out Qt's QImage. It has a pretty easy interface that makes this task really easy. Setup is pretty simple for every platform.
If Qt is overkill, you can try Magick++ http://www.imagemagick.org/Magick++/. It supports similar operations and is also well suited for that sort of task. The last time I used it, I struggled a bit with dependencies for it on Windows, but don't recall much trouble on Linux.
For Magick++'s Image class, the function you probably want is getConstPixels.
I have code that you can copy ( or just use as a reference ) for loading a jpeg image using the libjpeg library.
You can browse the code here: http://code.google.com/p/kgui/source/browse/trunk/kguiimage.cpp
Just look for the function LoadJPGImage.
The code is setup to handle c++ binding of my DataHandle class to it for loading the image, that way the image can be a file or data already in memory or whatever.
A slightly out of the box solution is to acquire a copy of the netpbm tools, which transform images from pretty much any format to any other format via one of several very simple intermediate formats. They work well from the shell, and are most often used in pipes to read some arbitrary image, perform an operation on it, and write it out to some other format.
The pbm formats can be as simple as a plain ASCII header followed by the RGB data in ASCII or binary. They are intended to be simple enough to use without required a library to implement.
JPEG is supported in netpbm by read and write filters that are implemented on top of libjpeg.

Saving an array of colour data as a PNG file on DS

I'm looking for a library to save an array of colour data to a PNG file. (That's all there is to it, right? I know very little about the internals of a PNG.)
This is for use in Nintendo DS development, so something lightweight is preferable. I don't need any other fancy features like rotation, etc.
To encode any kind of PNG file, libpng is the way of the walk.
However, on small devices like the DS you really want to store your image data in the format which the display hardware expects. It is technically possible to get libpng working on the platform, but it will add significant overhead, both in terms of loadtimes and footprint.
Have you looked at libpng? http://www.libpng.org/pub/png/libpng.html
I'm not sure whether the memory footprint will be acceptable, but you should probably be aware that PNG files are a lot more involved than just an array of colors. Performance is likely to be a concern on a DS.
If you go with libpng, you'll also need zlib, and if you're using DevKitPro, you'll probably run into some missing functions (from playing with the code for 5 minutes, it looks like it relies on pow() which doesn't seem to be in libnds.) I have no idea what the official Nintendo SDK offers in the way of a standard library - you might be in better shape if that's what you're using.
I managed to find a library that supports PNG (using libpng) and allows you to just give it raw image data.
It's called LibPicture. It's a bit hefty though: ~1MB.

C++ libraries to manipulate images

Do you know any open source/free software C++ libraries to manipulate images in these formats:
.jpg .gif .png .bmp ? The more formats it supports, the better. I am implementing a free program in C++ which hides a text file into one or more images, using steganography.
I am working under Unix.
ImageMagick can manipulate about anything and has interfaces for a dozen of languages, including the Magick++ API for C++.
#lurks: I assume that you are looking for LSB shifting? I did some stego work a couple of years ago, and that's how it appeared most apps worked. It appears that ImageMagick (suggested by others) allows you to identify and manipulate the LSBs.
It takes some setting up, but I'm a fan of Adobe's GIL (now part of Boost).
Have you considered GDI?
-- Kevin Fairchild
FreeImage is pretty solid. It has a C interface but is more C++-like in its implementation.
For .png images you could look into Cairo (and CairoMM). There's also Anti-Grain which people consider very fast.
I like vxl
VXL (the Vision-something-Libraries) is a collection of C++ libraries designed for computer vision research and implementation. It was created from TargetJr and the IUE with the aim of making a light, fast and consistent system. VXL is written in ANSI/ISO C++ and is designed to be portable over many platforms.