Image Processing & Conversion via Open Source C++ API
Read, Write & Convert Popular Image Formats Like PNG, JPEG, BMP, TIFF & many more via Free C++ Library.
OpenImageIO is a powerful image processing library that gives software developers the ability to read, write and process popular image file formats inside C++ applications. It provides support for several popular image formats through plugins. It provides support for popular image formats like OpenEXR, TIFF, JPEG/JFIF, PNG, BMP, JPEG-2000, ICO, PNM, DPX, IFF, Field3D, Ptex, Photoshop PSD, GIF & several more.
OpenImageIO library is widely used in animation and VFX studios all over the world and is also integrated into numerous commercial products. OpenImageIO library comes with several command-line image tools that demonstrate features like converting image formats to others, comparing two images, printing detailed information, searching images for match metadata, simple image viewer, and more.
Getting Started with OpenImageIO
You need to have C++11 and above for running the project smoothly.
OpenImageIO build system is based upon CMake. If need to install it on your system. After you build OpenImageIO, if you compiled with the EMBEDPLUGINS=0 flag you will need to set the environment variable OIIO_LIBRARY_PATH to point to the 'lib' directory where OpenImageIO is installed, or else it will not be able to find the plugins.
Install OpenImageIO via git command
git clone https://github.com/OpenImageIO/oiio.git
Reading and Writing images via C++ API
OpenImageIO library allows programmers to read & write images as well as control the way that later images will be externally produced in the output. Normally, all images read by the library are read into an ImageBuf backed by an underlying ImageCache, and are automatically converted to float pixels for inner storage. While writing images, it just outputs the current image to the named file. The image processing library does not remove the present image from the image stack; it simply saves a copy of it.
Advanced Image Reading via C++ API<
#include
using namespace OIIO;
...
const char *filename = "foo.jpg";
auto inp = ImageInput::open (filename);
if (! inp) {
std::cerr << "Could not open " << filename
<< ", error = " << OIIO::geterror() << "\n";
return;
}
const ImageSpec &spec = inp->spec();
int xres = spec.width;
int yres = spec.height;
int channels = spec.nchannels;
std::vector pixels(xres * yres * channels);
if (! inp->read_image (TypeDesc::UINT8, &pixels[0])) {
std::cerr << "Could not read pixels from " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}
if (! inp->close ()) {
std::cerr << "Error closing " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}
Converting Image to other Formats
OpenImageIO library provides features for converting images to other popular file formats. It is very easy to convert among supported image formats. The iconvert utility will read an image and then write the image to a new supported file format. It will simply infer the file format from the file extension. You can include an image caption, description, keywords, or metadata.
Change Image Metadata via C++
OpenImageIO API enables software developers to change current image metadata inside their own C++ applications via open source API. It provides commands that modify the metadata of the existing image, but do not change its pixel values. The good thing is that only the current image (image at the top of the stack) is affected, but not down the stack. You can also add or clear keywords, add caption or description, Removes any metadata, and more.
How to Set Image Metadata via C++
// spec["key"] = value sets the value of the metadata, using
// the type of value as a guide for the type of the metadata.
spec["Orientation"] = 1; // int
spec["PixelAspectRatio"] = 1.0f; // float
spec["ImageDescription"] = "selfie"; // string
spec["worldtocamera"] = Imath::M44f(...) // matrix
Draw Shapes or Text over Images via C++ API
It is always very useful to label your images with useful titles or watermark the image for to protect them from unauthorized usage. The open source OpenImageIO library has included several important features for drawing shapes, points, lines, boxes or text over the images with ease. You can draw shapes over your image or add text to it with just a couple of lines of code. The following code examples shows how to draw text over an image and align it.
How to Draw Text Over Image via C++ API
ImageBufAlgo::render_text (ImgA, 50, 100, "Hello, world");
float red[] = { 1, 0, 0, 1 };
ImageBufAlgo::render_text (ImgA, 100, 200, "Go Big Red!",
60, "Arial Bold", red);
float white[] = { 1, 1, 1, 1 };
ImageBufAlgo::render_text (ImgB, 320, 240, "Centered",
60, "Arial Bold", white,
TextAlignX::Center, TextAlignY::Center);