X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/591d3fa2106eae93a22829c7b2665dec524b4c00..2190f6aa153416d1c225532bb026df98b3e273ad:/src/common/image.cpp diff --git a/src/common/image.cpp b/src/common/image.cpp index b41fee58f6..07ae7ac0ac 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -123,7 +123,7 @@ wxImage wxNullImage; //----------------------------------------------------------------------------- -#define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData) +#define M_IMGDATA static_cast(m_refData) IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) @@ -252,7 +252,7 @@ wxObjectRefData* wxImage::CreateRefData() const wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const { - const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that); + const wxImageRefData* refData = static_cast(that); wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") ); wxImageRefData* refData_new = new wxImageRefData; @@ -1270,7 +1270,6 @@ void wxImage::Paste( const wxImage &image, int x, int y ) (GetMaskGreen()==image.GetMaskGreen()) && (GetMaskBlue()==image.GetMaskBlue())))) { - width *= 3; unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); int source_step = image.GetWidth()*3; @@ -1278,11 +1277,10 @@ void wxImage::Paste( const wxImage &image, int x, int y ) int target_step = M_IMGDATA->m_width*3; for (int j = 0; j < height; j++) { - memcpy( target_data, source_data, width ); + memcpy( target_data, source_data, width*3 ); source_data += source_step; target_data += target_step; } - return; } // Copy over the alpha channel from the original image @@ -1311,7 +1309,6 @@ void wxImage::Paste( const wxImage &image, int x, int y ) unsigned char g = image.GetMaskGreen(); unsigned char b = image.GetMaskBlue(); - width *= 3; unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); int source_step = image.GetWidth()*3; @@ -1320,7 +1317,7 @@ void wxImage::Paste( const wxImage &image, int x, int y ) for (int j = 0; j < height; j++) { - for (int i = 0; i < width; i+=3) + for (int i = 0; i < width*3; i+=3) { if ((source_data[i] != r) || (source_data[i+1] != g) || @@ -1393,7 +1390,7 @@ wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const { // calculate the luma double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5; - dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma); + dest[0] = dest[1] = dest[2] = static_cast(luma); } } @@ -1468,6 +1465,16 @@ wxBitmapType wxImage::GetType() const return M_IMGDATA->m_type; } +void wxImage::SetType(wxBitmapType type) +{ + wxCHECK_RET( IsOk(), "must create the image before setting its type"); + + // type can be wxBITMAP_TYPE_INVALID to reset the image type to default + wxASSERT_MSG( type != wxBITMAP_TYPE_MAX, "invalid bitmap type" ); + + M_IMGDATA->m_type = type; +} + long wxImage::XYToIndex(int x, int y) const { if ( Ok() && @@ -1971,12 +1978,10 @@ void wxImage::SetPalette(const wxPalette& palette) void wxImage::SetOption(const wxString& name, const wxString& value) { - wxCHECK_RET( Ok(), wxT("invalid image") ); - AllocExclusive(); int idx = M_IMGDATA->m_optionNames.Index(name, false); - if (idx == wxNOT_FOUND) + if ( idx == wxNOT_FOUND ) { M_IMGDATA->m_optionNames.Add(name); M_IMGDATA->m_optionValues.Add(value); @@ -1997,10 +2002,11 @@ void wxImage::SetOption(const wxString& name, int value) wxString wxImage::GetOption(const wxString& name) const { - wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") ); + if ( !M_IMGDATA ) + return wxEmptyString; int idx = M_IMGDATA->m_optionNames.Index(name, false); - if (idx == wxNOT_FOUND) + if ( idx == wxNOT_FOUND ) return wxEmptyString; else return M_IMGDATA->m_optionValues[idx]; @@ -2013,9 +2019,8 @@ int wxImage::GetOptionInt(const wxString& name) const bool wxImage::HasOption(const wxString& name) const { - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); - - return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND); + return M_IMGDATA ? M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND + : false; } // ---------------------------------------------------------------------------- @@ -2207,18 +2212,42 @@ int wxImage::GetImageCount( wxInputStream &stream, wxBitmapType type ) bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index) { + // save the options values which can be clobbered by the handler (e.g. many + // of them call Destroy() before trying to load the file) + const unsigned maxWidth = GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH), + maxHeight = GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT); + if ( !handler.LoadFile(this, stream, true/*verbose*/, index) ) return false; M_IMGDATA->m_type = handler.GetType(); + + // rescale the image to the specified size if needed + if ( maxWidth || maxHeight ) + { + const unsigned widthOrig = GetWidth(), + heightOrig = GetHeight(); + + // this uses the same (trivial) algorithm as the JPEG handler + unsigned width = widthOrig, + height = heightOrig; + while ( (maxWidth && width > maxWidth) || + (maxHeight && height > maxHeight) ) + { + width /= 2; + height /= 2; + } + + if ( width != widthOrig || height != heightOrig ) + Rescale(width, height, wxIMAGE_QUALITY_HIGH); + } + return true; } bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) { - UnRef(); - - m_refData = new wxImageRefData; + AllocExclusive(); wxImageHandler *handler; @@ -2281,7 +2310,7 @@ bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int ind bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const { - wxImage * const self = wx_const_cast(wxImage *, this); + wxImage * const self = const_cast(this); if ( !handler.SaveFile(self, stream) ) return false;