Magick++
| Language | C++ |
|---|---|
| License | ImageMagick License (Apache 2.0 style, permissive) |
| Repository | github.com/ImageMagick/ImageMagick (Magick++ is in the Magick++ folder) |
| Documentation | Magick++ API |
| Depends on | ImageMagick (MagickCore) |
| Read / write | yes / yes, all TGA image types, raw or RLE |
Magick++ is the object-oriented C++ interface of ImageMagick. ImageMagick’s own TGA coder reads palette, grayscale and true-color files (raw and RLE, all bit depths), honors the origin bit, and writes TGA with or without RLE. The same code reads and writes 200+ other formats, which makes it a good fit for converters and batch tools.
It’s a big dependency with many optional delegate libraries. For loading textures in a game, stb_image is far lighter.
Installation#
Shell
# Debian / Ubuntu
sudo apt install libmagick++-dev
# Fedora
sudo dnf install ImageMagick-c++-devel
# macOS
brew install imagemagick
Compile with the flags from Magick++-config:
Shell
c++ `Magick++-config --cxxflags --cppflags` -O2 -o tgaconv tgaconv.cpp `Magick++-config --ldflags --libs`
Basic usage: read a TGA and save it as PNG#
C++
#include <Magick++.h>
#include <iostream>
using namespace Magick;
int main(int argc, char **argv) {
MagickPlusPlusGenesis genesis(*argv); // initializes ImageMagick
Image image;
try {
image.read("in.tga");
std::cout << image.columns() << " x " << image.rows() << "\n";
image.write("out.png"); // format from the extension
} catch (Exception &e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}
How to convert PNG to TGA with RLE compression#
C++
Image image("in.png");
image.compressType(RLECompression); // NoCompression for raw pixels
image.write("out.tga");
How to get TGA width and height without decoding#
ping reads only the header:
C++
Image image;
image.ping("in.tga");
std::cout << image.columns() << " x " << image.rows()
<< ", alpha: " << (image.alpha() ? "yes" : "no") << "\n";
How to read a file without a .tga extension#
TGA has no magic number, so ImageMagick can’t detect it from content. Prefix the file name with the format:
C++
Image image;
image.read("TGA:texture.bin");
How to write TGA to memory#
C++
Blob blob;
image.magick("TGA");
image.write(&blob);
// blob.data(), blob.length()
Tips#
- The command-line equivalent is
magick in.png -compress RLE out.tga. - On Windows, the ImageMagick installer has an option to install the C/C++ headers and libraries.