X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/94406a493cf645083ac237a37bc9a3dee6691b13..82a16d7e5e5c83e95a4caa7314de2dbfbd1debdc:/src/common/image.cpp diff --git a/src/common/image.cpp b/src/common/image.cpp index 03a7d4fb9f..a86831b26b 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -110,10 +110,6 @@ wxImage wxNullImage; IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) -wxImage::wxImage() -{ -} - wxImage::wxImage( int width, int height, bool clear ) { Create( width, height, clear ); @@ -231,7 +227,7 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const { if( xFactor == 1 && yFactor == 1 ) return Copy() ; - + wxImage image; wxCHECK_MSG( Ok(), image, wxT("invalid image") ); @@ -242,7 +238,7 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const 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") ); @@ -265,14 +261,14 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const 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++) @@ -808,6 +804,33 @@ unsigned char wxImage::GetAlpha(int x, int y) const 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") ); @@ -935,8 +958,8 @@ bool wxImage::SetMaskFromImage(const wxImage& mask, return true; } - -bool wxImage::ConvertAlphaToMask(unsigned threshold) + +bool wxImage::ConvertAlphaToMask(unsigned char threshold) { if (!HasAlpha()) return true; @@ -954,12 +977,12 @@ bool wxImage::ConvertAlphaToMask(unsigned threshold) unsigned char *imgdata = GetData(); unsigned char *alphadata = GetAlpha(); - size_t w = GetWidth(); - size_t h = GetHeight(); + int w = GetWidth(); + int h = GetHeight(); - for (size_t y = 0; y < h; y++) + for (int y = 0; y < h; y++) { - for (size_t x = 0; x < w; x++, imgdata += 3, alphadata++) + for (int x = 0; x < w; x++, imgdata += 3, alphadata++) { if (*alphadata < threshold) { @@ -1485,7 +1508,7 @@ bool wxImageHandler::CanRead( const wxString& name ) bool wxImageHandler::CallDoCanRead(wxInputStream& stream) { - off_t posOld = stream.TellI(); + wxFileOffset posOld = stream.TellI(); if ( posOld == wxInvalidOffset ) { // can't test unseekable stream @@ -1672,15 +1695,25 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i { 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); @@ -1702,7 +1735,11 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i 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) { @@ -1714,6 +1751,10 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i // 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). @@ -1800,28 +1841,52 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i 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 { @@ -1847,6 +1912,19 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i ( (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 @@ -1854,6 +1932,9 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i *(dst++) = blank_r; *(dst++) = blank_g; *(dst++) = blank_b; + + if (has_alpha) + *(alpha_dst++) = 0; } } } @@ -1876,18 +1957,30 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i *(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; }