2003-03-22 14:00:48 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/rawbmp.h
|
|
|
|
// Purpose: macros for fast, raw bitmap data access
|
|
|
|
// Author: Eric Kidd, Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 10.03.03
|
|
|
|
// RCS-ID: $Id$
|
2004-05-23 10:56:36 -04:00
|
|
|
// Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwidgets.org>
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2003-03-22 14:00:48 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_RAWBMP_H_BASE_
|
|
|
|
#define _WX_RAWBMP_H_BASE_
|
|
|
|
|
2004-07-23 13:49:51 -04:00
|
|
|
#include "wx/image.h"
|
|
|
|
|
2003-03-22 14:00:48 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Abstract Pixel API
|
|
|
|
//
|
|
|
|
// We need to access our raw bitmap data (1) portably and (2) efficiently.
|
|
|
|
// We do this using a two-dimensional "iteration" interface. Performance
|
|
|
|
// is extremely important here: these functions will be called hundreds
|
|
|
|
// of thousands of times in a row, and even small inefficiencies will
|
|
|
|
// make applications seem slow.
|
|
|
|
//
|
|
|
|
// We can't always rely on inline functions, because not all compilers actually
|
|
|
|
// bother to inline them unless we crank the optimization levels way up.
|
|
|
|
// Therefore, we also provide macros to wring maximum speed out of compiler
|
|
|
|
// unconditionally (e.g. even in debug builds). Of course, if the performance
|
|
|
|
// isn't absolutely crucial for you you shouldn't be using them but the inline
|
|
|
|
// functions instead.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Usage example:
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
typedef wxPixelData<wxBitmap, wxNativePixelFormat> PixelData;
|
|
|
|
|
2003-03-22 14:00:48 -05:00
|
|
|
wxBitmap bmp;
|
2003-04-27 21:46:05 -04:00
|
|
|
PixelData data(bmp);
|
2003-03-22 14:00:48 -05:00
|
|
|
if ( !data )
|
|
|
|
{
|
|
|
|
... raw access to bitmap data unavailable, do something else ...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
|
|
|
... complain: the bitmap it too small ...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
PixelData::Iterator p(data);
|
2003-03-22 14:00:48 -05:00
|
|
|
|
|
|
|
// we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
|
2003-04-27 21:46:05 -04:00
|
|
|
p.Offset(data, 10, 10);
|
2003-03-22 14:00:48 -05:00
|
|
|
|
|
|
|
for ( int y = 0; y < 10; ++y )
|
|
|
|
{
|
2003-04-27 21:46:05 -04:00
|
|
|
PixelData::Iterator rowStart = p;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
|
|
|
for ( int x = 0; x < 10; ++x, ++p )
|
|
|
|
{
|
|
|
|
p.Red() = r;
|
|
|
|
p.Green() = g;
|
|
|
|
p.Blue() = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = rowStart;
|
2003-04-27 21:46:05 -04:00
|
|
|
p.OffsetY(data, 1);
|
2003-03-22 14:00:48 -05:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2005-03-01 12:49:27 -05:00
|
|
|
/*
|
|
|
|
Note: we do not use WXDLLEXPORT with classes in this file because VC++ has
|
|
|
|
problems with exporting inner class defined inside a specialization of a
|
|
|
|
template class from a DLL. Besides, as all the methods are inline it's not
|
|
|
|
really necessary to put them in DLL at all.
|
|
|
|
*/
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxPixelFormat
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
wxPixelFormat is a template class describing the bitmap data format. It
|
|
|
|
contains the constants describing the format of pixel data, but does not
|
|
|
|
describe how the entire bitmap is stored (i.e. top-to-bottom,
|
|
|
|
bottom-to-top, ...). It is also a "traits"-like class, i.e. it only
|
|
|
|
contains some constants and maybe static methods but nothing more, so it
|
|
|
|
can be safely used without incurring any overhead as all accesses to it are
|
|
|
|
done at compile-time.
|
|
|
|
|
|
|
|
Current limitations: we don't support RAGABA and ARAGAB formats supported
|
|
|
|
by Mac OS X. If there is sufficient interest, these classes could be
|
|
|
|
extended to deal with them. Neither do we support alpha channel having
|
|
|
|
different representation from the RGB ones (happens under QNX/Photon I
|
|
|
|
think), but again this could be achieved with some small extra effort.
|
|
|
|
|
|
|
|
Template parameters are:
|
|
|
|
- type of a single pixel component
|
|
|
|
- size of the single pixel in bits
|
|
|
|
- indices of red, green and blue pixel components inside the pixel
|
|
|
|
- index of the alpha component or -1 if none
|
|
|
|
- type which can contain the full pixel value (all channels)
|
|
|
|
*/
|
2003-05-06 11:08:52 -04:00
|
|
|
|
|
|
|
template <class Channel,
|
|
|
|
size_t Bpp, int R, int G, int B, int A = -1,
|
|
|
|
class Pixel = wxUint32>
|
2003-06-03 20:50:18 -04:00
|
|
|
|
2005-03-01 12:49:27 -05:00
|
|
|
struct wxPixelFormat
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
2003-04-27 21:46:05 -04:00
|
|
|
// iterator over pixels is usually of type "ChannelType *"
|
|
|
|
typedef Channel ChannelType;
|
|
|
|
|
|
|
|
// the type which may hold the entire pixel value
|
|
|
|
typedef Pixel PixelType;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// NB: using static ints initialized inside the class declaration is not
|
|
|
|
// portable as it doesn't work with VC++ 6, so we must use enums
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// size of one pixel in bits
|
|
|
|
enum { BitsPerPixel = Bpp };
|
|
|
|
|
|
|
|
// size of one pixel in ChannelType units (usually bytes)
|
2003-06-03 20:50:18 -04:00
|
|
|
enum { SizePixel = Bpp / (8 * sizeof(Channel)) };
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
// the channels indices inside the pixel
|
|
|
|
enum
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
2003-04-27 21:46:05 -04:00
|
|
|
RED = R,
|
|
|
|
GREEN = G,
|
|
|
|
BLUE = B,
|
|
|
|
ALPHA = A
|
|
|
|
};
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// true if we have an alpha channel (together with the other channels, this
|
|
|
|
// doesn't cover the case of wxImage which stores alpha separately)
|
|
|
|
enum { HasAlpha = A != -1 };
|
|
|
|
};
|
|
|
|
|
|
|
|
// some "predefined" pixel formats
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
// wxImage format is common to all platforms
|
|
|
|
typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxImagePixelFormat;
|
|
|
|
|
|
|
|
// the (most common) native bitmap format without alpha support
|
2003-06-03 20:50:18 -04:00
|
|
|
#if defined(__WXMSW__)
|
2005-05-31 05:20:43 -04:00
|
|
|
// under MSW the RGB components are reversed, they're in BGR order
|
2003-06-03 20:50:18 -04:00
|
|
|
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
|
2003-12-06 18:20:46 -05:00
|
|
|
#elif defined(__WXCOCOA__)
|
|
|
|
// Cocoa is standard RGB or RGBA (normally it is RGBA)
|
|
|
|
typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
|
|
|
|
|
2005-02-13 10:35:43 -05:00
|
|
|
#define wxPIXEL_FORMAT_ALPHA 3
|
|
|
|
#elif defined(__WXGTK__)
|
2006-10-06 12:34:45 -04:00
|
|
|
// Under GTK+ 2.X we use GdkPixbuf, which is standard RGB or RGBA
|
|
|
|
typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
|
2005-02-13 10:35:43 -05:00
|
|
|
|
2003-12-06 18:20:46 -05:00
|
|
|
#define wxPIXEL_FORMAT_ALPHA 3
|
2003-06-03 20:50:18 -04:00
|
|
|
#endif
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
// the (most common) native format for bitmaps with alpha channel
|
2003-06-03 20:50:18 -04:00
|
|
|
#ifdef wxPIXEL_FORMAT_ALPHA
|
|
|
|
typedef wxPixelFormat<unsigned char, 32,
|
|
|
|
wxNativePixelFormat::RED,
|
|
|
|
wxNativePixelFormat::GREEN,
|
|
|
|
wxNativePixelFormat::BLUE,
|
|
|
|
wxPIXEL_FORMAT_ALPHA> wxAlphaPixelFormat;
|
|
|
|
#endif // wxPIXEL_FORMAT_ALPHA
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2004-12-09 20:53:31 -05:00
|
|
|
#if wxUSE_IMAGE
|
2003-04-27 21:46:05 -04:00
|
|
|
// 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
|
|
|
|
template <>
|
2005-03-01 12:49:27 -05:00
|
|
|
struct wxPixelFormatFor<wxImage>
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
typedef wxImagePixelFormat Format;
|
|
|
|
};
|
2004-12-09 20:53:31 -05:00
|
|
|
#endif //wxUSE_IMAGE
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxPixelData
|
|
|
|
// ----------------------------------------------------------------------------
|
2003-04-13 09:45:20 -04:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
/*
|
|
|
|
wxPixelDataBase is just a helper for wxPixelData: it contains things common
|
|
|
|
to both wxImage and wxBitmap specializations.
|
|
|
|
*/
|
2005-03-01 12:49:27 -05:00
|
|
|
class wxPixelDataBase
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
public:
|
2003-05-01 12:37:51 -04:00
|
|
|
// origin of the rectangular region we represent
|
|
|
|
wxPoint GetOrigin() const { return m_ptOrigin; }
|
|
|
|
|
|
|
|
// width and height of the region we represent
|
2003-03-22 14:00:48 -05:00
|
|
|
int GetWidth() const { return m_width; }
|
|
|
|
int GetHeight() const { return m_height; }
|
2003-05-01 12:37:51 -04:00
|
|
|
|
2003-06-14 08:57:44 -04:00
|
|
|
wxSize GetSize() const { return wxSize(m_width, m_height); }
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// the distance between two rows
|
2003-03-22 14:00:48 -05:00
|
|
|
int GetRowStride() const { return m_stride; }
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// private: -- see comment in the beginning of the file
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// the origin of this image inside the bigger bitmap (usually (0, 0))
|
|
|
|
wxPoint m_ptOrigin;
|
|
|
|
|
2003-03-22 14:00:48 -05:00
|
|
|
// the size of the image we address, in pixels
|
|
|
|
int m_width,
|
|
|
|
m_height;
|
|
|
|
|
|
|
|
// this parameter is the offset of the start of the (N+1)st row from the
|
|
|
|
// Nth one and can be different from m_bypp*width in some cases:
|
|
|
|
// a) the most usual one is to force 32/64 bit alignment of rows
|
|
|
|
// b) another one is for bottom-to-top images where it's negative
|
|
|
|
// c) finally, it could conceivably be 0 for the images with all
|
|
|
|
// lines being identical
|
|
|
|
int m_stride;
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
protected:
|
|
|
|
// ctor is protected because this class is only meant to be used as the
|
|
|
|
// base class by wxPixelData
|
|
|
|
wxPixelDataBase()
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
2003-04-27 21:46:05 -04:00
|
|
|
m_width =
|
|
|
|
m_height =
|
|
|
|
m_stride = 0;
|
2003-03-22 14:00:48 -05:00
|
|
|
}
|
2003-04-27 21:46:05 -04:00
|
|
|
};
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
/*
|
|
|
|
wxPixelData represents the entire bitmap data, i.e. unlike
|
|
|
|
wxPixelFormat (which it uses) it also stores the global bitmap
|
|
|
|
characteristics such as its size, inter-row separation and so on.
|
|
|
|
|
|
|
|
Because of this it can be used to move the pixel iterators (which don't
|
|
|
|
have enough information about the bitmap themselves). This may seem a bit
|
|
|
|
unnatural but must be done in this way to keep the iterator objects as
|
|
|
|
small as possible for maximum efficiency as otherwise they wouldn't be put
|
|
|
|
into the CPU registers by the compiler any more.
|
|
|
|
|
|
|
|
Implementation note: we use the standard workaround for lack of partial
|
|
|
|
template specialization support in VC (both 6 and 7): instead of partly
|
|
|
|
specializing the class Foo<T, U> for some T we introduce FooOut<T> and
|
|
|
|
FooIn<U> nested in it, make Foo<T, U> equivalent to FooOut<T>::FooIn<U> and
|
|
|
|
fully specialize FooOut.
|
|
|
|
|
|
|
|
Also note that this class doesn't have any default definition because we
|
|
|
|
can't really do anything without knowing the exact image class. We do
|
|
|
|
provide wxPixelDataBase to make it simpler to write new wxPixelData
|
|
|
|
specializations.
|
|
|
|
*/
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// we need to define this skeleton template to mollify VC++
|
|
|
|
template <class Image>
|
2005-03-01 12:49:27 -05:00
|
|
|
struct wxPixelDataOut
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
template <class PixelFormat>
|
2005-03-01 12:49:27 -05:00
|
|
|
class wxPixelDataIn
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
2003-04-27 21:46:05 -04:00
|
|
|
public:
|
|
|
|
class Iterator { };
|
|
|
|
};
|
|
|
|
};
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2004-12-09 20:53:31 -05:00
|
|
|
#if wxUSE_IMAGE
|
2003-04-27 21:46:05 -04:00
|
|
|
// wxPixelData specialization for wxImage: this is the simplest case as we
|
|
|
|
// don't have to care about different pixel formats here
|
|
|
|
template <>
|
2005-03-01 12:49:27 -05:00
|
|
|
struct wxPixelDataOut<wxImage>
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
// NB: this is a template class even though it doesn't use its template
|
|
|
|
// parameter because otherwise wxPixelData couldn't compile
|
2003-04-29 17:15:28 -04:00
|
|
|
template <class dummyPixelFormat>
|
2005-03-01 12:49:27 -05:00
|
|
|
class wxPixelDataIn : public wxPixelDataBase
|
2003-03-22 14:00:48 -05:00
|
|
|
{
|
2005-02-26 12:26:24 -05:00
|
|
|
public:
|
2003-04-27 21:46:05 -04:00
|
|
|
// the type of the class we're working with
|
|
|
|
typedef wxImage ImageType;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// the iterator which should be used for working with data in this
|
|
|
|
// format
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// the pixel format we use
|
|
|
|
typedef wxImagePixelFormat PixelFormat;
|
|
|
|
|
|
|
|
// the type of the pixel components
|
2003-05-02 08:47:07 -04:00
|
|
|
typedef typename dummyPixelFormat::ChannelType ChannelType;
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
// the pixel data we're working with
|
|
|
|
typedef
|
|
|
|
wxPixelDataOut<wxImage>::wxPixelDataIn<PixelFormat> PixelData;
|
|
|
|
|
|
|
|
// go back to (0, 0)
|
|
|
|
void Reset(const PixelData& data)
|
|
|
|
{
|
|
|
|
*this = data.GetPixels();
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates the iterator pointing to the beginning of data
|
|
|
|
Iterator(PixelData& data)
|
|
|
|
{
|
|
|
|
Reset(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates the iterator initially pointing to the image origin
|
|
|
|
Iterator(const wxImage& image)
|
|
|
|
{
|
|
|
|
m_pRGB = image.GetData();
|
|
|
|
|
|
|
|
if ( image.HasAlpha() )
|
|
|
|
{
|
|
|
|
m_pAlpha = image.GetAlpha();
|
|
|
|
}
|
|
|
|
else // alpha is not used at all
|
|
|
|
{
|
|
|
|
m_pAlpha = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// true if the iterator is valid
|
|
|
|
bool IsOk() const { return m_pRGB != NULL; }
|
|
|
|
|
|
|
|
|
|
|
|
// navigation
|
|
|
|
// ----------
|
|
|
|
|
|
|
|
// advance the iterator to the next pixel, prefix version
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
m_pRGB += PixelFormat::SizePixel;
|
|
|
|
if ( m_pAlpha )
|
2003-05-09 19:50:43 -04:00
|
|
|
++m_pAlpha;
|
2003-04-27 21:46:05 -04:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// postfix (hence less efficient -- don't use it unless you
|
|
|
|
// absolutely must) version
|
2003-04-27 21:46:05 -04:00
|
|
|
Iterator operator++(int)
|
|
|
|
{
|
|
|
|
Iterator p(*this);
|
|
|
|
++*this;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move x pixels to the right and y down
|
|
|
|
//
|
|
|
|
// note that the rows don't wrap!
|
|
|
|
void Offset(const PixelData& data, int x, int y)
|
|
|
|
{
|
|
|
|
m_pRGB += data.GetRowStride()*y + PixelFormat::SizePixel*x;
|
|
|
|
if ( m_pAlpha )
|
|
|
|
m_pAlpha += data.GetWidth() + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move x pixels to the right (again, no row wrapping)
|
|
|
|
void OffsetX(const PixelData& WXUNUSED(data), int x)
|
|
|
|
{
|
|
|
|
m_pRGB += PixelFormat::SizePixel*x;
|
|
|
|
if ( m_pAlpha )
|
|
|
|
m_pAlpha += x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move y rows to the bottom
|
|
|
|
void OffsetY(const PixelData& data, int y)
|
|
|
|
{
|
|
|
|
m_pRGB += data.GetRowStride()*y;
|
|
|
|
if ( m_pAlpha )
|
|
|
|
m_pAlpha += data.GetWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
// go to the given position
|
|
|
|
void MoveTo(const PixelData& data, int x, int y)
|
|
|
|
{
|
|
|
|
Reset(data);
|
|
|
|
Offset(data, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// data access
|
|
|
|
// -----------
|
|
|
|
|
|
|
|
// access to invidividual colour components
|
2004-05-01 18:13:02 -04:00
|
|
|
ChannelType& Red() { return m_pRGB[PixelFormat::RED]; }
|
|
|
|
ChannelType& Green() { return m_pRGB[PixelFormat::GREEN]; }
|
|
|
|
ChannelType& Blue() { return m_pRGB[PixelFormat::BLUE]; }
|
2003-04-27 21:46:05 -04:00
|
|
|
ChannelType& Alpha() { return *m_pAlpha; }
|
|
|
|
|
|
|
|
// private: -- see comment in the beginning of the file
|
|
|
|
|
|
|
|
// pointer into RGB buffer
|
|
|
|
unsigned char *m_pRGB;
|
|
|
|
|
|
|
|
// pointer into alpha buffer or NULL if alpha isn't used
|
|
|
|
unsigned char *m_pAlpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
// initializes us with the data of the given image
|
|
|
|
wxPixelDataIn(ImageType& image) : m_image(image), m_pixels(image)
|
|
|
|
{
|
|
|
|
m_width = image.GetWidth();
|
|
|
|
m_height = image.GetHeight();
|
|
|
|
m_stride = Iterator::SizePixel * m_width;
|
|
|
|
}
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// 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;
|
|
|
|
|
2004-05-01 18:13:02 -04:00
|
|
|
InitRect(rect.GetPosition(), rect.GetSize());
|
2003-05-01 12:37:51 -04:00
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// we evaluate to true only if we could get access to bitmap data
|
|
|
|
// successfully
|
|
|
|
operator bool() const { return m_pixels.IsOk(); }
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// get the iterator pointing to the origin
|
|
|
|
Iterator GetPixels() const { return m_pixels; }
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
private:
|
2003-05-01 12:37:51 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// the image we're working with
|
|
|
|
ImageType& m_image;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// the iterator pointing to the image origin
|
|
|
|
Iterator m_pixels;
|
2003-03-22 14:00:48 -05:00
|
|
|
};
|
2003-04-27 21:46:05 -04:00
|
|
|
};
|
2004-12-09 20:53:31 -05:00
|
|
|
#endif //wxUSE_IMAGE
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2004-12-09 20:53:31 -05:00
|
|
|
#if wxUSE_GUI
|
2003-04-27 21:46:05 -04:00
|
|
|
// wxPixelData specialization for wxBitmap: here things are more interesting as
|
|
|
|
// we also have to support different pixel formats
|
|
|
|
template <>
|
2005-03-01 12:49:27 -05:00
|
|
|
struct wxPixelDataOut<wxBitmap>
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
template <class Format>
|
2005-03-01 12:49:27 -05:00
|
|
|
class wxPixelDataIn : public wxPixelDataBase
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// the type of the class we're working with
|
|
|
|
typedef wxBitmap ImageType;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2005-03-01 12:49:27 -05:00
|
|
|
class Iterator
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// the pixel format we use
|
|
|
|
typedef Format PixelFormat;
|
|
|
|
|
|
|
|
// the type of the pixel components
|
|
|
|
typedef typename PixelFormat::ChannelType ChannelType;
|
|
|
|
|
|
|
|
// the pixel data we're working with
|
|
|
|
typedef wxPixelDataOut<wxBitmap>::wxPixelDataIn<Format> PixelData;
|
|
|
|
|
|
|
|
|
|
|
|
// go back to (0, 0)
|
|
|
|
void Reset(const PixelData& data)
|
|
|
|
{
|
|
|
|
*this = data.GetPixels();
|
|
|
|
}
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// initializes the iterator to point to the origin of the given
|
|
|
|
// pixel data
|
2003-04-27 21:46:05 -04:00
|
|
|
Iterator(PixelData& data)
|
|
|
|
{
|
|
|
|
Reset(data);
|
|
|
|
}
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// initializes the iterator to point to the origin of the given
|
|
|
|
// bitmap
|
2003-04-27 21:46:05 -04:00
|
|
|
Iterator(wxBitmap& bmp, PixelData& data)
|
|
|
|
{
|
2003-05-01 12:37:51 -04:00
|
|
|
// 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
|
2003-04-27 21:46:05 -04:00
|
|
|
m_ptr = (ChannelType *)
|
|
|
|
bmp.GetRawData(data, PixelFormat::BitsPerPixel);
|
|
|
|
}
|
|
|
|
|
2006-08-24 23:19:06 -04:00
|
|
|
// default constructor
|
|
|
|
Iterator()
|
|
|
|
{
|
|
|
|
m_ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// return true if this iterator is valid
|
|
|
|
bool IsOk() const { return m_ptr != NULL; }
|
|
|
|
|
|
|
|
|
|
|
|
// navigation
|
|
|
|
// ----------
|
|
|
|
|
|
|
|
// advance the iterator to the next pixel, prefix version
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
m_ptr += PixelFormat::SizePixel;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// postfix (hence less efficient -- don't use it unless you
|
|
|
|
// absolutely must) version
|
2003-04-27 21:46:05 -04:00
|
|
|
Iterator operator++(int)
|
|
|
|
{
|
|
|
|
Iterator p(*this);
|
|
|
|
++*this;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move x pixels to the right and y down
|
|
|
|
//
|
|
|
|
// note that the rows don't wrap!
|
|
|
|
void Offset(const PixelData& data, int x, int y)
|
|
|
|
{
|
|
|
|
m_ptr += data.GetRowStride()*y + PixelFormat::SizePixel*x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move x pixels to the right (again, no row wrapping)
|
|
|
|
void OffsetX(const PixelData& WXUNUSED(data), int x)
|
|
|
|
{
|
|
|
|
m_ptr += PixelFormat::SizePixel*x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move y rows to the bottom
|
|
|
|
void OffsetY(const PixelData& data, int y)
|
|
|
|
{
|
|
|
|
m_ptr += data.GetRowStride()*y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// go to the given position
|
|
|
|
void MoveTo(const PixelData& data, int x, int y)
|
|
|
|
{
|
|
|
|
Reset(data);
|
|
|
|
Offset(data, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// data access
|
|
|
|
// -----------
|
|
|
|
|
|
|
|
// 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& Alpha() { return m_ptr[PixelFormat::ALPHA]; }
|
|
|
|
|
|
|
|
// address the pixel contents directly
|
|
|
|
//
|
|
|
|
// warning: the format is platform dependent
|
|
|
|
typename PixelFormat::PixelType& Data()
|
|
|
|
{ return *(typename PixelFormat::PixelType *)m_ptr; }
|
|
|
|
|
|
|
|
// private: -- see comment in the beginning of the file
|
|
|
|
|
2003-06-14 08:57:44 -04:00
|
|
|
// 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
|
2003-04-27 21:46:05 -04:00
|
|
|
ChannelType *m_ptr;
|
|
|
|
};
|
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
// 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)
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
}
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-05-01 12:37:51 -04:00
|
|
|
wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
|
|
|
|
: m_bmp(bmp), m_pixels(bmp, *this)
|
|
|
|
{
|
2003-07-10 15:18:06 -04:00
|
|
|
InitRect(rect.GetPosition(), rect.GetSize());
|
2003-05-01 12:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
|
|
|
|
: m_bmp(bmp), m_pixels(bmp, *this)
|
|
|
|
{
|
|
|
|
InitRect(pt, sz);
|
|
|
|
}
|
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// we evaluate to true only if we could get access to bitmap data
|
|
|
|
// successfully
|
|
|
|
operator bool() const { return m_pixels.IsOk(); }
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// get the iterator pointing to the origin
|
|
|
|
Iterator GetPixels() const { return m_pixels; }
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// dtor unlocks the bitmap
|
|
|
|
~wxPixelDataIn()
|
|
|
|
{
|
|
|
|
m_bmp.UngetRawData(*this);
|
|
|
|
}
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// call this to indicate that we should use the alpha channel
|
|
|
|
void UseAlpha() { m_bmp.UseAlpha(); }
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// private: -- see comment in the beginning of the file
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// the bitmap we're associated with
|
|
|
|
wxBitmap m_bmp;
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// the iterator pointing to the image origin
|
|
|
|
Iterator m_pixels;
|
2003-05-01 12:37:51 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2003-04-27 21:46:05 -04:00
|
|
|
};
|
|
|
|
};
|
2004-12-09 20:53:31 -05:00
|
|
|
#endif //wxUSE_GUI
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
|
|
|
|
class wxPixelData :
|
2003-05-10 08:53:08 -04:00
|
|
|
public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
public:
|
2003-05-10 21:04:10 -04:00
|
|
|
typedef
|
|
|
|
typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
|
|
|
|
Base;
|
2003-05-01 12:37:51 -04:00
|
|
|
|
2003-05-10 08:53:08 -04:00
|
|
|
wxPixelData(Image& image) : Base(image) { }
|
2003-05-01 12:37:51 -04:00
|
|
|
|
2003-05-10 08:53:08 -04:00
|
|
|
wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
|
|
|
|
|
|
|
|
wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
|
|
|
|
: Base(i, pt, sz)
|
|
|
|
{
|
|
|
|
}
|
2003-04-27 21:46:05 -04:00
|
|
|
};
|
2003-05-07 05:14:14 -04:00
|
|
|
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// some "predefined" pixel data classes
|
2004-12-09 20:53:31 -05:00
|
|
|
#if wxUSE_IMAGE
|
2003-04-27 21:46:05 -04:00
|
|
|
typedef wxPixelData<wxImage> wxImagePixelData;
|
2004-12-09 20:53:31 -05:00
|
|
|
#endif //wxUSE_IMAGE
|
|
|
|
#if wxUSE_GUI
|
2003-04-27 21:46:05 -04:00
|
|
|
typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
|
|
|
|
typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
|
2005-03-01 12:49:27 -05:00
|
|
|
|
2004-12-09 20:53:31 -05:00
|
|
|
#endif //wxUSE_GUI
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxPixelIterator
|
|
|
|
// ----------------------------------------------------------------------------
|
2003-03-22 14:00:48 -05:00
|
|
|
|
2003-04-27 21:46:05 -04:00
|
|
|
/*
|
|
|
|
wxPixel::Iterator represents something which points to the pixel data and
|
|
|
|
allows us to iterate over it. In the simplest case of wxBitmap it is,
|
|
|
|
indeed, just a pointer, but it can be something more complicated and,
|
|
|
|
moreover, you are free to specialize it for other image classes and bitmap
|
|
|
|
formats.
|
|
|
|
|
|
|
|
Note that although it would have been much more intuitive to have a real
|
|
|
|
class here instead of what we have now, this class would need two template
|
|
|
|
parameters, and this can't be done because we'd need compiler support for
|
|
|
|
partial template specialization then and neither VC6 nor VC7 provide it.
|
|
|
|
*/
|
|
|
|
template < class Image, class PixelFormat = wxPixelFormatFor<Image> >
|
2005-06-13 08:19:33 -04:00
|
|
|
struct wxPixelIterator : public wxPixelData<Image, PixelFormat>::Iterator
|
2003-04-27 21:46:05 -04:00
|
|
|
{
|
|
|
|
};
|
2003-03-22 14:00:48 -05:00
|
|
|
|
|
|
|
#endif // _WX_RAWBMP_H_BASE_
|
|
|
|
|