void ImageFile::readerPNG(GrayColorImageCommonImplementationT<Ipp32f> *image) { switch(image->channels()) { case 1: { Image buffer; readerPNG(&buffer); if(image->widthInline()!=buffer.width() || image->heightInline()!=buffer.height()) { image->resize(buffer.width(), buffer.height()); } grayToFloat(buffer,dynamic_cast<ImageT<Ipp32f>* >(image)); break; } case 3: { ColorImage buffer; readerPNG(&buffer); if(image->widthInline()!=buffer.width() || image->heightInline()!=buffer.height()) { image->resize(buffer.width(), buffer.height()); } convertBitDepth(buffer,dynamic_cast<ColorImageT<Ipp32f>* >(image)); break; } default: fthrow(ImageException,"No or invalid color image->channels()"); break; } }
int main ( int argc, char **argv ) { cout << "converting and cropping (not resizing!) tool (c) Erik Rodner, 2011" << endl; if ( (argc != 3) && (argc != 5) ) { cerr << "usage: " << argv[0] << " <src> <dst> [width] [height]" << endl; exit(-1); } ColorImage src ( argv[1] ); if ( argc == 3 ) { src.write(argv[2]); } else { int xsize = std::min( atoi(argv[3]), src.width() ); int ysize = std::min( atoi(argv[4]), src.height() ); src.createSubImage(Rect(0,0,xsize,ysize))->write ( argv[2] ); } }
void ImageTest::testColorImage() { ColorImage image(4,3); CPPUNIT_ASSERT_EQUAL(4, image.width()); CPPUNIT_ASSERT_EQUAL(3, image.height()); CPPUNIT_ASSERT_THROW(image.setPixel(4, 2, 1, 11), ImageException); CPPUNIT_ASSERT_THROW(image.setPixel(3, 3, 2, 12), ImageException); CPPUNIT_ASSERT_NO_THROW(image.setPixelSave(4, 2, 11, 12, 13)); CPPUNIT_ASSERT_NO_THROW(image.setPixelSave(3, 3, 11, 12, 13)); CPPUNIT_ASSERT_THROW(image.getPixel(4, 2, 1), ImageException); CPPUNIT_ASSERT_THROW(image.getPixel(3, 3, 2), ImageException); image.setPixel(0, 0, 11, 12, 13); image.setPixel(3, 2, 25, 26, 27); CPPUNIT_ASSERT_EQUAL(11, (int) image.getPixel(0, 0, 0)); CPPUNIT_ASSERT_EQUAL(12, (int) image.getPixel(0, 0, 1)); CPPUNIT_ASSERT_EQUAL(13, (int) image.getPixel(0, 0, 2)); CPPUNIT_ASSERT_EQUAL(25, (int) image.getPixel(3, 2, 0)); CPPUNIT_ASSERT_EQUAL(26, (int) image.getPixel(3, 2, 1)); CPPUNIT_ASSERT_EQUAL(27, (int) image.getPixel(3, 2, 2)); CPPUNIT_ASSERT_EQUAL(25, (int) *image.getPixelPointerXY(3, 2)); ColorImage image2(image, GrayColorImageCommonImplementation::noAlignment); CPPUNIT_ASSERT_EQUAL(26, (int) image.getPixel(3, 2, 1)); CPPUNIT_ASSERT_EQUAL(26, (int) image2.getPixel(3, 2, 1)); // set ColorImage image3(10,10); image3.set(15,64,253); for(int y=0; y<image3.height(); ++y) for(int x=0; x<image3.width(); ++x) { CPPUNIT_ASSERT_EQUAL( 15, static_cast<int>(image3.getPixelQuick(x,y,0))); CPPUNIT_ASSERT_EQUAL( 64, static_cast<int>(image3.getPixelQuick(x,y,1))); CPPUNIT_ASSERT_EQUAL(253, static_cast<int>(image3.getPixelQuick(x,y,2))); } // operator =, ==, != { ColorImage img(2,2); img.set(0,0,0); img.setPixelQuick(1,1,0,133); ColorImage fimg(3,2); CPPUNIT_ASSERT_THROW(fimg==img, ImageException); CPPUNIT_ASSERT_THROW(fimg!=img, ImageException); ColorImage timg; timg = img; CPPUNIT_ASSERT_NO_THROW(img==timg); CPPUNIT_ASSERT_EQUAL(true, img==timg); CPPUNIT_ASSERT_EQUAL(false, img!=timg); timg.setPixelQuick(0,0,3,99); CPPUNIT_ASSERT_NO_THROW(img!=timg); CPPUNIT_ASSERT_EQUAL(true, img!=timg); CPPUNIT_ASSERT_EQUAL(false, img==timg); } // Kontruktor: create a ColorImageT from 3 Channel Images ImageT { // exception { Image chan1, chan2, chan3; chan1 = Image(5,6); chan2 = Image(6,7); chan3 = Image(7,8); CPPUNIT_ASSERT_THROW(ColorImage c(chan1,chan2,chan3), ImageException); } // <Ipp8u> { Image chan1(5,5), chan2(5,5), chan3(5,5); chan1.set(100); chan2.set(150); chan3.set(200); for(int y=0; y<chan1.height(); ++y) for(int x=0; x<chan1.width(); ++x) { CPPUNIT_ASSERT_EQUAL(100, static_cast<int>(chan1.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(150, static_cast<int>(chan2.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(200, static_cast<int>(chan3.getPixelQuick(x,y))); } ColorImage c(chan1,chan2,chan3); CPPUNIT_ASSERT_EQUAL(static_cast<int>(c.width()), static_cast<int>(chan1.width())); CPPUNIT_ASSERT_EQUAL(static_cast<int>(c.height()), static_cast<int>(chan1.height())); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_EQUAL(100, static_cast<int>(c.getPixelQuick(x,y,0))); CPPUNIT_ASSERT_EQUAL(150, static_cast<int>(c.getPixelQuick(x,y,1))); CPPUNIT_ASSERT_EQUAL(200, static_cast<int>(c.getPixelQuick(x,y,2))); } } // FloatImage { FloatImage chan1(5,5), chan2(5,5), chan3(5,5); chan1.set( -50.0); chan2.set(1000.0); chan3.set( 1.0); for(int y=0; y<chan1.height(); ++y) for(int x=0; x<chan1.width(); ++x) { CPPUNIT_ASSERT_EQUAL( -50.0, static_cast<double>(chan1.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(1000.0, static_cast<double>(chan2.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL( 1.0, static_cast<double>(chan3.getPixelQuick(x,y))); } ColorImageT<float> c(chan1,chan2,chan3); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_DOUBLES_EQUAL( -50.0, static_cast<double>(c.getPixelQuick(x,y,0)), 0.0); CPPUNIT_ASSERT_DOUBLES_EQUAL(1000.0, static_cast<double>(c.getPixelQuick(x,y,1)), 0.0); CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, static_cast<double>(c.getPixelQuick(x,y,2)), 0.0); } } // Ipp16s { ImageT<Ipp16s> chan1(5,5), chan2(5,5), chan3(5,5); chan1.set(-255); chan2.set(+255); chan3.set( 0); for(int y=0; y<chan1.height(); ++y) for(int x=0; x<chan1.width(); ++x) { CPPUNIT_ASSERT_EQUAL(-255, static_cast<int>(chan1.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(+255, static_cast<int>(chan2.getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL( 0, static_cast<int>(chan3.getPixelQuick(x,y))); } ColorImageT<Ipp16s> c(chan1,chan2,chan3); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_EQUAL(-255, static_cast<int>(c.getPixelQuick(x,y,0))); CPPUNIT_ASSERT_EQUAL(+255, static_cast<int>(c.getPixelQuick(x,y,1))); CPPUNIT_ASSERT_EQUAL( 0, static_cast<int>(c.getPixelQuick(x,y,2))); } } } // getChannel { // exception { ColorImage c(5,5); CPPUNIT_ASSERT_NO_THROW(c.getChannel(0)); CPPUNIT_ASSERT_NO_THROW(c.getChannel(1)); CPPUNIT_ASSERT_NO_THROW(c.getChannel(2)); CPPUNIT_ASSERT_THROW(c.getChannel( 3), ImageException); CPPUNIT_ASSERT_THROW(c.getChannel( 4), ImageException); } // <Ipp8u> { ColorImage c(5,5); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) c.setPixelQuick(x,y, 100,150,200); Image* g0 = c.getChannel(0); Image* g1 = c.getChannel(1); Image* g2 = c.getChannel(2); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_EQUAL(100, static_cast<int>(g0->getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(150, static_cast<int>(g1->getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(200, static_cast<int>(g2->getPixelQuick(x,y))); } } // FloatImage { ColorImageT<float> c(5,5); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) c.setPixelQuick(x,y, -1000.125, 1.32678, +999.999); FloatImage* g0 = c.getChannel(0); FloatImage* g1 = c.getChannel(1); FloatImage* g2 = c.getChannel(2); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_DOUBLES_EQUAL(-1000.125, static_cast<double>(g0->getPixelQuick(x,y)), 0.001); CPPUNIT_ASSERT_DOUBLES_EQUAL(1.32678 , static_cast<double>(g1->getPixelQuick(x,y)), 0.00001); CPPUNIT_ASSERT_DOUBLES_EQUAL(999.999 , static_cast<double>(g2->getPixelQuick(x,y)), 0.001); } } // <Ipp16s> { ColorImageT<Ipp16s> c(5,5); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) c.setPixelQuick(x,y, -255, 0, +255); GrayImage16s* g0 = c.getChannel(0); GrayImage16s* g1 = c.getChannel(1); GrayImage16s* g2 = c.getChannel(2); for(int y=0; y<c.height(); ++y) for(int x=0; x<c.width(); ++x) { CPPUNIT_ASSERT_EQUAL(-255, static_cast<int>(g0->getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL( 0, static_cast<int>(g1->getPixelQuick(x,y))); CPPUNIT_ASSERT_EQUAL(+255, static_cast<int>(g2->getPixelQuick(x,y))); } } } // mirror ColorImage image4(5,5); for(int y=0; y<image4.height(); ++y) for(int x=0; x<image4.width(); ++x) image4.setPixelQuick(x,y,1*(x+1+y*5),3*(x+1+y*5),6*(x+1+y*5)); image4.mirror(ippAxsHorizontal); CPPUNIT_ASSERT_EQUAL( 21, static_cast<int>(image4.getPixelQuick(0,0,0))); CPPUNIT_ASSERT_EQUAL( 16, static_cast<int>(image4.getPixelQuick(0,1,0))); CPPUNIT_ASSERT_EQUAL( 11, static_cast<int>(image4.getPixelQuick(0,2,0))); CPPUNIT_ASSERT_EQUAL( 6, static_cast<int>(image4.getPixelQuick(0,3,0))); CPPUNIT_ASSERT_EQUAL( 1, static_cast<int>(image4.getPixelQuick(0,4,0))); CPPUNIT_ASSERT_EQUAL( 22, static_cast<int>(image4.getPixelQuick(1,0,0))); CPPUNIT_ASSERT_EQUAL( 17, static_cast<int>(image4.getPixelQuick(1,1,0))); CPPUNIT_ASSERT_EQUAL( 12, static_cast<int>(image4.getPixelQuick(1,2,0))); CPPUNIT_ASSERT_EQUAL( 7, static_cast<int>(image4.getPixelQuick(1,3,0))); CPPUNIT_ASSERT_EQUAL( 2, static_cast<int>(image4.getPixelQuick(1,4,0))); image4.mirror(ippAxsHorizontal); image4.mirror(ippAxsVertical); CPPUNIT_ASSERT_EQUAL( 5, static_cast<int>(image4.getPixelQuick(0,0,0))); CPPUNIT_ASSERT_EQUAL( 10, static_cast<int>(image4.getPixelQuick(0,1,0))); CPPUNIT_ASSERT_EQUAL( 15, static_cast<int>(image4.getPixelQuick(0,2,0))); CPPUNIT_ASSERT_EQUAL( 20, static_cast<int>(image4.getPixelQuick(0,3,0))); CPPUNIT_ASSERT_EQUAL( 25, static_cast<int>(image4.getPixelQuick(0,4,0))); CPPUNIT_ASSERT_EQUAL( 4, static_cast<int>(image4.getPixelQuick(1,0,0))); CPPUNIT_ASSERT_EQUAL( 9, static_cast<int>(image4.getPixelQuick(1,1,0))); CPPUNIT_ASSERT_EQUAL( 14, static_cast<int>(image4.getPixelQuick(1,2,0))); CPPUNIT_ASSERT_EQUAL( 19, static_cast<int>(image4.getPixelQuick(1,3,0))); CPPUNIT_ASSERT_EQUAL( 24, static_cast<int>(image4.getPixelQuick(1,4,0))); image4.mirror(ippAxsVertical); image4.mirror(ippAxsBoth); CPPUNIT_ASSERT_EQUAL( 25, static_cast<int>(image4.getPixelQuick(0,0,0))); CPPUNIT_ASSERT_EQUAL( 19, static_cast<int>(image4.getPixelQuick(1,1,0))); CPPUNIT_ASSERT_EQUAL( 13, static_cast<int>(image4.getPixelQuick(2,2,0))); CPPUNIT_ASSERT_EQUAL( 7, static_cast<int>(image4.getPixelQuick(3,3,0))); CPPUNIT_ASSERT_EQUAL( 1, static_cast<int>(image4.getPixelQuick(4,4,0))); CPPUNIT_ASSERT_EQUAL( 21, static_cast<int>(image4.getPixelQuick(4,0,0))); CPPUNIT_ASSERT_EQUAL( 17, static_cast<int>(image4.getPixelQuick(3,1,0))); CPPUNIT_ASSERT_EQUAL( 13, static_cast<int>(image4.getPixelQuick(2,2,0))); CPPUNIT_ASSERT_EQUAL( 9, static_cast<int>(image4.getPixelQuick(1,3,0))); CPPUNIT_ASSERT_EQUAL( 5, static_cast<int>(image4.getPixelQuick(0,4,0))); image4.mirror(ippAxsBoth); // transpose image4.transpose(); for(int y=0; y<image4.height(); ++y) for(int x=0; x<image4.width(); ++x) { CPPUNIT_ASSERT_EQUAL( 1*(y+1+x*5), static_cast<int>(image4.getPixelQuick(x,y,0))); CPPUNIT_ASSERT_EQUAL( 3*(y+1+x*5), static_cast<int>(image4.getPixelQuick(x,y,1))); CPPUNIT_ASSERT_EQUAL( 6*(y+1+x*5), static_cast<int>(image4.getPixelQuick(x,y,2))); } // copyRect image4 = ColorImage(10,10); for(int y=0; y<image4.width(); ++y) for(int x=0; x<image4.width(); ++x) image4.setPixelQuick(x,y,x+1+y*image4.width(),(x+1+y*image4.width())/2,(x+1+y*image4.width())/3); ColorImage* imgRect = image4.copyRect(Rect(2,3,4,3)); CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(imgRect->width())); CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(imgRect->height())); for(int y=0; y<imgRect->height(); ++y) for(int x=0; x<imgRect->width(); ++x) { CPPUNIT_ASSERT_EQUAL(((x+3)+(y+3)*image4.width())/1, static_cast<int>(imgRect->getPixelQuick(x,y,0))); CPPUNIT_ASSERT_EQUAL(((x+3)+(y+3)*image4.width())/2, static_cast<int>(imgRect->getPixelQuick(x,y,1))); CPPUNIT_ASSERT_EQUAL(((x+3)+(y+3)*image4.width())/3, static_cast<int>(imgRect->getPixelQuick(x,y,2))); } imgRect = image4.copyRect(Rect(7,6,10,10)); CPPUNIT_ASSERT_EQUAL( 3, static_cast<int>(imgRect->width())); CPPUNIT_ASSERT_EQUAL( 4, static_cast<int>(imgRect->height())); CPPUNIT_ASSERT_EQUAL( 68/1, static_cast<int>(imgRect->getPixelQuick(0,0,0))); CPPUNIT_ASSERT_EQUAL( 68/2, static_cast<int>(imgRect->getPixelQuick(0,0,1))); CPPUNIT_ASSERT_EQUAL( 68/3, static_cast<int>(imgRect->getPixelQuick(0,0,2))); CPPUNIT_ASSERT_EQUAL( 69/1, static_cast<int>(imgRect->getPixelQuick(1,0,0))); CPPUNIT_ASSERT_EQUAL( 69/2, static_cast<int>(imgRect->getPixelQuick(1,0,1))); CPPUNIT_ASSERT_EQUAL( 69/3, static_cast<int>(imgRect->getPixelQuick(1,0,2))); CPPUNIT_ASSERT_EQUAL( 70/1, static_cast<int>(imgRect->getPixelQuick(2,0,0))); CPPUNIT_ASSERT_EQUAL( 70/2, static_cast<int>(imgRect->getPixelQuick(2,0,1))); CPPUNIT_ASSERT_EQUAL( 70/3, static_cast<int>(imgRect->getPixelQuick(2,0,2))); CPPUNIT_ASSERT_EQUAL( 98/1, static_cast<int>(imgRect->getPixelQuick(0,3,0))); CPPUNIT_ASSERT_EQUAL( 98/2, static_cast<int>(imgRect->getPixelQuick(0,3,1))); CPPUNIT_ASSERT_EQUAL( 98/3, static_cast<int>(imgRect->getPixelQuick(0,3,2))); CPPUNIT_ASSERT_EQUAL( 99/1, static_cast<int>(imgRect->getPixelQuick(1,3,0))); CPPUNIT_ASSERT_EQUAL( 99/2, static_cast<int>(imgRect->getPixelQuick(1,3,1))); CPPUNIT_ASSERT_EQUAL( 99/3, static_cast<int>(imgRect->getPixelQuick(1,3,2))); CPPUNIT_ASSERT_EQUAL(100/1, static_cast<int>(imgRect->getPixelQuick(2,3,0))); CPPUNIT_ASSERT_EQUAL(100/2, static_cast<int>(imgRect->getPixelQuick(2,3,1))); CPPUNIT_ASSERT_EQUAL(100/3, static_cast<int>(imgRect->getPixelQuick(2,3,2))); }