| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| namespace std {} |
| using namespace std; |
|
|
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string> |
| #include "RefCntMem.h" |
|
|
| #ifdef WIN32 |
| #include <typeinfo.h> |
| #else |
| #include <typeinfo> |
| #define __max(a,b) (((a) > (b)) ? (a) : (b)) |
| #define __min(a,b) (((a) < (b)) ? (a) : (b)) |
| #endif |
|
|
| #include <float.h> |
| #ifndef FLT_MAX |
| #define FLT_MAX 3.402823466e+38F |
| #define FLT_MIN 1.175494351e-38F |
| #endif |
|
|
| typedef unsigned char uchar; |
|
|
|
|
| |
|
|
| struct CShape |
| { |
| int width, height; |
| int nBands; |
| |
| |
| CShape(void) : width(0), height(0), nBands(0) {} |
| CShape(int w, int h, int nb) : width(w), height(h), nBands(nb) {} |
| bool InBounds(int x, int y); |
| bool InBounds(int x, int y, int band); |
| bool operator==(const CShape& ref); |
| bool SameIgnoringNBands(const CShape& ref); |
| bool operator!=(const CShape& ref); |
| }; |
|
|
| inline bool CShape::InBounds(int x, int y) |
| { |
| |
| return (0 <= x && x < width && |
| 0 <= y && y < height); |
| } |
|
|
| inline bool CShape::InBounds(int x, int y, int b) |
| { |
| |
| return (0 <= x && x < width && |
| 0 <= y && y < height && |
| 0 <= b && b < nBands); |
| } |
|
|
|
|
| |
|
|
| enum EBorderMode |
| { |
| eBorderZero = 0, |
| eBorderReplicate = 1, |
| eBorderReflect = 2, |
| eBorderCyclic = 3 |
| }; |
|
|
| |
|
|
| struct CImageAttributes |
| { |
| int alphaChannel; |
| int origin[2]; |
| EBorderMode borderMode; |
| |
| }; |
|
|
|
|
| |
|
|
| class CImage : public CImageAttributes |
| { |
| public: |
| CImage(void); |
| CImage(CShape s, const type_info& ti, int bandSize); |
| |
|
|
| void ReAllocate(CShape s, const type_info& ti, int bandSize, |
| void *memory, bool deleteWhenDone, int rowSize); |
| void ReAllocate(CShape s, const type_info& ti, int bandSize, |
| bool evenIfSameShape = false); |
| void DeAllocate(void); |
|
|
| CShape Shape(void) { return m_shape; } |
| const type_info& PixType(void) { return *m_pTI; } |
| int BandSize(void) { return m_bandSize; } |
|
|
| void* PixelAddress(int x, int y, int band); |
|
|
| void SetSubImage(int xO, int yO, int width, int height); |
|
|
| protected: |
| void SetPixels(void *val_ptr); |
|
|
| private: |
| void SetDefaults(void); |
|
|
| CShape m_shape; |
| const type_info* m_pTI; |
| int m_bandSize; |
| int m_pixSize; |
| int m_rowSize; |
| char* m_memStart; |
| CRefCntMem m_memory; |
| public: |
| int alphaChannel; |
| }; |
|
|
| inline void* CImage::PixelAddress(int x, int y, int band) |
| { |
| |
| return (void *) &m_memStart[y * m_rowSize + x * m_pixSize + band * m_bandSize]; |
| } |
|
|
|
|
| |
|
|
| template <class T> |
| class CImageOf : public CImage |
| { |
| public: |
| CImageOf(void); |
| CImageOf(CShape s); |
| CImageOf(int width, int height, int nBands); |
| |
|
|
| void ReAllocate(CShape s, bool evenIfSameShape = false); |
| void ReAllocate(CShape s, T *memory, bool deleteWhenDone, int rowSize); |
|
|
| T& Pixel(int x, int y, int band); |
|
|
| CImageOf SubImage(int x, int y, int width, int height); |
|
|
| void FillPixels(T val); |
| void ClearPixels(void); |
|
|
| T MinVal(void); |
| T MaxVal(void); |
| }; |
|
|
| |
|
|
| template <class T> |
| inline CImageOf<T>::CImageOf(void) : |
| CImage(CShape(), typeid(T), sizeof(T)) {} |
|
|
| template <class T> |
| inline CImageOf<T>::CImageOf(CShape s) : |
| CImage(s, typeid(T), sizeof(T)) {} |
|
|
| template <class T> |
| inline CImageOf<T>::CImageOf(int width, int height, int nBands) : |
| CImage(CShape(width, height, nBands), typeid(T), sizeof(T)) {} |
|
|
| template <class T> |
| inline void CImageOf<T>::ReAllocate(CShape s, bool evenIfSameShape) |
| { |
| CImage::ReAllocate(s, typeid(T), sizeof(T), evenIfSameShape); |
| } |
|
|
| template <class T> |
| inline void CImageOf<T>::ReAllocate(CShape s, T *memory, |
| bool deleteWhenDone, int rowSize) |
| { |
| CImage::ReAllocate(s, typeid(T), sizeof(T), memory, deleteWhenDone, rowSize); |
| } |
| |
| template <class T> |
| inline T& CImageOf<T>::Pixel(int x, int y, int band) |
| { |
| return *(T *) PixelAddress(x, y, band); |
| } |
|
|
| template <class T> |
| inline CImageOf<T> CImageOf<T>::SubImage(int x, int y, int width, int height) |
| { |
| |
| CImageOf<T> retval = *this; |
| retval.SetSubImage(x, y, width, height); |
| return retval; |
| } |
|
|
| template <class T> |
| inline void CImageOf<T>::FillPixels(T val) |
| { |
| |
| SetPixels(&val); |
| } |
|
|
| template <class T> |
| inline void CImageOf<T>::ClearPixels(void) |
| { |
| |
| T val = 0; |
| FillPixels(val); |
| } |
|
|
| |
|
|
| typedef CImageOf<uchar> CByteImage; |
| typedef CImageOf<int> CIntImage; |
| typedef CImageOf<float> CFloatImage; |
|
|
| |
|
|
| template <class PixType> |
| struct RGBA |
| { |
| PixType B, G, R, A; |
| }; |
|
|