[Scummvm-git-logs] scummvm master -> 248ab4ca152b742fb2c197be070a6662ecb9006c

sev- noreply at scummvm.org
Thu May 9 13:19:21 UTC 2024


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
248ab4ca15 GRAPHICS: MACGUI: Improve image quality in markdown documents


Commit: 248ab4ca152b742fb2c197be070a6662ecb9006c
    https://github.com/scummvm/scummvm/commit/248ab4ca152b742fb2c197be070a6662ecb9006c
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2024-05-09T15:19:17+02:00

Commit Message:
GRAPHICS: MACGUI: Improve image quality in markdown documents

Changed paths:
  A graphics/image-archive.cpp
  A graphics/image-archive.h
    graphics/macgui/mactext-canvas.cpp
    graphics/macgui/mactext-canvas.h
    graphics/macgui/mactext.cpp
    graphics/macgui/mactext.h
    graphics/module.mk


diff --git a/graphics/image-archive.cpp b/graphics/image-archive.cpp
new file mode 100644
index 00000000000..298d03be796
--- /dev/null
+++ b/graphics/image-archive.cpp
@@ -0,0 +1,101 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "graphics/image-archive.h"
+#include "graphics/surface.h"
+
+#include "common/archive.h"
+#include "common/compression/unzip.h"
+
+#include "image/png.h"
+
+namespace Graphics {
+
+ImageArchive::~ImageArchive() {
+	reset();
+
+	delete _imageArchive;
+}
+
+void ImageArchive::reset() {
+#ifdef USE_PNG
+	for (auto &i : _imageCache)
+		delete i._value;
+	_imageCache.clear();
+#endif
+}
+
+bool ImageArchive::setImageArchive(const Common::Path &fname) {
+	reset();
+
+	delete _imageArchive;
+	_imageArchive = Common::makeZipArchive(fname);
+
+	if (!_imageArchive)
+		warning("ImageArchive::setImageArchive(): Could not find %s. Images will not be rendered", fname.toString().c_str());
+	return _imageArchive != nullptr;
+}
+
+const Surface *ImageArchive::getImageSurface(const Common::Path &fname, int w, int h) {
+#ifndef USE_PNG
+	warning("ImageArchive::getImageSurface(): PNG support not compiled. Cannot load file %s", fname.toString().c_str());
+
+	return nullptr;
+#else
+	if (_imageCache.contains(fname)) {
+		// TODO: Handle the case where a previously loaded image hasn't been scaled.
+		const Surface *surf = _imageCache[fname];
+		if (surf && surf->w == w && surf->h == h)
+			return surf;
+	}
+
+	if (!_imageArchive) {
+		warning("ImageArchive::getImageSurface(): Image Archive was not loaded. Use setImageArchive()");
+		return _imageCache[fname];
+	}
+
+	Common::SeekableReadStream *stream = _imageArchive->createReadStreamForMember(fname);
+
+	if (!stream) {
+		warning("ImageArchive::getImageSurface(): Cannot open file %s", fname.toString().c_str());
+		return _imageCache[fname];
+	}
+
+	Image::PNGDecoder decoder;
+	if (!decoder.loadStream(*stream)) {
+		warning("ImageArchive::getImageSurface(): Cannot load file %s", fname.toString().c_str());
+
+		return _imageCache[fname];
+	}
+
+	if (_imageCache.contains(fname)) {
+		delete _imageCache[fname];
+		_imageCache.erase(fname);
+	}
+
+	const Graphics::Surface *surf = decoder.getSurface();
+	_imageCache[fname] = surf->scale(w ? w : surf->w, h ? h : surf->h, true);
+	return _imageCache[fname];
+#endif // USE_PNG
+}
+
+} // End of namespace Graphics
+
diff --git a/graphics/image-archive.h b/graphics/image-archive.h
new file mode 100644
index 00000000000..6a0315810aa
--- /dev/null
+++ b/graphics/image-archive.h
@@ -0,0 +1,63 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRAPHICS_IMAGE_ARCHIVE_H
+#define GRAPHICS_IMAGE_ARCHIVE_H
+
+#include "common/hashmap.h"
+#include "common/path.h"
+
+namespace Common {
+class Archive;
+}
+
+namespace Graphics {
+struct Surface;
+
+/**
+ * Helper class for loading PNG images from zip files.
+ */
+class ImageArchive {
+public:
+	~ImageArchive();
+
+	/* Frees all previously loaded images. */
+	void reset();
+
+	/* Open a new zip archive, after closing the previous one. */
+	bool setImageArchive(const Common::Path &fname);
+
+	/* Retrieve an image from the cache, or load it from the archive if it hasn't been loaded previously. */
+	const Surface *getImageSurface(const Common::Path &fname) {
+		return getImageSurface(fname, 0, 0);
+	}
+	const Surface *getImageSurface(const Common::Path &fname, int w, int h);
+
+private:
+#ifdef USE_PNG
+	Common::HashMap<Common::Path, Surface *, Common::Path::IgnoreCase_Hash, Common::Path::IgnoreCase_EqualTo> _imageCache;
+#endif
+	Common::Archive *_imageArchive = nullptr;
+};
+
+} // End of namespace Graphics
+
+#endif
diff --git a/graphics/macgui/mactext-canvas.cpp b/graphics/macgui/mactext-canvas.cpp
index 6086d0cbbef..37857a3f916 100644
--- a/graphics/macgui/mactext-canvas.cpp
+++ b/graphics/macgui/mactext-canvas.cpp
@@ -650,13 +650,11 @@ void MacTextCanvas::render(int from, int to, int shadow) {
 
 	for (int i = myFrom; i != myTo; i += delta) {
 		if (!_text[i].picfname.empty()) {
-			const Surface *image = _macText->getImageSurface(_text[i].picfname);
-
-			int xOffset = (_text[i].width - _text[i].charwidth) / 2;
-			Common::Rect bbox(xOffset, _text[i].y, xOffset + _text[i].charwidth, _text[i].y + _text[i].height);
+			const Surface *image = _imageArchive.getImageSurface(_text[i].picfname, _text[i].charwidth, _text[i].height);
 
 			if (image) {
-				surface->blitFrom(image, Common::Rect(0, 0, image->w, image->h), bbox);
+				int xOffset = (_text[i].width - _text[i].charwidth) / 2;
+				surface->blitFrom(image, Common::Point(xOffset, _text[i].y));
 
 				D(9, "MacTextCanvas::render: Image %d x %d bbox: %d, %d, %d, %d", image->w, image->h, bbox.left, bbox.top,
 						bbox.right, bbox.bottom);
@@ -859,7 +857,7 @@ int MacTextCanvas::getLineWidth(int lineNum, bool enforce, int col) {
 		return line->width;
 
 	if (!line->picfname.empty()) {
-		const Surface *image = _macText->getImageSurface(line->picfname);
+		const Surface *image = _imageArchive.getImageSurface(line->picfname);
 
 		if (image) {
 			line->width = _maxWidth;
diff --git a/graphics/macgui/mactext-canvas.h b/graphics/macgui/mactext-canvas.h
index 501ab0d722d..7e53a3b6cd3 100644
--- a/graphics/macgui/mactext-canvas.h
+++ b/graphics/macgui/mactext-canvas.h
@@ -23,6 +23,7 @@
 #define GRAPHICS_MACGUI_MACTEXTCANVAS_H
 
 #include "graphics/macgui/macwindowmanager.h"
+#include "graphics/image-archive.h"
 
 namespace Graphics {
 
@@ -122,6 +123,7 @@ public:
 	bool _macFontMode = true;
 	MacText *_macText;
 	MacFontRun _defaultFormatting;
+	ImageArchive _imageArchive;
 
 public:
 	~MacTextCanvas();
diff --git a/graphics/macgui/mactext.cpp b/graphics/macgui/mactext.cpp
index f6903c45950..4879f224073 100644
--- a/graphics/macgui/mactext.cpp
+++ b/graphics/macgui/mactext.cpp
@@ -21,14 +21,9 @@
 
 #include "common/file.h"
 #include "common/timer.h"
-#include "common/compression/unzip.h"
 
 #include "graphics/macgui/mactext.h"
 
-#ifdef USE_PNG
-#include "image/png.h"
-#endif
-
 namespace Graphics {
 
 enum {
@@ -267,13 +262,6 @@ MacText::~MacText() {
 	delete _cursorRect;
 	delete _cursorSurface;
 	delete _cursorSurface2;
-
-#ifdef USE_PNG
-	for (auto &i : _imageCache)
-		delete i._value;
-#endif
-
-	delete _imageArchive;
 }
 
 // this func returns the fg color of the first character we met in text
@@ -2026,46 +2014,4 @@ void MacText::undrawCursor() {
 	_composeSurface->blitFrom(*_cursorSurface2, *_cursorRect, Common::Point(_cursorX + offset.x, _cursorY + offset.y));
 }
 
-void MacText::setImageArchive(const Common::Path &fname) {
-	_imageArchive = Common::makeZipArchive(fname);
-
-	if (!_imageArchive)
-		warning("MacText::setImageArchive(): Could not find %s. Images will not be rendered", fname.toString().c_str());
-}
-
-const Surface *MacText::getImageSurface(const Common::Path &fname) {
-#ifndef USE_PNG
-	warning("MacText::getImageSurface(): PNG support not compiled. Cannot load file %s", fname.toString().c_str());
-
-	return nullptr;
-#else
-	if (_imageCache.contains(fname))
-		return _imageCache[fname]->getSurface();
-
-	if (!_imageArchive) {
-		warning("MacText::getImageSurface(): Image Archive was not loaded. Use setImageArchive()");
-		return nullptr;
-	}
-
-	Common::SeekableReadStream *stream = _imageArchive->createReadStreamForMember(fname);
-
-	if (!stream) {
-		warning("MacText::getImageSurface(): Cannot open file %s", fname.toString().c_str());
-		return nullptr;
-	}
-
-	_imageCache[fname] = new Image::PNGDecoder();
-
-	if (!_imageCache[fname]->loadStream(*stream)) {
-		delete _imageCache[fname];
-
-		warning("MacText::getImageSurface(): Cannot load file %s", fname.toString().c_str());
-
-		return nullptr;
-	}
-
-	return _imageCache[fname]->getSurface();
-#endif // USE_PNG
-}
-
 } // End of namespace Graphics
diff --git a/graphics/macgui/mactext.h b/graphics/macgui/mactext.h
index b87f038d7a9..7ad521e2ac6 100644
--- a/graphics/macgui/mactext.h
+++ b/graphics/macgui/mactext.h
@@ -24,10 +24,6 @@
 
 #include "graphics/macgui/mactext-canvas.h"
 
-namespace Image {
-class PNGDecoder;
-}
-
 namespace Graphics {
 
 struct SelectedText {
@@ -122,7 +118,7 @@ public:
 	int getMouseLine(int x, int y);
 	Common::U32String getMouseLink(int x, int y);
 
-	void setImageArchive(const Common::Path &name);
+	bool setImageArchive(const Common::Path &name) { return _canvas._imageArchive.setImageArchive(name); }
 
 private:
 	MacFontRun getTextChunks(int start, int end);
@@ -184,7 +180,6 @@ public:
 	// Markdown
 public:
 	void setMarkdownText(const Common::U32String &str);
-	const Surface *getImageSurface(const Common::Path &fname);
 
 private:
 	void init(uint32 fgcolor, uint32 bgcolor, int maxWidth, TextAlign textAlignment, int interlinear, uint16 textShadow, bool macFontMode);
@@ -241,11 +236,6 @@ private:
 	SelectedText _selectedText;
 
 	MacMenu *_menu;
-
-#ifdef USE_PNG
-	Common::HashMap<Common::Path, Image::PNGDecoder *, Common::Path::IgnoreCase_Hash, Common::Path::IgnoreCase_EqualTo> _imageCache;
-#endif
-	Common::Archive *_imageArchive = nullptr;
 };
 
 int getStringWidth(MacFontRun &format, const Common::U32String &str);
diff --git a/graphics/module.mk b/graphics/module.mk
index 1013dfea3e0..775adce3df0 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -20,6 +20,7 @@ MODULE_OBJS := \
 	fonts/ttf.o \
 	fonts/winfont.o \
 	framelimiter.o \
+	image-archive.o \
 	korfont.o \
 	larryScale.o \
 	maccursor.o \




More information about the Scummvm-git-logs mailing list