// halfsize.cpp // // Scale down an image by a factor of 2 (i.e., to a quarter of its // original area) by averaging 4 pixels into one. For images with odd // width/height, the last column/row of the image is simply ignored. #include "imageLib.h" #define VERBOSE 0 void halfsize(CByteImage src, CByteImage& dst) { CShape sh = src.Shape(); int w = sh.width, h = sh.height, nB = sh.nBands; CShape sh2(w/2, h/2, nB); dst.ReAllocate(sh2); for (int y = 0; y < h-1; y += 2) { for (int x = 0; x < w-1; x += 2) { for (int b = 0; b < nB; b++) { dst.Pixel(x/2, y/2, b) = ( src.Pixel(x, y , b) + src.Pixel(x+1, y , b) + src.Pixel(x , y+1, b) + src.Pixel(x+1, y+1, b) + 2) // ROUND! / 4; } } } } int main(int argc, char *argv[]) { try { if (argc < 3) throw CError("\n usage: %s in.png out.png [ntimes]\n", argv[0]); CByteImage im1, im2; int ntimes = 1; if (argc > 3) ntimes = atoi(argv[3]); ReadImageVerb(im1, argv[1], VERBOSE); for (int i=0; i < ntimes; i++) { if (VERBOSE) fprintf(stderr, "reducing\n"); halfsize(im1, im2); im1 = im2; } WriteImageVerb(im1, argv[2], VERBOSE); } catch (CError &err) { fprintf(stderr, err.message); fprintf(stderr, "\n"); return -1; } return 0; }