#endif
//-----------------------------------------------------------------------------
-// wxImage
+// global data
+//-----------------------------------------------------------------------------
+
+wxList wxImage::sm_handlers;
+wxImage wxNullImage;
+
+//-----------------------------------------------------------------------------
+// wxImageRefData
//-----------------------------------------------------------------------------
class wxImageRefData: public wxObjectRefData
wxArrayString m_optionNames;
wxArrayString m_optionValues;
- DECLARE_NO_COPY_CLASS(wxImageRefData)
+ wxDECLARE_NO_COPY_CLASS(wxImageRefData);
};
wxImageRefData::wxImageRefData()
free( m_alpha );
}
-wxList wxImage::sm_handlers;
-
-wxImage wxNullImage;
+//-----------------------------------------------------------------------------
+// wxImage
//-----------------------------------------------------------------------------
-#define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
+#define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
-wxImage::wxImage( int width, int height, bool clear )
-{
- Create( width, height, clear );
-}
-
-wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
-{
- Create( width, height, data, static_data );
-}
-
-wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
-{
- Create( width, height, data, alpha, static_data );
-}
-
-wxImage::wxImage( const wxString& name, wxBitmapType type, int index )
-{
- LoadFile( name, type, index );
-}
-
-wxImage::wxImage( const wxString& name, const wxString& mimetype, int index )
-{
- LoadFile( name, mimetype, index );
-}
-
-#if wxUSE_STREAMS
-wxImage::wxImage( wxInputStream& stream, wxBitmapType type, int index )
-{
- LoadFile( stream, type, index );
-}
-
-wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index )
-{
- LoadFile( stream, mimetype, index );
-}
-#endif // wxUSE_STREAMS
-
-wxImage::wxImage(const char* const* xpmData)
-{
- Create(xpmData);
-}
-
bool wxImage::Create(const char* const* xpmData)
{
#if wxUSE_XPM
return false;
}
- if (clear)
- memset(M_IMGDATA->m_data, 0, width*height*3);
-
M_IMGDATA->m_width = width;
M_IMGDATA->m_height = height;
M_IMGDATA->m_ok = true;
+ if (clear)
+ {
+ Clear();
+ }
+
return true;
}
M_IMGDATA->m_height = height;
M_IMGDATA->m_ok = true;
M_IMGDATA->m_static = static_data;
+ M_IMGDATA->m_staticAlpha = static_data;
return true;
}
UnRef();
}
+void wxImage::Clear(unsigned char value)
+{
+ memset(M_IMGDATA->m_data, value, M_IMGDATA->m_width*M_IMGDATA->m_height*3);
+}
+
wxObjectRefData* wxImage::CreateRefData() const
{
return new wxImageRefData;
wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const
{
- const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that);
+ const wxImageRefData* refData = static_cast<const wxImageRefData*>(that);
wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") );
wxImageRefData* refData_new = new wxImageRefData;
{
// 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<unsigned char>(luma);
}
}
bool wxImage::ConvertAlphaToMask(unsigned char threshold)
{
- if (!HasAlpha())
+ if ( !HasAlpha() )
return true;
unsigned char mr, mg, mb;
- if (!FindFirstUnusedColour(&mr, &mg, &mb))
+ if ( !FindFirstUnusedColour(&mr, &mg, &mb) )
{
wxLogError( _("No unused colour in image being masked.") );
return false;
}
+ ConvertAlphaToMask(mr, mg, mb, threshold);
+ return true;
+}
+
+void wxImage::ConvertAlphaToMask(unsigned char mr,
+ unsigned char mg,
+ unsigned char mb,
+ unsigned char threshold)
+{
+ if ( !HasAlpha() )
+ return;
+
AllocExclusive();
SetMask(true);
}
}
- if( !M_IMGDATA->m_staticAlpha )
+ if ( !M_IMGDATA->m_staticAlpha )
free(M_IMGDATA->m_alpha);
M_IMGDATA->m_alpha = NULL;
M_IMGDATA->m_staticAlpha = false;
-
- return true;
}
// ----------------------------------------------------------------------------
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);
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];
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;
}
// ----------------------------------------------------------------------------
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;
bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const
{
- wxImage * const self = wx_const_cast(wxImage *, this);
+ wxImage * const self = const_cast<wxImage *>(this);
if ( !handler.SaveFile(self, stream) )
return false;
while (node)
{
wxImageHandler *handler = (wxImageHandler*)node->GetData();
- if ( (handler->GetExtension().Cmp(extension) == 0) &&
- ( (bitmapType == wxBITMAP_TYPE_ANY) || (handler->GetType() == bitmapType)) )
+ if ((bitmapType == wxBITMAP_TYPE_ANY) || (handler->GetType() == bitmapType))
{
- return handler;
+ if (handler->GetExtension() == extension)
+ return handler;
+ if (handler->GetAltExtensions().Index(extension, false) != wxNOT_FOUND)
+ return handler;
}
node = node->GetNext();
}
{
wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
fmts += wxT("*.") + Handler->GetExtension();
+ for (size_t i = 0; i < Handler->GetAltExtensions().size(); i++)
+ fmts += wxT(";*.") + Handler->GetAltExtensions()[i];
Node = Node->GetNext();
if ( Node ) fmts += wxT(";");
}
IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
#if wxUSE_STREAMS
-bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
+int wxImageHandler::GetImageCount( wxInputStream& stream )
{
- return false;
-}
+ // NOTE: this code is the same of wxAnimationDecoder::CanRead and
+ // wxImageHandler::CallDoCanRead
-bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
-{
- return false;
-}
+ if ( !stream.IsSeekable() )
+ return false; // can't test unseekable stream
-int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
-{
- return 1;
+ wxFileOffset posOld = stream.TellI();
+ int n = DoGetImageCount(stream);
+
+ // restore the old position to be able to test other formats and so on
+ if ( stream.SeekI(posOld) == wxInvalidOffset )
+ {
+ wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
+
+ // reading would fail anyhow as we're not at the right position
+ return false;
+ }
+
+ return n;
}
bool wxImageHandler::CanRead( const wxString& name )
bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
{
- wxFileOffset posOld = stream.TellI();
- if ( posOld == wxInvalidOffset )
- {
- // can't test unseekable stream
- return false;
- }
+ // NOTE: this code is the same of wxAnimationDecoder::CanRead and
+ // wxImageHandler::GetImageCount
+
+ if ( !stream.IsSeekable() )
+ return false; // can't test unseekable stream
+ wxFileOffset posOld = stream.TellI();
bool ok = DoCanRead(stream);
// restore the old position to be able to test other formats and so on