CxImage is a cross-platform C++ library for image processing, supporting formats like JPEG, PNG, BMP, GIF, TIFF, and more. It provides a lightweight, object-oriented API for loading, manipulating, and saving images, making it suitable for Linux-based development (including Ubuntu). The library is open-source and integrates with GCC, making it a viable choice for Ubuntu developers.
To use CxImage on Ubuntu, you need to install the precompiled development package (recommended for simplicity) or compile from source.
The easiest way to install CxImage is via apt:
sudo apt update
sudo apt install libcximage-dev
This installs the library headers (/usr/include/cximage) and shared library (/usr/lib/x86_64-linux-gnu/libcximage.so), ready for linking.
If you need the latest version or custom features, compile from source:
git clone https://github.com/antaresware/cximage.git
cd cximage
sudo apt install build-essential libz-dev libpng-dev libjpeg-dev
configure to generate Makefiles, then compile and install:./configure --prefix=/usr/local # Optional: Specify installation directory
make
sudo make install
~/.bashrc):export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
source ~/.bashrc
This step ensures the compiler finds CxImage’s headers and libraries.
CxImage’s API is intuitive for common tasks like loading, saving, and converting images. Below are key examples:
The simplest way to use CxImage is to load an image from disk, check its validity, and save it in another format:
#include <cximage.h>
#include <iostream>
int main() {
CxImage image;
if (image.Load("input.jpg", CXIMAGE_FORMAT_JPG)) { // Load JPEG
std::cout << "Image loaded: " << image.GetWidth() << "x" << image.GetHeight() << std::endl;
if (!image.Save("output.png", CXIMAGE_FORMAT_PNG)) { // Save as PNG
std::cerr << "Failed to save image!" << std::endl;
return 1;
}
} else {
std::cerr << "Failed to load image!" << std::endl;
return 1;
}
return 0;
}
Compile with:
g++ main.cpp -o image_converter -lcximage
Run:
./image_converter
This converts input.jpg to output.png.
CxImage supports direct format conversion without intermediate files. For example, converting a JPEG to TIFF:
#include <cximage.h>
int main() {
CxImage image;
if (image.Load("input.jpg", CXIMAGE_FORMAT_JPG)) {
if (!image.Save("output.tif", CXIMAGE_FORMAT_TIF)) {
std::cerr << "Failed to convert to TIFF!" << std::endl;
return 1;
}
} else {
std::cerr << "Failed to load JPEG!" << std::endl;
return 1;
}
return 0;
}
Compile and run as above. This leverages CxImage’s internal encoding/decoding logic for efficient format conversion.
You can retrieve metadata (width, height, format) and manipulate pixel data:
#include <cximage.h>
#include <iostream>
int main() {
CxImage image;
if (image.Load("input.png", CXIMAGE_FORMAT_PNG)) {
std::cout << "Dimensions: " << image.GetWidth() << "x" << image.GetHeight() << std::endl;
std::cout << "Format: " << image.GetFormatName() << std::endl;
// Access raw pixel data (BGRA format)
BYTE* pixels = image.GetBits();
int bpp = image.GetBpp(); // Bits per pixel
std::cout << "Bits per pixel: " << bpp << std::endl;
} else {
std::cerr << "Failed to load image!" << std::endl;
return 1;
}
return 0;
}
This prints the image’s dimensions, format, and bits per pixel (useful for advanced processing).
CxImage provides a rich set of functions for image manipulation. Below are frequently used operations:
Use Resample() to change the image size (supports bilinear/trilinear filtering):
image.Resample(800, 600, FILTER_BILINEAR); // Resize to 800x600 with bilinear filtering
Rotate by 90°, 180°, or 270° using Rotate():
image.Rotate(90); // Rotate 90 degrees clockwise
Use IncreaseBrightness()/DecreaseBrightness() and IncreaseContrast()/DecreaseContrast():
image.IncreaseBrightness(20); // Increase brightness by 20
image.DecreaseContrast(10); // Decrease contrast by 10
Convert a color image to grayscale using GrayScale():
image.GrayScale(); // Convert to grayscale
These operations modify the image in-place. Always check IsValid() after loading to ensure the image is usable.
If you encounter linker errors (e.g., undefined reference to 'CxImage::Load'), ensure you linked the library correctly:
g++ main.cpp -o app -lcximage # Correct
g++ main.cpp -o app # Incorrect (missing -lcximage)
If configure fails, ensure all dependencies are installed. For example, missing libz-dev causes errors with TIFF/PNG support:
sudo apt install libz-dev # Fix missing zlib
If the program fails to load an image, verify the file path (use absolute paths for testing) and format (CxImage only supports supported formats). Use CxImage::IsValid() to check if loading succeeded.
samples directory in the CxImage source code contains practical examples for various operations.By following this guide, you can efficiently develop image processing applications on Ubuntu using the CxImage library.