I have the following code:
#include <Magick++.h>
#include <iostream>
#include <fstream>
using namespace Magick;
using namespace std;
unsigned short int version = 1;
unsigned short int cols, rows;
short int myCol, myRow;
int main(int argc, char **argv) {
ofstream myFile;
myFile.open("img001.bin", ios::out | ios::trunc | ios::binary);
myFile.write(reinterpret_cast<const char*> (&version), sizeof(version));
try {
InitializeMagick(*argv);
Image img("noname-th.jpg");
cols = img.columns();
rows = img.rows();
myFile.write(reinterpret_cast <const char*> (&cols), sizeof(cols));
myFile.write(reinterpret_cast <const char*> (&rows), sizeof(rows));
for (myCol = cols - 1; myCol >= 0; myCol--) {
for (myRow = rows - 1; myRow >= 0; myRow--) {
ColorRGB rgb(img.pixelColor(myCol, myRow));
cout << "red: " << (rgb.red() * 255);
cout << ", green: " << (rgb.green() * 255);
cout << ", blue: " << (rgb.blue() * 255) << endl;
}
}
}
catch ( Magick::Exception & error) {
cerr << "Caught Magick++ exception: " << error.what() << endl;
}
return 0;
}
This works great in that it spits out the values for me to see (and make sure it's doing the right thing.) Note: yes, I am aware that it's starting at the lower right corner and reading down to 0,0. That's the intention here.
What I need now is figure out how to write those rgb values to the open file. For each pixel, I get the values from rgb.red() * 255, rgb.green() * 255, and rgb.blue() * 255. How do I store those as r,g,b (one pixel per line) in the file?
I think I need to do the same char casting on them, but I don't know how to get them concatenated together on a single line. Each file.write() automatically add an LF at the end.
Thanks.
main(). Usually global variables are frowned upon, unless you have a specific reason for using them.endlto a\nor what ever newline you want to go with.endlflushes, which is not good in such a large loop. You can flush the stream after the loop usingflush. just a tip.