From 64a95c3b432e9d7bcb7bd072a4f993fd7aeee8fd Mon Sep 17 00:00:00 2001 From: elasota <1137273+elasota@users.noreply.github.com> Date: Wed, 4 Sep 2024 00:06:03 -0400 Subject: [PATCH] IMAGE: Add codec accuracy options --- image/codec-options.h | 35 +++++++++++++++++++++++++++++++++++ image/codecs/codec.h | 7 +++++++ image/codecs/mjpeg.cpp | 6 ++++++ image/codecs/mjpeg.h | 2 ++ image/jpeg.cpp | 10 ++++++++++ image/jpeg.h | 2 ++ 6 files changed, 62 insertions(+) create mode 100644 image/codec-options.h diff --git a/image/codec-options.h b/image/codec-options.h new file mode 100644 index 00000000000..768b0a33b32 --- /dev/null +++ b/image/codec-options.h @@ -0,0 +1,35 @@ +/* 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 . + * + */ + +#ifndef IMAGE_CODEC_OPTIONS_H +#define IMAGE_CODEC_OPTIONS_H + +namespace Image { + +enum class CodecAccuracy { + Fast, + Default, + Accurate, +}; + +} // End of namespace Image + +#endif diff --git a/image/codecs/codec.h b/image/codecs/codec.h index 8109c50766d..0540dda82fb 100644 --- a/image/codecs/codec.h +++ b/image/codecs/codec.h @@ -25,6 +25,8 @@ #include "graphics/surface.h" #include "graphics/pixelformat.h" +#include "image/codec-options.h" + namespace Common { class SeekableReadStream; } @@ -116,6 +118,11 @@ public: */ virtual void setDither(DitherType type, const byte *palette) {} + /** + * Set the decoding accuracy of the codec, if supported + */ + virtual void setCodecAccuracy(CodecAccuracy accuracy) {} + /** * Create a dither table, as used by QuickTime codecs. */ diff --git a/image/codecs/mjpeg.cpp b/image/codecs/mjpeg.cpp index 397553e6366..51d42731a15 100644 --- a/image/codecs/mjpeg.cpp +++ b/image/codecs/mjpeg.cpp @@ -46,6 +46,7 @@ MJPEGDecoder::MJPEGDecoder() : Codec() { _pixelFormat = Graphics::PixelFormat(4, 8, 8, 8, 8, 8, 16, 24, 0); _surface = 0; + _accuracy = CodecAccuracy::Default; } MJPEGDecoder::~MJPEGDecoder() { @@ -204,6 +205,7 @@ const Graphics::Surface *MJPEGDecoder::decodeFrame(Common::SeekableReadStream &s Common::MemoryReadStream convertedStream(data, outputSize, DisposeAfterUse::YES); JPEGDecoder jpeg; + jpeg.setCodecAccuracy(_accuracy); jpeg.setOutputPixelFormat(_pixelFormat); if (!jpeg.loadStream(convertedStream)) { @@ -224,4 +226,8 @@ const Graphics::Surface *MJPEGDecoder::decodeFrame(Common::SeekableReadStream &s return _surface; } +void MJPEGDecoder::setCodecAccuracy(CodecAccuracy accuracy) { + _accuracy = accuracy; +} + } // End of namespace Image diff --git a/image/codecs/mjpeg.h b/image/codecs/mjpeg.h index c68ecc0d85e..ea62520915e 100644 --- a/image/codecs/mjpeg.h +++ b/image/codecs/mjpeg.h @@ -46,12 +46,14 @@ public: ~MJPEGDecoder() override; const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream) override; + void setCodecAccuracy(CodecAccuracy accuracy) override; Graphics::PixelFormat getPixelFormat() const override { return _pixelFormat; } bool setOutputPixelFormat(const Graphics::PixelFormat &format) override { _pixelFormat = format; return true; } private: Graphics::PixelFormat _pixelFormat; Graphics::Surface *_surface; + CodecAccuracy _accuracy; }; } // End of namespace Image diff --git a/image/jpeg.cpp b/image/jpeg.cpp index ffb197ae005..e1340054ea6 100644 --- a/image/jpeg.cpp +++ b/image/jpeg.cpp @@ -47,6 +47,7 @@ namespace Image { JPEGDecoder::JPEGDecoder() : _surface(), _colorSpace(kColorSpaceRGB), + _accuracy(CodecAccuracy::Default), _requestedPixelFormat(getByteOrderRgbPixelFormat()) { } @@ -77,6 +78,10 @@ const Graphics::Surface *JPEGDecoder::decodeFrame(Common::SeekableReadStream &st return getSurface(); } +void JPEGDecoder::setCodecAccuracy(CodecAccuracy accuracy) { + _accuracy = accuracy; +} + Graphics::PixelFormat JPEGDecoder::getPixelFormat() const { return _surface.format; } @@ -243,6 +248,11 @@ bool JPEGDecoder::loadStream(Common::SeekableReadStream &stream) { // Initialize the decompression structure jpeg_create_decompress(&cinfo); + if (_accuracy <= CodecAccuracy::Fast) + cinfo.dct_method = JDCT_FASTEST; + else if (_accuracy >= CodecAccuracy::Accurate) + cinfo.dct_method = JDCT_ISLOW; + // Initialize our buffer handling jpeg_scummvm_src(&cinfo, &stream); diff --git a/image/jpeg.h b/image/jpeg.h index 26b9a87a6c1..c5581f347de 100644 --- a/image/jpeg.h +++ b/image/jpeg.h @@ -58,6 +58,7 @@ public: // Codec API const Graphics::Surface *decodeFrame(Common::SeekableReadStream &stream) override; + void setCodecAccuracy(CodecAccuracy accuracy) override; Graphics::PixelFormat getPixelFormat() const override; bool setOutputPixelFormat(const Graphics::PixelFormat &format) override { _requestedPixelFormat = format; return true; } @@ -100,6 +101,7 @@ private: Graphics::Surface _surface; ColorSpace _colorSpace; Graphics::PixelFormat _requestedPixelFormat; + CodecAccuracy _accuracy; Graphics::PixelFormat getByteOrderRgbPixelFormat() const; };