Ubuntu CxImage开发文档
Ubuntu CxImage Development Documentation
1. Introduction to CxImage
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.
2. Installation on Ubuntu
To use CxImage on Ubuntu, you need to install the precompiled development package (recommended for simplicity) or compile from source.
2.1 Install Precompiled Package
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.
2.2 Compile from Source (Optional)
If you need the latest version or custom features, compile from source:
- Download Source: Clone the GitHub repository or download the tarball:
git clone https://github.com/antaresware/cximage.git cd cximage
- Install Dependencies: Ensure you have GCC, make, and required libraries (zlib, libpng, libjpeg):
sudo apt install build-essential libz-dev libpng-dev libjpeg-dev
- Configure and Build: Run
configure
to generate Makefiles, then compile and install:./configure --prefix=/usr/local # Optional: Specify installation directory make sudo make install
- Update Environment Variables: Add the installation paths to your shell configuration (e.g.,
~/.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.
3. Basic Usage Examples
CxImage’s API is intuitive for common tasks like loading, saving, and converting images. Below are key examples:
3.1 Load and Save an Image
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
.
3.2 Convert Between Formats
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.
3.3 Access Image Properties
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).
4. Common Operations
CxImage provides a rich set of functions for image manipulation. Below are frequently used operations:
4.1 Resize an Image
Use Resample()
to change the image size (supports bilinear/trilinear filtering):
image.Resample(800, 600, FILTER_BILINEAR);
// Resize to 800x600 with bilinear filtering
4.2 Rotate an Image
Rotate by 90°, 180°, or 270° using Rotate()
:
image.Rotate(90);
// Rotate 90 degrees clockwise
4.3 Adjust Brightness/Contrast
Use IncreaseBrightness()
/DecreaseBrightness()
and IncreaseContrast()
/DecreaseContrast()
:
image.IncreaseBrightness(20);
// Increase brightness by 20
image.DecreaseContrast(10);
// Decrease contrast by 10
4.4 Convert to Grayscale
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.
5. Troubleshooting
5.1 Missing Dependencies
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)
5.2 Compilation Errors
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
5.3 Runtime Errors
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.
6. Additional Resources
- Official Documentation: The CxImage GitHub wiki provides detailed API references and platform-specific notes.
- Community Forums: Search for Ubuntu-specific issues on Stack Overflow or the CxImage mailing list.
- Sample Code: The
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.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu CxImage开发文档
本文地址: https://pptw.com/jishu/733247.html