forked from matthewarcus/mmps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.cpp
105 lines (94 loc) · 2.58 KB
/
combine.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
93
94
95
96
97
98
99
100
101
102
103
104
105
// $Revision: 1.2 $
// combine.cpp
// (C) 2004 by Matthew Arcus
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#include "constants.h"
#include "image.h"
const char* usage =
"Usage: %s file1 file1";
template <class T>
inline T max (T i, T j) {
if (i < j) return j;
else return i;
}
int main(int argc, char* argv[])
{
Image image1;
Image image2;
const char* file1;
const char* file2;
const char* outfilename = NULL;
Rgb background = black;
int width;
int height;
double fuzz = 2.0;
char *progname = argv[0];
argc--;argv++;
OptSpec *optspecs [] = {
// General options
new RgbSpec ("-bg", background),
new DoubleSpec ("-fuzz", fuzz),
new StringSpec ("-out", outfilename),
NULL // Finish up
};
while (argc > 0) {
int result = OptSpec::ReadOpts(argc, argv, optspecs);
if (result != 1) {
break;
}
argc--; argv++;
}
if (argc < 2) {
error(usage, progname);
}
file1 = argv[0];
file2 = argv[1];
image1.Read(file1);
image2.Read(file2);
width = image1.Width();
height = image1.Height();
if (image2.Width() != width || image2.Height() != height) {
error("Images must have same dimensions");
}
Image outImage(width, height);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
Rgb rgb1;
Rgb rgb2;
Rgb out;
image1.GetRgb(col,row,rgb1);
image2.GetRgb(col,row,rgb2);
if (rgb1.Match(background,fuzz)) {
out = rgb2;
} else if (rgb2.Match(background, fuzz)) {
out = rgb1;
} else {
#if 0
// Average the rgb values, this produces alarming discontinuities
out.r = (rgb1.r + rgb2.r)/2;
out.g = (rgb1.g + rgb2.g)/2;
out.b = (rgb1.b + rgb2.b)/2;
#endif
// But taking the maximum of the rgb values is smooth
out.r = max(rgb1.r,rgb2.r);
out.g = max(rgb1.g,rgb2.g);
out.b = max(rgb1.b,rgb2.b);
}
outImage.SetRgb(col,row,out);
}
}
outImage.Write(outfilename);
return 0;
}