#include "ImageLoader.h" #include "rimage_ser.h" #ifdef USE_NSIMAGE #include "rimageFromNSImage.h" #endif #ifdef USE_IMAGEMAGICK #include #endif #ifdef WIN32 #ifndef WINVER #define WINVER 0x0502 #endif #include "afxwin.h" #include "bmpFromRimage.h" #endif namespace CERT { bool loadRImage_Mac( RImage &pixels, const std::string &path ); bool loadRImage_IM( RImage &pixels, const std::string &path ); bool loadRImage_WIN( RImage &pixels, const std::string &path ); ImageLoader::ImageLoader() { #ifdef USE_NSIMAGE initForNSImage(); #endif } ImageLoader::~ImageLoader() { cleanup(); } void ImageLoader::cleanup() { #ifdef USE_NSIMAGE releaseForNSImage(); #endif } bool ImageLoader::loadRImage( RImage &pixels, const std::string &path ) { if( loadRImage_Mac( pixels, path ) ) return true; if( loadRImage_WIN( pixels, path ) ) return true; if( loadRImage_IM( pixels, path ) ) return true; if( restoreRImageFromFile( pixels, path ) ) return true; return false; } bool loadRImage_Mac( RImage &pixels, const std::string &path ) { #ifdef USE_NSIMAGE if( rimageFromNSImage( pixels, path ) ) { return true; } #endif return false; } bool loadRImage_IM( RImage &pixels, const std::string &path ) { #ifdef USE_IMAGEMAGICK // Before trying to read the image we need to make sure the path is valid, // otherwise ImageMagick terminates the program. Magick::Image image; try { image.read( path.c_str() ); /* Or we could create an image from in memory data (e.g. video frame capture) Magick::Blob blob( inMemoryImageDataPtr, inMemoryImageDataLength ); Magick::Image image( blob ); */ if (image.columns() == 0 && image.rows() == 0) { std::cerr << "Image contains 0 rows and 0 columns!" << std::endl; return false; } image.quantizeColors(256); // convert to 256 colors image.quantize(); // convert to grayscale pixels.setSize( image.columns(), image.rows() ); image.write( 0, 0, image.columns(), image.rows(), "I", Magick::FloatPixel, pixels.array ); return true; } catch( Magick::ErrorBlob &error ) { std::cerr << "Caught ImageMagick IO exception: " << error.what() << std::endl; return false; } catch( Magick::ErrorMissingDelegate &error ) { std::cerr << "Caught ImageMagick exception: " << error.what() << std::endl; return false; } catch( Magick::Error &error ) { std::cerr << "Caught ImageMagick error: " << error.what() << std::endl; return false; } catch( Magick::Exception &error ) { std::cerr << "Caught ImageMagick exception: " << error.what() << std::endl; return false; } catch( std::exception &error ) { std::cerr << "Caught standard exception: " << error.what() << std::endl; return false; } catch( ... ) { std::cerr << "Caught unknown exception: " << std::endl; return false; } return true; #else return false; #endif } bool loadRImage_WIN( RImage &pixels, const std::string &path ) { #ifdef WIN32 // Load bitmap HBITMAP hBitmap = (HBITMAP) LoadImage(NULL, path.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); if (hBitmap) { CBitmap bmpBitmap; // Create a CBitmap bmpBitmap.Attach(hBitmap); BITMAP bm; // Get the Bitmap bmpBitmap.GetBitmap(&bm); if(bm.bmBitsPixel==24) // if it's 24 bits ... { // a bit tricky ... // align within 32 bits int align = (4-((bm.bmWidth)%4)) & 3; int nbr_byte = bm.bmHeight * bm.bmWidth + align * bm.bmHeight/3; BYTE* pBits= (BYTE*) bm.bmBits + (bm.bmHeight-1)*(3*bm.bmWidth+align); pixels.setSize(bm.bmWidth, bm.bmHeight); // read bitmap pixels, convert to grayscale, and copy into RImage for analysis for (int l=0;l> 10; pixels.setPixel( c, l, theGrayValue/255.0f ); // copy to RImage pBits=pBits+3; } pBits=pBits-align-2*3*bm.bmWidth; } return true; } else if( 32 == bm.bmBitsPixel ) { // bmp.bmWidthBytes BYTE *pBits= (BYTE*)bm.bmBits; pixels.setSize( bm.bmWidth, bm.bmHeight ); // read bitmap pixels, convert to grayscale, and copy into RImage for analysis for (int l=0;l> 10; pixels.setPixel( c, l, theGrayValue/255.0f ); // copy to RImage pBits = pBits + 4; } // pBits=pBits-align-2*3*bm.bmWidth; } return true; } else { std::cerr << "Unable to process bitmap. This demo only works with 24 bit bitmaps!" << std::endl; return false; } } else { return false; } #else return false; #endif } bool ImageLoader::saveRImage( RImage &pixels, const std::string &path, Images::filetype format ) { if( Images::rimage == format ) { return saveRImageToFile( pixels, path ); } #ifdef WIN32 else if( Images::bmp == format ) { return writeRimageAsBMP( pixels, path ); } #endif #ifdef USE_NSIMAGE return saveRimageToFile( pixels, path, format ); #endif return false; } } // end namespace CERT