-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtweaky.cpp
82 lines (72 loc) · 2.41 KB
/
tweaky.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
#include <Magick++.h>
#include <iostream>
#include <sstream>
#include <cassert>
using std::cout;
using std::string;
using std::endl;
using namespace Magick;
int main(int argc, char** argv)
{
InitializeMagick(*argv);
int numBits = argc > 1? atoi(argv[1]) : 0;
if(argc != 4 || numBits < 1 || numBits > 7) {
cout << "Usage: " << argv[0] << " N input.png out\n"
<< "where N is the number of bits (1-7) to tweak.\n"
<< "Output files will be named out-0s.png, out-1s.png, out-rand.png\n";
exit(1);
}
const string samplePath = argv[2];
const string outputPath = argv[3];
cout << "Loading " << samplePath << '\n';
Image im (samplePath);
Geometry g = im.size();
cout << "Dimensions are " << (string)g << '\n';
cout << "Depth is " << im.depth() << '\n';
assert(im.type() == TrueColorType);
Quantum mask = (1 << numBits) - 1;
#if QuantumDepth == 16
mask = (mask << 8) | mask;
#endif
// Zero out the lowest N bits
for(unsigned x = 0; x < g.width(); x++) {
for(unsigned y = 0; y < g.height(); y++) {
Color c = im.pixelColor(x, y);
c.redQuantum(c.redQuantum() & ~mask);
c.greenQuantum(c.greenQuantum() & ~mask);
c.blueQuantum(c.blueQuantum() & ~mask);
im.pixelColor(x, y, c);
}
}
cout << "Writing " << outputPath << "-0s.png" << endl;
im.write(outputPath + "-0s.png");
// Replace lowest N bits with ones
for(unsigned x = 0; x < g.width(); x++) {
for(unsigned y = 0; y < g.height(); y++) {
Color c = im.pixelColor(x, y);
c.redQuantum(c.redQuantum() | mask);
c.greenQuantum(c.greenQuantum() | mask);
c.blueQuantum(c.blueQuantum() | mask);
im.pixelColor(x, y, c);
}
}
cout << "Writing " << outputPath << "-1s.png" << endl;
im.write(outputPath + "-1s.png");
// Replace lowest N bits with random
srand(time(NULL));
for(unsigned x = 0; x < g.width(); x++) {
for(unsigned y = 0; y < g.height(); y++) {
Color c = im.pixelColor(x, y);
int r = rand();
c.redQuantum((c.redQuantum() & ~mask) | (r & mask));
r >>= 8;
c.greenQuantum((c.greenQuantum() & ~mask) | (r & mask));
r >>= 8;
c.blueQuantum((c.blueQuantum() & ~mask) | (r & mask));
im.pixelColor(x, y, c);
}
}
cout << "Writing " << outputPath << "-rand.png" << endl;
im.write(outputPath + "-rand.png");
return 0;
}