PlotTools.hh
3.26 KB
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
/*
* PlotTools.hh
*
* Created on: 15 nov. 2013
* Author: guillaume
*/
#ifndef PLOTTOOLS_HH_
#define PLOTTOOLS_HH_
#include <string>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
//#include <boost/gil/extension/io/png_dynamic_io.hpp>
class PlotTools {
public:
/**
* @brief Convert SVG, PDF or PS file format to PNG format.
* @return Return converted filename which contain input filename without input format( result.svg => result.png).
*/
static std::string convert2Png(const std::string pInputFile) {
// Get input file format
size_t pos = pInputFile.find_last_of(".");
std::string lInputFormat = pInputFile.substr(pos + 1);
// Copy input filename and replace format to "png".
std::string lOutputFile = pInputFile;
std::string lOutputFormat = "png";
lOutputFile.replace(pos + 1, lOutputFormat.size(), lOutputFormat);
if (lInputFormat == "svg") {
// rsvg can only convert svg format to png.
std::system(std::string("rsvg-convert " + pInputFile + " -f png -o " + lOutputFile).c_str());
} else {
// Ghostscript can only convert ps and pdf format to png.
std::system(std::string("gs -r300x300 -sDEVICE=pngalpha -o " + lOutputFile + " " + pInputFile).c_str());
}
return lOutputFile;
}
/**
* @brief Calculate error rate between reference file and result file.
* @note Files must be in PNG format and using RGB 8-bit color.
* @return Return error rate in percent (it corresponds to proportion of wrong pixel).
*/
/*static double calculateErrorRate(std::string pReferenceFile, std::string pResultFile) {
// Check if file exist
if ( (access(pReferenceFile.c_str(), F_OK) == 0) && (access(pResultFile.c_str(), F_OK) == 0) ) {
// Read file
using namespace boost::gil;
rgba8_image_t imRef;
point2<std::ptrdiff_t> lPointRef = png_read_dimensions(pReferenceFile);
png_read_image(pReferenceFile, imRef);
rgba8_pixel_t pixelRef;
rgba8_image_t imRes;
point2<std::ptrdiff_t> lPointRes = png_read_dimensions(pResultFile);
png_read_image(pResultFile, imRes);
rgba8_pixel_t pixelRes;
// Calculate error rate.
int invPixel = 0;
for (int x = 0; x != lPointRef.x; x++) {
for (int y = 0; y != lPointRef.y; y++) {
pixelRef = view(imRef)(x, y);
pixelRes = view(imRes)(x, y);
// TODO error rate can be improved by saying which element is wrong (Red, Green or blue)
// and not pixel.
// TODO error rate can also be improved by not taking into account pixel change from [255, 255, 255] to [0, 0, 0] and vice versa.
if( ((int)pixelRef[0] != (int)pixelRes[0]) || ((int)pixelRef[1] != (int)pixelRes[1]) || ((int)pixelRef[2] != (int)pixelRes[2]) ) {
invPixel ++;
//std::cout<<"pixel {"<<x<<", "<<y<<"}: "<<std::endl;
//std::cout<<"red = "<<(int)pixelRef[0]<<" green = "<<(int)pixelRef[1]<<" blue = "<<(int)pixelRef[2]<<std::endl;
//std::cout<<"red = "<<(int)pixelRes[0]<<" green = "<<(int)pixelRes[1]<<" blue = "<<(int)pixelRes[2]<<std::endl;
}
}
}
std::cout<<invPixel<<" / "<<lPointRef.x * lPointRef.y<<" invalid pixel {"<<(invPixel / (float)(lPointRef.x * lPointRef.y)) * 100<<"%}"<<std::endl;
return (invPixel / (float)(lPointRef.x * lPointRef.y)) * 100;
} else {
return nan(std::string("").c_str());
}
}*/
};
#endif /* PLOTTOOLS_HH_ */