Allow suppressing warnings from wxImage::LoadFile()

Add wxImage::SetLoadFlags() and static SetDefaultLoadFlags() to allow
suppressing the warning messages that can be logged when loading some files,
notably PNG ones with invalid sRGB profiles which, unfortunately, seem to be
rather common and result in annoying warnings about them with libpng 1.6+.

Closes #15331.
This commit is contained in:
Vadim Zeitlin 2016-01-31 02:22:55 +01:00
parent 5066bff04e
commit a016e6b896
4 changed files with 117 additions and 1 deletions

View File

@ -143,6 +143,7 @@ All (GUI):
- Use platform-specific stock icons for wxEditableListBox buttons.
- Add support for the events from multimedia keys (Jens Göpfert).
- Improve wxAUI appearance in high contrast themes (Zane U. Ji).
- Allow suppressing warnings from wxImage::LoadFile().
wxGTK:

View File

@ -408,6 +408,22 @@ public:
// colour given.
bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
// Methods for controlling LoadFile() behaviour. Currently they allow to
// specify whether the function should log warnings if there are any
// problems with the image file not completely preventing it from being
// loaded. By default the warnings are logged, but this can be disabled
// either globally or for a particular image object.
enum
{
Load_Verbose = 1
};
static void SetDefaultLoadFlags(int flags);
static int GetDefaultLoadFlags();
void SetLoadFlags(int flags);
int GetLoadFlags() const;
static bool CanRead( const wxString& name );
static int GetImageCount( const wxString& name, wxBitmapType type = wxBITMAP_TYPE_ANY );
virtual bool LoadFile( const wxString& name, wxBitmapType type = wxBITMAP_TYPE_ANY, int index = -1 );

View File

@ -1373,6 +1373,12 @@ public:
/**
Loads an image from an input stream.
If the file can't be loaded, this function returns false and logs an
error using wxLogError(). If the file can be loaded but some problems
were detected while doing it, it can also call wxLogWarning() to notify
about these problems. If this is undesirable, use SetLoadFlags() to
reset @c Load_Verbose flag and suppress these warnings.
@param stream
Opened input stream from which to load the image.
Currently, the stream must support seeking.
@ -1605,6 +1611,47 @@ public:
void SetData(unsigned char* data, int new_width, int new_height,
bool static_data = false);
/**
Sets the default value for the flags used for loading image files.
This method changes the global value of the flags used for all the
subsequently created wxImage objects by default. It doesn't affect the
already existing objects.
By default, the global flags include @c Load_Verbose flag value.
@see LoadFile(), SetLoadFlags(), GetDefaultLoadFlags()
@since 3.1.0
*/
static void SetDefaultLoadFlags(int flags);
/**
Sets the flags used for loading image files by this object.
The flags will affect any future calls to LoadFile() for this object.
To change the flags for all image objects, call SetDefaultLoadFlags()
before creating any of them.
Currently the only defined flag is @c Load_Verbose which determines if
the non-fatal (i.e. not preventing the file from being loaded
completely) problems should result in the calls to wxLogWarning()
function. It is recommended to customize handling of these warnings by
e.g. defining a custom log target (see @ref overview_log), but if such
warnings should be completely suppressed, clearing this flag provides a
simple way to do it, for example:
@code
wxImage image;
image.SetLoadFlags(image.GetLoadFlags() & ~wxImage::Load_Verbose);
image.LoadFile(...);
@endcode
@see LoadFile(), SetLoadFlags(), GetLoadFlags()
@since 3.1.0
*/
void SetLoadFlags(int flags);
/**
Specifies whether there is a mask or not.
@ -1852,6 +1899,15 @@ public:
*/
static bool CanRead(wxInputStream& stream);
/**
Returns the currently used default file load flags.
See SetDefaultLoadFlags() for more information about these flags.
@since 3.1.0
*/
static int GetDefaultLoadFlags();
//@{
/**
If the image file contains more than one image and the image handler is
@ -1909,6 +1965,15 @@ public:
*/
static wxString GetImageExtWildcard();
/**
Returns the file load flags used for this object.
See SetLoadFlags() for more information about these flags.
@since 3.1.0
*/
int GetLoadFlags() const;
/**
Converts a color in RGB color space to HSV color space.
*/

View File

@ -87,6 +87,10 @@ public:
// same as m_static but for m_alpha
bool m_staticAlpha;
// global and per-object flags determining LoadFile() behaviour
int m_loadFlags;
static int sm_defaultLoadFlags;
#if wxUSE_PALETTE
wxPalette m_palette;
#endif // wxUSE_PALETTE
@ -97,6 +101,9 @@ public:
wxDECLARE_NO_COPY_CLASS(wxImageRefData);
};
// For compatibility, if nothing else, loading is verbose by default.
int wxImageRefData::sm_defaultLoadFlags = wxImage::Load_Verbose;
wxImageRefData::wxImageRefData()
{
m_width = 0;
@ -113,6 +120,8 @@ wxImageRefData::wxImageRefData()
m_ok = false;
m_static =
m_staticAlpha = false;
m_loadFlags = sm_defaultLoadFlags;
}
wxImageRefData::~wxImageRefData()
@ -2341,6 +2350,30 @@ bool wxImage::HasOption(const wxString& name) const
// image I/O
// ----------------------------------------------------------------------------
/* static */
void wxImage::SetDefaultLoadFlags(int flags)
{
wxImageRefData::sm_defaultLoadFlags = flags;
}
/* static */
int wxImage::GetDefaultLoadFlags()
{
return wxImageRefData::sm_defaultLoadFlags;
}
void wxImage::SetLoadFlags(int flags)
{
AllocExclusive();
M_IMGDATA->m_loadFlags = flags;
}
int wxImage::GetLoadFlags() const
{
return M_IMGDATA ? M_IMGDATA->m_loadFlags : wxImageRefData::sm_defaultLoadFlags;
}
// Under Windows we can load wxImage not only from files but also from
// resources.
#if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE
@ -2629,7 +2662,8 @@ bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index)
if ( stream.IsSeekable() )
posOld = stream.TellI();
if ( !handler.LoadFile(this, stream, true/*verbose*/, index) )
if ( !handler.LoadFile(this, stream,
(M_IMGDATA->m_loadFlags & Load_Verbose) != 0, index) )
{
if ( posOld != wxInvalidOffset )
stream.SeekI(posOld);