// main.cpp // // Copyright (c) 2003 Philip Romanik, Amy Muntz // // Permission to use, copy, modify, distribute, and sell this software and // its documentation for any purpose is hereby granted without fee, provided // that (i) the above copyright notices and this permission notice appear in // all copies of the software and related documentation, and (ii) the names // of Philip Romanik and Amy Muntz may not be used in any advertising or // publicity relating to the software without the specific, prior written // permission of Philip Romanik and Amy Muntz. // // Use of this software and/or its documentation will be deemed to be // acceptance of these terms. // // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. // // IN NO EVENT SHALL PHILIP ROMANIK OR AMY MUNTZ BE LIABLE FOR // ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, // OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, // WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF // LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE // OF THIS SOFTWARE. // // // Unit Test for image storage class #include "image/imageStorageTmpl.h" #include "unitTest.h" #include #include // Ruler // 1 2 3 4 5 6 6 //345678901234567890123456789012345678901234567890123456789012345 template void row_set (apImageStorage image, T value) { typename apImageStorage::row_iterator i; unsigned int width = image.width (); for (i=image.row_begin(); i != image.row_end(); i++) { T* p = i->p; for (unsigned int x=0; x void pixel_set (apImageStorage image, T value) { typename apImageStorage::iterator i; for (i=image.begin(); i != image.end(); i++) *i = value; } template void stl_set (apImageStorage image, T value) { std::fill (image.begin(), image.end(), value); } template void set_nonstandard (apImageStorage image, T value) { unsigned int x, y; T* p; for (y=0; y void set_slow (apImageStorage image, T value) { int x0 = image.x0 (); int y0 = image.y0 (); unsigned int x, y; for (y=0; y byteImage1; apImageStorage byteImage2 (boundary); apImageStorage byteImage3 (boundary, apRectImageStorage::eQuadWordAlign); VERIFY (byteImage1.isNull()); VERIFY (!byteImage2.isNull()); VERIFY (!byteImage3.isNull()); VERIFY (byteImage2.boundary() == boundary); VERIFY (byteImage3.boundary() == boundary); VERIFY (byteImage2.bytesPerPixel() == sizeof (Pel8)); VERIFY (byteImage3.bytesPerPixel() == sizeof (Pel8)); VERIFY (byteImage2.xoffset() == 0); VERIFY (byteImage3.xoffset() == 0); VERIFY (byteImage2.yoffset() == 0); VERIFY (byteImage3.yoffset() == 0); apImageStorage longImage1; apImageStorage longImage2 (boundary); apImageStorage longImage3 (boundary, apRectImageStorage::eQuadWordAlign); VERIFY (longImage1.isNull()); VERIFY (!longImage2.isNull()); VERIFY (!longImage3.isNull()); VERIFY (longImage2.boundary() == boundary); VERIFY (longImage3.boundary() == boundary); VERIFY (longImage2.bytesPerPixel() == sizeof (Pel32s)); VERIFY (longImage3.bytesPerPixel() == sizeof (Pel32s)); VERIFY (longImage2.xoffset() == 0); VERIFY (longImage3.xoffset() == 0); VERIFY (longImage2.yoffset() == 0); VERIFY (longImage3.yoffset() == 0); } UTFUNC(rowaddress) { setDescription ("rowaddress"); apRect boundary (0, 0, 640, 480); apImageStorage byteImage2 (boundary); apImageStorage byteImage3 (boundary, apRectImageStorage::eQuadWordAlign); // Set all the pixels unsigned int i; for (i=0; i longImage2 (boundary); apImageStorage longImage3 (boundary, apRectImageStorage::eQuadWordAlign); // Set all the pixels for (i=0; i byteImage4 (oddBoundary, apRectImageStorage::eQuadWordAlign); // Make sure row spacing is constant and correct for (i=1; i longImage (boundary); // Set the pixels unsigned int x, y; Pel32s value = 0; for (y=0; y longImage (boundary); // Set the pixels unsigned int x, y; Pel32s value = 0; for (y=0; y longImage2 (boundary); copy (longImage, longImage2); // Verify their values value = 0; for (y=0; y byteImage (boundary); copy (longImage, byteImage); // Verify their values value = 0; for (y=0; y longImage (boundary); // Set the pixels unsigned int x, y; Pel32s value = 0; for (y=0; y longImage2 = duplicate (longImage, apRectImageStorage::eNoAlign); apImageStorage longImage3 = duplicate (longImage, apRectImageStorage::eQuadWordAlign); VERIFY (longImage3.alignment() == apRectImageStorage::eQuadWordAlign); // Verify their values value = 0; for (y=0; y byteImage (boundary); // Complete overlap apRect boundary2 (-100, -100, 200, 200); apImageStorage byteImage2 = byteImage; byteImage2.window (boundary2); VERIFY (byteImage2.boundary() == boundary); // No overlap apRect boundary3 (100, 100, 100, 100); apImageStorage byteImage3 = byteImage; byteImage3.window (boundary3); VERIFY (byteImage3.isNull()); // Inside window apRect boundary4 (0, 10, 30, 40); apImageStorage byteImage4 = byteImage; byteImage4.window (boundary4); VERIFY (byteImage4.boundary() == boundary4); // Overlap 1 apRect boundary5 (-50, -50, 60, 60); apRect overlap1 (-20, -10, 30, 20); apImageStorage byteImage5 = byteImage; byteImage5.window (boundary5); VERIFY (byteImage5.boundary() == overlap1); // Overlap 2 apRect boundary6 (70, 80, 60, 60); apRect overlap2 (70, 80, 30, 20); apImageStorage byteImage6 = byteImage; byteImage6.window (boundary6); VERIFY (byteImage6.boundary() == overlap2); } UTFUNC(rebase) { setDescription ("rebase"); apRect boundary (-20, -10, 120, 110); apImageStorage byteImage (boundary); // First check the existing image VERIFY (byteImage.boundary() == boundary); VERIFY (byteImage.x0() == -20); VERIFY (byteImage.y0() == -10); VERIFY (byteImage.width() == 120); VERIFY (byteImage.height() == 110); VERIFY (byteImage.xoffset() == 0); VERIFY (byteImage.yoffset() == 0); byteImage.rebase (); VERIFY (byteImage.boundary() == apRect (0, 0, 120, 110)); VERIFY (byteImage.x0() == 0); VERIFY (byteImage.y0() == 0); VERIFY (byteImage.width() == 120); VERIFY (byteImage.height() == 110); VERIFY (byteImage.xoffset() == 0); VERIFY (byteImage.yoffset() == 0); // Try it on a window byteImage = apImageStorage (boundary); row_set (byteImage, 0); apRect window (10, 20, 30, 40); byteImage.window (window); row_set (byteImage, 1); VERIFY (byteImage.boundary() == window); VERIFY (byteImage.x0() == 10); VERIFY (byteImage.y0() == 20); VERIFY (byteImage.width() == 30); VERIFY (byteImage.height() == 40); VERIFY (byteImage.xoffset() == 30); VERIFY (byteImage.yoffset() == 30); byteImage.rebase (); VERIFY (byteImage.boundary() == apRect (0, 0, 30, 40)); VERIFY (byteImage.x0() == 0); VERIFY (byteImage.y0() == 0); VERIFY (byteImage.width() == 30); VERIFY (byteImage.height() == 40); VERIFY (byteImage.xoffset() == 30); VERIFY (byteImage.yoffset() == 30); int x, y; for (y=0; y longImage (boundary); row_set (longImage, 0); // Set a small window to 1 apRect boundary2 (1, 2, 4, 5); apImageStorage longImage2 = longImage; longImage2.window (boundary2); row_set (longImage2, 1); apImageStorage::row_iterator i; for (i=longImage.row_begin(); i != longImage.row_end(); i++) { const Pel32s* p = i->p; std::cout << i->x << ", " << i->y << ": "; for (unsigned int x=0; x= 1 && x <= 4 && y >= 2 && y <= 6) VERIFY (longImage.getPixel (x, y) == 1); else VERIFY (longImage.getPixel (x, y) == 0); } } // Do this four other ways as a check apImageStorage longImage3 = longImage; longImage3.window (boundary2); pixel_set (longImage3, 1); apImageStorage longImage4 = longImage; longImage4.window (boundary2); stl_set (longImage3, 1); apImageStorage longImage5 = longImage; longImage5.window (boundary2); set_nonstandard (longImage3, 1); apImageStorage longImage6 = longImage; longImage6.window (boundary2); set_slow (longImage3, 1); VERIFY (longImage3 == longImage2); VERIFY (longImage4 == longImage2); VERIFY (longImage5 == longImage2); VERIFY (longImage6 == longImage2); } UTFUNC(row_iterator) { setDescription ("row iterator"); apRect boundary (0, 0, 32, 32); apImageStorage longImage (boundary); // Set the pixels manually unsigned int x, y; Pel32s value = 0; for (y=0; y::row_iterator i; long width = longImage.width (); value = 0; for (i=longImage.row_begin(); i != longImage.row_end(); i++) { Pel32s* p = i->p; for (int x=0; x longImage (boundary); // Set the pixels manually unsigned int x, y; Pel32s sum = 0; Pel32s value = 0; for (y=0; y::iterator i; value = 0; for (i=longImage.begin(); i != longImage.end(); i++) { VERIFY (*i == value++); } // Make sure that STL can use this iterator Pel32s computedSum = std::accumulate (longImage.begin(), longImage.end(), 0, std::plus()); VERIFY (computedSum == sum); std::fill (longImage.begin(), longImage.end(), 1); for (i=longImage.begin(); i != longImage.end(); i++) { VERIFY (*i == 1); } } #ifdef WIN32 #include "win32/fasttiming.h" UTFUNC(timing) { setDescription ("win32 timing"); VERIFY (1 == 1); int i; apRect boundary (0, 0, 1024, 1024); apImageStorage image (boundary); apHiResElapsedTime timer1; for (i=0; i<100; i++) row_set (image, 0); double t1 = timer1.msec ()/100; apHiResElapsedTime timer2; for (i=0; i<100; i++) pixel_set (image, 0); double t2 = timer2.msec ()/100; apHiResElapsedTime timer3; for (i=0; i<100; i++) stl_set (image, 0); double t3 = timer3.msec ()/100; apHiResElapsedTime timer4; for (i=0; i<100; i++) set_nonstandard (image, 0); double t4 = timer4.msec ()/100; apHiResElapsedTime timer5; for (i=0; i<10; i++) // Keep this at 10. It's slow! set_slow (image, 0); double t5 = timer5.msec ()/10; std::cout << "1024x1024 row_set<> in " << t1 << " msec" << std::endl; std::cout << "1024x1024 pixel_set<> in " << t2 << " msec" << std::endl; std::cout << "1024x1024 stl_set<> in " << t3 << " msec" << std::endl; std::cout << "1024x1024 set_nonstandard<> in " << t4 << " msec" << std::endl; std::cout << "1024x1024 set_slow<> in " << t5 << " msec" << std::endl; } #endif // Our main function is trivial int main() { bool state = apUnitTest::gOnly().run (); apUnitTest::gOnly().dumpResults (std::cout); return state; }