Thursday, May 2, 2013

ImageMagick convert to raw RGB format in C++

This is a code snippet to convert from any supported ImageMagick format to raw RGB format, it's just an example on how to use the conversion features of ImageMagick libraries. The example was tested on both: Windows and Linux.

#include <Magick++.h>
#include <iostream>
#include <boost/filesystem.hpp>

using namespace std;
using namespace Magick;
using namespace boost::filesystem;

int main(int argc, char **argv) {
 Image img;
 string src_path;
 string tgt_path;
 Blob blob;
 FILE* tgt_file;

 InitializeMagick(*argv);

 // read image
 src_path = argv[1];
 if(exists(src_path)) {
  img.read(src_path);
  cout << "Detected format: " << img.format() << endl;

  // set raw RGBS output format & convert it into a Blob
  img.magick("RGB");
  img.write(&blob);

  // dump blob to disk
  tgt_path = src_path + ".RGB.raw";
  tgt_file = fopen(tgt_path.c_str(), "wb");
  fwrite(blob.data(), blob.length(), 1, tgt_file);
  fclose(tgt_file);
  cout << "Converted to raw RGB: " << tgt_path << endl;
  exit(0);
 } else {
  cout << "Could not load image, file not found " << src_path << endl;
  exit(1);
 }
}

1 comment:

  1. this post is much helpful for us. this is really very massive value to all the readers and it will be the only reason for the post to get popular with great authority.
    Email Marketing Chennai

    ReplyDelete