// 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 3 of the License, or
// (at your option) any later version.
//
// This program 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 this program. If not, see .
// Applies a dither pattern to an image.
// The dither pattern is another image, in which each
// pixel is treated as a threshold.
//
// The thresholds are scaled from the usual range (0.0 to 1.0)
// to 8-bit bytes, (0 to 255).
//
// The three (r g b) components of each pixel in the
// dither pattern are averaged to obtain the threshold
// (this is a simplistic color-to-gray transformation).
//
// Author: Alejo Hausner (ahausner@truehaus.net) 2008.
#ifdef WIN32
#include
#endif
typedef unsigned char byte;
#include
#include
#include
#include "Colour.h"
#include "Image.h"
FILE *dbgfp = fopen("debug.dat","w");
void doDither(Image& in, Image& th, Image& out) {
int w = in.getWidth();
int h = in.getHeight();
byte **inBuf = in.getBuf();
int thW = th.getWidth();
int thH = th.getHeight();
byte **thBuf = th.getBuf();
out.ensureSpace(w,h);
byte **oBuf = out.getBuf();
for (int row=0; row thresh) {
*oP++ = 255;
*oP++ = 255;
*oP++ = 255;
*oP++ = 255;
}
else {
*oP++ = 0;
*oP++ = 0;
*oP++ = 0;
*oP++ = 255;
}
}
}
}
int main(int argc, char* argv[]) {
if (argc != 4) {
cerr << "Usage:\n";
cerr << " dither input.ppm dither-image.ppm output.ppm\n";
exit(1);
}
Image inImg;
inImg.readFile(argv[1]);
Image threshImg;
threshImg.readFile(argv[2]);
Image outImg;
doDither(inImg, threshImg, outImg);
outImg.writeFile(argv[3]);
}