#include "wx/module.h"
#include "wx/hash.h"
#include "wx/utils.h"
+#include "wx/math.h"
+
+#if wxUSE_XPM
+#include "wx/xpmdecod.h"
+#endif
// For memcpy
#include <string.h>
-#include <math.h>
#ifdef __SALFORDC__
#undef FAR
if (image) Ref(*image);
}
+wxImage::wxImage( const char** xpmData )
+{
+ Create(xpmData);
+}
+
+wxImage::wxImage( char** xpmData )
+{
+ Create((const char**) xpmData);
+}
+
+bool wxImage::Create( const char** xpmData )
+{
+#if wxUSE_XPM
+ UnRef();
+
+ wxXPMDecoder decoder;
+ (*this) = decoder.ReadData(xpmData);
+ return Ok();
+#else
+ return false;
+#endif
+}
+
bool wxImage::Create( int width, int height, bool clear )
{
UnRef();
unsigned char maskRed = 0;
unsigned char maskGreen = 0;
unsigned char maskBlue =0 ;
+
+ unsigned char *source_data = M_IMGDATA->m_data;
+ unsigned char *target_data = data;
+ unsigned char *source_alpha = 0 ;
+ unsigned char *target_alpha = 0 ;
if (M_IMGDATA->m_hasMask)
{
hasMask = true ;
M_IMGDATA->m_maskGreen,
M_IMGDATA->m_maskBlue );
}
- char unsigned *source_data = M_IMGDATA->m_data;
- char unsigned *target_data = data;
+ else
+ {
+ source_alpha = M_IMGDATA->m_alpha ;
+ if ( source_alpha )
+ {
+ image.SetAlpha() ;
+ target_alpha = image.GetAlpha() ;
+ }
+ }
for (long y = 0; y < height; y++)
{
unsigned long avgRed = 0 ;
unsigned long avgGreen = 0;
unsigned long avgBlue = 0;
+ unsigned long avgAlpha = 0 ;
unsigned long counter = 0 ;
// determine average
for ( int y1 = 0 ; y1 < yFactor ; ++y1 )
unsigned char red = pixel[0] ;
unsigned char green = pixel[1] ;
unsigned char blue = pixel[2] ;
+ unsigned char alpha = 255 ;
+ if ( source_alpha )
+ alpha = *(source_alpha + y_offset + x * xFactor + x1) ;
if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
{
- avgRed += red ;
- avgGreen += green ;
- avgBlue += blue ;
+ if ( alpha > 0 )
+ {
+ avgRed += red ;
+ avgGreen += green ;
+ avgBlue += blue ;
+ }
+ avgAlpha += alpha ;
counter++ ;
}
}
}
else
{
+ if ( source_alpha )
+ *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ;
*(target_data++) = (unsigned char)(avgRed / counter);
*(target_data++) = (unsigned char)(avgGreen / counter);
*(target_data++) = (unsigned char)(avgBlue / counter);
wxCHECK_MSG( data, image, wxT("unable to create image") );
+ unsigned char *source_data = M_IMGDATA->m_data;
+ unsigned char *target_data = data;
+ unsigned char *source_alpha = 0 ;
+ unsigned char *target_alpha = 0 ;
+
if (M_IMGDATA->m_hasMask)
{
image.SetMaskColour( M_IMGDATA->m_maskRed,
M_IMGDATA->m_maskGreen,
M_IMGDATA->m_maskBlue );
}
-
- unsigned char *source_data = M_IMGDATA->m_data;
- unsigned char *target_data = data;
+ else
+ {
+ source_alpha = M_IMGDATA->m_alpha ;
+ if ( source_alpha )
+ {
+ image.SetAlpha() ;
+ target_alpha = image.GetAlpha() ;
+ }
+ }
long x_delta = (old_width<<16) / width;
long y_delta = (old_height<<16) / height;
for ( long j = 0; j < height; j++ )
{
unsigned char* src_line = &source_data[(y>>16)*old_width*3];
-
+ unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
+
long x = 0;
for ( long i = 0; i < width; i++ )
{
unsigned char* src_pixel = &src_line[(x>>16)*3];
+ unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
dest_pixel[0] = src_pixel[0];
dest_pixel[1] = src_pixel[1];
dest_pixel[2] = src_pixel[2];
dest_pixel += 3;
+ if ( source_alpha )
+ *(target_alpha++) = *src_alpha_pixel ;
x += x_delta;
}
bool wxImage::ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b )
{
SetAlpha( NULL );
-
+
int w = M_IMGDATA->m_width,
h = M_IMGDATA->m_height;
-
+
unsigned char *alpha = GetAlpha();
unsigned char *data = GetData();
-
+
int x,y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
return M_IMGDATA->m_alpha;
}
+void wxImage::InitAlpha()
+{
+ wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
+
+ // initialize memory for alpha channel
+ SetAlpha();
+
+ unsigned char *alpha = M_IMGDATA->m_alpha;
+ const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
+
+ static const unsigned char ALPHA_TRANSPARENT = 0;
+ static const unsigned char ALPHA_OPAQUE = 0xff;
+ if ( HasMask() )
+ {
+ // use the mask to initialize the alpha channel.
+ const unsigned char * const alphaEnd = alpha + lenAlpha;
+
+ const unsigned char mr = M_IMGDATA->m_maskRed;
+ const unsigned char mg = M_IMGDATA->m_maskGreen;
+ const unsigned char mb = M_IMGDATA->m_maskBlue;
+ for ( unsigned char *src = M_IMGDATA->m_data;
+ alpha < alphaEnd;
+ src += 3, alpha++ )
+ {
+ *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
+ ? ALPHA_TRANSPARENT
+ : ALPHA_OPAQUE;
+ }
+
+ M_IMGDATA->m_hasMask = false;
+ }
+ else // no mask
+ {
+ // make the image fully opaque
+ memset(alpha, ALPHA_OPAQUE, lenAlpha);
+ }
+}
+
// ----------------------------------------------------------------------------
// mask support
// ----------------------------------------------------------------------------
int wxImage::GetOptionInt(const wxString& name) const
{
- wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
-
return wxAtoi(GetOption(name));
}
{
int i;
angle = -angle; // screen coordinates are a mirror image of "real" coordinates
-
+
bool has_alpha = HasAlpha();
// Create pointer-based array to accelerate access to wxImage's data
for (i = 1; i < GetHeight(); i++)
data[i] = data[i - 1] + (3 * GetWidth());
- // Same for alpha channel
+ // Same for alpha channel
unsigned char ** alpha = NULL;
if (has_alpha)
{
// array here (and in fact it would be slower).
//
unsigned char * dst = rotated.GetData();
-
+
unsigned char * alpha_dst = NULL;
if (has_alpha)
alpha_dst = rotated.GetAlpha();
*(dst++) = *(p++);
*(dst++) = *(p++);
*(dst++) = *p;
-
+
if (has_alpha)
{
unsigned char *p = alpha[y1] + x1;
*(dst++) = *(p++);
*(dst++) = *(p++);
*(dst++) = *p;
-
+
if (has_alpha)
{
unsigned char *p = alpha[y1] + x2;
*(dst++) = *(p++);
*(dst++) = *(p++);
*(dst++) = *p;
-
+
if (has_alpha)
{
unsigned char *p = alpha[y2] + x2;
*(dst++) = *(p++);
*(dst++) = *(p++);
*(dst++) = *p;
-
+
if (has_alpha)
{
unsigned char *p = alpha[y2] + x1;
( (w1 * *v1 + w2 * *v2 +
w3 * *v3 + w4 * *v4) /
(w1 + w2 + w3 + w4) );
-
+
if (has_alpha)
{
unsigned char *v1 = alpha[y1] + (x1);
*(dst++) = blank_r;
*(dst++) = blank_g;
*(dst++) = blank_b;
-
+
if (has_alpha)
*(alpha_dst++) = 0;
}
*(dst++) = *(p++);
*(dst++) = *(p++);
*(dst++) = *p;
-
+
if (has_alpha)
{
unsigned char *p = alpha[ys] + (xs);
*(dst++) = blank_r;
*(dst++) = blank_g;
*(dst++) = blank_b;
-
+
if (has_alpha)
*(alpha_dst++) = 255;
}
}
delete [] data;
-
+
if (has_alpha)
delete [] alpha;