#endif // wxUSE_STREAMS
wxImage::wxImage( const wxImage& image )
+ : wxObject()
{
Ref(image);
}
wxCHECK_MSG( data, image, wxT("unable to create image") );
- if (M_IMGDATA->m_hasMask)
- image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
+ image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
+ image.SetMask( M_IMGDATA->m_hasMask );
memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 );
return TRUE;
}
-
-// DoFloodFill
-// Fills with the colour extracted from fillBrush, starting at x,y until either
-// a color different from the start pixel is reached (wxFLOOD_SURFACE)
-// or fill color is reached (wxFLOOD_BORDER)
-
-bool wxImage::MatchPixel(int x, int y, int w, int h, const wxColour & c)
-{
- if ((x<0)||(x>=w)||(y<0)||(y>=h)) return false;
-
- unsigned char r = GetRed(x,y);
- unsigned char g = GetGreen(x,y);
- unsigned char b = GetBlue(x,y);
- return c.Red() == r && c.Green() == g && c.Blue() == b ;
-}
-
-bool wxImage::MatchBoundaryPixel(int x, int y, int w, int h, const wxColour & fill, const wxColour & bound)
-{
- if ((x<0)||(x>=w)||(y<0)||(y>=h)) return TRUE;
-
- unsigned char r = GetRed(x,y);
- unsigned char g = GetGreen(x,y);
- unsigned char b = GetBlue(x,y);
- if ( fill.Red() == r && fill.Green() == g && fill.Blue() == b ) return TRUE;
- if ( bound.Red() == r && bound.Green() == g && bound.Blue() == b ) return TRUE;
- return FALSE ;
-}
-
-
-void wxImage::DoFloodFill (wxCoord x, wxCoord y, const wxBrush & fillBrush,
- const wxColour& testColour, int style /*=wxFLOOD_SURFACE */,
- int LogicalFunction /*= wxCOPY, currently unused */)
-{
- /* A diamond flood-fill using a circular queue system.
- Each pixel surrounding the current pixel is added to
- the queue if it meets the criteria, then is retrieved in
- its turn. Code originally based on http://www.drawit.co.nz/Developers.htm */
-
- int width = GetWidth();
- int height = GetHeight();
-
- //Draw using a pen made from the current brush colour
- //Potentially allows us to use patterned flood fills in future code
- wxColour fillColour = fillBrush.GetColour();
- unsigned char r = fillColour.Red();
- unsigned char g = fillColour.Green();
- unsigned char b = fillColour.Blue();
-
- //initial test :
- if (style == wxFLOOD_SURFACE)
- {
- //if wxFLOOD_SURFACE, if fill colour is same as required, we don't do anything
- if ( GetRed(x,y) != r
- || GetGreen(x,y) != g
- || GetBlue (x,y) != b )
- {
- //prepare memory for queue
- //queue save, start, read
- size_t *qs, *qst, *qr;
-
- //queue size (physical)
- long qSz= height * width * 2;
- qst = new size_t [qSz];
-
- //temporary x and y locations
- int xt, yt;
-
- for (int i=0; i < qSz; i++)
- qst[i] = 0;
-
- // start queue
- qs=qr=qst;
- *qs=xt=x;
- qs++;
- *qs=yt=y;
- qs++;
-
- SetRGB(xt,yt,r,g,b);
-
- //Main queue loop
- while(qr!=qs)
- {
- //Add new members to queue
- //Above current pixel
- if(MatchPixel(xt,yt-1,width,height,testColour))
- {
- *qs=xt;
- qs++;
- *qs=yt-1;
- qs++;
- SetRGB(xt,yt-1,r,g,b);
-
- //Loop back to beginning of queue
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Below current pixel
- if(MatchPixel(xt,yt+1,width,height,testColour))
- {
- *qs=xt;
- qs++;
- *qs=yt+1;
- qs++;
- SetRGB(xt,yt+1,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Left of current pixel
- if(MatchPixel(xt-1,yt,width,height,testColour))
- {
- *qs=xt-1;
- qs++;
- *qs=yt;
- qs++;
- SetRGB(xt-1,yt,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Right of current pixel
- if(MatchPixel(xt+1,yt,width,height,testColour))
- {
- *qs=xt+1;
- qs++;
- *qs=yt;
- qs++;
- SetRGB(xt+1,yt,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Retrieve current queue member
- qr+=2;
-
- //Loop back to the beginning
- if(qr>=(qst+qSz)) qr=qst;
- xt=*qr;
- yt=*(qr+1);
-
- //Go Back to beginning of loop
- }
-
- delete [] qst ;
- }
- }
- else
- {
- //style is wxFLOOD_BORDER
- // fill up to testColor border - if already testColour don't do anything
- if ( GetRed(x,y) != testColour.Red()
- || GetGreen(x,y) != testColour.Green()
- || GetBlue(x,y) != testColour.Blue() )
- {
- //prepare memory for queue
- //queue save, start, read
- size_t *qs, *qst, *qr;
-
- //queue size (physical)
- long qSz= height * width * 2;
- qst = new size_t [qSz];
-
- //temporary x and y locations
- int xt, yt;
-
- for (int i=0; i < qSz; i++)
- qst[i] = 0;
-
- // start queue
- qs=qr=qst;
- *qs=xt=x;
- qs++;
- *qs=yt=y;
- qs++;
-
- SetRGB(xt,yt,r,g,b);
-
- //Main queue loop
- while(qr!=qs)
- {
- //Add new members to queue
- //Above current pixel
- if(!MatchBoundaryPixel(xt,yt-1,width,height,fillColour,testColour))
- {
- *qs=xt;
- qs++;
- *qs=yt-1;
- qs++;
- SetRGB(xt,yt-1,r,g,b);
-
- //Loop back to beginning of queue
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Below current pixel
- if(!MatchBoundaryPixel(xt,yt+1,width,height,fillColour,testColour))
- {
- *qs=xt;
- qs++;
- *qs=yt+1;
- qs++;
- SetRGB(xt,yt+1,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Left of current pixel
- if(!MatchBoundaryPixel(xt-1,yt,width,height,fillColour,testColour))
- {
- *qs=xt-1;
- qs++;
- *qs=yt;
- qs++;
- SetRGB(xt-1,yt,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Right of current pixel
- if(!MatchBoundaryPixel(xt+1,yt,width,height,fillColour,testColour))
- {
- *qs=xt+1;
- qs++;
- *qs=yt;
- qs++;
- SetRGB(xt+1,yt,r,g,b);
- if(qs>=(qst+qSz)) qs=qst;
- }
-
- //Retrieve current queue member
- qr+=2;
-
- //Loop back to the beginning
- if(qr>=(qst+qSz)) qr=qst;
- xt=*qr;
- yt=*(qr+1);
-
- //Go Back to beginning of loop
- }
-
- delete [] qst ;
- }
- }
- //all done,
-}
-
-
#if wxUSE_PALETTE
// Palette functions
wxFileOutputStream stream(filename);
- if ( stream.LastError() == wxStream_NOERROR )
+ if ( stream.IsOk() )
{
wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, type);
wxFileOutputStream stream(filename);
- if ( stream.LastError() == wxStream_NOERROR )
+ if ( stream.IsOk() )
{
wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, mimetype);
{
#if wxUSE_STREAMS
wxFileInputStream stream(name);
- return GetImageCount(stream, type);
+ if (!stream.Ok())
+ return 0;
+ else
+ return GetImageCount(stream, type);
#else
return 0;
#endif
bool wxImage::CanRead( wxInputStream &stream )
{
- wxList &list=GetHandlers();
+ const wxList& list = GetHandlers();
for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
{
handler = FindHandler(type);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %d defined."), type );
wxImageHandler *handler = FindHandlerMime(mimetype);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
wxImageHandler *handler = FindHandler(type);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %d defined."), type );
wxImageHandler *handler = FindHandlerMime(mimetype);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
// make sure that the memory will be freed at the program end
sm_handlers.DeleteContents(TRUE);
- sm_handlers.Append( handler );
+ // Check for an existing handler of the type being added.
+ if (FindHandler( handler->GetType() ) == 0)
+ {
+ sm_handlers.Append( handler );
+ }
+ else
+ {
+ // This is not documented behaviour, merely the simplest 'fix'
+ // for preventing duplicate additions. If someone ever has
+ // a good reason to add and remove duplicate handlers (and they
+ // may) we should probably refcount the duplicates.
+ // also an issue in InsertHandler below.
+
+ wxLogDebug( _T("Adding duplicate image handler for '%s'"),
+ handler->GetName().c_str() );
+ delete handler;
+ }
}
void wxImage::InsertHandler( wxImageHandler *handler )
// make sure that the memory will be freed at the program end
sm_handlers.DeleteContents(TRUE);
- sm_handlers.Insert( handler );
+ // Check for an existing handler of the type being added.
+ if (FindHandler( handler->GetType() ) == 0)
+ {
+ sm_handlers.Insert( handler );
+ }
+ else
+ {
+ // see AddHandler for additional comments.
+ wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
+ handler->GetName().c_str() );
+ delete handler;
+ }
}
bool wxImage::RemoveHandler( const wxString& name )
node = node->Next();
}
- return (wxImageHandler *)NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
return handler;
node = node->Next();
}
- return (wxImageHandler*)NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandler( long bitmapType )
if (handler->GetType() == bitmapType) return handler;
node = node->Next();
}
- return NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
node = node->Next();
}
- return NULL;
+ return 0;
}
void wxImage::InitStandardHandlers()
#if wxUSE_STREAMS
AddHandler(new wxBMPHandler);
#endif // wxUSE_STREAMS
-
-#if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__)
- AddHandler(new wxXPMHandler);
-#endif
}
void wxImage::CleanUpHandlers()
return CanRead(stream);
}
- else {
- wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
+ wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
+ return FALSE;
+}
+
+bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
+{
+ off_t posOld = stream.TellI();
+ if ( posOld == wxInvalidOffset )
+ {
+ // can't test unseekable stream
+ return FALSE;
+ }
+
+ bool ok = DoCanRead(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 FALSE;
+
+ return ok;
}
#endif // wxUSE_STREAMS
//-----------------------------------------------------------------------------
-// Deprecated wxBitmap convertion routines
+// Deprecated wxBitmap conversion routines
//-----------------------------------------------------------------------------
#if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI