diff --git a/src/emu/rendlay.cpp b/src/emu/rendlay.cpp index d196afa0850..b9e5b413f61 100644 --- a/src/emu/rendlay.cpp +++ b/src/emu/rendlay.cpp @@ -675,17 +675,28 @@ private: // internal helpers void load_bitmap() { - // load the basic bitmap assert(m_file != nullptr); - m_hasalpha = render_load_png(m_bitmap, *m_file, m_dirname.c_str(), m_imagefile.c_str()); - // load the alpha bitmap if specified - if (m_bitmap.valid() && !m_alphafile.empty()) - render_load_png(m_bitmap, *m_file, m_dirname.c_str(), m_alphafile.c_str(), true); + ru_imgformat const format = render_detect_image(*m_file, m_dirname.c_str(), m_imagefile.c_str()); + switch (format) + { + case RENDUTIL_IMGFORMAT_ERROR: + break; - // PNG failed, let's try JPG - if (!m_bitmap.valid()) - render_load_jpeg(m_bitmap, *m_file, m_dirname.c_str(), m_imagefile.c_str()); + case RENDUTIL_IMGFORMAT_PNG: + // load the basic bitmap + m_hasalpha = render_load_png(m_bitmap, *m_file, m_dirname.c_str(), m_imagefile.c_str()); + + // load the alpha bitmap if specified + if (m_bitmap.valid() && !m_alphafile.empty()) + render_load_png(m_bitmap, *m_file, m_dirname.c_str(), m_alphafile.c_str(), true); + break; + + default: + // try JPG + render_load_jpeg(m_bitmap, *m_file, m_dirname.c_str(), m_imagefile.c_str()); + break; + } // if we can't load the bitmap, allocate a dummy one and report an error if (!m_bitmap.valid()) diff --git a/src/emu/rendutil.cpp b/src/emu/rendutil.cpp index bbedfab9dae..d1ffecd7c7b 100644 --- a/src/emu/rendutil.cpp +++ b/src/emu/rendutil.cpp @@ -5,6 +5,7 @@ rendutil.c Core rendering utilities. + ***************************************************************************/ #include "emu.h" @@ -795,3 +796,36 @@ static bool copy_png_alpha_to_bitmap(bitmap_argb32 &bitmap, const png_info &png) // set the hasalpha flag return (accumalpha != 0xff); } + + +/*------------------------------------------------- + render_detect_image - detect image format +-------------------------------------------------*/ + +ru_imgformat render_detect_image(emu_file &file, const char *dirname, const char *filename) +{ + // open the file + std::string fname; + if (dirname) + fname.assign(dirname).append(PATH_SEPARATOR).append(filename); + else + fname.assign(filename); + osd_file::error const filerr = file.open(fname.c_str()); + if (filerr != osd_file::error::NONE) + return RENDUTIL_IMGFORMAT_ERROR; + + // PNG: check for valid header + png_info png; + png_error const result = png.verify_header(file); + if (result == PNGERR_NONE) + { + file.close(); + return RENDUTIL_IMGFORMAT_PNG; + } + + file.seek(0, SEEK_SET); + // TODO: add more when needed + + file.close(); + return RENDUTIL_IMGFORMAT_UNKNOWN; +} diff --git a/src/emu/rendutil.h b/src/emu/rendutil.h index b84fc60f704..1824fb0a34a 100644 --- a/src/emu/rendutil.h +++ b/src/emu/rendutil.h @@ -5,6 +5,7 @@ rendutil.h Core rendering utilities. + ***************************************************************************/ #ifndef MAME_EMU_RENDUTIL_H @@ -17,6 +18,18 @@ #include +/* ----- image formats ----- */ + +enum ru_imgformat +{ + RENDUTIL_IMGFORMAT_PNG, + + RENDUTIL_IMGFORMAT_UNKNOWN, + RENDUTIL_IMGFORMAT_ERROR +}; + + + /*************************************************************************** FUNCTION PROTOTYPES ***************************************************************************/ @@ -29,6 +42,7 @@ bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_q void render_line_to_quad(const render_bounds *bounds, float width, float length_extension, render_bounds *bounds0, render_bounds *bounds1); void render_load_jpeg(bitmap_argb32 &bitmap, emu_file &file, const char *dirname, const char *filename); bool render_load_png(bitmap_argb32 &bitmap, emu_file &file, const char *dirname, const char *filename, bool load_as_alpha_to_existing = false); +ru_imgformat render_detect_image(emu_file &file, const char *dirname, const char *filename);