Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- // using std::ofstream, std::ifstream, std::streampos
- void write(const char* fname, const SomeType& processedImage)
- {
- std::ofstream file(fname);
- file.exceptions(std::ofstream::badbit);
- const unsigned short cols = image.columns(), rows = image.rows();
- file.write(reinterpret_cast<const char*>(&cols), sizeof(cols));
- for(int col = 0; col != cols; ++col) {
- for(int row = 0; row != rows; ++row) {
- const Magick::ColorRGB rgb = processedImage.pixelColor(col, row);
- const unsigned char bytes[] = {
- rgb.red() * 255, rgb.green() * 255, rgb.blue() * 255
- };
- file.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
- }
- }
- }
- void read(const char* fname, SomeType& imageOutput)
- {
- std::ifstream file(fname);
- file.exceptions(std::ifstream::badbit | std::ifstream::failbit);
- unsigned short cols;
- file.read(reinterpret_cast<char*>(&cols), sizeof(cols));
- const std::streampos pos = file.tellg();
- file.seekg(0, std::ifstream::end);
- const std::streampos end = file.tellg();
- const unsigned short rows = (end - pos) / (cols * 3);
- file.seekg(pos, std::ifstream::beg);
- imageOutput.setCols(cols);
- imageOutput.setRows(rows);
- for(int col = 0; col != cols; ++col) {
- for(int row = 0; row != rows; ++row) {
- unsigned char bytes[3];
- file.read(reinterpret_cast<char*>(&bytes), sizeof(bytes));
- const Magick::ColorRGB rgb(bytes[0] / 255., bytes[1] / 255., bytes[2] / 255.);
- imageOutput.setPixelColor(col, row, rgb);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement