int m_width;
int m_height;
+ wxBitmapType m_type;
unsigned char *m_data;
bool m_hasMask;
{
m_width = 0;
m_height = 0;
+ m_type = wxBITMAP_TYPE_INVALID;
m_data =
m_alpha = (unsigned char *) NULL;
//-----------------------------------------------------------------------------
-#define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
+#define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
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;
(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;
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
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;
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) ||
{
// 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);
}
}
return M_IMGDATA->m_height;
}
+wxBitmapType wxImage::GetType() const
+{
+ wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID, wxT("invalid image") );
+
+ 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() &&
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::LoadFile( wxInputStream& stream, wxBitmapType type, int index )
+bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index)
{
- UnRef();
+ // 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);
- m_refData = new wxImageRefData;
+ 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 )
+{
+ AllocExclusive();
wxImageHandler *handler;
node = node->GetNext() )
{
handler = (wxImageHandler*)node->GetData();
- if ( handler->CanRead(stream) &&
- handler->LoadFile(this, stream, true/*verbose*/, index) )
- {
+ if ( handler->CanRead(stream) && DoLoad(*handler, stream, index) )
return true;
- }
-
}
wxLogWarning( _("No handler found for image type.") );
return false;
}
- return handler->LoadFile(this, stream, true/*verbose*/, index);
+ return DoLoad(*handler, stream, index);
}
bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
return false;
}
- return handler->LoadFile( this, stream, true/*verbose*/, index );
+ return DoLoad(*handler, stream, index);
+}
+
+bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const
+{
+ wxImage * const self = const_cast<wxImage *>(this);
+ if ( !handler.SaveFile(self, stream) )
+ return false;
+
+ M_IMGDATA->m_type = handler.GetType();
+ return true;
}
bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const
return false;
}
- return handler->SaveFile( (wxImage*)this, stream );
+ return DoSave(*handler, stream);
}
bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
}
- return handler->SaveFile( (wxImage*)this, stream );
+ return DoSave(*handler, stream);
}
#endif // wxUSE_STREAMS