]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Box art image handling update
authorToni Wilen <twilen@winuae.net>
Sat, 9 Oct 2021 18:00:58 +0000 (21:00 +0300)
committerToni Wilen <twilen@winuae.net>
Sat, 9 Oct 2021 18:00:58 +0000 (21:00 +0300)
od-win32/win32gui_extra.cpp

index 425359e3d0bdd47ad48ff1d19a2c20dce74859b4..2b18c89ec216a106c0a85712f6319f45f0772de4 100644 (file)
@@ -1273,6 +1273,7 @@ int scaleresource_choosefont (HWND hDlg, int fonttype)
 struct imagedata
 {
        Gdiplus::Image *image;
+       IStream *stream;
        TCHAR *metafile;
 };
 
@@ -1374,17 +1375,27 @@ void move_box_art_window(void)
        SetWindowPos(boxarthwnd, HWND_TOPMOST, r.left, r.top, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSIZE);
 }
 
-void close_box_art_window(void)
+static void freeimages(void)
 {
        for (int i = 0; i < MAX_BOX_ART_IMAGES; i++) {
-               if (images[i]) {
-                       struct imagedata *im = images[i];
-                       delete im->image;
+               struct imagedata *im = images[i];
+               if (im) {
+                       if (im->image) {
+                               delete im->image;
+                       }
+                       if (im->stream) {
+                               im->stream->Release();
+                       }
                        xfree(im->metafile);
                        xfree(im);
                        images[i] = NULL;
                }
        }
+}
+
+void close_box_art_window(void)
+{
+       freeimages();
        if (!boxarthwnd)
                return;
        ShowWindow(boxarthwnd, SW_HIDE);
@@ -1564,12 +1575,7 @@ bool show_box_art(const TCHAR *path, const TCHAR *configpath)
 {
        TCHAR tmp1[MAX_DPATH];
 
-       for (int i = 0; i < MAX_BOX_ART_IMAGES; i++) {
-               if (images[i]) {
-                       delete images[i];
-                       images[i] = NULL;
-               }
-       }
+       freeimages();
 
        if (!path) {
                close_box_art_window();
@@ -1672,32 +1678,38 @@ bool show_box_art(const TCHAR *path, const TCHAR *configpath)
                        }
 
                        Gdiplus::Image *image = NULL;
+                       IStream *stream = NULL;
                        for (int imgtype = 0; imgtype < 2 && !image; imgtype++) {
-                               if (imgtype == 1) {
+                               if (imgtype == 1 && _tcslen(tmp1) > 3) {
                                        _tcscpy(tmp1 + _tcslen(tmp1) - 3, _T("jpg"));
                                }
                                int outlen = 0;
                                uae_u8 *filedata = zfile_load_file(tmp1, &outlen);
                                if (filedata) {
-                                       IStream *stream = SHCreateMemStream(filedata, outlen);
+                                       stream = SHCreateMemStream(filedata, outlen);
+                                       xfree(filedata);
                                        if (stream) {
                                                image = Gdiplus::Image::FromStream(stream);
-                                               if (image && image->GetLastStatus() != Gdiplus::Ok) {
+                                               if (image && image->GetLastStatus() == Gdiplus::Ok) {
+                                                       break;
+                                               }
+                                               if (image) {
                                                        delete image;
                                                        image = NULL;
                                                }
                                                stream->Release();
+                                               stream = NULL;
                                        }
-                                       xfree(filedata);
                                }
                        }
-                       if (image) {
+                       if (image && stream) {
                                int w = image->GetWidth();
                                int h = image->GetHeight();
                                write_log(_T("Image '%s' loaded %d*%d\n"), tmp1, w, h);
                                struct imagedata *img = xcalloc(struct imagedata, 1);
                                if (img) {
                                        img->image = image;
+                                       img->stream = stream;
                                        if (metafile[0]) {
                                                img->metafile = my_strdup(metafile);
                                        }
@@ -1709,9 +1721,15 @@ bool show_box_art(const TCHAR *path, const TCHAR *configpath)
                                                total_images++;
                                        }
                                        image = NULL;
+                                       stream = NULL;
                                }
-                               if (image) {
-                                       delete image;
+                               if (image || stream) {
+                                       if (image) {
+                                               delete image;
+                                       }
+                                       if (stream) {
+                                               stream->Release();
+                                       }
                                        break;
                                }
                        }