// Utils.cpp -- general image helper functions // // Copyright © Daniel Scharstein, 2003. // /////////////////////////////////////////////////////////////////////////// #include "Error.h" #include "Image.h" #include "ImageIO.h" #include "Utils.h" #include "Color.h" #define ROUND(x) ((x) > 0 ? ((int)((x) + .5)) : ((int)((x) - .5))) // downsample a color image simply by averaging 4 pixels together void downsample(CByteImage im, CByteImage &im2) { CShape sh = im.Shape(); int x, y, w = sh.width, h = sh.height, nB = sh.nBands; int w2 = w/2, h2 = h/2; CShape sh2(w2, h2, nB); im2.ReAllocate(sh2); for (y = 0; y < h2; y++) { uchar *ia = &im.Pixel(0, 2*y, 0); uchar *ib = &im.Pixel(0, 2*y+1, 0); uchar *i2 = &im2.Pixel(0, y, 0); for (x = 0; x < w2; x++, ia += 2*nB, ib += 2*nB, i2 += nB) { for (int k = 0; k < nB; k++) i2[k] = (ia[k] + ia[k+nB] + ib[k] + ib[k+nB]) / 4; } } } /////////////////////////////////////////////////////////////////////////// // miscellaneous // read an image and perhaps tell the user you're doing so void ReadImageVerb(CImage& img, const char* filename, int verbose) { if (verbose) fprintf(stderr, "Reading image %s\n", filename); ReadImage(img, filename); } // write out an image and perhaps tell the user you're doing so void WriteImageVerb(CImage& img, const char* filename, int verbose) { if (verbose) fprintf(stderr, "Writing image %s\n", filename); WriteImage(img, filename); } // make sure a command-line argument is a non-negative integer static void AssertIntString(char *s) { for (int k=0; s[k]; k++) { if (*s < '0' || *s > '9') throw CError("Positive integer expected: '%s'", s); } } // safe parsing of integer argument int atoiSafe(char *s) { AssertIntString(s); return atoi(s); } //write float in exactly six spaces. 42.423434345 written " 42.432" void justify(float f){ int i=(int)f; float dec=f-(float)i; if(dec<0) dec=-dec; dec*=(100); printf("%3d.%.2d",i,(int)dec); } //Takes a 3D array and translates the integers into colors with color ids and then outputs a series of images void arrayToImage(int*** allData, int nFrames, int h, int w, char*name) { CShape sh = CShape(w,h,4); for(int f=0; f