-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.cpp
92 lines (75 loc) · 2.44 KB
/
convert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "convert.hpp"
#include "logging.hpp"
#include <Magick++.h>
#include <iostream>
#include <sstream>
using transmogrifier::error;
void
transmogrifier::streamToPixelMap(const std::istream& inputStream, std::ostream& ppmStream)
{
std::stringstream buffer;
buffer << inputStream.rdbuf();
std::string str = buffer.str();
Magick::Blob inputBlob( (void*) str.c_str(), str.length() );
Magick::Image image ( inputBlob );
// Setting quality to 0 ensures that the formatting is not compressed to binary.
// Instead, the file is converted to an ASCII format.
// See PNM section: http://www.graphicsmagick.org/formats.html
// Also, see: http://www.graphicsmagick.org/formats.html#quality
image.quality(0);
Magick::Blob outputBlob;
// Writing PPM stream
image.magick( "PPM" );
image.write( &outputBlob );
ppmStream << (char*) outputBlob.data();
}
void
transmogrifier::namedFileToPixelMap(const std::string& inputImgName, std::ostream& ppmStream)
{
Magick::Image image;
try {
image.read(inputImgName);
}
catch( Magick::ErrorFileOpen& e) {
error() << inputImgName << ": image file cannot be opened" << std::endl;
std::exit(1);
}
catch( Magick::ErrorCorruptImage& e ) {
error() << inputImgName << ": image file corrupt" << std::endl;
std::exit(1);
}
// Setting quality to 0 ensures that the formatting is not compressed to binary.
// Instead, the file is converted to an ASCII format.
// See PNM section: http://www.graphicsmagick.org/formats.html
// Also, see: http://www.graphicsmagick.org/formats.html#quality
image.quality(0);
Magick::Blob blob;
// Writing PPM stream
image.magick( "PPM" );
image.write( &blob );
ppmStream << (char*) blob.data();
}
void
transmogrifier::pixelMapToStream(const std::stringstream& ppmStream, std::ostream& outStream, const std::string& outputFormat)
{
std::string str = ppmStream.str();
Magick::Blob blob( (void*) str.c_str(), str.length());
Magick::Image image( blob );
image.magick( outputFormat );
try {
image.write( &blob );
}
catch (Magick::ErrorMissingDelegate& e) {
error() << "Output image format `" << outputFormat << "' is unsupported" << std::endl;
std::exit(1);
}
outStream.write((char*) blob.data(), blob.length());
}
void
transmogrifier::pixelMapToNamedFile(const std::stringstream& ppmStream, const std::string& outputImgName)
{
std::string str = ppmStream.str();
Magick::Blob blob( (void*) str.c_str(), str.length());
Magick::Image image( blob );
image.write(outputImgName);
}