/* * Copyright (c) 2004, 2005. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef __BLEPO_IMAGE_H__ #define __BLEPO_IMAGE_H__ #include "assert.h" #include "stdafx.h" // CDC, HDC namespace blepo { /** @class Reallocator Reallocates an array of basic types or structs, without calling the constructor on those types. @author Stan Birchfield (STB) */ template class Reallocator { public: typedef T Type; Reallocator() : m_nalloc(0), m_first(0), m_last(0) {} Reallocator(int n) : m_nalloc(0), m_first(0), m_last(0) { Reset(n); } Reallocator(const Reallocator& other) : m_nalloc(0), m_first(0), m_last(0) { *this = other; } virtual ~Reallocator() { free(m_first); } void Reset() { Reset(0); } void Reset(int n) { m_first = static_cast( realloc(m_first, n*sizeof(T)) ); m_last = m_first + n; m_nalloc = n; // If we tried to allocate memory, make sure we did if (m_nalloc>0 && m_first==0) { assert(0); // BLEPO_ERROR("Out of memory"); } } Reallocator& operator=(const Reallocator& other) { Reset(other.m_nalloc); memcpy(m_first, other.m_first, m_nalloc*sizeof(T)); return *this; } int GetN() const { return m_nalloc; } T& operator[](int indx) { assert(indx>=0 && indx=0 && indx 8-bit gray-level images Image 24-bit BGR images Image single-precision floating-point images Image gray-level images with sizeof(int) bytes per pixel etc. Packed binary images (one bit per pixel) require specialization because sizeof(bool)==1, i.e., a bool occupies a byte of memory even though it needs only a bit. @author Stan Birchfield (STB) */ template class Image { public: /// @name Typedefs //@{ typedef T PixelType; typedef T* Iterator; typedef const T* ConstIterator; //@} /// @name Constants //@{ static const int NBITS_PER_PIXEL; ///< number of bits per pixel static const int NCHANNELS; ///< number of channels static const PixelType MIN_VAL; ///< minimum pixel value static const PixelType MAX_VAL; ///< maximum pixel value //@} public: /// Constructor / destructor / copy constructor //@{ Image(int width, int height) : m_width(0), m_height(0), m_data() { Reset(width, height); } Image() : m_width(0), m_height(0), m_data() {} Image(const Image& other) : m_width(0), m_height(0), m_data() { *this = other; } virtual ~Image() {} //@} /// Assignment operator Image& operator=(const Image& other) { m_width = other.m_width; m_height = other.m_height; m_data = other.m_data; return *this; } /// @name Image reinitialization /// After calling Reset(), class object will be in the exact same state as if you /// were to instantiate a new object by calling the constructor with those parameters. /// Notice that the parameters for Reset() are identical to those for the constructor. //@{ void Reset(int width, int height) { m_width = width; m_height = height; m_data.Reset(width*height); } void Reset() { Reset(0,0); } //@} public: /// @name Image info //@{ /// returns number of pixels in a row of the image int Width() const { return m_width; } /// returns number of pixels in a column of the image int Height() const { return m_height; } /// returns the number of bytes used to hold this image int NBytes() const { return m_width*m_height*sizeof(T); } //@} /// @name Pixel accessing functions (inefficient but convenient) //@{ const PixelType& operator()(int x, int y) const { return *(Begin(x, y)); } PixelType& operator()(int x, int y) { return *(Begin(x, y)); } //@} /// @name Iterator functions for fast pixel accessing //@{ ConstIterator Begin() const { return m_data.Begin(); } ConstIterator End() const { return m_data.End(); } ConstIterator Begin(int x, int y) const { assert(x>=0 && x=0 && y=0 && x=0 && y(m_data.Begin()); } unsigned char* BytePtr() { return reinterpret_cast(m_data.Begin()); } //@} private: int m_width, m_height; ///< image dimensions Reallocator m_data; //< image data }; /// A struct to hold three bytes for red, green, and blue color values. struct Bgr { unsigned char b, g, r; Bgr(unsigned char blue, unsigned char green, unsigned char red) : b(blue), g(green), r(red) {} bool operator==(const Bgr& other) const { return b==other.b && g==other.g && r==other.r; } bool operator!=(const Bgr& other) const { return b!=other.b || g!=other.g || r!=other.r; } }; /** @class ImgBgr A color image in the blue-green-red (BGR) color space. Each pixel occupies 3 bytes, with colors interleaved, i.e., B0G0R0B1G1R1... @author Stan Birchfield (STB) */ typedef Image ImgBgr; // loading / saving an image void Load(const char* filename, ImgBgr* out); void Draw(const ImgBgr& img, HDC hdc, int x, int y); void Draw(const ImgBgr& img, CDC& dc, int x, int y); void Draw(const ImgBgr& img, HDC hdc, const CRect& src, const CRect& dst); void Draw(const ImgBgr& img, CDC& dc, const CRect& src, const CRect& dst); }; // end namespace blepo #endif //__BLEPO_IMAGE_H__