/** * @class vetFrameT * * @brief * * * * * @bug * @warning * @todo * * @see * @see vetFrame * * @version 0.6 * @date 12/07/2005 - //2005 * @author Alessandro Polo * * **************************************************************************** * VETLib Framework 1.0.2 * Copyright (C) Alessandro Polo 2006 * http://www.ewgate.net/vetlib * ****************************************************************************/ //BUGssssssssssssssss // SETPIXEL E GETPIXEL devono essere rifatte in base al pixelPacked e canali !! // ecc (realloc) #ifndef __VETLIB_VETFRAMETH__ #define __VETLIB_VETFRAMETH__ #define _VETFRAMET_SLOWMODE #include "vetDefs.h" #include "vetFrame.h" template class vetFrameT : public virtual vetFrame { public: /** * @var Width of the image */ unsigned int width; /** * @var Height of the image */ unsigned int height; bool autoFreeData; VETFRAME_PROFILE profile; VETFRAME_CHANNEL_TYPE dataType; /** * @var data array pointer. Size is unknown. */ T *data; public: /** * @brief Default constructor, initialize height and width to 0. */ vetFrameT() { height = 0; width = 0; data = NULL; autoFreeData = true; dataType = vetFrame::VETFRAME_CT_NONE; profile = vetFrame::VETFRAME_NONE; } vetFrameT(unsigned int w, unsigned int h) { data = NULL; autoFreeData = true; dataType = vetFrame::VETFRAME_CT_PACKED; profile = vetFrame::VETFRAME_MONO; reAllocCanvas(w, h); } /** * @brief Create an image with the given dimensions, allocates empty data. * @param w Width of the image. * @param h Height of the image. */ vetFrameT(unsigned int w, unsigned int h, vetFrame::VETFRAME_PROFILE prof, vetFrame::VETFRAME_CHANNEL_TYPE dataTy = vetFrame::VETFRAME_CT_PLANAR) { data = NULL; autoFreeData = true; dataType = dataTy; profile = prof; reAllocCanvas(w, h); } /** * @brief Copy Constructor, create an image from another image, copying memory. * @param img source image. */ vetFrameT(vetFrameT& img) { data = NULL; dataType = img.dataType; profile = img.profile; autoFreeData = img.autoFreeData; reAllocCanvas(img.width, img.height); memcpy(static_cast(data), static_cast(img.data), getBufferSize() ); } /** * @brief Destructor currenly clear pixel data (array). */ ~vetFrameT() { if ( autoFreeData && data != NULL ) delete [] data; } bool isBuiltInSupportedProfile(VETFRAME_PROFILE pr) { switch( profile ) { case vetFrame::VETFRAME_MONO: case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: case vetFrame::VETFRAME_RGB32: case vetFrame::VETFRAME_BGR32: return true; case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: //BUG TODO default: return false; } } /** * @brief read current image's width. * * @return Width in pixel. */ unsigned int getWidth() const { return width; }; unsigned int getWidth() { return width; }; /** * @brief read current image's height. * * @return Height in pixel. */ unsigned int getHeight() const { return height; }; unsigned int getHeight() { return height; }; void* dump_buffer() { return static_cast(data); }; VETRESULT reAllocCanvas(unsigned int w, unsigned int h) { width = w; height = h; if ( width == 0 || height == 0 ) return VETRET_PARAM_ERR; if ( data != NULL ) delete [] data; data = NULL; int multiplier = 1; switch( profile ) { case vetFrame::VETFRAME_MONO: multiplier = 1; break; case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: case vetFrame::VETFRAME_RGB96: case vetFrame::VETFRAME_BGR32: multiplier = 3; break; case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: multiplier = 4; break; //BUG TODO default: break; } data = new T[ (unsigned int)(width * height * multiplier) ]; return VETRET_OK; } VETRESULT extractBrightness(unsigned char* buffer, unsigned int* size = NULL ) { return 666; } unsigned int getBpp() { switch( profile ) { case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: return 24; case vetFrame::VETFRAME_RGB96: case vetFrame::VETFRAME_BGR96: return 96; case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: return 32; //BUG TODO case vetFrame::VETFRAME_MONO: return sizeof(T) * 8; default: return 0; } } unsigned int getBufferSize() { switch( profile ) { case vetFrame::VETFRAME_MONO: return width * height * sizeof(T); case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: return width * height * 3 * sizeof(T); case vetFrame::VETFRAME_RGB96: case vetFrame::VETFRAME_BGR96: return width * height * 3 * sizeof(T); case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: return width * height * 4 * sizeof(T); //BUG TODO default: return 0; } } VETRESULT setBlack() { switch( profile ) { case vetFrame::VETFRAME_MONO: case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: case vetFrame::VETFRAME_RGB96: case vetFrame::VETFRAME_BGR96: case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: { memset(data, '\0', getBufferSize() ); return VETRET_OK; } default: return VETRET_NOT_IMPLEMENTED; } } VETRESULT setWhite() { switch( profile ) { case vetFrame::VETFRAME_MONO: case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: case vetFrame::VETFRAME_RGB96: case vetFrame::VETFRAME_BGR96: { memset(data, 255, getBufferSize() ); return VETRET_OK; } case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: { memset(data, '\0', width*height ); return VETRET_OK; } case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: { memset(data+width*height*3, '\0', width*height ); return VETRET_OK; } default: return VETRET_NOT_IMPLEMENTED; } } /** * @brief Set pixel (x, y) to the specified value. * * @param x x position of the pixel. * @param y y position of the pixel. * @param p new value for the selected coords. * * @note No check is made that x and y are in range. */ VETRESULT setPixel(unsigned int x, unsigned int y, T p) { #ifdef _VETFRAMECACHE_SLOWMODE if ( x >= width || y >= height ) return VETRET_PARAM_ERR; // throw "Invalid Coordinates in method vetFrameCache::setPixel(unsigned int x, unsigned int y, PixelRGB p)"; #endif //_VETFRAMECACHE_SLOWMODE // if ( channelType == VETFRAMET_CT_PIXELPACKED) // data[y * width + x] = p; // else return VETRET_NOT_IMPLEMENTED; //BUG TODO return VETRET_OK; } /** * @brief Get pixel (x, y) value and store it to p. * * @param x x position of the pixel. * @param y y position of the pixel. * @param p address to store selected pixel's value. * * @note No check is made that x and y are in range. */ VETRESULT getPixel(unsigned int x, unsigned int y, T& p) { #ifdef _VETFRAMECACHE_SLOWMODE if ( x >= width || y >= height ) return VETRET_PARAM_ERR; // throw "Invalid Coordinates in method vetFrameCache::getPixel(unsigned int x, unsigned int y, PixelRGB& p)"; #endif //_VETFRAMECACHE_SLOWMODE // if ( channelType == VETFRAMET_CT_PIXELPACKED) // p = data[y * width + x]; // else return VETRET_NOT_IMPLEMENTED; //BUG TODO return VETRET_OK; } /** * @brief Copies all pixel data from img. * Throws an exception if images are of different size. * * @param img The image to copy the data from. * * @return current instance. */ vetFrameT& operator = (vetFrameT& img) { /* check we're not trying to copy ourself */ if (this == &img) throw "Source and Destination are same Image"; /* First check images are the same size and valid */ if ( width != img.width || height != img.height || profile != img.profile ) { profile = img.profile; dataType = img.dataType; reAllocCanvas(img.width, img.height); } if ( width == 0 || height == 0 ) throw "Cannot do that with empty image (no size)"; memcpy(static_cast(data), static_cast(img.data), getBufferSize() ); return *this; } /** * @brief Overload equals-add (+=) operator for two images (pixel += loop) * Throws an exception if images are of different size. * * @param img The image to add to current data. * * @return current instance. */ vetFrameT& operator += (vetFrameT& img) { INFO("vetFrameCache& vetFrameCache::operator += (vetFrameCache& img)") if (width != img.width || height != img.height) throw "Difference in vetFrameCache Dimensions"; if ( dataType != img.dataType || profile != img.profile ) throw "Difference in vetFrameT data structure"; // for(unsigned int i=0; i < width * height; i++) // data[i] += img.data[i]; //BUG TODO return *this; } // void operator << (const vetFrame& img); // void operator << (const vetFrame& img); // vetFrameT& operator >> (vetFrame& img); // vetFrameT& operator >> (vetFrame& img); VETFRAME_PROFILE getProfile() { return profile; }; VETFRAME_CHANNEL_TYPE getChannelType() { return dataType; }; int getFOURCC() { switch( profile ) { case vetFrame::VETFRAME_MONO: case vetFrame::VETFRAME_RGB24: case vetFrame::VETFRAME_BGR24: case vetFrame::VETFRAME_RGB32: case vetFrame::VETFRAME_BGR32: return 0x32424752; case vetFrame::VETFRAME_ARGB32: case vetFrame::VETFRAME_ABGR32: return 0x41424752; case vetFrame::VETFRAME_RGBA32: case vetFrame::VETFRAME_BGRA32: //BUG TODO default: return 0; } }; }; #endif //__VETLIB_VETFRAMETH__