// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "image.h"
#endif
IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
-wxImage::wxImage()
-{
-}
-
wxImage::wxImage( int width, int height, bool clear )
{
Create( width, height, clear );
memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 );
+ // also copy the image options
+ wxImageRefData *imgData = (wxImageRefData *)image.m_refData;
+ imgData->m_optionNames = M_IMGDATA->m_optionNames;
+ imgData->m_optionValues = M_IMGDATA->m_optionValues;
+
return image;
}
{
if( xFactor == 1 && yFactor == 1 )
return Copy() ;
-
+
wxImage image;
wxCHECK_MSG( Ok(), image, wxT("invalid image") );
long old_height = M_IMGDATA->m_height,
old_width = M_IMGDATA->m_width;
-
+
wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
wxT("invalid old image size") );
maskRed = M_IMGDATA->m_maskRed;
maskGreen = M_IMGDATA->m_maskGreen;
maskBlue =M_IMGDATA->m_maskBlue ;
-
+
image.SetMaskColour( M_IMGDATA->m_maskRed,
M_IMGDATA->m_maskGreen,
M_IMGDATA->m_maskBlue );
}
char unsigned *source_data = M_IMGDATA->m_data;
char unsigned *target_data = data;
-
+
for (long y = 0; y < height; y++)
{
for (long x = 0; x < width; x++)
}
else
{
- *(target_data++) = avgRed / counter ;
- *(target_data++) = avgGreen / counter ;
- *(target_data++) = avgBlue / counter ;
+ *(target_data++) = (unsigned char)(avgRed / counter);
+ *(target_data++) = (unsigned char)(avgGreen / counter);
+ *(target_data++) = (unsigned char)(avgBlue / counter);
}
}
}
M_IMGDATA->m_alpha[y*w + x] = alpha;
}
-unsigned char wxImage::GetAlpha(int x, int y)
+unsigned char wxImage::GetAlpha(int x, int y) const
{
wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
return M_IMGDATA->m_alpha[y*w + x];
}
+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++)
+ {
+ *alpha = *data;
+ alpha++;
+ *data = r;
+ data++;
+ *data = g;
+ data++;
+ *data = b;
+ data++;
+ }
+
+ return true;
+}
+
void wxImage::SetAlpha( unsigned char *alpha )
{
wxCHECK_RET( Ok(), wxT("invalid image") );
alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
}
- delete [] M_IMGDATA->m_alpha;
+ free(M_IMGDATA->m_alpha);
M_IMGDATA->m_alpha = alpha;
}
// check that the images are the same size
if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
{
- wxLogError( _("Image and Mask have different sizes") );
+ wxLogError( _("Image and mask have different sizes.") );
return false;
}
unsigned char r,g,b ;
if (!FindFirstUnusedColour(&r, &g, &b))
{
- wxLogError( _("No Unused Color in image being masked") );
+ wxLogError( _("No unused colour in image being masked.") );
return false ;
}
return true;
}
+bool wxImage::ConvertAlphaToMask(unsigned char threshold)
+{
+ if (!HasAlpha())
+ return true;
+
+ unsigned char mr, mg, mb;
+ if (!FindFirstUnusedColour(&mr, &mg, &mb))
+ {
+ wxLogError( _("No unused colour in image being masked.") );
+ return false;
+ }
+
+ SetMask(true);
+ SetMaskColour(mr, mg, mb);
+
+ unsigned char *imgdata = GetData();
+ unsigned char *alphadata = GetAlpha();
+
+ int w = GetWidth();
+ int h = GetHeight();
+
+ for (int y = 0; y < h; y++)
+ {
+ for (int x = 0; x < w; x++, imgdata += 3, alphadata++)
+ {
+ if (*alphadata < threshold)
+ {
+ imgdata[0] = mr;
+ imgdata[1] = mg;
+ imgdata[2] = mb;
+ }
+ }
+ }
+
+ free(M_IMGDATA->m_alpha);
+ M_IMGDATA->m_alpha = NULL;
+
+ return true;
+}
+
#if wxUSE_PALETTE
// Palette functions
{
const wxList& list = GetHandlers();
- for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
+ for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
{
wxImageHandler *handler=(wxImageHandler*)node->GetData();
if (handler->CanRead( stream ))
{
wxList &list=GetHandlers();
- for (wxList::Node *node = list.GetFirst(); node; node = node->GetNext())
+ for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
{
handler=(wxImageHandler*)node->GetData();
if ( handler->CanRead(stream) )
{
wxList &list=GetHandlers();
- for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
+ for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
{
handler=(wxImageHandler*)node->GetData();
if ( handler->CanRead(stream) )
void wxImage::AddHandler( wxImageHandler *handler )
{
- // make sure that the memory will be freed at the program end
- sm_handlers.DeleteContents(true);
-
// Check for an existing handler of the type being added.
if (FindHandler( handler->GetType() ) == 0)
{
void wxImage::InsertHandler( wxImageHandler *handler )
{
- // make sure that the memory will be freed at the program end
- sm_handlers.DeleteContents(true);
-
// Check for an existing handler of the type being added.
if (FindHandler( handler->GetType() ) == 0)
{
if (handler)
{
sm_handlers.DeleteObject(handler);
+ delete handler;
return true;
}
else
wxImageHandler *wxImage::FindHandler( const wxString& name )
{
- wxNode *node = sm_handlers.GetFirst();
+ wxList::compatibility_iterator node = sm_handlers.GetFirst();
while (node)
{
wxImageHandler *handler = (wxImageHandler*)node->GetData();
wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
{
- wxNode *node = sm_handlers.GetFirst();
+ wxList::compatibility_iterator node = sm_handlers.GetFirst();
while (node)
{
wxImageHandler *handler = (wxImageHandler*)node->GetData();
wxImageHandler *wxImage::FindHandler( long bitmapType )
{
- wxNode *node = sm_handlers.GetFirst();
+ wxList::compatibility_iterator node = sm_handlers.GetFirst();
while (node)
{
wxImageHandler *handler = (wxImageHandler *)node->GetData();
wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
{
- wxNode *node = sm_handlers.GetFirst();
+ wxList::compatibility_iterator node = sm_handlers.GetFirst();
while (node)
{
wxImageHandler *handler = (wxImageHandler *)node->GetData();
void wxImage::CleanUpHandlers()
{
- wxNode *node = sm_handlers.GetFirst();
+ wxList::compatibility_iterator node = sm_handlers.GetFirst();
while (node)
{
wxImageHandler *handler = (wxImageHandler *)node->GetData();
- wxNode *next = node->GetNext();
+ wxList::compatibility_iterator next = node->GetNext();
delete handler;
- delete node;
node = next;
}
-}
+ sm_handlers.Clear();
+}
wxString wxImage::GetImageExtWildcard()
{
wxString fmts;
wxList& Handlers = wxImage::GetHandlers();
- wxNode* Node = Handlers.GetFirst();
+ wxList::compatibility_iterator Node = Handlers.GetFirst();
while ( Node )
{
wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
return wxT("(") + fmts + wxT(")|") + fmts;
}
-
//-----------------------------------------------------------------------------
// wxImageHandler
//-----------------------------------------------------------------------------
bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
{
- off_t posOld = stream.TellI();
+ wxFileOffset posOld = stream.TellI();
if ( posOld == wxInvalidOffset )
{
// can't test unseekable stream
b2++;
if ( b2 >= 255 )
{
- wxLogError(_("GetUnusedColour:: No Unused Color in image ") );
+ wxLogError(_("No unused colour in image.") );
return false;
}
}
{
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
unsigned char ** data = new unsigned char * [GetHeight()];
-
data[0] = GetData();
-
for (i = 1; i < GetHeight(); i++)
data[i] = data[i - 1] + (3 * GetWidth());
+ // Same for alpha channel
+ unsigned char ** alpha = NULL;
+ if (has_alpha)
+ {
+ alpha = new unsigned char * [GetHeight()];
+ alpha[0] = GetAlpha();
+ for (i = 1; i < GetHeight(); i++)
+ alpha[i] = alpha[i - 1] + GetWidth();
+ }
+
// precompute coefficients for rotation formula
// (sine and cosine of the angle)
const double cos_angle = cos(angle);
int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
+ // Create rotated image
wxImage rotated (x2 - x1 + 1, y2 - y1 + 1, false);
+ // With alpha channel
+ if (has_alpha)
+ rotated.SetAlpha();
if (offset_after_rotation != NULL)
{
// 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();
// GRG: if the original image has a mask, use its RGB values
// as the blank pixel, else, fall back to default (black).
unsigned char *p = data[y1] + (3 * x1);
*(dst++) = *(p++);
*(dst++) = *(p++);
- *(dst++) = *(p++);
+ *(dst++) = *p;
+
+ if (has_alpha)
+ {
+ unsigned char *p = alpha[y1] + x1;
+ *(alpha_dst++) = *p;
+ }
}
else if (d2 < gs_Epsilon)
{
unsigned char *p = data[y1] + (3 * x2);
*(dst++) = *(p++);
*(dst++) = *(p++);
- *(dst++) = *(p++);
+ *(dst++) = *p;
+
+ if (has_alpha)
+ {
+ unsigned char *p = alpha[y1] + x2;
+ *(alpha_dst++) = *p;
+ }
}
else if (d3 < gs_Epsilon)
{
unsigned char *p = data[y2] + (3 * x2);
*(dst++) = *(p++);
*(dst++) = *(p++);
- *(dst++) = *(p++);
+ *(dst++) = *p;
+
+ if (has_alpha)
+ {
+ unsigned char *p = alpha[y2] + x2;
+ *(alpha_dst++) = *p;
+ }
}
else if (d4 < gs_Epsilon)
{
unsigned char *p = data[y2] + (3 * x1);
*(dst++) = *(p++);
*(dst++) = *(p++);
- *(dst++) = *(p++);
+ *(dst++) = *p;
+
+ if (has_alpha)
+ {
+ unsigned char *p = alpha[y2] + x1;
+ *(alpha_dst++) = *p;
+ }
}
else
{
w3 * *(v3++) + w4 * *(v4++)) /
(w1 + w2 + w3 + w4) );
*(dst++) = (unsigned char)
- ( (w1 * *(v1++) + w2 * *(v2++) +
- w3 * *(v3++) + w4 * *(v4++)) /
+ ( (w1 * *v1 + w2 * *v2 +
+ w3 * *v3 + w4 * *v4) /
(w1 + w2 + w3 + w4) );
+
+ if (has_alpha)
+ {
+ unsigned char *v1 = alpha[y1] + (x1);
+ unsigned char *v2 = alpha[y1] + (x2);
+ unsigned char *v3 = alpha[y2] + (x2);
+ unsigned char *v4 = alpha[y2] + (x1);
+
+ *(alpha_dst++) = (unsigned char)
+ ( (w1 * *v1 + w2 * *v2 +
+ w3 * *v3 + w4 * *v4) /
+ (w1 + w2 + w3 + w4) );
+ }
}
}
else
*(dst++) = blank_r;
*(dst++) = blank_g;
*(dst++) = blank_b;
+
+ if (has_alpha)
+ *(alpha_dst++) = 0;
}
}
}
unsigned char *p = data[ys] + (3 * xs);
*(dst++) = *(p++);
*(dst++) = *(p++);
- *(dst++) = *(p++);
+ *(dst++) = *p;
+
+ if (has_alpha)
+ {
+ unsigned char *p = alpha[ys] + (xs);
+ *(alpha_dst++) = *p;
+ }
}
else
{
*(dst++) = blank_r;
*(dst++) = blank_g;
*(dst++) = blank_b;
+
+ if (has_alpha)
+ *(alpha_dst++) = 255;
}
}
}
}
delete [] data;
+
+ if (has_alpha)
+ delete [] alpha;
return rotated;
}