#ifndef TXS_PYRAMID_GUARD #define TXS_PYRAMID_GUARD #include "txs_globals.h" #include "TiffImage.h" /* * A pyramid of images. Not necessarily a Gaussian pyramid.. * The finest image must be a square with power-of-2 edge dimensions. */ namespace TXS { class TxsPyramid { private: /* * All pyramids are square. * size is the edge len */ int finestSize; int numLevs; /* * Our image buffers */ TiffImage **levels; public: int log2(int n) { assert(n > 0); // use bit shifting to calculate log(n) base 2 int p = 0; while(n != 1) { p++; n = n >> 1; } return p; } TxsPyramid(int finestSize) { //__DBa("creating pyramid of finest size: " << finestSize); this->finestSize = finestSize; if(finestSize < 16) { __ERROR__("Creating a pyramid where the finest size is smaller than 16. this is too small to create " << NUM_PYLEVS << " levels!"); } // log2 + 1, because we INCLUDE the finest size // this->numLevs = log2(finestSize) + 1; // UPDATE: instead of creating all sizes down the 1x1, just create a fixed number of them this->numLevs = NUM_PYLEVS; // allocate pyramids this->levels = new TiffImage*[this->numLevs]; // start from the finest, and go down int size = this->finestSize; for(int lev = this->numLevs-1; lev >= 0; --lev) { __DBa(" - creating level " << lev << " of size " << size); this->levels[lev] = new TiffImage(size, size); // next size.. size /= 2; } } NormRGB getRGB(unsigned int lev, UVs uv) { // safety check if(lev >= numLevs) { __ERROR__("getRGB: bad level given: " << lev << ". This pyramid only has " << this->numLevs << " levels"); return NormRGB(); } else { return this->levels[lev]->getRgbAtUV(uv); } } int getNumLevs() { return this->numLevs; } TiffImage *getLevel(int lev) { return this->levels[lev]; } TiffImage *getFinestLevel() { int last = this->numLevs - 1; return this->levels[last]; } int finestLevel() { int last = this->numLevs - 1; return last; } int getSizeOfLevel(int lev) { return this->levels[lev]->getWidth(); } /* * Given a 2-level pyramid (lo/hi res level images), this will search itself * and try to find the closest pyramid to the given one. It will only search * the 2-levels n and n-1, where n == hiLevelNum. * The two output coords will be coordinates in the hi-res level. * midOfs is basically the middle coordinate of the hi-res level. */ void getClosestPyramid(TiffImage &loLev, TiffImage &hiLev, int hiLevelNum, int &xHiOut, int &yHiOut, int midOfs); }; } #endif /* TXS_PYRAMID_GUARD */