// Modified by:
// Created: 10.03.03
// RCS-ID: $Id$
-// Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwindows.org>
+// Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_RAWBMP_H_BASE_
#define _WX_RAWBMP_H_BASE_
+#include "wx/image.h"
+
// ----------------------------------------------------------------------------
// Abstract Pixel API
//
- index of the alpha component or -1 if none
- type which can contain the full pixel value (all channels)
*/
-template <typename Channel,
+
+template <class Channel,
size_t Bpp, int R, int G, int B, int A = -1,
- typename Pixel = wxUint32>
+ class Pixel = wxUint32>
+
struct WXDLLEXPORT wxPixelFormat
{
// iterator over pixels is usually of type "ChannelType *"
enum { BitsPerPixel = Bpp };
// size of one pixel in ChannelType units (usually bytes)
- enum { SizePixel = BitsPerPixel / (8 * sizeof(ChannelType)) };
+ enum { SizePixel = Bpp / (8 * sizeof(Channel)) };
// the channels indices inside the pixel
enum
typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxImagePixelFormat;
// the (most common) native bitmap format without alpha support
-typedef wxPixelFormat<unsigned char, 24,
- #ifdef __WXMSW__
- 2, 1, 0
- #else // !__WXMSW__
- 0, 1, 2
- #endif // __WXMSW__/!__WXMSW__
- > wxNativePixelFormat;
+#if defined(__WXMSW__)
+ // under MSW the RGB components are inversed, they're in BGR order
+ typedef wxPixelFormat<unsigned char, 24, 2, 1, 0> wxNativePixelFormat;
+
+ #define wxPIXEL_FORMAT_ALPHA 3
+#elif defined(__WXMAC__)
+ // under Mac, first component is unused but still present, hence we use
+ // 32bpp, not 24
+ typedef wxPixelFormat<unsigned char, 32, 1, 2, 3> wxNativePixelFormat;
+
+ #define wxPIXEL_FORMAT_ALPHA 0
+#elif defined(__WXCOCOA__)
+ // Cocoa is standard RGB or RGBA (normally it is RGBA)
+ typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
+
+ #define wxPIXEL_FORMAT_ALPHA 3
+#endif
// the (most common) native format for bitmaps with alpha channel
-typedef wxPixelFormat<unsigned char, 32,
- wxNativePixelFormat::RED,
- wxNativePixelFormat::GREEN,
- wxNativePixelFormat::BLUE,
- 3> wxAlphaPixelFormat;
+#ifdef wxPIXEL_FORMAT_ALPHA
+ typedef wxPixelFormat<unsigned char, 32,
+ wxNativePixelFormat::RED,
+ wxNativePixelFormat::GREEN,
+ wxNativePixelFormat::BLUE,
+ wxPIXEL_FORMAT_ALPHA> wxAlphaPixelFormat;
+#endif // wxPIXEL_FORMAT_ALPHA
// we also define the (default/best) pixel format for the given class: this is
// used as default value for the pixel format in wxPixelIterator template
template <class T> struct wxPixelFormatFor;
+#if wxUSE_IMAGE
// wxPixelFormatFor is only defined for wxImage, attempt to use it with other
// classes (wxBitmap...) will result in compile errors which is exactly what we
// want
{
typedef wxImagePixelFormat Format;
};
+#endif //wxUSE_IMAGE
// ----------------------------------------------------------------------------
// wxPixelData
class WXDLLEXPORT wxPixelDataBase
{
public:
+ // origin of the rectangular region we represent
+ wxPoint GetOrigin() const { return m_ptOrigin; }
+
+ // width and height of the region we represent
int GetWidth() const { return m_width; }
int GetHeight() const { return m_height; }
+
+ wxSize GetSize() const { return wxSize(m_width, m_height); }
+
+ // the distance between two rows
int GetRowStride() const { return m_stride; }
// private: -- see comment in the beginning of the file
+ // the origin of this image inside the bigger bitmap (usually (0, 0))
+ wxPoint m_ptOrigin;
+
// the size of the image we address, in pixels
int m_width,
m_height;
};
};
+#if wxUSE_IMAGE
// wxPixelData specialization for wxImage: this is the simplest case as we
// don't have to care about different pixel formats here
template <>
{
// NB: this is a template class even though it doesn't use its template
// parameter because otherwise wxPixelData couldn't compile
- template <class PixelFormat>
+ template <class dummyPixelFormat>
class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
{
// the type of the class we're working with
typedef wxImagePixelFormat PixelFormat;
// the type of the pixel components
- typedef typename PixelFormat::ChannelType ChannelType;
+ typedef typename dummyPixelFormat::ChannelType ChannelType;
// the pixel data we're working with
typedef
{
m_pRGB += PixelFormat::SizePixel;
if ( m_pAlpha )
- m_pAlpha += PixelFormat::SizePixel;
+ ++m_pAlpha;
return *this;
}
- // postfix (hence less efficient -- don't use unless you must) version
+ // postfix (hence less efficient -- don't use it unless you
+ // absolutely must) version
Iterator operator++(int)
{
Iterator p(*this);
// -----------
// access to invidividual colour components
- ChannelType& Red() { return m_ptr[PixelFormat::RED]; }
- ChannelType& Green() { return m_ptr[PixelFormat::GREEN]; }
- ChannelType& Blue() { return m_ptr[PixelFormat::BLUE]; }
+ ChannelType& Red() { return m_pRGB[PixelFormat::RED]; }
+ ChannelType& Green() { return m_pRGB[PixelFormat::GREEN]; }
+ ChannelType& Blue() { return m_pRGB[PixelFormat::BLUE]; }
ChannelType& Alpha() { return *m_pAlpha; }
// private: -- see comment in the beginning of the file
m_stride = Iterator::SizePixel * m_width;
}
+ // initializes us with the given region of the specified image
+ wxPixelDataIn(ImageType& image,
+ const wxPoint& pt,
+ const wxSize& sz) : m_image(image), m_pixels(image)
+ {
+ m_stride = Iterator::SizePixel * m_width;
+
+ InitRect(pt, sz);
+ }
+
+ // initializes us with the given region of the specified image
+ wxPixelDataIn(ImageType& image,
+ const wxRect& rect) : m_image(image), m_pixels(image)
+ {
+ m_stride = Iterator::SizePixel * m_width;
+
+ InitRect(rect.GetPosition(), rect.GetSize());
+ }
+
// we evaluate to true only if we could get access to bitmap data
// successfully
operator bool() const { return m_pixels.IsOk(); }
Iterator GetPixels() const { return m_pixels; }
private:
+ void InitRect(const wxPoint& pt, const wxSize& sz)
+ {
+ m_width = sz.x;
+ m_height = sz.y;
+
+ m_ptOrigin = pt;
+ m_pixels.Offset(*this, pt.x, pt.y);
+ }
+
// the image we're working with
ImageType& m_image;
Iterator m_pixels;
};
};
+#endif //wxUSE_IMAGE
+#if wxUSE_GUI
// wxPixelData specialization for wxBitmap: here things are more interesting as
// we also have to support different pixel formats
template <>
// the type of the class we're working with
typedef wxBitmap ImageType;
- class Iterator
+ class WXDLLEXPORT Iterator
{
public:
// the pixel format we use
*this = data.GetPixels();
}
- // initializes the iterator to point to the origin of the given pixel
- // data
+ // initializes the iterator to point to the origin of the given
+ // pixel data
Iterator(PixelData& data)
{
Reset(data);
}
- // initializes the iterator to point to the origin of the given bitmap
+ // initializes the iterator to point to the origin of the given
+ // bitmap
Iterator(wxBitmap& bmp, PixelData& data)
{
- // using cast here is ugly but it should be safe as GetRawData()
- // real return type should be consistent with BitsPerPixel (which
- // is in turn defined by ChannelType) and this is the only thing we
- // can do without making GetRawData() a template function which is
- // undesirable
+ // using cast here is ugly but it should be safe as
+ // GetRawData() real return type should be consistent with
+ // BitsPerPixel (which is in turn defined by ChannelType) and
+ // this is the only thing we can do without making GetRawData()
+ // a template function which is undesirable
m_ptr = (ChannelType *)
bmp.GetRawData(data, PixelFormat::BitsPerPixel);
}
return *this;
}
- // postfix (hence less efficient -- don't use unless you must) version
+ // postfix (hence less efficient -- don't use it unless you
+ // absolutely must) version
Iterator operator++(int)
{
Iterator p(*this);
// private: -- see comment in the beginning of the file
- // NB: for efficiency reasons this class must *not* have any other
- // fields, otherwise it won't be put into a CPU register (as it
- // should inside the inner loops) by some compilers, notably gcc
+ // for efficiency reasons this class should not have any other
+ // fields, otherwise it won't be put into a CPU register (as it
+ // should inside the inner loops) by some compilers, notably gcc
ChannelType *m_ptr;
};
- // ctor associates this pointer with a bitmap and locks the bitmap for raw
- // access, it will be unlocked only by our dtor and so these objects should
- // normally be only created on the stack, i.e. have limited life-time
- wxPixelDataIn(wxBitmap bmp) : m_bmp(bmp), m_pixels(bmp, *this)
+ // ctor associates this pointer with a bitmap and locks the bitmap for
+ // raw access, it will be unlocked only by our dtor and so these
+ // objects should normally be only created on the stack, i.e. have
+ // limited life-time
+ wxPixelDataIn(wxBitmap& bmp) : m_bmp(bmp), m_pixels(bmp, *this)
+ {
+ }
+
+ wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
+ : m_bmp(bmp), m_pixels(bmp, *this)
{
+ InitRect(rect.GetPosition(), rect.GetSize());
+ }
+
+ wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
+ : m_bmp(bmp), m_pixels(bmp, *this)
+ {
+ InitRect(pt, sz);
}
// we evaluate to true only if we could get access to bitmap data
// the iterator pointing to the image origin
Iterator m_pixels;
+
+ private:
+ void InitRect(const wxPoint& pt, const wxSize& sz)
+ {
+ m_pixels.Offset(*this, pt.x, pt.y);
+
+ m_ptOrigin = pt;
+ m_width = sz.x;
+ m_height = sz.y;
+ }
};
};
+#endif //wxUSE_GUI
+
+#ifdef __VISUALC__
+ // typedef-name 'foo' used as synonym for class-name 'bar'
+ // (VC++ gives this warning each time wxPixelData::Base is used but it
+ // doesn't make any sense here -- what's wrong with using typedef instead
+ // of class, this is what it is here for!)
+ #pragma warning(disable: 4097)
+#endif // __VISUALC__
template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
class wxPixelData :
public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
{
public:
- wxPixelData(const Image& image)
- : wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>(image)
- {
- }
+ typedef
+ typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
+ Base;
+
+ wxPixelData(Image& image) : Base(image) { }
+
+ wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
+
+ wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
+ : Base(i, pt, sz)
+ {
+ }
};
+
// some "predefined" pixel data classes
+#if wxUSE_IMAGE
typedef wxPixelData<wxImage> wxImagePixelData;
+#endif //wxUSE_IMAGE
+#if wxUSE_GUI
typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
+#endif //wxUSE_GUI
// ----------------------------------------------------------------------------
// wxPixelIterator
#ifdef __VISUALC__
#pragma warning(default: 4355)
+ #pragma warning(default: 4097)
#endif
#endif // _WX_RAWBMP_H_BASE_