-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_pgm_diff.cpp
74 lines (59 loc) · 2.29 KB
/
get_pgm_diff.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
/*
* Simpledjvu-0.1
* Based on djvulibre (http://djvu.sourceforge.net/)
* Copyright 2012, Mikhail Dektyarev <[email protected]>
*
* This file is part of Simpledjvu.
*
* Simpledjvu is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Simpledjvu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Simpledjvu. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <djvulibre.h>
#include <iostream>
#include <cmath>
#include <vector>
using std::vector;
vector<vector<int> > get_image_diff(const GBitmap &image1, const GBitmap &image2) {
vector<vector<int> > result(image1.columns(), vector<int> (image1.rows()));
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < result[i].size(); ++j) {
result[i][j] = static_cast<int>(image1[i][j]) - static_cast<int>(image2[i][j]);
}
}
return result;
}
double Lp_norm(const vector<vector<int> > &data, double p = 1.0) {
double result(0.0);
for (int i = 0; i < data.size(); ++i) {
for (int j = 0; j < data[i].size(); ++j) {
result += pow(fabs(data[i][j]), p);
}
}
return pow(result, 1.0 / p);
}
double Lp_diff(const GBitmap &image1, const GBitmap &image2, double p = 2.0) {
vector<vector<int> > diff = get_image_diff(image1, image2);
return Lp_norm(diff, p);
}
int main(int argc, char *argv[]) {
GP<GBitmap> gimage1 = GBitmap::create(*ByteStream::create(GURL::Filename::UTF8(argv[1]), "rb"));
GP<GBitmap> gimage2 = GBitmap::create(*ByteStream::create(GURL::Filename::UTF8(argv[2]), "rb"));
if (gimage1->columns() != gimage2->columns() || gimage1->rows() != gimage2->rows()) {
std::cerr << "Image sizes don't match\n";
return 1;
}
auto diff = get_image_diff(*gimage1, *gimage2);
std::cout << Lp_norm(diff, 1.0) << " " << Lp_norm(diff, 2.0) << '\n';
return 0;
}