[Scummvm-git-logs] scummvm master -> 8aa3e5313263481353334bff62c06c4174c1aa38

bluegr bluegr at gmail.com
Mon Mar 15 14:50:26 UTC 2021


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

Summary:
a38103fb23 IOS7: Remove scaler code
c8dcb9105f SDL: Temporarily remove code for handling scaler hotkeys
26e2a9711f GPH/DINGUX: Remove downscaling support
a3af191753 GRAPHICS/BASE: Adds definitions for scaler plugins
7bcc1f0b13 GRAPHICS: Adds a nearest neighbor plugin
29466870e8 BACKENDS: Changes scaler code to use plugin.
202f4efdee GRAPHICS: Changes name of normal plugin to be consistant
318130a905 BACKENDS/GRAPHICS: Stops overlay from being scaled
60c430d6e5 GRAPHICS: adds additinoal methods to scaler api
739c75c869 BACKEND: fixes some errors from changing modes
0d103d01c4 GRAPHICS: adds comments for scalers and clean up
bdb12c8733 SDL: add support for additional scaler plugins
83a8abda70 GRAPHICS: add support for hq scaler plugin
ef2b057eef GRAPHICS: adds 2xsai as scaler plugin
e69ec141aa BACKEND: fixes a bug related to AR correction
a0299cf5d2 GRAPHICS: fixes the value of extraPixels for 2xsai
489d361d18 GRAPHICS: fix formatting to conform to guidelines
ca1754c047 GRAPHICS/BASE: add maxExtraPixels() to ScalerMan
9402e2b43f SDL: clarifies code
8c9cb22734 GRAPHICS/SDL/BASE: fix scaler declarations
6320ea772e GRAPHICS: small formatting change in HQPlugin
b70a04214b GRAPHICS/BASE: adds Dotmatrix scaler plugin
76d4a5425c SDL: pass correct PixelFormat to scaler initialize
f015c0f835 SDL: use correct x and y for scale function
a54ea85641 GRAPHICS: fix dotmatrix inconsistencies
d350e2c7a8 SDL: use maxExtraPixels from ScalerMan
d56a7d610c GRAPHICS: add static methods for 1x and 1.5x scale
c802ab3b60 GRAPHICS: add setFactor to scalerplugins
aff49f3e2e SDL: mouse uses scaler plugins
0fe3419349 SDL: cache value of extraPixels() from scaler
64f3e5191a SDL: add convertSDLPixelFormat
70426d5e74 GRAPHICS: add getFactors() for scaler plugins
45ad2a4882 GRAPHICS: add PrettyName to scaler api for gui
a434253f7f SDL: Generate graphics modes from plugins
e6fac85afe SDL: scaler plugins use graphics modes
c65e0146dd SDL: remove unused table for cursor scaling
a55f51d01a SDL: add table for old gfxmode strings
bc03b567bb GRAPHICS: fix formatting
9091be08ea SDL: fix formatting
277c9a3a6b SDL: add const to legacy names table
fa3f6321a2 SDL: remove unused variable
4bdd8860be SDL: remove statically initialized object
2809ada459 SDL: legacy names for scalers corrected
8b00ee4873 GRAPHICS: added 4x scaling to normal scaler plugin
3baf738de8 SDL: change gfx_mode legacy names in game domains
fe6fb4b597 SDL: cache the list of scaler plugins
81ca88676c SDL: remove some debugging output
9d2eca9383 GRAPHICS: remove 1x scaling for non-normal scalers
90e855d1ab GRAPHICS: Move HQ init code from InitScalers to the plugin
c8bcb3912f SDL/GRAPHICS: remove disableScaling() hack from scaler plugins
dcfbf9da7f GRAPHICS: Unify hq scalers with plugin
8030b7879c GRAPHICS/SDL: add ifdefs for disabling scalers
5e73533110 GRAPHICS: Add SuperEagle and SuperSAI scaler plugins
20b4356ecd GRAPHICS: Add AdvMame scaler plugin
e620b79f15 GRAPHICS: add 4x scaling to AdvMame plugin
4b29408fb1 SDL: Remove references to old scaler data
044d8aea63 SDL: Fix signedness of some types
dc29ae5447 SDL: Remove old scaler init and destroy code
894e8fb187 SDL: Add comments to explain scaler state changes
a15689ae38 SDL: Fix formatting
7fd8107c76 GRAPHICS: Move Normal scaler functions into same file as plugin
a3a9736754 GRAPHICS: Add TV scaler plugin
3a42496719 GRAPHICS: Remove 1.5x scaler
301197b73b GRAPHICS: Remove unused scaler definitions
c2639a9ff6 GRAPHICS: Fix formatting in template
ff6fe4360d SDL: Change so that the Normal plugin is initialized
780d5a8689 GRAPHICS: Add 4 byte per pixel support for Normal scalers
b08da82253 GRAPHICS: Add 32bpp support for the sai family of scalers
4432c5a4d1 GRAPHICS: Add 32bpp color support for HQ scalers
3d357dc9d1 GRAPHICS: Fix Errors in ifdefs and switches for asm hq scalers
c7e874e2cd GRAPHICS: Add 32bpp support to dotmatrix scaler
4a69e136d2 GRAPHICS: Add 32bpp support for TV scaler
d91e970f4e GRAPHICS: Fix error in dotmatrix scaler initialization
267b385890 GRAPHICS: Add 2xPM scaler with 16 and 32 bit support
9ed7fde732 GRAPHICS: Capitalized pixel in template parameters
592617e2ce GRAPHICS: Add Edge2x/3x scaler
e77934b73d GRAPHICS: Change Edge filter to use shared interpolation functions
457e974c86 GRAPHICS: Use correct copyright notice in edge.cpp
fc3ee1989d GRAPHICS: Change another interpolation function in Edge filter.
b2dff9413e GRAPHICS: Rename functions in Edge filter according to guidelines
9ba4c24ae1 GRAPHICS: Migrate global funcs and vars to Edge Plugin class
5688622889 GRAPHICS: Move static variables to class members in Edge Plugin
f4e0331f2d GRAPHICS: Run edge.cpp through astyle to conform to style guidelines
a4741a7268 GRAPHICS: Template Edge plugin over pixel format
18a099c7d3 GRAPHICS: Remove unused variable in Edge Plugin
664ba9ae5d GRAPHICS: Change variable name to remove shadowing
3574823828 GRAPHICS: Remove code from Edge plugin that will never be used.
917ef83043 GRAPHICS: Add more templating to Edge plugin
c9b63ba7e3 GRAPHICS: Remove more unused code from the Edge plugin.
c84db891bf GRAPHICS: Remove unused define in edge.cpp.
4552ec6138 GRAPHICS: Add function to convert pixels to 16bpp format
0dcd6bae37 GRAPHICS: 32bpp-ize chooseGreyscale for Edge plugin
a6e6f7676d GRAPHICS: Remove unused parameter to findPricipleAxis in Edge plugin
f40485ee37 GRAPHICS: Move and format comments from edge.cpp to edge.h
ffb52288a9 GRAPHICS: Add useOldSrc and oldSrcScale to scaler plugin API
cfbc3e91cf SDL: Use oldSrcScale for scaler plugins that support it
e2484a0fba SDL: Swap and lock the old screen
3b408c2d87 SDL: Use an extra buffer for oldSrcScale so it is not confused by AR
1e724e6640 GRAPHICS: Use new oldSrcScale API for EdgePlugin
6cfba9d567 SDL: Only update if a change was made when using oldSrcScale
8baa92a190 SDL: Fix overlay while using oldSrcScale
86c2307351 GRAPHICS: Add some 32bpp abilty to Edge scalers
56d078e732 SDL: Fix assignment of variable
179705071d GRAPHICS: Fix Edge3x scaler
c915efa452 GRAPHICS: Respect alpha and rgba formats in interpolation and conversion
c5c5662330 GRAPHICS,SDL: Change oldSrcScale api
cb5c323b54 GRAPHICS: Constify the src in setSource
4c967c5fbc GRAPHICS,SDL: Revision to scaler API
4c4028cf1d GRAPHICS: Various formatting fixes
3e9bd387e7 GRAPHICS: Clean up Edge plugin source
a41cd52bbf SDL: Use a simple blit function instead of normal scaler plugin
853d545284 SDL: Make sure normal scaler plugin is initialized
ab8dd417a9 SDL: Use enableSource() when scaling to the overlay
cbf9072e45 SDL: Fix leaks related to graphics modes
ee008a44b1 SDL: Formatting fix
f9569ea2ff SDL: Fix formatting
f172718b79 CONFIGURE: Added configure option for Edge2x-3x scaler plugin
88363d5d36 SDL: Fix error when USE_SCALERS is undefined
e35a7c72c0 SDL: Cache default graphics mode upon initialization.
c7121fae65 SDL: Fix formatting for pointers.
d8240fa734 SDL: Initialize plugins when formats change.
b80c7908fa SDL: Update comments for scaling mouse cursor
4674c7c7b0 GRAPHICS: Check bytesPerPixel when USE_SCALERS is undefined
39608f391c SDL,GRAPHICS: Add option for AR correction without scalers
f4f9957f85 scalerplugin: port to new plugin api
fcff230fe8 fix build
ab000b6ae3 fix cursor
9296b92d8f fix crash on exit
b78323897d SDL: Fix compilation
ee438bebc2 SDL: Disable more AR related code when USE_ASPECT is not defined.
b225858e41 GRAPHICS: Let ScalerPluginObject::setFactor return the old factor.
6942dba3da GRAPHICS: Let each scaler handle 1x scaling.
e8d0182cd5 SDL: Take advantage that each ScalerPluginObject can use a scale factor of 1.
abbdadf869 GRAPHICS: Add possibility to search for a scaler via its name.
23bee4de30 SDL: Properly look up the normal scaler plugin.
5420a7f6bd GRAPHICS: Add a default implementation for ScalerPluginObject::getFactor.
8825c9e3b4 GRAPHICS: Keep destination buffer in SourceScaler instead of SDL backend.
088119b57c GRAPHICS: Very small cleanup for Edge scaler code.
5dc207a386 SDL: Properly handle extra pixels for scalers in showOverlay.
5df18f7d86 GRAPHICS: Take advantage of ColorMasks::PixelType in scaler code.
9030ac7305 GRAPHICS: Remove unused code
97e176a098 GRAPHICS: Increase the number of extra pixels required by AdvMame4x
12878afc06 SDL: Fix building without scalers
e50e010a9e SDL: Fix compilation when building without RGB colour support
6a6cdc2bce GRAPHICS: Use override and final keywords
99acdb00f9 SDL: Fix crash when switching from OpenGL to Edge3x
b68dbf9eec GRAPHICS: Ensure that _bufferedOutput is resized when the scale factor changes
4df5e9ed51 SDL: Fix crash with small cursors when using AdvMame4x
21c1f0d007 SDL: Restore keyboard shortcuts for switching scalers
621c4c2e9e SDL: Fix implementation of getGraphicsModeScale
035f5798c4 SDL: Remove the old graphics mode enum
f6c8207684 SDL: Minor cleanup to the aspect ratio code
40ee8ae527 SDL: Fix filtering with SDL 1.2
b596ebf8b8 GRAPHICS: Remove use of gBitFormat from the aspect ratio code
8aa3e53132 SDL: Replace the use of the normal scaler for the cursor


Commit: a38103fb2344e8d3ccff9772c6ab08c40d339c5d
    https://github.com/scummvm/scummvm/commit/a38103fb2344e8d3ccff9772c6ab08c40d339c5d
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
IOS7: Remove scaler code

Changed paths:
    backends/platform/ios7/ios7_common.h
    backends/platform/ios7/ios7_osys_main.cpp
    backends/platform/ios7/ios7_osys_main.h
    backends/platform/ios7/ios7_osys_video.mm
    backends/platform/ios7/ios7_video.h
    backends/platform/ios7/ios7_video.mm


diff --git a/backends/platform/ios7/ios7_common.h b/backends/platform/ios7/ios7_common.h
index 1d3d3fbfdd..f88ed51395 100644
--- a/backends/platform/ios7/ios7_common.h
+++ b/backends/platform/ios7/ios7_common.h
@@ -25,8 +25,6 @@
 
 #include "graphics/surface.h"
 
-#define ENABLE_IOS7_SCALERS
-
 
 enum InputEvent {
 	kInputMouseDown,
@@ -65,26 +63,11 @@ enum UIViewTapDescription {
 	kUIViewTapDouble = 2
 };
 
-enum GraphicsModes {
-	kGraphicsModeNone = 1,
-
-	kGraphicsMode2xSaI,
-	kGraphicsModeSuper2xSaI,
-	kGraphicsModeSuperEagle,
-	kGraphicsModeAdvMame2x,
-	kGraphicsModeAdvMame3x,
-	kGraphicsModeHQ2x,
-	kGraphicsModeHQ3x,
-	kGraphicsModeTV2x,
-	kGraphicsModeDotMatrix
-};
-
 struct VideoContext {
 	VideoContext() : asprectRatioCorrection(), screenWidth(), screenHeight(), overlayVisible(false),
 	                 overlayWidth(), overlayHeight(), mouseX(), mouseY(),
 	                 mouseHotspotX(), mouseHotspotY(), mouseWidth(), mouseHeight(),
-	                 mouseIsVisible(), graphicsMode(kGraphicsModeNone), filtering(false),
-	                 shakeXOffset(), shakeYOffset() {
+	                 mouseIsVisible(), filtering(false), shakeXOffset(), shakeYOffset() {
 	}
 
 	// Game screen state
@@ -105,7 +88,6 @@ struct VideoContext {
 	Graphics::Surface mouseTexture;
 
 	// Misc state
-	GraphicsModes graphicsMode;
 	bool filtering;
 	int shakeXOffset;
 	int shakeYOffset;
diff --git a/backends/platform/ios7/ios7_osys_main.cpp b/backends/platform/ios7/ios7_osys_main.cpp
index 02dad85f0d..60e9406243 100644
--- a/backends/platform/ios7/ios7_osys_main.cpp
+++ b/backends/platform/ios7/ios7_osys_main.cpp
@@ -52,35 +52,9 @@
 #include "audio/mixer.h"
 #include "audio/mixer_intern.h"
 
-#include "graphics/scaler.h"
-#include "graphics/scaler/aspect.h"
-
 #include "backends/platform/ios7/ios7_osys_main.h"
 
 
-const OSystem::GraphicsMode OSystem_iOS7::s_supportedGraphicsModes[] = {
-	{ "none", "Normal", kGraphicsModeNone },
-
-#ifdef ENABLE_IOS7_SCALERS
-#ifdef USE_SCALERS
-//	{"2x", "2x", GFX_DOUBLESIZE},
-//	{"3x", "3x", GFX_TRIPLESIZE},
-	{ "2xsai", "2xSAI", kGraphicsMode2xSaI},
-	{"super2xsai", "Super2xSAI", kGraphicsModeSuper2xSaI},
-	{"supereagle", "SuperEagle", kGraphicsModeSuperEagle},
-	{"advmame2x", "AdvMAME2x", kGraphicsModeAdvMame2x},
-	{"advmame3x", "AdvMAME3x", kGraphicsModeAdvMame3x},
-#ifdef USE_HQ_SCALERS
-	{"hq2x", "HQ2x", kGraphicsModeHQ2x},
-	{"hq3x", "HQ3x", kGraphicsModeHQ3x},
-#endif
-	{"tv2x", "TV2x", kGraphicsModeTV2x},
-	{"dotmatrix", "DotMatrix", kGraphicsModeDotMatrix},
-#endif
-#endif
-	{ 0, 0, 0 }
-};
-
 AQCallbackStruct OSystem_iOS7::s_AudioQueue;
 SoundProc OSystem_iOS7::s_soundCallback = NULL;
 void *OSystem_iOS7::s_soundParam = NULL;
diff --git a/backends/platform/ios7/ios7_osys_main.h b/backends/platform/ios7/ios7_osys_main.h
index 71f12a4d56..fc100ace18 100644
--- a/backends/platform/ios7/ios7_osys_main.h
+++ b/backends/platform/ios7/ios7_osys_main.h
@@ -56,7 +56,6 @@ struct AQCallbackStruct {
 
 class OSystem_iOS7 : public EventsBaseBackend, public ModularMutexBackend, public PaletteManager {
 protected:
-	static const OSystem::GraphicsMode s_supportedGraphicsModes[];
 	static AQCallbackStruct s_AudioQueue;
 	static SoundProc s_soundCallback;
 	static void *s_soundParam;
@@ -132,10 +131,6 @@ public:
 	virtual bool hasFeature(Feature f) override;
 	virtual void setFeatureState(Feature f, bool enable) override;
 	virtual bool getFeatureState(Feature f) override;
-	virtual const GraphicsMode *getSupportedGraphicsModes() const override;
-	virtual int getDefaultGraphicsMode() const override;
-	virtual bool setGraphicsMode(int mode, uint flags = kGfxModeNoFlags) override;
-	virtual int getGraphicsMode() const override;
 	virtual void initSize(uint width, uint height, const Graphics::PixelFormat *format) override;
 
 	virtual void beginGFXTransaction() override;
diff --git a/backends/platform/ios7/ios7_osys_video.mm b/backends/platform/ios7/ios7_osys_video.mm
index 8de628a901..748fbd9c24 100644
--- a/backends/platform/ios7/ios7_osys_video.mm
+++ b/backends/platform/ios7/ios7_osys_video.mm
@@ -99,38 +99,6 @@ void OSystem_iOS7::initVideoContext() {
 	_videoContext = [[iOS7AppDelegate iPhoneView] getVideoContext];
 }
 
-const OSystem::GraphicsMode *OSystem_iOS7::getSupportedGraphicsModes() const {
-	return s_supportedGraphicsModes;
-}
-
-int OSystem_iOS7::getDefaultGraphicsMode() const {
-	return kGraphicsModeNone;
-}
-
-bool OSystem_iOS7::setGraphicsMode(int mode, uint /*flags*/) {
-	switch (mode) {
-	case kGraphicsModeNone:
-	case kGraphicsMode2xSaI:
-	case kGraphicsModeSuper2xSaI:
-	case kGraphicsModeSuperEagle:
-	case kGraphicsModeAdvMame2x:
-	case kGraphicsModeAdvMame3x:
-	case kGraphicsModeHQ2x:
-	case kGraphicsModeHQ3x:
-	case kGraphicsModeTV2x:
-	case kGraphicsModeDotMatrix:
-		_videoContext->graphicsMode = (GraphicsModes)mode;
-		return true;
-
-	default:
-		return false;
-	}
-}
-
-int OSystem_iOS7::getGraphicsMode() const {
-	return _videoContext->graphicsMode;
-}
-
 #ifdef USE_RGB_COLOR
 Common::List<Graphics::PixelFormat> OSystem_iOS7::getSupportedFormats() const {
 	Common::List<Graphics::PixelFormat> list;
diff --git a/backends/platform/ios7/ios7_video.h b/backends/platform/ios7/ios7_video.h
index a8d0d6b064..2b3ac2b8c1 100644
--- a/backends/platform/ios7/ios7_video.h
+++ b/backends/platform/ios7/ios7_video.h
@@ -35,7 +35,6 @@
 #include "backends/platform/ios7/ios7_common.h"
 
 #include "common/list.h"
-#include "graphics/scaler.h"
 
 typedef struct {
 	GLfloat x, y;
@@ -92,15 +91,6 @@ typedef struct {
 
 	UITouch *_firstTouch;
 	UITouch *_secondTouch;
-
-#ifdef ENABLE_IOS7_SCALERS
-	uint8_t *_scalerMemorySrc;
-	uint8_t *_scalerMemoryDst;
-	size_t _scalerMemorySrcSize;
-	size_t _scalerMemoryDstSize;
-	int _scalerScale;
-	ScalerProc *_scaler;
-#endif
 }
 
 - (id)initWithFrame:(struct CGRect)frame;
diff --git a/backends/platform/ios7/ios7_video.mm b/backends/platform/ios7/ios7_video.mm
index d071e07786..0be0adf682 100644
--- a/backends/platform/ios7/ios7_video.mm
+++ b/backends/platform/ios7/ios7_video.mm
@@ -422,23 +422,10 @@ uint getSizeNextPOT(uint size) {
 
 	_backgroundSaveStateTask = UIBackgroundTaskInvalid;
 
-#if defined(USE_SCALERS) || defined(USE_HQ_SCALERS)
-	InitScalers(565);
-#endif
-
 	[self setupGestureRecognizers];
 
 	[self setContentScaleFactor:[[UIScreen mainScreen] scale]];
 
-#ifdef ENABLE_IOS7_SCALERS
-	_scalerMemorySrc = NULL;
-	_scalerMemoryDst = NULL;
-	_scalerMemorySrcSize = 0;
-	_scalerMemoryDstSize = 0;
-	_scaler = NULL;
-	_scalerScale = 1;
-#endif
-
 	_keyboardView = nil;
 	_keyboardVisible = NO;
 	_screenTexture = 0;
@@ -470,11 +457,6 @@ uint getSizeNextPOT(uint size) {
 	_videoContext.overlayTexture.free();
 	_videoContext.mouseTexture.free();
 
-#ifdef ENABLE_IOS7_SCALERS
-	free(_scalerMemorySrc);
-	free(_scalerMemoryDst);
-#endif
-
 	[_eventLock release];
 	[super dealloc];
 }
@@ -497,79 +479,10 @@ uint getSizeNextPOT(uint size) {
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); printOpenGLError();
 }
 
-#ifdef ENABLE_IOS7_SCALERS
-- (void)setScaler {
-	ScalerProc *scaler = NULL;
-	int scalerScale = 1;
-
-	switch (_videoContext.graphicsMode) {
-	case kGraphicsModeNone:
-		break;
-#ifdef USE_SCALERS
-	case kGraphicsMode2xSaI:
-		scaler = _2xSaI;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeSuper2xSaI:
-		scaler = Super2xSaI;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeSuperEagle:
-		scaler = SuperEagle;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeAdvMame2x:
-		scaler = AdvMame2x;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeAdvMame3x:
-		scaler = AdvMame3x;
-		scalerScale = 3;
-		break;
-
-#ifdef USE_HQ_SCALERS
-	case kGraphicsModeHQ2x:
-		scaler = HQ2x;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeHQ3x:
-		scaler = HQ3x;
-		scalerScale = 3;
-		break;
-#endif
-
-	case kGraphicsModeTV2x:
-		scaler = TV2x;
-		scalerScale = 2;
-		break;
-
-	case kGraphicsModeDotMatrix:
-		scaler = DotMatrix;
-		scalerScale = 2;
-		break;
-#endif
-
-	default:
-		break;
-	}
-
-	_scaler = scaler;
-	_scalerScale = scalerScale;
-}
-#endif
-
 - (void)setGraphicsMode {
 	[self setFilterModeForTexture:_screenTexture];
 	[self setFilterModeForTexture:_overlayTexture];
 	[self setFilterModeForTexture:_mouseCursorTexture];
-#ifdef ENABLE_IOS7_SCALERS
-	[self setScaler];
-#endif
 }
 
 - (void)updateSurface {
@@ -663,40 +576,7 @@ uint getSizeNextPOT(uint size) {
 	// Unfortunately we have to update the whole texture every frame, since glTexSubImage2D is actually slower in all cases
 	// due to the iPhone internals having to convert the whole texture back from its internal format when used.
 	// In the future we could use several tiled textures instead.
-#ifdef ENABLE_IOS7_SCALERS
-	if (_scaler) {
-		size_t neededSrcMemorySize = (size_t) (_videoContext.screenTexture.pitch * (_videoContext.screenTexture.h + 4));
-		size_t neededDstMemorySize = (size_t) (_videoContext.screenTexture.pitch * (_videoContext.screenTexture.h + 4) * _scalerScale * _scalerScale);
-		if (neededSrcMemorySize != _scalerMemorySrcSize) {
-			_scalerMemorySrc = (uint8_t *) realloc(_scalerMemorySrc, neededSrcMemorySize);
-			_scalerMemorySrcSize = neededSrcMemorySize;
-		}
-		if (neededDstMemorySize != _scalerMemoryDstSize) {
-			_scalerMemoryDst = (uint8_t *) realloc(_scalerMemoryDst, neededDstMemorySize);
-			_scalerMemoryDstSize = neededDstMemorySize;
-		}
-
-		// Clear two lines before
-		memset(_scalerMemorySrc, 0, (size_t) (_videoContext.screenTexture.pitch * 2));
-		// Copy original buffer
-		memcpy(_scalerMemorySrc + _videoContext.screenTexture.pitch * 2, _videoContext.screenTexture.getPixels(), _videoContext.screenTexture.pitch * _videoContext.screenTexture.h);
-		// Clear two lines after
-		memset(_scalerMemorySrc + _videoContext.screenTexture.pitch * (2 + _videoContext.screenTexture.h), 0, (size_t) (_videoContext.screenTexture.pitch * 2));
-		// Apply scaler
-		_scaler(_scalerMemorySrc + _videoContext.screenTexture.pitch * 2,
-		        _videoContext.screenTexture.pitch,
-		        _scalerMemoryDst,
-		        (uint32) (_videoContext.screenTexture.pitch * _scalerScale),
-		        _videoContext.screenTexture.w,
-		        _videoContext.screenTexture.h);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _videoContext.screenTexture.w * _scalerScale, _videoContext.screenTexture.h * _scalerScale, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, _scalerMemoryDst); printOpenGLError();
-	}
-	else {
-#endif
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _videoContext.screenTexture.w, _videoContext.screenTexture.h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, _videoContext.screenTexture.getPixels()); printOpenGLError();
-#ifdef ENABLE_IOS7_SCALERS
-	}
-#endif
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _videoContext.screenTexture.w, _videoContext.screenTexture.h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, _videoContext.screenTexture.getPixels()); printOpenGLError();
 
 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); printOpenGLError();
 }


Commit: c8dcb9105fd2b3c3e3a85445b382828e18be12e8
    https://github.com/scummvm/scummvm/commit/c8dcb9105fd2b3c3e3a85445b382828e18be12e8
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Temporarily remove code for handling scaler hotkeys

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index c4c9ed21e1..93d4c3a6a0 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2429,60 +2429,6 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
 	recalculateDisplayAreas();
 }
 
-void SurfaceSdlGraphicsManager::handleScalerHotkeys(int scalefactor, int scalerType) {
-	assert(scalerType >= 0 && scalerType < ARRAYSIZE(s_gfxModeSwitchTable));
-
-	int factor = CLIP(scalefactor - 1, 0, 4);
-
-	while (s_gfxModeSwitchTable[scalerType][factor] < 0) {
-		assert(factor > 0);
-		factor--;
-	}
-
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-	bool sizeChanged = _videoMode.scaleFactor != factor;
-#endif
-
-	int newMode = s_gfxModeSwitchTable[scalerType][factor];
-	if (newMode >= 0) {
-		_scalerType = scalerType;
-
-		beginGFXTransaction();
-			setGraphicsMode(newMode);
-		endGFXTransaction();
-#ifdef USE_OSD
-		const char *newScalerName = 0;
-		const OSystem::GraphicsMode *g = getSupportedGraphicsModes();
-		while (g->name) {
-			if (g->id == _videoMode.mode) {
-				newScalerName = g->description;
-				break;
-			}
-			g++;
-		}
-		if (newScalerName) {
-			const Common::U32String message = Common::U32String::format(
-				"%S %s\n%d x %d -> %d x %d",
-				_("Active graphics filter:").c_str(),
-				newScalerName,
-				_videoMode.screenWidth, _videoMode.screenHeight,
-				_hwScreen->w, _hwScreen->h);
-			displayMessageOnOSD(message);
-		}
-#endif
-
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-		if (sizeChanged) {
-			// Forcibly resizing the window here since a user switching scaler
-			// size will not normally cause the window to update
-			_window->createOrUpdateWindow(_hwScreen->w, _hwScreen->h, _lastFlags);
-		}
-#endif
-
-		internUpdateScreen();
-	}
-}
-
 bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 	if (event.type != Common::EVENT_CUSTOM_BACKEND_ACTION_START) {
 		return SdlGraphicsManager::notifyEvent(event);
@@ -2561,25 +2507,6 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 	}
 #endif
 
-	case kActionIncreaseScaleFactor:
-		handleScalerHotkeys(_videoMode.scaleFactor + 1, _scalerType);
-		return true;
-
-	case kActionDecreaseScaleFactor:
-		handleScalerHotkeys(_videoMode.scaleFactor - 1, _scalerType);
-		return true;
-
-	case kActionSetScaleFilter1:
-	case kActionSetScaleFilter2:
-	case kActionSetScaleFilter3:
-	case kActionSetScaleFilter4:
-	case kActionSetScaleFilter5:
-	case kActionSetScaleFilter6:
-	case kActionSetScaleFilter7:
-	case kActionSetScaleFilter8:
-		handleScalerHotkeys(_videoMode.scaleFactor, event.customType - kActionSetScaleFilter1);
-		return true;
-
 	default:
 		return SdlGraphicsManager::notifyEvent(event);
 	}
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 7172a6080e..926382882a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -424,7 +424,6 @@ protected:
 
 private:
 	void setFullscreenMode(bool enable);
-	void handleScalerHotkeys(int scalefactor, int scalerType);
 
 	/**
 	 * Converts the given point from the overlay's coordinate space to the


Commit: 26e2a9711fe5a20560572edb77cb53481f1dfd3b
    https://github.com/scummvm/scummvm/commit/26e2a9711fe5a20560572edb77cb53481f1dfd3b
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GPH/DINGUX: Remove downscaling support

Changed paths:
  R backends/graphics/downscalesdl/downscalesdl-graphics.cpp
  R backends/graphics/downscalesdl/downscalesdl-graphics.h
    backends/graphics/gph/gph-graphics.cpp
    backends/graphics/gph/gph-graphics.h
    backends/platform/dingux/dingux.cpp
    configure


diff --git a/backends/graphics/downscalesdl/downscalesdl-graphics.cpp b/backends/graphics/downscalesdl/downscalesdl-graphics.cpp
deleted file mode 100644
index 2413d3f1a7..0000000000
--- a/backends/graphics/downscalesdl/downscalesdl-graphics.cpp
+++ /dev/null
@@ -1,477 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "common/scummsys.h"
-
-#if (defined(GPH_DEVICE) || defined(DINGUX)) && defined(USE_SCALERS)
-
-#include "backends/graphics/downscalesdl/downscalesdl-graphics.h"
-#include "backends/events/sdl/sdl-events.h"
-#include "graphics/scaler/downscaler.h"
-#include "graphics/scaler/aspect.h"
-#include "common/mutex.h"
-#include "common/textconsole.h"
-
-static const OSystem::GraphicsMode s_supportedGraphicsModes[] = {
-	{"1x", "Standard", GFX_NORMAL},
-	{0, 0, 0}
-};
-
-DownscaleSdlGraphicsManager::DownscaleSdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window)
-	: SurfaceSdlGraphicsManager(sdlEventSource, window) {
-}
-
-const OSystem::GraphicsMode *DownscaleSdlGraphicsManager::getSupportedGraphicsModes() const {
-	return s_supportedGraphicsModes;
-}
-
-int DownscaleSdlGraphicsManager::getDefaultGraphicsMode() const {
-	return GFX_NORMAL;
-}
-
-int DownscaleSdlGraphicsManager::getGraphicsModeScale(int mode) const {
-	int scale;
-	switch (mode) {
-	case GFX_NORMAL:
-	case GFX_HALF:
-		scale = 1;
-		break;
-	default:
-		scale = -1;
-	}
-
-	return scale;
-}
-
-ScalerProc *DownscaleSdlGraphicsManager::getGraphicsScalerProc(int mode) const {
-	ScalerProc *newScalerProc = 0;
-	switch (_videoMode.mode) {
-	case GFX_NORMAL:
-		newScalerProc = Normal1x;
-		break;
-	case GFX_HALF:
-		newScalerProc = DownscaleAllByHalf;
-		break;
-	}
-
-	return newScalerProc;
-}
-
-void DownscaleSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFormat *format) {
-	assert(_transactionMode == kTransactionActive);
-
-	_gameScreenShakeXOffset = 0;
-	_gameScreenShakeYOffset = 0;
-
-#ifdef USE_RGB_COLOR
-	// Avoid redundant format changes
-	Graphics::PixelFormat newFormat;
-	if (!format)
-		newFormat = Graphics::PixelFormat::createFormatCLUT8();
-	else
-		newFormat = *format;
-
-	assert(newFormat.bytesPerPixel > 0);
-
-	if (newFormat != _videoMode.format) {
-		_videoMode.format = newFormat;
-		_transactionDetails.formatChanged = true;
-		_screenFormat = newFormat;
-	}
-#endif
-
-
-	// Avoid redundant res changes
-	if ((int)w == _videoMode.screenWidth && (int)h == _videoMode.screenHeight)
-		return;
-
-	_videoMode.screenWidth = w;
-	_videoMode.screenHeight = h;
-
-	if (w > 320 || h > 240) {
-		setGraphicsMode(GFX_HALF);
-		setGraphicsModeIntern();
-		_window->grabMouse(!getWindow()->mouseIsGrabbed());
-	}
-
-	_transactionDetails.sizeChanged = true;
-}
-
-void DownscaleSdlGraphicsManager::drawMouse() {
-	if (!_cursorVisible || !_mouseSurface || !_mouseCurState.w || !_mouseCurState.h) {
-		_mouseBackup.x = _mouseBackup.y = _mouseBackup.w = _mouseBackup.h = 0;
-		return;
-	}
-
-	SDL_Rect dst;
-	int scale;
-	int hotX, hotY;
-
-	const Common::Point virtualCursor = convertWindowToVirtual(_cursorX, _cursorY);
-
-	if (_videoMode.mode == GFX_HALF && !_overlayVisible) {
-		dst.x = virtualCursor.x / 2;
-		dst.y = virtualCursor.y / 2;
-	} else {
-		dst.x = virtualCursor.x;
-		dst.y = virtualCursor.y;
-	}
-
-	if (!_overlayVisible) {
-		scale = _videoMode.scaleFactor;
-		dst.w = _mouseCurState.vW;
-		dst.h = _mouseCurState.vH;
-		hotX = _mouseCurState.vHotX;
-		hotY = _mouseCurState.vHotY;
-	} else {
-		scale = 1;
-		dst.w = _mouseCurState.rW;
-		dst.h = _mouseCurState.rH;
-		hotX = _mouseCurState.rHotX;
-		hotY = _mouseCurState.rHotY;
-	}
-
-	// The mouse is undrawn using virtual coordinates, i.e. they may be
-	// scaled and aspect-ratio corrected.
-
-	_mouseBackup.x = dst.x - hotX;
-	_mouseBackup.y = dst.y - hotY;
-	_mouseBackup.w = dst.w;
-	_mouseBackup.h = dst.h;
-
-	// We draw the pre-scaled cursor image, so now we need to adjust for
-	// scaling, shake position and aspect ratio correction manually.
-
-	dst.x += _currentShakeXOffset;
-	dst.y += _currentShakeYOffset;
-
-	if (_videoMode.aspectRatioCorrection && !_overlayVisible)
-		dst.y = real2Aspect(dst.y);
-
-	dst.x = scale * dst.x - _mouseCurState.rHotX;
-	dst.y = scale * dst.y - _mouseCurState.rHotY;
-	dst.w = _mouseCurState.rW;
-	dst.h = _mouseCurState.rH;
-
-	// Note that SDL_BlitSurface() and addDirtyRect() will both perform any
-	// clipping necessary
-
-	if (SDL_BlitSurface(_mouseSurface, nullptr, _hwScreen, &dst) != 0)
-		error("SDL_BlitSurface failed: %s", SDL_GetError());
-
-	// The screen will be updated using real surface coordinates, i.e.
-	// they will not be scaled or aspect-ratio corrected.
-	addDirtyRect(dst.x, dst.y, dst.w, dst.h, true);
-}
-
-void DownscaleSdlGraphicsManager::undrawMouse() {
-	const int x = _mouseBackup.x;
-	const int y = _mouseBackup.y;
-
-	// When we switch bigger overlay off mouse jumps. Argh!
-	// This is intended to prevent undrawing offscreen mouse
-	if (!_overlayVisible && (x >= _videoMode.screenWidth || y >= _videoMode.screenHeight))
-		return;
-
-	if (_mouseBackup.w != 0 && _mouseBackup.h != 0) {
-		if (_videoMode.mode == GFX_HALF && !_overlayVisible) {
-			addDirtyRect(x * 2, y * 2, _mouseBackup.w * 2, _mouseBackup.h * 2);
-		} else {
-			addDirtyRect(x, y, _mouseBackup.w, _mouseBackup.h);
-		}
-	}
-}
-
-void DownscaleSdlGraphicsManager::internUpdateScreen() {
-	SDL_Surface *srcSurf, *origSurf;
-	int height, width;
-	ScalerProc *scalerProc;
-	int scale1;
-
-#if defined(DEBUG)
-	assert(_hwScreen != NULL);
-	assert(_hwScreen->map->sw_data != NULL);
-#endif
-
-	// If the shake position changed, fill the dirty area with blackness
-	if (_currentShakeXOffset != _gameScreenShakeXOffset ||
-		(_cursorNeedsRedraw && _mouseBackup.x <= _currentShakeXOffset)) {
-		SDL_Rect blackrect = {0, 0, (Uint16)(_gameScreenShakeXOffset * _videoMode.scaleFactor), (Uint16)(_videoMode.screenHeight * _videoMode.scaleFactor)};
-
-		if (_videoMode.aspectRatioCorrection && !_overlayVisible)
-			blackrect.h = real2Aspect(blackrect.h - 1) + 1;
-
-		SDL_FillRect(_hwScreen, &blackrect, 0);
-
-		_currentShakeXOffset = _gameScreenShakeXOffset;
-
-		_forceRedraw = true;
-	}
-	if (_currentShakeYOffset != _gameScreenShakeYOffset ||
-		(_cursorNeedsRedraw && _mouseBackup.y <= _currentShakeYOffset)) {
-		SDL_Rect blackrect = {0, 0, (Uint16)(_videoMode.screenWidth * _videoMode.scaleFactor), (Uint16)(_gameScreenShakeYOffset * _videoMode.scaleFactor)};
-
-		if (_videoMode.aspectRatioCorrection && !_overlayVisible)
-			blackrect.h = real2Aspect(blackrect.h - 1) + 1;
-
-		SDL_FillRect(_hwScreen, &blackrect, 0);
-
-		_currentShakeYOffset = _gameScreenShakeYOffset;
-
-		_forceRedraw = true;
-	}
-
-	// Check whether the palette was changed in the meantime and update the
-	// screen surface accordingly.
-	if (_screen && _paletteDirtyEnd != 0) {
-		SDL_SetColors(_screen, _currentPalette + _paletteDirtyStart,
-		              _paletteDirtyStart,
-		              _paletteDirtyEnd - _paletteDirtyStart);
-
-		_paletteDirtyEnd = 0;
-
-		_forceRedraw = true;
-	}
-
-	if (!_overlayVisible) {
-		origSurf = _screen;
-		srcSurf = _tmpscreen;
-		width = _videoMode.screenWidth;
-		height = _videoMode.screenHeight;
-		scalerProc = _scalerProc;
-		scale1 = _videoMode.scaleFactor;
-	} else {
-		origSurf = _overlayscreen;
-		srcSurf = _tmpscreen2;
-		width = _videoMode.overlayWidth;
-		height = _videoMode.overlayHeight;
-		scalerProc = Normal1x;
-		scale1 = 1;
-	}
-
-	// Add the area covered by the mouse cursor to the list of dirty rects if
-	// we have to redraw the mouse.
-	if (_cursorNeedsRedraw)
-		undrawMouse();
-
-#ifdef USE_OSD
-	updateOSD();
-#endif
-
-	// Force a full redraw if requested
-	if (_forceRedraw) {
-		_numDirtyRects = 1;
-		_dirtyRectList[0].x = 0;
-		_dirtyRectList[0].y = 0;
-		_dirtyRectList[0].w = width;
-		_dirtyRectList[0].h = height;
-
-#ifdef GPH_DEVICE
-		// HACK: Make sure the full hardware screen is wiped clean.
-		SDL_FillRect(_hwScreen, NULL, 0);
-#endif
-	}
-
-	// Only draw anything if necessary
-	if (_numDirtyRects > 0 || _cursorNeedsRedraw) {
-		SDL_Rect *r;
-		SDL_Rect dst;
-		uint32 srcPitch, dstPitch;
-		SDL_Rect *lastRect = _dirtyRectList + _numDirtyRects;
-
-		for (r = _dirtyRectList; r != lastRect; ++r) {
-			dst = *r;
-			dst.x++;    // Shift rect by one since 2xSai needs to access the data around
-			dst.y++;    // any pixel to scale it, and we want to avoid mem access crashes.
-
-			if (SDL_BlitSurface(origSurf, r, srcSurf, &dst) != 0)
-				error("SDL_BlitSurface failed: %s", SDL_GetError());
-		}
-
-		SDL_LockSurface(srcSurf);
-		SDL_LockSurface(_hwScreen);
-
-		srcPitch = srcSurf->pitch;
-		dstPitch = _hwScreen->pitch;
-
-		for (r = _dirtyRectList; r != lastRect; ++r) {
-			int dst_y = r->y + _currentShakeYOffset;
-			int dst_h = 0;
-			int dst_w = 0;
-			int orig_dst_y = 0;
-			int dst_x = r->x + _currentShakeXOffset;
-			int src_y;
-			int src_x;
-
-			if (dst_x < width && dst_y < height) {
-				dst_w = r->w;
-				if (dst_w > width - dst_x)
-					dst_w = width - dst_x;
-
-				dst_h = r->h;
-				if (dst_h > height - dst_y)
-					dst_h = height - dst_y;
-
-				orig_dst_y = dst_y;
-				src_x = dst_x;
-				src_y = dst_y;
-
-				if (_videoMode.aspectRatioCorrection && !_overlayVisible)
-					dst_y = real2Aspect(dst_y);
-
-				assert(scalerProc != NULL);
-
-				if (_videoMode.mode == GFX_HALF && scalerProc == DownscaleAllByHalf) {
-					if (dst_x % 2 == 1) {
-						dst_x--;
-						dst_w++;
-					}
-					if (dst_y % 2 == 1) {
-						dst_y--;
-						dst_h++;
-					}
-					src_x = dst_x;
-					src_y = dst_y;
-					dst_x = dst_x / 2;
-					dst_y = dst_y / 2;
-
-					scalerProc((byte *)srcSurf->pixels + (src_x * 2 + 2) + (src_y + 1) * srcPitch, srcPitch,
-					           (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
-				} else {
-					scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-					           (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
-				}
-			}
-
-			if (_videoMode.mode == GFX_HALF && scalerProc == DownscaleAllByHalf) {
-				r->w = dst_w / 2;
-				r->h = dst_h / 2;
-			} else {
-				r->w = dst_w;
-				r->h = dst_h;
-			}
-
-			r->x = dst_x;
-			r->y = dst_y;
-
-#ifdef USE_SCALERS
-			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible)
-				r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
-#endif
-		}
-		SDL_UnlockSurface(srcSurf);
-		SDL_UnlockSurface(_hwScreen);
-
-		// Readjust the dirty rect list in case we are doing a full update.
-		// This is necessary if shaking is active.
-		if (_forceRedraw) {
-			_dirtyRectList[0].x = 0;
-			_dirtyRectList[0].y = 0;
-			_dirtyRectList[0].w = (_videoMode.mode == GFX_HALF) ? _videoMode.hardwareWidth / 2 : _videoMode.hardwareWidth;
-			_dirtyRectList[0].h = (_videoMode.mode == GFX_HALF) ? _videoMode.hardwareHeight / 2 : _videoMode.hardwareHeight;
-		}
-
-		drawMouse();
-
-#ifdef USE_OSD
-		drawOSD();
-#endif
-
-		// Finally, blit all our changes to the screen
-		SDL_UpdateRects(_hwScreen, _numDirtyRects, _dirtyRectList);
-	}
-
-	_numDirtyRects = 0;
-	_forceRedraw = false;
-	_cursorNeedsRedraw = false;
-}
-
-void DownscaleSdlGraphicsManager::showOverlay() {
-	if (_videoMode.mode == GFX_HALF) {
-		_cursorX /= 2;
-		_cursorY /= 2;
-	}
-	SurfaceSdlGraphicsManager::showOverlay();
-}
-
-void DownscaleSdlGraphicsManager::hideOverlay() {
-	if (_videoMode.mode == GFX_HALF) {
-		_cursorX *= 2;
-		_cursorY *= 2;
-	}
-	SurfaceSdlGraphicsManager::hideOverlay();
-}
-
-void DownscaleSdlGraphicsManager::setupHardwareSize() {
-	debug("Game ScreenMode = %d*%d", _videoMode.screenWidth, _videoMode.screenHeight);
-
-	// Forcefully disable aspect ratio correction for games
-	// which starts with a native 240px height resolution.
-	// This fixes games with weird resolutions, like MM Nes (256x240)
-	if (_videoMode.screenHeight == 240) {
-		_videoMode.aspectRatioCorrection = false;
-	}
-
-	if (_videoMode.screenWidth > 320 || _videoMode.screenHeight > 240) {
-		_videoMode.aspectRatioCorrection = false;
-		setGraphicsMode(GFX_HALF);
-		debug("GraphicsMode set to HALF");
-	} else {
-		setGraphicsMode(GFX_NORMAL);
-		debug("GraphicsMode set to NORMAL");
-	}
-
-	if ((_videoMode.mode == GFX_HALF) && !_overlayVisible) {
-		_videoMode.overlayWidth = _videoMode.screenWidth / 2;
-		_videoMode.overlayHeight = _videoMode.screenHeight / 2;
-
-		_videoMode.hardwareWidth = _videoMode.screenWidth / 2;
-		_videoMode.hardwareHeight = _videoMode.screenHeight / 2;
-	} else {
-		SurfaceSdlGraphicsManager::setupHardwareSize();
-	}
-}
-
-void DownscaleSdlGraphicsManager::warpMouse(int x, int y) {
-	if (_cursorX != x || _cursorY != y) {
-		if (_videoMode.mode == GFX_HALF && !_overlayVisible) {
-			x /= 2;
-			y /= 2;
-		}
-	}
-	SurfaceSdlGraphicsManager::warpMouse(x, y);
-}
-
-void DownscaleSdlGraphicsManager::transformMouseCoordinates(Common::Point &point) {
-	if (!_overlayVisible) {
-		if (_videoMode.mode == GFX_HALF) {
-			point.x *= 2;
-			point.y *= 2;
-		}
-		point.x /= _videoMode.scaleFactor;
-		point.y /= _videoMode.scaleFactor;
-		if (_videoMode.aspectRatioCorrection)
-			point.y = aspect2Real(point.y);
-	}
-}
-
-#endif
diff --git a/backends/graphics/downscalesdl/downscalesdl-graphics.h b/backends/graphics/downscalesdl/downscalesdl-graphics.h
deleted file mode 100644
index 4e3f79dd39..0000000000
--- a/backends/graphics/downscalesdl/downscalesdl-graphics.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#ifndef BACKENDS_GRAPHICS_SDL_DOWNSCALE_H
-#define BACKENDS_GRAPHICS_SDL_DOWNSCALE_H
-
-#include "backends/graphics/surfacesdl/surfacesdl-graphics.h"
-
-enum {
-	GFX_HALF = 12
-};
-
-class DownscaleSdlGraphicsManager : public SurfaceSdlGraphicsManager {
-public:
-	DownscaleSdlGraphicsManager(SdlEventSource *boss, SdlWindow *window);
-
-	int getDefaultGraphicsMode() const override;
-
-	void initSize(uint w, uint h, const Graphics::PixelFormat *format = NULL) override;
-	const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
-	int getGraphicsModeScale(int mode) const override;
-	ScalerProc *getGraphicsScalerProc(int mode) const override;
-	void internUpdateScreen() override;
-	void showOverlay() override;
-	void hideOverlay() override;
-	void drawMouse() override;
-	void undrawMouse() override;
-	void warpMouse(int x, int y) override;
-
-	virtual void transformMouseCoordinates(Common::Point &point);
-
-protected:
-	void setupHardwareSize() override;
-};
-
-#endif /* BACKENDS_GRAPHICS_SDL_DOWNSCALE_H */
diff --git a/backends/graphics/gph/gph-graphics.cpp b/backends/graphics/gph/gph-graphics.cpp
index 97e44e423c..5686389469 100644
--- a/backends/graphics/gph/gph-graphics.cpp
+++ b/backends/graphics/gph/gph-graphics.cpp
@@ -27,7 +27,7 @@
 #include "backends/graphics/gph/gph-graphics.h"
 
 GPHGraphicsManager::GPHGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window)
-	: DownscaleSdlGraphicsManager(sdlEventSource, window) {
+	: SurfaceSdlGraphicsManager(sdlEventSource, window) {
 }
 
 void GPHGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFormat *format) {
@@ -47,21 +47,13 @@ void GPHGraphicsManager::setupHardwareSize() {
 
 	if (_videoMode.screenWidth > 320 || _videoMode.screenHeight > 240) {
 		_videoMode.aspectRatioCorrection = false;
-		setGraphicsMode(GFX_HALF);
-	} else {
-		setGraphicsMode(GFX_NORMAL);
 	}
 
-	if ((_videoMode.mode == GFX_HALF) && !_overlayVisible) {
-		_videoMode.overlayWidth = _videoMode.screenWidth / 2;
-		_videoMode.overlayHeight = _videoMode.screenHeight / 2;
-	} else {
-		_videoMode.overlayWidth = _videoMode.screenWidth * _videoMode.scaleFactor;
-		_videoMode.overlayHeight = _videoMode.screenHeight * _videoMode.scaleFactor;
+	_videoMode.overlayWidth = _videoMode.screenWidth * _videoMode.scaleFactor;
+	_videoMode.overlayHeight = _videoMode.screenHeight * _videoMode.scaleFactor;
 
-		if (_videoMode.aspectRatioCorrection)
-			_videoMode.overlayHeight = real2Aspect(_videoMode.overlayHeight);
-	}
+	if (_videoMode.aspectRatioCorrection)
+		_videoMode.overlayHeight = real2Aspect(_videoMode.overlayHeight);
 }
 
 bool GPHGraphicsManager::loadGFXMode() {
diff --git a/backends/graphics/gph/gph-graphics.h b/backends/graphics/gph/gph-graphics.h
index 30fa1b74de..2ea506744a 100644
--- a/backends/graphics/gph/gph-graphics.h
+++ b/backends/graphics/gph/gph-graphics.h
@@ -23,9 +23,9 @@
 #ifndef BACKENDS_GRAPHICS_GPH_H
 #define BACKENDS_GRAPHICS_GPH_H
 
-#include "backends/graphics/downscalesdl/downscalesdl-graphics.h"
+#include "backends/graphics/surfacesdl/surfacesdl-graphics.h"
 
-class GPHGraphicsManager : public DownscaleSdlGraphicsManager {
+class GPHGraphicsManager : public SurfaceSdlGraphicsManager {
 public:
 	GPHGraphicsManager(SdlEventSource *boss, SdlWindow *window);
 
diff --git a/backends/platform/dingux/dingux.cpp b/backends/platform/dingux/dingux.cpp
index 7448c40536..27488eea52 100644
--- a/backends/platform/dingux/dingux.cpp
+++ b/backends/platform/dingux/dingux.cpp
@@ -24,7 +24,6 @@
 
 #include "backends/platform/dingux/dingux.h"
 #include "backends/events/dinguxsdl/dinguxsdl-events.h"
-#include "backends/graphics/downscalesdl/downscalesdl-graphics.h"
 
 void OSystem_SDL_Dingux::initBackend() {
 	ConfMan.registerDefault("fullscreen", true);
@@ -33,13 +32,6 @@ void OSystem_SDL_Dingux::initBackend() {
 	if (_eventSource == 0)
 		_eventSource = new DINGUXSdlEventSource();
 
-#ifndef GCW0
-	// Create the graphics manager
-	if (_graphicsManager == 0) {
-		_graphicsManager = new DownscaleSdlGraphicsManager(_eventSource, _window);
-	}
-#endif
-
 	// Call parent implementation of this method
 	OSystem_POSIX::initBackend();
 }
diff --git a/configure b/configure
index e3f0e5a2a4..ffc3febd65 100755
--- a/configure
+++ b/configure
@@ -3984,7 +3984,7 @@ esac
 # Enable High resolution engines (>320x240) support only for backends which support it
 #
 case $_host in
-	ds | gcw0)
+	caanoo | ds | dingux | gcw0 | gp2x | gp2xwiz)
 		if test "$_highres" = yes ; then
 			_highres=yes
 		else


Commit: a3af191753e4c53cf33a730d6d10a47bcde37293
    https://github.com/scummvm/scummvm/commit/a3af191753e4c53cf33a730d6d10a47bcde37293
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS/BASE: Adds definitions for scaler plugins
This includes a class for plugin implementations to inherit from and
code necessary for plugin managers to use scalers

Changed paths:
  A graphics/scalerplugin.h
    base/plugins.cpp
    base/plugins.h


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 21d75e88f0..e7bd91e87a 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -39,6 +39,7 @@ int pluginTypeVersions[PLUGIN_TYPE_MAX] = {
 	PLUGIN_TYPE_ENGINE_VERSION,
 	PLUGIN_TYPE_MUSIC_VERSION,
 	PLUGIN_TYPE_DETECTION_VERSION,
+	PLUGIN_TYPE_SCALER_VERSION,
 };
 
 
@@ -942,3 +943,15 @@ DECLARE_SINGLETON(MusicManager);
 const PluginList &MusicManager::getPlugins() const {
 	return PluginManager::instance().getPlugins(PLUGIN_TYPE_MUSIC);
 }
+
+// Scaler plugins
+
+#include "graphics/scalerplugin.h"
+
+namespace Common {
+DECLARE_SINGLETON(ScalerManager);
+}
+
+const ScalerPlugin::List &ScalerManager::getPlugins() const {
+	return (const ScalerPlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_SCALER);
+}
diff --git a/base/plugins.h b/base/plugins.h
index 48780d8389..dced2b3bf0 100644
--- a/base/plugins.h
+++ b/base/plugins.h
@@ -43,7 +43,7 @@ enum PluginType {
 	PLUGIN_TYPE_ENGINE,
 	PLUGIN_TYPE_MUSIC,
 	PLUGIN_TYPE_DETECTION,
-	/* PLUGIN_TYPE_SCALER, */	// TODO: Add graphics scaler plugins
+	PLUGIN_TYPE_SCALER,
 
 	PLUGIN_TYPE_MAX
 };
@@ -54,6 +54,7 @@ enum PluginType {
 #define PLUGIN_TYPE_ENGINE_VERSION 2
 #define PLUGIN_TYPE_MUSIC_VERSION 1
 #define PLUGIN_TYPE_DETECTION_VERSION 1
+#define PLUGIN_TYPE_SCALER_VERSION 1
 
 extern int pluginTypeVersions[PLUGIN_TYPE_MAX];
 
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
new file mode 100644
index 0000000000..3db6b7f0a5
--- /dev/null
+++ b/graphics/scalerplugin.h
@@ -0,0 +1,64 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALERPLUGIN_H
+#define GRAPHICS_SCALERPLUGIN_H
+
+#include "base/plugins.h"
+
+class ScalerPluginObject : public PluginObject {
+public:
+	virtual ~ScalerPluginObject() {}
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height) = 0;
+
+	virtual int getFactor() = 0;
+
+	/**
+	 * Increase the factor of scaling.
+	 * @return the new factor
+	 */
+	virtual int increaseFactor() = 0;
+
+	/**
+	 * Decrease the factor of scaling.
+	 * @return the new factor
+	 */
+	virtual int decreaseFactor() = 0;
+};
+
+typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;
+
+/**
+ * Singleton class to manage scaler plugins
+ */
+class ScalerManager : public Common::Singleton<ScalerManager> {
+private:
+	friend class Common::Singleton<SingletonBaseType>;
+
+public:
+	const ScalerPlugin::List &getPlugins() const;
+};
+
+/** Convenience shortcut for accessing singleton */
+#define ScalerMan ScalerManager::instance()
+
+#endif


Commit: 7bcc1f0b13dc890298f5d1456f9b223684302120
    https://github.com/scummvm/scummvm/commit/7bcc1f0b13dc890298f5d1456f9b223684302120
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Adds a nearest neighbor plugin
It's not used yet, but it compiles and is managed by the plugin manager.

Changed paths:
  A graphics/scaler/normal.cpp
  A graphics/scaler/normal.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index e7bd91e87a..d73c79d543 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -161,6 +161,9 @@ public:
 		LINK_PLUGIN(COREMIDI)
 		#endif
 
+		// Scaler plugins
+		LINK_PLUGIN(NORMAL)
+
 		return pl;
 	}
 };
diff --git a/graphics/module.mk b/graphics/module.mk
index 0c367008dc..914a9e9a47 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -33,6 +33,7 @@ MODULE_OBJS := \
 	scaler.o \
 	scaler/thumbnail_intern.o \
 	screen.o \
+	scaler/normal.o \
 	sjis.o \
 	surface.o \
 	transform_struct.o \
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
new file mode 100644
index 0000000000..73589f2847
--- /dev/null
+++ b/graphics/scaler/normal.cpp
@@ -0,0 +1,60 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/normal.h"
+#include "graphics/scaler.h"
+
+void NearestNeighborPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	switch (_factor) {
+		case 1:
+			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 2:
+			Normal2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 3:
+			Normal3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+	}
+}
+
+int NearestNeighborPlugin::increaseFactor() {
+	if (_factor < 3)
+		++_factor;
+	return _factor;
+}
+
+int NearestNeighborPlugin::decreaseFactor() {
+	if (_factor > 1)
+		--_factor;
+	return _factor;
+}
+
+const char *NearestNeighborPlugin::getName() const {
+	return "NearestNeighbor";
+}
+
+int NearestNeighborPlugin::getFactor() {
+	return _factor;
+}
+
+REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NearestNeighborPlugin);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
new file mode 100644
index 0000000000..aac3fd787f
--- /dev/null
+++ b/graphics/scaler/normal.h
@@ -0,0 +1,41 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_NORMAL_H
+#define GRAPHICS_SCALER_NORMAL_H
+
+#include "graphics/scalerplugin.h"
+
+class NearestNeighborPlugin : public ScalerPluginObject {
+public:
+	NearestNeighborPlugin() { _factor = 1;}
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height);
+	virtual int increaseFactor();
+	virtual int decreaseFactor();
+	virtual int getFactor();
+	virtual const char *getName() const;
+private:
+	int _factor;
+};
+
+
+#endif


Commit: 29466870e82df698d0696dd8c070537eb74757c7
    https://github.com/scummvm/scummvm/commit/29466870e82df698d0696dd8c070537eb74757c7
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
BACKENDS: Changes scaler code to use plugin.
Much of the existing scaler code still exists and interferes.
This will have bugs

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 93d4c3a6a0..cad85c0217 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -185,6 +185,8 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 	_scalerProc = Normal2x;
+	// HACK: just pick first scaler plugin
+	_scalerPlugin = ScalerMan.getPlugins().front();
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
@@ -635,7 +637,8 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	_transactionDetails.needUpdatescreen = true;
 
 	_videoMode.mode = mode;
-	_videoMode.scaleFactor = newScaleFactor;
+	//_videoMode.scaleFactor = newScaleFactor;
+	_videoMode.scaleFactor = (*_scalerPlugin)->getFactor();
 
 	return true;
 }
@@ -1278,8 +1281,10 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					dst_y = real2Aspect(dst_y);
 
 				assert(scalerProc != NULL);
-				scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-					(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
+				//scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
+				//  (byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
+				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
+					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
 			}
 
 			r->x = dst_x;
@@ -1747,7 +1752,9 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 
 	SDL_LockSurface(_tmpscreen);
 	SDL_LockSurface(_overlayscreen);
-	_scalerProc((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
+	//_scalerProc((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
+	//(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight);
+	(*_scalerPlugin)->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight);
 
 #ifdef USE_SCALERS
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 926382882a..2a249f801e 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -27,6 +27,7 @@
 #include "backends/graphics/sdl/sdl-graphics.h"
 #include "graphics/pixelformat.h"
 #include "graphics/scaler.h"
+#include "graphics/scalerplugin.h"
 #include "common/events.h"
 #include "common/mutex.h"
 
@@ -329,6 +330,8 @@ protected:
 	// Indicates whether it is needed to free _hwSurface in destructor
 	bool _displayDisabled;
 
+	ScalerPlugin *_scalerPlugin;
+
 	bool _screenIsLocked;
 	Graphics::Surface _framebuffer;
 


Commit: 202f4efdee2ab62a630ec9083f540b319c35eacf
    https://github.com/scummvm/scummvm/commit/202f4efdee2ab62a630ec9083f540b319c35eacf
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Changes name of normal plugin to be consistant

Changed paths:
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h


diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 73589f2847..4556ecaa3e 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -22,7 +22,7 @@
 #include "graphics/scaler/normal.h"
 #include "graphics/scaler.h"
 
-void NearestNeighborPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	switch (_factor) {
 		case 1:
@@ -37,24 +37,24 @@ void NearestNeighborPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-int NearestNeighborPlugin::increaseFactor() {
+int NormalPlugin::increaseFactor() {
 	if (_factor < 3)
 		++_factor;
 	return _factor;
 }
 
-int NearestNeighborPlugin::decreaseFactor() {
+int NormalPlugin::decreaseFactor() {
 	if (_factor > 1)
 		--_factor;
 	return _factor;
 }
 
-const char *NearestNeighborPlugin::getName() const {
-	return "NearestNeighbor";
+const char *NormalPlugin::getName() const {
+	return "Normal";
 }
 
-int NearestNeighborPlugin::getFactor() {
+int NormalPlugin::getFactor() {
 	return _factor;
 }
 
-REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NearestNeighborPlugin);
+REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NormalPlugin);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index aac3fd787f..54d516f40e 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -24,9 +24,9 @@
 
 #include "graphics/scalerplugin.h"
 
-class NearestNeighborPlugin : public ScalerPluginObject {
+class NormalPlugin : public ScalerPluginObject {
 public:
-	NearestNeighborPlugin() { _factor = 1;}
+	NormalPlugin() { _factor = 1;}
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height);
 	virtual int increaseFactor();


Commit: 318130a905e709cb04cc315648ffa6fed9b76294
    https://github.com/scummvm/scummvm/commit/318130a905e709cb04cc315648ffa6fed9b76294
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
BACKENDS/GRAPHICS: Stops overlay from being scaled

Changed paths:
  A graphics/scalerplugin.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/module.mk
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index cad85c0217..f5c4854649 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1207,6 +1207,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		width = _videoMode.overlayWidth;
 		height = _videoMode.overlayHeight;
 		scalerProc = Normal1x;
+		// The overlay does not need to be scaled
+		(*_scalerPlugin)->disableScaling();
 
 		scale1 = 1;
 	}
@@ -1398,6 +1400,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		}
 	}
 
+	if (_overlayVisible)
+		(*_scalerPlugin)->enableScaling();
+
 	_numDirtyRects = 0;
 	_forceRedraw = false;
 	_cursorNeedsRedraw = false;
diff --git a/graphics/module.mk b/graphics/module.mk
index 914a9e9a47..5bbaeb40d5 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -31,6 +31,7 @@ MODULE_OBJS := \
 	primitives.o \
 	renderer.o \
 	scaler.o \
+	scalerplugin.o \
 	scaler/thumbnail_intern.o \
 	screen.o \
 	scaler/normal.o \
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 4556ecaa3e..a3ce667632 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -24,6 +24,10 @@
 
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	if (!_doScale) {
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		return;
+	}
 	switch (_factor) {
 		case 1:
 			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 54d516f40e..01c503b2dc 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -33,8 +33,6 @@ public:
 	virtual int decreaseFactor();
 	virtual int getFactor();
 	virtual const char *getName() const;
-private:
-	int _factor;
 };
 
 
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
new file mode 100644
index 0000000000..ac29abd55a
--- /dev/null
+++ b/graphics/scalerplugin.cpp
@@ -0,0 +1,34 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scalerplugin.h"
+
+ScalerPluginObject::ScalerPluginObject() {
+	_doScale = true;
+}
+
+void ScalerPluginObject::disableScaling() {
+	_doScale = false;
+}
+
+void ScalerPluginObject::enableScaling() {
+	_doScale = true;
+}
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 3db6b7f0a5..c8cda08f9a 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -26,6 +26,7 @@
 
 class ScalerPluginObject : public PluginObject {
 public:
+	ScalerPluginObject();
 	virtual ~ScalerPluginObject() {}
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height) = 0;
@@ -43,6 +44,17 @@ public:
 	 * @return the new factor
 	 */
 	virtual int decreaseFactor() = 0;
+
+	/**
+	 * Temporarily switch to a 1:1 scale ratio.
+	 * Useful for when the overlay is being displayed.
+	 */
+	virtual void disableScaling();
+
+	virtual void enableScaling();
+protected:
+	int _factor;
+	bool _doScale;
 };
 
 typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;


Commit: 60c430d6e510cc67ddd0933ca3b5badaa6be5d1a
    https://github.com/scummvm/scummvm/commit/60c430d6e510cc67ddd0933ca3b5badaa6be5d1a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: adds additinoal methods to scaler api

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f5c4854649..e95157e096 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1286,7 +1286,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				//scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
 				//  (byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
 				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
+					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, dst_w, dst_y);
 			}
 
 			r->x = dst_x;
@@ -1760,7 +1760,7 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	//_scalerProc((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	//(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight);
 	(*_scalerPlugin)->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
-	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight);
+	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
 #ifdef USE_SCALERS
 	if (_videoMode.aspectRatioCorrection)
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index a3ce667632..ed068aabbf 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -22,8 +22,11 @@
 #include "graphics/scaler/normal.h"
 #include "graphics/scaler.h"
 
+void NormalPlugin::initialize(Graphics::PixelFormat format) {}
+
+
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
 		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		return;
@@ -61,4 +64,13 @@ int NormalPlugin::getFactor() {
 	return _factor;
 }
 
+bool NormalPlugin::canDrawCursor() {
+	return true;
+}
+
+int NormalPlugin::extraPixels() {
+	return 0;
+}
+
+
 REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NormalPlugin);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 01c503b2dc..0d9bad73a6 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -27,11 +27,14 @@
 class NormalPlugin : public ScalerPluginObject {
 public:
 	NormalPlugin() { _factor = 1;}
+	virtual void initialize(Graphics::PixelFormat format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual int increaseFactor();
 	virtual int decreaseFactor();
 	virtual int getFactor();
+	virtual bool canDrawCursor();
+	virtual int extraPixels();
 	virtual const char *getName() const;
 };
 
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index c8cda08f9a..600aeb49ed 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -23,13 +23,19 @@
 #define GRAPHICS_SCALERPLUGIN_H
 
 #include "base/plugins.h"
+#include "graphics/pixelformat.h"
 
 class ScalerPluginObject : public PluginObject {
 public:
+
 	ScalerPluginObject();
+
 	virtual ~ScalerPluginObject() {}
+	
+	virtual void initialize(Graphics::PixelFormat format) = 0;
+
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height) = 0;
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) = 0;
 
 	virtual int getFactor() = 0;
 
@@ -45,16 +51,18 @@ public:
 	 */
 	virtual int decreaseFactor() = 0;
 
-	/**
-	 * Temporarily switch to a 1:1 scale ratio.
-	 * Useful for when the overlay is being displayed.
-	 */
+	virtual int extraPixels() = 0;
+
+	virtual bool canDrawCursor() = 0;
+
+	// temporary HACK
 	virtual void disableScaling();
 
 	virtual void enableScaling();
+
 protected:
 	int _factor;
-	bool _doScale;
+	bool _doScale; // < temporary
 };
 
 typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;


Commit: 739c75c869173d3c2b44511d06ea6bd781f94a4d
    https://github.com/scummvm/scummvm/commit/739c75c869173d3c2b44511d06ea6bd781f94a4d
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
BACKEND: fixes some errors from changing modes

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index e95157e096..87bb065995 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -621,8 +621,8 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	assert(_transactionMode == kTransactionActive);
 
-	if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
-		return true;
+	//if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
+	//	return true;
 
 	int newScaleFactor = getGraphicsModeScale(mode);
 
@@ -631,14 +631,15 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		return false;
 	}
 
+	newScaleFactor = (*_scalerPlugin)->getFactor();
+	_transactionDetails.normal1xScaler = (newScaleFactor == 1);
 	if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
 		_transactionDetails.needHotswap = true;
 
 	_transactionDetails.needUpdatescreen = true;
 
 	_videoMode.mode = mode;
-	//_videoMode.scaleFactor = newScaleFactor;
-	_videoMode.scaleFactor = (*_scalerPlugin)->getFactor();
+	_videoMode.scaleFactor = newScaleFactor;
 
 	return true;
 }
@@ -1584,8 +1585,8 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	if (!realCoordinates) {
 		x--;
 		y--;
-		w += 2;
-		h += 2;
+		w+= (*_scalerPlugin)->extraPixels()*2;
+		h+= (*_scalerPlugin)->extraPixels()*2;
 	}
 
 	// clip
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 600aeb49ed..b6ddc18192 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -34,6 +34,8 @@ public:
 	
 	virtual void initialize(Graphics::PixelFormat format) = 0;
 
+	virtual void deinitialize() {}
+
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) = 0;
 


Commit: 0d103d01c4d7c6aa315153f0ee7b3369b4be3401
    https://github.com/scummvm/scummvm/commit/0d103d01c4d7c6aa315153f0ee7b3369b4be3401
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: adds comments for scalers and clean up

Changed paths:
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scalerplugin.h


diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index ed068aabbf..42d99bafae 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -24,7 +24,6 @@
 
 void NormalPlugin::initialize(Graphics::PixelFormat format) {}
 
-
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
@@ -60,17 +59,5 @@ const char *NormalPlugin::getName() const {
 	return "Normal";
 }
 
-int NormalPlugin::getFactor() {
-	return _factor;
-}
-
-bool NormalPlugin::canDrawCursor() {
-	return true;
-}
-
-int NormalPlugin::extraPixels() {
-	return 0;
-}
-
 
 REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NormalPlugin);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 0d9bad73a6..0bab3686dc 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -32,9 +32,9 @@ public:
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual int increaseFactor();
 	virtual int decreaseFactor();
-	virtual int getFactor();
-	virtual bool canDrawCursor();
-	virtual int extraPixels();
+	virtual int getFactor() { return _factor; }
+	virtual bool canDrawCursor() { return true; }
+	virtual int extraPixels() { return 0; }
 	virtual const char *getName() const;
 };
 
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index b6ddc18192..45270dbaf4 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -32,29 +32,47 @@ public:
 
 	virtual ~ScalerPluginObject() {}
 	
+	/**
+	 * This function will be called before any scaler is used.
+	 * Precomputed data should be generated here.
+	 * @param format The pixel format to scale.
+	 */
 	virtual void initialize(Graphics::PixelFormat format) = 0;
 
+	/**
+	 * This is called when the plugin is not needed. It should clean
+	 * up memory from the initialize method.
+	 */
 	virtual void deinitialize() {}
 
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) = 0;
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                   uint32 dstPitch, int width, int height, int x, int y) = 0;
 
-	virtual int getFactor() = 0;
 
 	/**
 	 * Increase the factor of scaling.
-	 * @return the new factor
+	 * @return The new factor
 	 */
 	virtual int increaseFactor() = 0;
 
 	/**
 	 * Decrease the factor of scaling.
-	 * @return the new factor
+	 * @return The new factor
 	 */
 	virtual int decreaseFactor() = 0;
 
+	virtual int getFactor() = 0;
+
+	/**
+	 * Indicates how far outside the scaling region this scaler "looks"
+	 * @return The number of pixels in any direction
+	 */
 	virtual int extraPixels() = 0;
 
+	/**
+	 * Some scalers are not suitable for scaling the cursor.
+	 * Blurring scalers should return false.
+	 */
 	virtual bool canDrawCursor() = 0;
 
 	// temporary HACK


Commit: bdb12c8733486aadaf5af9d760f51f11fba9a338
    https://github.com/scummvm/scummvm/commit/bdb12c8733486aadaf5af9d760f51f11fba9a338
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: add support for additional scaler plugins
This enables scalers to be switched and uses api calls in a way
compatible with more scalers.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 87bb065995..c681dbdd55 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -187,6 +187,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_scalerProc = Normal2x;
 	// HACK: just pick first scaler plugin
 	_scalerPlugin = ScalerMan.getPlugins().front();
+	_scalerIndex = 0;
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
@@ -631,6 +632,12 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		return false;
 	}
 
+	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
+		(*_scalerPlugin)->deinitialize();
+		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
+		(*_scalerPlugin)->initialize(_videoMode.format);
+	}
+
 	newScaleFactor = (*_scalerPlugin)->getFactor();
 	_transactionDetails.normal1xScaler = (newScaleFactor == 1);
 	if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
@@ -1583,10 +1590,11 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	// Extend the dirty region by 1 pixel for scalers
 	// that "smear" the screen, e.g. 2xSAI
 	if (!realCoordinates) {
-		x--;
-		y--;
-		w+= (*_scalerPlugin)->extraPixels()*2;
-		h+= (*_scalerPlugin)->extraPixels()*2;
+		int adjust = (*_scalerPlugin)->extraPixels();
+		x -= adjust;
+		y -= adjust;
+		w += adjust * 2;
+		h += adjust * 2;
 	}
 
 	// clip
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 2a249f801e..62e998c159 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -331,6 +331,7 @@ protected:
 	bool _displayDisabled;
 
 	ScalerPlugin *_scalerPlugin;
+	int _scalerIndex;
 
 	bool _screenIsLocked;
 	Graphics::Surface _framebuffer;


Commit: 83a8abda70b99b5f3e00f55782633a5b9b527d3f
    https://github.com/scummvm/scummvm/commit/83a8abda70b99b5f3e00f55782633a5b9b527d3f
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add support for hq scaler plugin

Changed paths:
  A graphics/scaler/hq.cpp
  A graphics/scaler/hq.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index d73c79d543..58446ae24e 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -163,6 +163,7 @@ public:
 
 		// Scaler plugins
 		LINK_PLUGIN(NORMAL)
+		LINK_PLUGIN(HQ)
 
 		return pl;
 	}
diff --git a/graphics/module.mk b/graphics/module.mk
index 5bbaeb40d5..b0659c5cb9 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -101,6 +101,7 @@ endif
 
 ifdef USE_HQ_SCALERS
 MODULE_OBJS += \
+	scaler/hq.o \
 	scaler/hq2x.o \
 	scaler/hq3x.o
 
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
new file mode 100644
index 0000000000..abb78ebc73
--- /dev/null
+++ b/graphics/scaler/hq.cpp
@@ -0,0 +1,71 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/hq.h"
+#include "graphics/scaler.h"
+
+void InitLUT(Graphics::PixelFormat format);
+
+void HQPlugin::initialize(Graphics::PixelFormat format) {
+	//InitLUT(format);
+}
+
+void HQPlugin::deinitialize() {
+	//DestroyScalers();
+}
+
+void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (!_doScale) {
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		return;
+	}
+	switch (_factor) {
+		case 1:
+			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 2:
+			HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 3:
+			HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+	}
+}
+
+int HQPlugin::increaseFactor() {
+	if (_factor < 3)
+		++_factor;
+	return _factor;
+}
+
+int HQPlugin::decreaseFactor() {
+	if (_factor > 1)
+		--_factor;
+	return _factor;
+}
+
+const char *HQPlugin::getName() const {
+	return "HQ";
+}
+
+
+REGISTER_PLUGIN_STATIC(HQ, PLUGIN_TYPE_SCALER, HQPlugin);
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
new file mode 100644
index 0000000000..36db0cb681
--- /dev/null
+++ b/graphics/scaler/hq.h
@@ -0,0 +1,43 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_HQ_H
+#define GRAPHICS_SCALER_HQ_H
+
+#include "graphics/scalerplugin.h"
+
+class HQPlugin : public ScalerPluginObject {
+public:
+	HQPlugin() { _factor = 1;}
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual int increaseFactor();
+	virtual int decreaseFactor();
+	virtual int getFactor() { return _factor; }
+	virtual bool canDrawCursor() { return false; }
+	virtual int extraPixels() { return 1; }
+	virtual const char *getName() const;
+};
+
+
+#endif


Commit: ef2b057eefd3bab8255d3f372ac1fa584eabc89e
    https://github.com/scummvm/scummvm/commit/ef2b057eefd3bab8255d3f372ac1fa584eabc89e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: adds 2xsai as scaler plugin

Changed paths:
  A graphics/scaler/sai.cpp
  A graphics/scaler/sai.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 58446ae24e..022cb2b04a 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -164,6 +164,7 @@ public:
 		// Scaler plugins
 		LINK_PLUGIN(NORMAL)
 		LINK_PLUGIN(HQ)
+		LINK_PLUGIN(SAI)
 
 		return pl;
 	}
diff --git a/graphics/module.mk b/graphics/module.mk
index b0659c5cb9..ac10a33c64 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -86,6 +86,7 @@ endif
 ifdef USE_SCALERS
 MODULE_OBJS += \
 	scaler/2xsai.o \
+	scaler/sai.o \
 	scaler/aspect.o \
 	scaler/downscaler.o \
 	scaler/scale2x.o \
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
new file mode 100644
index 0000000000..e63fbd9395
--- /dev/null
+++ b/graphics/scaler/sai.cpp
@@ -0,0 +1,66 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/sai.h"
+#include "graphics/scaler.h"
+
+void SAIPlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void SAIPlugin::deinitialize() {
+	//DestroyScalers();
+}
+
+void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (!_doScale) {
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		return;
+	}
+	switch (_factor) {
+		case 1:
+			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 2:
+			_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+	}
+}
+
+int SAIPlugin::increaseFactor() {
+	if (_factor < 2)
+		++_factor;
+	return _factor;
+}
+
+int SAIPlugin::decreaseFactor() {
+	if (_factor > 1)
+		--_factor;
+	return _factor;
+}
+
+const char *SAIPlugin::getName() const {
+	return "SAI";
+}
+
+
+REGISTER_PLUGIN_STATIC(SAI, PLUGIN_TYPE_SCALER, SAIPlugin);
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
new file mode 100644
index 0000000000..5ee78bc280
--- /dev/null
+++ b/graphics/scaler/sai.h
@@ -0,0 +1,45 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_SAI_H
+#define GRAPHICS_SCALER_SAI_H
+
+#include "graphics/scalerplugin.h"
+
+class SAIPlugin : public ScalerPluginObject {
+public:
+	SAIPlugin() { _factor = 1;}
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual int increaseFactor();
+	virtual int decreaseFactor();
+	virtual int getFactor() { return _factor; }
+	virtual bool canDrawCursor() { return false; }
+	virtual int extraPixels() { return 1; }
+	virtual const char *getName() const;
+private:
+	Graphics::PixelFormat _format;
+};
+
+
+#endif


Commit: e69ec141aa9e9dad3790cea1a999d31fd9220b6c
    https://github.com/scummvm/scummvm/commit/e69ec141aa9e9dad3790cea1a999d31fd9220b6c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
BACKEND: fixes a bug related to AR correction

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index c681dbdd55..9e20db361a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1587,10 +1587,13 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 		height = _videoMode.overlayHeight;
 	}
 
-	// Extend the dirty region by 1 pixel for scalers
+	// Extend the dirty region for scalers
 	// that "smear" the screen, e.g. 2xSAI
 	if (!realCoordinates) {
 		int adjust = (*_scalerPlugin)->extraPixels();
+		// Aspect ratio correction requires this to be at least one
+		if (adjust == 0)
+			adjust = 1;
 		x -= adjust;
 		y -= adjust;
 		w += adjust * 2;


Commit: a0299cf5d2f8a4927dc80a6918b539d1b935489b
    https://github.com/scummvm/scummvm/commit/a0299cf5d2f8a4927dc80a6918b539d1b935489b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: fixes the value of extraPixels for 2xsai

Changed paths:
    graphics/scaler/sai.h


diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index 5ee78bc280..783225eb77 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -35,7 +35,7 @@ public:
 	virtual int decreaseFactor();
 	virtual int getFactor() { return _factor; }
 	virtual bool canDrawCursor() { return false; }
-	virtual int extraPixels() { return 1; }
+	virtual int extraPixels() { return 2; }
 	virtual const char *getName() const;
 private:
 	Graphics::PixelFormat _format;


Commit: 489d361d18fb9986cbbbfe1407dcf4f993fdfc9b
    https://github.com/scummvm/scummvm/commit/489d361d18fb9986cbbbfe1407dcf4f993fdfc9b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: fix formatting to conform to guidelines

Changed paths:
    graphics/scaler/hq.cpp
    graphics/scaler/normal.cpp
    graphics/scaler/sai.cpp


diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index abb78ebc73..c70d43732e 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -39,15 +39,15 @@ void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		return;
 	}
 	switch (_factor) {
-		case 1:
-			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
-		case 2:
-			HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
-		case 3:
-			HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
+	case 1:
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 2:
+		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 3:
+		HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
 	}
 }
 
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 42d99bafae..67a7b55220 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -31,15 +31,15 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		return;
 	}
 	switch (_factor) {
-		case 1:
-			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
-		case 2:
-			Normal2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
-		case 3:
-			Normal3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
+	case 1:
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 2:
+		Normal2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 3:
+		Normal3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
 	}
 }
 
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index e63fbd9395..fc61621b8c 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -37,12 +37,12 @@ void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		return;
 	}
 	switch (_factor) {
-		case 1:
-			Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
-		case 2:
-			_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
+	case 1:
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 2:
+		_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
 	}
 }
 


Commit: ca1754c047067bc66815d6257bf393b2a948c75c
    https://github.com/scummvm/scummvm/commit/ca1754c047067bc66815d6257bf393b2a948c75c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS/BASE: add maxExtraPixels() to ScalerMan

Changed paths:
    base/plugins.cpp
    graphics/scalerplugin.h


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 022cb2b04a..f8e9414052 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -960,3 +960,16 @@ DECLARE_SINGLETON(ScalerManager);
 const ScalerPlugin::List &ScalerManager::getPlugins() const {
 	return (const ScalerPlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_SCALER);
 }
+
+int ScalerManager::getMaxExtraPixels() const {
+	int maxPixels = 0;
+	ScalerPlugin::List plugins = getPlugins();
+	ScalerPlugin::List::iterator i = plugins.begin();
+	for (; i != plugins.end(); ++i) {
+		int n = (**i)->extraPixels();
+		if (n > maxPixels) {
+			maxPixels = n;
+		}
+	}
+	return maxPixels;
+}
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 45270dbaf4..db49d3d404 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -96,6 +96,13 @@ private:
 
 public:
 	const ScalerPlugin::List &getPlugins() const;
+
+	/**
+	 * Queries all scaler plugins for the maximum number of pixels they
+	 * can access out of bounds. Useful for adding extra rows and columns
+	 * to surfaces.
+	 */
+	int getMaxExtraPixels() const;
 };
 
 /** Convenience shortcut for accessing singleton */


Commit: 9402e2b43f62d15d8e4a8032a6abdc21b9533ce1
    https://github.com/scummvm/scummvm/commit/9402e2b43f62d15d8e4a8032a6abdc21b9533ce1
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: clarifies code

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 9e20db361a..0cf50e9996 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1590,10 +1590,8 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	// Extend the dirty region for scalers
 	// that "smear" the screen, e.g. 2xSAI
 	if (!realCoordinates) {
-		int adjust = (*_scalerPlugin)->extraPixels();
 		// Aspect ratio correction requires this to be at least one
-		if (adjust == 0)
-			adjust = 1;
+		int adjust = MAX((*_scalerPlugin)->extraPixels(), 1);
 		x -= adjust;
 		y -= adjust;
 		w += adjust * 2;


Commit: 8c9cb22734d6d777381fd99e2b9661236f65cb46
    https://github.com/scummvm/scummvm/commit/8c9cb22734d6d777381fd99e2b9661236f65cb46
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS/SDL/BASE: fix scaler declarations
mainly adding const and using uint instead of int

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    base/plugins.cpp
    graphics/scaler/hq.cpp
    graphics/scaler/hq.h
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 0cf50e9996..f0d437437e 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1591,7 +1591,7 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	// that "smear" the screen, e.g. 2xSAI
 	if (!realCoordinates) {
 		// Aspect ratio correction requires this to be at least one
-		int adjust = MAX((*_scalerPlugin)->extraPixels(), 1);
+		int adjust = MAX((*_scalerPlugin)->extraPixels(), (uint)1);
 		x -= adjust;
 		y -= adjust;
 		w += adjust * 2;
diff --git a/base/plugins.cpp b/base/plugins.cpp
index f8e9414052..839f9c30fe 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -961,12 +961,12 @@ const ScalerPlugin::List &ScalerManager::getPlugins() const {
 	return (const ScalerPlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_SCALER);
 }
 
-int ScalerManager::getMaxExtraPixels() const {
-	int maxPixels = 0;
+uint ScalerManager::getMaxExtraPixels() const {
+	uint maxPixels = 0;
 	ScalerPlugin::List plugins = getPlugins();
 	ScalerPlugin::List::iterator i = plugins.begin();
 	for (; i != plugins.end(); ++i) {
-		int n = (**i)->extraPixels();
+		uint n = (**i)->extraPixels();
 		if (n > maxPixels) {
 			maxPixels = n;
 		}
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index c70d43732e..d91fb6a25b 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -51,13 +51,13 @@ void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-int HQPlugin::increaseFactor() {
+uint HQPlugin::increaseFactor() {
 	if (_factor < 3)
 		++_factor;
 	return _factor;
 }
 
-int HQPlugin::decreaseFactor() {
+uint HQPlugin::decreaseFactor() {
 	if (_factor > 1)
 		--_factor;
 	return _factor;
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index 36db0cb681..e8a381fb91 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -31,11 +31,11 @@ public:
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual int increaseFactor();
-	virtual int decreaseFactor();
-	virtual int getFactor() { return _factor; }
-	virtual bool canDrawCursor() { return false; }
-	virtual int extraPixels() { return 1; }
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 };
 
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 67a7b55220..a152f58d47 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -43,13 +43,13 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-int NormalPlugin::increaseFactor() {
+uint NormalPlugin::increaseFactor() {
 	if (_factor < 3)
 		++_factor;
 	return _factor;
 }
 
-int NormalPlugin::decreaseFactor() {
+uint NormalPlugin::decreaseFactor() {
 	if (_factor > 1)
 		--_factor;
 	return _factor;
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 0bab3686dc..56136a35fb 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -30,11 +30,11 @@ public:
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual int increaseFactor();
-	virtual int decreaseFactor();
-	virtual int getFactor() { return _factor; }
-	virtual bool canDrawCursor() { return true; }
-	virtual int extraPixels() { return 0; }
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return true; }
+	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
 };
 
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index fc61621b8c..2da24f3fa2 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -46,13 +46,13 @@ void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-int SAIPlugin::increaseFactor() {
+uint SAIPlugin::increaseFactor() {
 	if (_factor < 2)
 		++_factor;
 	return _factor;
 }
 
-int SAIPlugin::decreaseFactor() {
+uint SAIPlugin::decreaseFactor() {
 	if (_factor > 1)
 		--_factor;
 	return _factor;
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index 783225eb77..afd17015d1 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -31,11 +31,11 @@ public:
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual int increaseFactor();
-	virtual int decreaseFactor();
-	virtual int getFactor() { return _factor; }
-	virtual bool canDrawCursor() { return false; }
-	virtual int extraPixels() { return 2; }
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
 private:
 	Graphics::PixelFormat _format;
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index db49d3d404..e70a588be8 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -53,27 +53,27 @@ public:
 	 * Increase the factor of scaling.
 	 * @return The new factor
 	 */
-	virtual int increaseFactor() = 0;
+	virtual uint increaseFactor() = 0;
 
 	/**
 	 * Decrease the factor of scaling.
 	 * @return The new factor
 	 */
-	virtual int decreaseFactor() = 0;
+	virtual uint decreaseFactor() = 0;
 
-	virtual int getFactor() = 0;
+	virtual uint getFactor() const = 0;
 
 	/**
 	 * Indicates how far outside the scaling region this scaler "looks"
 	 * @return The number of pixels in any direction
 	 */
-	virtual int extraPixels() = 0;
+	virtual uint extraPixels() const = 0;
 
 	/**
 	 * Some scalers are not suitable for scaling the cursor.
 	 * Blurring scalers should return false.
 	 */
-	virtual bool canDrawCursor() = 0;
+	virtual bool canDrawCursor() const = 0;
 
 	// temporary HACK
 	virtual void disableScaling();
@@ -81,7 +81,7 @@ public:
 	virtual void enableScaling();
 
 protected:
-	int _factor;
+	uint _factor;
 	bool _doScale; // < temporary
 };
 
@@ -102,7 +102,7 @@ public:
 	 * can access out of bounds. Useful for adding extra rows and columns
 	 * to surfaces.
 	 */
-	int getMaxExtraPixels() const;
+	uint getMaxExtraPixels() const;
 };
 
 /** Convenience shortcut for accessing singleton */


Commit: 6320ea772eb2cb31631e658a9bc8606474837055
    https://github.com/scummvm/scummvm/commit/6320ea772eb2cb31631e658a9bc8606474837055
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: small formatting change in HQPlugin

Changed paths:
    graphics/scaler/hq.h


diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index e8a381fb91..b962ba6002 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -26,7 +26,7 @@
 
 class HQPlugin : public ScalerPluginObject {
 public:
-	HQPlugin() { _factor = 1;}
+	HQPlugin() { _factor = 1; }
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,


Commit: b70a04214b4947002a5b5b32da48cc204d46e67a
    https://github.com/scummvm/scummvm/commit/b70a04214b4947002a5b5b32da48cc204d46e67a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS/BASE: adds Dotmatrix scaler plugin

Changed paths:
  A graphics/scaler/dotmatrix.cpp
  A graphics/scaler/dotmatrix.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 839f9c30fe..46289644ce 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -165,6 +165,7 @@ public:
 		LINK_PLUGIN(NORMAL)
 		LINK_PLUGIN(HQ)
 		LINK_PLUGIN(SAI)
+		LINK_PLUGIN(DOTMATRIX)
 
 		return pl;
 	}
diff --git a/graphics/module.mk b/graphics/module.mk
index ac10a33c64..9c7f8df11a 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -86,6 +86,7 @@ endif
 ifdef USE_SCALERS
 MODULE_OBJS += \
 	scaler/2xsai.o \
+	scaler/dotmatrix.o \
 	scaler/sai.o \
 	scaler/aspect.o \
 	scaler/downscaler.o \
diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
new file mode 100644
index 0000000000..9430a790d7
--- /dev/null
+++ b/graphics/scaler/dotmatrix.cpp
@@ -0,0 +1,62 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/dotmatrix.h"
+#include "graphics/scaler.h"
+
+void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
+	// TODO: initialize dotmatrix array
+}
+
+void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (!_doScale) {
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		return;
+	}
+	switch (_factor) {
+	case 1:
+		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	case 2:
+		DotMatrix(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
+	}
+}
+
+uint DotMatrixPlugin::increaseFactor() {
+	if (_factor < 2)
+		++_factor;
+	return _factor;
+}
+
+uint DotMatrixPlugin::decreaseFactor() {
+	if (_factor > 1)
+		--_factor;
+	return _factor;
+}
+
+const char *DotMatrixPlugin::getName() const {
+	return "DotMatrix";
+}
+
+
+REGISTER_PLUGIN_STATIC(DOTMATRIX, PLUGIN_TYPE_SCALER, DotMatrixPlugin);
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
new file mode 100644
index 0000000000..2785972967
--- /dev/null
+++ b/graphics/scaler/dotmatrix.h
@@ -0,0 +1,42 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_DOTMATRIX_H
+#define GRAPHICS_SCALER_DOTMATRIX_H
+
+#include "graphics/scalerplugin.h"
+
+class DotMatrixPlugin : public ScalerPluginObject {
+public:
+	DotMatrixPlugin() { _factor = 1;}
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return true; }
+	virtual uint extraPixels() const { return 0; }
+	virtual const char *getName() const;
+};
+
+
+#endif


Commit: 76d4a5425c25e4d868ded4cb86e8c51939f3a725
    https://github.com/scummvm/scummvm/commit/76d4a5425c25e4d868ded4cb86e8c51939f3a725
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: pass correct PixelFormat to scaler initialize

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f0d437437e..6dfd73ce5d 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -635,7 +635,12 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
 		(*_scalerPlugin)->deinitialize();
 		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
-		(*_scalerPlugin)->initialize(_videoMode.format);
+		Graphics::PixelFormat format(_tmpscreen->format->BytesPerPixel,
+	                                 8 - _tmpscreen->format->Rloss, 8 - _tmpscreen->format->Gloss,
+	                                 8 - _tmpscreen->format->Bloss, 8 - _tmpscreen->format->Aloss,
+	                                 _tmpscreen->format->Rshift, _tmpscreen->format->Gshift,
+	                                 _tmpscreen->format->Bshift, _tmpscreen->format->Ashift);
+		(*_scalerPlugin)->initialize(format);
 	}
 
 	newScaleFactor = (*_scalerPlugin)->getFactor();


Commit: f015c0f835f862d7dd038acdb0d522baa7ec0f30
    https://github.com/scummvm/scummvm/commit/f015c0f835f862d7dd038acdb0d522baa7ec0f30
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: use correct x and y for scale function

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 6dfd73ce5d..08e448bbbc 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1299,7 +1299,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				//scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
 				//  (byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
 				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, dst_w, dst_y);
+					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, dst_w, r->y);
 			}
 
 			r->x = dst_x;


Commit: a54ea85641c066ab8e0aa09b5eff7454de5aef32
    https://github.com/scummvm/scummvm/commit/a54ea85641c066ab8e0aa09b5eff7454de5aef32
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: fix dotmatrix inconsistencies
Now it uses the x and y coordinates to produce
the same pixels in the destination surface.

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 9430a790d7..7d3e7c5d41 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -23,7 +23,15 @@
 #include "graphics/scaler.h"
 
 void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
-	// TODO: initialize dotmatrix array
+	lookup[0] = lookup[10] = format.RGBToColor(0, 63, 0);
+	lookup[1] = lookup[11] = format.RGBToColor(0, 0, 63);
+	lookup[2] = lookup[8] = format.RGBToColor(63, 0, 0);
+	lookup[4] = lookup[6] =
+		lookup[12] = lookup[14] = format.RGBToColor(63, 63, 63);
+	lookup[5] = lookup[7] =
+		lookup[9] = lookup[13] =
+		lookup[15] = lookup[16] = format.RGBToColor(0, 0, 0);
+
 }
 
 void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
@@ -37,7 +45,7 @@ void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		break;
 	case 2:
-		DotMatrix(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
 		break;
 	}
 }
@@ -58,5 +66,35 @@ const char *DotMatrixPlugin::getName() const {
 	return "DotMatrix";
 }
 
+static inline uint16 DOT_16(const uint16 *dotmatrix, uint16 c, int j, int i) {
+	return c - ((c >> 2) & dotmatrix[((j & 3) << 2) + (i & 3)]);
+}
+
+void DotMatrixPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+					int width, int height, int x, int y) {
+
+	const uint16 *dotmatrix = lookup;
+
+	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
+	const uint16 *p = (const uint16 *)srcPtr;
+
+	const uint32 nextlineDst = dstPitch / sizeof(uint16);
+	uint16 *q = (uint16 *)dstPtr;
+
+	int ja = (y * 2) & 3;
+	int ia = (x * 2) & 3;
+
+	for (int j = 0, jj = 0; j < height; ++j, jj += 2) {
+		for (int i = 0, ii = 0; i < width; ++i, ii += 2) {
+			uint16 c = *(p + i);
+			*(q + ii) = DOT_16(dotmatrix, c, jj + ja, ii + ia);
+			*(q + ii + 1) = DOT_16(dotmatrix, c, jj + ja, ii + ia + 1);
+			*(q + ii + nextlineDst) = DOT_16(dotmatrix, c, jj + ja + 1, ii + ia);
+			*(q + ii + nextlineDst + 1) = DOT_16(dotmatrix, c, jj + ja + 1, ii + ia+ 1);
+		}
+		p += nextlineSrc;
+		q += nextlineDst << 1;
+	}
+}
 
 REGISTER_PLUGIN_STATIC(DOTMATRIX, PLUGIN_TYPE_SCALER, DotMatrixPlugin);
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 2785972967..48be275f0a 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -33,9 +33,13 @@ public:
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
-	virtual bool canDrawCursor() const { return true; }
+	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
+private:
+	uint16 lookup[16];
+	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+			uint32 dstPitch, int width, int height, int x, int y);
 };
 
 


Commit: d350e2c7a8559ef0eab8da82b35af60f34bbf2f4
    https://github.com/scummvm/scummvm/commit/d350e2c7a8559ef0eab8da82b35af60f34bbf2f4
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: use maxExtraPixels from ScalerMan

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 08e448bbbc..e761e1dfb7 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -188,6 +188,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	// HACK: just pick first scaler plugin
 	_scalerPlugin = ScalerMan.getPlugins().front();
 	_scalerIndex = 0;
+	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
@@ -996,7 +997,8 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 	//
 
 	// Need some extra bytes around when using 2xSaI
-	_tmpscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + 3, _videoMode.screenHeight + 3,
+	_tmpscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
+						_videoMode.screenHeight + _maxExtraPixels * 2,
 						16,
 						_hwScreen->format->Rmask,
 						_hwScreen->format->Gmask,
@@ -1018,7 +1020,8 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 
 	_overlayFormat = convertSDLPixelFormat(_overlayscreen->format);
 
-	_tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth + 3, _videoMode.overlayHeight + 3,
+	_tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth + _maxExtraPixels * 2,
+						_videoMode.overlayHeight + _maxExtraPixels * 2,
 						16,
 						_hwScreen->format->Rmask,
 						_hwScreen->format->Gmask,
@@ -1255,8 +1258,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 		for (r = _dirtyRectList; r != lastRect; ++r) {
 			dst = *r;
-			dst.x++;	// Shift rect by one since 2xSai needs to access the data around
-			dst.y++;	// any pixel to scale it, and we want to avoid mem access crashes.
+			dst.x += _maxExtraPixels;	// Shift rect since some scalers need to access the data around
+			dst.y += _maxExtraPixels;	// any pixel to scale it, and we want to avoid mem access crashes.
 
 			if (SDL_BlitSurface(origSurf, r, srcSurf, &dst) != 0)
 				error("SDL_BlitSurface failed: %s", SDL_GetError());
@@ -1296,10 +1299,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					dst_y = real2Aspect(dst_y);
 
 				assert(scalerProc != NULL);
-				//scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-				//  (byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w, dst_h);
-				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
-					(byte *)_hwscreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, dst_w, r->y);
+				//  (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h);
+				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+					(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 			}
 
 			r->x = dst_x;
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 62e998c159..760cd8b41a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -332,6 +332,7 @@ protected:
 
 	ScalerPlugin *_scalerPlugin;
 	int _scalerIndex;
+	uint _maxExtraPixels;
 
 	bool _screenIsLocked;
 	Graphics::Surface _framebuffer;


Commit: d56a7d610c27f3bedf66273628ac6d2453f3ae6b
    https://github.com/scummvm/scummvm/commit/d56a7d610c27f3bedf66273628ac6d2453f3ae6b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add static methods for 1x and 1.5x scale

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/hq.cpp
    graphics/scaler/sai.cpp
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 7d3e7c5d41..5e5230fdc7 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -31,18 +31,18 @@ void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
 	lookup[5] = lookup[7] =
 		lookup[9] = lookup[13] =
 		lookup[15] = lookup[16] = format.RGBToColor(0, 0, 0);
-
+	_format = format;
 }
 
 void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		return;
 	}
 	switch (_factor) {
 	case 1:
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		break;
 	case 2:
 		scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index d91fb6a25b..1067040fcb 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -26,6 +26,7 @@ void InitLUT(Graphics::PixelFormat format);
 
 void HQPlugin::initialize(Graphics::PixelFormat format) {
 	//InitLUT(format);
+	_format = format;
 }
 
 void HQPlugin::deinitialize() {
@@ -35,12 +36,12 @@ void HQPlugin::deinitialize() {
 void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		return;
 	}
 	switch (_factor) {
 	case 1:
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		break;
 	case 2:
 		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 2da24f3fa2..6c7aba5386 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -33,12 +33,12 @@ void SAIPlugin::deinitialize() {
 void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		return;
 	}
 	switch (_factor) {
 	case 1:
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
 		break;
 	case 2:
 		_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index ac29abd55a..f8e80f8eb7 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -20,6 +20,7 @@
  */
 
 #include "graphics/scalerplugin.h"
+#include "graphics/scaler.h"
 
 ScalerPluginObject::ScalerPluginObject() {
 	_doScale = true;
@@ -32,3 +33,15 @@ void ScalerPluginObject::disableScaling() {
 void ScalerPluginObject::enableScaling() {
 	_doScale = true;
 }
+
+void ScalerPluginObject::scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
+	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
+	Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
+
+void ScalerPluginObject::scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
+	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
+	Normal1o5x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index e70a588be8..ca178983ae 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -75,6 +75,18 @@ public:
 	 */
 	virtual bool canDrawCursor() const = 0;
 
+	/**
+	 * Usable for any scaler when 1x scaling is desired
+	 */
+	static void scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
+
+	/**
+	 * Useful for scaling the mouse
+	 */
+	static void scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
+
 	// temporary HACK
 	virtual void disableScaling();
 
@@ -82,6 +94,7 @@ public:
 
 protected:
 	uint _factor;
+	Graphics::PixelFormat _format;
 	bool _doScale; // < temporary
 };
 


Commit: c802ab3b60674ea0fcc40e28ee8daf79824a0ded
    https://github.com/scummvm/scummvm/commit/c802ab3b60674ea0fcc40e28ee8daf79824a0ded
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add setFactor to scalerplugins

Changed paths:
    graphics/scalerplugin.h


diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index ca178983ae..706ea7f884 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -63,6 +63,15 @@ public:
 
 	virtual uint getFactor() const = 0;
 
+	/**
+	 * Set the scaling factor.
+	 * Intended to be used with GUI to set a known valid factor.
+	 * Plugins should override if they require additional state changes.
+	 * @param factor A valid scaling factor for the plugin
+	 * @return The factor set
+	 */
+	virtual uint setFactor(uint factor) { return _factor = factor; }
+
 	/**
 	 * Indicates how far outside the scaling region this scaler "looks"
 	 * @return The number of pixels in any direction


Commit: aff49f3e2e36d8d6173ab91cd8040f8541783e07
    https://github.com/scummvm/scummvm/commit/aff49f3e2e36d8d6173ab91cd8040f8541783e07
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: mouse uses scaler plugins

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index e761e1dfb7..6c81ca7bb9 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -186,7 +186,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 	_scalerProc = Normal2x;
 	// HACK: just pick first scaler plugin
-	_scalerPlugin = ScalerMan.getPlugins().front();
+	_normalPlugin = _scalerPlugin = ScalerMan.getPlugins().front();
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 #else // for small screen platforms
@@ -1931,8 +1931,8 @@ void SurfaceSdlGraphicsManager::setMouseCursor(const void *buf, uint w, uint h,
 
 			// Allocate bigger surface because AdvMame2x adds black pixel at [0,0]
 			_mouseOrigSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
-							_mouseCurState.w + 2,
-							_mouseCurState.h + 2,
+							_mouseCurState.w + _maxExtraPixels * 2,
+							_mouseCurState.h + _maxExtraPixels * 2,
 							16,
 							_hwScreen->format->Rmask,
 							_hwScreen->format->Gmask,
@@ -2075,16 +2075,16 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	uint32 color;
 
 	// Make whole surface transparent
-	for (int i = 0; i < h + 2; i++) {
+	for (int i = 0; i < h + _maxExtraPixels * 2; i++) {
 		dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * i;
-		for (int j = 0; j < w + 2; j++) {
+		for (int j = 0; j < w + _maxExtraPixels * 2; j++) {
 			*(uint16 *)dstPtr = kMouseColorKey;
-			dstPtr += 2;
+			dstPtr += _cursorFormat.bytesPerPixel;
 		}
 	}
 
 	// Draw from [1,1] since AdvMame2x adds artefact at 0,0
-	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch + 2;
+	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel;
 
 	SDL_Color *palette;
 
@@ -2102,7 +2102,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 					_cursorFormat.colorToRGB(color, r, g, b);
 					*(uint16 *)dstPtr = SDL_MapRGB(_mouseOrigSurface->format, r, g, b);
 				}
-				dstPtr += 2;
+				dstPtr += bytesPerPixel;
 				srcPtr += _cursorFormat.bytesPerPixel;
 			} else {
 				color = *srcPtr;
@@ -2114,7 +2114,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 				srcPtr++;
 			}
 		}
-		dstPtr += _mouseOrigSurface->pitch - w * 2;
+		dstPtr += _mouseOrigSurface->pitch - w * _cursorFormat.bytesPerPixel;
 	}
 
 	if (sizeChanged || !_mouseSurface) {
@@ -2138,25 +2138,39 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 
 	SDL_LockSurface(_mouseSurface);
 
-	ScalerProc *scalerProc;
-
-	// Only apply scaling, when the user allows it.
-	if (!_cursorDontScale) {
-		// If possible, use the same scaler for the cursor as for the rest of
-		// the game. This only works well with the non-blurring scalers so we
-		// actually only use the 1x, 2x and AdvMame scalers.
-		if (_videoMode.mode == GFX_DOUBLESIZE || _videoMode.mode == GFX_TRIPLESIZE)
-			scalerProc = _scalerProc;
-		else
-			scalerProc = scalersMagn[_videoMode.scaleFactor - 1];
+	// If possible, use the same scaler for the cursor as for the rest of
+	// the game. This only works well with the non-blurring scalers so we
+	// otherwise use the Normal scaler
+	if (_cursorTargetScale == 1) {
+		if ((*_scalerPlugin)->canDrawCursor()) {
+		(*_scalerPlugin)->scale(
+			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+			_mouseCurState.w, _mouseCurState.h, 0, 0);
+		} else {
+			int tmpFactor = (*_normalPlugin)->getFactor();
+			(*_normalPlugin)->setFactor(_videoMode.scaleFactor);
+			(*_normalPlugin)->scale(
+				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+				_mouseCurState.w, _mouseCurState.h, 0, 0);
+			(*_normalPlugin)->setFactor(tmpFactor);
+		}
+	} else if (_cursorTargetScale == 3 || _videoMode.scaleFactor < 3) {
+			int tmpFactor = (*_normalPlugin)->getFactor();
+			(*_normalPlugin)->setFactor(1);
+			(*_normalPlugin)->scale(
+				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+				_mouseCurState.w, _mouseCurState.h, 0, 0);
+			(*_normalPlugin)->setFactor(tmpFactor);
 	} else {
-		scalerProc = Normal1x;
+		ScalerPluginObject::scale1o5x(
+			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel ,
+			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+			_mouseCurState.w, _mouseCurState.h, bytesPerPixel);
 	}
 
-	scalerProc((byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch + 2,
-		_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-		_mouseCurState.w, _mouseCurState.h);
-
 #ifdef USE_SCALERS
 	if (!_cursorDontScale && _videoMode.aspectRatioCorrection)
 		stretch200To240Nearest((uint8 *)_mouseSurface->pixels, _mouseSurface->pitch, rW, rH1, 0, 0, 0);
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 760cd8b41a..e853cec027 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -331,6 +331,7 @@ protected:
 	bool _displayDisabled;
 
 	ScalerPlugin *_scalerPlugin;
+	ScalerPlugin *_normalPlugin;
 	int _scalerIndex;
 	uint _maxExtraPixels;
 


Commit: 0fe3419349b23176ae37eea76b40e01e9c4ff106
    https://github.com/scummvm/scummvm/commit/0fe3419349b23176ae37eea76b40e01e9c4ff106
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: cache value of extraPixels() from scaler

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 6c81ca7bb9..9e2a3c4a4b 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -642,6 +642,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	                                 _tmpscreen->format->Rshift, _tmpscreen->format->Gshift,
 	                                 _tmpscreen->format->Bshift, _tmpscreen->format->Ashift);
 		(*_scalerPlugin)->initialize(format);
+		_extraPixels = (*_scalerPlugin)->extraPixels();
 	}
 
 	newScaleFactor = (*_scalerPlugin)->getFactor();
@@ -1598,7 +1599,7 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	// that "smear" the screen, e.g. 2xSAI
 	if (!realCoordinates) {
 		// Aspect ratio correction requires this to be at least one
-		int adjust = MAX((*_scalerPlugin)->extraPixels(), (uint)1);
+		int adjust = MAX(_extraPixels, (uint)1);
 		x -= adjust;
 		y -= adjust;
 		w += adjust * 2;
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index e853cec027..5658bbfe8a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -334,6 +334,7 @@ protected:
 	ScalerPlugin *_normalPlugin;
 	int _scalerIndex;
 	uint _maxExtraPixels;
+	uint _extraPixels;
 
 	bool _screenIsLocked;
 	Graphics::Surface _framebuffer;


Commit: 64f3e5191a1a8c13661de51338a5077b83df737d
    https://github.com/scummvm/scummvm/commit/64f3e5191a1a8c13661de51338a5077b83df737d
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: add convertSDLPixelFormat

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 9e2a3c4a4b..6154633d1a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -636,11 +636,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
 		(*_scalerPlugin)->deinitialize();
 		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
-		Graphics::PixelFormat format(_tmpscreen->format->BytesPerPixel,
-	                                 8 - _tmpscreen->format->Rloss, 8 - _tmpscreen->format->Gloss,
-	                                 8 - _tmpscreen->format->Bloss, 8 - _tmpscreen->format->Aloss,
-	                                 _tmpscreen->format->Rshift, _tmpscreen->format->Gshift,
-	                                 _tmpscreen->format->Bshift, _tmpscreen->format->Ashift);
+		Graphics::PixelFormat format = convertSDLPixelFormat(_hwscreen->format);
 		(*_scalerPlugin)->initialize(format);
 		_extraPixels = (*_scalerPlugin)->extraPixels();
 	}


Commit: 70426d5e74b2fe4e2976be651001f8731cfa2e97
    https://github.com/scummvm/scummvm/commit/70426d5e74b2fe4e2976be651001f8731cfa2e97
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add getFactors() for scaler plugins

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h
    graphics/scaler/hq.cpp
    graphics/scaler/hq.h
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 5e5230fdc7..6886949889 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -22,6 +22,12 @@
 #include "graphics/scaler/dotmatrix.h"
 #include "graphics/scaler.h"
 
+DotMatrixPlugin::DotMatrixPlugin() {
+	_factor = 1;
+	_factors.push_back(1);
+	_factors.push_back(2);
+}
+
 void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
 	lookup[0] = lookup[10] = format.RGBToColor(0, 63, 0);
 	lookup[1] = lookup[11] = format.RGBToColor(0, 0, 63);
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 48be275f0a..39fddc8d3e 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -26,7 +26,7 @@
 
 class DotMatrixPlugin : public ScalerPluginObject {
 public:
-	DotMatrixPlugin() { _factor = 1;}
+	DotMatrixPlugin();
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 1067040fcb..dd63b98d69 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -24,6 +24,13 @@
 
 void InitLUT(Graphics::PixelFormat format);
 
+HQPlugin::HQPlugin() {
+	_factor = 1;
+	_factors.push_back(1);
+	_factors.push_back(2);
+	_factors.push_back(3);
+}
+
 void HQPlugin::initialize(Graphics::PixelFormat format) {
 	//InitLUT(format);
 	_format = format;
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index b962ba6002..215c8620e1 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -26,7 +26,7 @@
 
 class HQPlugin : public ScalerPluginObject {
 public:
-	HQPlugin() { _factor = 1; }
+	HQPlugin();
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index a152f58d47..983e291933 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -22,7 +22,15 @@
 #include "graphics/scaler/normal.h"
 #include "graphics/scaler.h"
 
-void NormalPlugin::initialize(Graphics::PixelFormat format) {}
+NormalPlugin::NormalPlugin() {
+	_factor = 1;
+	_factors.push_back(1);
+	_factors.push_back(2);
+	_factors.push_back(3);
+}
+
+void NormalPlugin::initialize(Graphics::PixelFormat format) {
+}
 
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 56136a35fb..0edff62d19 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -26,7 +26,7 @@
 
 class NormalPlugin : public ScalerPluginObject {
 public:
-	NormalPlugin() { _factor = 1;}
+	NormalPlugin();
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 6c7aba5386..461900da30 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -22,6 +22,12 @@
 #include "graphics/scaler/sai.h"
 #include "graphics/scaler.h"
 
+SAIPlugin::SAIPlugin() {
+	_factor = 1;
+	_factors.push_back(1);
+	_factors.push_back(2);
+}
+
 void SAIPlugin::initialize(Graphics::PixelFormat format) {
 	_format = format;
 }
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index afd17015d1..61ec865d65 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -26,7 +26,7 @@
 
 class SAIPlugin : public ScalerPluginObject {
 public:
-	SAIPlugin() { _factor = 1;}
+	SAIPlugin();
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 706ea7f884..1668c28780 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -63,6 +63,8 @@ public:
 
 	virtual uint getFactor() const = 0;
 
+	virtual const Common::Array<uint>& getFactors() const { return _factors; }
+
 	/**
 	 * Set the scaling factor.
 	 * Intended to be used with GUI to set a known valid factor.
@@ -103,6 +105,7 @@ public:
 
 protected:
 	uint _factor;
+	Common::Array<uint> _factors;
 	Graphics::PixelFormat _format;
 	bool _doScale; // < temporary
 };


Commit: 45ad2a48822c9cd07d4ed681514da505275b5f60
    https://github.com/scummvm/scummvm/commit/45ad2a48822c9cd07d4ed681514da505275b5f60
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add PrettyName to scaler api for gui

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h
    graphics/scaler/hq.cpp
    graphics/scaler/hq.h
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 6886949889..271b100c9f 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -69,6 +69,10 @@ uint DotMatrixPlugin::decreaseFactor() {
 }
 
 const char *DotMatrixPlugin::getName() const {
+	return "dotmatrix";
+}
+
+const char *DotMatrixPlugin::getPrettyName() const {
 	return "DotMatrix";
 }
 
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 39fddc8d3e..c2d1e13ca0 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -36,6 +36,7 @@ public:
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
 private:
 	uint16 lookup[16];
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index dd63b98d69..9e244153f0 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -72,8 +72,11 @@ uint HQPlugin::decreaseFactor() {
 }
 
 const char *HQPlugin::getName() const {
-	return "HQ";
+	return "hq";
 }
 
+const char *HQPlugin::getPrettyName() const {
+	return "HQ";
+}
 
 REGISTER_PLUGIN_STATIC(HQ, PLUGIN_TYPE_SCALER, HQPlugin);
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index 215c8620e1..842f506a0d 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -37,6 +37,7 @@ public:
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
 };
 
 
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 983e291933..92798cbcf9 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -64,8 +64,11 @@ uint NormalPlugin::decreaseFactor() {
 }
 
 const char *NormalPlugin::getName() const {
-	return "Normal";
+	return "normal";
 }
 
+const char *NormalPlugin::getPrettyName() const {
+	return "Normal";
+}
 
 REGISTER_PLUGIN_STATIC(NORMAL, PLUGIN_TYPE_SCALER, NormalPlugin);
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 0edff62d19..eeb3e11c97 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -36,6 +36,7 @@ public:
 	virtual bool canDrawCursor() const { return true; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
 };
 
 
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 461900da30..8fb260582d 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -65,8 +65,11 @@ uint SAIPlugin::decreaseFactor() {
 }
 
 const char *SAIPlugin::getName() const {
-	return "SAI";
+	return "sai";
 }
 
+const char *SAIPlugin::getPrettyName() const {
+	return "SAI";
+}
 
 REGISTER_PLUGIN_STATIC(SAI, PLUGIN_TYPE_SCALER, SAIPlugin);
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index 61ec865d65..8bcf9d0ff9 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -37,6 +37,7 @@ public:
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
 private:
 	Graphics::PixelFormat _format;
 };
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 1668c28780..87cc18b441 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -1,6 +1,5 @@
 /* ScummVM - Graphic Adventure Engine
- *
- * ScummVM is the legal property of its developers, whose names
+ * * 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.
  *
@@ -86,6 +85,11 @@ public:
 	 */
 	virtual bool canDrawCursor() const = 0;
 
+	/**
+	 * This value will be displayed on the GUI.
+	 */
+	virtual const char * getPrettyName() const = 0;
+
 	/**
 	 * Usable for any scaler when 1x scaling is desired
 	 */


Commit: a434253f7f4d1d6a5460caab0c338285ab1fb2ec
    https://github.com/scummvm/scummvm/commit/a434253f7f4d1d6a5460caab0c338285ab1fb2ec
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Generate graphics modes from plugins
This currently does not work since the modes are queried before
plugins are loaded.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 6154633d1a..8e4a2eaed2 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -58,26 +58,14 @@
 #define SDL_FULLSCREEN  0x40000000
 #endif
 
-static const OSystem::GraphicsMode s_supportedGraphicsModes[] = {
-	{"1x", _s("Normal (no scaling)"), GFX_NORMAL},
-#ifdef USE_SCALERS
-	{"2x", "2x", GFX_DOUBLESIZE},
-	{"3x", "3x", GFX_TRIPLESIZE},
-	{"2xsai", "2xSAI", GFX_2XSAI},
-	{"super2xsai", "Super2xSAI", GFX_SUPER2XSAI},
-	{"supereagle", "SuperEagle", GFX_SUPEREAGLE},
-	{"advmame2x", "AdvMAME2x", GFX_ADVMAME2X},
-	{"advmame3x", "AdvMAME3x", GFX_ADVMAME3X},
-#ifdef USE_HQ_SCALERS
-	{"hq2x", "HQ2x", GFX_HQ2X},
-	{"hq3x", "HQ3x", GFX_HQ3X},
-#endif
-	{"tv2x", "TV2x", GFX_TV2X},
-	{"dotmatrix", "DotMatrix", GFX_DOTMATRIX},
-#endif
-	{0, 0, 0}
+struct GraphicsModeData {
+	const char * pluginName;
+	uint scaleFactor;
 };
 
+static Common::Array<OSystem::GraphicsMode> s_supportedGraphicsModes;
+static Common::Array<GraphicsModeData> s_supportedGraphicsModesData;
+
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 const OSystem::GraphicsMode s_supportedStretchModes[] = {
 	{"center", _s("Center"), STRETCH_CENTER},
@@ -277,8 +265,44 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 	}
 }
 
+void static initGraphicsModes () {
+	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
+	OSystem::GraphicsMode gm;
+	GraphicsModeData gmd;
+	for (uint i = 0; i < plugins.size(); ++i) {
+		const Common::Array<uint> &factors = (*plugins[i])->getFactors();
+		const char * name = (*plugins[i])->getName();
+		const char * prettyName = (*plugins[i])->getPrettyName();
+		gmd.pluginName = name;
+		for (uint j = 0; j < factors.size(); ++j) {
+			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
+			Common::String n2 = Common::String::format("%s%dx", prettyName, factors[j]);
+			char * s1 = new char[n1.size()];
+			char * s2 = new char[n2.size()];
+			strcpy(s1, n1.c_str());
+			strcpy(s2, n2.c_str());
+			gm.name = s1;
+			gm.description = s2;
+			gm.id = s_supportedGraphicsModes.size();
+			s_supportedGraphicsModes.push_back(gm);
+			gmd.scaleFactor = factors[j];
+			s_supportedGraphicsModesData.push_back(gmd);
+		}
+	}
+	gm.name = 0;
+	gm.description = 0;
+	gm.id = 0;
+	s_supportedGraphicsModes.push_back(gm);
+}
+
+const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() {
+	if (s_supportedGraphicsModes.size() < 2)
+		initGraphicsModes();
+	return &s_supportedGraphicsModes[0];
+}
+
 const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
-	return s_supportedGraphicsModes;
+	return supportedGraphicsModes();
 }
 
 int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
@@ -623,8 +647,8 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	assert(_transactionMode == kTransactionActive);
 
-	//if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
-	//	return true;
+	if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
+		return true;
 
 	int newScaleFactor = getGraphicsModeScale(mode);
 
@@ -633,6 +657,19 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		return false;
 	}
 
+	OSystem::GraphicsMode gm = s_supportedGraphicsModes[mode];
+
+	const char * name = s_supportedGraphicsModesData[mode].pluginName;
+	newScaleFactor = s_supportedGraphicsModesData[mode].scaleFactor;
+	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
+
+	while (strcmp(name, (*plugins[_scalerIndex])->getName()) != 0) {
+		_scalerIndex++;
+		if (_scalerIndex >= plugins.size()) {
+			_scalerIndex = 0;
+		}
+	}
+
 	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
 		(*_scalerPlugin)->deinitialize();
 		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
@@ -641,7 +678,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		_extraPixels = (*_scalerPlugin)->extraPixels();
 	}
 
-	newScaleFactor = (*_scalerPlugin)->getFactor();
+	(*_scalerPlugin)->setFactor(newScaleFactor);
 	_transactionDetails.normal1xScaler = (newScaleFactor == 1);
 	if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
 		_transactionDetails.needHotswap = true;


Commit: e6fac85afe7445015306cb41b0bc7ce068506886
    https://github.com/scummvm/scummvm/commit/e6fac85afe7445015306cb41b0bc7ce068506886
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: scaler plugins use graphics modes

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 8e4a2eaed2..23f0b7abd0 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -277,8 +277,8 @@ void static initGraphicsModes () {
 		for (uint j = 0; j < factors.size(); ++j) {
 			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
 			Common::String n2 = Common::String::format("%s%dx", prettyName, factors[j]);
-			char * s1 = new char[n1.size()];
-			char * s2 = new char[n2.size()];
+			char * s1 = new char[n1.size()+1];
+			char * s2 = new char[n2.size()+1];
 			strcpy(s1, n1.c_str());
 			strcpy(s2, n2.c_str());
 			gm.name = s1;
@@ -650,7 +650,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
 		return true;
 
-	int newScaleFactor = getGraphicsModeScale(mode);
+	int newScaleFactor;
 
 	if (newScaleFactor == -1) {
 		warning("unknown gfx mode %d", mode);
@@ -670,15 +670,6 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		}
 	}
 
-	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
-		(*_scalerPlugin)->deinitialize();
-		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
-		Graphics::PixelFormat format = convertSDLPixelFormat(_hwscreen->format);
-		(*_scalerPlugin)->initialize(format);
-		_extraPixels = (*_scalerPlugin)->extraPixels();
-	}
-
-	(*_scalerPlugin)->setFactor(newScaleFactor);
 	_transactionDetails.normal1xScaler = (newScaleFactor == 1);
 	if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
 		_transactionDetails.needHotswap = true;
@@ -691,79 +682,22 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	return true;
 }
 
-ScalerProc *SurfaceSdlGraphicsManager::getGraphicsScalerProc(int mode) const {
-	ScalerProc *newScalerProc = 0;
-	switch (_videoMode.mode) {
-	case GFX_NORMAL:
-		newScalerProc = Normal1x;
-		break;
-#ifdef USE_SCALERS
-	case GFX_DOUBLESIZE:
-		newScalerProc = Normal2x;
-		break;
-	case GFX_TRIPLESIZE:
-		newScalerProc = Normal3x;
-		break;
-
-	case GFX_2XSAI:
-		newScalerProc = _2xSaI;
-		break;
-	case GFX_SUPER2XSAI:
-		newScalerProc = Super2xSaI;
-		break;
-	case GFX_SUPEREAGLE:
-		newScalerProc = SuperEagle;
-		break;
-	case GFX_ADVMAME2X:
-		newScalerProc = AdvMame2x;
-		break;
-	case GFX_ADVMAME3X:
-		newScalerProc = AdvMame3x;
-		break;
-#ifdef USE_HQ_SCALERS
-	case GFX_HQ2X:
-		newScalerProc = HQ2x;
-		break;
-	case GFX_HQ3X:
-		newScalerProc = HQ3x;
-		break;
-#endif
-	case GFX_TV2X:
-		newScalerProc = TV2x;
-		break;
-	case GFX_DOTMATRIX:
-		newScalerProc = DotMatrix;
-		break;
-#endif // USE_SCALERS
-	default:
-		break;
-	}
-
-	return newScalerProc;
-}
-
 void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 	Common::StackLock lock(_graphicsMutex);
 
-	ScalerProc *newScalerProc = getGraphicsScalerProc(_videoMode.mode);
-
-	if (!newScalerProc) {
-		error("Unknown gfx mode %d", _videoMode.mode);
-	}
-
-	_scalerProc = newScalerProc;
+	if (!_screen || !_hwscreen)
+		return;
 
-	if (_videoMode.mode != GFX_NORMAL) {
-		for (int i = 0; i < ARRAYSIZE(s_gfxModeSwitchTable); i++) {
-			if (s_gfxModeSwitchTable[i][1] == _videoMode.mode || s_gfxModeSwitchTable[i][2] == _videoMode.mode) {
-				_scalerType = i;
-				break;
-			}
-		}
+	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
+		(*_scalerPlugin)->deinitialize();
+		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
+		Graphics::PixelFormat format;
+		convertSDLPixelFormat(_hwscreen->format, &format);
+		(*_scalerPlugin)->initialize(format);
+		_extraPixels = (*_scalerPlugin)->extraPixels();
 	}
 
-	if (!_screen || !_hwScreen)
-		return;
+	(*_scalerPlugin)->setFactor(_videoMode.scaleFactor);
 
 	// Blit everything to the screen
 	_forceRedraw = true;
@@ -2506,6 +2440,29 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
 	recalculateDisplayAreas();
 }
 
+/**
+ * Finds what the graphics mode should be using factor and plugin
+ *
+ * @param scalerIndex The index of the scaler plugin to match
+ * @param factor      The scale factor to match
+ * @return            The graphics mode
+ */
+int findGraphicsMode(int factor, uint scalerIndex) {
+	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
+	for (uint i = 0; i < s_supportedGraphicsModesData.size(); ++i) {
+		warning("%s, %d == %s, %d",
+				s_supportedGraphicsModesData[i].pluginName,
+				s_supportedGraphicsModesData[i].scaleFactor,
+				(*plugins[scalerIndex])->getName(),
+				factor);
+		if (strcmp(s_supportedGraphicsModesData[i].pluginName, (*plugins[scalerIndex])->getName()) == 0
+				&& s_supportedGraphicsModesData[i].scaleFactor == factor) {
+			return i;
+		}
+	}
+	return -1;
+}
+
 bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 	if (event.type != Common::EVENT_CUSTOM_BACKEND_ACTION_START) {
 		return SdlGraphicsManager::notifyEvent(event);
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 5658bbfe8a..78441a6182 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -182,7 +182,6 @@ protected:
 	virtual void handleResizeImpl(const int width, const int height, const int xdpi, const int ydpi) override;
 
 	virtual int getGraphicsModeScale(int mode) const override;
-	virtual ScalerProc *getGraphicsScalerProc(int mode) const;
 
 	virtual void setupHardwareSize();
 


Commit: c65e0146dd372b5d9fd50fe710c6e1b0c0f5a514
    https://github.com/scummvm/scummvm/commit/c65e0146dd372b5d9fd50fe710c6e1b0c0f5a514
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: remove unused table for cursor scaling

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 23f0b7abd0..7d84c50441 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -79,15 +79,6 @@ const OSystem::GraphicsMode s_supportedStretchModes[] = {
 
 DECLARE_TRANSLATION_ADDITIONAL_CONTEXT("Normal (no scaling)", "lowres")
 
-// Table of the cursor scalers [scaleFactor - 1]
-static ScalerProc *scalersMagn[3] = {
-#ifdef USE_SCALERS
-	Normal1x, AdvMame2x, AdvMame3x
-#else // remove dependencies on other scalers
-	Normal1x, Normal1x, Normal1x
-#endif
-};
-
 static const int s_gfxModeSwitchTable[][4] = {
 		{ GFX_NORMAL, GFX_DOUBLESIZE, GFX_TRIPLESIZE, -1 },
 		{ GFX_NORMAL, GFX_ADVMAME2X, GFX_ADVMAME3X, -1 },


Commit: a55f51d01a879714e837000ef2c077502395aefd
    https://github.com/scummvm/scummvm/commit/a55f51d01a879714e837000ef2c077502395aefd
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: add table for old gfxmode strings

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 7d84c50441..1652a119e3 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -90,6 +90,24 @@ static const int s_gfxModeSwitchTable[][4] = {
 		{ GFX_NORMAL, GFX_DOTMATRIX, -1, -1 }
 	};
 
+struct LegacyGraphicsMode {
+	const char * name;
+	const char * oldName;
+};
+
+
+// Table for using old names for scalers in the configuration
+// to keep compatibiblity with old config files.
+static LegacyGraphicsMode s_legacyGraphicsModes[] {
+	{ "supereagle2x", "supereagle" },
+	{ "dotmatrix2x", "dotmatrix" },
+	{ "sai2x", "2xsai" },
+	{ "normal1x", "1x" },
+	{ "normal2x", "2x" },
+	{ "normal3x", "3x" },
+	{ "supersai2x", "super2xsai" },
+};
+
 AspectRatio::AspectRatio(int w, int h) {
 	// TODO : Validation and so on...
 	// Currently, we just ensure the program don't instantiate non-supported aspect ratios
@@ -275,6 +293,13 @@ void static initGraphicsModes () {
 			gm.name = s1;
 			gm.description = s2;
 			gm.id = s_supportedGraphicsModes.size();
+			// Check if this is a legacy name
+			for (uint k = 0; k != ARRAYSIZE(s_legacyGraphicsModes); ++k) {
+				if (strcmp(s_legacyGraphicsModes[k].name, gm.name) == 0) {
+					gm.name = s_legacyGraphicsModes[k].oldName;
+					break;
+				}
+			}
 			s_supportedGraphicsModes.push_back(gm);
 			gmd.scaleFactor = factors[j];
 			s_supportedGraphicsModesData.push_back(gmd);


Commit: bc03b567bbe9cd4899ecd50645046f00dfc796db
    https://github.com/scummvm/scummvm/commit/bc03b567bbe9cd4899ecd50645046f00dfc796db
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: fix formatting

Changed paths:
    graphics/scalerplugin.h


diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 87cc18b441..f50f83607e 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -1,5 +1,6 @@
 /* ScummVM - Graphic Adventure Engine
- * * ScummVM is the legal property of its developers, whose names
+ *
+ * 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.
  *
@@ -88,7 +89,7 @@ public:
 	/**
 	 * This value will be displayed on the GUI.
 	 */
-	virtual const char * getPrettyName() const = 0;
+	virtual const char *getPrettyName() const = 0;
 
 	/**
 	 * Usable for any scaler when 1x scaling is desired


Commit: 9091be08ea97bb77b52f034a5a803763e071756e
    https://github.com/scummvm/scummvm/commit/9091be08ea97bb77b52f034a5a803763e071756e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: fix formatting

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 1652a119e3..a299811154 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -59,7 +59,7 @@
 #endif
 
 struct GraphicsModeData {
-	const char * pluginName;
+	const char *pluginName;
 	uint scaleFactor;
 };
 
@@ -91,8 +91,8 @@ static const int s_gfxModeSwitchTable[][4] = {
 	};
 
 struct LegacyGraphicsMode {
-	const char * name;
-	const char * oldName;
+	const char *name;
+	const char *oldName;
 };
 
 
@@ -280,14 +280,14 @@ void static initGraphicsModes () {
 	GraphicsModeData gmd;
 	for (uint i = 0; i < plugins.size(); ++i) {
 		const Common::Array<uint> &factors = (*plugins[i])->getFactors();
-		const char * name = (*plugins[i])->getName();
-		const char * prettyName = (*plugins[i])->getPrettyName();
+		const char *name = (*plugins[i])->getName();
+		const char *prettyName = (*plugins[i])->getPrettyName();
 		gmd.pluginName = name;
 		for (uint j = 0; j < factors.size(); ++j) {
 			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
 			Common::String n2 = Common::String::format("%s%dx", prettyName, factors[j]);
-			char * s1 = new char[n1.size()+1];
-			char * s2 = new char[n2.size()+1];
+			char *s1 = new char[n1.size()+1];
+			char *s2 = new char[n2.size()+1];
 			strcpy(s1, n1.c_str());
 			strcpy(s2, n2.c_str());
 			gm.name = s1;
@@ -675,7 +675,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	OSystem::GraphicsMode gm = s_supportedGraphicsModes[mode];
 
-	const char * name = s_supportedGraphicsModesData[mode].pluginName;
+	const char *name = s_supportedGraphicsModesData[mode].pluginName;
 	newScaleFactor = s_supportedGraphicsModesData[mode].scaleFactor;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
 


Commit: 277c9a3a6b342d894027cf29aeebefcd3ecbdd80
    https://github.com/scummvm/scummvm/commit/277c9a3a6b342d894027cf29aeebefcd3ecbdd80
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: add const to legacy names table

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index a299811154..71d01e8106 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -98,7 +98,7 @@ struct LegacyGraphicsMode {
 
 // Table for using old names for scalers in the configuration
 // to keep compatibiblity with old config files.
-static LegacyGraphicsMode s_legacyGraphicsModes[] {
+static const LegacyGraphicsMode s_legacyGraphicsModes[] {
 	{ "supereagle2x", "supereagle" },
 	{ "dotmatrix2x", "dotmatrix" },
 	{ "sai2x", "2xsai" },


Commit: fa3f6321a2acf5521b2d06036231074aeab0773e
    https://github.com/scummvm/scummvm/commit/fa3f6321a2acf5521b2d06036231074aeab0773e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: remove unused variable

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 71d01e8106..ce76ab2b48 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -673,8 +673,6 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		return false;
 	}
 
-	OSystem::GraphicsMode gm = s_supportedGraphicsModes[mode];
-
 	const char *name = s_supportedGraphicsModesData[mode].pluginName;
 	newScaleFactor = s_supportedGraphicsModesData[mode].scaleFactor;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();


Commit: 4bdd8860beba7e2c4bf5d24e45b144236c772952
    https://github.com/scummvm/scummvm/commit/4bdd8860beba7e2c4bf5d24e45b144236c772952
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: remove statically initialized object

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index ce76ab2b48..a8f4130d66 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -63,8 +63,8 @@ struct GraphicsModeData {
 	uint scaleFactor;
 };
 
-static Common::Array<OSystem::GraphicsMode> s_supportedGraphicsModes;
-static Common::Array<GraphicsModeData> s_supportedGraphicsModesData;
+static Common::Array<OSystem::GraphicsMode> *s_supportedGraphicsModes = NULL;
+static Common::Array<GraphicsModeData> *s_supportedGraphicsModesData = NULL;
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 const OSystem::GraphicsMode s_supportedStretchModes[] = {
@@ -275,6 +275,8 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 }
 
 void static initGraphicsModes () {
+	s_supportedGraphicsModes = new Common::Array<OSystem::GraphicsMode>;
+	s_supportedGraphicsModesData = new Common::Array<GraphicsModeData>;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
 	OSystem::GraphicsMode gm;
 	GraphicsModeData gmd;
@@ -292,7 +294,7 @@ void static initGraphicsModes () {
 			strcpy(s2, n2.c_str());
 			gm.name = s1;
 			gm.description = s2;
-			gm.id = s_supportedGraphicsModes.size();
+			gm.id = s_supportedGraphicsModes->size();
 			// Check if this is a legacy name
 			for (uint k = 0; k != ARRAYSIZE(s_legacyGraphicsModes); ++k) {
 				if (strcmp(s_legacyGraphicsModes[k].name, gm.name) == 0) {
@@ -300,21 +302,21 @@ void static initGraphicsModes () {
 					break;
 				}
 			}
-			s_supportedGraphicsModes.push_back(gm);
+			s_supportedGraphicsModes->push_back(gm);
 			gmd.scaleFactor = factors[j];
-			s_supportedGraphicsModesData.push_back(gmd);
+			s_supportedGraphicsModesData->push_back(gmd);
 		}
 	}
 	gm.name = 0;
 	gm.description = 0;
 	gm.id = 0;
-	s_supportedGraphicsModes.push_back(gm);
+	s_supportedGraphicsModes->push_back(gm);
 }
 
 const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() {
-	if (s_supportedGraphicsModes.size() < 2)
+	if (!s_supportedGraphicsModes)
 		initGraphicsModes();
-	return &s_supportedGraphicsModes[0];
+	return &(*s_supportedGraphicsModes)[0];
 }
 
 const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
@@ -668,13 +670,8 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	int newScaleFactor;
 
-	if (newScaleFactor == -1) {
-		warning("unknown gfx mode %d", mode);
-		return false;
-	}
-
-	const char *name = s_supportedGraphicsModesData[mode].pluginName;
-	newScaleFactor = s_supportedGraphicsModesData[mode].scaleFactor;
+	const char *name = (*s_supportedGraphicsModesData)[mode].pluginName;
+	newScaleFactor = (*s_supportedGraphicsModesData)[mode].scaleFactor;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
 
 	while (strcmp(name, (*plugins[_scalerIndex])->getName()) != 0) {
@@ -2463,14 +2460,14 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
  */
 int findGraphicsMode(int factor, uint scalerIndex) {
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
-	for (uint i = 0; i < s_supportedGraphicsModesData.size(); ++i) {
+	for (uint i = 0; i < s_supportedGraphicsModesData->size(); ++i) {
 		warning("%s, %d == %s, %d",
-				s_supportedGraphicsModesData[i].pluginName,
-				s_supportedGraphicsModesData[i].scaleFactor,
+				(*s_supportedGraphicsModesData)[i].pluginName,
+				(*s_supportedGraphicsModesData)[i].scaleFactor,
 				(*plugins[scalerIndex])->getName(),
 				factor);
-		if (strcmp(s_supportedGraphicsModesData[i].pluginName, (*plugins[scalerIndex])->getName()) == 0
-				&& s_supportedGraphicsModesData[i].scaleFactor == factor) {
+		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugins[scalerIndex])->getName()) == 0
+				&& (*s_supportedGraphicsModesData)[i].scaleFactor == factor) {
 			return i;
 		}
 	}


Commit: 2809ada4596a08f198dcc964350cf81abb3b0b47
    https://github.com/scummvm/scummvm/commit/2809ada4596a08f198dcc964350cf81abb3b0b47
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: legacy names for scalers corrected
Old names are recognized and they will be replaced in the new config

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/platform/sdl/sdl.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index a8f4130d66..9ca69980f0 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -90,24 +90,6 @@ static const int s_gfxModeSwitchTable[][4] = {
 		{ GFX_NORMAL, GFX_DOTMATRIX, -1, -1 }
 	};
 
-struct LegacyGraphicsMode {
-	const char *name;
-	const char *oldName;
-};
-
-
-// Table for using old names for scalers in the configuration
-// to keep compatibiblity with old config files.
-static const LegacyGraphicsMode s_legacyGraphicsModes[] {
-	{ "supereagle2x", "supereagle" },
-	{ "dotmatrix2x", "dotmatrix" },
-	{ "sai2x", "2xsai" },
-	{ "normal1x", "1x" },
-	{ "normal2x", "2x" },
-	{ "normal3x", "3x" },
-	{ "supersai2x", "super2xsai" },
-};
-
 AspectRatio::AspectRatio(int w, int h) {
 	// TODO : Validation and so on...
 	// Currently, we just ensure the program don't instantiate non-supported aspect ratios
@@ -295,13 +277,6 @@ void static initGraphicsModes () {
 			gm.name = s1;
 			gm.description = s2;
 			gm.id = s_supportedGraphicsModes->size();
-			// Check if this is a legacy name
-			for (uint k = 0; k != ARRAYSIZE(s_legacyGraphicsModes); ++k) {
-				if (strcmp(s_legacyGraphicsModes[k].name, gm.name) == 0) {
-					gm.name = s_legacyGraphicsModes[k].oldName;
-					break;
-				}
-			}
 			s_supportedGraphicsModes->push_back(gm);
 			gmd.scaleFactor = factors[j];
 			s_supportedGraphicsModesData->push_back(gmd);
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index ff3795941c..c710985c8d 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -73,6 +73,22 @@
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 #include <SDL_clipboard.h>
 #endif
+struct LegacyGraphicsMode {
+	const char *name;
+	const char *oldName;
+};
+
+// Table for using old names for scalers in the configuration
+// to keep compatibiblity with old config files.
+static const LegacyGraphicsMode s_legacyGraphicsModes[] {
+	{ "supereagle2x", "supereagle" },
+	{ "dotmatrix2x", "dotmatrix" },
+	{ "sai2x", "2xsai" },
+	{ "normal1x", "1x" },
+	{ "normal2x", "2x" },
+	{ "normal3x", "3x" },
+	{ "supersai2x", "super2xsai" },
+};
 
 OSystem_SDL::OSystem_SDL()
 	:
@@ -237,6 +253,17 @@ void OSystem_SDL::initBackend() {
 		_eventManager = new DefaultEventManager(_eventSourceWrapper ? _eventSourceWrapper : _eventSource);
 	}
 
+	// Search for legacy gfx_mode and replace it
+	if (ConfMan.hasKey("gfx_mode")) {
+		Common::String gfxMode(ConfMan.get("gfx_mode"));
+		for (uint i = 0; i < ARRAYSIZE(s_legacyGraphicsModes); ++i) {
+			if (gfxMode == s_legacyGraphicsModes[i].oldName) {
+				ConfMan.set("gfx_mode", s_legacyGraphicsModes[i].name);
+				break;
+			}
+		}
+	}
+
 	if (_graphicsManager == 0) {
 #ifdef USE_OPENGL
 		// Setup a list with both SDL and OpenGL graphics modes. We only do


Commit: 8b00ee4873cba791da638447709afc0c94393099
    https://github.com/scummvm/scummvm/commit/8b00ee4873cba791da638447709afc0c94393099
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: added 4x scaling to normal scaler plugin

Changed paths:
    graphics/scaler/normal.cpp


diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 92798cbcf9..d349a66ff3 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -27,11 +27,50 @@ NormalPlugin::NormalPlugin() {
 	_factors.push_back(1);
 	_factors.push_back(2);
 	_factors.push_back(3);
+	_factors.push_back(4);
 }
 
 void NormalPlugin::initialize(Graphics::PixelFormat format) {
 }
 
+/**
+ * Trivial nearest-neighbor 4x scaler.
+ */
+void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	uint8 *r;
+	const uint32 dstPitch2 = dstPitch * 2;
+	const uint32 dstPitch3 = dstPitch * 3;
+	const uint32 dstPitch4 = dstPitch * 4;
+
+	assert(IS_ALIGNED(dstPtr, 2));
+	while (height--) {
+		r = dstPtr;
+		for (int i = 0; i < width; ++i, r += 8) {
+			uint16 color = *(((const uint16 *)srcPtr) + i);
+
+			*(uint16 *)(r + 0) = color;
+			*(uint16 *)(r + 2) = color;
+			*(uint16 *)(r + 4) = color;
+			*(uint16 *)(r + 6) = color;
+			*(uint16 *)(r + 0 + dstPitch) = color;
+			*(uint16 *)(r + 2 + dstPitch) = color;
+			*(uint16 *)(r + 4 + dstPitch) = color;
+			*(uint16 *)(r + 6 + dstPitch) = color;
+			*(uint16 *)(r + 0 + dstPitch2) = color;
+			*(uint16 *)(r + 2 + dstPitch2) = color;
+			*(uint16 *)(r + 4 + dstPitch2) = color;
+			*(uint16 *)(r + 6 + dstPitch2) = color;
+			*(uint16 *)(r + 0 + dstPitch3) = color;
+			*(uint16 *)(r + 2 + dstPitch3) = color;
+			*(uint16 *)(r + 4 + dstPitch3) = color;
+			*(uint16 *)(r + 6 + dstPitch3) = color;
+		}
+		srcPtr += srcPitch;
+		dstPtr += dstPitch4;
+	}
+}
+
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
@@ -48,11 +87,14 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	case 3:
 		Normal3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		break;
+	case 4:
+		Normal4x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		break;
 	}
 }
 
 uint NormalPlugin::increaseFactor() {
-	if (_factor < 3)
+	if (_factor < 4)
 		++_factor;
 	return _factor;
 }


Commit: 3baf738de884a92b41a6bd4435d533def42f86f1
    https://github.com/scummvm/scummvm/commit/3baf738de884a92b41a6bd4435d533def42f86f1
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: change gfx_mode legacy names in game domains

Changed paths:
    backends/platform/sdl/sdl.cpp


diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index c710985c8d..d6dc564a6a 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -263,6 +263,19 @@ void OSystem_SDL::initBackend() {
 			}
 		}
 	}
+	// Look in all game domains as well
+	Common::ConfigManager::DomainMap &dm = ConfMan.getGameDomains();
+	for (Common::ConfigManager::DomainMap::iterator domain = dm.begin(); domain != dm.end(); ++domain) {
+		Common::ConfigManager::Domain::iterator gm = domain->_value.find("gfx_mode");
+		if (gm != domain->_value.end()) {
+			for (uint i = 0; i < ARRAYSIZE(s_legacyGraphicsModes); ++i) {
+				if (gm->_value == s_legacyGraphicsModes[i].oldName) {
+					gm->_value = s_legacyGraphicsModes[i].name;
+					break;
+				}
+			}
+		}
+	}
 
 	if (_graphicsManager == 0) {
 #ifdef USE_OPENGL


Commit: fe6fb4b5973a7640c7a010fd635de7fd8d2aec69
    https://github.com/scummvm/scummvm/commit/fe6fb4b5973a7640c7a010fd635de7fd8d2aec69
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: cache the list of scaler plugins

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 9ca69980f0..48ddb61ec0 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -145,7 +145,8 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #ifdef USE_SDL_DEBUG_FOCUSRECT
 	_enableFocusRectDebugCode(false), _enableFocusRect(false), _focusRect(),
 #endif
-	_transactionMode(kTransactionNone) {
+	_transactionMode(kTransactionNone),
+	_scalerPlugins(ScalerMan.getPlugins()) {
 
 	// allocate palette storage
 	_currentPalette = (SDL_Color *)calloc(sizeof(SDL_Color), 256);
@@ -165,7 +166,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 	_scalerProc = Normal2x;
 	// HACK: just pick first scaler plugin
-	_normalPlugin = _scalerPlugin = ScalerMan.getPlugins().front();
+	_normalPlugin = _scalerPlugin = _scalerPlugins.front();
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 #else // for small screen platforms
@@ -647,11 +648,10 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	const char *name = (*s_supportedGraphicsModesData)[mode].pluginName;
 	newScaleFactor = (*s_supportedGraphicsModesData)[mode].scaleFactor;
-	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
 
-	while (strcmp(name, (*plugins[_scalerIndex])->getName()) != 0) {
+	while (strcmp(name, (*_scalerPlugins[_scalerIndex])->getName()) != 0) {
 		_scalerIndex++;
-		if (_scalerIndex >= plugins.size()) {
+		if (_scalerIndex >= _scalerPlugins.size()) {
 			_scalerIndex = 0;
 		}
 	}
@@ -674,9 +674,9 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 	if (!_screen || !_hwscreen)
 		return;
 
-	if (ScalerMan.getPlugins()[_scalerIndex] != _scalerPlugin) {
+	if (_scalerPlugins[_scalerIndex] != _scalerPlugin) {
 		(*_scalerPlugin)->deinitialize();
-		_scalerPlugin = ScalerMan.getPlugins()[_scalerIndex];
+		_scalerPlugin = _scalerPlugins[_scalerIndex];
 		Graphics::PixelFormat format;
 		convertSDLPixelFormat(_hwscreen->format, &format);
 		(*_scalerPlugin)->initialize(format);
@@ -2429,19 +2429,18 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
 /**
  * Finds what the graphics mode should be using factor and plugin
  *
- * @param scalerIndex The index of the scaler plugin to match
+ * @param plugin      The scaler plugin to match
  * @param factor      The scale factor to match
  * @return            The graphics mode
  */
-int findGraphicsMode(int factor, uint scalerIndex) {
-	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
+int findGraphicsMode(int factor, ScalerPlugin *plugin) {
 	for (uint i = 0; i < s_supportedGraphicsModesData->size(); ++i) {
 		warning("%s, %d == %s, %d",
 				(*s_supportedGraphicsModesData)[i].pluginName,
 				(*s_supportedGraphicsModesData)[i].scaleFactor,
-				(*plugins[scalerIndex])->getName(),
+				(*plugin)->getName(),
 				factor);
-		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugins[scalerIndex])->getName()) == 0
+		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugin)->getName()) == 0
 				&& (*s_supportedGraphicsModesData)[i].scaleFactor == factor) {
 			return i;
 		}
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 78441a6182..77bea64ed7 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -329,6 +329,7 @@ protected:
 	// Indicates whether it is needed to free _hwSurface in destructor
 	bool _displayDisabled;
 
+	const ScalerPlugin::List &_scalerPlugins;
 	ScalerPlugin *_scalerPlugin;
 	ScalerPlugin *_normalPlugin;
 	int _scalerIndex;


Commit: 81ca88676ce3bccb41ea60cf3412f083d8e5f13a
    https://github.com/scummvm/scummvm/commit/81ca88676ce3bccb41ea60cf3412f083d8e5f13a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: remove some debugging output

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 48ddb61ec0..9a9e0d448b 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2435,11 +2435,6 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
  */
 int findGraphicsMode(int factor, ScalerPlugin *plugin) {
 	for (uint i = 0; i < s_supportedGraphicsModesData->size(); ++i) {
-		warning("%s, %d == %s, %d",
-				(*s_supportedGraphicsModesData)[i].pluginName,
-				(*s_supportedGraphicsModesData)[i].scaleFactor,
-				(*plugin)->getName(),
-				factor);
 		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugin)->getName()) == 0
 				&& (*s_supportedGraphicsModesData)[i].scaleFactor == factor) {
 			return i;


Commit: 9d2eca93837dc0d7715cf51380722b9ffadf7e91
    https://github.com/scummvm/scummvm/commit/9d2eca93837dc0d7715cf51380722b9ffadf7e91
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: remove 1x scaling for non-normal scalers
Scalers that fell back on the Normal1x scaler no longer support 1x
scaling

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/hq.cpp
    graphics/scaler/sai.cpp


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 271b100c9f..21a84995df 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -23,8 +23,7 @@
 #include "graphics/scaler.h"
 
 DotMatrixPlugin::DotMatrixPlugin() {
-	_factor = 1;
-	_factors.push_back(1);
+	_factor = 2;
 	_factors.push_back(2);
 }
 
@@ -44,27 +43,16 @@ void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
 		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		return;
-	}
-	switch (_factor) {
-	case 1:
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		break;
-	case 2:
+	} else {
 		scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
-		break;
 	}
 }
 
 uint DotMatrixPlugin::increaseFactor() {
-	if (_factor < 2)
-		++_factor;
 	return _factor;
 }
 
 uint DotMatrixPlugin::decreaseFactor() {
-	if (_factor > 1)
-		--_factor;
 	return _factor;
 }
 
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 9e244153f0..3110efb4cf 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -25,8 +25,7 @@
 void InitLUT(Graphics::PixelFormat format);
 
 HQPlugin::HQPlugin() {
-	_factor = 1;
-	_factors.push_back(1);
+	_factor = 2;
 	_factors.push_back(2);
 	_factors.push_back(3);
 }
@@ -47,9 +46,6 @@ void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		return;
 	}
 	switch (_factor) {
-	case 1:
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		break;
 	case 2:
 		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		break;
@@ -66,7 +62,7 @@ uint HQPlugin::increaseFactor() {
 }
 
 uint HQPlugin::decreaseFactor() {
-	if (_factor > 1)
+	if (_factor > 2)
 		--_factor;
 	return _factor;
 }
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 8fb260582d..fa55b72a7d 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -23,8 +23,7 @@
 #include "graphics/scaler.h"
 
 SAIPlugin::SAIPlugin() {
-	_factor = 1;
-	_factors.push_back(1);
+	_factor = 2;
 	_factors.push_back(2);
 }
 
@@ -40,27 +39,16 @@ void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_doScale) {
 		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		return;
-	}
-	switch (_factor) {
-	case 1:
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		break;
-	case 2:
+	} else {
 		_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		break;
 	}
 }
 
 uint SAIPlugin::increaseFactor() {
-	if (_factor < 2)
-		++_factor;
 	return _factor;
 }
 
 uint SAIPlugin::decreaseFactor() {
-	if (_factor > 1)
-		--_factor;
 	return _factor;
 }
 


Commit: 90e855d1abb0f69ff7147079f133952fb7bc95f7
    https://github.com/scummvm/scummvm/commit/90e855d1abb0f69ff7147079f133952fb7bc95f7
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Move HQ init code from InitScalers to the plugin

Changed paths:
    graphics/scaler.cpp
    graphics/scaler/hq.cpp


diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
index e084fd9473..5283c3aa25 100644
--- a/graphics/scaler.cpp
+++ b/graphics/scaler.cpp
@@ -28,98 +28,6 @@
 
 int gBitFormat = 565;
 
-#ifdef USE_HQ_SCALERS
-// RGB-to-YUV lookup table
-extern "C" {
-
-#ifdef USE_NASM
-// NOTE: if your compiler uses different mangled names, add another
-//       condition here
-
-#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
-#define RGBtoYUV _RGBtoYUV
-#define hqx_highbits _hqx_highbits
-#define hqx_lowbits _hqx_lowbits
-#define hqx_low2bits _hqx_low2bits
-#define hqx_low3bits _hqx_low3bits
-#define hqx_greenMask _hqx_greenMask
-#define hqx_redBlueMask _hqx_redBlueMask
-#define hqx_green_redBlue_Mask _hqx_green_redBlue_Mask
-#endif
-
-uint32 hqx_highbits = 0xF7DEF7DE;
-uint32 hqx_lowbits = 0x0821;
-uint32 hqx_low2bits = 0x0C63;
-uint32 hqx_low3bits = 0x1CE7;
-uint32 hqx_greenMask = 0;
-uint32 hqx_redBlueMask = 0;
-uint32 hqx_green_redBlue_Mask = 0;
-
-#endif
-
-/**
- * 16bit RGB to YUV conversion table. This table is setup by InitLUT().
- * Used by the hq scaler family.
- *
- * FIXME/TODO: The RGBtoYUV table sucks up 256 KB. This is bad.
- * In addition we never free it...
- *
- * Note: a memory lookup table is *not* necessarily faster than computing
- * these things on the fly, because of its size. The table together with
- * the code, plus the input/output GFX data, may not fit in the cache on some
- * systems, so main memory has to be accessed, which is about the worst thing
- * that can happen to code which tries to be fast...
- *
- * So we should think about ways to get this smaller / removed. Maybe we can
- * use the same technique employed by our MPEG code to reduce the size of the
- * lookup table at the cost of some additional computations?
- *
- * Of course, the above is largely a conjecture, and the actual speed
- * differences are likely to vary a lot between different architectures and
- * CPUs.
- */
-uint32 *RGBtoYUV = 0;
-}
-
-void InitLUT(Graphics::PixelFormat format) {
-	uint8 r, g, b;
-	int Y, u, v;
-
-	assert(format.bytesPerPixel == 2);
-
-	// Allocate the YUV/LUT buffers on the fly if needed.
-	if (RGBtoYUV == 0)
-		RGBtoYUV = (uint32 *)malloc(65536 * sizeof(uint32));
-
-	if (!RGBtoYUV)
-		error("[InitLUT] Cannot allocate memory for YUV/LUT buffers");
-
-	for (int color = 0; color < 65536; ++color) {
-		format.colorToRGB(color, r, g, b);
-		Y = (r + g + b) >> 2;
-		u = 128 + ((r - b) >> 2);
-		v = 128 + ((-r + 2 * g - b) >> 3);
-		RGBtoYUV[color] = (Y << 16) | (u << 8) | v;
-	}
-
-#ifdef USE_NASM
-	hqx_lowbits  = (1 << format.rShift) | (1 << format.gShift) | (1 << format.bShift),
-	hqx_low2bits = (3 << format.rShift) | (3 << format.gShift) | (3 << format.bShift),
-	hqx_low3bits = (7 << format.rShift) | (7 << format.gShift) | (7 << format.bShift),
-
-	hqx_highbits = format.RGBToColor(255, 255, 255) ^ hqx_lowbits;
-
-	// FIXME: The following code only does the right thing
-	// if the color order is RGB or BGR, i.e., green is in the middle.
-	hqx_greenMask   = format.RGBToColor(  0, 255,   0);
-	hqx_redBlueMask = format.RGBToColor(255,   0, 255);
-
-	hqx_green_redBlue_Mask = (hqx_greenMask << 16) | hqx_redBlueMask;
-#endif
-}
-#endif
-
-
 /** Lookup table for the DotMatrix scaler. */
 uint16 g_dotmatrix[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
@@ -140,10 +48,6 @@ void InitScalers(uint32 BitFormat) {
 		format = g_system->getOverlayFormat();
 	}
 
-#ifdef USE_HQ_SCALERS
-	InitLUT(format);
-#endif
-
 	// Build dotmatrix lookup table for the DotMatrix scaler.
 	g_dotmatrix[0] = g_dotmatrix[10] = format.RGBToColor( 0, 63,  0);
 	g_dotmatrix[1] = g_dotmatrix[11] = format.RGBToColor( 0,  0, 63);
@@ -153,10 +57,6 @@ void InitScalers(uint32 BitFormat) {
 }
 
 void DestroyScalers() {
-#ifdef USE_HQ_SCALERS
-	free(RGBtoYUV);
-	RGBtoYUV = 0;
-#endif
 }
 
 
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 3110efb4cf..2e2b7c888e 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -22,7 +22,93 @@
 #include "graphics/scaler/hq.h"
 #include "graphics/scaler.h"
 
-void InitLUT(Graphics::PixelFormat format);
+// RGB-to-YUV lookup table
+extern "C" {
+
+#ifdef USE_NASM
+// NOTE: if your compiler uses different mangled names, add another
+//       condition here
+
+#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
+#define RGBtoYUV _RGBtoYUV
+#define hqx_highbits _hqx_highbits
+#define hqx_lowbits _hqx_lowbits
+#define hqx_low2bits _hqx_low2bits
+#define hqx_low3bits _hqx_low3bits
+#define hqx_greenMask _hqx_greenMask
+#define hqx_redBlueMask _hqx_redBlueMask
+#define hqx_green_redBlue_Mask _hqx_green_redBlue_Mask
+#endif
+
+uint32 hqx_highbits = 0xF7DEF7DE;
+uint32 hqx_lowbits = 0x0821;
+uint32 hqx_low2bits = 0x0C63;
+uint32 hqx_low3bits = 0x1CE7;
+uint32 hqx_greenMask = 0;
+uint32 hqx_redBlueMask = 0;
+uint32 hqx_green_redBlue_Mask = 0;
+#endif
+
+/**
+ * 16bit RGB to YUV conversion table. This table is setup by InitLUT().
+ * Used by the hq scaler family.
+ *
+ * FIXME/TODO: The RGBtoYUV table sucks up 256 KB. This is bad.
+ * In addition we never free it...
+ *
+ * Note: a memory lookup table is *not* necessarily faster than computing
+ * these things on the fly, because of its size. The table together with
+ * the code, plus the input/output GFX data, may not fit in the cache on some
+ * systems, so main memory has to be accessed, which is about the worst thing
+ * that can happen to code which tries to be fast...
+ *
+ * So we should think about ways to get this smaller / removed. Maybe we can
+ * use the same technique employed by our MPEG code to reduce the size of the
+ * lookup table at the cost of some additional computations?
+ *
+ * Of course, the above is largely a conjecture, and the actual speed
+ * differences are likely to vary a lot between different architectures and
+ * CPUs.
+ */
+uint32 *RGBtoYUV = 0;
+}
+
+void InitLUT(Graphics::PixelFormat format) {
+	uint8 r, g, b;
+	int Y, u, v;
+
+	assert(format.bytesPerPixel == 2);
+
+	// Allocate the YUV/LUT buffers on the fly if needed.
+	if (RGBtoYUV == 0)
+		RGBtoYUV = (uint32 *)malloc(65536 * sizeof(uint32));
+
+	if (!RGBtoYUV)
+		error("[InitLUT] Cannot allocate memory for YUV/LUT buffers");
+
+	for (int color = 0; color < 65536; ++color) {
+		format.colorToRGB(color, r, g, b);
+		Y = (r + g + b) >> 2;
+		u = 128 + ((r - b) >> 2);
+		v = 128 + ((-r + 2 * g - b) >> 3);
+		RGBtoYUV[color] = (Y << 16) | (u << 8) | v;
+	}
+
+#ifdef USE_NASM
+	hqx_lowbits  = (1 << format.rShift) | (1 << format.gShift) | (1 << format.bShift),
+	hqx_low2bits = (3 << format.rShift) | (3 << format.gShift) | (3 << format.bShift),
+	hqx_low3bits = (7 << format.rShift) | (7 << format.gShift) | (7 << format.bShift),
+
+	hqx_highbits = format.RGBToColor(255,255,255) ^ hqx_lowbits;
+
+	// FIXME: The following code only does the right thing
+	// if the color order is RGB or BGR, i.e., green is in the middle.
+	hqx_greenMask = format.RGBToColor(0,255,0);
+	hqx_redBlueMask = format.RGBToColor(255,0,255);
+
+	hqx_green_redBlue_Mask = (hqx_greenMask << 16) | hqx_redBlueMask;
+#endif
+}
 
 HQPlugin::HQPlugin() {
 	_factor = 2;
@@ -31,12 +117,13 @@ HQPlugin::HQPlugin() {
 }
 
 void HQPlugin::initialize(Graphics::PixelFormat format) {
-	//InitLUT(format);
+	InitLUT(format);
 	_format = format;
 }
 
 void HQPlugin::deinitialize() {
-	//DestroyScalers();
+	free(RGBtoYUV);
+	RGBtoYUV = 0;
 }
 
 void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,


Commit: c8bcb3912f2659d85852c952946ae7edb0cf3bb5
    https://github.com/scummvm/scummvm/commit/c8bcb3912f2659d85852c952946ae7edb0cf3bb5
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL/GRAPHICS: remove disableScaling() hack from scaler plugins

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/hq.cpp
    graphics/scaler/normal.cpp
    graphics/scaler/sai.cpp
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 9a9e0d448b..91d5b2ef78 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1177,9 +1177,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		width = _videoMode.overlayWidth;
 		height = _videoMode.overlayHeight;
 		scalerProc = Normal1x;
-		// The overlay does not need to be scaled
-		(*_scalerPlugin)->disableScaling();
-
 		scale1 = 1;
 	}
 
@@ -1254,8 +1251,16 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 				assert(scalerProc != NULL);
 				//  (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h);
-				(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-					(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+				if (_overlayVisible) {
+					uint tmpFactor = (*_normalPlugin)->getFactor();
+					(*_normalPlugin)->setFactor(1);
+					(*_normalPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+					(*_normalPlugin)->setFactor(tmpFactor);
+				} else {
+					(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+				}
 			}
 
 			r->x = dst_x;
@@ -1369,9 +1374,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		}
 	}
 
-	if (_overlayVisible)
-		(*_scalerPlugin)->enableScaling();
-
 	_numDirtyRects = 0;
 	_forceRedraw = false;
 	_cursorNeedsRedraw = false;
diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 21a84995df..c5eede1280 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -41,11 +41,7 @@ void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
 
 void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (!_doScale) {
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-	} else {
-		scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
-	}
+	scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
 }
 
 uint DotMatrixPlugin::increaseFactor() {
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 2e2b7c888e..6c3bdb777c 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -128,10 +128,6 @@ void HQPlugin::deinitialize() {
 
 void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (!_doScale) {
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-		return;
-	}
 	switch (_factor) {
 	case 2:
 		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index d349a66ff3..5a552d9c16 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -73,10 +73,6 @@ void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (!_doScale) {
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		return;
-	}
 	switch (_factor) {
 	case 1:
 		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index fa55b72a7d..5442f383b9 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -37,11 +37,7 @@ void SAIPlugin::deinitialize() {
 
 void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (!_doScale) {
-		scale1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height, _format.bytesPerPixel);
-	} else {
-		_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	}
+	_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 }
 
 uint SAIPlugin::increaseFactor() {
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index f8e80f8eb7..d68a5f4357 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -22,18 +22,6 @@
 #include "graphics/scalerplugin.h"
 #include "graphics/scaler.h"
 
-ScalerPluginObject::ScalerPluginObject() {
-	_doScale = true;
-}
-
-void ScalerPluginObject::disableScaling() {
-	_doScale = false;
-}
-
-void ScalerPluginObject::enableScaling() {
-	_doScale = true;
-}
-
 void ScalerPluginObject::scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
 	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index f50f83607e..6d9c6048ce 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -28,8 +28,6 @@
 class ScalerPluginObject : public PluginObject {
 public:
 
-	ScalerPluginObject();
-
 	virtual ~ScalerPluginObject() {}
 	
 	/**
@@ -103,16 +101,10 @@ public:
 	static void scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
 
-	// temporary HACK
-	virtual void disableScaling();
-
-	virtual void enableScaling();
-
 protected:
 	uint _factor;
 	Common::Array<uint> _factors;
 	Graphics::PixelFormat _format;
-	bool _doScale; // < temporary
 };
 
 typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;


Commit: dcfbf9da7f66c620ce3e0e9cfaca2e0f11be4e34
    https://github.com/scummvm/scummvm/commit/dcfbf9da7f66c620ce3e0e9cfaca2e0f11be4e34
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Unify hq scalers with plugin
The scalers read pixelformat from the plugin now.
However there are still only 16bpp implementations.

Changed paths:
    graphics/module.mk
    graphics/scaler/hq.cpp


diff --git a/graphics/module.mk b/graphics/module.mk
index 9c7f8df11a..a6d9726a89 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -104,8 +104,6 @@ endif
 ifdef USE_HQ_SCALERS
 MODULE_OBJS += \
 	scaler/hq.o \
-	scaler/hq2x.o \
-	scaler/hq3x.o
 
 ifdef USE_NASM
 MODULE_OBJS += \
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 6c3bdb777c..44ed3f4d40 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -21,6 +21,7 @@
 
 #include "graphics/scaler/hq.h"
 #include "graphics/scaler.h"
+#include "graphics/scaler/intern.h"
 
 // RGB-to-YUV lookup table
 extern "C" {
@@ -110,6 +111,4857 @@ void InitLUT(Graphics::PixelFormat format) {
 #endif
 }
 
+#ifdef USE_NASM
+// Assembly version of HQ2x
+
+extern "C" {
+
+#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
+#define hq2x_16 _hq2x_16
+#endif
+
+void hq2x_16(const byte *, byte *, uint32, uint32, uint32, uint32);
+
+}
+
+void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	hq2x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
+}
+
+#else
+
+#define PIXEL00_0	*(q) = w5;
+#define PIXEL00_10	*(q) = interpolate16_3_1<ColorMask >(w5, w1);
+#define PIXEL00_11	*(q) = interpolate16_3_1<ColorMask >(w5, w4);
+#define PIXEL00_12	*(q) = interpolate16_3_1<ColorMask >(w5, w2);
+#define PIXEL00_20	*(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
+#define PIXEL00_21	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w2);
+#define PIXEL00_22	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w4);
+#define PIXEL00_60	*(q) = interpolate16_5_2_1<ColorMask >(w5, w2, w4);
+#define PIXEL00_61	*(q) = interpolate16_5_2_1<ColorMask >(w5, w4, w2);
+#define PIXEL00_70	*(q) = interpolate16_6_1_1<ColorMask >(w5, w4, w2);
+#define PIXEL00_90	*(q) = interpolate16_2_3_3<ColorMask >(w5, w4, w2);
+#define PIXEL00_100	*(q) = interpolate16_14_1_1<ColorMask >(w5, w4, w2);
+
+#define PIXEL01_0	*(q+1) = w5;
+#define PIXEL01_10	*(q+1) = interpolate16_3_1<ColorMask >(w5, w3);
+#define PIXEL01_11	*(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
+#define PIXEL01_12	*(q+1) = interpolate16_3_1<ColorMask >(w5, w6);
+#define PIXEL01_20	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
+#define PIXEL01_21	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w6);
+#define PIXEL01_22	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w2);
+#define PIXEL01_60	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w6, w2);
+#define PIXEL01_61	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w2, w6);
+#define PIXEL01_70	*(q+1) = interpolate16_6_1_1<ColorMask >(w5, w2, w6);
+#define PIXEL01_90	*(q+1) = interpolate16_2_3_3<ColorMask >(w5, w2, w6);
+#define PIXEL01_100	*(q+1) = interpolate16_14_1_1<ColorMask >(w5, w2, w6);
+
+#define PIXEL10_0	*(q+nextlineDst) = w5;
+#define PIXEL10_10	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w7);
+#define PIXEL10_11	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
+#define PIXEL10_12	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
+#define PIXEL10_20	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
+#define PIXEL10_21	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w4);
+#define PIXEL10_22	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w8);
+#define PIXEL10_60	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w4, w8);
+#define PIXEL10_61	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w4);
+#define PIXEL10_70	*(q+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w8, w4);
+#define PIXEL10_90	*(q+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w8, w4);
+#define PIXEL10_100	*(q+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w8, w4);
+
+#define PIXEL11_0	*(q+1+nextlineDst) = w5;
+#define PIXEL11_10	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w9);
+#define PIXEL11_11	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
+#define PIXEL11_12	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
+#define PIXEL11_20	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
+#define PIXEL11_21	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w8);
+#define PIXEL11_22	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w6);
+#define PIXEL11_60	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w6);
+#define PIXEL11_61	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w6, w8);
+#define PIXEL11_70	*(q+1+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w6, w8);
+#define PIXEL11_90	*(q+1+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w6, w8);
+#define PIXEL11_100	*(q+1+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w6, w8);
+
+extern "C" uint32   *RGBtoYUV;
+#define YUV(x)	RGBtoYUV[w ## x]
+
+/*
+ * The HQ2x high quality 2x graphics filter.
+ * Original author Maxim Stepin (see http://www.hiend3d.com/hq2x.html).
+ * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
+ */
+template<typename ColorMask>
+static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	register int w1, w2, w3, w4, w5, w6, w7, w8, w9;
+
+	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
+	const uint16 *p = (const uint16 *)srcPtr;
+
+	const uint32 nextlineDst = dstPitch / sizeof(uint16);
+	uint16 *q = (uint16 *)dstPtr;
+
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w1 | w2 | w3 |
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w4 | w5 | w6 |
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w7 | w8 | w9 |
+	//	 +----+----+----+
+
+	while (height--) {
+		w1 = *(p - 1 - nextlineSrc);
+		w4 = *(p - 1);
+		w7 = *(p - 1 + nextlineSrc);
+
+		w2 = *(p - nextlineSrc);
+		w5 = *(p);
+		w8 = *(p + nextlineSrc);
+
+		int tmpWidth = width;
+		while (tmpWidth--) {
+			p++;
+
+			w3 = *(p - nextlineSrc);
+			w6 = *(p);
+			w9 = *(p + nextlineSrc);
+
+			int pattern = 0;
+			const int yuv5 = YUV(5);
+			if (w5 != w1 && diffYUV(yuv5, YUV(1))) pattern |= 0x0001;
+			if (w5 != w2 && diffYUV(yuv5, YUV(2))) pattern |= 0x0002;
+			if (w5 != w3 && diffYUV(yuv5, YUV(3))) pattern |= 0x0004;
+			if (w5 != w4 && diffYUV(yuv5, YUV(4))) pattern |= 0x0008;
+			if (w5 != w6 && diffYUV(yuv5, YUV(6))) pattern |= 0x0010;
+			if (w5 != w7 && diffYUV(yuv5, YUV(7))) pattern |= 0x0020;
+			if (w5 != w8 && diffYUV(yuv5, YUV(8))) pattern |= 0x0040;
+			if (w5 != w9 && diffYUV(yuv5, YUV(9))) pattern |= 0x0080;
+
+			switch (pattern) {
+			case 0:
+			case 1:
+			case 4:
+			case 32:
+			case 128:
+			case 5:
+			case 132:
+			case 160:
+			case 33:
+			case 129:
+			case 36:
+			case 133:
+			case 164:
+			case 161:
+			case 37:
+			case 165:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 2:
+			case 34:
+			case 130:
+			case 162:
+				PIXEL00_22
+				PIXEL01_21
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 16:
+			case 17:
+			case 48:
+			case 49:
+				PIXEL00_20
+				PIXEL01_22
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 64:
+			case 65:
+			case 68:
+			case 69:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_21
+				PIXEL11_22
+				break;
+			case 8:
+			case 12:
+			case 136:
+			case 140:
+				PIXEL00_21
+				PIXEL01_20
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 3:
+			case 35:
+			case 131:
+			case 163:
+				PIXEL00_11
+				PIXEL01_21
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 6:
+			case 38:
+			case 134:
+			case 166:
+				PIXEL00_22
+				PIXEL01_12
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 20:
+			case 21:
+			case 52:
+			case 53:
+				PIXEL00_20
+				PIXEL01_11
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 144:
+			case 145:
+			case 176:
+			case 177:
+				PIXEL00_20
+				PIXEL01_22
+				PIXEL10_20
+				PIXEL11_12
+				break;
+			case 192:
+			case 193:
+			case 196:
+			case 197:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_21
+				PIXEL11_11
+				break;
+			case 96:
+			case 97:
+			case 100:
+			case 101:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_12
+				PIXEL11_22
+				break;
+			case 40:
+			case 44:
+			case 168:
+			case 172:
+				PIXEL00_21
+				PIXEL01_20
+				PIXEL10_11
+				PIXEL11_20
+				break;
+			case 9:
+			case 13:
+			case 137:
+			case 141:
+				PIXEL00_12
+				PIXEL01_20
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 18:
+			case 50:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 80:
+			case 81:
+				PIXEL00_20
+				PIXEL01_22
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 72:
+			case 76:
+				PIXEL00_21
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 10:
+			case 138:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 66:
+				PIXEL00_22
+				PIXEL01_21
+				PIXEL10_21
+				PIXEL11_22
+				break;
+			case 24:
+				PIXEL00_21
+				PIXEL01_22
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 7:
+			case 39:
+			case 135:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 148:
+			case 149:
+			case 180:
+				PIXEL00_20
+				PIXEL01_11
+				PIXEL10_20
+				PIXEL11_12
+				break;
+			case 224:
+			case 228:
+			case 225:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 41:
+			case 169:
+			case 45:
+				PIXEL00_12
+				PIXEL01_20
+				PIXEL10_11
+				PIXEL11_20
+				break;
+			case 22:
+			case 54:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 208:
+			case 209:
+				PIXEL00_20
+				PIXEL01_22
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 104:
+			case 108:
+				PIXEL00_21
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 11:
+			case 139:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 19:
+			case 51:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_11
+					PIXEL01_10
+				} else {
+					PIXEL00_60
+					PIXEL01_90
+				}
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 146:
+			case 178:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+					PIXEL11_12
+				} else {
+					PIXEL01_90
+					PIXEL11_61
+				}
+				PIXEL10_20
+				break;
+			case 84:
+			case 85:
+				PIXEL00_20
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL01_11
+					PIXEL11_10
+				} else {
+					PIXEL01_60
+					PIXEL11_90
+				}
+				PIXEL10_21
+				break;
+			case 112:
+			case 113:
+				PIXEL00_20
+				PIXEL01_22
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL10_12
+					PIXEL11_10
+				} else {
+					PIXEL10_61
+					PIXEL11_90
+				}
+				break;
+			case 200:
+			case 204:
+				PIXEL00_21
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+					PIXEL11_11
+				} else {
+					PIXEL10_90
+					PIXEL11_60
+				}
+				break;
+			case 73:
+			case 77:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_12
+					PIXEL10_10
+				} else {
+					PIXEL00_61
+					PIXEL10_90
+				}
+				PIXEL01_20
+				PIXEL11_22
+				break;
+			case 42:
+			case 170:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+					PIXEL10_11
+				} else {
+					PIXEL00_90
+					PIXEL10_60
+				}
+				PIXEL01_21
+				PIXEL11_20
+				break;
+			case 14:
+			case 142:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+					PIXEL01_12
+				} else {
+					PIXEL00_90
+					PIXEL01_61
+				}
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 67:
+				PIXEL00_11
+				PIXEL01_21
+				PIXEL10_21
+				PIXEL11_22
+				break;
+			case 70:
+				PIXEL00_22
+				PIXEL01_12
+				PIXEL10_21
+				PIXEL11_22
+				break;
+			case 28:
+				PIXEL00_21
+				PIXEL01_11
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 152:
+				PIXEL00_21
+				PIXEL01_22
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 194:
+				PIXEL00_22
+				PIXEL01_21
+				PIXEL10_21
+				PIXEL11_11
+				break;
+			case 98:
+				PIXEL00_22
+				PIXEL01_21
+				PIXEL10_12
+				PIXEL11_22
+				break;
+			case 56:
+				PIXEL00_21
+				PIXEL01_22
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 25:
+				PIXEL00_12
+				PIXEL01_22
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 26:
+			case 31:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 82:
+			case 214:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 88:
+			case 248:
+				PIXEL00_21
+				PIXEL01_22
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 74:
+			case 107:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 27:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_10
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 86:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_21
+				PIXEL11_10
+				break;
+			case 216:
+				PIXEL00_21
+				PIXEL01_22
+				PIXEL10_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 106:
+				PIXEL00_10
+				PIXEL01_21
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 30:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 210:
+				PIXEL00_22
+				PIXEL01_10
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 120:
+				PIXEL00_21
+				PIXEL01_22
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_10
+				break;
+			case 75:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				PIXEL10_10
+				PIXEL11_22
+				break;
+			case 29:
+				PIXEL00_12
+				PIXEL01_11
+				PIXEL10_22
+				PIXEL11_21
+				break;
+			case 198:
+				PIXEL00_22
+				PIXEL01_12
+				PIXEL10_21
+				PIXEL11_11
+				break;
+			case 184:
+				PIXEL00_21
+				PIXEL01_22
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 99:
+				PIXEL00_11
+				PIXEL01_21
+				PIXEL10_12
+				PIXEL11_22
+				break;
+			case 57:
+				PIXEL00_12
+				PIXEL01_22
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 71:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_21
+				PIXEL11_22
+				break;
+			case 156:
+				PIXEL00_21
+				PIXEL01_11
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 226:
+				PIXEL00_22
+				PIXEL01_21
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 60:
+				PIXEL00_21
+				PIXEL01_11
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 195:
+				PIXEL00_11
+				PIXEL01_21
+				PIXEL10_21
+				PIXEL11_11
+				break;
+			case 102:
+				PIXEL00_22
+				PIXEL01_12
+				PIXEL10_12
+				PIXEL11_22
+				break;
+			case 153:
+				PIXEL00_12
+				PIXEL01_22
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 58:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 83:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 92:
+				PIXEL00_21
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 202:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				PIXEL01_21
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				PIXEL11_11
+				break;
+			case 78:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				PIXEL11_22
+				break;
+			case 154:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 114:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 89:
+				PIXEL00_12
+				PIXEL01_22
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 90:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 55:
+			case 23:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_11
+					PIXEL01_0
+				} else {
+					PIXEL00_60
+					PIXEL01_90
+				}
+				PIXEL10_20
+				PIXEL11_21
+				break;
+			case 182:
+			case 150:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+					PIXEL11_12
+				} else {
+					PIXEL01_90
+					PIXEL11_61
+				}
+				PIXEL10_20
+				break;
+			case 213:
+			case 212:
+				PIXEL00_20
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL01_11
+					PIXEL11_0
+				} else {
+					PIXEL01_60
+					PIXEL11_90
+				}
+				PIXEL10_21
+				break;
+			case 241:
+			case 240:
+				PIXEL00_20
+				PIXEL01_22
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL10_12
+					PIXEL11_0
+				} else {
+					PIXEL10_61
+					PIXEL11_90
+				}
+				break;
+			case 236:
+			case 232:
+				PIXEL00_21
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+					PIXEL11_11
+				} else {
+					PIXEL10_90
+					PIXEL11_60
+				}
+				break;
+			case 109:
+			case 105:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_12
+					PIXEL10_0
+				} else {
+					PIXEL00_61
+					PIXEL10_90
+				}
+				PIXEL01_20
+				PIXEL11_22
+				break;
+			case 171:
+			case 43:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+					PIXEL10_11
+				} else {
+					PIXEL00_90
+					PIXEL10_60
+				}
+				PIXEL01_21
+				PIXEL11_20
+				break;
+			case 143:
+			case 15:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+					PIXEL01_12
+				} else {
+					PIXEL00_90
+					PIXEL01_61
+				}
+				PIXEL10_22
+				PIXEL11_20
+				break;
+			case 124:
+				PIXEL00_21
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_10
+				break;
+			case 203:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				PIXEL10_10
+				PIXEL11_11
+				break;
+			case 62:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 211:
+				PIXEL00_11
+				PIXEL01_10
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 118:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_12
+				PIXEL11_10
+				break;
+			case 217:
+				PIXEL00_12
+				PIXEL01_22
+				PIXEL10_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 110:
+				PIXEL00_10
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 155:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_10
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 188:
+				PIXEL00_21
+				PIXEL01_11
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 185:
+				PIXEL00_12
+				PIXEL01_22
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 61:
+				PIXEL00_12
+				PIXEL01_11
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 157:
+				PIXEL00_12
+				PIXEL01_11
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 103:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_12
+				PIXEL11_22
+				break;
+			case 227:
+				PIXEL00_11
+				PIXEL01_21
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 230:
+				PIXEL00_22
+				PIXEL01_12
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 199:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_21
+				PIXEL11_11
+				break;
+			case 220:
+				PIXEL00_21
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 158:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 234:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				PIXEL01_21
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_11
+				break;
+			case 242:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 59:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 121:
+				PIXEL00_12
+				PIXEL01_22
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 87:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 79:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				PIXEL11_22
+				break;
+			case 122:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 94:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 218:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 91:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 229:
+				PIXEL00_20
+				PIXEL01_20
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 167:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_20
+				PIXEL11_20
+				break;
+			case 173:
+				PIXEL00_12
+				PIXEL01_20
+				PIXEL10_11
+				PIXEL11_20
+				break;
+			case 181:
+				PIXEL00_20
+				PIXEL01_11
+				PIXEL10_20
+				PIXEL11_12
+				break;
+			case 186:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 115:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 93:
+				PIXEL00_12
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 206:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				PIXEL11_11
+				break;
+			case 205:
+			case 201:
+				PIXEL00_12
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_10
+				} else {
+					PIXEL10_70
+				}
+				PIXEL11_11
+				break;
+			case 174:
+			case 46:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_10
+				} else {
+					PIXEL00_70
+				}
+				PIXEL01_12
+				PIXEL10_11
+				PIXEL11_20
+				break;
+			case 179:
+			case 147:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_10
+				} else {
+					PIXEL01_70
+				}
+				PIXEL10_20
+				PIXEL11_12
+				break;
+			case 117:
+			case 116:
+				PIXEL00_20
+				PIXEL01_11
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_10
+				} else {
+					PIXEL11_70
+				}
+				break;
+			case 189:
+				PIXEL00_12
+				PIXEL01_11
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 231:
+				PIXEL00_11
+				PIXEL01_12
+				PIXEL10_12
+				PIXEL11_11
+				break;
+			case 126:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_10
+				break;
+			case 219:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_10
+				PIXEL10_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 125:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_12
+					PIXEL10_0
+				} else {
+					PIXEL00_61
+					PIXEL10_90
+				}
+				PIXEL01_11
+				PIXEL11_10
+				break;
+			case 221:
+				PIXEL00_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL01_11
+					PIXEL11_0
+				} else {
+					PIXEL01_60
+					PIXEL11_90
+				}
+				PIXEL10_10
+				break;
+			case 207:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+					PIXEL01_12
+				} else {
+					PIXEL00_90
+					PIXEL01_61
+				}
+				PIXEL10_10
+				PIXEL11_11
+				break;
+			case 238:
+				PIXEL00_10
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+					PIXEL11_11
+				} else {
+					PIXEL10_90
+					PIXEL11_60
+				}
+				break;
+			case 190:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+					PIXEL11_12
+				} else {
+					PIXEL01_90
+					PIXEL11_61
+				}
+				PIXEL10_11
+				break;
+			case 187:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+					PIXEL10_11
+				} else {
+					PIXEL00_90
+					PIXEL10_60
+				}
+				PIXEL01_10
+				PIXEL11_12
+				break;
+			case 243:
+				PIXEL00_11
+				PIXEL01_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL10_12
+					PIXEL11_0
+				} else {
+					PIXEL10_61
+					PIXEL11_90
+				}
+				break;
+			case 119:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_11
+					PIXEL01_0
+				} else {
+					PIXEL00_60
+					PIXEL01_90
+				}
+				PIXEL10_12
+				PIXEL11_10
+				break;
+			case 237:
+			case 233:
+				PIXEL00_12
+				PIXEL01_20
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				PIXEL11_11
+				break;
+			case 175:
+			case 47:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				PIXEL01_12
+				PIXEL10_11
+				PIXEL11_20
+				break;
+			case 183:
+			case 151:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_20
+				PIXEL11_12
+				break;
+			case 245:
+			case 244:
+				PIXEL00_20
+				PIXEL01_11
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 250:
+				PIXEL00_10
+				PIXEL01_10
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 123:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_10
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_10
+				break;
+			case 95:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_10
+				PIXEL11_10
+				break;
+			case 222:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 252:
+				PIXEL00_21
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 249:
+				PIXEL00_12
+				PIXEL01_22
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 235:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_21
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				PIXEL11_11
+				break;
+			case 111:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_22
+				break;
+			case 63:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_11
+				PIXEL11_21
+				break;
+			case 159:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_22
+				PIXEL11_12
+				break;
+			case 215:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_21
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 246:
+				PIXEL00_22
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 254:
+				PIXEL00_10
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 253:
+				PIXEL00_12
+				PIXEL01_11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 251:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				PIXEL01_10
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 239:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				PIXEL01_12
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				PIXEL11_11
+				break;
+			case 127:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_20
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_20
+				}
+				PIXEL11_10
+				break;
+			case 191:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_11
+				PIXEL11_12
+				break;
+			case 223:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_20
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_10
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_20
+				}
+				break;
+			case 247:
+				PIXEL00_11
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				PIXEL10_12
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			case 255:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_0
+				} else {
+					PIXEL00_100
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_0
+				} else {
+					PIXEL01_100
+				}
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_0
+				} else {
+					PIXEL10_100
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL11_0
+				} else {
+					PIXEL11_100
+				}
+				break;
+			}
+
+			w1 = w2;
+			w4 = w5;
+			w7 = w8;
+
+			w2 = w3;
+			w5 = w6;
+			w8 = w9;
+
+			q += 2;
+		}
+		p += nextlineSrc - width;
+		q += (nextlineDst - width) * 2;
+	}
+}
+
+#endif // Assembly version
+
+#ifdef USE_NASM
+// Assembly version of HQ3x
+
+extern "C" {
+
+#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
+#define hq3x_16 _hq3x_16
+#endif
+
+
+void hq3x_16(const byte *, byte *, uint32, uint32, uint32, uint32);
+
+}
+
+void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	hq3x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
+}
+
+#else
+
+#define PIXEL00_1M  *(q) = interpolate16_3_1<ColorMask >(w5, w1);
+#define PIXEL00_1U  *(q) = interpolate16_3_1<ColorMask >(w5, w2);
+#define PIXEL00_1L  *(q) = interpolate16_3_1<ColorMask >(w5, w4);
+#define PIXEL00_2   *(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
+#define PIXEL00_4   *(q) = interpolate16_2_7_7<ColorMask >(w5, w4, w2);
+#define PIXEL00_5   *(q) = interpolate16_1_1<ColorMask >(w4, w2);
+#define PIXEL00_C   *(q) = w5;
+
+#define PIXEL01_1   *(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
+#define PIXEL01_3   *(q+1) = interpolate16_7_1<ColorMask >(w5, w2);
+#define PIXEL01_6   *(q+1) = interpolate16_3_1<ColorMask >(w2, w5);
+#define PIXEL01_C   *(q+1) = w5;
+
+#define PIXEL02_1M  *(q+2) = interpolate16_3_1<ColorMask >(w5, w3);
+#define PIXEL02_1U  *(q+2) = interpolate16_3_1<ColorMask >(w5, w2);
+#define PIXEL02_1R  *(q+2) = interpolate16_3_1<ColorMask >(w5, w6);
+#define PIXEL02_2   *(q+2) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
+#define PIXEL02_4   *(q+2) = interpolate16_2_7_7<ColorMask >(w5, w2, w6);
+#define PIXEL02_5   *(q+2) = interpolate16_1_1<ColorMask >(w2, w6);
+#define PIXEL02_C   *(q+2) = w5;
+
+#define PIXEL10_1   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
+#define PIXEL10_3   *(q+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w4);
+#define PIXEL10_6   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w4, w5);
+#define PIXEL10_C   *(q+nextlineDst) = w5;
+
+#define PIXEL11     *(q+1+nextlineDst) = w5;
+
+#define PIXEL12_1   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
+#define PIXEL12_3   *(q+2+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w6);
+#define PIXEL12_6   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w6, w5);
+#define PIXEL12_C   *(q+2+nextlineDst) = w5;
+
+#define PIXEL20_1M  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w7);
+#define PIXEL20_1D  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
+#define PIXEL20_1L  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w4);
+#define PIXEL20_2   *(q+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
+#define PIXEL20_4   *(q+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w8, w4);
+#define PIXEL20_5   *(q+nextlineDst2) = interpolate16_1_1<ColorMask >(w8, w4);
+#define PIXEL20_C   *(q+nextlineDst2) = w5;
+
+#define PIXEL21_1   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
+#define PIXEL21_3   *(q+1+nextlineDst2) = interpolate16_7_1<ColorMask >(w5, w8);
+#define PIXEL21_6   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w8, w5);
+#define PIXEL21_C   *(q+1+nextlineDst2) = w5;
+
+#define PIXEL22_1M  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w9);
+#define PIXEL22_1D  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
+#define PIXEL22_1R  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w6);
+#define PIXEL22_2   *(q+2+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
+#define PIXEL22_4   *(q+2+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w6, w8);
+#define PIXEL22_5   *(q+2+nextlineDst2) = interpolate16_1_1<ColorMask >(w6, w8);
+#define PIXEL22_C   *(q+2+nextlineDst2) = w5;
+
+extern "C" uint32   *RGBtoYUV;
+#define YUV(x)	RGBtoYUV[w ## x]
+
+/*
+ * The HQ3x high quality 3x graphics filter.
+ * Original author Maxim Stepin (see http://www.hiend3d.com/hq3x.html).
+ * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
+ */
+template<typename ColorMask>
+static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	register int  w1, w2, w3, w4, w5, w6, w7, w8, w9;
+
+	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
+	const uint16 *p = (const uint16 *)srcPtr;
+
+	const uint32 nextlineDst = dstPitch / sizeof(uint16);
+	const uint32 nextlineDst2 = 2 * nextlineDst;
+	uint16 *q = (uint16 *)dstPtr;
+
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w1 | w2 | w3 |
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w4 | w5 | w6 |
+	//	 +----+----+----+
+	//	 |    |    |    |
+	//	 | w7 | w8 | w9 |
+	//	 +----+----+----+
+
+	while (height--) {
+		w1 = *(p - 1 - nextlineSrc);
+		w4 = *(p - 1);
+		w7 = *(p - 1 + nextlineSrc);
+
+		w2 = *(p - nextlineSrc);
+		w5 = *(p);
+		w8 = *(p + nextlineSrc);
+
+		int tmpWidth = width;
+		while (tmpWidth--) {
+			p++;
+
+			w3 = *(p - nextlineSrc);
+			w6 = *(p);
+			w9 = *(p + nextlineSrc);
+
+			int pattern = 0;
+			const int yuv5 = YUV(5);
+			if (w5 != w1 && diffYUV(yuv5, YUV(1))) pattern |= 0x0001;
+			if (w5 != w2 && diffYUV(yuv5, YUV(2))) pattern |= 0x0002;
+			if (w5 != w3 && diffYUV(yuv5, YUV(3))) pattern |= 0x0004;
+			if (w5 != w4 && diffYUV(yuv5, YUV(4))) pattern |= 0x0008;
+			if (w5 != w6 && diffYUV(yuv5, YUV(6))) pattern |= 0x0010;
+			if (w5 != w7 && diffYUV(yuv5, YUV(7))) pattern |= 0x0020;
+			if (w5 != w8 && diffYUV(yuv5, YUV(8))) pattern |= 0x0040;
+			if (w5 != w9 && diffYUV(yuv5, YUV(9))) pattern |= 0x0080;
+
+			switch (pattern) {
+			case 0:
+			case 1:
+			case 4:
+			case 32:
+			case 128:
+			case 5:
+			case 132:
+			case 160:
+			case 33:
+			case 129:
+			case 36:
+			case 133:
+			case 164:
+			case 161:
+			case 37:
+			case 165:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 2:
+			case 34:
+			case 130:
+			case 162:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 16:
+			case 17:
+			case 48:
+			case 49:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 64:
+			case 65:
+			case 68:
+			case 69:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 8:
+			case 12:
+			case 136:
+			case 140:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 3:
+			case 35:
+			case 131:
+			case 163:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 6:
+			case 38:
+			case 134:
+			case 166:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 20:
+			case 21:
+			case 52:
+			case 53:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 144:
+			case 145:
+			case 176:
+			case 177:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 192:
+			case 193:
+			case 196:
+			case 197:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 96:
+			case 97:
+			case 100:
+			case 101:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 40:
+			case 44:
+			case 168:
+			case 172:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 9:
+			case 13:
+			case 137:
+			case 141:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 18:
+			case 50:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_1M
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 80:
+			case 81:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_1M
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 72:
+			case 76:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_1M
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 10:
+			case 138:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 66:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 24:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 7:
+			case 39:
+			case 135:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 148:
+			case 149:
+			case 180:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 224:
+			case 228:
+			case 225:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 41:
+			case 169:
+			case 45:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 22:
+			case 54:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 208:
+			case 209:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 104:
+			case 108:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 11:
+			case 139:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 19:
+			case 51:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_1L
+					PIXEL01_C
+					PIXEL02_1M
+					PIXEL12_C
+				} else {
+					PIXEL00_2
+					PIXEL01_6
+					PIXEL02_5
+					PIXEL12_1
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 146:
+			case 178:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_1M
+					PIXEL12_C
+					PIXEL22_1D
+				} else {
+					PIXEL01_1
+					PIXEL02_5
+					PIXEL12_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				break;
+			case 84:
+			case 85:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL02_1U
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_1M
+				} else {
+					PIXEL02_2
+					PIXEL12_6
+					PIXEL21_1
+					PIXEL22_5
+				}
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				break;
+			case 112:
+			case 113:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL20_1L
+					PIXEL21_C
+					PIXEL22_1M
+				} else {
+					PIXEL12_1
+					PIXEL20_2
+					PIXEL21_6
+					PIXEL22_5
+				}
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				break;
+			case 200:
+			case 204:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_1M
+					PIXEL21_C
+					PIXEL22_1R
+				} else {
+					PIXEL10_1
+					PIXEL20_5
+					PIXEL21_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				break;
+			case 73:
+			case 77:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_1U
+					PIXEL10_C
+					PIXEL20_1M
+					PIXEL21_C
+				} else {
+					PIXEL00_2
+					PIXEL10_6
+					PIXEL20_5
+					PIXEL21_1
+				}
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				PIXEL22_1M
+				break;
+			case 42:
+			case 170:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+					PIXEL01_C
+					PIXEL10_C
+					PIXEL20_1D
+				} else {
+					PIXEL00_5
+					PIXEL01_1
+					PIXEL10_6
+					PIXEL20_2
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 14:
+			case 142:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+					PIXEL01_C
+					PIXEL02_1R
+					PIXEL10_C
+				} else {
+					PIXEL00_5
+					PIXEL01_6
+					PIXEL02_2
+					PIXEL10_1
+				}
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 67:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 70:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 28:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 152:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 194:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 98:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 56:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 25:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 26:
+			case 31:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL10_3
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 82:
+			case 214:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 88:
+			case 248:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL22_4
+				}
+				break;
+			case 74:
+			case 107:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+				}
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 27:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 86:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 216:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 106:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 30:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 210:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 120:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 75:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 29:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 198:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 184:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 99:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 57:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 71:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 156:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 226:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 60:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 195:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 102:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 153:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 58:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 83:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 92:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 202:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 78:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 154:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 114:
+				PIXEL00_1M
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 89:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 90:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 55:
+			case 23:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_1L
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL00_2
+					PIXEL01_6
+					PIXEL02_5
+					PIXEL12_1
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 182:
+			case 150:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+					PIXEL22_1D
+				} else {
+					PIXEL01_1
+					PIXEL02_5
+					PIXEL12_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_2
+				PIXEL21_1
+				break;
+			case 213:
+			case 212:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL02_1U
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL02_2
+					PIXEL12_6
+					PIXEL21_1
+					PIXEL22_5
+				}
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				break;
+			case 241:
+			case 240:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL20_1L
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_1
+					PIXEL20_2
+					PIXEL21_6
+					PIXEL22_5
+				}
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				break;
+			case 236:
+			case 232:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+					PIXEL22_1R
+				} else {
+					PIXEL10_1
+					PIXEL20_5
+					PIXEL21_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				break;
+			case 109:
+			case 105:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_1U
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL00_2
+					PIXEL10_6
+					PIXEL20_5
+					PIXEL21_1
+				}
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL11
+				PIXEL12_1
+				PIXEL22_1M
+				break;
+			case 171:
+			case 43:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+					PIXEL20_1D
+				} else {
+					PIXEL00_5
+					PIXEL01_1
+					PIXEL10_6
+					PIXEL20_2
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 143:
+			case 15:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL02_1R
+					PIXEL10_C
+				} else {
+					PIXEL00_5
+					PIXEL01_6
+					PIXEL02_2
+					PIXEL10_1
+				}
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 124:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 203:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 62:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 211:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 118:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 217:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 110:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 155:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 188:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 185:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 61:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 157:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 103:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 227:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 230:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 199:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 220:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 158:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 234:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1R
+				break;
+			case 242:
+				PIXEL00_1M
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1L
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 59:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 121:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 87:
+				PIXEL00_1L
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 79:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1R
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 122:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 94:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_C
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 218:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 91:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 229:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 167:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 173:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 181:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 186:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 115:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 93:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 206:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 205:
+			case 201:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_1M
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 174:
+			case 46:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_1M
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 179:
+			case 147:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_1M
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 117:
+			case 116:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_1M
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 189:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 231:
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 126:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 219:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+					PIXEL10_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 125:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL00_1U
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL00_2
+					PIXEL10_6
+					PIXEL20_5
+					PIXEL21_1
+				}
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL11
+				PIXEL12_C
+				PIXEL22_1M
+				break;
+			case 221:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL02_1U
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL02_2
+					PIXEL12_6
+					PIXEL21_1
+					PIXEL22_5
+				}
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1M
+				break;
+			case 207:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL02_1R
+					PIXEL10_C
+				} else {
+					PIXEL00_5
+					PIXEL01_6
+					PIXEL02_2
+					PIXEL10_1
+				}
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 238:
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+					PIXEL22_1R
+				} else {
+					PIXEL10_1
+					PIXEL20_5
+					PIXEL21_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL11
+				PIXEL12_1
+				break;
+			case 190:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+					PIXEL22_1D
+				} else {
+					PIXEL01_1
+					PIXEL02_5
+					PIXEL12_6
+					PIXEL22_2
+				}
+				PIXEL00_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1D
+				PIXEL21_1
+				break;
+			case 187:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+					PIXEL20_1D
+				} else {
+					PIXEL00_5
+					PIXEL01_1
+					PIXEL10_6
+					PIXEL20_2
+				}
+				PIXEL02_1M
+				PIXEL11
+				PIXEL12_C
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 243:
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL20_1L
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_1
+					PIXEL20_2
+					PIXEL21_6
+					PIXEL22_5
+				}
+				PIXEL00_1L
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL10_1
+				PIXEL11
+				break;
+			case 119:
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL00_1L
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL00_2
+					PIXEL01_6
+					PIXEL02_5
+					PIXEL12_1
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL20_1L
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 237:
+			case 233:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_2
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 175:
+			case 47:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_2
+				break;
+			case 183:
+			case 151:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_2
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 245:
+			case 244:
+				PIXEL00_2
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 250:
+				PIXEL00_1M
+				PIXEL01_C
+				PIXEL02_1M
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL22_4
+				}
+				break;
+			case 123:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+				}
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 95:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL10_3
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL11
+				PIXEL20_1M
+				PIXEL21_C
+				PIXEL22_1M
+				break;
+			case 222:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 252:
+				PIXEL00_1M
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 249:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL22_4
+				}
+				break;
+			case 235:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+				}
+				PIXEL02_1M
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 111:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 63:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1M
+				break;
+			case 159:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL10_3
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 215:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 246:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 254:
+				PIXEL00_1M
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+				} else {
+					PIXEL01_3
+					PIXEL02_4
+				}
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+				} else {
+					PIXEL10_3
+					PIXEL20_4
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL21_3
+					PIXEL22_2
+				}
+				break;
+			case 253:
+				PIXEL00_1U
+				PIXEL01_1
+				PIXEL02_1U
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 251:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+				} else {
+					PIXEL00_4
+					PIXEL01_3
+				}
+				PIXEL02_1M
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL10_C
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL10_3
+					PIXEL20_2
+					PIXEL21_3
+				}
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL12_C
+					PIXEL22_C
+				} else {
+					PIXEL12_3
+					PIXEL22_4
+				}
+				break;
+			case 239:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				PIXEL02_1R
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_1
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				PIXEL22_1R
+				break;
+			case 127:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL01_C
+					PIXEL10_C
+				} else {
+					PIXEL00_2
+					PIXEL01_3
+					PIXEL10_3
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL02_4
+					PIXEL12_3
+				}
+				PIXEL11
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+					PIXEL21_C
+				} else {
+					PIXEL20_4
+					PIXEL21_3
+				}
+				PIXEL22_1M
+				break;
+			case 191:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1D
+				PIXEL21_1
+				PIXEL22_1D
+				break;
+			case 223:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+					PIXEL10_C
+				} else {
+					PIXEL00_4
+					PIXEL10_3
+				}
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL01_C
+					PIXEL02_C
+					PIXEL12_C
+				} else {
+					PIXEL01_3
+					PIXEL02_2
+					PIXEL12_3
+				}
+				PIXEL11
+				PIXEL20_1M
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL21_C
+					PIXEL22_C
+				} else {
+					PIXEL21_3
+					PIXEL22_4
+				}
+				break;
+			case 247:
+				PIXEL00_1L
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_1
+				PIXEL11
+				PIXEL12_C
+				PIXEL20_1L
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			case 255:
+				if (diffYUV(YUV(4), YUV(2))) {
+					PIXEL00_C
+				} else {
+					PIXEL00_2
+				}
+				PIXEL01_C
+				if (diffYUV(YUV(2), YUV(6))) {
+					PIXEL02_C
+				} else {
+					PIXEL02_2
+				}
+				PIXEL10_C
+				PIXEL11
+				PIXEL12_C
+				if (diffYUV(YUV(8), YUV(4))) {
+					PIXEL20_C
+				} else {
+					PIXEL20_2
+				}
+				PIXEL21_C
+				if (diffYUV(YUV(6), YUV(8))) {
+					PIXEL22_C
+				} else {
+					PIXEL22_2
+				}
+				break;
+			}
+
+			w1 = w2;
+			w4 = w5;
+			w7 = w8;
+
+			w2 = w3;
+			w5 = w6;
+			w8 = w9;
+
+			q += 3;
+		}
+		p += nextlineSrc - width;
+		q += (nextlineDst - width) * 3;
+	}
+}
+
+#endif // Assembly version
+
 HQPlugin::HQPlugin() {
 	_factor = 2;
 	_factors.push_back(2);
@@ -130,10 +4982,28 @@ void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	switch (_factor) {
 	case 2:
+#ifdef USE_NASM
 		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+#else
+		if (_format.gLoss == 2)
+			HQ2x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
+					dstPitch, width, height);
+		else
+			HQ2x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
+					dstPitch, width, height);
+#endif
 		break;
 	case 3:
+#ifdef USE_NASM
 		HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+#else
+		if (_format.gLoss == 2)
+			HQ3x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
+					dstPitch, width, height);
+		else
+			HQ3x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
+					dstPitch, width, height);
+#endif
 		break;
 	}
 }


Commit: 8030b7879caaaeecf650111f3993f05af5ab36a1
    https://github.com/scummvm/scummvm/commit/8030b7879caaaeecf650111f3993f05af5ab36a1
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS/SDL: add ifdefs for disabling scalers

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    base/plugins.cpp
    graphics/scaler/normal.cpp
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 91d5b2ef78..9c32320816 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -165,16 +165,16 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 	_scalerProc = Normal2x;
-	// HACK: just pick first scaler plugin
-	_normalPlugin = _scalerPlugin = _scalerPlugins.front();
-	_scalerIndex = 0;
-	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
 	_videoMode.aspectRatioCorrection = false;
 	_scalerProc = Normal1x;
 #endif
+	// HACK: just pick first scaler plugin
+	_normalPlugin = _scalerPlugin = _scalerPlugins.front();
+	_scalerIndex = 0;
+	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 	_scalerType = 0;
 
 	_videoMode.fullscreen = ConfMan.getBool("fullscreen");
@@ -2097,12 +2097,15 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	// If possible, use the same scaler for the cursor as for the rest of
 	// the game. This only works well with the non-blurring scalers so we
 	// otherwise use the Normal scaler
+#ifdef USE_SCALERS
 	if (_cursorTargetScale == 1) {
 		if ((*_scalerPlugin)->canDrawCursor()) {
+#endif
 		(*_scalerPlugin)->scale(
 			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
 			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 			_mouseCurState.w, _mouseCurState.h, 0, 0);
+#ifdef USE_SCALERS
 		} else {
 			int tmpFactor = (*_normalPlugin)->getFactor();
 			(*_normalPlugin)->setFactor(_videoMode.scaleFactor);
@@ -2126,6 +2129,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 			_mouseCurState.w, _mouseCurState.h, bytesPerPixel);
 	}
+#endif
 
 #ifdef USE_SCALERS
 	if (!_cursorDontScale && _videoMode.aspectRatioCorrection)
diff --git a/base/plugins.cpp b/base/plugins.cpp
index 46289644ce..47f38610da 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -163,9 +163,13 @@ public:
 
 		// Scaler plugins
 		LINK_PLUGIN(NORMAL)
+#ifdef USE_SCALERS
+#ifdef USE_HQ_SCALERS
 		LINK_PLUGIN(HQ)
+#endif
 		LINK_PLUGIN(SAI)
 		LINK_PLUGIN(DOTMATRIX)
+#endif
 
 		return pl;
 	}
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 5a552d9c16..7f535a7178 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -25,14 +25,17 @@
 NormalPlugin::NormalPlugin() {
 	_factor = 1;
 	_factors.push_back(1);
+#ifdef USE_SCALERS
 	_factors.push_back(2);
 	_factors.push_back(3);
 	_factors.push_back(4);
+#endif
 }
 
 void NormalPlugin::initialize(Graphics::PixelFormat format) {
 }
 
+#ifdef USE_SCALERS
 /**
  * Trivial nearest-neighbor 4x scaler.
  */
@@ -70,9 +73,11 @@ void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 		dstPtr += dstPitch4;
 	}
 }
+#endif
 
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+#ifdef USE_SCALERS
 	switch (_factor) {
 	case 1:
 		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
@@ -87,17 +92,24 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		Normal4x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		break;
 	}
+#else
+	Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+#endif
 }
 
 uint NormalPlugin::increaseFactor() {
+#ifdef USE_SCALERS
 	if (_factor < 4)
 		++_factor;
+#endif
 	return _factor;
 }
 
 uint NormalPlugin::decreaseFactor() {
+#ifdef USE_SCALERS
 	if (_factor > 1)
 		--_factor;
+#endif
 	return _factor;
 }
 
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index d68a5f4357..63b5e4573c 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -28,8 +28,10 @@ void ScalerPluginObject::scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *ds
 	Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 }
 
+#ifdef USE_SCALERS
 void ScalerPluginObject::scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
 	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
 	Normal1o5x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 }
+#endif
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 6d9c6048ce..efa5d274f9 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -95,11 +95,13 @@ public:
 	static void scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
 
+#ifdef USE_SCALERS
 	/**
 	 * Useful for scaling the mouse
 	 */
 	static void scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
+#endif
 
 protected:
 	uint _factor;


Commit: 5e735331106eed263600cf36353bc51c56dfad69
    https://github.com/scummvm/scummvm/commit/5e735331106eed263600cf36353bc51c56dfad69
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add SuperEagle and SuperSAI scaler plugins

Changed paths:
    base/plugins.cpp
    graphics/module.mk
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 47f38610da..1bfc957fa3 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -168,6 +168,8 @@ public:
 		LINK_PLUGIN(HQ)
 #endif
 		LINK_PLUGIN(SAI)
+		LINK_PLUGIN(SUPERSAI)
+		LINK_PLUGIN(SUPEREAGLE)
 		LINK_PLUGIN(DOTMATRIX)
 #endif
 
diff --git a/graphics/module.mk b/graphics/module.mk
index a6d9726a89..d8464c5b28 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -85,7 +85,6 @@ endif
 
 ifdef USE_SCALERS
 MODULE_OBJS += \
-	scaler/2xsai.o \
 	scaler/dotmatrix.o \
 	scaler/sai.o \
 	scaler/aspect.o \
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 5442f383b9..cb0f995b49 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -20,7 +20,380 @@
  */
 
 #include "graphics/scaler/sai.h"
-#include "graphics/scaler.h"
+#include "graphics/scaler/intern.h"
+
+static inline int GetResult(uint32 A, uint32 B, uint32 C, uint32 D) {
+	const bool ac = (A==C);
+	const bool bc = (B==C);
+	const int x1 = ac;
+	const int y1 = (bc & !ac);
+	const bool ad = (A==D);
+	const bool bd = (B==D);
+	const int x2 = ad;
+	const int y2 = (bd & !ad);
+	const int x = x1+x2;
+	const int y = y1+y2;
+	return (y>>1) - (x>>1);
+}
+
+#define interpolate_1_1		interpolate16_1_1<ColorMask>
+#define interpolate_3_1		interpolate16_3_1<ColorMask>
+#define interpolate_6_1_1	interpolate16_6_1_1<ColorMask>
+#define interpolate_1_1_1_1	interpolate16_1_1_1_1<ColorMask>
+
+template<typename ColorMask>
+void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	const uint16 *bP;
+	uint16 *dP;
+	const uint32 nextlineSrc = srcPitch >> 1;
+
+	while (height--) {
+		bP = (const uint16 *)srcPtr;
+		dP = (uint16 *)dstPtr;
+
+		for (int i = 0; i < width; ++i) {
+			unsigned color4, color5, color6;
+			unsigned color1, color2, color3;
+			unsigned colorA0, colorA1, colorA2, colorA3;
+			unsigned colorB0, colorB1, colorB2, colorB3;
+			unsigned colorS1, colorS2;
+			unsigned product1a, product1b, product2a, product2b;
+
+//---------------------------------------    B1 B2
+//                                         4  5  6 S2
+//                                         1  2  3 S1
+//                                           A1 A2
+
+			colorB0 = *(bP - nextlineSrc - 1);
+			colorB1 = *(bP - nextlineSrc);
+			colorB2 = *(bP - nextlineSrc + 1);
+			colorB3 = *(bP - nextlineSrc + 2);
+
+			color4 = *(bP - 1);
+			color5 = *(bP);
+			color6 = *(bP + 1);
+			colorS2 = *(bP + 2);
+
+			color1 = *(bP + nextlineSrc - 1);
+			color2 = *(bP + nextlineSrc);
+			color3 = *(bP + nextlineSrc + 1);
+			colorS1 = *(bP + nextlineSrc + 2);
+
+			colorA0 = *(bP + 2 * nextlineSrc - 1);
+			colorA1 = *(bP + 2 * nextlineSrc);
+			colorA2 = *(bP + 2 * nextlineSrc + 1);
+			colorA3 = *(bP + 2 * nextlineSrc + 2);
+
+//--------------------------------------
+			if (color2 == color6 && color5 != color3) {
+				product2b = product1b = color2;
+			} else if (color5 == color3 && color2 != color6) {
+				product2b = product1b = color5;
+			} else if (color5 == color3 && color2 == color6) {
+				register int r = 0;
+
+				r += GetResult(color6, color5, color1, colorA1);
+				r += GetResult(color6, color5, color4, colorB1);
+				r += GetResult(color6, color5, colorA2, colorS1);
+				r += GetResult(color6, color5, colorB2, colorS2);
+
+				if (r > 0)
+					product2b = product1b = color6;
+				else if (r < 0)
+					product2b = product1b = color5;
+				else {
+					product2b = product1b = interpolate_1_1(color5, color6);
+				}
+			} else {
+				if (color6 == color3 && color3 == colorA1 && color2 != colorA2 && color3 != colorA0)
+					product2b = interpolate_3_1(color3, color2);
+				else if (color5 == color2 && color2 == colorA2 && colorA1 != color3 && color2 != colorA3)
+					product2b = interpolate_3_1(color2, color3);
+				else
+					product2b = interpolate_1_1(color2, color3);
+
+				if (color6 == color3 && color6 == colorB1 && color5 != colorB2 && color6 != colorB0)
+					product1b = interpolate_3_1(color6, color5);
+				else if (color5 == color2 && color5 == colorB2 && colorB1 != color6 && color5 != colorB3)
+					product1b = interpolate_3_1(color5, color6);
+				else
+					product1b = interpolate_1_1(color5, color6);
+			}
+
+			if (color5 == color3 && color2 != color6 && color4 == color5 && color5 != colorA2)
+				product2a = interpolate_1_1(color2, color5);
+			else if (color5 == color1 && color6 == color5 && color4 != color2 && color5 != colorA0)
+				product2a = interpolate_1_1(color2, color5);
+			else
+				product2a = color2;
+
+			if (color2 == color6 && color5 != color3 && color1 == color2 && color2 != colorB2)
+				product1a = interpolate_1_1(color2, color5);
+			else if (color4 == color2 && color3 == color2 && color1 != color5 && color2 != colorB0)
+				product1a = interpolate_1_1(color2, color5);
+			else
+				product1a = color5;
+
+			*(dP + 0) = (uint16) product1a;
+			*(dP + 1) = (uint16) product1b;
+			*(dP + dstPitch/2 + 0) = (uint16) product2a;
+			*(dP + dstPitch/2 + 1) = (uint16) product2b;
+
+			bP += 1;
+			dP += 2;
+		}
+
+		srcPtr += srcPitch;
+		dstPtr += dstPitch * 2;
+	}
+}
+
+template<typename ColorMask>
+void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	const uint16 *bP;
+	uint16 *dP;
+	const uint32 nextlineSrc = srcPitch >> 1;
+
+	while (height--) {
+		bP = (const uint16 *)srcPtr;
+		dP = (uint16 *)dstPtr;
+		for (int i = 0; i < width; ++i) {
+			unsigned color4, color5, color6;
+			unsigned color1, color2, color3;
+			unsigned colorA1, colorA2, colorB1, colorB2, colorS1, colorS2;
+			unsigned product1a, product1b, product2a, product2b;
+
+			colorB1 = *(bP - nextlineSrc);
+			colorB2 = *(bP - nextlineSrc + 1);
+
+			color4 = *(bP - 1);
+			color5 = *(bP);
+			color6 = *(bP + 1);
+			colorS2 = *(bP + 2);
+
+			color1 = *(bP + nextlineSrc - 1);
+			color2 = *(bP + nextlineSrc);
+			color3 = *(bP + nextlineSrc + 1);
+			colorS1 = *(bP + nextlineSrc + 2);
+
+			colorA1 = *(bP + 2 * nextlineSrc);
+			colorA2 = *(bP + 2 * nextlineSrc + 1);
+
+			// --------------------------------------
+			if (color5 != color3) {
+				if (color2 == color6) {
+					product1b = product2a = color2;
+					if ((color1 == color2) || (color6 == colorB2)) {
+						product1a = interpolate_3_1(color2, color5);
+					} else {
+						product1a = interpolate_1_1(color5, color6);
+					}
+
+					if ((color6 == colorS2) || (color2 == colorA1)) {
+						product2b = interpolate_3_1(color2, color3);
+					} else {
+						product2b = interpolate_1_1(color2, color3);
+					}
+				} else {
+					product2b = interpolate_6_1_1(color3, color2, color6);
+					product1a = interpolate_6_1_1(color5, color2, color6);
+
+					product2a = interpolate_6_1_1(color2, color5, color3);
+					product1b = interpolate_6_1_1(color6, color5, color3);
+				}
+			} else {
+				if (color2 != color6) {
+					product2b = product1a = color5;
+
+					if ((colorB1 == color5) || (color3 == colorS1)) {
+						product1b = interpolate_3_1(color5, color6);
+					} else {
+						product1b = interpolate_1_1(color5, color6);
+					}
+
+					if ((color3 == colorA2) || (color4 == color5)) {
+						product2a = interpolate_3_1(color5, color2);
+					} else {
+						product2a = interpolate_1_1(color2, color3);
+					}
+				} else {
+					register int r = 0;
+
+					r += GetResult(color6, color5, color1, colorA1);
+					r += GetResult(color6, color5, color4, colorB1);
+					r += GetResult(color6, color5, colorA2, colorS1);
+					r += GetResult(color6, color5, colorB2, colorS2);
+
+					if (r > 0) {
+						product1b = product2a = color2;
+						product1a = product2b = interpolate_1_1(color5, color6);
+					} else if (r < 0) {
+						product2b = product1a = color5;
+						product1b = product2a = interpolate_1_1(color5, color6);
+					} else {
+						product2b = product1a = color5;
+						product1b = product2a = color2;
+					}
+				}
+			}
+
+			*(dP + 0) = (uint16) product1a;
+			*(dP + 1) = (uint16) product1b;
+			*(dP + dstPitch/2 + 0) = (uint16) product2a;
+			*(dP + dstPitch/2 + 1) = (uint16) product2b;
+
+			bP += 1;
+			dP += 2;
+		}
+
+		srcPtr += srcPitch;
+		dstPtr += dstPitch * 2;
+	}
+}
+
+
+void SuperEagle(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	extern int gBitFormat;
+	if (gBitFormat == 565)
+		SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	else
+		SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
+
+template<typename ColorMask>
+void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	const uint16 *bP;
+	uint16 *dP;
+	const uint32 nextlineSrc = srcPitch >> 1;
+
+	while (height--) {
+		bP = (const uint16 *)srcPtr;
+		dP = (uint16 *)dstPtr;
+
+		for (int i = 0; i < width; ++i) {
+
+			register unsigned colorA, colorB;
+			unsigned colorC, colorD,
+				colorE, colorF, colorG, colorH, colorI, colorJ, colorK, colorL, colorM, colorN, colorO;
+			unsigned product, product1, product2;
+
+//---------------------------------------
+// Map of the pixels:                    I|E F|J
+//                                       G|A B|K
+//                                       H|C D|L
+//                                       M|N O|P
+			colorI = *(bP - nextlineSrc - 1);
+			colorE = *(bP - nextlineSrc);
+			colorF = *(bP - nextlineSrc + 1);
+			colorJ = *(bP - nextlineSrc + 2);
+
+			colorG = *(bP - 1);
+			colorA = *(bP);
+			colorB = *(bP + 1);
+			colorK = *(bP + 2);
+
+			colorH = *(bP + nextlineSrc - 1);
+			colorC = *(bP + nextlineSrc);
+			colorD = *(bP + nextlineSrc + 1);
+			colorL = *(bP + nextlineSrc + 2);
+
+			colorM = *(bP + 2 * nextlineSrc - 1);
+			colorN = *(bP + 2 * nextlineSrc);
+			colorO = *(bP + 2 * nextlineSrc + 1);
+
+			if ((colorA == colorD) && (colorB != colorC)) {
+				if (((colorA == colorE) && (colorB == colorL)) ||
+					((colorA == colorC) && (colorA == colorF) && (colorB != colorE) && (colorB == colorJ))) {
+					product = colorA;
+				} else {
+					product = interpolate_1_1(colorA, colorB);
+				}
+
+				if (((colorA == colorG) && (colorC == colorO)) ||
+					((colorA == colorB) && (colorA == colorH) && (colorG != colorC)  && (colorC == colorM))) {
+					product1 = colorA;
+				} else {
+					product1 = interpolate_1_1(colorA, colorC);
+				}
+				product2 = colorA;
+			} else if ((colorB == colorC) && (colorA != colorD)) {
+				if (((colorB == colorF) && (colorA == colorH)) ||
+					((colorB == colorE) && (colorB == colorD) && (colorA != colorF) && (colorA == colorI))) {
+					product = colorB;
+				} else {
+					product = interpolate_1_1(colorA, colorB);
+				}
+
+				if (((colorC == colorH) && (colorA == colorF)) ||
+					((colorC == colorG) && (colorC == colorD) && (colorA != colorH) && (colorA == colorI))) {
+					product1 = colorC;
+				} else {
+					product1 = interpolate_1_1(colorA, colorC);
+				}
+				product2 = colorB;
+			} else if ((colorA == colorD) && (colorB == colorC)) {
+				if (colorA == colorB) {
+					product = colorA;
+					product1 = colorA;
+					product2 = colorA;
+				} else {
+					register int r = 0;
+
+					product1 = interpolate_1_1(colorA, colorC);
+					product = interpolate_1_1(colorA, colorB);
+
+					r += GetResult(colorA, colorB, colorG, colorE);
+					r -= GetResult(colorB, colorA, colorK, colorF);
+					r -= GetResult(colorB, colorA, colorH, colorN);
+					r += GetResult(colorA, colorB, colorL, colorO);
+
+					if (r > 0)
+						product2 = colorA;
+					else if (r < 0)
+						product2 = colorB;
+					else {
+						product2 = interpolate_1_1_1_1(colorA, colorB, colorC, colorD);
+					}
+				}
+			} else {
+				product2 = interpolate_1_1_1_1(colorA, colorB, colorC, colorD);
+
+				if ((colorA == colorC) && (colorA == colorF)
+						&& (colorB != colorE) && (colorB == colorJ)) {
+					product = colorA;
+				} else if ((colorB == colorE) && (colorB == colorD)
+									 && (colorA != colorF) && (colorA == colorI)) {
+					product = colorB;
+				} else {
+					product = interpolate_1_1(colorA, colorB);
+				}
+
+				if ((colorA == colorB) && (colorA == colorH)
+						&& (colorG != colorC) && (colorC == colorM)) {
+					product1 = colorA;
+				} else if ((colorC == colorG) && (colorC == colorD)
+									 && (colorA != colorH) && (colorA == colorI)) {
+					product1 = colorC;
+				} else {
+					product1 = interpolate_1_1(colorA, colorC);
+				}
+			}
+
+			*(dP + 0) = (uint16) colorA;
+			*(dP + 1) = (uint16) product;
+			*(dP + dstPitch/2 + 0) = (uint16) product1;
+			*(dP + dstPitch/2 + 1) = (uint16) product2;
+
+			bP += 1;
+			dP += 2;
+		}
+
+		srcPtr += srcPitch;
+		dstPtr += dstPitch * 2;
+	}
+}
+
+// SAI
 
 SAIPlugin::SAIPlugin() {
 	_factor = 2;
@@ -32,12 +405,14 @@ void SAIPlugin::initialize(Graphics::PixelFormat format) {
 }
 
 void SAIPlugin::deinitialize() {
-	//DestroyScalers();
 }
 
 void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	_2xSaI(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.gLoss == 2)
+		_2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	else
+		_2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 }
 
 uint SAIPlugin::increaseFactor() {
@@ -57,3 +432,83 @@ const char *SAIPlugin::getPrettyName() const {
 }
 
 REGISTER_PLUGIN_STATIC(SAI, PLUGIN_TYPE_SCALER, SAIPlugin);
+
+// SuperSAI
+
+SuperSAIPlugin::SuperSAIPlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+}
+
+void SuperSAIPlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void SuperSAIPlugin::deinitialize() {
+}
+
+void SuperSAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.gLoss == 2)
+		Super2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	else
+		Super2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
+
+uint SuperSAIPlugin::increaseFactor() {
+	return _factor;
+}
+
+uint SuperSAIPlugin::decreaseFactor() {
+	return _factor;
+}
+
+const char *SuperSAIPlugin::getName() const {
+	return "supersai";
+}
+
+const char *SuperSAIPlugin::getPrettyName() const {
+	return "SuperSAI";
+}
+
+REGISTER_PLUGIN_STATIC(SUPERSAI, PLUGIN_TYPE_SCALER, SuperSAIPlugin);
+
+// SuperEagle
+
+SuperEaglePlugin::SuperEaglePlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+}
+
+void SuperEaglePlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void SuperEaglePlugin::deinitialize() {
+}
+
+void SuperEaglePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.gLoss == 2)
+		SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	else
+		SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
+
+uint SuperEaglePlugin::increaseFactor() {
+	return _factor;
+}
+
+uint SuperEaglePlugin::decreaseFactor() {
+	return _factor;
+}
+
+const char *SuperEaglePlugin::getName() const {
+	return "supereagle";
+}
+
+const char *SuperEaglePlugin::getPrettyName() const {
+	return "SuperEagle";
+}
+
+REGISTER_PLUGIN_STATIC(SUPEREAGLE, PLUGIN_TYPE_SCALER, SuperEaglePlugin);
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index 8bcf9d0ff9..bcfe870c5f 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -42,5 +42,40 @@ private:
 	Graphics::PixelFormat _format;
 };
 
+class SuperSAIPlugin : public ScalerPluginObject {
+public:
+	SuperSAIPlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 2; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+private:
+	Graphics::PixelFormat _format;
+};
+
+class SuperEaglePlugin : public ScalerPluginObject {
+public:
+	SuperEaglePlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 2; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+private:
+	Graphics::PixelFormat _format;
+};
 
 #endif


Commit: 20b4356ecd0dad6d1f61357a891157fdf286d16c
    https://github.com/scummvm/scummvm/commit/20b4356ecd0dad6d1f61357a891157fdf286d16c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add AdvMame scaler plugin

Changed paths:
    base/plugins.cpp
    graphics/scaler/scalebit.cpp
    graphics/scaler/scalebit.h


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 1bfc957fa3..b5d11f1a4e 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -167,6 +167,7 @@ public:
 #ifdef USE_HQ_SCALERS
 		LINK_PLUGIN(HQ)
 #endif
+		LINK_PLUGIN(ADVMAME)
 		LINK_PLUGIN(SAI)
 		LINK_PLUGIN(SUPERSAI)
 		LINK_PLUGIN(SUPEREAGLE)
diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp
index 2c79cfcd27..ab31a90df5 100644
--- a/graphics/scaler/scalebit.cpp
+++ b/graphics/scaler/scalebit.cpp
@@ -37,6 +37,7 @@
 
 #include "graphics/scaler/scale2x.h"
 #include "graphics/scaler/scale3x.h"
+#include "graphics/scaler/scalebit.h"
 
 #define DST(bits, num)	(scale2x_uint ## bits *)dst ## num
 #define SRC(bits, num)	(const scale2x_uint ## bits *)src ## num
@@ -348,3 +349,43 @@ void scale(unsigned scale, void* void_dst, unsigned dst_slice, const void* void_
 		break;
 	}
 }
+
+AdvMamePlugin::AdvMamePlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+	_factors.push_back(3);
+}
+
+void AdvMamePlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void AdvMamePlugin::deinitialize() {
+}
+
+void AdvMamePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	::scale(_factor, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, _format.bytesPerPixel, width, height);
+}
+
+uint AdvMamePlugin::increaseFactor() {
+	if (_factor < 3)
+		++_factor;
+	return _factor;
+}
+
+uint AdvMamePlugin::decreaseFactor() {
+	if (_factor > 2)
+		--_factor;
+	return _factor;
+}
+
+const char *AdvMamePlugin::getName() const {
+	return "advmame";
+}
+
+const char *AdvMamePlugin::getPrettyName() const {
+	return "AdvMame";
+}
+
+REGISTER_PLUGIN_STATIC(ADVMAME, PLUGIN_TYPE_SCALER, AdvMamePlugin);
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index 75f9dae455..5c72110e01 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -36,7 +36,25 @@
 #ifndef SCALER_SCALEBIT_H
 #define SCALER_SCALEBIT_H
 
+#include "graphics/scalerplugin.h"
+
 int scale_precondition(unsigned scale, unsigned pixel, unsigned width, unsigned height);
 void scale(unsigned scale, void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height);
 
+class AdvMamePlugin : public ScalerPluginObject {
+public:
+	AdvMamePlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return true; }
+	virtual uint extraPixels() const { return 1; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+};
+
 #endif


Commit: e620b79f15dcb612e2e1c06d0e8b287f8be0cfdf
    https://github.com/scummvm/scummvm/commit/e620b79f15dcb612e2e1c06d0e8b287f8be0cfdf
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: add 4x scaling to AdvMame plugin

Changed paths:
    graphics/scaler/scalebit.cpp
    graphics/scaler/scalebit.h


diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp
index ab31a90df5..45794937b1 100644
--- a/graphics/scaler/scalebit.cpp
+++ b/graphics/scaler/scalebit.cpp
@@ -197,6 +197,8 @@ static void scale4x_buf(void* void_dst, unsigned dst_slice, void* void_mid, unsi
 	mid[4] = mid[3] + mid_slice;
 	mid[5] = mid[4] + mid_slice;
 
+	stage_scale2x(SCMID(0), SCMID(1), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
+	stage_scale2x(SCMID(2), SCMID(3), SCSRC(1), SCSRC(2), SCSRC(3), pixel, width);
 	while (count) {
 		unsigned char* tmp;
 
@@ -354,6 +356,7 @@ AdvMamePlugin::AdvMamePlugin() {
 	_factor = 2;
 	_factors.push_back(2);
 	_factors.push_back(3);
+	_factors.push_back(4);
 }
 
 void AdvMamePlugin::initialize(Graphics::PixelFormat format) {
@@ -365,11 +368,14 @@ void AdvMamePlugin::deinitialize() {
 
 void AdvMamePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	::scale(_factor, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, _format.bytesPerPixel, width, height);
+	if (_factor != 4)
+		::scale(_factor, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, _format.bytesPerPixel, width, height);
+	else
+		::scale(_factor, dstPtr, dstPitch, srcPtr - srcPitch * 2, srcPitch, _format.bytesPerPixel, width, height);
 }
 
 uint AdvMamePlugin::increaseFactor() {
-	if (_factor < 3)
+	if (_factor < 4)
 		++_factor;
 	return _factor;
 }
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index 5c72110e01..46f6dd95c9 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -52,7 +52,7 @@ public:
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return true; }
-	virtual uint extraPixels() const { return 1; }
+	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 };


Commit: 4b29408fb1fbef6649bbb3e64114c48ea745fed5
    https://github.com/scummvm/scummvm/commit/4b29408fb1fbef6649bbb3e64114c48ea745fed5
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Remove references to old scaler data

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 9c32320816..c7e9f59129 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -79,17 +79,6 @@ const OSystem::GraphicsMode s_supportedStretchModes[] = {
 
 DECLARE_TRANSLATION_ADDITIONAL_CONTEXT("Normal (no scaling)", "lowres")
 
-static const int s_gfxModeSwitchTable[][4] = {
-		{ GFX_NORMAL, GFX_DOUBLESIZE, GFX_TRIPLESIZE, -1 },
-		{ GFX_NORMAL, GFX_ADVMAME2X, GFX_ADVMAME3X, -1 },
-		{ GFX_NORMAL, GFX_HQ2X, GFX_HQ3X, -1 },
-		{ GFX_NORMAL, GFX_2XSAI, -1, -1 },
-		{ GFX_NORMAL, GFX_SUPER2XSAI, -1, -1 },
-		{ GFX_NORMAL, GFX_SUPEREAGLE, -1, -1 },
-		{ GFX_NORMAL, GFX_TV2X, -1, -1 },
-		{ GFX_NORMAL, GFX_DOTMATRIX, -1, -1 }
-	};
-
 AspectRatio::AspectRatio(int w, int h) {
 	// TODO : Validation and so on...
 	// Currently, we just ensure the program don't instantiate non-supported aspect ratios
@@ -135,7 +124,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_overlayscreen(0), _tmpscreen2(0),
-	_scalerProc(0), _screenChangeCount(0),
+	_screenChangeCount(0),
 	_mouseData(nullptr), _mouseSurface(nullptr),
 	_mouseOrigSurface(nullptr), _cursorDontScale(false), _cursorPaletteDisabled(true),
 	_currentShakeXOffset(0), _currentShakeYOffset(0),
@@ -164,18 +153,15 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.scaleFactor = 2;
 	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
-	_scalerProc = Normal2x;
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
 	_videoMode.aspectRatioCorrection = false;
-	_scalerProc = Normal1x;
 #endif
 	// HACK: just pick first scaler plugin
 	_normalPlugin = _scalerPlugin = _scalerPlugins.front();
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
-	_scalerType = 0;
 
 	_videoMode.fullscreen = ConfMan.getBool("fullscreen");
 	_videoMode.filtering = ConfMan.getBool("filtering");
@@ -300,11 +286,13 @@ const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsMode
 }
 
 int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
-#ifdef USE_SCALERS
-	return GFX_DOUBLESIZE;
-#else
-	return GFX_NORMAL;
-#endif
+	for (uint i = 0; i < s_supportedGraphicsModes->size(); ++i) {
+		// if normal2x exists, it is the default
+		if (strcmp((*s_supportedGraphicsModes)[i].name, "normal2x") == 0)
+			return (*s_supportedGraphicsModes)[i].id;
+	}
+	// 0 should be the normal1x mode
+	return 0;
 }
 
 void SurfaceSdlGraphicsManager::beginGFXTransaction() {
@@ -778,7 +766,8 @@ void SurfaceSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFo
 	if ((int)w != _videoMode.screenWidth || (int)h != _videoMode.screenHeight) {
 		const bool useDefault = defaultGraphicsModeConfig();
 		if (useDefault && w > 320) {
-			setGraphicsMode(s_gfxModeSwitchTable[_scalerType][0]);
+			// 0 will currently always be Normal1x scaling
+			setGraphicsMode(0);
 		} else {
 			setGraphicsMode(getGraphicsModeIdByName(ConfMan.get("gfx_mode")));
 		}
@@ -1112,7 +1101,6 @@ void SurfaceSdlGraphicsManager::updateScreen() {
 void SurfaceSdlGraphicsManager::internUpdateScreen() {
 	SDL_Surface *srcSurf, *origSurf;
 	int height, width;
-	ScalerProc *scalerProc;
 	int scale1;
 
 	// If there's an active debugger, update it
@@ -1169,14 +1157,12 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		srcSurf = _tmpscreen;
 		width = _videoMode.screenWidth;
 		height = _videoMode.screenHeight;
-		scalerProc = _scalerProc;
 		scale1 = _videoMode.scaleFactor;
 	} else {
 		origSurf = _overlayscreen;
 		srcSurf = _tmpscreen2;
 		width = _videoMode.overlayWidth;
 		height = _videoMode.overlayHeight;
-		scalerProc = Normal1x;
 		scale1 = 1;
 	}
 
@@ -1249,8 +1235,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				if (_videoMode.aspectRatioCorrection && !_overlayVisible)
 					dst_y = real2Aspect(dst_y);
 
-				assert(scalerProc != NULL);
-				//  (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h);
 				if (_overlayVisible) {
 					uint tmpFactor = (*_normalPlugin)->getFactor();
 					(*_normalPlugin)->setFactor(1);
@@ -1730,8 +1714,6 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 
 	SDL_LockSurface(_tmpscreen);
 	SDL_LockSurface(_overlayscreen);
-	//_scalerProc((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
-	//(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight);
 	(*_scalerPlugin)->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 77bea64ed7..691b5f71fd 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -322,8 +322,6 @@ protected:
 	uint8 _originalBitsPerPixel;
 #endif
 
-	ScalerProc *_scalerProc;
-	int _scalerType;
 	int _transactionMode;
 
 	// Indicates whether it is needed to free _hwSurface in destructor


Commit: 044d8aea63af35580779d1e591accd53a3607f9b
    https://github.com/scummvm/scummvm/commit/044d8aea63af35580779d1e591accd53a3607f9b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix signedness of some types

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index c7e9f59129..3dab890777 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -634,6 +634,11 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	int newScaleFactor;
 
+	if (mode >= (int)s_supportedGraphicsModes->size()) {
+		warning("unknown gfx mode %d", mode);
+		return false;
+	}
+
 	const char *name = (*s_supportedGraphicsModesData)[mode].pluginName;
 	newScaleFactor = (*s_supportedGraphicsModesData)[mode].scaleFactor;
 
@@ -1902,8 +1907,8 @@ void SurfaceSdlGraphicsManager::setMouseCursor(const void *buf, uint w, uint h,
 }
 
 void SurfaceSdlGraphicsManager::blitCursor() {
-	const int w = _mouseCurState.w;
-	const int h = _mouseCurState.h;
+	const uint w = _mouseCurState.w;
+	const uint h = _mouseCurState.h;
 
 	if (!w || !h || !_mouseOrigSurface) {
 		return;
@@ -2421,7 +2426,7 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
  * @param factor      The scale factor to match
  * @return            The graphics mode
  */
-int findGraphicsMode(int factor, ScalerPlugin *plugin) {
+int findGraphicsMode(uint factor, ScalerPlugin *plugin) {
 	for (uint i = 0; i < s_supportedGraphicsModesData->size(); ++i) {
 		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugin)->getName()) == 0
 				&& (*s_supportedGraphicsModesData)[i].scaleFactor == factor) {
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 691b5f71fd..69f5cb4a31 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -330,7 +330,7 @@ protected:
 	const ScalerPlugin::List &_scalerPlugins;
 	ScalerPlugin *_scalerPlugin;
 	ScalerPlugin *_normalPlugin;
-	int _scalerIndex;
+	uint _scalerIndex;
 	uint _maxExtraPixels;
 	uint _extraPixels;
 


Commit: dc29ae5447417608d9bceb8a284e6f72551cddc2
    https://github.com/scummvm/scummvm/commit/dc29ae5447417608d9bceb8a284e6f72551cddc2
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Remove old scaler init and destroy code

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 3dab890777..c10b252500 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -979,12 +979,6 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 	if (_tmpscreen2 == NULL)
 		error("allocating _tmpscreen2 failed");
 
-	// Distinguish 555 and 565 mode
-	if (_hwScreen->format->Gmask == 0x3E0)
-		InitScalers(555);
-	else
-		InitScalers(565);
-
 	return true;
 }
 
@@ -1029,7 +1023,6 @@ void SurfaceSdlGraphicsManager::unloadGFXMode() {
 		_osdIconSurface = NULL;
 	}
 #endif
-	DestroyScalers();
 
 #if defined(WIN32) && !SDL_VERSION_ATLEAST(2, 0, 0)
 	// Reset video mode to original.


Commit: 894e8fb18763494817a9baa4013ee0c2acfce2f3
    https://github.com/scummvm/scummvm/commit/894e8fb18763494817a9baa4013ee0c2acfce2f3
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Add comments to explain scaler state changes

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index c10b252500..a740163597 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -642,6 +642,8 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 	const char *name = (*s_supportedGraphicsModesData)[mode].pluginName;
 	newScaleFactor = (*s_supportedGraphicsModesData)[mode].scaleFactor;
 
+	// Find which plugin corresponds to the desired mode and set
+	// _scalerIndex accordingly. _scalerPlugin will be updated later.
 	while (strcmp(name, (*_scalerPlugins[_scalerIndex])->getName()) != 0) {
 		_scalerIndex++;
 		if (_scalerIndex >= _scalerPlugins.size()) {
@@ -667,6 +669,7 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 	if (!_screen || !_hwscreen)
 		return;
 
+	// If the _scalerIndex has changed, change scaler plugins
 	if (_scalerPlugins[_scalerIndex] != _scalerPlugin) {
 		(*_scalerPlugin)->deinitialize();
 		_scalerPlugin = _scalerPlugins[_scalerIndex];
@@ -1234,10 +1237,12 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					dst_y = real2Aspect(dst_y);
 
 				if (_overlayVisible) {
+					// Use the Normal plugin so the overlay is not scaled/filtered
 					uint tmpFactor = (*_normalPlugin)->getFactor();
 					(*_normalPlugin)->setFactor(1);
 					(*_normalPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 						(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+					// Revert state in case the normal plugin is used elsewhere
 					(*_normalPlugin)->setFactor(tmpFactor);
 				} else {
 					(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,


Commit: a15689ae386e5b1118253120e8777de668a5b860
    https://github.com/scummvm/scummvm/commit/a15689ae386e5b1118253120e8777de668a5b860
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix formatting

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index a740163597..f165b4bc11 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2086,10 +2086,10 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	if (_cursorTargetScale == 1) {
 		if ((*_scalerPlugin)->canDrawCursor()) {
 #endif
-		(*_scalerPlugin)->scale(
-			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
-			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-			_mouseCurState.w, _mouseCurState.h, 0, 0);
+            (*_scalerPlugin)->scale(
+                    (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+                    _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+                    _mouseCurState.w, _mouseCurState.h, 0, 0);
 #ifdef USE_SCALERS
 		} else {
 			int tmpFactor = (*_normalPlugin)->getFactor();
@@ -2100,19 +2100,14 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 				_mouseCurState.w, _mouseCurState.h, 0, 0);
 			(*_normalPlugin)->setFactor(tmpFactor);
 		}
-	} else if (_cursorTargetScale == 3 || _videoMode.scaleFactor < 3) {
-			int tmpFactor = (*_normalPlugin)->getFactor();
-			(*_normalPlugin)->setFactor(1);
-			(*_normalPlugin)->scale(
-				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
-				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-				_mouseCurState.w, _mouseCurState.h, 0, 0);
-			(*_normalPlugin)->setFactor(tmpFactor);
 	} else {
-		ScalerPluginObject::scale1o5x(
-			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel ,
-			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-			_mouseCurState.w, _mouseCurState.h, bytesPerPixel);
+        int tmpFactor = (*_normalPlugin)->getFactor();
+        (*_normalPlugin)->setFactor(1);
+        (*_normalPlugin)->scale(
+                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+                _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+                _mouseCurState.w, _mouseCurState.h, 0, 0);
+        (*_normalPlugin)->setFactor(tmpFactor);
 	}
 #endif
 


Commit: 7fd8107c764281b3a446341de7f01d49953299d9
    https://github.com/scummvm/scummvm/commit/7fd8107c764281b3a446341de7f01d49953299d9
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Move Normal scaler functions into same file as plugin

Changed paths:
    graphics/scaler.cpp
    graphics/scaler/normal.cpp


diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
index 5283c3aa25..2c0fec01e9 100644
--- a/graphics/scaler.cpp
+++ b/graphics/scaler.cpp
@@ -59,100 +59,8 @@ void InitScalers(uint32 BitFormat) {
 void DestroyScalers() {
 }
 
-
-/**
- * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
- * source to the destination.
- */
-void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							int width, int height) {
-	// Spot the case when it can all be done in 1 hit
-	if ((srcPitch == sizeof(uint16) * (uint)width) && (dstPitch == sizeof(uint16) * (uint)width)) {
-		memcpy(dstPtr, srcPtr, sizeof(uint16) * width * height);
-		return;
-	}
-	while (height--) {
-		memcpy(dstPtr, srcPtr, sizeof(uint16) * width);
-		srcPtr += srcPitch;
-		dstPtr += dstPitch;
-	}
-}
-
 #ifdef USE_SCALERS
 
-
-#ifdef USE_ARM_SCALER_ASM
-extern "C" void Normal2xARM(const uint8  *srcPtr,
-                                  uint32  srcPitch,
-                                  uint8  *dstPtr,
-                                  uint32  dstPitch,
-                                  int     width,
-                                  int     height);
-
-void Normal2x(const uint8  *srcPtr,
-                    uint32  srcPitch,
-                    uint8  *dstPtr,
-                    uint32  dstPitch,
-                    int     width,
-                    int     height) {
-	Normal2xARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-#else
-/**
- * Trivial nearest-neighbor 2x scaler.
- */
-void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							int width, int height) {
-	uint8 *r;
-
-	assert(IS_ALIGNED(dstPtr, 4));
-	while (height--) {
-		r = dstPtr;
-		for (int i = 0; i < width; ++i, r += 4) {
-			uint32 color = *(((const uint16 *)srcPtr) + i);
-
-			color |= color << 16;
-
-			*(uint32 *)(r) = color;
-			*(uint32 *)(r + dstPitch) = color;
-		}
-		srcPtr += srcPitch;
-		dstPtr += dstPitch << 1;
-	}
-}
-#endif
-
-/**
- * Trivial nearest-neighbor 3x scaler.
- */
-void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							int width, int height) {
-	uint8 *r;
-	const uint32 dstPitch2 = dstPitch * 2;
-	const uint32 dstPitch3 = dstPitch * 3;
-
-	assert(IS_ALIGNED(dstPtr, 2));
-	while (height--) {
-		r = dstPtr;
-		for (int i = 0; i < width; ++i, r += 6) {
-			uint16 color = *(((const uint16 *)srcPtr) + i);
-
-			*(uint16 *)(r + 0) = color;
-			*(uint16 *)(r + 2) = color;
-			*(uint16 *)(r + 4) = color;
-			*(uint16 *)(r + 0 + dstPitch) = color;
-			*(uint16 *)(r + 2 + dstPitch) = color;
-			*(uint16 *)(r + 4 + dstPitch) = color;
-			*(uint16 *)(r + 0 + dstPitch2) = color;
-			*(uint16 *)(r + 2 + dstPitch2) = color;
-			*(uint16 *)(r + 4 + dstPitch2) = color;
-		}
-		srcPtr += srcPitch;
-		dstPtr += dstPitch3;
-	}
-}
-
 /**
  * The Scale2x filter, also known as AdvMame2x.
  * See also http://scale2x.sourceforge.net
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 7f535a7178..63304127bc 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -20,7 +20,6 @@
  */
 
 #include "graphics/scaler/normal.h"
-#include "graphics/scaler.h"
 
 NormalPlugin::NormalPlugin() {
 	_factor = 1;
@@ -35,7 +34,100 @@ NormalPlugin::NormalPlugin() {
 void NormalPlugin::initialize(Graphics::PixelFormat format) {
 }
 
+/**
+ * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
+ * source to the destination.
+ */
+void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	// Spot the case when it can all be done in 1 hit
+	if ((srcPitch == sizeof(OverlayColor) * (uint)width) && (dstPitch == sizeof(OverlayColor) * (uint)width)) {
+		memcpy(dstPtr, srcPtr, sizeof(OverlayColor) * width * height);
+		return;
+	}
+	while (height--) {
+		memcpy(dstPtr, srcPtr, sizeof(OverlayColor) * width);
+		srcPtr += srcPitch;
+		dstPtr += dstPitch;
+	}
+}
+
 #ifdef USE_SCALERS
+
+
+#ifdef USE_ARM_SCALER_ASM
+extern "C" void Normal2xARM(const uint8  *srcPtr,
+                                  uint32  srcPitch,
+                                  uint8  *dstPtr,
+                                  uint32  dstPitch,
+                                  int     width,
+                                  int     height);
+
+void Normal2x(const uint8  *srcPtr,
+                    uint32  srcPitch,
+                    uint8  *dstPtr,
+                    uint32  dstPitch,
+                    int     width,
+                    int     height) {
+	Normal2xARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+}
+
+#else
+/**
+ * Trivial nearest-neighbor 2x scaler.
+ */
+void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	uint8 *r;
+
+	assert(IS_ALIGNED(dstPtr, 4));
+	assert(sizeof(OverlayColor) == 2);
+	while (height--) {
+		r = dstPtr;
+		for (int i = 0; i < width; ++i, r += 4) {
+			uint32 color = *(((const OverlayColor *)srcPtr) + i);
+
+			color |= color << 16;
+
+			*(uint32 *)(r) = color;
+			*(uint32 *)(r + dstPitch) = color;
+		}
+		srcPtr += srcPitch;
+		dstPtr += dstPitch << 1;
+	}
+}
+#endif
+
+/**
+ * Trivial nearest-neighbor 3x scaler.
+ */
+void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	uint8 *r;
+	const uint32 dstPitch2 = dstPitch * 2;
+	const uint32 dstPitch3 = dstPitch * 3;
+
+	assert(IS_ALIGNED(dstPtr, 2));
+	while (height--) {
+		r = dstPtr;
+		for (int i = 0; i < width; ++i, r += 6) {
+			uint16 color = *(((const uint16 *)srcPtr) + i);
+
+			*(uint16 *)(r + 0) = color;
+			*(uint16 *)(r + 2) = color;
+			*(uint16 *)(r + 4) = color;
+			*(uint16 *)(r + 0 + dstPitch) = color;
+			*(uint16 *)(r + 2 + dstPitch) = color;
+			*(uint16 *)(r + 4 + dstPitch) = color;
+			*(uint16 *)(r + 0 + dstPitch2) = color;
+			*(uint16 *)(r + 2 + dstPitch2) = color;
+			*(uint16 *)(r + 4 + dstPitch2) = color;
+		}
+		srcPtr += srcPitch;
+		dstPtr += dstPitch3;
+	}
+}
+
 /**
  * Trivial nearest-neighbor 4x scaler.
  */


Commit: a3a97367547a01225aa0bee183d9db7cfb67116d
    https://github.com/scummvm/scummvm/commit/a3a97367547a01225aa0bee183d9db7cfb67116d
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add TV scaler plugin

Changed paths:
  A graphics/scaler/tv.cpp
  A graphics/scaler/tv.h
    base/plugins.cpp
    graphics/module.mk
    graphics/scaler.cpp


diff --git a/base/plugins.cpp b/base/plugins.cpp
index b5d11f1a4e..ad6f2234c3 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -172,6 +172,7 @@ public:
 		LINK_PLUGIN(SUPERSAI)
 		LINK_PLUGIN(SUPEREAGLE)
 		LINK_PLUGIN(DOTMATRIX)
+		LINK_PLUGIN(TV)
 #endif
 
 		return pl;
diff --git a/graphics/module.mk b/graphics/module.mk
index d8464c5b28..ae104ceda8 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -91,7 +91,8 @@ MODULE_OBJS += \
 	scaler/downscaler.o \
 	scaler/scale2x.o \
 	scaler/scale3x.o \
-	scaler/scalebit.o
+	scaler/scalebit.o \
+	scaler/tv.o
 
 ifdef USE_ARM_SCALER_ASM
 MODULE_OBJS += \
diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
index 2c0fec01e9..6c80150f84 100644
--- a/graphics/scaler.cpp
+++ b/graphics/scaler.cpp
@@ -79,39 +79,6 @@ void AdvMame3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPi
 	scale(3, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, 2, width, height);
 }
 
-template<typename ColorMask>
-void TV2xTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-					int width, int height) {
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
-
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
-
-	while (height--) {
-		for (int i = 0, j = 0; i < width; ++i, j += 2) {
-			uint16 p1 = *(p + i);
-			uint32 pi;
-
-			pi = (((p1 & ColorMask::kRedBlueMask) * 7) >> 3) & ColorMask::kRedBlueMask;
-			pi |= (((p1 & ColorMask::kGreenMask) * 7) >> 3) & ColorMask::kGreenMask;
-
-			*(q + j) = p1;
-			*(q + j + 1) = p1;
-			*(q + j + nextlineDst) = (uint16)pi;
-			*(q + j + nextlineDst + 1) = (uint16)pi;
-		}
-		p += nextlineSrc;
-		q += nextlineDst << 1;
-	}
-}
-
-void TV2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	if (gBitFormat == 565)
-		TV2xTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		TV2xTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
 
 static inline uint16 DOT_16(const uint16 *dotmatrix, uint16 c, int j, int i) {
 	return c - ((c >> 2) & dotmatrix[((j & 3) << 2) + (i & 3)]);
diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
new file mode 100644
index 0000000000..5154410400
--- /dev/null
+++ b/graphics/scaler/tv.cpp
@@ -0,0 +1,87 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/tv.h"
+#include "graphics/scaler.h"
+#include "graphics/colormasks.h"
+
+TVPlugin::TVPlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+}
+
+void TVPlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void TVPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.gLoss == 2)
+		scaleIntern<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	else
+		scaleIntern<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+
+}
+
+uint TVPlugin::increaseFactor() {
+	return _factor;
+}
+
+uint TVPlugin::decreaseFactor() {
+	return _factor;
+}
+
+const char *TVPlugin::getName() const {
+	return "tv";
+}
+
+const char *TVPlugin::getPrettyName() const {
+	return "TV";
+}
+
+template <typename ColorMask>
+void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+					int width, int height) {
+	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
+	const uint16 *p = (const uint16 *)srcPtr;
+
+	const uint32 nextlineDst = dstPitch / sizeof(uint16);
+	uint16 *q = (uint16 *)dstPtr;
+
+	while (height--) {
+		for (int i = 0, j = 0; i < width; ++i, j += 2) {
+			uint16 p1 = *(p + i);
+			uint32 pi;
+
+			pi = (((p1 & ColorMask::kRedBlueMask) * 7) >> 3) & ColorMask::kRedBlueMask;
+			pi |= (((p1 & ColorMask::kGreenMask) * 7) >> 3) & ColorMask::kGreenMask;
+
+			*(q + j) = p1;
+			*(q + j + 1) = p1;
+			*(q + j + nextlineDst) = (uint16)pi;
+			*(q + j + nextlineDst + 1) = (uint16)pi;
+		}
+		p += nextlineSrc;
+		q += nextlineDst << 1;
+	}
+}
+
+REGISTER_PLUGIN_STATIC(TV, PLUGIN_TYPE_SCALER, TVPlugin);
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
new file mode 100644
index 0000000000..625d3ac7ed
--- /dev/null
+++ b/graphics/scaler/tv.h
@@ -0,0 +1,47 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_TV_H
+#define GRAPHICS_SCALER_TV_H
+
+#include "graphics/scalerplugin.h"
+
+class TVPlugin : public ScalerPluginObject {
+public:
+	TVPlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 0; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+private:
+	template <typename ColorMask>
+	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+			uint32 dstPitch, int width, int height);
+};
+
+
+#endif


Commit: 3a42496719d8c802a146236646490d51fa54fbed
    https://github.com/scummvm/scummvm/commit/3a42496719d8c802a146236646490d51fa54fbed
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove 1.5x scaler

Changed paths:
  R graphics/scalerplugin.cpp
    graphics/module.mk
    graphics/scalerplugin.h


diff --git a/graphics/module.mk b/graphics/module.mk
index ae104ceda8..fd7071b635 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -31,7 +31,6 @@ MODULE_OBJS := \
 	primitives.o \
 	renderer.o \
 	scaler.o \
-	scalerplugin.o \
 	scaler/thumbnail_intern.o \
 	screen.o \
 	scaler/normal.o \
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
deleted file mode 100644
index 63b5e4573c..0000000000
--- a/graphics/scalerplugin.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-#include "graphics/scalerplugin.h"
-#include "graphics/scaler.h"
-
-void ScalerPluginObject::scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
-	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
-	Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-#ifdef USE_SCALERS
-void ScalerPluginObject::scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                uint32 dstPitch, int width, int height, int bytesPerPixel) {
-	assert(bytesPerPixel == 2); // TODO add support for 4 bytes
-	Normal1o5x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-#endif
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index efa5d274f9..252c77883c 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -89,20 +89,6 @@ public:
 	 */
 	virtual const char *getPrettyName() const = 0;
 
-	/**
-	 * Usable for any scaler when 1x scaling is desired
-	 */
-	static void scale1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
-
-#ifdef USE_SCALERS
-	/**
-	 * Useful for scaling the mouse
-	 */
-	static void scale1o5x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                   uint32 dstPitch, int width, int height, int bytesPerPixel);
-#endif
-
 protected:
 	uint _factor;
 	Common::Array<uint> _factors;


Commit: 301197b73b05810f2d8ee287da2f4782e2acb25b
    https://github.com/scummvm/scummvm/commit/301197b73b05810f2d8ee287da2f4782e2acb25b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove unused scaler definitions

Changed paths:
    graphics/scaler.cpp
    graphics/scaler.h


diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
index 6c80150f84..fb6eff7c3e 100644
--- a/graphics/scaler.cpp
+++ b/graphics/scaler.cpp
@@ -28,91 +28,5 @@
 
 int gBitFormat = 565;
 
-/** Lookup table for the DotMatrix scaler. */
-uint16 g_dotmatrix[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
-
-/** Init the scaler subsystem. */
-void InitScalers(uint32 BitFormat) {
-	gBitFormat = BitFormat;
-
-	// FIXME: The pixelformat should be param to this function, not the bitformat.
-	// Until then, determine the pixelformat in other ways. Unfortunately,
-	// calling OSystem::getOverlayFormat() here might not be safe on all ports.
-	Graphics::PixelFormat format;
-	if (gBitFormat == 555) {
-		format = Graphics::createPixelFormat<555>();
-	} else if (gBitFormat == 565) {
-		format = Graphics::createPixelFormat<565>();
-	} else {
-		assert(g_system);
-		format = g_system->getOverlayFormat();
-	}
-
-	// Build dotmatrix lookup table for the DotMatrix scaler.
-	g_dotmatrix[0] = g_dotmatrix[10] = format.RGBToColor( 0, 63,  0);
-	g_dotmatrix[1] = g_dotmatrix[11] = format.RGBToColor( 0,  0, 63);
-	g_dotmatrix[2] = g_dotmatrix[ 8] = format.RGBToColor(63,  0,  0);
-	g_dotmatrix[4] = g_dotmatrix[ 6] =
-		g_dotmatrix[12] = g_dotmatrix[14] = format.RGBToColor(63, 63, 63);
-}
-
 void DestroyScalers() {
 }
-
-#ifdef USE_SCALERS
-
-/**
- * The Scale2x filter, also known as AdvMame2x.
- * See also http://scale2x.sourceforge.net
- */
-void AdvMame2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							 int width, int height) {
-	scale(2, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, 2, width, height);
-}
-
-/**
- * The Scale3x filter, also known as AdvMame3x.
- * See also http://scale2x.sourceforge.net
- */
-void AdvMame3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							 int width, int height) {
-	scale(3, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, 2, width, height);
-}
-
-
-static inline uint16 DOT_16(const uint16 *dotmatrix, uint16 c, int j, int i) {
-	return c - ((c >> 2) & dotmatrix[((j & 3) << 2) + (i & 3)]);
-}
-
-
-// FIXME: This scaler doesn't quite work. Either it needs to know where on the
-// screen it's drawing, or the dirty rects will have to be adjusted so that
-// access to the dotmatrix array are made in a consistent way. (Doing that in
-// a way that also works together with aspect-ratio correction is left as an
-// exercise for the reader.)
-
-void DotMatrix(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-					int width, int height) {
-
-	const uint16 *dotmatrix = g_dotmatrix;
-
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
-
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
-
-	for (int j = 0, jj = 0; j < height; ++j, jj += 2) {
-		for (int i = 0, ii = 0; i < width; ++i, ii += 2) {
-			uint16 c = *(p + i);
-			*(q + ii) = DOT_16(dotmatrix, c, jj, ii);
-			*(q + ii + 1) = DOT_16(dotmatrix, c, jj, ii + 1);
-			*(q + ii + nextlineDst) = DOT_16(dotmatrix, c, jj + 1, ii);
-			*(q + ii + nextlineDst + 1) = DOT_16(dotmatrix, c, jj + 1, ii + 1);
-		}
-		p += nextlineSrc;
-		q += nextlineDst << 1;
-	}
-}
-
-#endif // #ifdef USE_SCALERS
diff --git a/graphics/scaler.h b/graphics/scaler.h
index 7309403eea..3a878c13cc 100644
--- a/graphics/scaler.h
+++ b/graphics/scaler.h
@@ -36,31 +36,6 @@ typedef void ScalerProc(const uint8 *srcPtr, uint32 srcPitch,
 	extern void x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, \
 					uint32 dstPitch, int width, int height)
 
-
-DECLARE_SCALER(Normal1x);
-
-#ifdef USE_SCALERS
-
-DECLARE_SCALER(Normal2x);
-DECLARE_SCALER(Normal3x);
-
-DECLARE_SCALER(_2xSaI);
-DECLARE_SCALER(Super2xSaI);
-DECLARE_SCALER(SuperEagle);
-
-DECLARE_SCALER(AdvMame2x);
-DECLARE_SCALER(AdvMame3x);
-
-DECLARE_SCALER(TV2x);
-DECLARE_SCALER(DotMatrix);
-
-#ifdef USE_HQ_SCALERS
-DECLARE_SCALER(HQ2x);
-DECLARE_SCALER(HQ3x);
-#endif
-
-#endif // #ifdef USE_SCALERS
-
 // creates a 160x100 thumbnail for 320x200 games
 // and 160x120 thumbnail for 320x240 and 640x480 games
 // only 565 mode


Commit: c2639a9ff66ee122fdfa970c7ce8a6053959e02a
    https://github.com/scummvm/scummvm/commit/c2639a9ff66ee122fdfa970c7ce8a6053959e02a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Fix formatting in template

Changed paths:
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h


diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index 5154410400..538a4f0492 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -57,7 +57,7 @@ const char *TVPlugin::getPrettyName() const {
 	return "TV";
 }
 
-template <typename ColorMask>
+template<typename ColorMask>
 void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height) {
 	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 625d3ac7ed..0dfaedfc0b 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -38,7 +38,7 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
-	template <typename ColorMask>
+	template<typename ColorMask>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
 };


Commit: ff6fe4360ded5eb96659ad01dab74e8b6c9c141f
    https://github.com/scummvm/scummvm/commit/ff6fe4360ded5eb96659ad01dab74e8b6c9c141f
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Change so that the Normal plugin is initialized

The plugin is now initially null so that a change is detected.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f165b4bc11..5f083020da 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -159,7 +159,8 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.aspectRatioCorrection = false;
 #endif
 	// HACK: just pick first scaler plugin
-	_normalPlugin = _scalerPlugin = _scalerPlugins.front();
+	_normalPlugin = _scalerPlugins.front();
+	_scalerPlugin = NULL;
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
 
@@ -671,7 +672,8 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 
 	// If the _scalerIndex has changed, change scaler plugins
 	if (_scalerPlugins[_scalerIndex] != _scalerPlugin) {
-		(*_scalerPlugin)->deinitialize();
+		if (_scalerPlugin)
+			(*_scalerPlugin)->deinitialize();
 		_scalerPlugin = _scalerPlugins[_scalerIndex];
 		Graphics::PixelFormat format;
 		convertSDLPixelFormat(_hwscreen->format, &format);


Commit: 780d5a8689daa9d5f94e50d9fa78af79a5794af4
    https://github.com/scummvm/scummvm/commit/780d5a8689daa9d5f94e50d9fa78af79a5794af4
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 4 byte per pixel support for Normal scalers

Changed paths:
    graphics/scaler/normal.cpp


diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 63304127bc..848275bf3e 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -32,21 +32,24 @@ NormalPlugin::NormalPlugin() {
 }
 
 void NormalPlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
 }
 
 /**
  * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
  * source to the destination.
  */
+template<typename pixel>
 void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	// Spot the case when it can all be done in 1 hit
-	if ((srcPitch == sizeof(OverlayColor) * (uint)width) && (dstPitch == sizeof(OverlayColor) * (uint)width)) {
-		memcpy(dstPtr, srcPtr, sizeof(OverlayColor) * width * height);
+	int BytesPerPixel = sizeof(pixel);
+	if ((srcPitch == BytesPerPixel * (uint)width) && (dstPitch == BytesPerPixel * (uint)width)) {
+		memcpy(dstPtr, srcPtr, BytesPerPixel * width * height);
 		return;
 	}
 	while (height--) {
-		memcpy(dstPtr, srcPtr, sizeof(OverlayColor) * width);
+		memcpy(dstPtr, srcPtr, BytesPerPixel * width);
 		srcPtr += srcPitch;
 		dstPtr += dstPitch;
 	}
@@ -54,6 +57,31 @@ void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 
 #ifdef USE_SCALERS
 
+/**
+ * Trivial nearest-neighbor 2x scaler.
+ */
+template<typename pixel>
+void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	uint8 *r;
+	int b = sizeof(pixel);
+
+	assert(IS_ALIGNED(dstPtr, 2));
+	while (height--) {
+		r = dstPtr;
+		for (int i = 0; i < width; ++i, r += b * 2) {
+			pixel color = *(((const pixel*)srcPtr) + i);
+
+			*(pixel *)(r) = color;
+			*(pixel *)(r + b) = color;
+			*(pixel *)(r + dstPitch) = color;
+			*(pixel *)(r + b + dstPitch) = color;
+		}
+		srcPtr += srcPitch;
+		dstPtr += dstPitch << 1;
+	}
+}
+
 
 #ifdef USE_ARM_SCALER_ASM
 extern "C" void Normal2xARM(const uint8  *srcPtr,
@@ -63,29 +91,20 @@ extern "C" void Normal2xARM(const uint8  *srcPtr,
                                   int     width,
                                   int     height);
 
-void Normal2x(const uint8  *srcPtr,
-                    uint32  srcPitch,
-                    uint8  *dstPtr,
-                    uint32  dstPitch,
-                    int     width,
-                    int     height) {
-	Normal2xARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
 #else
 /**
- * Trivial nearest-neighbor 2x scaler.
+ * Template Specialization that writes 2 pixels at a time.
  */
-void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+template<>
+void Normal2x<uint16>(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
 
 	assert(IS_ALIGNED(dstPtr, 4));
-	assert(sizeof(OverlayColor) == 2);
 	while (height--) {
 		r = dstPtr;
 		for (int i = 0; i < width; ++i, r += 4) {
-			uint32 color = *(((const OverlayColor *)srcPtr) + i);
+			uint32 color = *(((const uint16*)srcPtr) + i);
 
 			color |= color << 16;
 
@@ -101,27 +120,29 @@ void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 /**
  * Trivial nearest-neighbor 3x scaler.
  */
+template<typename pixel>
 void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
 	const uint32 dstPitch2 = dstPitch * 2;
 	const uint32 dstPitch3 = dstPitch * 3;
+	int b = sizeof(pixel);
 
 	assert(IS_ALIGNED(dstPtr, 2));
 	while (height--) {
 		r = dstPtr;
-		for (int i = 0; i < width; ++i, r += 6) {
-			uint16 color = *(((const uint16 *)srcPtr) + i);
-
-			*(uint16 *)(r + 0) = color;
-			*(uint16 *)(r + 2) = color;
-			*(uint16 *)(r + 4) = color;
-			*(uint16 *)(r + 0 + dstPitch) = color;
-			*(uint16 *)(r + 2 + dstPitch) = color;
-			*(uint16 *)(r + 4 + dstPitch) = color;
-			*(uint16 *)(r + 0 + dstPitch2) = color;
-			*(uint16 *)(r + 2 + dstPitch2) = color;
-			*(uint16 *)(r + 4 + dstPitch2) = color;
+		for (int i = 0; i < width; ++i, r += b * 3) {
+			pixel color = *(((const pixel *)srcPtr) + i);
+
+			*(pixel *)(r + b * 0) = color;
+			*(pixel *)(r + b * 1) = color;
+			*(pixel *)(r + b * 2) = color;
+			*(pixel *)(r + b * 0 + dstPitch) = color;
+			*(pixel *)(r + b * 1 + dstPitch) = color;
+			*(pixel *)(r + b * 2 + dstPitch) = color;
+			*(pixel *)(r + b * 0 + dstPitch2) = color;
+			*(pixel *)(r + b * 1 + dstPitch2) = color;
+			*(pixel *)(r + b * 2 + dstPitch2) = color;
 		}
 		srcPtr += srcPitch;
 		dstPtr += dstPitch3;
@@ -131,35 +152,37 @@ void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 /**
  * Trivial nearest-neighbor 4x scaler.
  */
+template<typename pixel>
 void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
 	const uint32 dstPitch2 = dstPitch * 2;
 	const uint32 dstPitch3 = dstPitch * 3;
 	const uint32 dstPitch4 = dstPitch * 4;
+	int b = sizeof(pixel);
 
 	assert(IS_ALIGNED(dstPtr, 2));
 	while (height--) {
 		r = dstPtr;
-		for (int i = 0; i < width; ++i, r += 8) {
-			uint16 color = *(((const uint16 *)srcPtr) + i);
-
-			*(uint16 *)(r + 0) = color;
-			*(uint16 *)(r + 2) = color;
-			*(uint16 *)(r + 4) = color;
-			*(uint16 *)(r + 6) = color;
-			*(uint16 *)(r + 0 + dstPitch) = color;
-			*(uint16 *)(r + 2 + dstPitch) = color;
-			*(uint16 *)(r + 4 + dstPitch) = color;
-			*(uint16 *)(r + 6 + dstPitch) = color;
-			*(uint16 *)(r + 0 + dstPitch2) = color;
-			*(uint16 *)(r + 2 + dstPitch2) = color;
-			*(uint16 *)(r + 4 + dstPitch2) = color;
-			*(uint16 *)(r + 6 + dstPitch2) = color;
-			*(uint16 *)(r + 0 + dstPitch3) = color;
-			*(uint16 *)(r + 2 + dstPitch3) = color;
-			*(uint16 *)(r + 4 + dstPitch3) = color;
-			*(uint16 *)(r + 6 + dstPitch3) = color;
+		for (int i = 0; i < width; ++i, r += b * 4) {
+			pixel color = *(((const pixel *)srcPtr) + i);
+
+			*(pixel *)(r + b * 0) = color;
+			*(pixel *)(r + b * 1) = color;
+			*(pixel *)(r + b * 2) = color;
+			*(pixel *)(r + b * 3) = color;
+			*(pixel *)(r + b * 0 + dstPitch) = color;
+			*(pixel *)(r + b * 1 + dstPitch) = color;
+			*(pixel *)(r + b * 2 + dstPitch) = color;
+			*(pixel *)(r + b * 3 + dstPitch) = color;
+			*(pixel *)(r + b * 0 + dstPitch2) = color;
+			*(pixel *)(r + b * 1 + dstPitch2) = color;
+			*(pixel *)(r + b * 2 + dstPitch2) = color;
+			*(pixel *)(r + b * 3 + dstPitch2) = color;
+			*(pixel *)(r + b * 0 + dstPitch3) = color;
+			*(pixel *)(r + b * 1 + dstPitch3) = color;
+			*(pixel *)(r + b * 2 + dstPitch3) = color;
+			*(pixel *)(r + b * 3 + dstPitch3) = color;
 		}
 		srcPtr += srcPitch;
 		dstPtr += dstPitch4;
@@ -170,22 +193,44 @@ void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 #ifdef USE_SCALERS
-	switch (_factor) {
-	case 1:
-		Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		break;
-	case 2:
-		Normal2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		break;
-	case 3:
-		Normal3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		break;
-	case 4:
-		Normal4x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-		break;
+	if (_format.bytesPerPixel == 2) {
+		switch (_factor) {
+		case 1:
+			Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 2:
+#ifdef USE_ARM_SCALER_ASM
+			Normal2xARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+#else
+			Normal2x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+#endif
+			break;
+		case 3:
+			Normal3x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 4:
+			Normal4x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		}
+	} else {
+		assert(_format.bytesPerPixel == 4);
+		switch (_factor) {
+		case 1:
+			Normal1x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 2:
+			Normal2x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 3:
+			Normal3x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 4:
+			Normal4x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		}
 	}
 #else
-	Normal1x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 #endif
 }
 


Commit: b08da82253d862411b9b45d4ac8409bc340c73bb
    https://github.com/scummvm/scummvm/commit/b08da82253d862411b9b45d4ac8409bc340c73bb
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 32bpp support for the sai family of scalers

Changed paths:
    graphics/scaler/sai.cpp


diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index cb0f995b49..91898303f1 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -36,20 +36,20 @@ static inline int GetResult(uint32 A, uint32 B, uint32 C, uint32 D) {
 	return (y>>1) - (x>>1);
 }
 
-#define interpolate_1_1		interpolate16_1_1<ColorMask>
-#define interpolate_3_1		interpolate16_3_1<ColorMask>
-#define interpolate_6_1_1	interpolate16_6_1_1<ColorMask>
-#define interpolate_1_1_1_1	interpolate16_1_1_1_1<ColorMask>
+#define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
+#define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
+#define interpolate_6_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_6_1_1<ColorMask>(a,b,c) : interpolate32_6_1_1<ColorMask>(a,b,c))
+#define interpolate_1_1_1_1(a,b,c,d) (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1_1<ColorMask>(a,b,c,d) : interpolate32_1_1_1_1<ColorMask>(a,b,c,d))
 
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
+	const pixel *bP;
+	pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
 
 	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
+		bP = (const pixel *)srcPtr;
+		dP = (pixel *)dstPtr;
 
 		for (int i = 0; i < width; ++i) {
 			unsigned color4, color5, color6;
@@ -134,10 +134,10 @@ void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 			else
 				product1a = color5;
 
-			*(dP + 0) = (uint16) product1a;
-			*(dP + 1) = (uint16) product1b;
-			*(dP + dstPitch/2 + 0) = (uint16) product2a;
-			*(dP + dstPitch/2 + 1) = (uint16) product2b;
+			*(dP + 0) = (pixel) product1a;
+			*(dP + 1) = (pixel) product1b;
+			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product2a;
+			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2b;
 
 			bP += 1;
 			dP += 2;
@@ -148,15 +148,15 @@ void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
+	const pixel *bP;
+	pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
 
 	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
+		bP = (const pixel *)srcPtr;
+		dP = (pixel *)dstPtr;
 		for (int i = 0; i < width; ++i) {
 			unsigned color4, color5, color6;
 			unsigned color1, color2, color3;
@@ -237,10 +237,10 @@ void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 				}
 			}
 
-			*(dP + 0) = (uint16) product1a;
-			*(dP + 1) = (uint16) product1b;
-			*(dP + dstPitch/2 + 0) = (uint16) product2a;
-			*(dP + dstPitch/2 + 1) = (uint16) product2b;
+			*(dP + 0) = (pixel) product1a;
+			*(dP + 1) = (pixel) product1b;
+			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product2a;
+			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2b;
 
 			bP += 1;
 			dP += 2;
@@ -251,24 +251,15 @@ void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-
-void SuperEagle(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
+	const pixel *bP;
+	pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
 
 	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
+		bP = (const pixel *)srcPtr;
+		dP = (pixel *)dstPtr;
 
 		for (int i = 0; i < width; ++i) {
 
@@ -379,10 +370,10 @@ void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32
 				}
 			}
 
-			*(dP + 0) = (uint16) colorA;
-			*(dP + 1) = (uint16) product;
-			*(dP + dstPitch/2 + 0) = (uint16) product1;
-			*(dP + dstPitch/2 + 1) = (uint16) product2;
+			*(dP + 0) = (pixel) colorA;
+			*(dP + 1) = (pixel) product;
+			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product1;
+			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2;
 
 			bP += 1;
 			dP += 2;
@@ -409,10 +400,17 @@ void SAIPlugin::deinitialize() {
 
 void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (_format.gLoss == 2)
-		_2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		_2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.bytesPerPixel == 2) {
+		if (_format.gLoss == 2)
+			_2xSaITemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			_2xSaITemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		if (_format.aLoss == 0)
+			_2xSaITemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			_2xSaITemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 }
 
 uint SAIPlugin::increaseFactor() {
@@ -449,10 +447,17 @@ void SuperSAIPlugin::deinitialize() {
 
 void SuperSAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (_format.gLoss == 2)
-		Super2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		Super2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.bytesPerPixel == 2) {
+		if (_format.gLoss == 2)
+			Super2xSaITemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			Super2xSaITemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		if (_format.aLoss == 0)
+			Super2xSaITemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			Super2xSaITemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 }
 
 uint SuperSAIPlugin::increaseFactor() {
@@ -489,10 +494,17 @@ void SuperEaglePlugin::deinitialize() {
 
 void SuperEaglePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (_format.gLoss == 2)
-		SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.bytesPerPixel == 2) {
+		if (_format.gLoss == 2)
+			SuperEagleTemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			SuperEagleTemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		if (_format.aLoss == 0)
+			SuperEagleTemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			SuperEagleTemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 }
 
 uint SuperEaglePlugin::increaseFactor() {


Commit: 4432c5a4d111c3197cff4af7e4dc6826bddda555
    https://github.com/scummvm/scummvm/commit/4432c5a4d111c3197cff4af7e4dc6826bddda555
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 32bpp color support for HQ scalers

This implementation uses 16bpp rgb conversion to lookup Yuv values. A
different implementation will need to edit ConvertYUV and InitLUT
accordingly.

Changed paths:
    graphics/scaler/hq.cpp


diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 44ed3f4d40..85efe13c4e 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -130,75 +130,104 @@ void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 
 #else
 
+#define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
+#define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
+#define interpolate_7_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_7_1<ColorMask>(a,b) : interpolate32_7_1<ColorMask>(a,b))
+#define interpolate_2_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_2_1_1<ColorMask>(a,b,c) : interpolate32_2_1_1<ColorMask>(a,b,c))
+#define interpolate_2_3_3(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_2_3_3<ColorMask>(a,b,c) : interpolate32_2_3_3<ColorMask>(a,b,c))
+#define interpolate_2_7_7(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_2_7_7<ColorMask>(a,b,c) : interpolate32_2_7_7<ColorMask>(a,b,c))
+#define interpolate_5_2_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_5_2_1<ColorMask>(a,b,c) : interpolate32_5_2_1<ColorMask>(a,b,c))
+#define interpolate_6_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_6_1_1<ColorMask>(a,b,c) : interpolate32_6_1_1<ColorMask>(a,b,c))
+#define interpolate_14_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_14_1_1<ColorMask>(a,b,c) : interpolate32_14_1_1<ColorMask>(a,b,c))
+#define interpolate_1_1_1_1(a,b,c,d) (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1_1<ColorMask>(a,b,c,d) : interpolate32_1_1_1_1<ColorMask>(a,b,c,d))
+
 #define PIXEL00_0	*(q) = w5;
-#define PIXEL00_10	*(q) = interpolate16_3_1<ColorMask >(w5, w1);
-#define PIXEL00_11	*(q) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL00_12	*(q) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL00_20	*(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_21	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w2);
-#define PIXEL00_22	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w4);
-#define PIXEL00_60	*(q) = interpolate16_5_2_1<ColorMask >(w5, w2, w4);
-#define PIXEL00_61	*(q) = interpolate16_5_2_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_70	*(q) = interpolate16_6_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_90	*(q) = interpolate16_2_3_3<ColorMask >(w5, w4, w2);
-#define PIXEL00_100	*(q) = interpolate16_14_1_1<ColorMask >(w5, w4, w2);
+#define PIXEL00_10	*(q) = interpolate_3_1(w5, w1);
+#define PIXEL00_11	*(q) = interpolate_3_1(w5, w4);
+#define PIXEL00_12	*(q) = interpolate_3_1(w5, w2);
+#define PIXEL00_20	*(q) = interpolate_2_1_1(w5, w4, w2);
+#define PIXEL00_21	*(q) = interpolate_2_1_1(w5, w1, w2);
+#define PIXEL00_22	*(q) = interpolate_2_1_1(w5, w1, w4);
+#define PIXEL00_60	*(q) = interpolate_5_2_1(w5, w2, w4);
+#define PIXEL00_61	*(q) = interpolate_5_2_1(w5, w4, w2);
+#define PIXEL00_70	*(q) = interpolate_6_1_1(w5, w4, w2);
+#define PIXEL00_90	*(q) = interpolate_2_3_3(w5, w4, w2);
+#define PIXEL00_100	*(q) = interpolate_14_1_1(w5, w4, w2);
 
 #define PIXEL01_0	*(q+1) = w5;
-#define PIXEL01_10	*(q+1) = interpolate16_3_1<ColorMask >(w5, w3);
-#define PIXEL01_11	*(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL01_12	*(q+1) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL01_20	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_21	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w6);
-#define PIXEL01_22	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w2);
-#define PIXEL01_60	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w6, w2);
-#define PIXEL01_61	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_70	*(q+1) = interpolate16_6_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_90	*(q+1) = interpolate16_2_3_3<ColorMask >(w5, w2, w6);
-#define PIXEL01_100	*(q+1) = interpolate16_14_1_1<ColorMask >(w5, w2, w6);
+#define PIXEL01_10	*(q+1) = interpolate_3_1(w5, w3);
+#define PIXEL01_11	*(q+1) = interpolate_3_1(w5, w2);
+#define PIXEL01_12	*(q+1) = interpolate_3_1(w5, w6);
+#define PIXEL01_20	*(q+1) = interpolate_2_1_1(w5, w2, w6);
+#define PIXEL01_21	*(q+1) = interpolate_2_1_1(w5, w3, w6);
+#define PIXEL01_22	*(q+1) = interpolate_2_1_1(w5, w3, w2);
+#define PIXEL01_60	*(q+1) = interpolate_5_2_1(w5, w6, w2);
+#define PIXEL01_61	*(q+1) = interpolate_5_2_1(w5, w2, w6);
+#define PIXEL01_70	*(q+1) = interpolate_6_1_1(w5, w2, w6);
+#define PIXEL01_90	*(q+1) = interpolate_2_3_3(w5, w2, w6);
+#define PIXEL01_100	*(q+1) = interpolate_14_1_1(w5, w2, w6);
 
 #define PIXEL10_0	*(q+nextlineDst) = w5;
-#define PIXEL10_10	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w7);
-#define PIXEL10_11	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL10_12	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL10_20	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_21	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w4);
-#define PIXEL10_22	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w8);
-#define PIXEL10_60	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w4, w8);
-#define PIXEL10_61	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_70	*(q+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_90	*(q+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w8, w4);
-#define PIXEL10_100	*(q+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w8, w4);
+#define PIXEL10_10	*(q+nextlineDst) = interpolate_3_1(w5, w7);
+#define PIXEL10_11	*(q+nextlineDst) = interpolate_3_1(w5, w8);
+#define PIXEL10_12	*(q+nextlineDst) = interpolate_3_1(w5, w4);
+#define PIXEL10_20	*(q+nextlineDst) = interpolate_2_1_1(w5, w8, w4);
+#define PIXEL10_21	*(q+nextlineDst) = interpolate_2_1_1(w5, w7, w4);
+#define PIXEL10_22	*(q+nextlineDst) = interpolate_2_1_1(w5, w7, w8);
+#define PIXEL10_60	*(q+nextlineDst) = interpolate_5_2_1(w5, w4, w8);
+#define PIXEL10_61	*(q+nextlineDst) = interpolate_5_2_1(w5, w8, w4);
+#define PIXEL10_70	*(q+nextlineDst) = interpolate_6_1_1(w5, w8, w4);
+#define PIXEL10_90	*(q+nextlineDst) = interpolate_2_3_3(w5, w8, w4);
+#define PIXEL10_100	*(q+nextlineDst) = interpolate_14_1_1(w5, w8, w4);
 
 #define PIXEL11_0	*(q+1+nextlineDst) = w5;
-#define PIXEL11_10	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w9);
-#define PIXEL11_11	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL11_12	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL11_20	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_21	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w8);
-#define PIXEL11_22	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w6);
-#define PIXEL11_60	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w6);
-#define PIXEL11_61	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_70	*(q+1+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_90	*(q+1+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w6, w8);
-#define PIXEL11_100	*(q+1+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w6, w8);
+#define PIXEL11_10	*(q+1+nextlineDst) = interpolate_3_1(w5, w9);
+#define PIXEL11_11	*(q+1+nextlineDst) = interpolate_3_1(w5, w6);
+#define PIXEL11_12	*(q+1+nextlineDst) = interpolate_3_1(w5, w8);
+#define PIXEL11_20	*(q+1+nextlineDst) = interpolate_2_1_1(w5, w6, w8);
+#define PIXEL11_21	*(q+1+nextlineDst) = interpolate_2_1_1(w5, w9, w8);
+#define PIXEL11_22	*(q+1+nextlineDst) = interpolate_2_1_1(w5, w9, w6);
+#define PIXEL11_60	*(q+1+nextlineDst) = interpolate_5_2_1(w5, w8, w6);
+#define PIXEL11_61	*(q+1+nextlineDst) = interpolate_5_2_1(w5, w6, w8);
+#define PIXEL11_70	*(q+1+nextlineDst) = interpolate_6_1_1(w5, w6, w8);
+#define PIXEL11_90	*(q+1+nextlineDst) = interpolate_2_3_3(w5, w6, w8);
+#define PIXEL11_100	*(q+1+nextlineDst) = interpolate_14_1_1(w5, w6, w8);
 
 extern "C" uint32   *RGBtoYUV;
-#define YUV(x)	RGBtoYUV[w ## x]
+#define YUV(x)	(sizeof(pixel) == 2 ? RGBtoYUV[w ## x] : ConvertYUV<ColorMask>(w ## x)) 
+
+/**
+ * Convert 32 bit RGB values to Yuv
+ */
+template<typename ColorMask>
+static inline uint32 ConvertYUV(uint32 x) {
+	int r, g, b;
+
+	r = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & x)
+		>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
+	g = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & x)
+		>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
+	b = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & x)
+		>> (ColorMask::kBlueShift + (8 - Graphics::ColorMasks<565>::kBlueBits) - Graphics::ColorMasks<565>::kBlueShift);
+
+	// lookup in 16 bit table
+	return RGBtoYUV[r | g | b];
+}
 
 /*
  * The HQ2x high quality 2x graphics filter.
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq2x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	register int w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const pixel *p = (const pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(pixel);
+	pixel *q = (pixel *)dstPtr;
 
 	//	 +----+----+----+
 	//	 |    |    |    |
@@ -2068,78 +2097,75 @@ void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 
 #else
 
-#define PIXEL00_1M  *(q) = interpolate16_3_1<ColorMask >(w5, w1);
-#define PIXEL00_1U  *(q) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL00_1L  *(q) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL00_2   *(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_4   *(q) = interpolate16_2_7_7<ColorMask >(w5, w4, w2);
-#define PIXEL00_5   *(q) = interpolate16_1_1<ColorMask >(w4, w2);
+#define PIXEL00_1M  *(q) = interpolate_3_1(w5, w1);
+#define PIXEL00_1U  *(q) = interpolate_3_1(w5, w2);
+#define PIXEL00_1L  *(q) = interpolate_3_1(w5, w4);
+#define PIXEL00_2   *(q) = interpolate_2_1_1(w5, w4, w2);
+#define PIXEL00_4   *(q) = interpolate_2_7_7(w5, w4, w2);
+#define PIXEL00_5   *(q) = interpolate_1_1(w4, w2);
 #define PIXEL00_C   *(q) = w5;
 
-#define PIXEL01_1   *(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL01_3   *(q+1) = interpolate16_7_1<ColorMask >(w5, w2);
-#define PIXEL01_6   *(q+1) = interpolate16_3_1<ColorMask >(w2, w5);
+#define PIXEL01_1   *(q+1) = interpolate_3_1(w5, w2);
+#define PIXEL01_3   *(q+1) = interpolate_7_1(w5, w2);
+#define PIXEL01_6   *(q+1) = interpolate_3_1(w2, w5);
 #define PIXEL01_C   *(q+1) = w5;
 
-#define PIXEL02_1M  *(q+2) = interpolate16_3_1<ColorMask >(w5, w3);
-#define PIXEL02_1U  *(q+2) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL02_1R  *(q+2) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL02_2   *(q+2) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL02_4   *(q+2) = interpolate16_2_7_7<ColorMask >(w5, w2, w6);
-#define PIXEL02_5   *(q+2) = interpolate16_1_1<ColorMask >(w2, w6);
+#define PIXEL02_1M  *(q+2) = interpolate_3_1(w5, w3);
+#define PIXEL02_1U  *(q+2) = interpolate_3_1(w5, w2);
+#define PIXEL02_1R  *(q+2) = interpolate_3_1(w5, w6);
+#define PIXEL02_2   *(q+2) = interpolate_2_1_1(w5, w2, w6);
+#define PIXEL02_4   *(q+2) = interpolate_2_7_7(w5, w2, w6);
+#define PIXEL02_5   *(q+2) = interpolate_1_1(w2, w6);
 #define PIXEL02_C   *(q+2) = w5;
 
-#define PIXEL10_1   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL10_3   *(q+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w4);
-#define PIXEL10_6   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w4, w5);
+#define PIXEL10_1   *(q+nextlineDst) = interpolate_3_1(w5, w4);
+#define PIXEL10_3   *(q+nextlineDst) = interpolate_7_1(w5, w4);
+#define PIXEL10_6   *(q+nextlineDst) = interpolate_3_1(w4, w5);
 #define PIXEL10_C   *(q+nextlineDst) = w5;
 
 #define PIXEL11     *(q+1+nextlineDst) = w5;
 
-#define PIXEL12_1   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL12_3   *(q+2+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w6);
-#define PIXEL12_6   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w6, w5);
+#define PIXEL12_1   *(q+2+nextlineDst) = interpolate_3_1(w5, w6);
+#define PIXEL12_3   *(q+2+nextlineDst) = interpolate_7_1(w5, w6);
+#define PIXEL12_6   *(q+2+nextlineDst) = interpolate_3_1(w6, w5);
 #define PIXEL12_C   *(q+2+nextlineDst) = w5;
 
-#define PIXEL20_1M  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w7);
-#define PIXEL20_1D  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL20_1L  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL20_2   *(q+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL20_4   *(q+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w8, w4);
-#define PIXEL20_5   *(q+nextlineDst2) = interpolate16_1_1<ColorMask >(w8, w4);
+#define PIXEL20_1M  *(q+nextlineDst2) = interpolate_3_1(w5, w7);
+#define PIXEL20_1D  *(q+nextlineDst2) = interpolate_3_1(w5, w8);
+#define PIXEL20_1L  *(q+nextlineDst2) = interpolate_3_1(w5, w4);
+#define PIXEL20_2   *(q+nextlineDst2) = interpolate_2_1_1(w5, w8, w4);
+#define PIXEL20_4   *(q+nextlineDst2) = interpolate_2_7_7(w5, w8, w4);
+#define PIXEL20_5   *(q+nextlineDst2) = interpolate_1_1(w8, w4);
 #define PIXEL20_C   *(q+nextlineDst2) = w5;
 
-#define PIXEL21_1   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL21_3   *(q+1+nextlineDst2) = interpolate16_7_1<ColorMask >(w5, w8);
-#define PIXEL21_6   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w8, w5);
+#define PIXEL21_1   *(q+1+nextlineDst2) = interpolate_3_1(w5, w8);
+#define PIXEL21_3   *(q+1+nextlineDst2) = interpolate_7_1(w5, w8);
+#define PIXEL21_6   *(q+1+nextlineDst2) = interpolate_3_1(w8, w5);
 #define PIXEL21_C   *(q+1+nextlineDst2) = w5;
 
-#define PIXEL22_1M  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w9);
-#define PIXEL22_1D  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL22_1R  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL22_2   *(q+2+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL22_4   *(q+2+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w6, w8);
-#define PIXEL22_5   *(q+2+nextlineDst2) = interpolate16_1_1<ColorMask >(w6, w8);
+#define PIXEL22_1M  *(q+2+nextlineDst2) = interpolate_3_1(w5, w9);
+#define PIXEL22_1D  *(q+2+nextlineDst2) = interpolate_3_1(w5, w8);
+#define PIXEL22_1R  *(q+2+nextlineDst2) = interpolate_3_1(w5, w6);
+#define PIXEL22_2   *(q+2+nextlineDst2) = interpolate_2_1_1(w5, w6, w8);
+#define PIXEL22_4   *(q+2+nextlineDst2) = interpolate_2_7_7(w5, w6, w8);
+#define PIXEL22_5   *(q+2+nextlineDst2) = interpolate_1_1(w6, w8);
 #define PIXEL22_C   *(q+2+nextlineDst2) = w5;
 
-extern "C" uint32   *RGBtoYUV;
-#define YUV(x)	RGBtoYUV[w ## x]
-
 /*
  * The HQ3x high quality 3x graphics filter.
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq3x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	register int  w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const pixel *p = (const pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
+	const uint32 nextlineDst = dstPitch / sizeof(pixel);
 	const uint32 nextlineDst2 = 2 * nextlineDst;
-	uint16 *q = (uint16 *)dstPtr;
+	pixel *q = (pixel *)dstPtr;
 
 	//	 +----+----+----+
 	//	 |    |    |    |
@@ -4969,7 +4995,15 @@ HQPlugin::HQPlugin() {
 }
 
 void HQPlugin::initialize(Graphics::PixelFormat format) {
-	InitLUT(format);
+	if (format.bytesPerPixel == 2) {
+		InitLUT(format);
+	} else {
+		// Pass a 16 bit 565 format to InitLut
+		Graphics::PixelFormat format16(2,
+		                               5, 6, 5, 0,
+		                               11, 5, 0, 0);
+		InitLUT(format16);
+	}
 	_format = format;
 }
 
@@ -4980,32 +5014,51 @@ void HQPlugin::deinitialize() {
 
 void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+#ifdef USE_NASM
 	switch (_factor) {
 	case 2:
-#ifdef USE_NASM
 		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-#else
-		if (_format.gLoss == 2)
-			HQ2x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
-					dstPitch, width, height);
-		else
-			HQ2x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
-					dstPitch, width, height);
-#endif
-		break;
 	case 3:
-#ifdef USE_NASM
 		HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 #else
-		if (_format.gLoss == 2)
-			HQ3x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
-					dstPitch, width, height);
-		else
-			HQ3x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
-					dstPitch, width, height);
-#endif
+	switch (_factor) {
+	case 2:
+		if (_format.bytesPerPixel == 2) {
+			if (_format.gLoss == 2)
+				HQ2x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+			else
+				HQ2x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+		} else {
+			if (_format.gLoss == 0)
+				HQ2x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+			else
+				HQ2x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+		}
+		break;
+	case 3:
+		if (_format.bytesPerPixel == 2) {
+			if (_format.gLoss == 2)
+				HQ3x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+			else
+				HQ3x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+		} else {
+			if (_format.gLoss == 0)
+				HQ3x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+			else
+				HQ3x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
+						dstPitch, width, height);
+		}
 		break;
 	}
+#endif
 }
 
 uint HQPlugin::increaseFactor() {


Commit: 3d357dc9d1b009a45cdf7a00b6896536a9e7f957
    https://github.com/scummvm/scummvm/commit/3d357dc9d1b009a45cdf7a00b6896536a9e7f957
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Fix Errors in ifdefs and switches for asm hq scalers

Changed paths:
    graphics/scaler/hq.cpp


diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 85efe13c4e..232db7c616 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -128,7 +128,7 @@ void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 	hq2x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
 }
 
-#else
+#endif
 
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
 #define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
@@ -2075,8 +2075,6 @@ static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dst
 	}
 }
 
-#endif // Assembly version
-
 #ifdef USE_NASM
 // Assembly version of HQ3x
 
@@ -2095,7 +2093,7 @@ void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 	hq3x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
 }
 
-#else
+#endif
 
 #define PIXEL00_1M  *(q) = interpolate_3_1(w5, w1);
 #define PIXEL00_1U  *(q) = interpolate_3_1(w5, w2);
@@ -4986,8 +4984,6 @@ static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dst
 	}
 }
 
-#endif // Assembly version
-
 HQPlugin::HQPlugin() {
 	_factor = 2;
 	_factors.push_back(2);
@@ -5014,51 +5010,54 @@ void HQPlugin::deinitialize() {
 
 void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.bytesPerPixel == 2) {
+		switch (_factor) {
 #ifdef USE_NASM
-	switch (_factor) {
-	case 2:
-		HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	case 3:
-		HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	}
+		case 2:
+			HQ2x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
+		case 3:
+			HQ3x(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			break;
 #else
-	switch (_factor) {
-	case 2:
-		if (_format.bytesPerPixel == 2) {
+		case 2:
 			if (_format.gLoss == 2)
 				HQ2x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
 				HQ2x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
-		} else {
-			if (_format.gLoss == 0)
-				HQ2x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
+			break;
+		case 3:
+			if (_format.gLoss == 2)
+				HQ3x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ2x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
+				HQ3x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
+			break;
+#endif
 		}
-		break;
-	case 3:
-		if (_format.bytesPerPixel == 2) {
-			if (_format.gLoss == 2)
-				HQ3x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
+	} else {
+		switch (_factor) {
+		case 2:
+			if (_format.aLoss == 0)
+				HQ2x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ3x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
+				HQ2x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
-		} else {
-			if (_format.gLoss == 0)
+			break;
+		case 3:
+			if (_format.aLoss == 0)
 				HQ3x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
 				HQ3x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
+			break;
 		}
-		break;
 	}
-#endif
 }
 
 uint HQPlugin::increaseFactor() {


Commit: c7e874e2cd38323ced09e64d881743e88f73ee36
    https://github.com/scummvm/scummvm/commit/c7e874e2cd38323ced09e64d881743e88f73ee36
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 32bpp support to dotmatrix scaler

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index c5eede1280..6aa78274b1 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -28,20 +28,37 @@ DotMatrixPlugin::DotMatrixPlugin() {
 }
 
 void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
-	lookup[0] = lookup[10] = format.RGBToColor(0, 63, 0);
-	lookup[1] = lookup[11] = format.RGBToColor(0, 0, 63);
-	lookup[2] = lookup[8] = format.RGBToColor(63, 0, 0);
-	lookup[4] = lookup[6] =
-		lookup[12] = lookup[14] = format.RGBToColor(63, 63, 63);
-	lookup[5] = lookup[7] =
-		lookup[9] = lookup[13] =
-		lookup[15] = lookup[16] = format.RGBToColor(0, 0, 0);
+	if (format.bytesPerPixel == 2) {
+		uint16 *lookup16 = (uint16 *)lookup;
+		lookup16[0] = lookup16[10] = format.RGBToColor(0, 63, 0);
+		lookup16[1] = lookup16[11] = format.RGBToColor(0, 0, 63);
+		lookup16[2] = lookup16[8] = format.RGBToColor(63, 0, 0);
+		lookup16[4] = lookup16[6] =
+			lookup16[12] = lookup16[14] = format.RGBToColor(63, 63, 63);
+		lookup16[5] = lookup16[7] =
+			lookup16[9] = lookup16[13] =
+			lookup16[15] = lookup16[16] = format.RGBToColor(0, 0, 0);
+	} else {
+		uint32 *lookup32 = (uint32 *)lookup;
+		lookup32[0] = lookup32[10] = format.ARGBToColor(0, 0, 63, 0);
+		lookup32[1] = lookup32[11] = format.ARGBToColor(0, 0, 0, 63);
+		lookup32[2] = lookup32[8] = format.ARGBToColor(0, 63, 0, 0);
+		lookup32[4] = lookup32[6] =
+			lookup32[12] = lookup32[14] = format.ARGBToColor(0, 63, 63, 63);
+		lookup[3] = lookup32[5] = lookup32[7] =
+			lookup32[9] = lookup32[13] =
+			lookup32[15] = lookup32[16] = format.ARGBToColor(0, 0, 0, 0);
+	}
 	_format = format;
 }
 
 void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
+	if (_format.bytesPerPixel == 2) {
+		scaleIntern<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
+	} else {
+		scaleIntern<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
+	}
 }
 
 uint DotMatrixPlugin::increaseFactor() {
@@ -60,31 +77,33 @@ const char *DotMatrixPlugin::getPrettyName() const {
 	return "DotMatrix";
 }
 
-static inline uint16 DOT_16(const uint16 *dotmatrix, uint16 c, int j, int i) {
+template<typename pixel>
+static inline pixel DOT(const pixel *dotmatrix, pixel c, int j, int i) {
 	return c - ((c >> 2) & dotmatrix[((j & 3) << 2) + (i & 3)]);
 }
 
+template<typename pixel>
 void DotMatrixPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height, int x, int y) {
 
-	const uint16 *dotmatrix = lookup;
+	const pixel *dotmatrix = (pixel *)lookup;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const pixel *p = (const pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(pixel);
+	pixel *q = (pixel *)dstPtr;
 
 	int ja = (y * 2) & 3;
 	int ia = (x * 2) & 3;
 
 	for (int j = 0, jj = 0; j < height; ++j, jj += 2) {
 		for (int i = 0, ii = 0; i < width; ++i, ii += 2) {
-			uint16 c = *(p + i);
-			*(q + ii) = DOT_16(dotmatrix, c, jj + ja, ii + ia);
-			*(q + ii + 1) = DOT_16(dotmatrix, c, jj + ja, ii + ia + 1);
-			*(q + ii + nextlineDst) = DOT_16(dotmatrix, c, jj + ja + 1, ii + ia);
-			*(q + ii + nextlineDst + 1) = DOT_16(dotmatrix, c, jj + ja + 1, ii + ia+ 1);
+			pixel c = *(p + i);
+			*(q + ii) = DOT<pixel>(dotmatrix, c, jj + ja, ii + ia);
+			*(q + ii + 1) = DOT<pixel>(dotmatrix, c, jj + ja, ii + ia + 1);
+			*(q + ii + nextlineDst) = DOT<pixel>(dotmatrix, c, jj + ja + 1, ii + ia);
+			*(q + ii + nextlineDst + 1) = DOT<pixel>(dotmatrix, c, jj + ja + 1, ii + ia+ 1);
 		}
 		p += nextlineSrc;
 		q += nextlineDst << 1;
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index c2d1e13ca0..10031b8a3c 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -38,7 +38,9 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
-	uint16 lookup[16];
+	// Allocate enough for 32bpp formats
+	uint32 lookup[16];
+	template<typename pixel>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height, int x, int y);
 };


Commit: 4a69e136d224edc1a47e86cfe01eb6912501aa74
    https://github.com/scummvm/scummvm/commit/4a69e136d224edc1a47e86cfe01eb6912501aa74
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 32bpp support for TV scaler

Changed paths:
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h


diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index 538a4f0492..c3f8081fc1 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -34,10 +34,17 @@ void TVPlugin::initialize(Graphics::PixelFormat format) {
 
 void TVPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
-	if (_format.gLoss == 2)
-		scaleIntern<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		scaleIntern<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.bytesPerPixel == 2) {
+		if (_format.gLoss == 2)
+			scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		if (_format.aLoss == 0)
+			scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 
 }
 
@@ -57,27 +64,32 @@ const char *TVPlugin::getPrettyName() const {
 	return "TV";
 }
 
-template<typename ColorMask>
+template<typename ColorMask, typename pixel>
 void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height) {
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const pixel *p = (const pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(pixel);
+	pixel *q = (pixel *)dstPtr;
 
 	while (height--) {
 		for (int i = 0, j = 0; i < width; ++i, j += 2) {
-			uint16 p1 = *(p + i);
-			uint32 pi;
+			pixel p1 = *(p + i);
+			pixel pi;
 
 			pi = (((p1 & ColorMask::kRedBlueMask) * 7) >> 3) & ColorMask::kRedBlueMask;
 			pi |= (((p1 & ColorMask::kGreenMask) * 7) >> 3) & ColorMask::kGreenMask;
+			pi |= p1 & ColorMask::kAlphaMask;
+
+			uint8 r, g, b;
+			_format.colorToRGB(p1,r,g,b);
+			pi = _format.RGBToColor((r*7)/8, (g*7) / 8, (b*7) / 8);
 
 			*(q + j) = p1;
 			*(q + j + 1) = p1;
-			*(q + j + nextlineDst) = (uint16)pi;
-			*(q + j + nextlineDst + 1) = (uint16)pi;
+			*(q + j + nextlineDst) = pi;
+			*(q + j + nextlineDst + 1) = pi;
 		}
 		p += nextlineSrc;
 		q += nextlineDst << 1;
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 0dfaedfc0b..8aedc6860a 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -38,7 +38,7 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
-	template<typename ColorMask>
+	template<typename ColorMask, typename pixel>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
 };


Commit: d91e970f4e02b3531c0fe725d3c11e6b8d48ba19
    https://github.com/scummvm/scummvm/commit/d91e970f4e02b3531c0fe725d3c11e6b8d48ba19
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Fix error in dotmatrix scaler initialization

Changed paths:
    graphics/scaler/dotmatrix.cpp


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 6aa78274b1..712f25726a 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -35,7 +35,7 @@ void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
 		lookup16[2] = lookup16[8] = format.RGBToColor(63, 0, 0);
 		lookup16[4] = lookup16[6] =
 			lookup16[12] = lookup16[14] = format.RGBToColor(63, 63, 63);
-		lookup16[5] = lookup16[7] =
+		lookup16[3] = lookup16[5] = lookup16[7] =
 			lookup16[9] = lookup16[13] =
 			lookup16[15] = lookup16[16] = format.RGBToColor(0, 0, 0);
 	} else {
@@ -45,7 +45,7 @@ void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
 		lookup32[2] = lookup32[8] = format.ARGBToColor(0, 63, 0, 0);
 		lookup32[4] = lookup32[6] =
 			lookup32[12] = lookup32[14] = format.ARGBToColor(0, 63, 63, 63);
-		lookup[3] = lookup32[5] = lookup32[7] =
+		lookup32[3] = lookup32[5] = lookup32[7] =
 			lookup32[9] = lookup32[13] =
 			lookup32[15] = lookup32[16] = format.ARGBToColor(0, 0, 0, 0);
 	}


Commit: 267b385890dae95d78ae444160aaa489fdb7b230
    https://github.com/scummvm/scummvm/commit/267b385890dae95d78ae444160aaa489fdb7b230
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add 2xPM scaler with 16 and 32 bit support

Changed paths:
  A graphics/scaler/pm.cpp
  A graphics/scaler/pm.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index ad6f2234c3..6b01fc3c26 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -171,6 +171,7 @@ public:
 		LINK_PLUGIN(SAI)
 		LINK_PLUGIN(SUPERSAI)
 		LINK_PLUGIN(SUPEREAGLE)
+		LINK_PLUGIN(PM)
 		LINK_PLUGIN(DOTMATRIX)
 		LINK_PLUGIN(TV)
 #endif
diff --git a/graphics/module.mk b/graphics/module.mk
index fd7071b635..fe70e5c7c6 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -86,6 +86,7 @@ ifdef USE_SCALERS
 MODULE_OBJS += \
 	scaler/dotmatrix.o \
 	scaler/sai.o \
+	scaler/pm.o \
 	scaler/aspect.o \
 	scaler/downscaler.o \
 	scaler/scale2x.o \
diff --git a/graphics/scaler/pm.cpp b/graphics/scaler/pm.cpp
new file mode 100644
index 0000000000..10446afbcb
--- /dev/null
+++ b/graphics/scaler/pm.cpp
@@ -0,0 +1,231 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scaler/pm.h"
+#include "graphics/scaler/intern.h"
+
+//
+// Code taken from Pablo Medina (aka "pm") (pjmedina3 at yahoo.com)
+// You can find it here: http://2xpm.freeservers.com
+// Thanks for his great work
+// Implemented and fixed for ScummVM by Johannes Schickel (aka "LordHoto") (lordhoto [at] gmail [dot] com)
+//
+
+#define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
+#define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
+
+template<typename ColorMask, typename Pixel>
+void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	uint32 nextLineSrc = srcPitch / sizeof(Pixel);
+	uint32 nextLine = dstPitch / sizeof(Pixel);
+	uint32 completeLineSrc = nextLineSrc - width;
+	uint32 completeLine = nextLine - (2*width);
+
+	const Pixel *startAddr2 = (const Pixel*)srcPtr;
+	const Pixel *startAddr1 = startAddr2 - nextLineSrc;
+	const Pixel *startAddr3 = startAddr2 + nextLineSrc;
+
+	Pixel *dstPixel = (Pixel*)dstPtr;
+	int y = height;
+
+	Pixel E[4] = { 0, 0, 0, 0 };
+
+	while (y--) {
+		int x = width;
+
+		while (x--) {
+			//
+			// PA PB PC
+			// PD PE PF
+			// PG PH PI
+			//
+			Pixel PB = startAddr1[0];
+			Pixel PE = startAddr2[0];
+			Pixel PH = startAddr3[0];
+
+			Pixel PA = startAddr1[-1];
+			Pixel PD = startAddr2[-1];
+			Pixel PG = startAddr3[-1];
+
+			Pixel PC = startAddr1[1];
+			Pixel PF = startAddr2[1];
+			Pixel PI = startAddr3[1];
+
+			bool doNotReblit = false;
+			E[0] = E[1] = E[2] = E[3] = PE;
+
+			if (!doNotReblit) {
+				if (PD != PF) {
+					if ((PE != PD) && (PD == PH) && (PD == PI) && (PE != PG)
+						&& ((PD != PG) || (PE != PF) || (PA != PD))
+						&& (!((PD == PA) && (PD == PG) && (PE == PB) && (PE == PF)))) {
+						E[2] = PH;
+						E[3] = interpolate_1_1(E[3], PH);
+						doNotReblit = true;
+					} else if ((PE != PF) && (PF == PH) && (PF == PG) && (PE != PI)
+						&& ((PF != PI) || (PE != PD) || (PC != PF))
+						&& (!((PF == PC) && (PF == PI) && (PE == PB) && (PE == PD)))) {
+						E[2] = interpolate_1_1(E[2], PH);
+						E[3] = PH;
+						doNotReblit = true;
+					}
+				}
+
+				if (PB != PH) {
+					if (PE != PB) {
+						if ((PA != PB) || (PB != PC) || (PE != PH)) {
+							if ((PB == PD) && (PB == PG) && (PE != PA)
+								&& (!((PD == PA) && (PD == PC) && (PE == PH) && (PE == PF)))) {
+								E[0] = interpolate_3_1( PB,E[0]);
+								E[2] = interpolate_3_1(E[2], PB);
+								doNotReblit = true;
+							} else if ((PB == PF) && (PB == PI) && (PE != PC)
+								&& (!((PF == PC) && (PF == PA) && (PE == PH) && (PE == PD)))) {
+								E[1] = interpolate_3_1(PB, E[1]);
+								E[3] = interpolate_3_1(E[3], PB);
+								doNotReblit = true;
+							}
+						}
+					}
+
+					if (PE != PH) {
+						if ((PG != PH) || (PE != PB) || (PH != PI)) {
+							if ((PH == PD) && (PH == PA) && (PE != PG)
+								&& (!((PD == PG) && (PD == PI) && (PE == PB) && (PE == PF)))) {
+								E[2] = interpolate_3_1( PH,E[2]);
+								E[0] = interpolate_3_1(E[0], PH);
+								doNotReblit = true;
+							} else if ((PH == PF) && (PH == PC) && (PE != PI)
+								&& (!((PF == PI) && (PF == PG) && (PE == PB) && (PE == PD)))) {
+								E[3] = interpolate_3_1( PH,E[3]);
+								E[1] = interpolate_3_1(E[1], PH);
+								doNotReblit = true;
+							}
+						}
+					}
+				}
+			}
+
+			if (!doNotReblit) {
+				if ((PB != PH) && (PD != PF)) {
+
+					if ((PB == PD) && (PE != PD)
+						&& (!((PE == PA) && (PB == PC) && (PE == PF))) // Block
+						&& (!((PB == PA) && (PB == PG)))
+						&& (!((PD == PA) && (PD == PC) && (PE == PF) && (PG != PD) && (PG != PE))))
+						E[0] = interpolate_1_1(E[0], PB);
+
+					if ((PB == PF) && (PE != PF)
+						&& (!((PE == PC) && (PB == PA) && (PE == PD))) // Block
+						&& (!((PB == PC) && (PB == PI)))
+						&& (!((PF == PA) && (PF == PC) && (PE == PD) && (PI != PF) && (PI != PE))))
+						E[1] = interpolate_1_1(E[1], PB);
+
+					if ((PH == PD) && ((PE != PG) || (PE != PD))
+						&& (!((PE == PG) && (PH == PI) && (PE == PF))) // Block
+						&& (!((PH == PG) && (PH == PA)))
+						&& (!((PD == PG) && (PD == PI) && (PE == PF) && (PA != PD) && (PA != PE))))
+						E[2] = interpolate_1_1(E[2], PH);
+
+					if ((PH == PF) && ((PE != PI) || (PE != PF))
+						&& (!((PE == PI) && (PH == PG) && (PE == PD))) // Block
+						&& (!((PH == PI) && (PH == PC)))
+						&& (!((PF == PG) && (PF == PI) && (PE == PD) && (PC != PF) && (PI != PE))))
+						E[3] = interpolate_1_1(E[3], PH);
+
+				} else if ((PD == PB) && (PD == PF) && (PD == PH) && (PD != PE)) {
+					if ((PD == PG) || (PD == PC)) {
+						E[1] = interpolate_1_1(E[1], PD);
+						E[2] = E[1];
+					}
+
+					if ((PD == PA) || (PD == PI)) {
+						E[0] = interpolate_1_1(E[0], PD);
+						E[3] = E[0];
+					}
+				}
+			}
+
+			dstPixel[0] = E[0];
+			dstPixel[1] = E[1];
+			dstPixel[nextLine] = E[2];
+			dstPixel[nextLine + 1] = E[3];
+
+			startAddr1++;
+			startAddr2++;
+			startAddr3++;
+
+			dstPixel += 2;
+		}
+
+		startAddr2 += completeLineSrc;
+		startAddr1 = startAddr2 - nextLineSrc;
+		startAddr3 = startAddr2 + nextLineSrc;
+		dstPixel += completeLine + nextLine;
+	}
+
+}
+
+
+PMPlugin::PMPlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+}
+
+void PMPlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+}
+
+void PMPlugin::deinitialize() {
+}
+
+void PMPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.bytesPerPixel == 2) {
+		if (_format.gLoss == 2)
+			scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		if (_format.aLoss == 0)
+			scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		else
+			scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
+}
+
+uint PMPlugin::increaseFactor() {
+	return _factor;
+}
+
+uint PMPlugin::decreaseFactor() {
+	return _factor;
+}
+
+const char *PMPlugin::getName() const {
+	return "pm";
+}
+
+const char *PMPlugin::getPrettyName() const {
+	return "PM";
+}
+
+REGISTER_PLUGIN_STATIC(PM, PLUGIN_TYPE_SCALER, PMPlugin);
diff --git a/graphics/scaler/pm.h b/graphics/scaler/pm.h
new file mode 100644
index 0000000000..3357072927
--- /dev/null
+++ b/graphics/scaler/pm.h
@@ -0,0 +1,45 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_PM_H
+#define GRAPHICS_SCALER_PM_H
+
+#include "graphics/scalerplugin.h"
+
+class PMPlugin : public ScalerPluginObject {
+public:
+	PMPlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 1; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+private:
+	Graphics::PixelFormat _format;
+};
+
+#endif


Commit: 9ed7fde7325d5fa88f27383d80418cb10423228a
    https://github.com/scummvm/scummvm/commit/9ed7fde7325d5fa88f27383d80418cb10423228a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Capitalized pixel in template parameters

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h
    graphics/scaler/hq.cpp
    graphics/scaler/normal.cpp
    graphics/scaler/sai.cpp
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index 712f25726a..f5e8103adc 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -77,33 +77,33 @@ const char *DotMatrixPlugin::getPrettyName() const {
 	return "DotMatrix";
 }
 
-template<typename pixel>
-static inline pixel DOT(const pixel *dotmatrix, pixel c, int j, int i) {
+template<typename Pixel>
+static inline Pixel DOT(const Pixel *dotmatrix, Pixel c, int j, int i) {
 	return c - ((c >> 2) & dotmatrix[((j & 3) << 2) + (i & 3)]);
 }
 
-template<typename pixel>
+template<typename Pixel>
 void DotMatrixPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height, int x, int y) {
 
-	const pixel *dotmatrix = (pixel *)lookup;
+	const Pixel *dotmatrix = (Pixel *)lookup;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
-	const pixel *p = (const pixel *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
+	const Pixel *p = (const Pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(pixel);
-	pixel *q = (pixel *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(Pixel);
+	Pixel *q = (Pixel *)dstPtr;
 
 	int ja = (y * 2) & 3;
 	int ia = (x * 2) & 3;
 
 	for (int j = 0, jj = 0; j < height; ++j, jj += 2) {
 		for (int i = 0, ii = 0; i < width; ++i, ii += 2) {
-			pixel c = *(p + i);
-			*(q + ii) = DOT<pixel>(dotmatrix, c, jj + ja, ii + ia);
-			*(q + ii + 1) = DOT<pixel>(dotmatrix, c, jj + ja, ii + ia + 1);
-			*(q + ii + nextlineDst) = DOT<pixel>(dotmatrix, c, jj + ja + 1, ii + ia);
-			*(q + ii + nextlineDst + 1) = DOT<pixel>(dotmatrix, c, jj + ja + 1, ii + ia+ 1);
+			Pixel c = *(p + i);
+			*(q + ii) = DOT<Pixel>(dotmatrix, c, jj + ja, ii + ia);
+			*(q + ii + 1) = DOT<Pixel>(dotmatrix, c, jj + ja, ii + ia + 1);
+			*(q + ii + nextlineDst) = DOT<Pixel>(dotmatrix, c, jj + ja + 1, ii + ia);
+			*(q + ii + nextlineDst + 1) = DOT<Pixel>(dotmatrix, c, jj + ja + 1, ii + ia+ 1);
 		}
 		p += nextlineSrc;
 		q += nextlineDst << 1;
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 10031b8a3c..06d5801e3f 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -40,7 +40,7 @@ public:
 private:
 	// Allocate enough for 32bpp formats
 	uint32 lookup[16];
-	template<typename pixel>
+	template<typename Pixel>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height, int x, int y);
 };
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 232db7c616..c28dfef981 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -194,7 +194,7 @@ void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 #define PIXEL11_100	*(q+1+nextlineDst) = interpolate_14_1_1(w5, w6, w8);
 
 extern "C" uint32   *RGBtoYUV;
-#define YUV(x)	(sizeof(pixel) == 2 ? RGBtoYUV[w ## x] : ConvertYUV<ColorMask>(w ## x)) 
+#define YUV(x)	(sizeof(Pixel) == 2 ? RGBtoYUV[w ## x] : ConvertYUV<ColorMask>(w ## x)) 
 
 /**
  * Convert 32 bit RGB values to Yuv
@@ -219,15 +219,15 @@ static inline uint32 ConvertYUV(uint32 x) {
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq2x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	register int w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
-	const pixel *p = (const pixel *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
+	const Pixel *p = (const Pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(pixel);
-	pixel *q = (pixel *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(Pixel);
+	Pixel *q = (Pixel *)dstPtr;
 
 	//	 +----+----+----+
 	//	 |    |    |    |
@@ -2154,16 +2154,16 @@ void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq3x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	register int  w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
-	const pixel *p = (const pixel *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
+	const Pixel *p = (const Pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(pixel);
+	const uint32 nextlineDst = dstPitch / sizeof(Pixel);
 	const uint32 nextlineDst2 = 2 * nextlineDst;
-	pixel *q = (pixel *)dstPtr;
+	Pixel *q = (Pixel *)dstPtr;
 
 	//	 +----+----+----+
 	//	 |    |    |    |
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 848275bf3e..cccec22789 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -39,11 +39,11 @@ void NormalPlugin::initialize(Graphics::PixelFormat format) {
  * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
  * source to the destination.
  */
-template<typename pixel>
+template<typename Pixel>
 void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	// Spot the case when it can all be done in 1 hit
-	int BytesPerPixel = sizeof(pixel);
+	int BytesPerPixel = sizeof(Pixel);
 	if ((srcPitch == BytesPerPixel * (uint)width) && (dstPitch == BytesPerPixel * (uint)width)) {
 		memcpy(dstPtr, srcPtr, BytesPerPixel * width * height);
 		return;
@@ -60,22 +60,22 @@ void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 /**
  * Trivial nearest-neighbor 2x scaler.
  */
-template<typename pixel>
+template<typename Pixel>
 void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
-	int b = sizeof(pixel);
+	int b = sizeof(Pixel);
 
 	assert(IS_ALIGNED(dstPtr, 2));
 	while (height--) {
 		r = dstPtr;
 		for (int i = 0; i < width; ++i, r += b * 2) {
-			pixel color = *(((const pixel*)srcPtr) + i);
+			Pixel color = *(((const Pixel*)srcPtr) + i);
 
-			*(pixel *)(r) = color;
-			*(pixel *)(r + b) = color;
-			*(pixel *)(r + dstPitch) = color;
-			*(pixel *)(r + b + dstPitch) = color;
+			*(Pixel *)(r) = color;
+			*(Pixel *)(r + b) = color;
+			*(Pixel *)(r + dstPitch) = color;
+			*(Pixel *)(r + b + dstPitch) = color;
 		}
 		srcPtr += srcPitch;
 		dstPtr += dstPitch << 1;
@@ -120,29 +120,29 @@ void Normal2x<uint16>(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint3
 /**
  * Trivial nearest-neighbor 3x scaler.
  */
-template<typename pixel>
+template<typename Pixel>
 void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
 	const uint32 dstPitch2 = dstPitch * 2;
 	const uint32 dstPitch3 = dstPitch * 3;
-	int b = sizeof(pixel);
+	int b = sizeof(Pixel);
 
 	assert(IS_ALIGNED(dstPtr, 2));
 	while (height--) {
 		r = dstPtr;
 		for (int i = 0; i < width; ++i, r += b * 3) {
-			pixel color = *(((const pixel *)srcPtr) + i);
+			Pixel color = *(((const Pixel *)srcPtr) + i);
 
-			*(pixel *)(r + b * 0) = color;
-			*(pixel *)(r + b * 1) = color;
-			*(pixel *)(r + b * 2) = color;
-			*(pixel *)(r + b * 0 + dstPitch) = color;
-			*(pixel *)(r + b * 1 + dstPitch) = color;
-			*(pixel *)(r + b * 2 + dstPitch) = color;
-			*(pixel *)(r + b * 0 + dstPitch2) = color;
-			*(pixel *)(r + b * 1 + dstPitch2) = color;
-			*(pixel *)(r + b * 2 + dstPitch2) = color;
+			*(Pixel *)(r + b * 0) = color;
+			*(Pixel *)(r + b * 1) = color;
+			*(Pixel *)(r + b * 2) = color;
+			*(Pixel *)(r + b * 0 + dstPitch) = color;
+			*(Pixel *)(r + b * 1 + dstPitch) = color;
+			*(Pixel *)(r + b * 2 + dstPitch) = color;
+			*(Pixel *)(r + b * 0 + dstPitch2) = color;
+			*(Pixel *)(r + b * 1 + dstPitch2) = color;
+			*(Pixel *)(r + b * 2 + dstPitch2) = color;
 		}
 		srcPtr += srcPitch;
 		dstPtr += dstPitch3;
@@ -152,37 +152,37 @@ void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 /**
  * Trivial nearest-neighbor 4x scaler.
  */
-template<typename pixel>
+template<typename Pixel>
 void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 							int width, int height) {
 	uint8 *r;
 	const uint32 dstPitch2 = dstPitch * 2;
 	const uint32 dstPitch3 = dstPitch * 3;
 	const uint32 dstPitch4 = dstPitch * 4;
-	int b = sizeof(pixel);
+	int b = sizeof(Pixel);
 
 	assert(IS_ALIGNED(dstPtr, 2));
 	while (height--) {
 		r = dstPtr;
 		for (int i = 0; i < width; ++i, r += b * 4) {
-			pixel color = *(((const pixel *)srcPtr) + i);
+			Pixel color = *(((const Pixel *)srcPtr) + i);
 
-			*(pixel *)(r + b * 0) = color;
-			*(pixel *)(r + b * 1) = color;
-			*(pixel *)(r + b * 2) = color;
-			*(pixel *)(r + b * 3) = color;
-			*(pixel *)(r + b * 0 + dstPitch) = color;
-			*(pixel *)(r + b * 1 + dstPitch) = color;
-			*(pixel *)(r + b * 2 + dstPitch) = color;
-			*(pixel *)(r + b * 3 + dstPitch) = color;
-			*(pixel *)(r + b * 0 + dstPitch2) = color;
-			*(pixel *)(r + b * 1 + dstPitch2) = color;
-			*(pixel *)(r + b * 2 + dstPitch2) = color;
-			*(pixel *)(r + b * 3 + dstPitch2) = color;
-			*(pixel *)(r + b * 0 + dstPitch3) = color;
-			*(pixel *)(r + b * 1 + dstPitch3) = color;
-			*(pixel *)(r + b * 2 + dstPitch3) = color;
-			*(pixel *)(r + b * 3 + dstPitch3) = color;
+			*(Pixel *)(r + b * 0) = color;
+			*(Pixel *)(r + b * 1) = color;
+			*(Pixel *)(r + b * 2) = color;
+			*(Pixel *)(r + b * 3) = color;
+			*(Pixel *)(r + b * 0 + dstPitch) = color;
+			*(Pixel *)(r + b * 1 + dstPitch) = color;
+			*(Pixel *)(r + b * 2 + dstPitch) = color;
+			*(Pixel *)(r + b * 3 + dstPitch) = color;
+			*(Pixel *)(r + b * 0 + dstPitch2) = color;
+			*(Pixel *)(r + b * 1 + dstPitch2) = color;
+			*(Pixel *)(r + b * 2 + dstPitch2) = color;
+			*(Pixel *)(r + b * 3 + dstPitch2) = color;
+			*(Pixel *)(r + b * 0 + dstPitch3) = color;
+			*(Pixel *)(r + b * 1 + dstPitch3) = color;
+			*(Pixel *)(r + b * 2 + dstPitch3) = color;
+			*(Pixel *)(r + b * 3 + dstPitch3) = color;
 		}
 		srcPtr += srcPitch;
 		dstPtr += dstPitch4;
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 91898303f1..bcbf765050 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -41,15 +41,15 @@ static inline int GetResult(uint32 A, uint32 B, uint32 C, uint32 D) {
 #define interpolate_6_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_6_1_1<ColorMask>(a,b,c) : interpolate32_6_1_1<ColorMask>(a,b,c))
 #define interpolate_1_1_1_1(a,b,c,d) (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1_1<ColorMask>(a,b,c,d) : interpolate32_1_1_1_1<ColorMask>(a,b,c,d))
 
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const pixel *bP;
-	pixel *dP;
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const Pixel *bP;
+	Pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
 
 	while (height--) {
-		bP = (const pixel *)srcPtr;
-		dP = (pixel *)dstPtr;
+		bP = (const Pixel *)srcPtr;
+		dP = (Pixel *)dstPtr;
 
 		for (int i = 0; i < width; ++i) {
 			unsigned color4, color5, color6;
@@ -134,10 +134,10 @@ void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 			else
 				product1a = color5;
 
-			*(dP + 0) = (pixel) product1a;
-			*(dP + 1) = (pixel) product1b;
-			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product2a;
-			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2b;
+			*(dP + 0) = (Pixel) product1a;
+			*(dP + 1) = (Pixel) product1b;
+			*(dP + dstPitch / sizeof(Pixel) + 0) = (Pixel) product2a;
+			*(dP + dstPitch / sizeof(Pixel) + 1) = (Pixel) product2b;
 
 			bP += 1;
 			dP += 2;
@@ -148,15 +148,15 @@ void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const pixel *bP;
-	pixel *dP;
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const Pixel *bP;
+	Pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
 
 	while (height--) {
-		bP = (const pixel *)srcPtr;
-		dP = (pixel *)dstPtr;
+		bP = (const Pixel *)srcPtr;
+		dP = (Pixel *)dstPtr;
 		for (int i = 0; i < width; ++i) {
 			unsigned color4, color5, color6;
 			unsigned color1, color2, color3;
@@ -237,10 +237,10 @@ void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 				}
 			}
 
-			*(dP + 0) = (pixel) product1a;
-			*(dP + 1) = (pixel) product1b;
-			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product2a;
-			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2b;
+			*(dP + 0) = (Pixel) product1a;
+			*(dP + 1) = (Pixel) product1b;
+			*(dP + dstPitch / sizeof(Pixel) + 0) = (Pixel) product2a;
+			*(dP + dstPitch / sizeof(Pixel) + 1) = (Pixel) product2b;
 
 			bP += 1;
 			dP += 2;
@@ -251,15 +251,15 @@ void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const pixel *bP;
-	pixel *dP;
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
+	const Pixel *bP;
+	Pixel *dP;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
 
 	while (height--) {
-		bP = (const pixel *)srcPtr;
-		dP = (pixel *)dstPtr;
+		bP = (const Pixel *)srcPtr;
+		dP = (Pixel *)dstPtr;
 
 		for (int i = 0; i < width; ++i) {
 
@@ -370,10 +370,10 @@ void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32
 				}
 			}
 
-			*(dP + 0) = (pixel) colorA;
-			*(dP + 1) = (pixel) product;
-			*(dP + dstPitch / sizeof(pixel) + 0) = (pixel) product1;
-			*(dP + dstPitch / sizeof(pixel) + 1) = (pixel) product2;
+			*(dP + 0) = (Pixel) colorA;
+			*(dP + 1) = (Pixel) product;
+			*(dP + dstPitch / sizeof(Pixel) + 0) = (Pixel) product1;
+			*(dP + dstPitch / sizeof(Pixel) + 1) = (Pixel) product2;
 
 			bP += 1;
 			dP += 2;
diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index c3f8081fc1..53dfa047b8 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -64,19 +64,19 @@ const char *TVPlugin::getPrettyName() const {
 	return "TV";
 }
 
-template<typename ColorMask, typename pixel>
+template<typename ColorMask, typename Pixel>
 void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height) {
-	const uint32 nextlineSrc = srcPitch / sizeof(pixel);
-	const pixel *p = (const pixel *)srcPtr;
+	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
+	const Pixel *p = (const Pixel *)srcPtr;
 
-	const uint32 nextlineDst = dstPitch / sizeof(pixel);
-	pixel *q = (pixel *)dstPtr;
+	const uint32 nextlineDst = dstPitch / sizeof(Pixel);
+	Pixel *q = (Pixel *)dstPtr;
 
 	while (height--) {
 		for (int i = 0, j = 0; i < width; ++i, j += 2) {
-			pixel p1 = *(p + i);
-			pixel pi;
+			Pixel p1 = *(p + i);
+			Pixel pi;
 
 			pi = (((p1 & ColorMask::kRedBlueMask) * 7) >> 3) & ColorMask::kRedBlueMask;
 			pi |= (((p1 & ColorMask::kGreenMask) * 7) >> 3) & ColorMask::kGreenMask;
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 8aedc6860a..7cabdd7659 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -38,7 +38,7 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
-	template<typename ColorMask, typename pixel>
+	template<typename ColorMask, typename Pixel>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
 };


Commit: 592617e2ceb50a6f15909ccf3579573bc0fcc0a5
    https://github.com/scummvm/scummvm/commit/592617e2ceb50a6f15909ccf3579573bc0fcc0a5
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add Edge2x/3x scaler

This currently does not conform to coding standards and contains a lot
of dead code (This will be fixed). Thanks to Eric Welsh for the original
patch.

Changed paths:
  A graphics/scaler/edge.cpp
  A graphics/scaler/edge.h
    base/plugins.cpp
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 6b01fc3c26..99d672ba20 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -166,6 +166,7 @@ public:
 #ifdef USE_SCALERS
 #ifdef USE_HQ_SCALERS
 		LINK_PLUGIN(HQ)
+		LINK_PLUGIN(EDGE)
 #endif
 		LINK_PLUGIN(ADVMAME)
 		LINK_PLUGIN(SAI)
diff --git a/graphics/module.mk b/graphics/module.mk
index fe70e5c7c6..341c0e0d6f 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -103,6 +103,7 @@ endif
 
 ifdef USE_HQ_SCALERS
 MODULE_OBJS += \
+	scaler/edge.o \
 	scaler/hq.o \
 
 ifdef USE_NASM
diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
new file mode 100644
index 0000000000..7b8e77ef4f
--- /dev/null
+++ b/graphics/scaler/edge.cpp
@@ -0,0 +1,4626 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2001  Ludvig Strigeus
+ * Copyright (C) 2001-2006 The ScummVM project
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ */
+
+/*
+ * Another edge-directed 2x/3x anti-aliasing scaler for ScummVM
+ *
+ * Author: Eric A. Welsh
+ *
+ * INTERPOLATE/Q_INTERPOLATE macros taken from HQ2x/HQ3x scalers
+ *  (Authors: Maxim Stepin and Max Horn)
+ * 
+ *
+ * Sharp, clean, anti-aliased image with very few artifacts.
+ * Detects and appropriately handles mouse overlays with transparent pixels.
+ * The Edge3x filter detects unchanged pixels and does not redraw them,
+ * resulting in a considerable gain in speed when there are even a moderate
+ * number of unchanged pixels.  Edge3x and Edge2x anti-alias using nearest-
+ * neighbor methods.  Edge2xi interpolates.
+ *
+ * The really slow speed is mainly due to the edge detection algorithm.  In
+ * order to accurately detect the edge direction (and thus avoid artifacts
+ * caused by mis-detection), the edge detection and refinement process is
+ * rather long and involved.  Speed must be sacrificed in order to avoid
+ * artifacts :(  If anyone is tempted to optimize using lower precision
+ * math, such as converting some of the double math to fixed-point integer,
+ * or lowering the number of significant bits used in the existing integer
+ * math to squeeze it into accelerated 16-bit vector instructions, please do
+ * not do this.  Any loss in precision results in visibly degraded image
+ * quality.  I've tried integer conversions of various double math, and tried
+ * reducing the number of significant digits I use in the integer math, and
+ * it always results in less accurate edge detection and lower image quality.
+ * If you're going to optimize, make sure you don't sacrifice any precision.
+ * There may be a few places I could change the variable types from
+ * int32 to int16 without introducing overflows, though.  I should also
+ * probably change some of the flags and arrays to bools or chars, rather
+ * than ints, since they are only 0 or 1.
+ *
+ * It's a bit slow... but ScummVM runs most things fine on my 1.53 GHz Athlon.
+ * Increasing the Win32 thread priority can help by forcing Windows not to
+ * twiddle its thumbs and idle as much during heavy CPU load.  The Dig
+ * cutscene when the asteroid is activated is a little jerky, pans/fades in
+ * Amazon Queen and Beneath a Steel Sky are slow.  Faster machines probably
+ * won't have a problem.  I remember a time when my home machine was too slow
+ * to run SNES emulators with 2xSaI filters, so I don't think speed is such a
+ * big issue here.  It won't be too long before the average home computer is
+ * plenty fast enough to run this filter.
+ *
+ */
+
+/*
+ * Notes on handling overlays, mouse, transparencies, etc.:
+ *
+ * As I write this, the SDL backend does not call different filters based on
+ * whether or not the bitmaps contain transparency.  Bitmaps with transparency
+ * need to be treated differently.  1) Interpolation needs to be disabled,
+ * since interpolating with transparent pixels produces ugly smears around the
+ * transparent areas.  2) Transparent pixels need to be treated differently
+ * during edge detection, so that they appear to be as if they were colors
+ * that would give maximal contrast in the current 3x3 window.
+ *
+ * Currently, the SDL backend calls anti-aliasing resize filters to either
+ * resize the game screen or resize the mouse.  The filter stores the src
+ * array bounds whenever the width and height of the area to be resized are
+ * equal to the width and height of the current game screen.  If the current
+ * src array is outside these bounds, then it is assumed that the mouse or
+ * menu overlay is being drawn.  This works perfectly for the current SDL
+ * backend, but it is still a hack.  If, in the future, the filter were to
+ * to be used to resize a transparent overlay with dimensions equal to those
+ * of the current game screen, that overlay would be resized without any
+ * special transparency consideration.  The same goes for a mouse pointer
+ * that is equal to the size of the screen, but I don't forsee this ever
+ * being a problem....  The correct solution would be to rewrite the backends
+ * to call filters differently depending on whether or not the bitmap contains
+ * transparent pixels, and whether or not the end result should be
+ * interpolated or resized using nearest-neighbor.  Until then, the array
+ * bounds checking hack will have to do.
+ *
+ */
+
+#include <math.h>
+#include "common/scummsys.h"
+#include "common/system.h"
+#include "graphics/scaler/edge.h"
+
+/* Randomly XORs one of 2x2 or 3x3 resized pixels in order to indicate
+ * which pixels have been redrawn.  Useful for seeing which areas of
+ * the screen are being redrawn.  Good for seeing dirty rects, full screen
+ * refreshes, etc..  Also good for seeing if the unchanged pixel detection is
+ * working correctly or not :)
+ */
+#define DEBUG_REFRESH_RANDOM_XOR		0	/* debug redraws */
+
+/* Use with DEBUG_REFRESH_RANDOM_XOR.  Randomize the borders of the drawing
+ * area, whether they are unchanged or not.  Useful for visualizing the
+ * borders of the drawing area.  You might be surprised at which areas of the
+ * screen get redraw requests, even areas with absolutely nothing moving,
+ * or color cycling, or anything that would cause a dirty rect or require a
+ * redraw....
+ */
+#define DEBUG_DRAW_REFRESH_BORDERS		0	/* more redraw debug */
+
+#define INCREASE_WIN32_PRIORITY			0	/* 1 for slow CPUs */
+#define PARANOID_KNIGHTS			1	/* avoid artifacts */
+#define PARANOID_ARROWS				1	/* avoid artifacts */
+#define HANDLE_TRANSPARENT_OVERLAYS		1	/* as it says */
+
+#define SIN45 0.7071067811865		/* sin of 45 degrees */
+#define GREY_SHIFT 12			/* bit shift for greyscale precision */
+#define RGB_SHIFT 13			/* bit shift for RGB precision */
+
+const int16 one_sqrt2 = (int16) (((int16)1<<GREY_SHIFT) / sqrt(2.0) + 0.5);
+int16 rgb_table[65536][3] = {0};	/* table lookup for RGB */
+int16 greyscale_table[3][65536] = {0};	/* greyscale tables */
+int16 *chosen_greyscale;		/* pointer to chosen greyscale table */
+int16 *bptr_global;			/* too awkward to pass variables */
+int8 sim_sum;				/* sum of similarity matrix */
+
+uint16 div3[189];			/* tables for pixel interpolation */
+uint16 div9[567];
+
+int32 max_old_src_size = 0;		/* maximum observed rectangle size */
+int32 max_overlay_size = 0;		/* maximum observed overlay size */
+int32 max_old_dst_size = 0;
+int32 max_dst_overlay_size = 0;
+uint16 *old_src = NULL;			/* old src array */
+uint16 *old_overlay = NULL;		/* holds the old overlay */
+uint16 *old_dst = NULL;			/* old dst array */
+uint16 *old_dst_overlay = NULL;		/* old dst overlay array */
+
+int cur_screen_width = 0;		/* current game screen w and h */
+int cur_screen_height = 0;
+int old_screen_width = 0;		/* previous game screen w and h */
+int old_screen_height = 0;
+int cur_dst_screen_width = 0;		/* scaled dst w and h */
+int cur_dst_screen_height = 0;
+int old_dst_screen_width = 0;
+int old_dst_screen_height = 0;
+int cur_overlay_width = 0;
+int cur_overlay_height = 0;
+int old_overlay_width = 0;
+int old_overlay_height = 0;
+int cur_dst_overlay_width = 0;
+int cur_dst_overlay_height = 0;
+int old_dst_overlay_width = 0;
+int old_dst_overlay_height = 0;
+int max_scale = -1;			/* used for dst buffer arrays */
+
+int init_flag = 0;			/* have the tables been initialized? */
+const uint16 *src_addr_min = NULL;	/* start of src screen array */
+const uint16 *src_addr_max = NULL;	/* end of src screen array */
+
+const int16 int32_sqrt3 = (int16) (((int16)1<<GREY_SHIFT) * sqrt(3.0) + 0.5);
+
+
+
+#if DEBUG_REFRESH_RANDOM_XOR
+/* Random number generators with very good randomness properties, far better
+ * than the simple linear congruential generator in the ScummVM utils.
+ * The RNG algorithms were developed by George Marsaglia, as published in his
+ * "Xorshift RNGs" paper and various USENET postings.
+ *
+ * Marsaglia, George, 2003, "Xorshift RNGs", Journal of Statistical Software,
+ * Volume 7, Issue 14
+ *
+ * Note the comments in the code below about how many of the triplet and shift
+ * combinations published in his paper and various USENET postings don't quite
+ * work as expected (yes, I exhaustively tested all 648 of them with his test
+ * suite).  Only a few "magic" combinations actually produce good random
+ * numbers.  I suspect this is due to the numbers being shifted too far off
+ * the ends of the registers?
+ *
+ * While numbers this highly random are overkill for our purposes, I already
+ * had this code written for various scientific analysis programs, and I've
+ * tested the generators with Marsaglia's comprehensive randomness test
+ * suite, so I know that they have very good randomness.  The simple single
+ * seed algorithm does fail a few minor tests, but it is still LOADS better
+ * than a linear congruential generator.  The 4 seed RNG passes all tests
+ * with flying colors and has a pretty big period to boot.  I actually use
+ * a 4096 seed RNG for my scientific work, but that is MAJOR overkill for
+ * the simple purposes we require here, so I've not included it :)
+ */
+
+uint32 seed0, seed1, seed2, seed3;
+
+/* period 2^32 - 1 */
+/* fails Gorilla test, binary rank matrix */
+/* the ONLY good triplet and shift combinations out of the list of 648:
+ *
+ *    3  7 13     >> >> <<
+ *    3  7 13     << << >>
+ *    7  3 13     >> >> <<
+ *    7  3 13     << << >>
+ *
+ *    5  6 13     >> >> <<
+ *    5  6 13     << << >>
+ *    6  5 13     >> >> <<
+ *    6  5 13     << << >>	seems to be slightly "better" than the others?
+ *
+ * all others, including the "favorite" (13, 17, 5), fail some Monkey tests
+ */
+uint32 xorshift_32(void)
+{
+	seed0 ^= seed0 << 6;
+	seed0 ^= seed0 << 5;
+	seed0 ^= seed0 >> 13;
+
+	return(seed0);
+}
+
+/* period 2^128 - 1 */
+/* None of the other published 2^128-1 xorshift RNGs passed OPERM5 */
+uint32 xorshift_128(void)
+{
+	uint32 temp;
+
+	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
+		(seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
+	seed0 = seed1;
+	seed1 = seed2;
+	seed2 = seed3;
+	seed3 = temp;
+
+	return (temp);
+}
+
+/* return a random fraction over the range [0, 1) */
+double dxorshift_128(void)
+{
+	uint32 temp;
+
+	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
+		(seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
+	seed0 = seed1;
+	seed1 = seed2;
+	seed2 = seed3;
+	seed3 = temp;
+
+	return (temp / 4294967296.0);
+}
+
+void initialize_xorshift_128(uint32 seed)
+{
+	/* seed0 needs to be initialized prior to calling xorshift_32() */
+	seed0 = seed;
+
+	/* initialize with xorshift_32() */
+	seed0 = xorshift_32();
+	seed1 = xorshift_32();
+	seed2 = xorshift_32();
+	seed3 = xorshift_32();
+}
+#endif
+
+
+
+/*
+ * Faster than standard double atan(), |error| < 7E-6
+ *
+ * Original equation from Ranko Bojanic in StuChat37:
+ *
+ * |x| <= 1:
+ *
+ *    x + A*x3		A = 0.43157974, B = 0.76443945, C = 0.05831938
+ * ---------------
+ * 1 + B*x2 + C*x4
+ *
+ *
+ *
+ * After some optimizations:
+ *
+ * |x| <= 1:
+ *
+ *  x * (E + F*x2)	E = 1/C, F = A/C
+ * ----------------	G = (B/C + sqrt(B2/C2 - 4/C)) / 2
+ * (G + x2)(H + x2)	H = (B/C - sqrt(B2/C2 - 4/C)) / 2
+ *
+ *			E = 17.14695869537, F = 7.400279975541
+ *			G = 11.63393762882, H = 1.473874045440
+ *
+ * |x| > 1: pi/2 -
+ *
+ *   x * (I + x2)	I = A
+ * ----------------	J = (B + sqrt(B2 - 4C)) / 2
+ * (J + x2)(K + x2)	K = (B - sqrt(B2 - 4C)) / 2
+ *
+ *			I = 0.43157974
+ *			J = 0.6784840295980, K = 0.0859554204018
+ *
+ */
+double fast_atan(double x0)
+{
+	double x2;
+	double x;
+
+	x = fabs(x0);
+	x2 = x*x;
+	if (x > 1)
+	{
+		x2 = 1.570796326795 -
+			x * (0.43157974 + x2) /
+			((0.6784840295980 + x2) * (0.0859554204018 + x2));
+		if (x0 < 0) return -x2;
+		return x2;
+	}
+
+	return x0 * (17.14695869537 + 7.400279975541 * x2) /
+		((11.63393762882 + x2) * (1.473874045440 + x2));
+}
+
+
+
+/*
+ * Choose greyscale bitplane to use, return diff array.  Exit early and
+ * return NULL for a block of solid color (all diffs zero).
+ *
+ * No matter how you do it, mapping 3 bitplanes into a single greyscale
+ * bitplane will always result in colors which are very different mapping to
+ * the same greyscale value.  Inevitably, these pixels will appear next to 
+ * each other at some point in some image, and edge detection on a single 
+ * bitplane will behave quite strangely due to them having the same or nearly
+ * the same greyscale values.  Calculating distances between pixels using all 
+ * three RGB bitplanes is *way* too time consuming, so single bitplane 
+ * edge detection is used for speed's sake.  In order to try to avoid the 
+ * color mapping problems of using a single bitplane, 3 different greyscale 
+ * mappings are tested for each 3x3 grid, and the one with the most "signal" 
+ * (sum of squares difference from center pixel) is chosen.  This usually 
+ * results in useable contrast within the 3x3 grid.
+ *
+ * This results in a whopping 25% increase in overall runtime of the filter 
+ * over simply using luma or some other single greyscale bitplane, but it
+ * does greatly reduce the amount of errors due to greyscale mapping 
+ * problems.  I think this is the best compromise between accuracy and 
+ * speed, and is still a lot faster than edge detecting over all three RGB
+ * bitplanes.  The increase in image quality is well worth the speed hit.
+ *
+ */
+int16 * choose_greyscale(uint16 *pixels)
+{
+	static int16 greyscale_diffs[3][8];
+	static int16 bplanes[3][9];
+	int i, j;
+	int32 scores[3];
+
+	for (i = 0; i < 3; i++)
+	{
+		int16 *diff_ptr;
+		int16 *bptr;
+		uint16 *pptr;
+		int16 *grey_ptr;
+		int16 center;
+		int32 sum_diffs;
+
+		sum_diffs = 0;
+
+		grey_ptr = greyscale_table[i];
+
+		/* fill the 9 pixel window with greyscale values */
+		bptr = bplanes[i];
+		pptr = pixels;
+		for (j = 9; j; --j)
+			*bptr++ = grey_ptr[*pptr++];
+		bptr = bplanes[i];
+
+		center = grey_ptr[pixels[4]];
+		diff_ptr = greyscale_diffs[i];
+
+		/* calculate the delta from center pixel */
+		diff_ptr[0] = bptr[0] - center;
+		diff_ptr[1] = bptr[1] - center;
+		diff_ptr[2] = bptr[2] - center;
+		diff_ptr[3] = bptr[3] - center;
+		diff_ptr[4] = bptr[5] - center;
+		diff_ptr[5] = bptr[6] - center;
+		diff_ptr[6] = bptr[7] - center;
+		diff_ptr[7] = bptr[8] - center;
+
+		/* calculate sum of squares distance */
+		for (j = 8; j; --j) {
+			sum_diffs += *diff_ptr * *diff_ptr;
+			++diff_ptr;
+		}
+
+		scores[i] = sum_diffs;
+	}
+
+	/* choose greyscale with highest score, ties decided in GRB order */
+
+	if (scores[1] >= scores[0] && scores[1] >= scores[2])
+	{
+		if (!scores[1]) return NULL;
+
+		chosen_greyscale = greyscale_table[1];
+		bptr_global = bplanes[1];
+		return greyscale_diffs[1];
+	}
+
+	if (scores[0] >= scores[1] && scores[0] >= scores[2])
+	{
+		if (!scores[0]) return NULL;
+
+		chosen_greyscale = greyscale_table[0];
+		bptr_global = bplanes[0];
+		return greyscale_diffs[0];
+	}
+
+	if (!scores[2]) return NULL;
+
+	chosen_greyscale = greyscale_table[2];
+	bptr_global = bplanes[2];
+	return greyscale_diffs[2];
+}
+
+
+
+/*
+ * Calculate the distance between pixels in RGB space.  Greyscale isn't 
+ * accurate enough for choosing nearest-neighbors :(  Luma-like weighting 
+ * of the individual bitplane distances prior to squaring gives the most 
+ * useful results.
+ *
+ */
+int32 calc_pixel_diff_nosqrt(uint16 pixel1, uint16 pixel2)
+{
+
+#if 1	/* distance between pixels, weighted by roughly luma proportions */
+	int32 sum = 0;
+	int16 *rgb_ptr1 = rgb_table[pixel1];
+	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 diff;
+
+	diff = (*rgb_ptr1++ - *rgb_ptr2++) << 1;
+	sum += diff * diff;
+	diff = (*rgb_ptr1++ - *rgb_ptr2++) << 2;
+	sum += diff * diff;
+	diff = (*rgb_ptr1 - *rgb_ptr2);
+	sum += diff * diff;
+
+	return sum;
+#endif
+
+#if 0	/* distance between pixels, weighted by chosen greyscale proportions */
+	int32 sum = 0;
+	int16 *rgb_ptr1 = rgb_table[pixel1];
+	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 diff;
+	int r_shift, g_shift, b_shift;
+
+	if (chosen_greyscale == greyscale_table[1])
+	{
+		r_shift = 1;
+		g_shift = 2;
+		b_shift = 0;
+	}
+	else if (chosen_greyscale == greyscale_table[0])
+	{
+		r_shift = 2;
+		g_shift = 1;
+		b_shift = 0;
+	}
+	else
+	{
+		r_shift = 0;
+		g_shift = 1;
+		b_shift = 2;
+	}
+
+	diff = (*rgb_ptr1++ - *rgb_ptr2++) << r_shift;
+	sum += diff * diff;
+	diff = (*rgb_ptr1++ - *rgb_ptr2++) << g_shift;
+	sum += diff * diff;
+	diff = (*rgb_ptr1 - *rgb_ptr2) << b_shift;
+	sum += diff * diff;
+
+	return sum;
+#endif
+
+#if 0	/* distance between pixels, unweighted */
+	int32 sum = 0;
+	int16 *rgb_ptr1 = rgb_table[pixel1];
+	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 diff;
+
+	diff = *rgb_ptr1++ - *rgb_ptr2++;
+	sum += diff * diff;
+	diff = *rgb_ptr1++ - *rgb_ptr2++;
+	sum += diff * diff;
+	diff = *rgb_ptr1 - *rgb_ptr2;
+	sum += diff * diff;
+
+	return sum;
+#endif
+
+#if 0	/* use the greyscale directly */
+	return labs(chosen_greyscale[pixel1] - chosen_greyscale[pixel2]);
+#endif
+}
+
+
+
+/*
+ * Create vectors of all delta grey values from center pixel, with magnitudes
+ * ranging from [1.0, 0.0] (zero difference, maximum difference).  Find
+ * the two principle axes of the grid by calculating the eigenvalues and
+ * eigenvectors of the inertia tensor.  Use the eigenvectors to calculate the 
+ * edge direction.  In other words, find the angle of the line that optimally
+ * passes through the 3x3 pattern of pixels.
+ *
+ * Return horizontal (-), vertical (|), diagonal (/,\), multi (*), or none '0'
+ *
+ * Don't replace any of the double math with integer-based approximations,
+ * since everything I have tried has lead to slight mis-detection errors.
+ *
+ */
+int find_principle_axis(uint16 *pixels, int16 *diffs, int16 *bplane,
+		int8 *sim,
+		int32 *return_angle)
+{
+	struct xy_point
+	{
+		int16 x, y;
+	};
+
+	int i;
+	int16 centx = 0, centy = 0;
+	struct xy_point xy_points[9];
+	int angle;
+	int reverse_flag = 1;
+	int16 cutoff;
+	int16 max_diff;
+
+	double x, y;
+	int32 half_matrix[3] = {0};
+
+	double eigenval1, eigenval2;
+	double best_val;
+	double a, b, c;
+	double ratio;
+	int32 scale;
+
+	/* absolute value of differences */
+	for (i = 0; i < 8; i++)
+		diffs[i] = labs(diffs[i]);
+
+	/* find the max difference */
+	max_diff = *diffs;
+	for (i = 1; i < 8; i++)
+		if (diffs[i] > max_diff) max_diff = diffs[i];
+
+	/* exit early on uniform window */
+	/* already taken care of earlier elsewhere after greyscale assignment */
+	/* if (max_diff == 0) return '0'; */
+
+	/* normalize the differences */
+	scale = (1L<<(GREY_SHIFT+GREY_SHIFT)) / max_diff;
+	for (i = 0; i < 8; i++)
+		diffs[i] = (diffs[i] * scale + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+
+	/*
+	 * Some pixel patterns need to NOT be reversed, since the pixels of 
+	 * interest that form the edge to be detected are off-center.
+	 * 
+	 */
+
+	/* calculate yes/no similarity matrix to center pixel */
+	/* store the number of similar pixels */
+	cutoff = ((int16)1<<(GREY_SHIFT-3));
+	for (i = 0, sim_sum = 0; i < 8; i++)
+		sim_sum += (sim[i] = (diffs[i] < cutoff));
+
+	/* don't reverse pattern for off-center knights and sharp corners */
+	if (sim_sum >= 3 && sim_sum <= 5)
+	{
+		/* |. */ /* '- */
+		if (sim[1] && sim[4] && sim[5] && !sim[3] && !sim[6] &&
+				(!sim[0] ^ !sim[7]))
+			reverse_flag = 0;
+
+		/* -. */ /* '| */
+		else if (sim[2] && sim[3] && sim[6] && !sim[1] && !sim[4] &&
+				(!sim[0] ^ !sim[7]))
+			reverse_flag = 0;
+
+		/* .- */ /* |' */
+		else if (sim[4] && sim[6] && sim[0] && !sim[1] && !sim[3] &&
+				(!sim[2] ^ !sim[5]))
+			reverse_flag = 0;
+
+		/* .| */ /* -' */
+		else if (sim[1] && sim[3] && sim[7] && !sim[4] && !sim[6] &&
+				(!sim[2] ^ !sim[5]))
+			reverse_flag = 0;
+
+		/* 90 degree corners */
+		else if (sim_sum == 3)
+		{
+			if ((sim[0] && sim[1] && sim[3]) ||
+					(sim[1] && sim[2] && sim[4]) ||
+					(sim[3] && sim[5] && sim[6]) ||
+					(sim[4] && sim[6] && sim[7]))
+				reverse_flag = 0;
+		}
+	}
+
+	/* redo similarity array, less stringent for later checks */
+	cutoff = ((int16)1<<(GREY_SHIFT-1));
+	for (i = 0, sim_sum = 0; i < 8; i++)
+		sim_sum += (sim[i] = (diffs[i] < cutoff));
+
+	/* center pixel is different from all the others, not an edge */
+	if (sim_sum == 0) return '0';
+
+	/* reverse the difference array, so most similar is closest to 1 */
+	if (reverse_flag)
+	{
+		diffs[0] = ((int16)1<<GREY_SHIFT) - diffs[0];
+		diffs[1] = ((int16)1<<GREY_SHIFT) - diffs[1];
+		diffs[2] = ((int16)1<<GREY_SHIFT) - diffs[2];
+		diffs[3] = ((int16)1<<GREY_SHIFT) - diffs[3];
+		diffs[4] = ((int16)1<<GREY_SHIFT) - diffs[4];
+		diffs[5] = ((int16)1<<GREY_SHIFT) - diffs[5];
+		diffs[6] = ((int16)1<<GREY_SHIFT) - diffs[6];
+		diffs[7] = ((int16)1<<GREY_SHIFT) - diffs[7];
+	}
+
+	/* scale diagonals for projection onto axes */
+	diffs[0] = (diffs[0] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+	diffs[2] = (diffs[2] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+	diffs[5] = (diffs[5] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+	diffs[7] = (diffs[7] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+
+	/* create the vectors, centered at 0,0 */
+	xy_points[0].x = -diffs[0];
+	xy_points[0].y = diffs[0];
+	xy_points[1].x = 0;
+	xy_points[1].y = diffs[1];
+	xy_points[2].x = xy_points[2].y = diffs[2];
+	xy_points[3].x = -diffs[3];
+	xy_points[3].y = 0;
+	xy_points[4].x = 0;
+	xy_points[4].y = 0;
+	xy_points[5].x = diffs[4];
+	xy_points[5].y = 0;
+	xy_points[6].x = xy_points[6].y = -diffs[5];
+	xy_points[7].x = 0;
+	xy_points[7].y = -diffs[6];
+	xy_points[8].x = diffs[7];
+	xy_points[8].y = -diffs[7];
+
+	/* calculate the centroid of the points */
+	for (i = 0; i < 9; i++)
+	{
+		centx += xy_points[i].x;
+		centy += xy_points[i].y;
+	}
+	centx /= 9;
+	centy /= 9;
+
+	/* translate centroid to 0,0 */
+	for (i = 0; i < 9; i++)
+	{
+		xy_points[i].x -= centx;
+		xy_points[i].y -= centy;
+	}
+
+	/* fill inertia tensor 3x3 matrix */
+	for (i = 0; i < 9; i++)
+	{
+		half_matrix[0] += xy_points[i].x * xy_points[i].x;
+		half_matrix[1] += xy_points[i].y * xy_points[i].x;
+		half_matrix[2] += xy_points[i].y * xy_points[i].y;
+	}
+
+	/* calculate eigenvalues */
+	a = half_matrix[0] - half_matrix[2];
+	b = half_matrix[1] << 1;
+	b = sqrt(b*b + a*a);
+	a = half_matrix[0] + half_matrix[2];
+	eigenval1 = (a + b);
+	eigenval2 = (a - b);
+
+	/* find largest eigenvalue */	
+	if (eigenval1 == eigenval2)	/* X and + shapes */
+		return '*';
+	else if (eigenval1 > eigenval2)
+		best_val = eigenval1;
+	else
+		best_val = eigenval2;
+
+	/* white center pixel, black background */
+	if (!best_val)
+		return '*';
+
+	/* divide eigenvalue by 2, postponed from when it should have been
+	   done during the eigenvalue calculation but was delayed for
+	   another early exit opportunity */
+	best_val *= 0.5;
+
+	/* calculate eigenvectors */
+	c = best_val + half_matrix[1];
+	a = c - half_matrix[0];
+	b = c - half_matrix[2];
+	if (b)
+	{
+		x = 1.0;
+		ratio = y = a / b;
+	}
+	else if (a)
+	{
+		y = 1.0;
+		x = b / a;
+		ratio = a / b;
+	}
+	else if (a == b)
+	{
+		*return_angle = 13500;
+		return '\\';
+	}
+	else
+		return '*';
+
+	/* calculate angle in degrees * 100 */
+	if (x)
+	{
+		angle = (int32) floor(5729.577951307 * fast_atan(ratio) + 0.5);
+		if (x < 0.0) angle += 18000;
+	}
+	else
+	{
+		if (y > 0.0) angle = 9000;
+		else if (y < 0.0) angle = -9000;
+		else return '0';
+	}
+
+	/* force angle to lie between 0 to 360 */
+	if (angle < 0) angle += 36000;
+	if (angle > 18000) angle -= 18000;
+	*return_angle = angle;
+
+	if (angle <= 2250)
+		return '-';
+
+	if (angle < 6750)
+		return '/';
+
+	if (angle <= 11250)
+		return '|';
+
+	if (angle < 15750)
+		return '\\';
+
+	return '-';
+}
+
+
+
+/* Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad). */
+int check_arrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
+{
+	uint16 center = pixels[4];
+
+	if (center == pixels[0] && center == pixels[2] &&
+			center == pixels[6] && center == pixels[8])
+	{
+		switch(best_dir)
+		{
+			case 5:
+				if (center != pixels[5])	/* < */
+					return 0;
+				break;
+			case 6:
+				if (center != pixels[3])	/* > */
+					return 0;
+				break;
+			case 7:
+				if (center != pixels[7])	/* ^ */
+					return 0;
+				break;
+			case 8:
+				if (center != pixels[1])	/* v */
+					return 0;
+				break;
+		}
+	}
+
+	switch(best_dir)
+	{
+		case 5:		/* < */
+			if (center == pixels[2] && center == pixels[8] &&
+					pixels[1] == pixels[5] && pixels[5] == pixels[7] &&
+					(((center == pixels[0]) ^ (center == pixels[6])) ||
+					 (center == pixels[0] && center == pixels[6] &&
+					  pixels[1] != pixels[3])))
+				return 0;
+			break;
+
+		case 6:		/* > */
+			if (center == pixels[0] && center == pixels[6] &&
+					pixels[1] == pixels[3] && pixels[3] == pixels[7] &&
+					(((center == pixels[2]) ^ (center == pixels[8])) ||
+					 (center == pixels[2] && center == pixels[8] &&
+					  pixels[1] != pixels[5])))
+				return 0;
+			break;
+
+		case 7:		/* ^ */
+			if (center == pixels[6] && center == pixels[8] &&
+					pixels[3] == pixels[7] && pixels[7] == pixels[5] &&
+					(((center == pixels[0]) ^ (center == pixels[2])) ||
+					 (center == pixels[0] && center == pixels[2] &&
+					  pixels[3] != pixels[1])))
+				return 0;
+			break;
+
+		case 8:		/* v */
+			if (center == pixels[0] && center == pixels[2] &&
+					pixels[1] == pixels[3] && pixels[1] == pixels[5] &&
+					(((center == pixels[6]) ^ (center == pixels[8])) ||
+					 (center == pixels[6] && center == pixels[8] &&
+					  pixels[3] != pixels[7])))
+				return 0;
+			break;
+	}
+
+	switch(best_dir)
+	{
+		case 5:
+			if (sim[0] == sim[5] &&
+					sim[1] == sim[3] &&
+					sim[3] == sim[6] &&
+					((sim[2] && sim[7]) ||
+					 (half_flag && sim_sum == 2 && sim[4] &&
+					  (sim[2] || sim[7]))))	/* < */
+				return 1;
+			break;
+		case 6:
+			if (sim[2] == sim[7] &&
+					sim[1] == sim[4] &&
+					sim[4] == sim[6] &&
+					((sim[0] && sim[5]) ||
+					 (half_flag && sim_sum == 2 && sim[3] &&
+					  (sim[0] || sim[5]))))	/* > */
+				return 1;
+			break;
+		case 7:
+			if (sim[0] == sim[2] &&
+					sim[1] == sim[3] &&
+					sim[3] == sim[4] &&
+					((sim[5] && sim[7]) ||
+					 (half_flag && sim_sum == 2 && sim[6] &&
+					  (sim[5] || sim[7]))))	/* ^ */
+				return 1;
+			break;
+		case 8:
+			if (sim[5] == sim[7] &&
+					sim[3] == sim[6] &&
+					sim[4] == sim[6] &&
+					((sim[0] && sim[2]) ||
+					 (half_flag && sim_sum == 2 && sim[1] &&
+					  (sim[0] || sim[2]))))	/* v */
+				return 1;
+			break;
+	}
+
+	return 0;
+}
+
+
+
+/*
+ * Take original direction, refine it by testing different pixel difference 
+ * patterns based on the initial gross edge direction.
+ *
+ * The angle value is not currently used, but may be useful for future 
+ * refinement algorithms.
+ *
+ */
+int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
+		int8 *sim, double angle)
+{
+	int32 sums_dir[9] = { 0 };
+	int32 sum;
+	int32 best_sum;
+	int i, n, best_dir;
+	int16 diff_array[26];
+	int ok_arrow_flag = 1;
+
+	/*
+	 * -   '-  -.  \   '|  |.  |   |'  .|  /   -'  .-  <   >   ^   v
+	 *
+	 * 0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
+	 *
+	 * \'  .\  '/  /.
+	 *
+	 * 16  17  18  19
+	 *
+	 */
+
+	switch(edge_type)
+	{
+		case '|':
+			diff_array[0]  = labs(bptr[4] - bptr[1]);
+			diff_array[1]  = labs(bptr[4] - bptr[7]);
+			diff_array[2]  = labs(bptr[3] - bptr[0]);
+			diff_array[3]  = labs(bptr[3] - bptr[6]);
+			diff_array[4]  = labs(bptr[5] - bptr[2]);
+			diff_array[5]  = labs(bptr[5] - bptr[8]);
+			diff_array[6]  = labs(bptr[4] - bptr[2]);
+			diff_array[7]  = labs(bptr[4] - bptr[8]);
+			diff_array[8]  = labs(bptr[3] - bptr[1]);
+			diff_array[9]  = labs(bptr[3] - bptr[7]);
+			diff_array[10] = labs(bptr[4] - bptr[0]);
+			diff_array[11] = labs(bptr[4] - bptr[6]);
+			diff_array[12] = labs(bptr[5] - bptr[1]);
+			diff_array[13] = labs(bptr[5] - bptr[7]);
+			diff_array[14] = labs(bptr[0] - bptr[6]);
+			diff_array[15] = labs(bptr[2] - bptr[8]);
+			diff_array[16] = labs(bptr[1] - bptr[7]);
+			diff_array[17] = labs(bptr[0] - bptr[1]);
+			diff_array[18] = labs(bptr[2] - bptr[1]);
+			diff_array[19] = labs(bptr[0] - bptr[2]);
+			diff_array[20] = labs(bptr[3] - bptr[5]);
+			diff_array[21] = labs(bptr[6] - bptr[8]);
+			diff_array[22] = labs(bptr[6] - bptr[7]);
+			diff_array[23] = labs(bptr[8] - bptr[7]);
+
+			/* | vertical */
+			sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
+				diff_array[3] + diff_array[4] + diff_array[5];
+
+			/* << top */
+			sum = diff_array[8] + diff_array[9] +
+				((diff_array[6] + diff_array[7] +
+				  diff_array[12] + diff_array[13]) << 1) +
+				diff_array[14] + diff_array[16] + diff_array[15];
+			sum = (sum * 6) / 13;
+			sums_dir[5] = sum;
+
+			/* >> top */
+			sum = diff_array[12] + diff_array[13] +
+				((diff_array[10] + diff_array[11] +
+				  diff_array[8] + diff_array[9]) << 1) +
+				diff_array[15] + diff_array[16] + diff_array[14];
+			sum = (sum * 6) / 13;
+			sums_dir[6] = sum;
+
+			/* ^ bottom */
+			sum = diff_array[8] + diff_array[12] +
+				((diff_array[11] + diff_array[7]) << 1) +
+				(diff_array[1] << 2) +
+				diff_array[19] + diff_array[20] + diff_array[21];
+			sum = (sum * 6) / 13;
+			sums_dir[7] = sum;
+
+			/* v bottom */
+			sum = diff_array[9] + diff_array[13] +
+				((diff_array[10] + diff_array[6]) << 1) +
+				(diff_array[0] << 2) +
+				diff_array[21] + diff_array[20] + diff_array[19];
+			sum = (sum * 6) / 13;
+			sums_dir[8] = sum;
+
+			/* '| */
+			sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
+				diff_array[12] + (diff_array[3] << 1);
+			/* '| alt */
+			sum = diff_array[10] + diff_array[1] + diff_array[18] +
+				diff_array[4] + diff_array[5] + diff_array[14];
+			if (sum < sums_dir[1])
+				sums_dir[1] = sum;
+
+			/* |. */
+			sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
+				diff_array[9] + (diff_array[4] << 1);
+			/* |. alt */
+			sum = diff_array[0] + diff_array[7] + diff_array[22] +
+				diff_array[3] + diff_array[2] + diff_array[15];
+			if (sum < sums_dir[2])
+				sums_dir[2] = sum;
+
+			/* |' */
+			sums_dir[3] = diff_array[1] + diff_array[3] + diff_array[6] +
+				diff_array[8] + (diff_array[5] << 1);
+			/* |' alt */
+			sum = diff_array[6] + diff_array[1] + diff_array[17] +
+				diff_array[2] + diff_array[3] + diff_array[15];
+			if (sum < sums_dir[3])
+				sums_dir[3] = sum;
+
+			/* .| */
+			sums_dir[4] = diff_array[0] + diff_array[4] + diff_array[11] +
+				diff_array[13] + (diff_array[2] << 1);
+			/* .| alt */
+			sum = diff_array[11] + diff_array[0] + diff_array[23] +
+				diff_array[5] + diff_array[4] + diff_array[14];
+			if (sum < sums_dir[4])
+				sums_dir[4] = sum;
+
+			best_sum = sums_dir[0];
+			for (i = 1; i < 9; i++)
+				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+			if (best_sum == sums_dir[0]) return 6;	/* | */
+
+			best_dir = 0;
+			for (i = 0, n = 0; i < 9; i++)
+			{
+				if (sums_dir[i] == best_sum)
+				{
+					best_dir = i;
+					n++;
+				}
+			}
+
+			/* best direction uncertain, return original direction */
+			if (n > 1) return 6;	/* | */
+
+			if (best_dir >= 5)
+				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 1);
+
+			switch(best_dir)
+			{
+				case 1:
+					return 4;		/* '| */
+					break;
+				case 2:
+					return 5;		/* |. */
+					break;
+				case 3:
+					return 7;		/* |' */
+					break;
+				case 4:
+					return 8;		/* .| */
+					break;
+				case 5:
+					if (ok_arrow_flag)
+						return 12;		/* < */
+					break;
+				case 6:
+					if (ok_arrow_flag)
+						return 13;		/* > */
+					break;
+				case 7:
+					if (ok_arrow_flag)
+						return 14;		/* ^ */
+					break;
+				case 8:
+					if (ok_arrow_flag)
+						return 15;		/* V */
+					break;
+				case 0:
+				default:
+					return 6;		/* | */
+					break;
+			}
+
+			break;
+
+		case '-':
+			diff_array[0]  = labs(bptr[4] - bptr[3]);
+			diff_array[1]  = labs(bptr[4] - bptr[5]);
+			diff_array[2]  = labs(bptr[0] - bptr[1]);
+			diff_array[3]  = labs(bptr[1] - bptr[2]);
+			diff_array[4]  = labs(bptr[7] - bptr[6]);
+			diff_array[5]  = labs(bptr[7] - bptr[8]);
+			diff_array[6]  = labs(bptr[4] - bptr[6]);
+			diff_array[7]  = labs(bptr[4] - bptr[8]);
+			diff_array[8]  = labs(bptr[1] - bptr[3]);
+			diff_array[9]  = labs(bptr[1] - bptr[5]);
+			diff_array[10] = labs(bptr[4] - bptr[0]);
+			diff_array[11] = labs(bptr[4] - bptr[2]);
+			diff_array[12] = labs(bptr[7] - bptr[3]);
+			diff_array[13] = labs(bptr[7] - bptr[5]);
+			diff_array[14] = labs(bptr[0] - bptr[2]);
+			diff_array[15] = labs(bptr[6] - bptr[8]);
+			diff_array[16] = labs(bptr[3] - bptr[5]);
+			diff_array[17] = labs(bptr[0] - bptr[3]);
+			diff_array[18] = labs(bptr[6] - bptr[3]);
+			diff_array[19] = labs(bptr[0] - bptr[6]);
+			diff_array[20] = labs(bptr[1] - bptr[7]);
+			diff_array[21] = labs(bptr[2] - bptr[8]);
+			diff_array[22] = labs(bptr[2] - bptr[5]);
+			diff_array[23] = labs(bptr[8] - bptr[5]);
+
+			/* - horizontal */
+			sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
+				diff_array[3] + diff_array[4] + diff_array[5];
+
+			/* << bottom */
+			sum = diff_array[8] + diff_array[12] +
+				((diff_array[11] + diff_array[7]) << 1) +
+				(diff_array[1] << 2) +
+				diff_array[19] + diff_array[20] + diff_array[21];
+			sum = (sum * 6) / 13;
+			sums_dir[5] = sum;
+
+			/* >> bottom */
+			sum = diff_array[9] + diff_array[13] +
+				((diff_array[10] + diff_array[6]) << 1) +
+				(diff_array[0] << 2) +
+				diff_array[21] + diff_array[20] + diff_array[19];
+			sum = (sum * 6) / 13;
+			sums_dir[6] = sum;
+
+			/* ^ top */
+			sum = diff_array[8] + diff_array[9] +
+				((diff_array[6] + diff_array[7] +
+				  diff_array[12] + diff_array[13]) << 1) +
+				diff_array[14] + diff_array[16] + diff_array[15];
+			sum = (sum * 6) / 13;
+			sums_dir[7] = sum;
+
+			/* v top */
+			sum = diff_array[12] + diff_array[13] +
+				((diff_array[10] + diff_array[11] +
+				  diff_array[8] + diff_array[9]) << 1) +
+				diff_array[15] + diff_array[16] + diff_array[14];
+			sum = (sum * 6) / 13;
+			sums_dir[8] = sum;
+
+			/* '- */
+			sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
+				diff_array[12] + (diff_array[3] << 1);
+			/* '- alt */
+			sum = diff_array[10] + diff_array[1] + diff_array[18] +
+				diff_array[4] + diff_array[5] + diff_array[14];
+			if (sum < sums_dir[1])
+				sums_dir[1] = sum;
+
+			/* -. */
+			sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
+				diff_array[9] + (diff_array[4] << 1);
+			/* -. alt */
+			sum = diff_array[0] + diff_array[7] + diff_array[22] +
+				diff_array[3] + diff_array[2] + diff_array[15];
+			if (sum < sums_dir[2])
+				sums_dir[2] = sum;
+
+			/* -' */
+			sums_dir[3] = diff_array[0] + diff_array[4] + diff_array[11] +
+				diff_array[13] + (diff_array[2] << 1);
+			/* -' alt */
+			sum = diff_array[11] + diff_array[0] + diff_array[23] +
+				diff_array[5] + diff_array[4] + diff_array[14];
+			if (sum < sums_dir[3])
+				sums_dir[3] = sum;
+
+			/* .- */
+			sums_dir[4] = diff_array[1] + diff_array[3] + diff_array[6] +
+				diff_array[8] + (diff_array[5] << 1);
+			/* .- alt */
+			sum = diff_array[6] + diff_array[1] + diff_array[17] +
+				diff_array[2] + diff_array[3] + diff_array[15];
+			if (sum < sums_dir[4])
+				sums_dir[4] = sum;
+
+			best_sum = sums_dir[0];
+			for (i = 1; i < 9; i++)
+				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+			if (best_sum == sums_dir[0]) return 0;	/* - */
+
+			best_dir = 0;
+			for (i = 0, n = 0; i < 9; i++)
+			{
+				if (sums_dir[i] == best_sum)
+				{
+					best_dir = i;
+					n++;
+				}
+			}
+
+			/* best direction uncertain, return original direction */
+			if (n > 1) return 0;	/* - */
+
+			if (best_dir >= 5)
+				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 1);
+
+			switch(best_dir)
+			{
+				case 1:
+					return 1;		/* '- */
+					break;
+				case 2:
+					return 2;		/* -. */
+					break;
+				case 3:
+					return 10;		/* -' */
+					break;
+				case 4:
+					return 11;		/* .- */
+					break;
+				case 5:
+					if (ok_arrow_flag)
+						return 12;		/* < */
+					break;
+				case 6:
+					if (ok_arrow_flag)
+						return 13;		/* > */
+					break;
+				case 7:
+					if (ok_arrow_flag)
+						return 14;		/* ^ */
+					break;
+				case 8:
+					if (ok_arrow_flag)
+						return 15;		/* V */
+					break;
+				case 0:
+				default:
+					return 0;		/* - */
+					break;
+			}
+
+			break;
+
+		case '\\':
+
+			/* CHECK -- handle noisy half-diags */
+			if (sim_sum == 1)
+			{
+				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+						pixels[5] == pixels[7])
+				{
+					if (pixels[2] != pixels[1] && pixels[6] != pixels[1])
+					{
+						sum = labs(bptr[2] - bptr[4]) +
+							labs(bptr[6] - bptr[4]);
+
+						if (sim[0] && sum < (labs(bptr[8] - bptr[4]) << 1))
+						{
+							if (bptr[4] > bptr[8])
+							{
+								if (bptr[2] > bptr[4] &&
+										bptr[6] > bptr[4])
+									return 18;		/* '/ */
+							}
+							else
+							{
+								if (bptr[2] < bptr[4] &&
+										bptr[6] < bptr[4])
+									return 18;		/* '/ */
+							}
+						}
+
+						if (sim[7] && sum < (labs(bptr[0] - bptr[4]) << 1))
+						{
+							if (bptr[4] > bptr[0])
+							{
+								if (bptr[2] > bptr[4] &&
+										bptr[6] > bptr[4])
+									return 19;		/* /. */
+							}
+							else
+							{
+								if (bptr[2] < bptr[4] &&
+										bptr[6] < bptr[4])
+									return 19;		/* /. */
+							}
+						}
+					}
+				}
+
+				if (sim[0] &&
+						labs(bptr[4] - bptr[0]) < ((int16)1<<(GREY_SHIFT-3)))
+					return 3;	/* \ */
+				if (sim[7] &&
+						labs(bptr[4] - bptr[8]) < ((int16)1<<(GREY_SHIFT-3)))
+					return 3;	/* \ */
+			}
+
+			diff_array[0]  = labs(bptr[4] - bptr[0]);
+			diff_array[1]  = labs(bptr[4] - bptr[5]);
+			diff_array[2]  = labs(bptr[3] - bptr[7]);
+			diff_array[3]  = labs(bptr[7] - bptr[8]);
+			diff_array[4]  = labs(bptr[1] - bptr[2]);
+			diff_array[5]  = labs(bptr[4] - bptr[3]);
+			diff_array[6]  = labs(bptr[4] - bptr[8]);
+			diff_array[7]  = labs(bptr[0] - bptr[1]);
+			diff_array[8]  = labs(bptr[1] - bptr[5]);
+			diff_array[9]  = labs(bptr[6] - bptr[7]);
+			diff_array[10] = labs(bptr[4] - bptr[7]);
+			diff_array[11] = labs(bptr[5] - bptr[8]);
+			diff_array[12] = labs(bptr[3] - bptr[6]);
+			diff_array[13] = labs(bptr[4] - bptr[1]);
+			diff_array[14] = labs(bptr[0] - bptr[3]);
+			diff_array[15] = labs(bptr[2] - bptr[5]);
+			diff_array[20] = labs(bptr[0] - bptr[2]);
+			diff_array[21] = labs(bptr[6] - bptr[8]);
+			diff_array[22] = labs(bptr[0] - bptr[6]);
+			diff_array[23] = labs(bptr[2] - bptr[8]);
+			diff_array[16] = labs(bptr[4] - bptr[2]);
+			diff_array[18] = labs(bptr[4] - bptr[6]);
+
+			/* '- */
+			sums_dir[1] = diff_array[0] + diff_array[1] + diff_array[2] +
+				diff_array[3] + (diff_array[4] << 1);
+			/* '- alt */
+			sum = diff_array[0] + diff_array[1] + diff_array[12] +
+				diff_array[9] + diff_array[3] + diff_array[20];
+			if (sum < sums_dir[1])
+				sums_dir[1] = sum;
+
+			/* -. */
+			sums_dir[2] = diff_array[5] + diff_array[6] + diff_array[7] +
+				diff_array[8] + (diff_array[9] << 1);
+			/* -. alt */
+			sum = diff_array[6] + diff_array[5] + diff_array[15] +
+				diff_array[4] + diff_array[7] + diff_array[21];
+			if (sum < sums_dir[2])
+				sums_dir[2] = sum;
+
+			/* '| */
+			sums_dir[3] = diff_array[0] + diff_array[8] + diff_array[10] +
+				diff_array[11] + (diff_array[12] << 1);
+			/* '| alt */
+			sum = diff_array[0] + diff_array[10] + diff_array[4] +
+				diff_array[15] + diff_array[11] + diff_array[22];
+			if (sum < sums_dir[3])
+				sums_dir[3] = sum;
+
+			/* |. */
+			sums_dir[4] = diff_array[2] + diff_array[6] + diff_array[13] +
+				diff_array[14] + (diff_array[15] << 1);
+			/* |. alt */
+			sum = diff_array[13] + diff_array[6] + diff_array[9] +
+				diff_array[12] + diff_array[14] + diff_array[23];
+			if (sum < sums_dir[4])
+				sums_dir[4] = sum;
+
+			/* \ 45 */
+			sums_dir[0] = diff_array[0] + diff_array[6] +
+				(diff_array[2] << 1) + (diff_array[8] << 1);
+
+			/* << top */
+			sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
+				((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
+				  labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
+				labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
+				labs(bptr[2] - bptr[8]);
+			sum = (sum * 6) / 13;
+			sums_dir[5] = sum;
+
+			/* >> top */
+			sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
+				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
+				  labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
+				labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
+				labs(bptr[0] - bptr[6]);
+			sum = (sum * 6) / 13;
+			sums_dir[6] = sum;
+
+			/* ^ top */
+			sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
+				((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
+				  labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
+				labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
+				labs(bptr[6] - bptr[8]);
+			sum = (sum * 6) / 13;
+			sums_dir[7] = sum;
+
+			/* v top */
+			sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
+				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
+				  labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
+				labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
+				labs(bptr[0] - bptr[2]);
+			sum = (sum * 6) / 13;
+			sums_dir[8] = sum;
+
+			best_sum = sums_dir[0];
+			for (i = 1; i < 9; i++)
+				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+
+			best_dir = 0;
+			for (i = 0, n = 0; i < 9; i++)
+			{
+				if (sums_dir[i] == best_sum)
+				{
+					best_dir = i;
+					n++;
+				}
+			}
+
+			/* CHECK -- handle zig-zags */
+			if (sim_sum == 3)
+			{
+				if ((best_dir == 0 || best_dir == 1) &&
+						sim[0] && sim[1] && sim[4])
+					return 1;				/* '- */
+				if ((best_dir == 0 || best_dir == 2) &&
+						sim[3] && sim[6] && sim[7])
+					return 2;				/* -. */
+				if ((best_dir == 0 || best_dir == 3) &&
+						sim[0] && sim[3] && sim[6])
+					return 4;				/* '| */
+				if ((best_dir == 0 || best_dir == 4) &&
+						sim[1] && sim[4] && sim[7])
+					return 5;				/* |. */
+			}
+
+			if (n > 1 && best_sum == sums_dir[0]) return 3;	/* \ */
+
+			/* best direction uncertain, return non-edge to avoid artifacts */
+			if (n > 1) return -1;
+
+			/* CHECK -- diagonal intersections */
+			if (best_dir == 0 &&
+					(sim[1] == sim[4] || sim[3] == sim[6]) &&
+					(sim[1] == sim[3] || sim[4] == sim[6]))
+			{
+				if ((pixels[1] == pixels[3] || pixels[5] == pixels[7]) ||
+						((pixels[0] == pixels[4]) ^ (pixels[8] == pixels[4])))
+				{
+					if (pixels[2] == pixels[4])
+						return 16;		/* \' */
+					if (pixels[6] == pixels[4])
+						return 17;		/* .\ */
+				}
+
+				if (sim_sum == 3 && sim[0] && sim[7] &&
+						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+						pixels[5] == pixels[7])
+				{
+					if (sim[2])
+						return 16;		/* \' */
+					if (sim[5])
+						return 17;		/* .\ */
+				}
+
+				if (sim_sum == 3 && sim[2] && sim[5])
+				{
+					if (sim[0])
+						return 18;		/* '/ */
+					if (sim[7])
+						return 19;		/* /. */
+				}
+			}
+
+			if (best_dir >= 5)
+				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 0);
+
+			switch(best_dir)
+			{
+				case 1:
+					return 1;		/* '- */
+					break;
+				case 2:
+					return 2;		/* -. */
+					break;
+				case 3:
+					return 4;		/* '| */
+					break;
+				case 4:
+					return 5;		/* |. */
+					break;
+				case 5:
+					if (ok_arrow_flag)
+						return 12;		/* < */
+					break;
+				case 6:
+					if (ok_arrow_flag)
+						return 13;		/* > */
+					break;
+				case 7:
+					if (ok_arrow_flag)
+						return 14;		/* ^ */
+					break;
+				case 8:
+					if (ok_arrow_flag)
+						return 15;		/* V */
+					break;
+				case 0:
+				default:
+					return 3;		/* \ */
+					break;
+			}
+
+			break;
+
+		case '/':
+
+			/* CHECK -- handle noisy half-diags */
+			if (sim_sum == 1)
+			{
+				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+						pixels[5] == pixels[7])
+				{
+					if (pixels[0] != pixels[1] && pixels[8] != pixels[1])
+					{
+						sum = labs(bptr[0] - bptr[4]) +
+							labs(bptr[8] - bptr[4]);
+
+						if (sim[2] && sum < (labs(bptr[6] - bptr[4]) << 1))
+						{
+							if (bptr[4] > bptr[6])
+							{
+								if (bptr[0] > bptr[4] &&
+										bptr[8] > bptr[4])
+									return 16;		/* \' */
+							}
+							else
+							{
+								if (bptr[0] < bptr[4] &&
+										bptr[8] < bptr[4])
+									return 16;		/* \' */
+							}
+						}
+
+						if (sim[5] && sum < (labs(bptr[2] - bptr[4]) << 1))
+						{
+							if (bptr[4] > bptr[2])
+							{
+								if (bptr[0] > bptr[4] &&
+										bptr[8] > bptr[4])
+								{
+									if (pixels[6] == pixels[4])
+										return 17;		/* .\ */
+									return 3;			/* \ */
+								}
+							}
+							else
+							{
+								if (bptr[0] < bptr[4] &&
+										bptr[8] < bptr[4])
+								{
+									if (pixels[6] == pixels[4])
+										return 17;		/* .\ */
+									return 3;			/* \ */
+								}
+							}
+						}
+					}
+				}
+
+				if (sim[2] &&
+						labs(bptr[4] - bptr[2]) < ((int16)1<<(GREY_SHIFT-3)))
+					return 9;	/* / */
+				if (sim[5] &&
+						labs(bptr[4] - bptr[6]) < ((int16)1<<(GREY_SHIFT-3)))
+					return 9;	/* / */
+			}
+
+			diff_array[0]  = labs(bptr[4] - bptr[0]);
+			diff_array[1]  = labs(bptr[4] - bptr[5]);
+			diff_array[3]  = labs(bptr[7] - bptr[8]);
+			diff_array[4]  = labs(bptr[1] - bptr[2]);
+			diff_array[5]  = labs(bptr[4] - bptr[3]);
+			diff_array[6]  = labs(bptr[4] - bptr[8]);
+			diff_array[7]  = labs(bptr[0] - bptr[1]);
+			diff_array[9]  = labs(bptr[6] - bptr[7]);
+			diff_array[10] = labs(bptr[4] - bptr[7]);
+			diff_array[11] = labs(bptr[5] - bptr[8]);
+			diff_array[12] = labs(bptr[3] - bptr[6]);
+			diff_array[13] = labs(bptr[4] - bptr[1]);
+			diff_array[14] = labs(bptr[0] - bptr[3]);
+			diff_array[15] = labs(bptr[2] - bptr[5]);
+			diff_array[16] = labs(bptr[4] - bptr[2]);
+			diff_array[17] = labs(bptr[1] - bptr[3]);
+			diff_array[18] = labs(bptr[4] - bptr[6]);
+			diff_array[19] = labs(bptr[5] - bptr[7]);
+			diff_array[20] = labs(bptr[0] - bptr[2]);
+			diff_array[21] = labs(bptr[6] - bptr[8]);
+			diff_array[22] = labs(bptr[0] - bptr[6]);
+			diff_array[23] = labs(bptr[2] - bptr[8]);
+
+			/* |' */
+			sums_dir[1] = diff_array[10] + diff_array[12] + diff_array[16] +
+				diff_array[17] + (diff_array[11] << 1);
+			/* |' alt */
+			sum = diff_array[16] + diff_array[10] + diff_array[7] +
+				diff_array[14] + diff_array[12] + diff_array[23];
+			if (sum < sums_dir[1])
+				sums_dir[1] = sum;
+
+			/* .| */
+			sums_dir[2] = diff_array[13] + diff_array[15] + diff_array[18] +
+				diff_array[19] + (diff_array[14] << 1);
+			/* .| alt */
+			sum = diff_array[18] + diff_array[13] + diff_array[3] +
+				diff_array[11] + diff_array[15] + diff_array[22];
+			if (sum < sums_dir[2])
+				sums_dir[2] = sum;
+
+			/* -' */
+			sums_dir[3] = diff_array[5] + diff_array[9] + diff_array[16] +
+				diff_array[19] + (diff_array[7] << 1);
+			/* -' alt */
+			sum = diff_array[16] + diff_array[5] + diff_array[11] +
+				diff_array[3] + diff_array[9] + diff_array[20];
+			if (sum < sums_dir[3])
+				sums_dir[3] = sum;
+
+			/* .- */
+			sums_dir[4] = diff_array[1] + diff_array[4] + diff_array[17] +
+				diff_array[18] + (diff_array[3] << 1);
+			/* .- alt */
+			sum = diff_array[18] + diff_array[1] + diff_array[14] +
+				diff_array[7] + diff_array[4] + diff_array[21];
+			if (sum < sums_dir[4])
+				sums_dir[4] = sum;
+
+			/* / 135 */
+			sums_dir[0] = diff_array[16] + diff_array[18] +
+				(diff_array[17] << 1) + (diff_array[19] << 1);
+
+			/* << top */
+			sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
+				((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
+				  labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
+				labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
+				labs(bptr[2] - bptr[8]);
+			sum = (sum * 6) / 13;
+			sums_dir[5] = sum;
+
+			/* >> top */
+			sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
+				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
+				  labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
+				labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
+				labs(bptr[0] - bptr[6]);
+			sum = (sum * 6) / 13;
+			sums_dir[6] = sum;
+
+			/* ^ top */
+			sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
+				((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
+				  labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
+				labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
+				labs(bptr[6] - bptr[8]);
+			sum = (sum * 6) / 13;
+			sums_dir[7] = sum;
+
+			/* v top */
+			sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
+				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
+				  labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
+				labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
+				labs(bptr[0] - bptr[2]);
+			sum = (sum * 6) / 13;
+			sums_dir[8] = sum;
+
+			best_sum = sums_dir[0];
+			for (i = 1; i < 9; i++)
+				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+
+			best_dir = 0;
+			for (i = 0, n = 0; i < 9; i++)
+			{
+				if (sums_dir[i] == best_sum)
+				{
+					best_dir = i;
+					n++;
+				}
+			}
+
+			/* CHECK -- handle zig-zags */
+			if (sim_sum == 3)
+			{
+				if ((best_dir == 0 || best_dir == 1) &&
+						sim[2] && sim[4] && sim[6])
+					return 7;				/* |' */
+				if ((best_dir == 0 || best_dir == 2) &&
+						sim[1] && sim[3] && sim[5])
+					return 8;				/* .| */
+				if ((best_dir == 0 || best_dir == 3) &&
+						sim[1] && sim[2] && sim[3])
+					return 10;				/* -' */
+				if ((best_dir == 0 || best_dir == 4) &&
+						sim[4] && sim[5] && sim[6])
+					return 11;				/* .- */
+			}
+
+			if (n > 1 && best_sum == sums_dir[0]) return 9;	/* / */
+
+			/* best direction uncertain, return non-edge to avoid artifacts */
+			if (n > 1) return -1;
+
+			/* CHECK -- diagonal intersections */
+			if (best_dir == 0 &&
+					(sim[1] == sim[3] || sim[4] == sim[6]) &&
+					(sim[1] == sim[4] || sim[3] == sim[6]))
+			{
+				if ((pixels[1] == pixels[5] || pixels[3] == pixels[7]) ||
+						((pixels[2] == pixels[4]) ^ (pixels[6] == pixels[4])))
+				{
+					if (pixels[0] == pixels[4])
+						return 18;		/* '/ */
+					if (pixels[8] == pixels[4])
+						return 19;		/* /. */
+				}
+
+				if (sim_sum == 3 && sim[2] && sim[5] &&
+						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+						pixels[5] == pixels[7])
+				{
+					if (sim[0])
+						return 18;		/* '/ */
+					if (sim[7])
+						return 19;		/* /. */
+				}
+
+				if (sim_sum == 3 && sim[0] && sim[7])
+				{
+					if (sim[2])
+						return 16;		/* \' */
+					if (sim[5])
+						return 17;		/* .\ */
+				}
+			}
+
+			if (best_dir >= 5)
+				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 0);
+
+			switch(best_dir)
+			{
+				case 1:
+					return 7;		/* |' */
+					break;
+				case 2:
+					return 8;		/* .| */
+					break;
+				case 3:
+					return 10;		/* -' */
+					break;
+				case 4:
+					return 11;		/* .- */
+					break;
+				case 5:
+					if (ok_arrow_flag)
+						return 12;		/* < */
+					break;
+				case 6:
+					if (ok_arrow_flag)
+						return 13;		/* > */
+					break;
+				case 7:
+					if (ok_arrow_flag)
+						return 14;		/* ^ */
+					break;
+				case 8:
+					if (ok_arrow_flag)
+						return 15;		/* V */
+					break;
+					break;
+				case 0:
+				default:
+					return 9;		/* / */
+					break;
+			}
+
+			break;
+
+		case '*':
+			return 127;
+			break;
+
+		case '0':
+		default:
+			return -1;
+			break;
+	}
+
+	return -1;
+}
+
+
+
+/* "Chess Knight" patterns can be mis-detected, fix easy cases. */
+int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
+{
+	uint16 center = pixels[4];
+	int dir = sub_type;
+	int n = 0;
+	int flags[12] = {0};
+	int ok_orig_flag = 0;
+
+	/*
+	 * -   '-  -.  \   '|  |.  |   |'  .|  /   -'  .-
+	 *
+	 * 0   1   2   3   4   5   6   7   8   9   10  11
+	 *
+	 */
+
+	/* check to see if original knight is ok */
+	switch(sub_type)
+	{
+		case 1:		/* '- */
+			if (sim[0] && sim[4] &&
+					!(sim_sum == 3 && sim[5] &&
+						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 2:		/* -. */
+			if (sim[3] && sim[7] &&
+					!(sim_sum == 3 && sim[2] &&
+						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 4:		/* '| */
+			if (sim[0] && sim[6] &&
+					!(sim_sum == 3 && sim[2] &&
+						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 5:		/* |. */
+			if (sim[1] && sim[7] &&
+					!(sim_sum == 3 && sim[5] &&
+						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 7:		/* |' */
+			if (sim[2] && sim[6] &&
+					!(sim_sum == 3 && sim[0] &&
+						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 8:		/* .| */
+			if (sim[1] && sim[5] &&
+					!(sim_sum == 3 && sim[7] &&
+						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 10:	/* -' */
+			if (sim[2] && sim[3] &&
+					!(sim_sum == 3 && sim[7] &&
+						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		case 11:	/* .- */
+			if (sim[4] && sim[5] &&
+					!(sim_sum == 3 && sim[0] &&
+						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
+				ok_orig_flag = 1;
+			break;
+
+		default:	/* not a knight */
+			return sub_type;
+			break;
+	}
+
+	/* look for "better" knights */
+	if (center == pixels[0] && center == pixels[5])	/* '- */
+	{
+		dir = 1;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[3] && center == pixels[8])	/* -. */
+	{
+		dir = 2;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[0] && center == pixels[7])	/* '| */
+	{
+		dir = 4;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[1] && center == pixels[8])	/* |. */
+	{
+		dir = 5;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[2] && center == pixels[7])	/* |' */
+	{
+		dir = 7;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[1] && center == pixels[6])	/* .| */
+	{
+		dir = 8;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[3] && center == pixels[2])	/* -' */
+	{
+		dir = 10;
+		flags[dir] = 1;
+		n++;
+	}
+	if (center == pixels[6] && center == pixels[5])	/* .- */
+	{
+		dir = 11;
+		flags[dir] = 1;
+		n++;
+	}
+
+	if (n == 0)
+	{
+		if (ok_orig_flag) return sub_type;
+		return -1;
+	}
+	if (n == 1) return dir;
+	if (n == 2)
+	{
+		/* slanted W patterns */
+		if (flags[1] && flags[5]) return 3;	/* \ */
+		if (flags[2] && flags[4]) return 3;	/* \ */
+		if (flags[7] && flags[11]) return 9;	/* / */
+		if (flags[8] && flags[10]) return 9;	/* / */
+	}
+	if (flags[sub_type] && ok_orig_flag) return sub_type;
+
+	return -1;
+}
+
+
+
+/* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
+#define highBits	0xF7DEF7DE
+#define lowBits		0x08210821
+#define qhighBits	0xE79CE79C
+#define qlowBits	0x18631863
+#define redblueMask	0xF81F
+#define greenMask	0x07E0
+
+/* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
+/**
+ * Interpolate two 16 bit pixel pairs at once with equal weights 1.
+ * In particular, A and B can contain two pixels/each in the upper
+ * and lower halves.
+ */
+uint32 INTERPOLATE(uint32 A, uint32 B)
+{
+	return (((A & highBits) >> 1) + ((B & highBits) >> 1) + (A & B & lowBits));
+}
+
+/* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
+/**
+ * Interpolate four 16 bit pixel pairs at once with equal weights 1.
+ * In particular, A and B can contain two pixels/each in the upper
+ * and lower halves.
+ */
+uint32 Q_INTERPOLATE(uint32 A, uint32 B, uint32 C, uint32 D)
+{
+	uint32 x = ((A & qhighBits) >> 2) + ((B & qhighBits) >> 2) + ((C & qhighBits) >> 2) + ((D & qhighBits) >> 2);
+	uint32 y = ((A & qlowBits) + (B & qlowBits) + (C & qlowBits) + (D & qlowBits)) >> 2;
+
+	y &= qlowBits;
+	return x + y;
+}
+
+
+
+/* Average three pixels together */
+uint16 average_three_pixels(uint16 pixel1, uint16 pixel2, uint16 pixel3)
+{
+	uint32 rsum;
+	uint16 gsum, bsum;
+
+	rsum =  (pixel1 & 0xF800);
+	rsum += (pixel2 & 0xF800);
+	rsum += (pixel3 & 0xF800);
+	rsum = div3[rsum >> 11];
+
+	gsum =  (pixel1 & 0x07E0);
+	gsum += (pixel2 & 0x07E0);
+	gsum += (pixel3 & 0x07E0);
+	gsum = div3[gsum >> 5];
+
+	bsum =  (pixel1 & 0x001F);
+	bsum += (pixel2 & 0x001F);
+	bsum += (pixel3 & 0x001F);
+	bsum = div3[bsum];
+
+	return ((rsum << 11) | (gsum << 5) | bsum);
+}
+
+
+
+/* Interpolate 1/3rd of the way between two pixels */
+uint16 average_one_third(uint16 pixel1, uint16 pixel2)
+{
+	uint32 rsum;
+	uint16 gsum, bsum;
+
+	rsum =  (pixel1 & 0xF800) << 1;
+	rsum += (pixel2 & 0xF800);
+	rsum = div3[rsum >> 11];
+
+	gsum =  (pixel1 & 0x07E0) << 1;
+	gsum += (pixel2 & 0x07E0);
+	gsum = div3[gsum >> 5];
+
+	bsum =  (pixel1 & 0x001F) << 1;
+	bsum += (pixel2 & 0x001F);
+	bsum = div3[bsum];
+
+	return ((rsum << 11) | (gsum << 5) | bsum);
+}
+
+
+
+/* Fill pixel grid without interpolation, using the detected edge */
+void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
+		uint16 *pixels, int sub_type, int16 *bptr)
+{
+	uint16 *dptr2;
+	int16 tmp_grey;
+	uint16 center = pixels[4];
+	int32 diff1, diff2, diff3;
+	uint16 tmp[9];
+	uint16 *ptmp;
+	int i;
+
+	switch (sub_type)
+	{
+		case 1:		/* '- */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[6]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[6] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[6] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[6] = pixels[7];
+				else
+					tmp[6] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[6]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[8]);
+				if (diff1 <= diff2)
+					tmp[7] = tmp[6];
+			}
+
+			break;
+
+		case 2:		/* -. */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[5];
+				else
+					tmp[2] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[2]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[0]);
+				if (diff1 <= diff2)
+					tmp[1] = tmp[2];
+			}
+
+			break;
+
+		case 3:		/* \ */
+		case 16:	/* \' */
+		case 17:	/* .\ */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			if (sub_type != 16)
+			{
+				tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[5];
+				else
+					tmp[2] = pixels[4];
+			}
+
+			if (sub_type != 17)
+			{
+				tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[6] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[6] = pixels[7];
+				else
+					tmp[6] = pixels[4];
+			}
+
+			break;
+
+		case 4:		/* '| */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[5];
+				else
+					tmp[2] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[2]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[8]);
+				if (diff1 <= diff2)
+					tmp[5] = tmp[2];
+			}
+
+			break;
+
+		case 5:		/* |. */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[6]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[6] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[6] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[6] = pixels[7];
+				else
+					tmp[6] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[6]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[0]);
+				if (diff1 <= diff2)
+					tmp[3] = tmp[6];
+			}
+
+			break;
+
+		case 7:		/* |' */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[0]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[6]);
+				if (diff1 <= diff2)
+					tmp[3] = tmp[0];
+			}
+
+			break;
+
+		case 8:		/* .| */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[8]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[8] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[8] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[8] = pixels[7];
+				else
+					tmp[8] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[8]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[2]);
+				if (diff1 <= diff2)
+					tmp[5] = tmp[8];
+			}
+
+			break;
+
+		case 9:		/* / */
+		case 18:	/* '/ */
+		case 19:	/* /. */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			if (sub_type != 18)
+			{
+				tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+			}
+
+			if (sub_type != 19)
+			{
+				tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[8] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[8] = pixels[7];
+				else
+					tmp[8] = pixels[4];
+			}
+
+			break;
+
+		case 10:	/* -' */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[8]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[8] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[8] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[8] = pixels[7];
+				else
+					tmp[8] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[8]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[6]);
+				if (diff1 <= diff2)
+					tmp[7] = tmp[8];
+			}
+
+			break;
+
+		case 11:	/* .- */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[0]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[2]);
+				if (diff1 <= diff2)
+					tmp[1] = tmp[0];
+			}
+
+			break;
+
+		case 12:	/* < */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+			}
+
+			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[6]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[6] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[6] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[6] = pixels[7];
+				else
+					tmp[6] = pixels[4];
+			}
+
+			break;
+
+		case 13:	/* > */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[5];
+				else
+					tmp[2] = pixels[4];
+			}
+
+			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[8]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[8] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[8] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[8] = pixels[7];
+				else
+					tmp[8] = pixels[4];
+			}
+
+			break;
+
+		case 14:	/* ^ */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+			}
+
+			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[5];
+				else
+					tmp[2] = pixels[4];
+			}
+
+			break;
+
+		case 15:	/* v */
+			for (i = 0; i < 9; i++)
+				tmp[i] = center;
+
+			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[6]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[6] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[6] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[6] = pixels[7];
+				else
+					tmp[6] = pixels[4];
+			}
+
+			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[8]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[8] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[8] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[8] = pixels[7];
+				else
+					tmp[8] = pixels[4];
+			}
+
+			break;
+
+		case 127:	/* * */
+		case -1:	/* no edge */
+		case 0:		/* - */
+		case 6:		/* | */
+		default:
+			dptr2 = ((uint16 *) (dptr - dstPitch)) - 1;
+			*dptr2++ = center;
+			*dptr2++ = center;
+			*dptr2 = center;
+			dptr2 = ((uint16 *) dptr) - 1;
+			*dptr2++ = center;
+			*dptr2++ = center;
+#if DEBUG_REFRESH_RANDOM_XOR
+			*dptr2 = center ^ (uint16) (dxorshift_128() * (1L<<16));
+#else
+			*dptr2 = center;
+#endif
+			dptr2 = ((uint16 *) (dptr + dstPitch)) - 1;
+			*dptr2++ = center;
+			*dptr2++ = center;
+			*dptr2 = center;
+
+			return;
+
+			break;
+	}
+
+	ptmp = tmp;
+	dptr2 = ((uint16 *) (dptr - dstPitch)) - 1;
+	*dptr2++ = *ptmp++;
+	*dptr2++ = *ptmp++;
+	*dptr2 = *ptmp++;
+	dptr2 = ((uint16 *) dptr) - 1;
+	*dptr2++ = *ptmp++;
+	*dptr2++ = *ptmp++;
+#if DEBUG_REFRESH_RANDOM_XOR
+	*dptr2 = *ptmp++ ^ (uint16) (dxorshift_128() * (1L<<16));
+#else
+	*dptr2 = *ptmp++;
+#endif
+	dptr2 = ((uint16 *) (dptr + dstPitch)) - 1;
+	*dptr2++ = *ptmp++;
+	*dptr2++ = *ptmp++;
+	*dptr2 = *ptmp;
+}
+
+
+
+/* Fill pixel grid with or without interpolation, using the detected edge */
+void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
+		uint16 *pixels, int sub_type, int16 *bptr,
+		int8 *sim,
+		int interpolate_2x)
+{
+	uint16 *dptr2;
+	uint16 center = pixels[4];
+	int32 diff1, diff2, diff3;
+	int16 tmp_grey;
+	uint16 tmp[4];
+	uint16 *ptmp;
+
+	switch (sub_type)
+	{
+		case 1:		/* '- */
+			tmp[0] = tmp[1] = tmp[3] = center;
+
+			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[7];
+				else
+					tmp[2] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[2]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[8]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[2];
+						tmp[2] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[3] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[2] = INTERPOLATE(tmp[2], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[2]])
+							tmp[2] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 2:		/* -. */
+			tmp[0] = tmp[2] = tmp[3] = center;
+
+			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[1]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[1] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[1] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[1] = pixels[5];
+				else
+					tmp[1] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[1]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[0]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[1];
+						tmp[1] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[0] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[1] = INTERPOLATE(tmp[1], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[1]])
+							tmp[1] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 3:		/* \ */
+		case 16:	/* \' */
+		case 17:	/* .\ */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			if (sub_type != 16)
+			{
+				tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[1] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[1] = pixels[5];
+				else
+					tmp[1] = pixels[4];
+
+				if (interpolate_2x)
+				{
+					tmp[1] = INTERPOLATE(tmp[1], center);
+				}
+				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
+				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+				 * nearest-neighbor, so detect them and don't anti-alias them.
+				 */
+				else if (bptr[4] > chosen_greyscale[tmp[1]] ||
+						(sim_sum == 1 && (sim[0] || sim[7]) &&
+						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
+					tmp[1] = center;
+			}
+
+			if (sub_type != 17)
+			{
+				tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[7];
+				else
+					tmp[2] = pixels[4];
+
+				if (interpolate_2x)
+				{
+					tmp[2] = INTERPOLATE(tmp[2], center);
+				}
+				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
+				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+				 * nearest-neighbor, so detect them and don't anti-alias them.
+				 */
+				else if (bptr[4] > chosen_greyscale[tmp[2]] ||
+						(sim_sum == 1 && (sim[0] || sim[7]) &&
+						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
+					tmp[2] = center;
+			}
+
+			break;
+
+		case 4:		/* '| */
+			tmp[0] = tmp[2] = tmp[3] = center;
+
+			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[1]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[1] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[1] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[1] = pixels[5];
+				else
+					tmp[1] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[1]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[8]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[1];
+						tmp[1] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[3] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[1] = INTERPOLATE(tmp[1], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[1]])
+							tmp[1] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 5:		/* |. */
+			tmp[0] = tmp[1] = tmp[3] = center;
+
+			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[7];
+				else
+					tmp[2] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[2]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[0]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[2];
+						tmp[2] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[0] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[2] = INTERPOLATE(tmp[2], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[2]])
+							tmp[2] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 7:		/* |' */
+			tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[0]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[6]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[0];
+						tmp[0] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[2] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[0] = INTERPOLATE(tmp[0], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[0]])
+							tmp[0] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 8:		/* .| */
+			tmp[0] = tmp[1] = tmp[2] = center;
+
+			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[3]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[3] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[3] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[3] = pixels[7];
+				else
+					tmp[3] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[3]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[2]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[3];
+						tmp[3] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[1] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[3] = INTERPOLATE(tmp[3], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[3]])
+							tmp[3] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 9:		/* / */
+		case 18:	/* '/ */
+		case 19:	/* /. */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			if (sub_type != 18)
+			{
+				tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				if (interpolate_2x)
+				{
+					tmp[0] = INTERPOLATE(tmp[0], center);
+				}
+				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
+				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+				 * nearest-neighbor, so detect them and don't anti-alias them.
+				 */
+				else if (bptr[4] > chosen_greyscale[tmp[0]] ||
+						(sim_sum == 1 && (sim[2] || sim[5]) &&
+						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
+					tmp[0] = center;
+			}
+
+			if (sub_type != 19)
+			{
+				tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[3] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[3] = pixels[7];
+				else
+					tmp[3] = pixels[4];
+
+				if (interpolate_2x)
+				{
+					tmp[3] = INTERPOLATE(tmp[3], center);
+				}
+				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
+				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+				 * nearest-neighbor, so detect them and don't anti-alias them.
+				 */
+				else if (bptr[4] > chosen_greyscale[tmp[3]] ||
+						(sim_sum == 1 && (sim[2] || sim[5]) &&
+						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
+					tmp[3] = center;
+			}
+
+			break;
+
+		case 10:	/* -' */
+			tmp[0] = tmp[1] = tmp[2] = center;
+
+			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[3]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[3] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[3] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[3] = pixels[7];
+				else
+					tmp[3] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[3]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[6]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[3];
+						tmp[3] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[2] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[3] = INTERPOLATE(tmp[3], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[3]])
+							tmp[3] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 11:	/* .- */
+			tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_KNIGHTS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				tmp_grey = chosen_greyscale[tmp[0]];
+				diff1 = labs(bptr[4] - tmp_grey);
+				diff2 = labs(bptr[4] - bptr[2]);
+				if (diff1 <= diff2)
+				{
+					if (interpolate_2x)
+					{
+						uint16 tmp_pixel = tmp[0];
+						tmp[0] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
+								tmp_pixel, center);
+						tmp[1] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+					}
+				}
+				else
+				{
+					if (interpolate_2x)
+					{
+						tmp[0] = INTERPOLATE(tmp[0], center);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[0]])
+							tmp[0] = center;
+					}
+				}
+			}
+
+			break;
+
+		case 12:	/* < */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[4] && sim[2])
+				{
+					if (interpolate_2x)
+					{
+						tmp[0] = INTERPOLATE(center, tmp[0]);
+						tmp[2] = average_one_third(center, tmp[0]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[0]])
+							tmp[0] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[0] = average_one_third(center, tmp[0]);
+				else
+					tmp[0] = center;
+			}
+
+			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[7];
+				else
+					tmp[2] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[4] && sim[7])
+				{
+					if (interpolate_2x)
+					{
+						tmp[2] = INTERPOLATE(center, tmp[2]);
+						tmp[0] = average_one_third(center, tmp[2]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[2]])
+							tmp[2] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[2] = average_one_third(center, tmp[2]);
+				else
+					tmp[2] = center;
+			}
+
+			break;
+
+		case 13:	/* > */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[1]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[1] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[1] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[1] = pixels[5];
+				else
+					tmp[1] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[3] && sim[0])
+				{
+					if (interpolate_2x)
+					{
+						tmp[1] = INTERPOLATE(center, tmp[1]);
+						tmp[3] = average_one_third(center, tmp[1]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[1]])
+							tmp[1] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[1] = average_one_third(center, tmp[1]);
+				else
+					tmp[1] = center;
+			}
+
+			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[3]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[3] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[3] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[3] = pixels[7];
+				else
+					tmp[3] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[3] && sim[5])
+				{
+					if (interpolate_2x)
+					{
+						tmp[3] = INTERPOLATE(center, tmp[3]);
+						tmp[1] = average_one_third(center, tmp[3]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[3]])
+							tmp[3] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[3] = average_one_third(center, tmp[3]);
+				else
+					tmp[3] = center;
+			}
+
+			break;
+
+		case 14:	/* ^ */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp_grey = chosen_greyscale[tmp[0]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[1]);
+			diff2 = labs(bptr[4] - bptr[3]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[0] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[0] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[0] = pixels[3];
+				else
+					tmp[0] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[6] && sim[5])
+				{
+					if (interpolate_2x)
+					{
+						tmp[0] = INTERPOLATE(center, tmp[0]);
+						tmp[1] = average_one_third(center, tmp[0]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[0]])
+							tmp[0] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[0] = average_one_third(center, tmp[0]);
+				else
+					tmp[0] = center;
+			}
+
+			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp_grey = chosen_greyscale[tmp[1]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[5]);
+			diff2 = labs(bptr[4] - bptr[1]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[1] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[1] = pixels[1];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[1] = pixels[5];
+				else
+					tmp[1] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[6] && sim[7])
+				{
+					if (interpolate_2x)
+					{
+						tmp[1] = INTERPOLATE(center, tmp[1]);
+						tmp[0] = average_one_third(center, tmp[1]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[1]])
+							tmp[1] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[1] = average_one_third(center, tmp[1]);
+				else
+					tmp[1] = center;
+			}
+
+			break;
+
+		case 15:	/* v */
+			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[2]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[3]);
+			diff2 = labs(bptr[4] - bptr[7]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[2] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[2] = pixels[3];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[2] = pixels[7];
+				else
+					tmp[2] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[1] && sim[0])
+				{
+					if (interpolate_2x)
+					{
+						tmp[2] = INTERPOLATE(center, tmp[2]);
+						tmp[3] = average_one_third(center, tmp[2]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[2]])
+							tmp[2] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[2] = average_one_third(center, tmp[2]);
+				else
+					tmp[2] = center;
+			}
+
+			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp_grey = chosen_greyscale[tmp[3]];
+#if PARANOID_ARROWS
+			diff1 = labs(bptr[4] - bptr[7]);
+			diff2 = labs(bptr[4] - bptr[5]);
+			diff3 = labs(bptr[4] - tmp_grey);
+			if (diff1 < diff3 || diff2 < diff3)
+				tmp[3] = center;
+			else	/* choose nearest pixel */
+#endif
+			{
+				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
+				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
+				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				if (diff1 <= diff2 && diff1 <= diff3)
+					tmp[3] = pixels[5];
+				else if (diff2 <= diff1 && diff2 <= diff3)
+					tmp[3] = pixels[7];
+				else
+					tmp[3] = pixels[4];
+
+				/* check for half-arrow */
+				if (sim_sum == 2 && sim[1] && sim[2])
+				{
+					if (interpolate_2x)
+					{
+						tmp[3] = INTERPOLATE(center, tmp[3]);
+						tmp[2] = average_one_third(center, tmp[3]);
+					}
+					else
+					{
+						if (bptr[4] > chosen_greyscale[tmp[3]])
+							tmp[3] = center;
+					}
+
+					break;
+				}
+
+				if (interpolate_2x)
+					tmp[3] = average_one_third(center, tmp[3]);
+				else
+					tmp[3] = center;
+			}
+
+			break;
+
+		case -1:	/* no edge */
+		case 0:		/* - */
+		case 6:		/* | */
+		case 127:	/* * */
+		default:	/* no edge */
+			dptr2 = (uint16 *) dptr;
+			*dptr2++ = center;
+#if DEBUG_REFRESH_RANDOM_XOR
+			*dptr2 = center ^ (uint16) (dxorshift_128() * (1L<<16));
+#else
+			*dptr2 = center;
+#endif
+			dptr2 = (uint16 *) (dptr + dstPitch);
+			*dptr2++ = center;
+			*dptr2 = center;
+
+			return;
+
+			break;
+	}
+
+	ptmp = tmp;
+	dptr2 = (uint16 *) dptr;
+	*dptr2++ = *ptmp++;
+#if DEBUG_REFRESH_RANDOM_XOR
+	*dptr2 = *ptmp++ ^ (uint16) (dxorshift_128() * (1L<<16));
+#else
+	*dptr2 = *ptmp++;
+#endif
+	dptr2 = (uint16 *) (dptr + dstPitch);
+	*dptr2++ = *ptmp++;
+	*dptr2 = *ptmp;
+}
+
+
+
+#if HANDLE_TRANSPARENT_OVERLAYS
+/* Deal with transparent pixels */
+void handle_transparent_overlay(uint16 transp, uint16 *pixels,
+		int16 *bplane, int16 *diffs)
+{
+	int16 tmp_grey;
+	int16 max_diff;
+	int16 min_grey = ((int16)1<<RGB_SHIFT);
+	int16 max_grey = 0;
+	int i;
+
+	/* find min and max grey values in window */
+	for (i = 0; i < 9; i++)
+	{
+		if (bplane[i] < min_grey) min_grey = bplane[i];
+		if (bplane[i] > max_grey) max_grey = bplane[i];
+	}
+
+	/* treat transparent pixels as the average of min_grey and max_grey */
+	/* set diff from center pixel to maximum difference within the window */
+	tmp_grey = (min_grey + max_grey + 1) >> 1;
+	max_diff = max_grey - min_grey;
+
+	if (pixels[4] == transp)	/* center pixel is transparent */
+	{
+		/* set all transparent pixels to middle grey */
+		if (pixels[0] == transp) bplane[0] = tmp_grey;
+		if (pixels[1] == transp) bplane[1] = tmp_grey;
+		if (pixels[2] == transp) bplane[2] = tmp_grey;
+		if (pixels[3] == transp) bplane[3] = tmp_grey;
+		if (pixels[4] == transp) bplane[4] = tmp_grey;
+		if (pixels[5] == transp) bplane[5] = tmp_grey;
+		if (pixels[6] == transp) bplane[6] = tmp_grey;
+		if (pixels[7] == transp) bplane[7] = tmp_grey;
+		if (pixels[8] == transp) bplane[8] = tmp_grey;
+
+		/* set all diffs to non-transparent pixels to max diff */
+		if (pixels[0] != transp) diffs[0] = max_diff;
+		if (pixels[1] != transp) diffs[1] = max_diff;
+		if (pixels[2] != transp) diffs[2] = max_diff;
+		if (pixels[3] != transp) diffs[3] = max_diff;
+		if (pixels[5] != transp) diffs[4] = max_diff;
+		if (pixels[6] != transp) diffs[5] = max_diff;
+		if (pixels[7] != transp) diffs[6] = max_diff;
+		if (pixels[8] != transp) diffs[7] = max_diff;
+	}
+	else			/* center pixel is non-transparent */
+	{
+		/* choose transparent grey value to give largest contrast */
+		tmp_grey = (bplane[4] >= tmp_grey) ? min_grey : max_grey;
+
+		/* set new transparent pixel values and diffs */
+		if (pixels[0] == transp)
+		{
+			bplane[0] = tmp_grey;
+			diffs[0] = max_diff;
+		}
+		if (pixels[1] == transp)
+		{
+			bplane[1] = tmp_grey;
+			diffs[1] = max_diff;
+		}
+		if (pixels[2] == transp)
+		{
+			bplane[2] = tmp_grey;
+			diffs[2] = max_diff;
+		}
+		if (pixels[3] == transp)
+		{
+			bplane[3] = tmp_grey;
+			diffs[3] = max_diff;
+		}
+		if (pixels[5] == transp)
+		{
+			bplane[5] = tmp_grey;
+			diffs[4] = max_diff;
+		}
+		if (pixels[6] == transp)
+		{
+			bplane[6] = tmp_grey;
+			diffs[5] = max_diff;
+		}
+		if (pixels[7] == transp)
+		{
+			bplane[7] = tmp_grey;
+			diffs[6] = max_diff;
+		}
+		if (pixels[8] == transp)
+		{
+			bplane[8] = tmp_grey;
+			diffs[7] = max_diff;
+		}
+	}
+}
+#endif
+
+
+
+/* Check for changed pixel grid, return 1 if unchanged. */
+int check_unchanged_pixels(uint16 *old_src_ptr, uint16 *pixels, int w)
+{
+	uint16 *dptr16;
+
+	dptr16 = old_src_ptr - w - 1;
+	if (*dptr16++ != pixels[0]) return 0;
+	if (*dptr16++ != pixels[1]) return 0;
+	if (*dptr16 != pixels[2]) return 0;
+
+	dptr16 += w - 2;
+	if (*dptr16 != pixels[3]) return 0;
+	dptr16 += 2;
+	if (*dptr16 != pixels[5]) return 0;
+
+	dptr16 += w - 2;
+	if (*dptr16++ != pixels[6]) return 0;
+	if (*dptr16++ != pixels[7]) return 0;
+	if (*dptr16 != pixels[8]) return 0;
+
+	return 1;
+}
+
+
+/* Draw unchanged pixel grid, 3x */
+/* old_dptr16 starts in top left of grid, dptr16 in center */
+void draw_unchanged_grid_3x(uint16 *dptr16, int dstPitch,
+		uint16 *old_dptr16, int old_dst_inc)
+{
+	uint16 *sptr;
+	uint16 *dptr;
+	uint8 *dptr8 = (uint8 *) dptr16;
+
+	sptr = old_dptr16;
+	dptr = (uint16 *) (dptr8 - dstPitch) - 1;
+	*dptr++ = *sptr++;
+	*dptr++ = *sptr++;
+	*dptr = *sptr;
+
+	sptr = old_dptr16 + old_dst_inc;
+	dptr = dptr16 - 1;
+	*dptr++ = *sptr++;
+	*dptr++ = *sptr++;
+	*dptr = *sptr;
+
+	sptr = old_dptr16 + old_dst_inc + old_dst_inc;
+	dptr = (uint16 *) (dptr8 + dstPitch) - 1;
+	*dptr++ = *sptr++;
+	*dptr++ = *sptr++;
+	*dptr = *sptr;
+}
+
+
+
+/* Draw unchanged pixel grid, 2x */
+void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
+		uint16 *old_dptr16, int old_dst_inc)
+{
+	uint16 *sptr;
+	uint16 *dptr;
+	uint8 *dptr8 = (uint8 *) dptr16;
+
+	sptr = old_dptr16;
+	dptr = dptr16;
+	*dptr++ = *sptr++;
+	*dptr = *sptr;
+
+	sptr = old_dptr16 + old_dst_inc;
+	dptr = (uint16 *) (dptr8 + dstPitch);
+	*dptr++ = *sptr++;
+	*dptr = *sptr;
+}
+
+
+/* Perform edge detection, draw the new 3x pixels */
+void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
+		int w, int h, int w_new, int h_new,
+		int srcPitch, int dstPitch,
+		int overlay_flag)
+{
+	int x, y;
+	int w2 = w + 2;
+	const uint8 *sptr8 = src;
+	uint8 *dptr8 = dst + dstPitch + 2;
+	const uint16 *sptr16;
+	uint16 *dptr16;
+	int16 *bplane;
+	int8 sim[8];
+	int sub_type;
+	int32 angle;
+	int16 *diffs;
+	int dstPitch3 = dstPitch * 3;
+
+	uint16 *old_src_ptr, *old_dst_ptr;
+	uint16 *old_sptr16, *old_dptr16;
+	int32 dist, old_src_y, old_src_x;
+	int old_src_inc;
+	int old_dst_inc, old_dst_inc3;
+
+	if (overlay_flag)
+	{
+		old_src_ptr = old_overlay + w2 + 1;
+		old_src_inc = w2;
+
+		old_dst_ptr = old_dst_overlay;
+		old_dst_inc = w_new;
+	}
+	else
+	{
+		dist = src - (const uint8 *) src_addr_min;
+		old_src_y = dist / srcPitch;
+		old_src_x = (dist - old_src_y * srcPitch) >> 1;
+
+		old_src_inc = cur_screen_width + 2;
+		old_src_ptr = old_src + (old_src_y + 1) * old_src_inc + old_src_x + 1;
+
+		old_dst_inc = 3 * cur_screen_width;
+		old_dst_ptr = old_dst + 3 * (old_src_y * old_dst_inc + old_src_x);
+	}
+
+	old_dst_inc3 = 3 * old_dst_inc;
+
+#if 0
+#if HANDLE_TRANSPARENT_OVERLAYS
+	uint16 transp = 0;	/* transparent color */
+
+	/* assume bitmap is padded by a transparent border, take src-1 pixel */
+	if (overlay_flag) transp = *((const uint16 *) src - 1);
+#endif
+
+	/* The dirty rects optimizer in the SDL backend is helpful for frames
+	 * with few changes, but _REALLY_ bogs things down when the whole screen
+	 * changes.  Overall, the dirty rects optimizer isn't worth it, since
+	 * the Edge2x/3x unchanged pixel detection is far faster than full blown
+	 * dirty rects optimization.
+	 */
+	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
+#endif
+
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3,
+			old_src_ptr += old_src_inc, old_dst_ptr += old_dst_inc3)
+	{
+		for (x = 0,
+				sptr16 = (const uint16 *) sptr8,
+				dptr16 = (uint16 *) dptr8,
+				old_sptr16 = old_src_ptr,
+				old_dptr16 = old_dst_ptr;
+				x < w; x++, sptr16++, dptr16 += 3, old_sptr16++, old_dptr16 += 3)
+		{
+			const uint16 *sptr2, *addr3;
+			uint16 pixels[9];
+			char edge_type;
+
+			sptr2 = ((const uint16 *) ((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const uint16 *) ((const uint8 *) sptr16 + srcPitch)) + 1;
+
+			/* fill the 3x3 grid */
+			memcpy(pixels, sptr2, 3*sizeof(uint16));
+			memcpy(pixels+3, sptr16 - 1, 3*sizeof(uint16));
+			memcpy(pixels+6, addr3 - 2, 3*sizeof(uint16));
+
+#if 0
+			/* skip interior unchanged 3x3 blocks */
+			if (*sptr16 == *old_sptr16 &&
+#if DEBUG_DRAW_REFRESH_BORDERS
+					x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+#endif
+					check_unchanged_pixels(old_sptr16, pixels, old_src_inc))
+			{
+				draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
+						old_dst_inc);
+
+#if DEBUG_REFRESH_RANDOM_XOR
+				*(dptr16 + 1) = 0;
+#endif
+				continue;
+			}
+
+#endif
+			diffs = choose_greyscale(pixels);
+
+			/* block of solid color */
+			if (!diffs)
+			{
+				anti_alias_grid_clean_3x((uint8 *) dptr16, dstPitch, pixels,
+						0, NULL);
+				continue;
+			}
+
+#if 0
+#if HANDLE_TRANSPARENT_OVERLAYS
+			if (overlay_flag)
+				handle_transparent_overlay(transp, pixels, bplane, diffs);
+#endif
+#endif
+
+			bplane = bptr_global;
+
+			edge_type = find_principle_axis(pixels, diffs, bplane,
+					sim, &angle);
+			sub_type = refine_direction(edge_type, pixels, bplane,
+					sim, angle);
+			if (sub_type >= 0)
+				sub_type = fix_knights(sub_type, pixels, sim);
+
+			anti_alias_grid_clean_3x((uint8 *) dptr16, dstPitch, pixels,
+					sub_type, bplane);
+		}
+	}
+}
+
+
+
+/* Perform edge detection, draw the new 2x pixels */
+void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
+		int w, int h, int w_new, int h_new,
+		int srcPitch, int dstPitch,
+		int overlay_flag,
+		int interpolate_2x)
+{
+	int x, y;
+	int w2 = w + 2;
+	const uint8 *sptr8 = src;
+	uint8 *dptr8 = dst;
+	const uint16 *sptr16;
+	uint16 *dptr16;
+	int16 *bplane;
+	int8 sim[8];
+	int sub_type;
+	int32 angle;
+	int16 *diffs;
+	int dstPitch2 = dstPitch << 1;
+
+	uint16 *old_src_ptr, *old_dst_ptr;
+	uint16 *old_sptr16, *old_dptr16;
+	int32 dist, old_src_y, old_src_x;
+	int old_src_inc;
+	int old_dst_inc, old_dst_inc2;
+
+	if (overlay_flag)
+	{
+		old_src_ptr = old_overlay + w2 + 1;
+		old_src_inc = w2;
+
+		old_dst_ptr = old_dst_overlay;
+		old_dst_inc = w_new;
+	}
+	else
+	{
+		dist = src - (const uint8 *) src_addr_min;
+		old_src_y = dist / srcPitch;
+		old_src_x = (dist - old_src_y * srcPitch) >> 1;
+
+		old_src_inc = cur_screen_width + 2;
+		old_src_ptr = old_src + (old_src_y + 1) * old_src_inc + old_src_x + 1;
+
+		old_dst_inc = 2 * cur_screen_width;
+		old_dst_ptr = old_dst + 2 * (old_src_y * old_dst_inc + old_src_x);
+	}
+
+	old_dst_inc2 = 2 * old_dst_inc;
+
+#if 0 
+#if HANDLE_TRANSPARENT_OVERLAYS
+	uint16 transp = 0;	/* transparent color */
+
+	/* assume bitmap is padded by a transparent border, take src-1 pixel */
+	if (overlay_flag) transp = *((const uint16 *) src - 1);
+#endif
+
+
+	/* The dirty rects optimizer in the SDL backend is helpful for frames
+	 * with few changes, but _REALLY_ bogs things down when the whole screen
+	 * changes.  Overall, the dirty rects optimizer isn't worth it, since
+	 * the Edge2x/3x unchanged pixel detection is far faster than full blown
+	 * dirty rects optimization.
+	 */
+	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
+#endif
+
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2,
+			old_src_ptr += old_src_inc, old_dst_ptr += old_dst_inc2)
+	{
+		for (x = 0,
+				sptr16 = (const uint16 *) sptr8,
+				dptr16 = (uint16 *) dptr8,
+				old_sptr16 = old_src_ptr,
+				old_dptr16 = old_dst_ptr;
+				x < w; x++, sptr16++, dptr16 += 2, old_sptr16++, old_dptr16 += 2)
+		{
+			const uint16 *sptr2, *addr3;
+			uint16 pixels[9];
+			char edge_type;
+
+			sptr2 = ((const uint16 *) ((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const uint16 *) ((const uint8 *) sptr16 + srcPitch)) + 1;
+
+			/* fill the 3x3 grid */
+			memcpy(pixels, sptr2, 3*sizeof(uint16));
+			memcpy(pixels+3, sptr16 - 1, 3*sizeof(uint16));
+			memcpy(pixels+6, addr3 - 2, 3*sizeof(uint16));
+
+#if 0
+			/* skip interior unchanged 3x3 blocks */
+			if (*sptr16 == *old_sptr16 &&
+#if DEBUG_DRAW_REFRESH_BORDERS
+					x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+#endif
+					check_unchanged_pixels(old_sptr16, pixels, old_src_inc))
+			{
+				draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
+						old_dst_inc);
+
+#if DEBUG_REFRESH_RANDOM_XOR
+				*(dptr16 + 1) = 0;
+#endif
+				continue;
+			}
+#endif
+
+			diffs = choose_greyscale(pixels);
+
+			/* block of solid color */
+			if (!diffs)
+			{
+				anti_alias_grid_2x((uint8 *) dptr16, dstPitch, pixels,
+						0, NULL, NULL, 0);
+				continue;
+			}
+
+#if 0
+#if HANDLE_TRANSPARENT_OVERLAYS
+			if (overlay_flag)
+				handle_transparent_overlay(transp, pixels, bplane, diffs);
+#endif
+#endif
+
+			bplane = bptr_global;
+
+			edge_type = find_principle_axis(pixels, diffs, bplane,
+					sim, &angle);
+			sub_type = refine_direction(edge_type, pixels, bplane,
+					sim, angle);
+			if (sub_type >= 0)
+				sub_type = fix_knights(sub_type, pixels, sim);
+
+			anti_alias_grid_2x((uint8 *) dptr16, dstPitch, pixels,
+					sub_type, bplane, sim,
+					interpolate_2x);
+		}
+	}
+}
+
+
+
+/* Initialize various lookup tables */
+void init_tables(const uint8 *srcPtr, uint32 srcPitch,
+		int width, int height)
+{
+	double r_float, g_float, b_float;
+	int r, g, b;
+	uint16 i;
+	double val[3];
+	double intensity;
+	int16 *rgb_ptr;
+
+#if DEBUG_REFRESH_RANDOM_XOR
+	/* seed the random number generator, we don't care if the seed is random */
+	initialize_xorshift_128(42);
+#endif
+
+	/* initialize greyscale table */
+	for (r = 0; r < 32; r++) {
+		r_float = r / 31.0;
+
+		for (g = 0; g < 64; g++) {
+			g_float = g / 63.0;
+
+			for (b = 0; b < 32; b++) {
+				b_float = b / 31.0;
+
+				intensity = (r_float + g_float + b_float) / 3;
+
+				i = (r << 11) | (g << 5) | b;
+
+				/* use luma-like weights for each color, 2x increments */
+				val[0] = 0.571 * r_float + 0.286 * g_float + 0.143 * b_float;
+				val[1] = 0.286 * r_float + 0.571 * g_float + 0.143 * b_float;
+				val[2] = 0.143 * r_float + 0.286 * g_float + 0.571 * b_float;
+
+				/* factor in a little intensity too, it helps */
+				val[0] = (intensity + 9*val[0]) / 10;
+				val[1] = (intensity + 9*val[1]) / 10;
+				val[2] = (intensity + 9*val[2]) / 10;
+
+				/* store the greyscale tables */
+				greyscale_table[0][i] = (int16) (val[0] * ((int16)1<<GREY_SHIFT) + 0.5);
+				greyscale_table[1][i] = (int16) (val[1] * ((int16)1<<GREY_SHIFT) + 0.5);
+				greyscale_table[2][i] = (int16) (val[2] * ((int16)1<<GREY_SHIFT) + 0.5);
+
+				/* normalized RGB channel lookups */
+				rgb_ptr = rgb_table[(r << 11) | (g << 5) | b];
+				rgb_ptr[0] = (int16) (r_float * ((int16)1<<RGB_SHIFT) + 0.5);
+				rgb_ptr[1] = (int16) (g_float * ((int16)1<<RGB_SHIFT) + 0.5);
+				rgb_ptr[2] = (int16) (b_float * ((int16)1<<RGB_SHIFT) + 0.5);
+			}
+		}
+	}
+
+	/* initialize interpolation division tables */
+	for (r = 0; r <= 189; r++)
+		div3[r] = ((r<<1)+3) / 6;
+	for (r = 0; r <= 567; r++)
+		div9[r] = ((r<<1)+9) / 18;
+
+#if 0
+#if INCREASE_WIN32_PRIORITY
+	/*
+	 * Greatly increase thread and process priority under Win32.
+	 *
+	 * -- WARNING -- WinXP has a rather poor priority system, it's basicly
+	 * "largely ignore me", "normal", or "interfere with some system processes".
+	 * At least Win9x/ME had "increased" and "decreased" modes, but now those API
+	 * functions aren't "supported" in WinNT/2K/XP, and we're stuck with the mess
+	 * we have now.  "Normal" is preempted by so much other unimportant stuff,
+	 * including the idle process, that ScummVM never gets above 75% CPU usage,
+	 * even when the filter is overworked and the frame rate is getting jerky.
+	 * Massively increasing BOTH the process and thread priorities (need to boost
+	 * the process priority to keep audio in sync with the increased thread
+	 * priority when the frame rate gets jerky) beats Windows into giving the
+	 * program the time it needs, however, doing things outside ScummVM may be
+	 * sluggish.  I recommend pausing ScummVM prior to shrinking from full-screen
+	 * to a window, or before switching from ScummVM to some other task.  Other
+	 * than the above mentioned sluggishness, I have not seen any other sort of
+	 * system instability while running with increased priorities.
+	 *
+	 */
+
+	/* Rather than check for all Win32 compiler flavors, just check for WIN32 and
+	 * PRIORITY defines.
+	 */
+#if defined WIN32 && defined(HIGH_PRIORITY_CLASS) && defined(THREAD_PRIORITY_TIME_CRITICAL)
+
+	/* Raise task and thread priority under Win32 */
+	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
+	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
+#endif
+#endif
+#endif 
+}
+
+
+
+/*
+ * Resize old src and dst arrays. Also check for problems that require full
+ * redraws, rather than using the unchanged pixel buffering.  If the ScummVM
+ * backend evolves to draw even more things outside the filter than it
+ * currently does, some more checks may need to be added, or external flags
+ * set in the backend to tell the filter to redraw everything without using
+ * the unchanged pixel buffers.  The current checks seem to catch everything
+ * for now....
+ *
+ */
+void resize_old_arrays(const uint8 *src, uint8 *dst,
+		int w, int h, int scale)
+{
+	int w2, h2;
+	int32 size, max_scaled_size;
+	const uint16 *sptr16 = (const uint16 *) src;
+	int overlay_flag = 0;
+	int skip_unchanged_pixels_flag;
+
+	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
+		overlay_flag = 1;
+
+	if (scale > max_scale) max_scale = scale;
+
+	/* Deal with overlays */
+	if (overlay_flag)
+	{
+		skip_unchanged_pixels_flag = 1;
+
+		w2 = w + 2;
+		h2 = h + 2;
+		size = w2 * h2;
+
+		if (size > max_overlay_size)
+		{
+			max_overlay_size = size;
+			old_overlay = (uint16 *) realloc(old_overlay,
+					size * sizeof(uint16));
+
+			skip_unchanged_pixels_flag = 0;
+		}
+
+		max_scaled_size = max_scale * max_scale * max_overlay_size;
+
+		if (max_scaled_size > max_dst_overlay_size)
+		{
+			max_dst_overlay_size = max_scaled_size;
+			old_dst_overlay = (uint16 *) realloc(old_dst_overlay,
+					max_scaled_size * sizeof(uint16));
+
+			skip_unchanged_pixels_flag = 0;
+		}
+
+		cur_overlay_width = w;
+		cur_overlay_height = h;
+		cur_dst_overlay_width = w * scale;
+		cur_dst_overlay_height = h * scale;
+		if (cur_overlay_width != old_overlay_width ||
+				cur_overlay_height != old_overlay_height ||
+				cur_dst_overlay_width != old_dst_overlay_width ||
+				cur_dst_overlay_height != old_dst_overlay_height)
+		{
+			skip_unchanged_pixels_flag = 0;
+		}
+		old_overlay_width = cur_overlay_width;
+		old_overlay_height = cur_overlay_height;
+		old_dst_overlay_width = cur_dst_overlay_width;
+		old_dst_overlay_height = cur_dst_overlay_height;
+
+		if (skip_unchanged_pixels_flag == 0)
+		{
+			memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
+			memset(old_dst_overlay, 0, max_dst_overlay_size * sizeof(uint16));
+		}
+	}
+	else
+	{
+		w2 = cur_screen_width + 2;
+		h2 = cur_screen_height + 2;
+		size = w2 * h2;
+	}
+
+	skip_unchanged_pixels_flag = 1;
+
+	if (overlay_flag == 0 && size > max_old_src_size)
+	{
+		max_old_src_size = size;
+		old_src = (uint16 *) realloc(old_src, size * sizeof(uint16));
+
+		skip_unchanged_pixels_flag = 0;
+	}
+
+	max_scaled_size = max_scale * max_scale * max_old_src_size;
+
+	if (overlay_flag == 0 && max_scaled_size > max_old_dst_size)
+	{
+		max_old_dst_size = max_scaled_size;
+		old_dst = (uint16 *) realloc(old_dst,
+				max_scaled_size * sizeof(uint16));
+
+		skip_unchanged_pixels_flag = 0;
+	}
+
+	/* screen dimensions have changed */
+	if (cur_screen_width != old_screen_width ||
+			cur_screen_height != old_screen_height)
+	{
+		skip_unchanged_pixels_flag = 0;
+	}
+	old_screen_width = cur_screen_width;
+	old_screen_height = cur_screen_height;
+
+	cur_dst_screen_width = cur_screen_width * scale;
+	cur_dst_screen_height = cur_screen_height * scale;
+	if (cur_dst_screen_width != old_dst_screen_width ||
+			cur_dst_screen_height != old_dst_screen_height)
+	{
+		skip_unchanged_pixels_flag = 0;
+	}
+	old_dst_screen_width = cur_dst_screen_width;
+	old_dst_screen_height = cur_dst_screen_height;
+
+	/* set all the buffers to 0, so that everything gets redrawn */
+	if (skip_unchanged_pixels_flag == 0)
+	{
+		memset(old_src, 0, max_old_src_size * sizeof(uint16));
+		memset(old_dst, 0, max_old_dst_size * sizeof(uint16));
+		memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
+		memset(old_dst_overlay, 0, max_dst_overlay_size * sizeof(uint16));
+	}
+}
+
+
+
+/* Fill old src array, which is used in checking for unchanged pixels */
+void fill_old_src(const uint8 *src, int srcPitch, int w, int h)
+{
+	int x, y;
+	int x2, y2;
+	const uint16 *sptr16 = (const uint16 *) src;
+	const uint16 *sptr2;
+	uint16 *optr16;
+	int32 screen_width2 = cur_screen_width + 2;
+	int32 dist = src - (const uint8 *) src_addr_min;
+	int32 x_fudge;
+	int32 src_fudge;
+
+	/* Deal with overlays */
+	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
+	{
+		w += 2;
+		h += 2;
+
+		optr16 = old_overlay;
+		sptr2 = (const uint16 *) (src - srcPitch) - 1;
+
+		x_fudge = 0;
+		src_fudge = srcPitch - w - w;
+	}
+	else
+	{
+		y = dist / srcPitch;
+		x = (dist - y * srcPitch) >> 1;
+
+		optr16 = old_src + (y + 1) * screen_width2 + x + 1;
+		sptr2 = (const uint16 *) src;
+
+		x_fudge = screen_width2 - w;
+		src_fudge = srcPitch - w - w;
+	}
+
+	for (y2 = 0; y2 < h; y2++)
+	{
+		for (x2 = 0; x2 < w; x2++)
+			*optr16++ = *sptr2++;
+
+		optr16 += x_fudge;
+		sptr2 = (const uint16 *) ((const uint8 *) sptr2 + src_fudge);
+	}
+}
+
+
+
+/* Fill old dst array, which is used in drawing unchanged pixels */
+void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
+		int w, int h, int scale)
+{
+	int x, y;
+	int x2, y2;
+
+	const uint16 *sptr16 = (const uint16 *) src;
+	uint16 *dptr2;
+	uint16 *optr16;
+	int32 screen_width_scaled = cur_screen_width * scale;
+	int32 dist;
+	int w_new = w * scale;
+	int h_new = h * scale;
+	int32 x_fudge;
+	int32 dst_fudge;
+
+	/* Deal with overlays */
+	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
+	{
+		optr16 = old_dst_overlay;
+		dptr2 = (uint16 *) dst;
+		x_fudge = 0;
+		dst_fudge = dstPitch - w_new - w_new;
+	}
+	else
+	{
+		dist = src - (const uint8 *) src_addr_min;
+		y = dist / srcPitch;
+		x = (dist - y * srcPitch) >> 1;
+
+		optr16 = old_dst + scale * (y * screen_width_scaled + x);
+		dptr2 = (uint16 *) dst;
+
+		x_fudge = screen_width_scaled - w_new;
+		dst_fudge = dstPitch - w_new - w_new;
+	}
+
+	for (y2 = 0; y2 < h_new; y2++)
+	{
+		for (x2 = 0; x2 < w_new; x2++)
+			*optr16++ = *dptr2++;
+
+		optr16 += x_fudge;
+		dptr2 = (uint16 *) ((uint8 *) dptr2 + dst_fudge);
+	}
+}
+
+
+
+/* 3x anti-aliased resize filter, nearest-neighbor anti-aliasing */
+void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
+		uint8 *dstPtr, uint32 dstPitch, int width, int height)
+{
+	/* Initialize stuff */
+	if (!init_flag)
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+
+		/* set initial best guess on min/max screen addresses */
+		/* indent by 1 in case only the mouse is ever drawn */
+		src_addr_min = (const uint16 *) srcPtr + 1;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
+					srcPitch)) + (width - 1) - 1;
+
+		init_tables(srcPtr, srcPitch, width, height);
+		init_flag = 1;
+	}
+
+	/* Uh oh, the screen size has changed */
+	if (cur_screen_width != g_system->getWidth() ||
+			cur_screen_height != g_system->getHeight())
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
+	   src array for later bounds checking */
+	if (width == g_system->getWidth() &&
+			height == g_system->getHeight())
+	{
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* resize and/or blank the old src and dst array */
+	resize_old_arrays(srcPtr, dstPtr, width, height, 3);
+
+	/* Hmm, the src address isn't within the proper bounds.
+	 * We're probably drawing an overlay now.
+	 */
+	if ((const uint16 *) srcPtr < src_addr_min ||
+			(const uint16 *) srcPtr > src_addr_max)
+	{
+		anti_alias_pass_3x(srcPtr, dstPtr, width, height,
+				3*width, 3*height, srcPitch, dstPitch, 1);
+	}
+	else   /* Draw the regular screen, this isn't an overlay. */
+	{
+		anti_alias_pass_3x(srcPtr, dstPtr, width, height,
+				3*width, 3*height, srcPitch, dstPitch, 0);
+	}
+
+	/* fill old src array */
+	fill_old_src(srcPtr, srcPitch, width, height);
+	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 3);
+}
+
+
+
+/* 2x anti-aliased resize filter, nearest-neighbor anti-aliasing */
+void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
+		uint8 *dstPtr, uint32 dstPitch, int width, int height)
+{
+	/* Initialize stuff */
+	if (!init_flag)
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+
+		/* set initial best guess on min/max screen addresses */
+		/* indent by 1 in case only the mouse is ever drawn */
+		src_addr_min = (const uint16 *) srcPtr + 1;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
+					srcPitch)) + (width - 1) - 1;
+
+		init_tables(srcPtr, srcPitch, width, height);
+		init_flag = 1;
+	}
+
+	/* Uh oh, the screen size has changed */
+	if (cur_screen_width != g_system->getWidth() ||
+			cur_screen_height != g_system->getHeight())
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
+	   src array for later bounds checking */
+	if (width == g_system->getWidth() &&
+			height == g_system->getHeight())
+	{
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* resize and/or blank the old src and dst array */
+	resize_old_arrays(srcPtr, dstPtr, width, height, 2);
+
+	/* Hmm, the src address isn't within the proper bounds.
+	 * We're probably drawing an overlay now.
+	 */
+	if ((const uint16 *) srcPtr < src_addr_min ||
+			(const uint16 *) srcPtr > src_addr_max)
+	{
+		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+				2*width, 2*height, srcPitch, dstPitch, 1, 0);
+	}
+	else   /* Draw the regular screen, this isn't an overlay. */
+	{
+		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+				2*width, 2*height, srcPitch, dstPitch, 0, 0);
+	}
+
+	/* fill old src array */
+	fill_old_src(srcPtr, srcPitch, width, height);
+	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
+}
+
+
+
+/*
+ * 2x anti-aliased resize filter, interpolation based anti-aliasing.
+ * This is useful prior to upsizing with the 1.5x filter for the menu
+ * overlay.  Nearest-neighbor looks bad with 1.5x resize.  Interpolated gives
+ * results that look good, like a somewhat blurry Edge3x.
+ *
+ */
+void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
+		uint8 *dstPtr, uint32 dstPitch, int width, int height)
+{
+	/* Initialize stuff */
+	if (!init_flag)
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+
+		/* set initial best guess on min/max screen addresses */
+		/* indent by 1 in case only the mouse is ever drawn */
+		src_addr_min = (const uint16 *) srcPtr + 1;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
+					srcPitch)) + (width - 1) - 1;
+
+		init_tables(srcPtr, srcPitch, width, height);
+		init_flag = 1;
+	}
+
+	/* Uh oh, the screen size has changed */
+	if (cur_screen_width != g_system->getWidth() ||
+			cur_screen_height != g_system->getHeight())
+	{
+		cur_screen_width = g_system->getWidth();
+		cur_screen_height = g_system->getHeight();
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
+	   src array for later bounds checking */
+	if (width == g_system->getWidth() &&
+			height == g_system->getHeight())
+	{
+		src_addr_min = (const uint16 *) srcPtr;
+		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
+			(width - 1);
+	}
+
+	/* resize and/or blank the old src and dst array */
+	resize_old_arrays(srcPtr, dstPtr, width, height, 2);
+
+	/* Hmm, the src address isn't within the proper bounds.
+	 * We're probably drawing an overlay now.
+	 */
+	if ((const uint16 *) srcPtr < src_addr_min ||
+			(const uint16 *) srcPtr > src_addr_max)
+	{
+		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+				2*width, 2*height, srcPitch, dstPitch, 1, 0);
+	}
+	else   /* Draw the regular screen, this isn't an overlay. */
+	{
+		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+				2*width, 2*height, srcPitch, dstPitch, 0, 1);
+	}
+
+	/* fill old src array */
+	fill_old_src(srcPtr, srcPitch, width, height);
+	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
+}
+
+EdgePlugin::EdgePlugin() {
+	_factor = 2;
+	_factors.push_back(2);
+	_factors.push_back(3);
+}
+
+void EdgePlugin::initialize(Graphics::PixelFormat format) {
+	_format = format;
+	init_tables(0, 0, 0, 0);
+}
+
+void EdgePlugin::deinitialize() {
+}
+
+void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+		uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+	if (_format.bytesPerPixel == 2) {
+		if (_factor == 2)
+			anti_alias_pass_2x(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+		else
+			anti_alias_pass_3x(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+	} else {
+		warning("FIXME: EdgePlugin 32bpp format");
+	}
+}
+
+uint EdgePlugin::increaseFactor() {
+	if (_factor == 2)
+		++_factor;
+	return _factor;
+}
+
+uint EdgePlugin::decreaseFactor() {
+	if (_factor == 3)
+		--_factor;
+	return _factor;
+}
+
+const char *EdgePlugin::getName() const {
+	return "edge";
+}
+
+const char *EdgePlugin::getPrettyName() const {
+	return "Edge";
+}
+
+REGISTER_PLUGIN_STATIC(EDGE, PLUGIN_TYPE_SCALER, EdgePlugin);
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
new file mode 100644
index 0000000000..5bb6eade67
--- /dev/null
+++ b/graphics/scaler/edge.h
@@ -0,0 +1,44 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GRAPHICS_SCALER_EDGE_H
+#define GRAPHICS_SCALER_EDGE_H
+
+#include "graphics/scalerplugin.h"
+
+class EdgePlugin : public ScalerPluginObject {
+public:
+	EdgePlugin();
+	virtual void initialize(Graphics::PixelFormat format);
+	virtual void deinitialize();
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual uint increaseFactor();
+	virtual uint decreaseFactor();
+	virtual uint getFactor() const { return _factor; }
+	virtual bool canDrawCursor() const { return false; }
+	virtual uint extraPixels() const { return 1; }
+	virtual const char *getName() const;
+	virtual const char *getPrettyName() const;
+};
+
+
+#endif


Commit: e77934b73d1f70ec754edcd12a6c516bca9b389c
    https://github.com/scummvm/scummvm/commit/e77934b73d1f70ec754edcd12a6c516bca9b389c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Change Edge filter to use shared interpolation functions

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/intern.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 7b8e77ef4f..21ce190fc3 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -23,9 +23,6 @@
  *
  * Author: Eric A. Welsh
  *
- * INTERPOLATE/Q_INTERPOLATE macros taken from HQ2x/HQ3x scalers
- *  (Authors: Maxim Stepin and Max Horn)
- * 
  *
  * Sharp, clean, anti-aliased image with very few artifacts.
  * Detects and appropriately handles mouse overlays with transparent pixels.
@@ -97,6 +94,7 @@
 #include <math.h>
 #include "common/scummsys.h"
 #include "common/system.h"
+#include "graphics/scaler/intern.h"
 #include "graphics/scaler/edge.h"
 
 /* Randomly XORs one of 2x2 or 3x3 resized pixels in order to indicate
@@ -168,7 +166,9 @@ const uint16 *src_addr_max = NULL;	/* end of src screen array */
 
 const int16 int32_sqrt3 = (int16) (((int16)1<<GREY_SHIFT) * sqrt(3.0) + 0.5);
 
-
+#define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
+#define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
+#define interpolate_1_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1<ColorMask>(a,b,c) : interpolate32_1_1_1<ColorMask>(a,b,c))
 
 #if DEBUG_REFRESH_RANDOM_XOR
 /* Random number generators with very good randomness properties, far better
@@ -1939,59 +1939,6 @@ int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
 #define redblueMask	0xF81F
 #define greenMask	0x07E0
 
-/* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
-/**
- * Interpolate two 16 bit pixel pairs at once with equal weights 1.
- * In particular, A and B can contain two pixels/each in the upper
- * and lower halves.
- */
-uint32 INTERPOLATE(uint32 A, uint32 B)
-{
-	return (((A & highBits) >> 1) + ((B & highBits) >> 1) + (A & B & lowBits));
-}
-
-/* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
-/**
- * Interpolate four 16 bit pixel pairs at once with equal weights 1.
- * In particular, A and B can contain two pixels/each in the upper
- * and lower halves.
- */
-uint32 Q_INTERPOLATE(uint32 A, uint32 B, uint32 C, uint32 D)
-{
-	uint32 x = ((A & qhighBits) >> 2) + ((B & qhighBits) >> 2) + ((C & qhighBits) >> 2) + ((D & qhighBits) >> 2);
-	uint32 y = ((A & qlowBits) + (B & qlowBits) + (C & qlowBits) + (D & qlowBits)) >> 2;
-
-	y &= qlowBits;
-	return x + y;
-}
-
-
-
-/* Average three pixels together */
-uint16 average_three_pixels(uint16 pixel1, uint16 pixel2, uint16 pixel3)
-{
-	uint32 rsum;
-	uint16 gsum, bsum;
-
-	rsum =  (pixel1 & 0xF800);
-	rsum += (pixel2 & 0xF800);
-	rsum += (pixel3 & 0xF800);
-	rsum = div3[rsum >> 11];
-
-	gsum =  (pixel1 & 0x07E0);
-	gsum += (pixel2 & 0x07E0);
-	gsum += (pixel3 & 0x07E0);
-	gsum = div3[gsum >> 5];
-
-	bsum =  (pixel1 & 0x001F);
-	bsum += (pixel2 & 0x001F);
-	bsum += (pixel3 & 0x001F);
-	bsum = div3[bsum];
-
-	return ((rsum << 11) | (gsum << 5) | bsum);
-}
-
-
 
 /* Interpolate 1/3rd of the way between two pixels */
 uint16 average_one_third(uint16 pixel1, uint16 pixel2)
@@ -2017,6 +1964,7 @@ uint16 average_one_third(uint16 pixel1, uint16 pixel2)
 
 
 /* Fill pixel grid without interpolation, using the detected edge */
+template<typename ColorMask>
 void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr)
 {
@@ -2034,7 +1982,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[6]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -2068,7 +2016,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -2106,7 +2054,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 16)
 			{
-				tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+				tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
@@ -2120,7 +2068,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 17)
 			{
-				tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+				tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
@@ -2138,7 +2086,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2172,7 +2120,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[6]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2206,7 +2154,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2240,7 +2188,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[8]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2278,7 +2226,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 18)
 			{
-				tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
@@ -2292,7 +2240,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 19)
 			{
-				tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+				tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
@@ -2310,7 +2258,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[8]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -2344,7 +2292,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -2378,7 +2326,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2400,7 +2348,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 					tmp[0] = pixels[4];
 			}
 
-			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[6]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -2428,7 +2376,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -2450,7 +2398,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 					tmp[2] = pixels[4];
 			}
 
-			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[8]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2478,7 +2426,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2500,7 +2448,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 					tmp[0] = pixels[4];
 			}
 
-			tmp[2] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -2528,7 +2476,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			for (i = 0; i < 9; i++)
 				tmp[i] = center;
 
-			tmp[6] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[6]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -2550,7 +2498,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 					tmp[6] = pixels[4];
 			}
 
-			tmp[8] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[8]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2623,6 +2571,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 
 /* Fill pixel grid with or without interpolation, using the detected edge */
+template<typename ColorMask>
 void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
@@ -2640,7 +2589,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 1:		/* '- */
 			tmp[0] = tmp[1] = tmp[3] = center;
 
-			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -2669,16 +2618,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[2];
-						tmp[2] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[3] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[2] = interpolate_3_1(tmp_pixel, center);
+						tmp[3] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[2] = INTERPOLATE(tmp[2], center);
+						tmp[2] = interpolate_1_1(tmp[2], center);
 					}
 					else
 					{
@@ -2693,7 +2641,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 2:		/* -. */
 			tmp[0] = tmp[2] = tmp[3] = center;
 
-			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[1]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -2722,16 +2670,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[1];
-						tmp[1] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[0] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[1] = interpolate_3_1(tmp_pixel, center);
+						tmp[0] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[1] = INTERPOLATE(tmp[1], center);
+						tmp[1] = interpolate_1_1(tmp[1], center);
 					}
 					else
 					{
@@ -2750,7 +2697,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 16)
 			{
-				tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+				tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
@@ -2763,7 +2710,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 				if (interpolate_2x)
 				{
-					tmp[1] = INTERPOLATE(tmp[1], center);
+					tmp[1] = interpolate_1_1(tmp[1], center);
 				}
 				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
@@ -2777,7 +2724,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 17)
 			{
-				tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+				tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
@@ -2790,7 +2737,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 				if (interpolate_2x)
 				{
-					tmp[2] = INTERPOLATE(tmp[2], center);
+					tmp[2] = interpolate_1_1(tmp[2], center);
 				}
 				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
@@ -2807,7 +2754,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 4:		/* '| */
 			tmp[0] = tmp[2] = tmp[3] = center;
 
-			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[1]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2836,16 +2783,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[1];
-						tmp[1] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[3] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[1] = interpolate_3_1(tmp_pixel, center);
+						tmp[3] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[1] = INTERPOLATE(tmp[1], center);
+						tmp[1] = interpolate_1_1(tmp[1], center);
 					}
 					else
 					{
@@ -2860,7 +2806,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 5:		/* |. */
 			tmp[0] = tmp[1] = tmp[3] = center;
 
-			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2889,16 +2835,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[2];
-						tmp[2] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[0] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[2] = interpolate_3_1(tmp_pixel, center);
+						tmp[0] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[2] = INTERPOLATE(tmp[2], center);
+						tmp[2] = interpolate_1_1(tmp[2], center);
 					}
 					else
 					{
@@ -2913,7 +2858,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 7:		/* |' */
 			tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -2942,16 +2887,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[0];
-						tmp[0] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[2] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[0] = interpolate_3_1(tmp_pixel, center);
+						tmp[2] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[0] = INTERPOLATE(tmp[0], center);
+						tmp[0] = interpolate_1_1(tmp[0], center);
 					}
 					else
 					{
@@ -2966,7 +2910,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 8:		/* .| */
 			tmp[0] = tmp[1] = tmp[2] = center;
 
-			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[3]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -2995,16 +2939,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[3];
-						tmp[3] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[1] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[3] = interpolate_3_1(tmp_pixel, center);
+						tmp[1] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[3] = INTERPOLATE(tmp[3], center);
+						tmp[3] = interpolate_1_1(tmp[3], center);
 					}
 					else
 					{
@@ -3023,7 +2966,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 18)
 			{
-				tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
@@ -3036,7 +2979,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 				if (interpolate_2x)
 				{
-					tmp[0] = INTERPOLATE(tmp[0], center);
+					tmp[0] = interpolate_1_1(tmp[0], center);
 				}
 				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
@@ -3050,7 +2993,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 			if (sub_type != 19)
 			{
-				tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+				tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
 				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
 				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
 				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
@@ -3063,7 +3006,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 				if (interpolate_2x)
 				{
-					tmp[3] = INTERPOLATE(tmp[3], center);
+					tmp[3] = interpolate_1_1(tmp[3], center);
 				}
 				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
@@ -3080,7 +3023,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 10:	/* -' */
 			tmp[0] = tmp[1] = tmp[2] = center;
 
-			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[3]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -3109,16 +3052,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[3];
-						tmp[3] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[2] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[3] = interpolate_3_1(tmp_pixel, center);
+						tmp[2] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[3] = INTERPOLATE(tmp[3], center);
+						tmp[3] = interpolate_1_1(tmp[3], center);
 					}
 					else
 					{
@@ -3133,7 +3075,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 11:	/* .- */
 			tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -3162,16 +3104,15 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						uint16 tmp_pixel = tmp[0];
-						tmp[0] = Q_INTERPOLATE(tmp_pixel, tmp_pixel,
-								tmp_pixel, center);
-						tmp[1] = Q_INTERPOLATE(center, center, center, tmp_pixel);
+						tmp[0] = interpolate_3_1(tmp_pixel, center);
+						tmp[1] = interpolate_3_1(center, tmp_pixel);
 					}
 				}
 				else
 				{
 					if (interpolate_2x)
 					{
-						tmp[0] = INTERPOLATE(tmp[0], center);
+						tmp[0] = interpolate_1_1(tmp[0], center);
 					}
 					else
 					{
@@ -3186,7 +3127,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 12:	/* < */
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -3212,7 +3153,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[0] = INTERPOLATE(center, tmp[0]);
+						tmp[0] = interpolate_1_1(center, tmp[0]);
 						tmp[2] = average_one_third(center, tmp[0]);
 					}
 					else
@@ -3230,7 +3171,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = center;
 			}
 
-			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -3256,7 +3197,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[2] = INTERPOLATE(center, tmp[2]);
+						tmp[2] = interpolate_1_1(center, tmp[2]);
 						tmp[0] = average_one_third(center, tmp[2]);
 					}
 					else
@@ -3279,7 +3220,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 13:	/* > */
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[1]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -3305,7 +3246,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[1] = INTERPOLATE(center, tmp[1]);
+						tmp[1] = interpolate_1_1(center, tmp[1]);
 						tmp[3] = average_one_third(center, tmp[1]);
 					}
 					else
@@ -3323,7 +3264,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[1] = center;
 			}
 
-			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[3]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -3349,7 +3290,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[3] = INTERPOLATE(center, tmp[3]);
+						tmp[3] = interpolate_1_1(center, tmp[3]);
 						tmp[1] = average_one_third(center, tmp[3]);
 					}
 					else
@@ -3372,7 +3313,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 14:	/* ^ */
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = average_three_pixels(pixels[1], pixels[3], center);
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
 			tmp_grey = chosen_greyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
@@ -3398,7 +3339,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[0] = INTERPOLATE(center, tmp[0]);
+						tmp[0] = interpolate_1_1(center, tmp[0]);
 						tmp[1] = average_one_third(center, tmp[0]);
 					}
 					else
@@ -3416,7 +3357,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = center;
 			}
 
-			tmp[1] = average_three_pixels(pixels[1], pixels[5], center);
+			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
 			tmp_grey = chosen_greyscale[tmp[1]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
@@ -3442,7 +3383,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[1] = INTERPOLATE(center, tmp[1]);
+						tmp[1] = interpolate_1_1(center, tmp[1]);
 						tmp[0] = average_one_third(center, tmp[1]);
 					}
 					else
@@ -3465,7 +3406,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		case 15:	/* v */
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[2] = average_three_pixels(pixels[3], pixels[7], center);
+			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
@@ -3491,7 +3432,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[2] = INTERPOLATE(center, tmp[2]);
+						tmp[2] = interpolate_1_1(center, tmp[2]);
 						tmp[3] = average_one_third(center, tmp[2]);
 					}
 					else
@@ -3509,7 +3450,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[2] = center;
 			}
 
-			tmp[3] = average_three_pixels(pixels[5], pixels[7], center);
+			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
 			tmp_grey = chosen_greyscale[tmp[3]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
@@ -3535,7 +3476,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				{
 					if (interpolate_2x)
 					{
-						tmp[3] = INTERPOLATE(center, tmp[3]);
+						tmp[3] = interpolate_1_1(center, tmp[3]);
 						tmp[2] = average_one_third(center, tmp[3]);
 					}
 					else
@@ -3764,6 +3705,7 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 
 
 /* Perform edge detection, draw the new 3x pixels */
+template<typename ColorMask>
 void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
@@ -3873,7 +3815,7 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 			/* block of solid color */
 			if (!diffs)
 			{
-				anti_alias_grid_clean_3x((uint8 *) dptr16, dstPitch, pixels,
+				anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 						0, NULL);
 				continue;
 			}
@@ -3894,7 +3836,7 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fix_knights(sub_type, pixels, sim);
 
-			anti_alias_grid_clean_3x((uint8 *) dptr16, dstPitch, pixels,
+			anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 					sub_type, bplane);
 		}
 	}
@@ -3903,6 +3845,7 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 
 
 /* Perform edge detection, draw the new 2x pixels */
+template<typename ColorMask>
 void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
@@ -4014,7 +3957,7 @@ void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 			/* block of solid color */
 			if (!diffs)
 			{
-				anti_alias_grid_2x((uint8 *) dptr16, dstPitch, pixels,
+				anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 						0, NULL, NULL, 0);
 				continue;
 			}
@@ -4035,7 +3978,7 @@ void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fix_knights(sub_type, pixels, sim);
 
-			anti_alias_grid_2x((uint8 *) dptr16, dstPitch, pixels,
+			anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 					sub_type, bplane, sim,
 					interpolate_2x);
 		}
@@ -4376,6 +4319,7 @@ void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
 
 
 /* 3x anti-aliased resize filter, nearest-neighbor anti-aliasing */
+#if 0
 void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 		uint8 *dstPtr, uint32 dstPitch, int width, int height)
 {
@@ -4438,10 +4382,12 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 	fill_old_src(srcPtr, srcPitch, width, height);
 	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 3);
 }
+#endif
 
 
 
 /* 2x anti-aliased resize filter, nearest-neighbor anti-aliasing */
+#if 0
 void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 		uint8 *dstPtr, uint32 dstPitch, int width, int height)
 {
@@ -4504,6 +4450,7 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 	fill_old_src(srcPtr, srcPitch, width, height);
 	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
 }
+#endif
 
 
 
@@ -4514,6 +4461,7 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
  * results that look good, like a somewhat blurry Edge3x.
  *
  */
+#if 0
 void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 		uint8 *dstPtr, uint32 dstPitch, int width, int height)
 {
@@ -4576,6 +4524,7 @@ void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 	fill_old_src(srcPtr, srcPitch, width, height);
 	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
 }
+#endif
 
 EdgePlugin::EdgePlugin() {
 	_factor = 2;
@@ -4594,10 +4543,17 @@ void EdgePlugin::deinitialize() {
 void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
-		if (_factor == 2)
-			anti_alias_pass_2x(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
-		else
-			anti_alias_pass_3x(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+		if (_factor == 2) {
+			if (_format.gLoss == 2)
+				anti_alias_pass_2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+			else
+				anti_alias_pass_2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+		} else {
+			if (_format.gLoss == 2)
+				anti_alias_pass_3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+			else
+				anti_alias_pass_3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+		}
 	} else {
 		warning("FIXME: EdgePlugin 32bpp format");
 	}
diff --git a/graphics/scaler/intern.h b/graphics/scaler/intern.h
index dbe68a84ed..2a0e1f4d2f 100644
--- a/graphics/scaler/intern.h
+++ b/graphics/scaler/intern.h
@@ -288,6 +288,33 @@ uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3) {
 	return (rsum & ColorMask::kRedMask) | (gsum & ColorMask::kGreenMask) | (bsum & ColorMask::kBlueMask);
 }
 
+/**
+ * Interpolate three 32 bit pixels with weights 1, 1, and 1, i.e., (p1+p2+p3)/3.
+ */
+
+template<typename ColorMask>
+uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3)
+{
+	uint32 rsum, gsum, bsum;
+
+	rsum =  (pixel1 & ColorMask::kRedMask);
+	rsum += (pixel2 & ColorMask::kRedMask);
+	rsum += (pixel3 & ColorMask::kRedMask);
+	rsum /= 3;
+
+	gsum =  (pixel1 & ColorMask::kGreenMask);
+	gsum += (pixel2 & ColorMask::kGreenMask);
+	gsum += (pixel3 & ColorMask::kGreenMask);
+	gsum /= 3;
+
+	bsum =  (pixel1 & ColorMask::kBlueMask);
+	bsum += (pixel2 & ColorMask::kBlueMask);
+	bsum += (pixel3 & ColorMask::kBlueMask);
+	bsum /= 3;
+
+	return (rsum & ColorMask::kRedMask) | (gsum & ColorMask::kGreenMask) | (bsum & ColorMask::kBlueMask);
+}
+
 /**
  * Interpolate four 32 bit pixels with weights 1, 1, 1, and 1, i.e., (p1+p2+p3+p4)/4.
  *


Commit: 457e974c86e2e50ce4e60864cfa853aed16562f4
    https://github.com/scummvm/scummvm/commit/457e974c86e2e50ce4e60864cfa853aed16562f4
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Use correct copyright notice in edge.cpp

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 21ce190fc3..0d802846c0 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -1,20 +1,22 @@
-/* ScummVM - Scumm Interpreter
- * Copyright (C) 2001  Ludvig Strigeus
- * Copyright (C) 2001-2006 The ScummVM project
+/* 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 2
  * 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  */
 


Commit: fc3ee1989dfc14d0cbf0bb60325a981859375d17
    https://github.com/scummvm/scummvm/commit/fc3ee1989dfc14d0cbf0bb60325a981859375d17
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Change another interpolation function in Edge filter.

A 2 to 1 interpolation was causing errors. It is fixed, renamed, and
moved to the common interpolation file.

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 0d802846c0..0a2d6852a2 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -170,6 +170,7 @@ const int16 int32_sqrt3 = (int16) (((int16)1<<GREY_SHIFT) * sqrt(3.0) + 0.5);
 
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
 #define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
+#define interpolate_2_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_2_1<ColorMask>(a,b) : interpolate32_2_1<ColorMask>(a,b))
 #define interpolate_1_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1<ColorMask>(a,b,c) : interpolate32_1_1_1<ColorMask>(a,b,c))
 
 #if DEBUG_REFRESH_RANDOM_XOR
@@ -1941,30 +1942,6 @@ int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
 #define redblueMask	0xF81F
 #define greenMask	0x07E0
 
-
-/* Interpolate 1/3rd of the way between two pixels */
-uint16 average_one_third(uint16 pixel1, uint16 pixel2)
-{
-	uint32 rsum;
-	uint16 gsum, bsum;
-
-	rsum =  (pixel1 & 0xF800) << 1;
-	rsum += (pixel2 & 0xF800);
-	rsum = div3[rsum >> 11];
-
-	gsum =  (pixel1 & 0x07E0) << 1;
-	gsum += (pixel2 & 0x07E0);
-	gsum = div3[gsum >> 5];
-
-	bsum =  (pixel1 & 0x001F) << 1;
-	bsum += (pixel2 & 0x001F);
-	bsum = div3[bsum];
-
-	return ((rsum << 11) | (gsum << 5) | bsum);
-}
-
-
-
 /* Fill pixel grid without interpolation, using the detected edge */
 template<typename ColorMask>
 void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
@@ -3156,7 +3133,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[0] = interpolate_1_1(center, tmp[0]);
-						tmp[2] = average_one_third(center, tmp[0]);
+						tmp[2] = interpolate_2_1(center, tmp[0]);
 					}
 					else
 					{
@@ -3168,7 +3145,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[0] = average_one_third(center, tmp[0]);
+					tmp[0] = interpolate_2_1(center, tmp[0]);
 				else
 					tmp[0] = center;
 			}
@@ -3200,7 +3177,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[2] = interpolate_1_1(center, tmp[2]);
-						tmp[0] = average_one_third(center, tmp[2]);
+						tmp[0] = interpolate_2_1(center, tmp[2]);
 					}
 					else
 					{
@@ -3212,7 +3189,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[2] = average_one_third(center, tmp[2]);
+					tmp[2] = interpolate_2_1(center, tmp[2]);
 				else
 					tmp[2] = center;
 			}
@@ -3249,7 +3226,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[1] = interpolate_1_1(center, tmp[1]);
-						tmp[3] = average_one_third(center, tmp[1]);
+						tmp[3] = interpolate_2_1(center, tmp[1]);
 					}
 					else
 					{
@@ -3261,7 +3238,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[1] = average_one_third(center, tmp[1]);
+					tmp[1] = interpolate_2_1(center, tmp[1]);
 				else
 					tmp[1] = center;
 			}
@@ -3293,7 +3270,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[3] = interpolate_1_1(center, tmp[3]);
-						tmp[1] = average_one_third(center, tmp[3]);
+						tmp[1] = interpolate_2_1(center, tmp[3]);
 					}
 					else
 					{
@@ -3305,7 +3282,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[3] = average_one_third(center, tmp[3]);
+					tmp[3] = interpolate_2_1(center, tmp[3]);
 				else
 					tmp[3] = center;
 			}
@@ -3342,7 +3319,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[0] = interpolate_1_1(center, tmp[0]);
-						tmp[1] = average_one_third(center, tmp[0]);
+						tmp[1] = interpolate_2_1(center, tmp[0]);
 					}
 					else
 					{
@@ -3354,7 +3331,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[0] = average_one_third(center, tmp[0]);
+					tmp[0] = interpolate_2_1(center, tmp[0]);
 				else
 					tmp[0] = center;
 			}
@@ -3386,7 +3363,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[1] = interpolate_1_1(center, tmp[1]);
-						tmp[0] = average_one_third(center, tmp[1]);
+						tmp[0] = interpolate_2_1(center, tmp[1]);
 					}
 					else
 					{
@@ -3398,7 +3375,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[1] = average_one_third(center, tmp[1]);
+					tmp[1] = interpolate_2_1(center, tmp[1]);
 				else
 					tmp[1] = center;
 			}
@@ -3435,7 +3412,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[2] = interpolate_1_1(center, tmp[2]);
-						tmp[3] = average_one_third(center, tmp[2]);
+						tmp[3] = interpolate_2_1(center, tmp[2]);
 					}
 					else
 					{
@@ -3447,7 +3424,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[2] = average_one_third(center, tmp[2]);
+					tmp[2] = interpolate_2_1(center, tmp[2]);
 				else
 					tmp[2] = center;
 			}
@@ -3479,7 +3456,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					if (interpolate_2x)
 					{
 						tmp[3] = interpolate_1_1(center, tmp[3]);
-						tmp[2] = average_one_third(center, tmp[3]);
+						tmp[2] = interpolate_2_1(center, tmp[3]);
 					}
 					else
 					{
@@ -3491,7 +3468,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				}
 
 				if (interpolate_2x)
-					tmp[3] = average_one_third(center, tmp[3]);
+					tmp[3] = interpolate_2_1(center, tmp[3]);
 				else
 					tmp[3] = center;
 			}


Commit: b2dff9413e2b73b0093ea9fa327aef893b1af278
    https://github.com/scummvm/scummvm/commit/b2dff9413e2b73b0093ea9fa327aef893b1af278
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Rename functions in Edge filter according to guidelines

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 0a2d6852a2..acbe586147 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -354,7 +354,7 @@ double fast_atan(double x0)
  * bitplanes.  The increase in image quality is well worth the speed hit.
  *
  */
-int16 * choose_greyscale(uint16 *pixels)
+int16 * chooseGreyscale(uint16 *pixels)
 {
 	static int16 greyscale_diffs[3][8];
 	static int16 bplanes[3][9];
@@ -439,7 +439,7 @@ int16 * choose_greyscale(uint16 *pixels)
  * useful results.
  *
  */
-int32 calc_pixel_diff_nosqrt(uint16 pixel1, uint16 pixel2)
+int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
 {
 
 #if 1	/* distance between pixels, weighted by roughly luma proportions */
@@ -531,7 +531,7 @@ int32 calc_pixel_diff_nosqrt(uint16 pixel1, uint16 pixel2)
  * since everything I have tried has lead to slight mis-detection errors.
  *
  */
-int find_principle_axis(uint16 *pixels, int16 *diffs, int16 *bplane,
+int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 		int8 *sim,
 		int32 *return_angle)
 {
@@ -774,7 +774,7 @@ int find_principle_axis(uint16 *pixels, int16 *diffs, int16 *bplane,
 
 
 /* Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad). */
-int check_arrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
+int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
 {
 	uint16 center = pixels[4];
 
@@ -894,7 +894,7 @@ int check_arrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
  * refinement algorithms.
  *
  */
-int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
+int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		int8 *sim, double angle)
 {
 	int32 sums_dir[9] = { 0 };
@@ -1034,7 +1034,7 @@ int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
 			if (n > 1) return 6;	/* | */
 
 			if (best_dir >= 5)
-				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 1);
+				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
 
 			switch(best_dir)
 			{
@@ -1191,7 +1191,7 @@ int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
 			if (n > 1) return 0;	/* - */
 
 			if (best_dir >= 5)
-				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 1);
+				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
 
 			switch(best_dir)
 			{
@@ -1455,7 +1455,7 @@ int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
 			}
 
 			if (best_dir >= 5)
-				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 0);
+				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
 
 			switch(best_dir)
 			{
@@ -1727,7 +1727,7 @@ int refine_direction(char edge_type, uint16 *pixels, int16 *bptr,
 			}
 
 			if (best_dir >= 5)
-				ok_arrow_flag = check_arrows(best_dir, pixels, sim, 0);
+				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
 
 			switch(best_dir)
 			{
@@ -1972,9 +1972,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[6] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2006,9 +2006,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2034,9 +2034,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			if (sub_type != 16)
 			{
 				tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2048,9 +2048,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			if (sub_type != 17)
 			{
 				tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[6] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2076,9 +2076,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2110,9 +2110,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[6] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2144,9 +2144,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2178,9 +2178,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[8] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2206,9 +2206,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			if (sub_type != 18)
 			{
 				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2220,9 +2220,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			if (sub_type != 19)
 			{
 				tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[8] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2248,9 +2248,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[8] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2282,9 +2282,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2316,9 +2316,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2338,9 +2338,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[6] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2366,9 +2366,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2388,9 +2388,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[8] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2416,9 +2416,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2438,9 +2438,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2466,9 +2466,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[6], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[6], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[6], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[6] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2488,9 +2488,9 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[8], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[8], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[8], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[8] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2579,9 +2579,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2631,9 +2631,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[1] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2677,9 +2677,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			if (sub_type != 16)
 			{
 				tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[1] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2704,9 +2704,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			if (sub_type != 17)
 			{
 				tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2744,9 +2744,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[1] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2796,9 +2796,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2848,9 +2848,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2900,9 +2900,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[3] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2946,9 +2946,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			if (sub_type != 18)
 			{
 				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2973,9 +2973,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			if (sub_type != 19)
 			{
 				tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[3] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3013,9 +3013,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[3] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3065,9 +3065,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3117,9 +3117,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3161,9 +3161,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3210,9 +3210,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[1] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3254,9 +3254,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[3] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3303,9 +3303,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[0], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[0], pixels[3]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[0], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[0] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3347,9 +3347,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[1], pixels[1]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[1], pixels[5]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[1], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[1] = pixels[1];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3396,9 +3396,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[2], pixels[3]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[2], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[2], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[2] = pixels[3];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3440,9 +3440,9 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else	/* choose nearest pixel */
 #endif
 			{
-				diff1 = calc_pixel_diff_nosqrt(tmp[3], pixels[5]);
-				diff2 = calc_pixel_diff_nosqrt(tmp[3], pixels[7]);
-				diff3 = calc_pixel_diff_nosqrt(tmp[3], pixels[4]);
+				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
 				if (diff1 <= diff2 && diff1 <= diff3)
 					tmp[3] = pixels[5];
 				else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3685,7 +3685,7 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 
 /* Perform edge detection, draw the new 3x pixels */
 template<typename ColorMask>
-void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
+void antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		int overlay_flag)
@@ -3789,7 +3789,7 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 			}
 
 #endif
-			diffs = choose_greyscale(pixels);
+			diffs = chooseGreyscale(pixels);
 
 			/* block of solid color */
 			if (!diffs)
@@ -3808,9 +3808,9 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 
 			bplane = bptr_global;
 
-			edge_type = find_principle_axis(pixels, diffs, bplane,
+			edge_type = findPrincipleAxis(pixels, diffs, bplane,
 					sim, &angle);
-			sub_type = refine_direction(edge_type, pixels, bplane,
+			sub_type = refineDirection(edge_type, pixels, bplane,
 					sim, angle);
 			if (sub_type >= 0)
 				sub_type = fix_knights(sub_type, pixels, sim);
@@ -3825,7 +3825,7 @@ void anti_alias_pass_3x(const uint8 *src, uint8 *dst,
 
 /* Perform edge detection, draw the new 2x pixels */
 template<typename ColorMask>
-void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
+void antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		int overlay_flag,
@@ -3931,7 +3931,7 @@ void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 			}
 #endif
 
-			diffs = choose_greyscale(pixels);
+			diffs = chooseGreyscale(pixels);
 
 			/* block of solid color */
 			if (!diffs)
@@ -3950,9 +3950,9 @@ void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 
 			bplane = bptr_global;
 
-			edge_type = find_principle_axis(pixels, diffs, bplane,
+			edge_type = findPrincipleAxis(pixels, diffs, bplane,
 					sim, &angle);
-			sub_type = refine_direction(edge_type, pixels, bplane,
+			sub_type = refineDirection(edge_type, pixels, bplane,
 					sim, angle);
 			if (sub_type >= 0)
 				sub_type = fix_knights(sub_type, pixels, sim);
@@ -3967,7 +3967,7 @@ void anti_alias_pass_2x(const uint8 *src, uint8 *dst,
 
 
 /* Initialize various lookup tables */
-void init_tables(const uint8 *srcPtr, uint32 srcPitch,
+void initTables(const uint8 *srcPtr, uint32 srcPitch,
 		int width, int height)
 {
 	double r_float, g_float, b_float;
@@ -4314,7 +4314,7 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
 					srcPitch)) + (width - 1) - 1;
 
-		init_tables(srcPtr, srcPitch, width, height);
+		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
 	}
 
@@ -4348,12 +4348,12 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 	if ((const uint16 *) srcPtr < src_addr_min ||
 			(const uint16 *) srcPtr > src_addr_max)
 	{
-		anti_alias_pass_3x(srcPtr, dstPtr, width, height,
+		antiAliasPass3x(srcPtr, dstPtr, width, height,
 				3*width, 3*height, srcPitch, dstPitch, 1);
 	}
 	else   /* Draw the regular screen, this isn't an overlay. */
 	{
-		anti_alias_pass_3x(srcPtr, dstPtr, width, height,
+		antiAliasPass3x(srcPtr, dstPtr, width, height,
 				3*width, 3*height, srcPitch, dstPitch, 0);
 	}
 
@@ -4382,7 +4382,7 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
 					srcPitch)) + (width - 1) - 1;
 
-		init_tables(srcPtr, srcPitch, width, height);
+		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
 	}
 
@@ -4416,12 +4416,12 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 	if ((const uint16 *) srcPtr < src_addr_min ||
 			(const uint16 *) srcPtr > src_addr_max)
 	{
-		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+		antiAliasPass2x(srcPtr, dstPtr, width, height,
 				2*width, 2*height, srcPitch, dstPitch, 1, 0);
 	}
 	else   /* Draw the regular screen, this isn't an overlay. */
 	{
-		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+		antiAliasPass2x(srcPtr, dstPtr, width, height,
 				2*width, 2*height, srcPitch, dstPitch, 0, 0);
 	}
 
@@ -4456,7 +4456,7 @@ void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
 					srcPitch)) + (width - 1) - 1;
 
-		init_tables(srcPtr, srcPitch, width, height);
+		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
 	}
 
@@ -4490,12 +4490,12 @@ void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 	if ((const uint16 *) srcPtr < src_addr_min ||
 			(const uint16 *) srcPtr > src_addr_max)
 	{
-		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+		antiAliasPass2x(srcPtr, dstPtr, width, height,
 				2*width, 2*height, srcPitch, dstPitch, 1, 0);
 	}
 	else   /* Draw the regular screen, this isn't an overlay. */
 	{
-		anti_alias_pass_2x(srcPtr, dstPtr, width, height,
+		antiAliasPass2x(srcPtr, dstPtr, width, height,
 				2*width, 2*height, srcPitch, dstPitch, 0, 1);
 	}
 
@@ -4513,7 +4513,7 @@ EdgePlugin::EdgePlugin() {
 
 void EdgePlugin::initialize(Graphics::PixelFormat format) {
 	_format = format;
-	init_tables(0, 0, 0, 0);
+	initTables(0, 0, 0, 0);
 }
 
 void EdgePlugin::deinitialize() {
@@ -4524,14 +4524,14 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				anti_alias_pass_2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
 			else
-				anti_alias_pass_2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
 		} else {
 			if (_format.gLoss == 2)
-				anti_alias_pass_3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
 			else
-				anti_alias_pass_3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
 		}
 	} else {
 		warning("FIXME: EdgePlugin 32bpp format");


Commit: 9ba4c24ae133ccdcdc01091812f4470c03a2adeb
    https://github.com/scummvm/scummvm/commit/9ba4c24ae133ccdcdc01091812f4470c03a2adeb
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Migrate global funcs and vars to Edge Plugin class

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index acbe586147..1e02dd8da2 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -126,48 +126,9 @@
 #define RGB_SHIFT 13			/* bit shift for RGB precision */
 
 const int16 one_sqrt2 = (int16) (((int16)1<<GREY_SHIFT) / sqrt(2.0) + 0.5);
-int16 rgb_table[65536][3] = {0};	/* table lookup for RGB */
-int16 greyscale_table[3][65536] = {0};	/* greyscale tables */
-int16 *chosen_greyscale;		/* pointer to chosen greyscale table */
-int16 *bptr_global;			/* too awkward to pass variables */
-int8 sim_sum;				/* sum of similarity matrix */
-
-uint16 div3[189];			/* tables for pixel interpolation */
-uint16 div9[567];
-
-int32 max_old_src_size = 0;		/* maximum observed rectangle size */
-int32 max_overlay_size = 0;		/* maximum observed overlay size */
-int32 max_old_dst_size = 0;
-int32 max_dst_overlay_size = 0;
-uint16 *old_src = NULL;			/* old src array */
-uint16 *old_overlay = NULL;		/* holds the old overlay */
-uint16 *old_dst = NULL;			/* old dst array */
-uint16 *old_dst_overlay = NULL;		/* old dst overlay array */
-
-int cur_screen_width = 0;		/* current game screen w and h */
-int cur_screen_height = 0;
-int old_screen_width = 0;		/* previous game screen w and h */
-int old_screen_height = 0;
-int cur_dst_screen_width = 0;		/* scaled dst w and h */
-int cur_dst_screen_height = 0;
-int old_dst_screen_width = 0;
-int old_dst_screen_height = 0;
-int cur_overlay_width = 0;
-int cur_overlay_height = 0;
-int old_overlay_width = 0;
-int old_overlay_height = 0;
-int cur_dst_overlay_width = 0;
-int cur_dst_overlay_height = 0;
-int old_dst_overlay_width = 0;
-int old_dst_overlay_height = 0;
-int max_scale = -1;			/* used for dst buffer arrays */
-
-int init_flag = 0;			/* have the tables been initialized? */
-const uint16 *src_addr_min = NULL;	/* start of src screen array */
-const uint16 *src_addr_max = NULL;	/* end of src screen array */
-
 const int16 int32_sqrt3 = (int16) (((int16)1<<GREY_SHIFT) * sqrt(3.0) + 0.5);
 
+
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
 #define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
 #define interpolate_2_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_2_1<ColorMask>(a,b) : interpolate32_2_1<ColorMask>(a,b))
@@ -354,7 +315,7 @@ double fast_atan(double x0)
  * bitplanes.  The increase in image quality is well worth the speed hit.
  *
  */
-int16 * chooseGreyscale(uint16 *pixels)
+int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 {
 	static int16 greyscale_diffs[3][8];
 	static int16 bplanes[3][9];
@@ -372,7 +333,7 @@ int16 * chooseGreyscale(uint16 *pixels)
 
 		sum_diffs = 0;
 
-		grey_ptr = greyscale_table[i];
+		grey_ptr = _greyscaleTable[i];
 
 		/* fill the 9 pixel window with greyscale values */
 		bptr = bplanes[i];
@@ -409,8 +370,8 @@ int16 * chooseGreyscale(uint16 *pixels)
 	{
 		if (!scores[1]) return NULL;
 
-		chosen_greyscale = greyscale_table[1];
-		bptr_global = bplanes[1];
+		_chosenGreyscale = _greyscaleTable[1];
+		_bptr = bplanes[1];
 		return greyscale_diffs[1];
 	}
 
@@ -418,15 +379,15 @@ int16 * chooseGreyscale(uint16 *pixels)
 	{
 		if (!scores[0]) return NULL;
 
-		chosen_greyscale = greyscale_table[0];
-		bptr_global = bplanes[0];
+		_chosenGreyscale = _greyscaleTable[0];
+		_bptr = bplanes[0];
 		return greyscale_diffs[0];
 	}
 
 	if (!scores[2]) return NULL;
 
-	chosen_greyscale = greyscale_table[2];
-	bptr_global = bplanes[2];
+	_chosenGreyscale = _greyscaleTable[2];
+	_bptr = bplanes[2];
 	return greyscale_diffs[2];
 }
 
@@ -439,13 +400,12 @@ int16 * chooseGreyscale(uint16 *pixels)
  * useful results.
  *
  */
-int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
-{
+int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
 
 #if 1	/* distance between pixels, weighted by roughly luma proportions */
 	int32 sum = 0;
-	int16 *rgb_ptr1 = rgb_table[pixel1];
-	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 *rgb_ptr1 = _rgbTable[pixel1];
+	int16 *rgb_ptr2 = _rgbTable[pixel2];
 	int16 diff;
 
 	diff = (*rgb_ptr1++ - *rgb_ptr2++) << 1;
@@ -460,18 +420,18 @@ int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
 
 #if 0	/* distance between pixels, weighted by chosen greyscale proportions */
 	int32 sum = 0;
-	int16 *rgb_ptr1 = rgb_table[pixel1];
-	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 *rgb_ptr1 = _rgbTable[pixel1];
+	int16 *rgb_ptr2 = _rgbTable[pixel2];
 	int16 diff;
 	int r_shift, g_shift, b_shift;
 
-	if (chosen_greyscale == greyscale_table[1])
+	if (_chosenGreyscale == _greyscaleTable[1])
 	{
 		r_shift = 1;
 		g_shift = 2;
 		b_shift = 0;
 	}
-	else if (chosen_greyscale == greyscale_table[0])
+	else if (_chosenGreyscale == _greyscaleTable[0])
 	{
 		r_shift = 2;
 		g_shift = 1;
@@ -496,8 +456,8 @@ int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
 
 #if 0	/* distance between pixels, unweighted */
 	int32 sum = 0;
-	int16 *rgb_ptr1 = rgb_table[pixel1];
-	int16 *rgb_ptr2 = rgb_table[pixel2];
+	int16 *rgb_ptr1 = _rgbTable[pixel1];
+	int16 *rgb_ptr2 = _rgbTable[pixel2];
 	int16 diff;
 
 	diff = *rgb_ptr1++ - *rgb_ptr2++;
@@ -511,7 +471,7 @@ int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
 #endif
 
 #if 0	/* use the greyscale directly */
-	return labs(chosen_greyscale[pixel1] - chosen_greyscale[pixel2]);
+	return labs(_chosenGreyscale[pixel1] - _chosenGreyscale[pixel2]);
 #endif
 }
 
@@ -531,10 +491,9 @@ int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2)
  * since everything I have tried has lead to slight mis-detection errors.
  *
  */
-int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
+int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 		int8 *sim,
-		int32 *return_angle)
-{
+		int32 *return_angle) {
 	struct xy_point
 	{
 		int16 x, y;
@@ -584,11 +543,11 @@ int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	/* calculate yes/no similarity matrix to center pixel */
 	/* store the number of similar pixels */
 	cutoff = ((int16)1<<(GREY_SHIFT-3));
-	for (i = 0, sim_sum = 0; i < 8; i++)
-		sim_sum += (sim[i] = (diffs[i] < cutoff));
+	for (i = 0, _simSum = 0; i < 8; i++)
+		_simSum += (sim[i] = (diffs[i] < cutoff));
 
 	/* don't reverse pattern for off-center knights and sharp corners */
-	if (sim_sum >= 3 && sim_sum <= 5)
+	if (_simSum >= 3 && _simSum <= 5)
 	{
 		/* |. */ /* '- */
 		if (sim[1] && sim[4] && sim[5] && !sim[3] && !sim[6] &&
@@ -611,7 +570,7 @@ int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 			reverse_flag = 0;
 
 		/* 90 degree corners */
-		else if (sim_sum == 3)
+		else if (_simSum == 3)
 		{
 			if ((sim[0] && sim[1] && sim[3]) ||
 					(sim[1] && sim[2] && sim[4]) ||
@@ -623,11 +582,11 @@ int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 
 	/* redo similarity array, less stringent for later checks */
 	cutoff = ((int16)1<<(GREY_SHIFT-1));
-	for (i = 0, sim_sum = 0; i < 8; i++)
-		sim_sum += (sim[i] = (diffs[i] < cutoff));
+	for (i = 0, _simSum = 0; i < 8; i++)
+		_simSum += (sim[i] = (diffs[i] < cutoff));
 
 	/* center pixel is different from all the others, not an edge */
-	if (sim_sum == 0) return '0';
+	if (_simSum == 0) return '0';
 
 	/* reverse the difference array, so most similar is closest to 1 */
 	if (reverse_flag)
@@ -774,8 +733,7 @@ int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 
 
 /* Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad). */
-int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
-{
+int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag) {
 	uint16 center = pixels[4];
 
 	if (center == pixels[0] && center == pixels[2] &&
@@ -848,7 +806,7 @@ int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
 					sim[1] == sim[3] &&
 					sim[3] == sim[6] &&
 					((sim[2] && sim[7]) ||
-					 (half_flag && sim_sum == 2 && sim[4] &&
+					 (half_flag && _simSum == 2 && sim[4] &&
 					  (sim[2] || sim[7]))))	/* < */
 				return 1;
 			break;
@@ -857,7 +815,7 @@ int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
 					sim[1] == sim[4] &&
 					sim[4] == sim[6] &&
 					((sim[0] && sim[5]) ||
-					 (half_flag && sim_sum == 2 && sim[3] &&
+					 (half_flag && _simSum == 2 && sim[3] &&
 					  (sim[0] || sim[5]))))	/* > */
 				return 1;
 			break;
@@ -866,7 +824,7 @@ int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
 					sim[1] == sim[3] &&
 					sim[3] == sim[4] &&
 					((sim[5] && sim[7]) ||
-					 (half_flag && sim_sum == 2 && sim[6] &&
+					 (half_flag && _simSum == 2 && sim[6] &&
 					  (sim[5] || sim[7]))))	/* ^ */
 				return 1;
 			break;
@@ -875,7 +833,7 @@ int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
 					sim[3] == sim[6] &&
 					sim[4] == sim[6] &&
 					((sim[0] && sim[2]) ||
-					 (half_flag && sim_sum == 2 && sim[1] &&
+					 (half_flag && _simSum == 2 && sim[1] &&
 					  (sim[0] || sim[2]))))	/* v */
 				return 1;
 			break;
@@ -894,9 +852,8 @@ int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag)
  * refinement algorithms.
  *
  */
-int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
-		int8 *sim, double angle)
-{
+int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
+		int8 *sim, double angle) {
 	int32 sums_dir[9] = { 0 };
 	int32 sum;
 	int32 best_sum;
@@ -1234,7 +1191,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		case '\\':
 
 			/* CHECK -- handle noisy half-diags */
-			if (sim_sum == 1)
+			if (_simSum == 1)
 			{
 				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
 						pixels[5] == pixels[7])
@@ -1400,7 +1357,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 			}
 
 			/* CHECK -- handle zig-zags */
-			if (sim_sum == 3)
+			if (_simSum == 3)
 			{
 				if ((best_dir == 0 || best_dir == 1) &&
 						sim[0] && sim[1] && sim[4])
@@ -1435,7 +1392,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 						return 17;		/* .\ */
 				}
 
-				if (sim_sum == 3 && sim[0] && sim[7] &&
+				if (_simSum == 3 && sim[0] && sim[7] &&
 						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
 						pixels[5] == pixels[7])
 				{
@@ -1445,7 +1402,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 						return 17;		/* .\ */
 				}
 
-				if (sim_sum == 3 && sim[2] && sim[5])
+				if (_simSum == 3 && sim[2] && sim[5])
 				{
 					if (sim[0])
 						return 18;		/* '/ */
@@ -1498,7 +1455,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		case '/':
 
 			/* CHECK -- handle noisy half-diags */
-			if (sim_sum == 1)
+			if (_simSum == 1)
 			{
 				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
 						pixels[5] == pixels[7])
@@ -1672,7 +1629,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 			}
 
 			/* CHECK -- handle zig-zags */
-			if (sim_sum == 3)
+			if (_simSum == 3)
 			{
 				if ((best_dir == 0 || best_dir == 1) &&
 						sim[2] && sim[4] && sim[6])
@@ -1707,7 +1664,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 						return 19;		/* /. */
 				}
 
-				if (sim_sum == 3 && sim[2] && sim[5] &&
+				if (_simSum == 3 && sim[2] && sim[5] &&
 						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
 						pixels[5] == pixels[7])
 				{
@@ -1717,7 +1674,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 						return 19;		/* /. */
 				}
 
-				if (sim_sum == 3 && sim[0] && sim[7])
+				if (_simSum == 3 && sim[0] && sim[7])
 				{
 					if (sim[2])
 						return 16;		/* \' */
@@ -1784,8 +1741,7 @@ int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 
 
 /* "Chess Knight" patterns can be mis-detected, fix easy cases. */
-int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
-{
+int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 	uint16 center = pixels[4];
 	int dir = sub_type;
 	int n = 0;
@@ -1804,56 +1760,56 @@ int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
 	{
 		case 1:		/* '- */
 			if (sim[0] && sim[4] &&
-					!(sim_sum == 3 && sim[5] &&
+					!(_simSum == 3 && sim[5] &&
 						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 2:		/* -. */
 			if (sim[3] && sim[7] &&
-					!(sim_sum == 3 && sim[2] &&
+					!(_simSum == 3 && sim[2] &&
 						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 4:		/* '| */
 			if (sim[0] && sim[6] &&
-					!(sim_sum == 3 && sim[2] &&
+					!(_simSum == 3 && sim[2] &&
 						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 5:		/* |. */
 			if (sim[1] && sim[7] &&
-					!(sim_sum == 3 && sim[5] &&
+					!(_simSum == 3 && sim[5] &&
 						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 7:		/* |' */
 			if (sim[2] && sim[6] &&
-					!(sim_sum == 3 && sim[0] &&
+					!(_simSum == 3 && sim[0] &&
 						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 8:		/* .| */
 			if (sim[1] && sim[5] &&
-					!(sim_sum == 3 && sim[7] &&
+					!(_simSum == 3 && sim[7] &&
 						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 10:	/* -' */
 			if (sim[2] && sim[3] &&
-					!(sim_sum == 3 && sim[7] &&
+					!(_simSum == 3 && sim[7] &&
 						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
 
 		case 11:	/* .- */
 			if (sim[4] && sim[5] &&
-					!(sim_sum == 3 && sim[0] &&
+					!(_simSum == 3 && sim[0] &&
 						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
 				ok_orig_flag = 1;
 			break;
@@ -1944,9 +1900,8 @@ int fix_knights(int sub_type, uint16 *pixels, int8 *sim)
 
 /* Fill pixel grid without interpolation, using the detected edge */
 template<typename ColorMask>
-void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
-		uint16 *pixels, int sub_type, int16 *bptr)
-{
+void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
+		uint16 *pixels, int sub_type, int16 *bptr) {
 	uint16 *dptr2;
 	int16 tmp_grey;
 	uint16 center = pixels[4];
@@ -1962,7 +1917,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -1982,7 +1937,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[6] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[6]];
+				tmp_grey = _chosenGreyscale[tmp[6]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[8]);
 				if (diff1 <= diff2)
@@ -1996,7 +1951,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -2016,7 +1971,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[2] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[2]];
+				tmp_grey = _chosenGreyscale[tmp[2]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[0]);
 				if (diff1 <= diff2)
@@ -2066,7 +2021,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2086,7 +2041,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[2] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[2]];
+				tmp_grey = _chosenGreyscale[tmp[2]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[8]);
 				if (diff1 <= diff2)
@@ -2100,7 +2055,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2120,7 +2075,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[6] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[6]];
+				tmp_grey = _chosenGreyscale[tmp[6]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[0]);
 				if (diff1 <= diff2)
@@ -2134,7 +2089,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2154,7 +2109,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[0] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[0]];
+				tmp_grey = _chosenGreyscale[tmp[0]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[6]);
 				if (diff1 <= diff2)
@@ -2168,7 +2123,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2188,7 +2143,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[8] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[8]];
+				tmp_grey = _chosenGreyscale[tmp[8]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[2]);
 				if (diff1 <= diff2)
@@ -2238,7 +2193,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -2258,7 +2213,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[8] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[8]];
+				tmp_grey = _chosenGreyscale[tmp[8]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[6]);
 				if (diff1 <= diff2)
@@ -2272,7 +2227,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -2292,7 +2247,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				else
 					tmp[0] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[0]];
+				tmp_grey = _chosenGreyscale[tmp[0]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[2]);
 				if (diff1 <= diff2)
@@ -2306,7 +2261,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2328,7 +2283,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -2356,7 +2311,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -2378,7 +2333,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2406,7 +2361,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2428,7 +2383,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -2456,7 +2411,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 				tmp[i] = center;
 
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -2478,7 +2433,7 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2551,11 +2506,10 @@ void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 /* Fill pixel grid with or without interpolation, using the detected edge */
 template<typename ColorMask>
-void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
+void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
-		int interpolate_2x)
-{
+		int interpolate_2x) {
 	uint16 *dptr2;
 	uint16 center = pixels[4];
 	int32 diff1, diff2, diff3;
@@ -2569,7 +2523,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[3] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -2589,7 +2543,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[2] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[2]];
+				tmp_grey = _chosenGreyscale[tmp[2]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[8]);
 				if (diff1 <= diff2)
@@ -2609,7 +2563,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[2]])
+						if (bptr[4] > _chosenGreyscale[tmp[2]])
 							tmp[2] = center;
 					}
 				}
@@ -2621,7 +2575,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[2] = tmp[3] = center;
 
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -2641,7 +2595,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[1] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[1]];
+				tmp_grey = _chosenGreyscale[tmp[1]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[0]);
 				if (diff1 <= diff2)
@@ -2661,7 +2615,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[1]])
+						if (bptr[4] > _chosenGreyscale[tmp[1]])
 							tmp[1] = center;
 					}
 				}
@@ -2695,8 +2649,8 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 				 * nearest-neighbor, so detect them and don't anti-alias them.
 				 */
-				else if (bptr[4] > chosen_greyscale[tmp[1]] ||
-						(sim_sum == 1 && (sim[0] || sim[7]) &&
+				else if (bptr[4] > _chosenGreyscale[tmp[1]] ||
+						(_simSum == 1 && (sim[0] || sim[7]) &&
 						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 					tmp[1] = center;
 			}
@@ -2722,8 +2676,8 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 				 * nearest-neighbor, so detect them and don't anti-alias them.
 				 */
-				else if (bptr[4] > chosen_greyscale[tmp[2]] ||
-						(sim_sum == 1 && (sim[0] || sim[7]) &&
+				else if (bptr[4] > _chosenGreyscale[tmp[2]] ||
+						(_simSum == 1 && (sim[0] || sim[7]) &&
 						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 					tmp[2] = center;
 			}
@@ -2734,7 +2688,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[2] = tmp[3] = center;
 
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2754,7 +2708,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[1] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[1]];
+				tmp_grey = _chosenGreyscale[tmp[1]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[8]);
 				if (diff1 <= diff2)
@@ -2774,7 +2728,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[1]])
+						if (bptr[4] > _chosenGreyscale[tmp[1]])
 							tmp[1] = center;
 					}
 				}
@@ -2786,7 +2740,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[3] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2806,7 +2760,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[2] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[2]];
+				tmp_grey = _chosenGreyscale[tmp[2]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[0]);
 				if (diff1 <= diff2)
@@ -2826,7 +2780,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[2]])
+						if (bptr[4] > _chosenGreyscale[tmp[2]])
 							tmp[2] = center;
 					}
 				}
@@ -2838,7 +2792,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -2858,7 +2812,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[0] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[0]];
+				tmp_grey = _chosenGreyscale[tmp[0]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[6]);
 				if (diff1 <= diff2)
@@ -2878,7 +2832,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[0]])
+						if (bptr[4] > _chosenGreyscale[tmp[0]])
 							tmp[0] = center;
 					}
 				}
@@ -2890,7 +2844,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = center;
 
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -2910,7 +2864,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[3] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[3]];
+				tmp_grey = _chosenGreyscale[tmp[3]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[2]);
 				if (diff1 <= diff2)
@@ -2930,7 +2884,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[3]])
+						if (bptr[4] > _chosenGreyscale[tmp[3]])
 							tmp[3] = center;
 					}
 				}
@@ -2964,8 +2918,8 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 				 * nearest-neighbor, so detect them and don't anti-alias them.
 				 */
-				else if (bptr[4] > chosen_greyscale[tmp[0]] ||
-						(sim_sum == 1 && (sim[2] || sim[5]) &&
+				else if (bptr[4] > _chosenGreyscale[tmp[0]] ||
+						(_simSum == 1 && (sim[2] || sim[5]) &&
 						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 					tmp[0] = center;
 			}
@@ -2991,8 +2945,8 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 				 * nearest-neighbor, so detect them and don't anti-alias them.
 				 */
-				else if (bptr[4] > chosen_greyscale[tmp[3]] ||
-						(sim_sum == 1 && (sim[2] || sim[5]) &&
+				else if (bptr[4] > _chosenGreyscale[tmp[3]] ||
+						(_simSum == 1 && (sim[2] || sim[5]) &&
 						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 					tmp[3] = center;
 			}
@@ -3003,7 +2957,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = center;
 
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -3023,7 +2977,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[3] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[3]];
+				tmp_grey = _chosenGreyscale[tmp[3]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[6]);
 				if (diff1 <= diff2)
@@ -3043,7 +2997,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[3]])
+						if (bptr[4] > _chosenGreyscale[tmp[3]])
 							tmp[3] = center;
 					}
 				}
@@ -3055,7 +3009,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -3075,7 +3029,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				else
 					tmp[0] = pixels[4];
 
-				tmp_grey = chosen_greyscale[tmp[0]];
+				tmp_grey = _chosenGreyscale[tmp[0]];
 				diff1 = labs(bptr[4] - tmp_grey);
 				diff2 = labs(bptr[4] - bptr[2]);
 				if (diff1 <= diff2)
@@ -3095,7 +3049,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[0]])
+						if (bptr[4] > _chosenGreyscale[tmp[0]])
 							tmp[0] = center;
 					}
 				}
@@ -3107,7 +3061,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -3128,7 +3082,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[4] && sim[2])
+				if (_simSum == 2 && sim[4] && sim[2])
 				{
 					if (interpolate_2x)
 					{
@@ -3137,7 +3091,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[0]])
+						if (bptr[4] > _chosenGreyscale[tmp[0]])
 							tmp[0] = center;
 					}
 
@@ -3151,7 +3105,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -3172,7 +3126,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[2] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[4] && sim[7])
+				if (_simSum == 2 && sim[4] && sim[7])
 				{
 					if (interpolate_2x)
 					{
@@ -3181,7 +3135,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[2]])
+						if (bptr[4] > _chosenGreyscale[tmp[2]])
 							tmp[2] = center;
 					}
 
@@ -3200,7 +3154,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -3221,7 +3175,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[1] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[3] && sim[0])
+				if (_simSum == 2 && sim[3] && sim[0])
 				{
 					if (interpolate_2x)
 					{
@@ -3230,7 +3184,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[1]])
+						if (bptr[4] > _chosenGreyscale[tmp[1]])
 							tmp[1] = center;
 					}
 
@@ -3244,7 +3198,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -3265,7 +3219,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[3] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[3] && sim[5])
+				if (_simSum == 2 && sim[3] && sim[5])
 				{
 					if (interpolate_2x)
 					{
@@ -3274,7 +3228,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[3]])
+						if (bptr[4] > _chosenGreyscale[tmp[3]])
 							tmp[3] = center;
 					}
 
@@ -3293,7 +3247,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = chosen_greyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[1]);
 			diff2 = labs(bptr[4] - bptr[3]);
@@ -3314,7 +3268,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[6] && sim[5])
+				if (_simSum == 2 && sim[6] && sim[5])
 				{
 					if (interpolate_2x)
 					{
@@ -3323,7 +3277,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[0]])
+						if (bptr[4] > _chosenGreyscale[tmp[0]])
 							tmp[0] = center;
 					}
 
@@ -3337,7 +3291,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = chosen_greyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[5]);
 			diff2 = labs(bptr[4] - bptr[1]);
@@ -3358,7 +3312,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[1] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[6] && sim[7])
+				if (_simSum == 2 && sim[6] && sim[7])
 				{
 					if (interpolate_2x)
 					{
@@ -3367,7 +3321,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[1]])
+						if (bptr[4] > _chosenGreyscale[tmp[1]])
 							tmp[1] = center;
 					}
 
@@ -3386,7 +3340,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[3]);
 			diff2 = labs(bptr[4] - bptr[7]);
@@ -3407,7 +3361,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[2] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[1] && sim[0])
+				if (_simSum == 2 && sim[1] && sim[0])
 				{
 					if (interpolate_2x)
 					{
@@ -3416,7 +3370,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[2]])
+						if (bptr[4] > _chosenGreyscale[tmp[2]])
 							tmp[2] = center;
 					}
 
@@ -3430,7 +3384,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			}
 
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = chosen_greyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_ARROWS
 			diff1 = labs(bptr[4] - bptr[7]);
 			diff2 = labs(bptr[4] - bptr[5]);
@@ -3451,7 +3405,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[3] = pixels[4];
 
 				/* check for half-arrow */
-				if (sim_sum == 2 && sim[1] && sim[2])
+				if (_simSum == 2 && sim[1] && sim[2])
 				{
 					if (interpolate_2x)
 					{
@@ -3460,7 +3414,7 @@ void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					}
 					else
 					{
-						if (bptr[4] > chosen_greyscale[tmp[3]])
+						if (bptr[4] > _chosenGreyscale[tmp[3]])
 							tmp[3] = center;
 					}
 
@@ -3685,11 +3639,10 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 
 /* Perform edge detection, draw the new 3x pixels */
 template<typename ColorMask>
-void antiAliasPass3x(const uint8 *src, uint8 *dst,
+void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
-		int overlay_flag)
-{
+		int overlay_flag) {
 	int x, y;
 	int w2 = w + 2;
 	const uint8 *sptr8 = src;
@@ -3703,35 +3656,6 @@ void antiAliasPass3x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch3 = dstPitch * 3;
 
-	uint16 *old_src_ptr, *old_dst_ptr;
-	uint16 *old_sptr16, *old_dptr16;
-	int32 dist, old_src_y, old_src_x;
-	int old_src_inc;
-	int old_dst_inc, old_dst_inc3;
-
-	if (overlay_flag)
-	{
-		old_src_ptr = old_overlay + w2 + 1;
-		old_src_inc = w2;
-
-		old_dst_ptr = old_dst_overlay;
-		old_dst_inc = w_new;
-	}
-	else
-	{
-		dist = src - (const uint8 *) src_addr_min;
-		old_src_y = dist / srcPitch;
-		old_src_x = (dist - old_src_y * srcPitch) >> 1;
-
-		old_src_inc = cur_screen_width + 2;
-		old_src_ptr = old_src + (old_src_y + 1) * old_src_inc + old_src_x + 1;
-
-		old_dst_inc = 3 * cur_screen_width;
-		old_dst_ptr = old_dst + 3 * (old_src_y * old_dst_inc + old_src_x);
-	}
-
-	old_dst_inc3 = 3 * old_dst_inc;
-
 #if 0
 #if HANDLE_TRANSPARENT_OVERLAYS
 	uint16 transp = 0;	/* transparent color */
@@ -3749,16 +3673,11 @@ void antiAliasPass3x(const uint8 *src, uint8 *dst,
 	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
 #endif
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3,
-			old_src_ptr += old_src_inc, old_dst_ptr += old_dst_inc3)
-	{
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3) {
 		for (x = 0,
 				sptr16 = (const uint16 *) sptr8,
-				dptr16 = (uint16 *) dptr8,
-				old_sptr16 = old_src_ptr,
-				old_dptr16 = old_dst_ptr;
-				x < w; x++, sptr16++, dptr16 += 3, old_sptr16++, old_dptr16 += 3)
-		{
+				dptr16 = (uint16 *) dptr8;
+				x < w; x++, sptr16++, dptr16 += 3) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
@@ -3806,14 +3725,14 @@ void antiAliasPass3x(const uint8 *src, uint8 *dst,
 #endif
 #endif
 
-			bplane = bptr_global;
+			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,
 					sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
 					sim, angle);
 			if (sub_type >= 0)
-				sub_type = fix_knights(sub_type, pixels, sim);
+				sub_type = fixKnights(sub_type, pixels, sim);
 
 			anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 					sub_type, bplane);
@@ -3825,12 +3744,11 @@ void antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 /* Perform edge detection, draw the new 2x pixels */
 template<typename ColorMask>
-void antiAliasPass2x(const uint8 *src, uint8 *dst,
+void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		int overlay_flag,
-		int interpolate_2x)
-{
+		int interpolate_2x) {
 	int x, y;
 	int w2 = w + 2;
 	const uint8 *sptr8 = src;
@@ -3844,35 +3762,6 @@ void antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch2 = dstPitch << 1;
 
-	uint16 *old_src_ptr, *old_dst_ptr;
-	uint16 *old_sptr16, *old_dptr16;
-	int32 dist, old_src_y, old_src_x;
-	int old_src_inc;
-	int old_dst_inc, old_dst_inc2;
-
-	if (overlay_flag)
-	{
-		old_src_ptr = old_overlay + w2 + 1;
-		old_src_inc = w2;
-
-		old_dst_ptr = old_dst_overlay;
-		old_dst_inc = w_new;
-	}
-	else
-	{
-		dist = src - (const uint8 *) src_addr_min;
-		old_src_y = dist / srcPitch;
-		old_src_x = (dist - old_src_y * srcPitch) >> 1;
-
-		old_src_inc = cur_screen_width + 2;
-		old_src_ptr = old_src + (old_src_y + 1) * old_src_inc + old_src_x + 1;
-
-		old_dst_inc = 2 * cur_screen_width;
-		old_dst_ptr = old_dst + 2 * (old_src_y * old_dst_inc + old_src_x);
-	}
-
-	old_dst_inc2 = 2 * old_dst_inc;
-
 #if 0 
 #if HANDLE_TRANSPARENT_OVERLAYS
 	uint16 transp = 0;	/* transparent color */
@@ -3891,16 +3780,11 @@ void antiAliasPass2x(const uint8 *src, uint8 *dst,
 	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
 #endif
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2,
-			old_src_ptr += old_src_inc, old_dst_ptr += old_dst_inc2)
-	{
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2) {
 		for (x = 0,
 				sptr16 = (const uint16 *) sptr8,
-				dptr16 = (uint16 *) dptr8,
-				old_sptr16 = old_src_ptr,
-				old_dptr16 = old_dst_ptr;
-				x < w; x++, sptr16++, dptr16 += 2, old_sptr16++, old_dptr16 += 2)
-		{
+				dptr16 = (uint16 *) dptr8;
+				x < w; x++, sptr16++, dptr16 += 2) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
@@ -3948,14 +3832,14 @@ void antiAliasPass2x(const uint8 *src, uint8 *dst,
 #endif
 #endif
 
-			bplane = bptr_global;
+			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,
 					sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
 					sim, angle);
 			if (sub_type >= 0)
-				sub_type = fix_knights(sub_type, pixels, sim);
+				sub_type = fixKnights(sub_type, pixels, sim);
 
 			anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 					sub_type, bplane, sim,
@@ -3967,9 +3851,8 @@ void antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 
 /* Initialize various lookup tables */
-void initTables(const uint8 *srcPtr, uint32 srcPitch,
-		int width, int height)
-{
+void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
+		int width, int height) {
 	double r_float, g_float, b_float;
 	int r, g, b;
 	uint16 i;
@@ -4007,12 +3890,12 @@ void initTables(const uint8 *srcPtr, uint32 srcPitch,
 				val[2] = (intensity + 9*val[2]) / 10;
 
 				/* store the greyscale tables */
-				greyscale_table[0][i] = (int16) (val[0] * ((int16)1<<GREY_SHIFT) + 0.5);
-				greyscale_table[1][i] = (int16) (val[1] * ((int16)1<<GREY_SHIFT) + 0.5);
-				greyscale_table[2][i] = (int16) (val[2] * ((int16)1<<GREY_SHIFT) + 0.5);
+				_greyscaleTable[0][i] = (int16) (val[0] * ((int16)1<<GREY_SHIFT) + 0.5);
+				_greyscaleTable[1][i] = (int16) (val[1] * ((int16)1<<GREY_SHIFT) + 0.5);
+				_greyscaleTable[2][i] = (int16) (val[2] * ((int16)1<<GREY_SHIFT) + 0.5);
 
 				/* normalized RGB channel lookups */
-				rgb_ptr = rgb_table[(r << 11) | (g << 5) | b];
+				rgb_ptr = _rgbTable[(r << 11) | (g << 5) | b];
 				rgb_ptr[0] = (int16) (r_float * ((int16)1<<RGB_SHIFT) + 0.5);
 				rgb_ptr[1] = (int16) (g_float * ((int16)1<<RGB_SHIFT) + 0.5);
 				rgb_ptr[2] = (int16) (b_float * ((int16)1<<RGB_SHIFT) + 0.5);
@@ -4020,12 +3903,6 @@ void initTables(const uint8 *srcPtr, uint32 srcPitch,
 		}
 	}
 
-	/* initialize interpolation division tables */
-	for (r = 0; r <= 189; r++)
-		div3[r] = ((r<<1)+3) / 6;
-	for (r = 0; r <= 567; r++)
-		div9[r] = ((r<<1)+9) / 18;
-
 #if 0
 #if INCREASE_WIN32_PRIORITY
 	/*
@@ -4074,6 +3951,7 @@ void initTables(const uint8 *srcPtr, uint32 srcPitch,
  * for now....
  *
  */
+#if 0
 void resize_old_arrays(const uint8 *src, uint8 *dst,
 		int w, int h, int scale)
 {
@@ -4294,7 +4172,7 @@ void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
 		dptr2 = (uint16 *) ((uint8 *) dptr2 + dst_fudge);
 	}
 }
-
+#endif
 
 
 /* 3x anti-aliased resize filter, nearest-neighbor anti-aliasing */
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 5bb6eade67..d7c64db822 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -38,6 +38,44 @@ public:
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
+private:
+	int16* chooseGreyscale(uint16 *pixels);
+	int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2);
+	int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
+		int8 *sim,
+		int32 *return_angle);
+	int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag);
+	int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
+		int8 *sim, double angle);
+	int fixKnights(int sub_type, uint16 *pixels, int8 *sim);
+	void initTables(const uint8 *srcPtr, uint32 srcPitch,
+		int width, int height);
+
+	template<typename ColorMask>
+	void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
+		uint16 *pixels, int sub_type, int16 *bptr,
+		int8 *sim,
+		int interpolate_2x);
+	template<typename ColorMask>
+	void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
+		uint16 *pixels, int sub_type, int16 *bptr);
+	template<typename ColorMask>
+	void antiAliasPass2x(const uint8 *src, uint8 *dst,
+		int w, int h, int w_new, int h_new,
+		int srcPitch, int dstPitch,
+		int overlay_flag,
+		int interpolate_2x);
+	template<typename ColorMask>
+	void antiAliasPass3x(const uint8 *src, uint8 *dst,
+		int w, int h, int w_new, int h_new,
+		int srcPitch, int dstPitch,
+		int overlay_flag);
+
+	int16 _rgbTable[65536][3];       ///< table lookup for RGB
+	int16 _greyscaleTable[3][65536]; ///< greyscale tables
+	int16 *_chosenGreyscale;               ///< pointer to chosen greyscale table
+	int16 *_bptr;                          ///< too awkward to pass variables
+	int8 _simSum;                          ///< sum of similarity matrix
 };
 
 


Commit: 5688622889bfc12252187bafd3bbde3fa77c36e8
    https://github.com/scummvm/scummvm/commit/5688622889bfc12252187bafd3bbde3fa77c36e8
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Move static variables to class members in Edge Plugin

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 1e02dd8da2..d116ff917c 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -317,8 +317,6 @@ double fast_atan(double x0)
  */
 int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 {
-	static int16 greyscale_diffs[3][8];
-	static int16 bplanes[3][9];
 	int i, j;
 	int32 scores[3];
 
@@ -336,14 +334,14 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 		grey_ptr = _greyscaleTable[i];
 
 		/* fill the 9 pixel window with greyscale values */
-		bptr = bplanes[i];
+		bptr = _bplanes[i];
 		pptr = pixels;
 		for (j = 9; j; --j)
 			*bptr++ = grey_ptr[*pptr++];
-		bptr = bplanes[i];
+		bptr = _bplanes[i];
 
 		center = grey_ptr[pixels[4]];
-		diff_ptr = greyscale_diffs[i];
+		diff_ptr = _greyscaleDiffs[i];
 
 		/* calculate the delta from center pixel */
 		diff_ptr[0] = bptr[0] - center;
@@ -371,8 +369,8 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 		if (!scores[1]) return NULL;
 
 		_chosenGreyscale = _greyscaleTable[1];
-		_bptr = bplanes[1];
-		return greyscale_diffs[1];
+		_bptr = _bplanes[1];
+		return _greyscaleDiffs[1];
 	}
 
 	if (scores[0] >= scores[1] && scores[0] >= scores[2])
@@ -380,15 +378,15 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 		if (!scores[0]) return NULL;
 
 		_chosenGreyscale = _greyscaleTable[0];
-		_bptr = bplanes[0];
-		return greyscale_diffs[0];
+		_bptr = _bplanes[0];
+		return _greyscaleDiffs[0];
 	}
 
 	if (!scores[2]) return NULL;
 
 	_chosenGreyscale = _greyscaleTable[2];
-	_bptr = bplanes[2];
-	return greyscale_diffs[2];
+	_bptr = _bplanes[2];
+	return _greyscaleDiffs[2];
 }
 
 
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index d7c64db822..00965b6019 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -76,6 +76,8 @@ private:
 	int16 *_chosenGreyscale;               ///< pointer to chosen greyscale table
 	int16 *_bptr;                          ///< too awkward to pass variables
 	int8 _simSum;                          ///< sum of similarity matrix
+	int16 _greyscaleDiffs[3][8];
+	int16 _bplanes[3][9];
 };
 
 


Commit: f4e0331f2d26d73172c48afedcd037ccbb03df8b
    https://github.com/scummvm/scummvm/commit/f4e0331f2d26d73172c48afedcd037ccbb03df8b
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Run edge.cpp through astyle to conform to style guidelines

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index d116ff917c..a4f5cb74ad 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -105,7 +105,7 @@
  * refreshes, etc..  Also good for seeing if the unchanged pixel detection is
  * working correctly or not :)
  */
-#define DEBUG_REFRESH_RANDOM_XOR		0	/* debug redraws */
+#define DEBUG_REFRESH_RANDOM_XOR        0   /* debug redraws */
 
 /* Use with DEBUG_REFRESH_RANDOM_XOR.  Randomize the borders of the drawing
  * area, whether they are unchanged or not.  Useful for visualizing the
@@ -114,19 +114,19 @@
  * or color cycling, or anything that would cause a dirty rect or require a
  * redraw....
  */
-#define DEBUG_DRAW_REFRESH_BORDERS		0	/* more redraw debug */
+#define DEBUG_DRAW_REFRESH_BORDERS      0   /* more redraw debug */
 
-#define INCREASE_WIN32_PRIORITY			0	/* 1 for slow CPUs */
-#define PARANOID_KNIGHTS			1	/* avoid artifacts */
-#define PARANOID_ARROWS				1	/* avoid artifacts */
-#define HANDLE_TRANSPARENT_OVERLAYS		1	/* as it says */
+#define INCREASE_WIN32_PRIORITY         0   /* 1 for slow CPUs */
+#define PARANOID_KNIGHTS            1   /* avoid artifacts */
+#define PARANOID_ARROWS             1   /* avoid artifacts */
+#define HANDLE_TRANSPARENT_OVERLAYS     1   /* as it says */
 
-#define SIN45 0.7071067811865		/* sin of 45 degrees */
-#define GREY_SHIFT 12			/* bit shift for greyscale precision */
-#define RGB_SHIFT 13			/* bit shift for RGB precision */
+#define SIN45 0.7071067811865       /* sin of 45 degrees */
+#define GREY_SHIFT 12           /* bit shift for greyscale precision */
+#define RGB_SHIFT 13            /* bit shift for RGB precision */
 
-const int16 one_sqrt2 = (int16) (((int16)1<<GREY_SHIFT) / sqrt(2.0) + 0.5);
-const int16 int32_sqrt3 = (int16) (((int16)1<<GREY_SHIFT) * sqrt(3.0) + 0.5);
+const int16 one_sqrt2 = (int16)(((int16)1 << GREY_SHIFT) / sqrt(2.0) + 0.5);
+const int16 int32_sqrt3 = (int16)(((int16)1 << GREY_SHIFT) * sqrt(3.0) + 0.5);
 
 
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
@@ -175,12 +175,11 @@ uint32 seed0, seed1, seed2, seed3;
  *    5  6 13     >> >> <<
  *    5  6 13     << << >>
  *    6  5 13     >> >> <<
- *    6  5 13     << << >>	seems to be slightly "better" than the others?
+ *    6  5 13     << << >>  seems to be slightly "better" than the others?
  *
  * all others, including the "favorite" (13, 17, 5), fail some Monkey tests
  */
-uint32 xorshift_32(void)
-{
+uint32 xorshift_32(void) {
 	seed0 ^= seed0 << 6;
 	seed0 ^= seed0 << 5;
 	seed0 ^= seed0 >> 13;
@@ -190,12 +189,11 @@ uint32 xorshift_32(void)
 
 /* period 2^128 - 1 */
 /* None of the other published 2^128-1 xorshift RNGs passed OPERM5 */
-uint32 xorshift_128(void)
-{
+uint32 xorshift_128(void) {
 	uint32 temp;
 
 	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
-		(seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
+	       (seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
 	seed0 = seed1;
 	seed1 = seed2;
 	seed2 = seed3;
@@ -205,12 +203,11 @@ uint32 xorshift_128(void)
 }
 
 /* return a random fraction over the range [0, 1) */
-double dxorshift_128(void)
-{
+double dxorshift_128(void) {
 	uint32 temp;
 
 	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
-		(seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
+	       (seed2 ^ (seed2 << 27)) ^ (seed3 ^ (seed3 >> 6));
 	seed0 = seed1;
 	seed1 = seed2;
 	seed2 = seed3;
@@ -219,8 +216,7 @@ double dxorshift_128(void)
 	return (temp / 4294967296.0);
 }
 
-void initialize_xorshift_128(uint32 seed)
-{
+void initialize_xorshift_128(uint32 seed) {
 	/* seed0 needs to be initialized prior to calling xorshift_32() */
 	seed0 = seed;
 
@@ -241,7 +237,7 @@ void initialize_xorshift_128(uint32 seed)
  *
  * |x| <= 1:
  *
- *    x + A*x3		A = 0.43157974, B = 0.76443945, C = 0.05831938
+ *    x + A*x3      A = 0.43157974, B = 0.76443945, C = 0.05831938
  * ---------------
  * 1 + B*x2 + C*x4
  *
@@ -251,41 +247,39 @@ void initialize_xorshift_128(uint32 seed)
  *
  * |x| <= 1:
  *
- *  x * (E + F*x2)	E = 1/C, F = A/C
- * ----------------	G = (B/C + sqrt(B2/C2 - 4/C)) / 2
- * (G + x2)(H + x2)	H = (B/C - sqrt(B2/C2 - 4/C)) / 2
+ *  x * (E + F*x2)  E = 1/C, F = A/C
+ * ---------------- G = (B/C + sqrt(B2/C2 - 4/C)) / 2
+ * (G + x2)(H + x2) H = (B/C - sqrt(B2/C2 - 4/C)) / 2
  *
- *			E = 17.14695869537, F = 7.400279975541
- *			G = 11.63393762882, H = 1.473874045440
+ *          E = 17.14695869537, F = 7.400279975541
+ *          G = 11.63393762882, H = 1.473874045440
  *
  * |x| > 1: pi/2 -
  *
- *   x * (I + x2)	I = A
- * ----------------	J = (B + sqrt(B2 - 4C)) / 2
- * (J + x2)(K + x2)	K = (B - sqrt(B2 - 4C)) / 2
+ *   x * (I + x2)   I = A
+ * ---------------- J = (B + sqrt(B2 - 4C)) / 2
+ * (J + x2)(K + x2) K = (B - sqrt(B2 - 4C)) / 2
  *
- *			I = 0.43157974
- *			J = 0.6784840295980, K = 0.0859554204018
+ *          I = 0.43157974
+ *          J = 0.6784840295980, K = 0.0859554204018
  *
  */
-double fast_atan(double x0)
-{
+double fast_atan(double x0) {
 	double x2;
 	double x;
 
 	x = fabs(x0);
-	x2 = x*x;
-	if (x > 1)
-	{
+	x2 = x * x;
+	if (x > 1) {
 		x2 = 1.570796326795 -
-			x * (0.43157974 + x2) /
-			((0.6784840295980 + x2) * (0.0859554204018 + x2));
+		     x * (0.43157974 + x2) /
+		     ((0.6784840295980 + x2) * (0.0859554204018 + x2));
 		if (x0 < 0) return -x2;
 		return x2;
 	}
 
 	return x0 * (17.14695869537 + 7.400279975541 * x2) /
-		((11.63393762882 + x2) * (1.473874045440 + x2));
+	       ((11.63393762882 + x2) * (1.473874045440 + x2));
 }
 
 
@@ -296,32 +290,30 @@ double fast_atan(double x0)
  *
  * No matter how you do it, mapping 3 bitplanes into a single greyscale
  * bitplane will always result in colors which are very different mapping to
- * the same greyscale value.  Inevitably, these pixels will appear next to 
- * each other at some point in some image, and edge detection on a single 
+ * the same greyscale value.  Inevitably, these pixels will appear next to
+ * each other at some point in some image, and edge detection on a single
  * bitplane will behave quite strangely due to them having the same or nearly
- * the same greyscale values.  Calculating distances between pixels using all 
- * three RGB bitplanes is *way* too time consuming, so single bitplane 
- * edge detection is used for speed's sake.  In order to try to avoid the 
- * color mapping problems of using a single bitplane, 3 different greyscale 
- * mappings are tested for each 3x3 grid, and the one with the most "signal" 
- * (sum of squares difference from center pixel) is chosen.  This usually 
+ * the same greyscale values.  Calculating distances between pixels using all
+ * three RGB bitplanes is *way* too time consuming, so single bitplane
+ * edge detection is used for speed's sake.  In order to try to avoid the
+ * color mapping problems of using a single bitplane, 3 different greyscale
+ * mappings are tested for each 3x3 grid, and the one with the most "signal"
+ * (sum of squares difference from center pixel) is chosen.  This usually
  * results in useable contrast within the 3x3 grid.
  *
- * This results in a whopping 25% increase in overall runtime of the filter 
+ * This results in a whopping 25% increase in overall runtime of the filter
  * over simply using luma or some other single greyscale bitplane, but it
- * does greatly reduce the amount of errors due to greyscale mapping 
- * problems.  I think this is the best compromise between accuracy and 
+ * does greatly reduce the amount of errors due to greyscale mapping
+ * problems.  I think this is the best compromise between accuracy and
  * speed, and is still a lot faster than edge detecting over all three RGB
  * bitplanes.  The increase in image quality is well worth the speed hit.
  *
  */
-int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
-{
+int16 *EdgePlugin::chooseGreyscale(uint16 *pixels) {
 	int i, j;
 	int32 scores[3];
 
-	for (i = 0; i < 3; i++)
-	{
+	for (i = 0; i < 3; i++) {
 		int16 *diff_ptr;
 		int16 *bptr;
 		uint16 *pptr;
@@ -364,8 +356,7 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 
 	/* choose greyscale with highest score, ties decided in GRB order */
 
-	if (scores[1] >= scores[0] && scores[1] >= scores[2])
-	{
+	if (scores[1] >= scores[0] && scores[1] >= scores[2]) {
 		if (!scores[1]) return NULL;
 
 		_chosenGreyscale = _greyscaleTable[1];
@@ -373,8 +364,7 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 		return _greyscaleDiffs[1];
 	}
 
-	if (scores[0] >= scores[1] && scores[0] >= scores[2])
-	{
+	if (scores[0] >= scores[1] && scores[0] >= scores[2]) {
 		if (!scores[0]) return NULL;
 
 		_chosenGreyscale = _greyscaleTable[0];
@@ -392,15 +382,15 @@ int16 * EdgePlugin::chooseGreyscale(uint16 *pixels)
 
 
 /*
- * Calculate the distance between pixels in RGB space.  Greyscale isn't 
- * accurate enough for choosing nearest-neighbors :(  Luma-like weighting 
- * of the individual bitplane distances prior to squaring gives the most 
+ * Calculate the distance between pixels in RGB space.  Greyscale isn't
+ * accurate enough for choosing nearest-neighbors :(  Luma-like weighting
+ * of the individual bitplane distances prior to squaring gives the most
  * useful results.
  *
  */
 int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
 
-#if 1	/* distance between pixels, weighted by roughly luma proportions */
+#if 1   /* distance between pixels, weighted by roughly luma proportions */
 	int32 sum = 0;
 	int16 *rgb_ptr1 = _rgbTable[pixel1];
 	int16 *rgb_ptr2 = _rgbTable[pixel2];
@@ -416,27 +406,22 @@ int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
 	return sum;
 #endif
 
-#if 0	/* distance between pixels, weighted by chosen greyscale proportions */
+#if 0   /* distance between pixels, weighted by chosen greyscale proportions */
 	int32 sum = 0;
 	int16 *rgb_ptr1 = _rgbTable[pixel1];
 	int16 *rgb_ptr2 = _rgbTable[pixel2];
 	int16 diff;
 	int r_shift, g_shift, b_shift;
 
-	if (_chosenGreyscale == _greyscaleTable[1])
-	{
+	if (_chosenGreyscale == _greyscaleTable[1]) {
 		r_shift = 1;
 		g_shift = 2;
 		b_shift = 0;
-	}
-	else if (_chosenGreyscale == _greyscaleTable[0])
-	{
+	} else if (_chosenGreyscale == _greyscaleTable[0]) {
 		r_shift = 2;
 		g_shift = 1;
 		b_shift = 0;
-	}
-	else
-	{
+	} else {
 		r_shift = 0;
 		g_shift = 1;
 		b_shift = 2;
@@ -452,7 +437,7 @@ int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
 	return sum;
 #endif
 
-#if 0	/* distance between pixels, unweighted */
+#if 0   /* distance between pixels, unweighted */
 	int32 sum = 0;
 	int16 *rgb_ptr1 = _rgbTable[pixel1];
 	int16 *rgb_ptr2 = _rgbTable[pixel2];
@@ -468,7 +453,7 @@ int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
 	return sum;
 #endif
 
-#if 0	/* use the greyscale directly */
+#if 0   /* use the greyscale directly */
 	return labs(_chosenGreyscale[pixel1] - _chosenGreyscale[pixel2]);
 #endif
 }
@@ -479,7 +464,7 @@ int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
  * Create vectors of all delta grey values from center pixel, with magnitudes
  * ranging from [1.0, 0.0] (zero difference, maximum difference).  Find
  * the two principle axes of the grid by calculating the eigenvalues and
- * eigenvectors of the inertia tensor.  Use the eigenvectors to calculate the 
+ * eigenvectors of the inertia tensor.  Use the eigenvectors to calculate the
  * edge direction.  In other words, find the angle of the line that optimally
  * passes through the 3x3 pattern of pixels.
  *
@@ -490,10 +475,9 @@ int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
  *
  */
 int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
-		int8 *sim,
-		int32 *return_angle) {
-	struct xy_point
-	{
+                                  int8 *sim,
+                                  int32 *return_angle) {
+	struct xy_point {
 		int16 x, y;
 	};
 
@@ -528,58 +512,56 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	/* if (max_diff == 0) return '0'; */
 
 	/* normalize the differences */
-	scale = (1L<<(GREY_SHIFT+GREY_SHIFT)) / max_diff;
+	scale = (1L << (GREY_SHIFT + GREY_SHIFT)) / max_diff;
 	for (i = 0; i < 8; i++)
-		diffs[i] = (diffs[i] * scale + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+		diffs[i] = (diffs[i] * scale + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
 
 	/*
-	 * Some pixel patterns need to NOT be reversed, since the pixels of 
+	 * Some pixel patterns need to NOT be reversed, since the pixels of
 	 * interest that form the edge to be detected are off-center.
-	 * 
+	 *
 	 */
 
 	/* calculate yes/no similarity matrix to center pixel */
 	/* store the number of similar pixels */
-	cutoff = ((int16)1<<(GREY_SHIFT-3));
+	cutoff = ((int16)1 << (GREY_SHIFT - 3));
 	for (i = 0, _simSum = 0; i < 8; i++)
 		_simSum += (sim[i] = (diffs[i] < cutoff));
 
 	/* don't reverse pattern for off-center knights and sharp corners */
-	if (_simSum >= 3 && _simSum <= 5)
-	{
+	if (_simSum >= 3 && _simSum <= 5) {
 		/* |. */ /* '- */
 		if (sim[1] && sim[4] && sim[5] && !sim[3] && !sim[6] &&
-				(!sim[0] ^ !sim[7]))
+		        (!sim[0] ^ !sim[7]))
 			reverse_flag = 0;
 
 		/* -. */ /* '| */
 		else if (sim[2] && sim[3] && sim[6] && !sim[1] && !sim[4] &&
-				(!sim[0] ^ !sim[7]))
+		         (!sim[0] ^ !sim[7]))
 			reverse_flag = 0;
 
 		/* .- */ /* |' */
 		else if (sim[4] && sim[6] && sim[0] && !sim[1] && !sim[3] &&
-				(!sim[2] ^ !sim[5]))
+		         (!sim[2] ^ !sim[5]))
 			reverse_flag = 0;
 
 		/* .| */ /* -' */
 		else if (sim[1] && sim[3] && sim[7] && !sim[4] && !sim[6] &&
-				(!sim[2] ^ !sim[5]))
+		         (!sim[2] ^ !sim[5]))
 			reverse_flag = 0;
 
 		/* 90 degree corners */
-		else if (_simSum == 3)
-		{
+		else if (_simSum == 3) {
 			if ((sim[0] && sim[1] && sim[3]) ||
-					(sim[1] && sim[2] && sim[4]) ||
-					(sim[3] && sim[5] && sim[6]) ||
-					(sim[4] && sim[6] && sim[7]))
+			        (sim[1] && sim[2] && sim[4]) ||
+			        (sim[3] && sim[5] && sim[6]) ||
+			        (sim[4] && sim[6] && sim[7]))
 				reverse_flag = 0;
 		}
 	}
 
 	/* redo similarity array, less stringent for later checks */
-	cutoff = ((int16)1<<(GREY_SHIFT-1));
+	cutoff = ((int16)1 << (GREY_SHIFT - 1));
 	for (i = 0, _simSum = 0; i < 8; i++)
 		_simSum += (sim[i] = (diffs[i] < cutoff));
 
@@ -587,23 +569,22 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	if (_simSum == 0) return '0';
 
 	/* reverse the difference array, so most similar is closest to 1 */
-	if (reverse_flag)
-	{
-		diffs[0] = ((int16)1<<GREY_SHIFT) - diffs[0];
-		diffs[1] = ((int16)1<<GREY_SHIFT) - diffs[1];
-		diffs[2] = ((int16)1<<GREY_SHIFT) - diffs[2];
-		diffs[3] = ((int16)1<<GREY_SHIFT) - diffs[3];
-		diffs[4] = ((int16)1<<GREY_SHIFT) - diffs[4];
-		diffs[5] = ((int16)1<<GREY_SHIFT) - diffs[5];
-		diffs[6] = ((int16)1<<GREY_SHIFT) - diffs[6];
-		diffs[7] = ((int16)1<<GREY_SHIFT) - diffs[7];
+	if (reverse_flag) {
+		diffs[0] = ((int16)1 << GREY_SHIFT) - diffs[0];
+		diffs[1] = ((int16)1 << GREY_SHIFT) - diffs[1];
+		diffs[2] = ((int16)1 << GREY_SHIFT) - diffs[2];
+		diffs[3] = ((int16)1 << GREY_SHIFT) - diffs[3];
+		diffs[4] = ((int16)1 << GREY_SHIFT) - diffs[4];
+		diffs[5] = ((int16)1 << GREY_SHIFT) - diffs[5];
+		diffs[6] = ((int16)1 << GREY_SHIFT) - diffs[6];
+		diffs[7] = ((int16)1 << GREY_SHIFT) - diffs[7];
 	}
 
 	/* scale diagonals for projection onto axes */
-	diffs[0] = (diffs[0] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
-	diffs[2] = (diffs[2] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
-	diffs[5] = (diffs[5] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
-	diffs[7] = (diffs[7] * one_sqrt2 + ((int16)1<<(GREY_SHIFT-1))) >> GREY_SHIFT;
+	diffs[0] = (diffs[0] * one_sqrt2 + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
+	diffs[2] = (diffs[2] * one_sqrt2 + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
+	diffs[5] = (diffs[5] * one_sqrt2 + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
+	diffs[7] = (diffs[7] * one_sqrt2 + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
 
 	/* create the vectors, centered at 0,0 */
 	xy_points[0].x = -diffs[0];
@@ -624,8 +605,7 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	xy_points[8].y = -diffs[7];
 
 	/* calculate the centroid of the points */
-	for (i = 0; i < 9; i++)
-	{
+	for (i = 0; i < 9; i++) {
 		centx += xy_points[i].x;
 		centy += xy_points[i].y;
 	}
@@ -633,15 +613,13 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	centy /= 9;
 
 	/* translate centroid to 0,0 */
-	for (i = 0; i < 9; i++)
-	{
+	for (i = 0; i < 9; i++) {
 		xy_points[i].x -= centx;
 		xy_points[i].y -= centy;
 	}
 
 	/* fill inertia tensor 3x3 matrix */
-	for (i = 0; i < 9; i++)
-	{
+	for (i = 0; i < 9; i++) {
 		half_matrix[0] += xy_points[i].x * xy_points[i].x;
 		half_matrix[1] += xy_points[i].y * xy_points[i].x;
 		half_matrix[2] += xy_points[i].y * xy_points[i].y;
@@ -650,13 +628,13 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	/* calculate eigenvalues */
 	a = half_matrix[0] - half_matrix[2];
 	b = half_matrix[1] << 1;
-	b = sqrt(b*b + a*a);
+	b = sqrt(b * b + a * a);
 	a = half_matrix[0] + half_matrix[2];
 	eigenval1 = (a + b);
 	eigenval2 = (a - b);
 
-	/* find largest eigenvalue */	
-	if (eigenval1 == eigenval2)	/* X and + shapes */
+	/* find largest eigenvalue */
+	if (eigenval1 == eigenval2) /* X and + shapes */
 		return '*';
 	else if (eigenval1 > eigenval2)
 		best_val = eigenval1;
@@ -676,33 +654,24 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	c = best_val + half_matrix[1];
 	a = c - half_matrix[0];
 	b = c - half_matrix[2];
-	if (b)
-	{
+	if (b) {
 		x = 1.0;
 		ratio = y = a / b;
-	}
-	else if (a)
-	{
+	} else if (a) {
 		y = 1.0;
 		x = b / a;
 		ratio = a / b;
-	}
-	else if (a == b)
-	{
+	} else if (a == b) {
 		*return_angle = 13500;
 		return '\\';
-	}
-	else
+	} else
 		return '*';
 
 	/* calculate angle in degrees * 100 */
-	if (x)
-	{
+	if (x) {
 		angle = (int32) floor(5729.577951307 * fast_atan(ratio) + 0.5);
 		if (x < 0.0) angle += 18000;
-	}
-	else
-	{
+	} else {
 		if (y > 0.0) angle = 9000;
 		else if (y < 0.0) angle = -9000;
 		else return '0';
@@ -735,106 +704,102 @@ int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_fl
 	uint16 center = pixels[4];
 
 	if (center == pixels[0] && center == pixels[2] &&
-			center == pixels[6] && center == pixels[8])
-	{
-		switch(best_dir)
-		{
-			case 5:
-				if (center != pixels[5])	/* < */
-					return 0;
-				break;
-			case 6:
-				if (center != pixels[3])	/* > */
-					return 0;
-				break;
-			case 7:
-				if (center != pixels[7])	/* ^ */
-					return 0;
-				break;
-			case 8:
-				if (center != pixels[1])	/* v */
-					return 0;
-				break;
-		}
-	}
-
-	switch(best_dir)
-	{
-		case 5:		/* < */
-			if (center == pixels[2] && center == pixels[8] &&
-					pixels[1] == pixels[5] && pixels[5] == pixels[7] &&
-					(((center == pixels[0]) ^ (center == pixels[6])) ||
-					 (center == pixels[0] && center == pixels[6] &&
-					  pixels[1] != pixels[3])))
+	        center == pixels[6] && center == pixels[8]) {
+		switch (best_dir) {
+		case 5:
+			if (center != pixels[5])    /* < */
 				return 0;
 			break;
-
-		case 6:		/* > */
-			if (center == pixels[0] && center == pixels[6] &&
-					pixels[1] == pixels[3] && pixels[3] == pixels[7] &&
-					(((center == pixels[2]) ^ (center == pixels[8])) ||
-					 (center == pixels[2] && center == pixels[8] &&
-					  pixels[1] != pixels[5])))
+		case 6:
+			if (center != pixels[3])    /* > */
 				return 0;
 			break;
-
-		case 7:		/* ^ */
-			if (center == pixels[6] && center == pixels[8] &&
-					pixels[3] == pixels[7] && pixels[7] == pixels[5] &&
-					(((center == pixels[0]) ^ (center == pixels[2])) ||
-					 (center == pixels[0] && center == pixels[2] &&
-					  pixels[3] != pixels[1])))
+		case 7:
+			if (center != pixels[7])    /* ^ */
 				return 0;
 			break;
-
-		case 8:		/* v */
-			if (center == pixels[0] && center == pixels[2] &&
-					pixels[1] == pixels[3] && pixels[1] == pixels[5] &&
-					(((center == pixels[6]) ^ (center == pixels[8])) ||
-					 (center == pixels[6] && center == pixels[8] &&
-					  pixels[3] != pixels[7])))
+		case 8:
+			if (center != pixels[1])    /* v */
 				return 0;
 			break;
+		}
 	}
 
-	switch(best_dir)
-	{
-		case 5:
-			if (sim[0] == sim[5] &&
-					sim[1] == sim[3] &&
-					sim[3] == sim[6] &&
-					((sim[2] && sim[7]) ||
-					 (half_flag && _simSum == 2 && sim[4] &&
-					  (sim[2] || sim[7]))))	/* < */
-				return 1;
-			break;
-		case 6:
-			if (sim[2] == sim[7] &&
-					sim[1] == sim[4] &&
-					sim[4] == sim[6] &&
-					((sim[0] && sim[5]) ||
-					 (half_flag && _simSum == 2 && sim[3] &&
-					  (sim[0] || sim[5]))))	/* > */
-				return 1;
-			break;
-		case 7:
-			if (sim[0] == sim[2] &&
-					sim[1] == sim[3] &&
-					sim[3] == sim[4] &&
-					((sim[5] && sim[7]) ||
-					 (half_flag && _simSum == 2 && sim[6] &&
-					  (sim[5] || sim[7]))))	/* ^ */
-				return 1;
-			break;
-		case 8:
-			if (sim[5] == sim[7] &&
-					sim[3] == sim[6] &&
-					sim[4] == sim[6] &&
-					((sim[0] && sim[2]) ||
-					 (half_flag && _simSum == 2 && sim[1] &&
-					  (sim[0] || sim[2]))))	/* v */
-				return 1;
-			break;
+	switch (best_dir) {
+	case 5:     /* < */
+		if (center == pixels[2] && center == pixels[8] &&
+		        pixels[1] == pixels[5] && pixels[5] == pixels[7] &&
+		        (((center == pixels[0]) ^ (center == pixels[6])) ||
+		         (center == pixels[0] && center == pixels[6] &&
+		          pixels[1] != pixels[3])))
+			return 0;
+		break;
+
+	case 6:     /* > */
+		if (center == pixels[0] && center == pixels[6] &&
+		        pixels[1] == pixels[3] && pixels[3] == pixels[7] &&
+		        (((center == pixels[2]) ^ (center == pixels[8])) ||
+		         (center == pixels[2] && center == pixels[8] &&
+		          pixels[1] != pixels[5])))
+			return 0;
+		break;
+
+	case 7:     /* ^ */
+		if (center == pixels[6] && center == pixels[8] &&
+		        pixels[3] == pixels[7] && pixels[7] == pixels[5] &&
+		        (((center == pixels[0]) ^ (center == pixels[2])) ||
+		         (center == pixels[0] && center == pixels[2] &&
+		          pixels[3] != pixels[1])))
+			return 0;
+		break;
+
+	case 8:     /* v */
+		if (center == pixels[0] && center == pixels[2] &&
+		        pixels[1] == pixels[3] && pixels[1] == pixels[5] &&
+		        (((center == pixels[6]) ^ (center == pixels[8])) ||
+		         (center == pixels[6] && center == pixels[8] &&
+		          pixels[3] != pixels[7])))
+			return 0;
+		break;
+	}
+
+	switch (best_dir) {
+	case 5:
+		if (sim[0] == sim[5] &&
+		        sim[1] == sim[3] &&
+		        sim[3] == sim[6] &&
+		        ((sim[2] && sim[7]) ||
+		         (half_flag && _simSum == 2 && sim[4] &&
+		          (sim[2] || sim[7])))) /* < */
+			return 1;
+		break;
+	case 6:
+		if (sim[2] == sim[7] &&
+		        sim[1] == sim[4] &&
+		        sim[4] == sim[6] &&
+		        ((sim[0] && sim[5]) ||
+		         (half_flag && _simSum == 2 && sim[3] &&
+		          (sim[0] || sim[5])))) /* > */
+			return 1;
+		break;
+	case 7:
+		if (sim[0] == sim[2] &&
+		        sim[1] == sim[3] &&
+		        sim[3] == sim[4] &&
+		        ((sim[5] && sim[7]) ||
+		         (half_flag && _simSum == 2 && sim[6] &&
+		          (sim[5] || sim[7])))) /* ^ */
+			return 1;
+		break;
+	case 8:
+		if (sim[5] == sim[7] &&
+		        sim[3] == sim[6] &&
+		        sim[4] == sim[6] &&
+		        ((sim[0] && sim[2]) ||
+		         (half_flag && _simSum == 2 && sim[1] &&
+		          (sim[0] || sim[2])))) /* v */
+			return 1;
+		break;
 	}
 
 	return 0;
@@ -843,15 +808,15 @@ int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_fl
 
 
 /*
- * Take original direction, refine it by testing different pixel difference 
+ * Take original direction, refine it by testing different pixel difference
  * patterns based on the initial gross edge direction.
  *
- * The angle value is not currently used, but may be useful for future 
+ * The angle value is not currently used, but may be useful for future
  * refinement algorithms.
  *
  */
 int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
-		int8 *sim, double angle) {
+                                int8 *sim, double angle) {
 	int32 sums_dir[9] = { 0 };
 	int32 sum;
 	int32 best_sum;
@@ -870,867 +835,820 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 	 *
 	 */
 
-	switch(edge_type)
-	{
-		case '|':
-			diff_array[0]  = labs(bptr[4] - bptr[1]);
-			diff_array[1]  = labs(bptr[4] - bptr[7]);
-			diff_array[2]  = labs(bptr[3] - bptr[0]);
-			diff_array[3]  = labs(bptr[3] - bptr[6]);
-			diff_array[4]  = labs(bptr[5] - bptr[2]);
-			diff_array[5]  = labs(bptr[5] - bptr[8]);
-			diff_array[6]  = labs(bptr[4] - bptr[2]);
-			diff_array[7]  = labs(bptr[4] - bptr[8]);
-			diff_array[8]  = labs(bptr[3] - bptr[1]);
-			diff_array[9]  = labs(bptr[3] - bptr[7]);
-			diff_array[10] = labs(bptr[4] - bptr[0]);
-			diff_array[11] = labs(bptr[4] - bptr[6]);
-			diff_array[12] = labs(bptr[5] - bptr[1]);
-			diff_array[13] = labs(bptr[5] - bptr[7]);
-			diff_array[14] = labs(bptr[0] - bptr[6]);
-			diff_array[15] = labs(bptr[2] - bptr[8]);
-			diff_array[16] = labs(bptr[1] - bptr[7]);
-			diff_array[17] = labs(bptr[0] - bptr[1]);
-			diff_array[18] = labs(bptr[2] - bptr[1]);
-			diff_array[19] = labs(bptr[0] - bptr[2]);
-			diff_array[20] = labs(bptr[3] - bptr[5]);
-			diff_array[21] = labs(bptr[6] - bptr[8]);
-			diff_array[22] = labs(bptr[6] - bptr[7]);
-			diff_array[23] = labs(bptr[8] - bptr[7]);
-
-			/* | vertical */
-			sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
-				diff_array[3] + diff_array[4] + diff_array[5];
-
-			/* << top */
-			sum = diff_array[8] + diff_array[9] +
-				((diff_array[6] + diff_array[7] +
-				  diff_array[12] + diff_array[13]) << 1) +
-				diff_array[14] + diff_array[16] + diff_array[15];
-			sum = (sum * 6) / 13;
-			sums_dir[5] = sum;
-
-			/* >> top */
-			sum = diff_array[12] + diff_array[13] +
-				((diff_array[10] + diff_array[11] +
-				  diff_array[8] + diff_array[9]) << 1) +
-				diff_array[15] + diff_array[16] + diff_array[14];
-			sum = (sum * 6) / 13;
-			sums_dir[6] = sum;
-
-			/* ^ bottom */
-			sum = diff_array[8] + diff_array[12] +
-				((diff_array[11] + diff_array[7]) << 1) +
-				(diff_array[1] << 2) +
-				diff_array[19] + diff_array[20] + diff_array[21];
-			sum = (sum * 6) / 13;
-			sums_dir[7] = sum;
-
-			/* v bottom */
-			sum = diff_array[9] + diff_array[13] +
-				((diff_array[10] + diff_array[6]) << 1) +
-				(diff_array[0] << 2) +
-				diff_array[21] + diff_array[20] + diff_array[19];
-			sum = (sum * 6) / 13;
-			sums_dir[8] = sum;
-
-			/* '| */
-			sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
-				diff_array[12] + (diff_array[3] << 1);
-			/* '| alt */
-			sum = diff_array[10] + diff_array[1] + diff_array[18] +
-				diff_array[4] + diff_array[5] + diff_array[14];
-			if (sum < sums_dir[1])
-				sums_dir[1] = sum;
-
-			/* |. */
-			sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
-				diff_array[9] + (diff_array[4] << 1);
-			/* |. alt */
-			sum = diff_array[0] + diff_array[7] + diff_array[22] +
-				diff_array[3] + diff_array[2] + diff_array[15];
-			if (sum < sums_dir[2])
-				sums_dir[2] = sum;
-
-			/* |' */
-			sums_dir[3] = diff_array[1] + diff_array[3] + diff_array[6] +
-				diff_array[8] + (diff_array[5] << 1);
-			/* |' alt */
-			sum = diff_array[6] + diff_array[1] + diff_array[17] +
-				diff_array[2] + diff_array[3] + diff_array[15];
-			if (sum < sums_dir[3])
-				sums_dir[3] = sum;
-
-			/* .| */
-			sums_dir[4] = diff_array[0] + diff_array[4] + diff_array[11] +
-				diff_array[13] + (diff_array[2] << 1);
-			/* .| alt */
-			sum = diff_array[11] + diff_array[0] + diff_array[23] +
-				diff_array[5] + diff_array[4] + diff_array[14];
-			if (sum < sums_dir[4])
-				sums_dir[4] = sum;
-
-			best_sum = sums_dir[0];
-			for (i = 1; i < 9; i++)
-				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
-			if (best_sum == sums_dir[0]) return 6;	/* | */
-
-			best_dir = 0;
-			for (i = 0, n = 0; i < 9; i++)
-			{
-				if (sums_dir[i] == best_sum)
-				{
-					best_dir = i;
-					n++;
-				}
+	switch (edge_type) {
+	case '|':
+		diff_array[0]  = labs(bptr[4] - bptr[1]);
+		diff_array[1]  = labs(bptr[4] - bptr[7]);
+		diff_array[2]  = labs(bptr[3] - bptr[0]);
+		diff_array[3]  = labs(bptr[3] - bptr[6]);
+		diff_array[4]  = labs(bptr[5] - bptr[2]);
+		diff_array[5]  = labs(bptr[5] - bptr[8]);
+		diff_array[6]  = labs(bptr[4] - bptr[2]);
+		diff_array[7]  = labs(bptr[4] - bptr[8]);
+		diff_array[8]  = labs(bptr[3] - bptr[1]);
+		diff_array[9]  = labs(bptr[3] - bptr[7]);
+		diff_array[10] = labs(bptr[4] - bptr[0]);
+		diff_array[11] = labs(bptr[4] - bptr[6]);
+		diff_array[12] = labs(bptr[5] - bptr[1]);
+		diff_array[13] = labs(bptr[5] - bptr[7]);
+		diff_array[14] = labs(bptr[0] - bptr[6]);
+		diff_array[15] = labs(bptr[2] - bptr[8]);
+		diff_array[16] = labs(bptr[1] - bptr[7]);
+		diff_array[17] = labs(bptr[0] - bptr[1]);
+		diff_array[18] = labs(bptr[2] - bptr[1]);
+		diff_array[19] = labs(bptr[0] - bptr[2]);
+		diff_array[20] = labs(bptr[3] - bptr[5]);
+		diff_array[21] = labs(bptr[6] - bptr[8]);
+		diff_array[22] = labs(bptr[6] - bptr[7]);
+		diff_array[23] = labs(bptr[8] - bptr[7]);
+
+		/* | vertical */
+		sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
+		              diff_array[3] + diff_array[4] + diff_array[5];
+
+		/* << top */
+		sum = diff_array[8] + diff_array[9] +
+		      ((diff_array[6] + diff_array[7] +
+		        diff_array[12] + diff_array[13]) << 1) +
+		      diff_array[14] + diff_array[16] + diff_array[15];
+		sum = (sum * 6) / 13;
+		sums_dir[5] = sum;
+
+		/* >> top */
+		sum = diff_array[12] + diff_array[13] +
+		      ((diff_array[10] + diff_array[11] +
+		        diff_array[8] + diff_array[9]) << 1) +
+		      diff_array[15] + diff_array[16] + diff_array[14];
+		sum = (sum * 6) / 13;
+		sums_dir[6] = sum;
+
+		/* ^ bottom */
+		sum = diff_array[8] + diff_array[12] +
+		      ((diff_array[11] + diff_array[7]) << 1) +
+		      (diff_array[1] << 2) +
+		      diff_array[19] + diff_array[20] + diff_array[21];
+		sum = (sum * 6) / 13;
+		sums_dir[7] = sum;
+
+		/* v bottom */
+		sum = diff_array[9] + diff_array[13] +
+		      ((diff_array[10] + diff_array[6]) << 1) +
+		      (diff_array[0] << 2) +
+		      diff_array[21] + diff_array[20] + diff_array[19];
+		sum = (sum * 6) / 13;
+		sums_dir[8] = sum;
+
+		/* '| */
+		sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
+		              diff_array[12] + (diff_array[3] << 1);
+		/* '| alt */
+		sum = diff_array[10] + diff_array[1] + diff_array[18] +
+		      diff_array[4] + diff_array[5] + diff_array[14];
+		if (sum < sums_dir[1])
+			sums_dir[1] = sum;
+
+		/* |. */
+		sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
+		              diff_array[9] + (diff_array[4] << 1);
+		/* |. alt */
+		sum = diff_array[0] + diff_array[7] + diff_array[22] +
+		      diff_array[3] + diff_array[2] + diff_array[15];
+		if (sum < sums_dir[2])
+			sums_dir[2] = sum;
+
+		/* |' */
+		sums_dir[3] = diff_array[1] + diff_array[3] + diff_array[6] +
+		              diff_array[8] + (diff_array[5] << 1);
+		/* |' alt */
+		sum = diff_array[6] + diff_array[1] + diff_array[17] +
+		      diff_array[2] + diff_array[3] + diff_array[15];
+		if (sum < sums_dir[3])
+			sums_dir[3] = sum;
+
+		/* .| */
+		sums_dir[4] = diff_array[0] + diff_array[4] + diff_array[11] +
+		              diff_array[13] + (diff_array[2] << 1);
+		/* .| alt */
+		sum = diff_array[11] + diff_array[0] + diff_array[23] +
+		      diff_array[5] + diff_array[4] + diff_array[14];
+		if (sum < sums_dir[4])
+			sums_dir[4] = sum;
+
+		best_sum = sums_dir[0];
+		for (i = 1; i < 9; i++)
+			if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+		if (best_sum == sums_dir[0]) return 6;  /* | */
+
+		best_dir = 0;
+		for (i = 0, n = 0; i < 9; i++) {
+			if (sums_dir[i] == best_sum) {
+				best_dir = i;
+				n++;
 			}
+		}
 
-			/* best direction uncertain, return original direction */
-			if (n > 1) return 6;	/* | */
-
-			if (best_dir >= 5)
-				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
-
-			switch(best_dir)
-			{
-				case 1:
-					return 4;		/* '| */
-					break;
-				case 2:
-					return 5;		/* |. */
-					break;
-				case 3:
-					return 7;		/* |' */
-					break;
-				case 4:
-					return 8;		/* .| */
-					break;
-				case 5:
-					if (ok_arrow_flag)
-						return 12;		/* < */
-					break;
-				case 6:
-					if (ok_arrow_flag)
-						return 13;		/* > */
-					break;
-				case 7:
-					if (ok_arrow_flag)
-						return 14;		/* ^ */
-					break;
-				case 8:
-					if (ok_arrow_flag)
-						return 15;		/* V */
-					break;
-				case 0:
-				default:
-					return 6;		/* | */
-					break;
-			}
+		/* best direction uncertain, return original direction */
+		if (n > 1) return 6;    /* | */
 
+		if (best_dir >= 5)
+			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
+
+		switch (best_dir) {
+		case 1:
+			return 4;       /* '| */
+			break;
+		case 2:
+			return 5;       /* |. */
+			break;
+		case 3:
+			return 7;       /* |' */
+			break;
+		case 4:
+			return 8;       /* .| */
+			break;
+		case 5:
+			if (ok_arrow_flag)
+				return 12;      /* < */
 			break;
+		case 6:
+			if (ok_arrow_flag)
+				return 13;      /* > */
+			break;
+		case 7:
+			if (ok_arrow_flag)
+				return 14;      /* ^ */
+			break;
+		case 8:
+			if (ok_arrow_flag)
+				return 15;      /* V */
+			break;
+		case 0:
+		default:
+			return 6;       /* | */
+			break;
+		}
 
-		case '-':
-			diff_array[0]  = labs(bptr[4] - bptr[3]);
-			diff_array[1]  = labs(bptr[4] - bptr[5]);
-			diff_array[2]  = labs(bptr[0] - bptr[1]);
-			diff_array[3]  = labs(bptr[1] - bptr[2]);
-			diff_array[4]  = labs(bptr[7] - bptr[6]);
-			diff_array[5]  = labs(bptr[7] - bptr[8]);
-			diff_array[6]  = labs(bptr[4] - bptr[6]);
-			diff_array[7]  = labs(bptr[4] - bptr[8]);
-			diff_array[8]  = labs(bptr[1] - bptr[3]);
-			diff_array[9]  = labs(bptr[1] - bptr[5]);
-			diff_array[10] = labs(bptr[4] - bptr[0]);
-			diff_array[11] = labs(bptr[4] - bptr[2]);
-			diff_array[12] = labs(bptr[7] - bptr[3]);
-			diff_array[13] = labs(bptr[7] - bptr[5]);
-			diff_array[14] = labs(bptr[0] - bptr[2]);
-			diff_array[15] = labs(bptr[6] - bptr[8]);
-			diff_array[16] = labs(bptr[3] - bptr[5]);
-			diff_array[17] = labs(bptr[0] - bptr[3]);
-			diff_array[18] = labs(bptr[6] - bptr[3]);
-			diff_array[19] = labs(bptr[0] - bptr[6]);
-			diff_array[20] = labs(bptr[1] - bptr[7]);
-			diff_array[21] = labs(bptr[2] - bptr[8]);
-			diff_array[22] = labs(bptr[2] - bptr[5]);
-			diff_array[23] = labs(bptr[8] - bptr[5]);
-
-			/* - horizontal */
-			sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
-				diff_array[3] + diff_array[4] + diff_array[5];
-
-			/* << bottom */
-			sum = diff_array[8] + diff_array[12] +
-				((diff_array[11] + diff_array[7]) << 1) +
-				(diff_array[1] << 2) +
-				diff_array[19] + diff_array[20] + diff_array[21];
-			sum = (sum * 6) / 13;
-			sums_dir[5] = sum;
-
-			/* >> bottom */
-			sum = diff_array[9] + diff_array[13] +
-				((diff_array[10] + diff_array[6]) << 1) +
-				(diff_array[0] << 2) +
-				diff_array[21] + diff_array[20] + diff_array[19];
-			sum = (sum * 6) / 13;
-			sums_dir[6] = sum;
-
-			/* ^ top */
-			sum = diff_array[8] + diff_array[9] +
-				((diff_array[6] + diff_array[7] +
-				  diff_array[12] + diff_array[13]) << 1) +
-				diff_array[14] + diff_array[16] + diff_array[15];
-			sum = (sum * 6) / 13;
-			sums_dir[7] = sum;
-
-			/* v top */
-			sum = diff_array[12] + diff_array[13] +
-				((diff_array[10] + diff_array[11] +
-				  diff_array[8] + diff_array[9]) << 1) +
-				diff_array[15] + diff_array[16] + diff_array[14];
-			sum = (sum * 6) / 13;
-			sums_dir[8] = sum;
-
-			/* '- */
-			sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
-				diff_array[12] + (diff_array[3] << 1);
-			/* '- alt */
-			sum = diff_array[10] + diff_array[1] + diff_array[18] +
-				diff_array[4] + diff_array[5] + diff_array[14];
-			if (sum < sums_dir[1])
-				sums_dir[1] = sum;
-
-			/* -. */
-			sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
-				diff_array[9] + (diff_array[4] << 1);
-			/* -. alt */
-			sum = diff_array[0] + diff_array[7] + diff_array[22] +
-				diff_array[3] + diff_array[2] + diff_array[15];
-			if (sum < sums_dir[2])
-				sums_dir[2] = sum;
-
-			/* -' */
-			sums_dir[3] = diff_array[0] + diff_array[4] + diff_array[11] +
-				diff_array[13] + (diff_array[2] << 1);
-			/* -' alt */
-			sum = diff_array[11] + diff_array[0] + diff_array[23] +
-				diff_array[5] + diff_array[4] + diff_array[14];
-			if (sum < sums_dir[3])
-				sums_dir[3] = sum;
-
-			/* .- */
-			sums_dir[4] = diff_array[1] + diff_array[3] + diff_array[6] +
-				diff_array[8] + (diff_array[5] << 1);
-			/* .- alt */
-			sum = diff_array[6] + diff_array[1] + diff_array[17] +
-				diff_array[2] + diff_array[3] + diff_array[15];
-			if (sum < sums_dir[4])
-				sums_dir[4] = sum;
-
-			best_sum = sums_dir[0];
-			for (i = 1; i < 9; i++)
-				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
-			if (best_sum == sums_dir[0]) return 0;	/* - */
-
-			best_dir = 0;
-			for (i = 0, n = 0; i < 9; i++)
-			{
-				if (sums_dir[i] == best_sum)
-				{
-					best_dir = i;
-					n++;
-				}
+		break;
+
+	case '-':
+		diff_array[0]  = labs(bptr[4] - bptr[3]);
+		diff_array[1]  = labs(bptr[4] - bptr[5]);
+		diff_array[2]  = labs(bptr[0] - bptr[1]);
+		diff_array[3]  = labs(bptr[1] - bptr[2]);
+		diff_array[4]  = labs(bptr[7] - bptr[6]);
+		diff_array[5]  = labs(bptr[7] - bptr[8]);
+		diff_array[6]  = labs(bptr[4] - bptr[6]);
+		diff_array[7]  = labs(bptr[4] - bptr[8]);
+		diff_array[8]  = labs(bptr[1] - bptr[3]);
+		diff_array[9]  = labs(bptr[1] - bptr[5]);
+		diff_array[10] = labs(bptr[4] - bptr[0]);
+		diff_array[11] = labs(bptr[4] - bptr[2]);
+		diff_array[12] = labs(bptr[7] - bptr[3]);
+		diff_array[13] = labs(bptr[7] - bptr[5]);
+		diff_array[14] = labs(bptr[0] - bptr[2]);
+		diff_array[15] = labs(bptr[6] - bptr[8]);
+		diff_array[16] = labs(bptr[3] - bptr[5]);
+		diff_array[17] = labs(bptr[0] - bptr[3]);
+		diff_array[18] = labs(bptr[6] - bptr[3]);
+		diff_array[19] = labs(bptr[0] - bptr[6]);
+		diff_array[20] = labs(bptr[1] - bptr[7]);
+		diff_array[21] = labs(bptr[2] - bptr[8]);
+		diff_array[22] = labs(bptr[2] - bptr[5]);
+		diff_array[23] = labs(bptr[8] - bptr[5]);
+
+		/* - horizontal */
+		sums_dir[0] = diff_array[0] + diff_array[1] + diff_array[2] +
+		              diff_array[3] + diff_array[4] + diff_array[5];
+
+		/* << bottom */
+		sum = diff_array[8] + diff_array[12] +
+		      ((diff_array[11] + diff_array[7]) << 1) +
+		      (diff_array[1] << 2) +
+		      diff_array[19] + diff_array[20] + diff_array[21];
+		sum = (sum * 6) / 13;
+		sums_dir[5] = sum;
+
+		/* >> bottom */
+		sum = diff_array[9] + diff_array[13] +
+		      ((diff_array[10] + diff_array[6]) << 1) +
+		      (diff_array[0] << 2) +
+		      diff_array[21] + diff_array[20] + diff_array[19];
+		sum = (sum * 6) / 13;
+		sums_dir[6] = sum;
+
+		/* ^ top */
+		sum = diff_array[8] + diff_array[9] +
+		      ((diff_array[6] + diff_array[7] +
+		        diff_array[12] + diff_array[13]) << 1) +
+		      diff_array[14] + diff_array[16] + diff_array[15];
+		sum = (sum * 6) / 13;
+		sums_dir[7] = sum;
+
+		/* v top */
+		sum = diff_array[12] + diff_array[13] +
+		      ((diff_array[10] + diff_array[11] +
+		        diff_array[8] + diff_array[9]) << 1) +
+		      diff_array[15] + diff_array[16] + diff_array[14];
+		sum = (sum * 6) / 13;
+		sums_dir[8] = sum;
+
+		/* '- */
+		sums_dir[1] = diff_array[1] + diff_array[5] + diff_array[10] +
+		              diff_array[12] + (diff_array[3] << 1);
+		/* '- alt */
+		sum = diff_array[10] + diff_array[1] + diff_array[18] +
+		      diff_array[4] + diff_array[5] + diff_array[14];
+		if (sum < sums_dir[1])
+			sums_dir[1] = sum;
+
+		/* -. */
+		sums_dir[2] = diff_array[0] + diff_array[2] + diff_array[7] +
+		              diff_array[9] + (diff_array[4] << 1);
+		/* -. alt */
+		sum = diff_array[0] + diff_array[7] + diff_array[22] +
+		      diff_array[3] + diff_array[2] + diff_array[15];
+		if (sum < sums_dir[2])
+			sums_dir[2] = sum;
+
+		/* -' */
+		sums_dir[3] = diff_array[0] + diff_array[4] + diff_array[11] +
+		              diff_array[13] + (diff_array[2] << 1);
+		/* -' alt */
+		sum = diff_array[11] + diff_array[0] + diff_array[23] +
+		      diff_array[5] + diff_array[4] + diff_array[14];
+		if (sum < sums_dir[3])
+			sums_dir[3] = sum;
+
+		/* .- */
+		sums_dir[4] = diff_array[1] + diff_array[3] + diff_array[6] +
+		              diff_array[8] + (diff_array[5] << 1);
+		/* .- alt */
+		sum = diff_array[6] + diff_array[1] + diff_array[17] +
+		      diff_array[2] + diff_array[3] + diff_array[15];
+		if (sum < sums_dir[4])
+			sums_dir[4] = sum;
+
+		best_sum = sums_dir[0];
+		for (i = 1; i < 9; i++)
+			if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+		if (best_sum == sums_dir[0]) return 0;  /* - */
+
+		best_dir = 0;
+		for (i = 0, n = 0; i < 9; i++) {
+			if (sums_dir[i] == best_sum) {
+				best_dir = i;
+				n++;
 			}
+		}
 
-			/* best direction uncertain, return original direction */
-			if (n > 1) return 0;	/* - */
-
-			if (best_dir >= 5)
-				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
-
-			switch(best_dir)
-			{
-				case 1:
-					return 1;		/* '- */
-					break;
-				case 2:
-					return 2;		/* -. */
-					break;
-				case 3:
-					return 10;		/* -' */
-					break;
-				case 4:
-					return 11;		/* .- */
-					break;
-				case 5:
-					if (ok_arrow_flag)
-						return 12;		/* < */
-					break;
-				case 6:
-					if (ok_arrow_flag)
-						return 13;		/* > */
-					break;
-				case 7:
-					if (ok_arrow_flag)
-						return 14;		/* ^ */
-					break;
-				case 8:
-					if (ok_arrow_flag)
-						return 15;		/* V */
-					break;
-				case 0:
-				default:
-					return 0;		/* - */
-					break;
-			}
+		/* best direction uncertain, return original direction */
+		if (n > 1) return 0;    /* - */
+
+		if (best_dir >= 5)
+			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
 
+		switch (best_dir) {
+		case 1:
+			return 1;       /* '- */
+			break;
+		case 2:
+			return 2;       /* -. */
+			break;
+		case 3:
+			return 10;      /* -' */
 			break;
+		case 4:
+			return 11;      /* .- */
+			break;
+		case 5:
+			if (ok_arrow_flag)
+				return 12;      /* < */
+			break;
+		case 6:
+			if (ok_arrow_flag)
+				return 13;      /* > */
+			break;
+		case 7:
+			if (ok_arrow_flag)
+				return 14;      /* ^ */
+			break;
+		case 8:
+			if (ok_arrow_flag)
+				return 15;      /* V */
+			break;
+		case 0:
+		default:
+			return 0;       /* - */
+			break;
+		}
 
-		case '\\':
-
-			/* CHECK -- handle noisy half-diags */
-			if (_simSum == 1)
-			{
-				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
-						pixels[5] == pixels[7])
-				{
-					if (pixels[2] != pixels[1] && pixels[6] != pixels[1])
-					{
-						sum = labs(bptr[2] - bptr[4]) +
-							labs(bptr[6] - bptr[4]);
-
-						if (sim[0] && sum < (labs(bptr[8] - bptr[4]) << 1))
-						{
-							if (bptr[4] > bptr[8])
-							{
-								if (bptr[2] > bptr[4] &&
-										bptr[6] > bptr[4])
-									return 18;		/* '/ */
-							}
-							else
-							{
-								if (bptr[2] < bptr[4] &&
-										bptr[6] < bptr[4])
-									return 18;		/* '/ */
-							}
+		break;
+
+	case '\\':
+
+		/* CHECK -- handle noisy half-diags */
+		if (_simSum == 1) {
+			if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+			        pixels[5] == pixels[7]) {
+				if (pixels[2] != pixels[1] && pixels[6] != pixels[1]) {
+					sum = labs(bptr[2] - bptr[4]) +
+					      labs(bptr[6] - bptr[4]);
+
+					if (sim[0] && sum < (labs(bptr[8] - bptr[4]) << 1)) {
+						if (bptr[4] > bptr[8]) {
+							if (bptr[2] > bptr[4] &&
+							        bptr[6] > bptr[4])
+								return 18;      /* '/ */
+						} else {
+							if (bptr[2] < bptr[4] &&
+							        bptr[6] < bptr[4])
+								return 18;      /* '/ */
 						}
+					}
 
-						if (sim[7] && sum < (labs(bptr[0] - bptr[4]) << 1))
-						{
-							if (bptr[4] > bptr[0])
-							{
-								if (bptr[2] > bptr[4] &&
-										bptr[6] > bptr[4])
-									return 19;		/* /. */
-							}
-							else
-							{
-								if (bptr[2] < bptr[4] &&
-										bptr[6] < bptr[4])
-									return 19;		/* /. */
-							}
+					if (sim[7] && sum < (labs(bptr[0] - bptr[4]) << 1)) {
+						if (bptr[4] > bptr[0]) {
+							if (bptr[2] > bptr[4] &&
+							        bptr[6] > bptr[4])
+								return 19;      /* /. */
+						} else {
+							if (bptr[2] < bptr[4] &&
+							        bptr[6] < bptr[4])
+								return 19;      /* /. */
 						}
 					}
 				}
-
-				if (sim[0] &&
-						labs(bptr[4] - bptr[0]) < ((int16)1<<(GREY_SHIFT-3)))
-					return 3;	/* \ */
-				if (sim[7] &&
-						labs(bptr[4] - bptr[8]) < ((int16)1<<(GREY_SHIFT-3)))
-					return 3;	/* \ */
 			}
 
-			diff_array[0]  = labs(bptr[4] - bptr[0]);
-			diff_array[1]  = labs(bptr[4] - bptr[5]);
-			diff_array[2]  = labs(bptr[3] - bptr[7]);
-			diff_array[3]  = labs(bptr[7] - bptr[8]);
-			diff_array[4]  = labs(bptr[1] - bptr[2]);
-			diff_array[5]  = labs(bptr[4] - bptr[3]);
-			diff_array[6]  = labs(bptr[4] - bptr[8]);
-			diff_array[7]  = labs(bptr[0] - bptr[1]);
-			diff_array[8]  = labs(bptr[1] - bptr[5]);
-			diff_array[9]  = labs(bptr[6] - bptr[7]);
-			diff_array[10] = labs(bptr[4] - bptr[7]);
-			diff_array[11] = labs(bptr[5] - bptr[8]);
-			diff_array[12] = labs(bptr[3] - bptr[6]);
-			diff_array[13] = labs(bptr[4] - bptr[1]);
-			diff_array[14] = labs(bptr[0] - bptr[3]);
-			diff_array[15] = labs(bptr[2] - bptr[5]);
-			diff_array[20] = labs(bptr[0] - bptr[2]);
-			diff_array[21] = labs(bptr[6] - bptr[8]);
-			diff_array[22] = labs(bptr[0] - bptr[6]);
-			diff_array[23] = labs(bptr[2] - bptr[8]);
-			diff_array[16] = labs(bptr[4] - bptr[2]);
-			diff_array[18] = labs(bptr[4] - bptr[6]);
-
-			/* '- */
-			sums_dir[1] = diff_array[0] + diff_array[1] + diff_array[2] +
-				diff_array[3] + (diff_array[4] << 1);
-			/* '- alt */
-			sum = diff_array[0] + diff_array[1] + diff_array[12] +
-				diff_array[9] + diff_array[3] + diff_array[20];
-			if (sum < sums_dir[1])
-				sums_dir[1] = sum;
-
-			/* -. */
-			sums_dir[2] = diff_array[5] + diff_array[6] + diff_array[7] +
-				diff_array[8] + (diff_array[9] << 1);
-			/* -. alt */
-			sum = diff_array[6] + diff_array[5] + diff_array[15] +
-				diff_array[4] + diff_array[7] + diff_array[21];
-			if (sum < sums_dir[2])
-				sums_dir[2] = sum;
-
-			/* '| */
-			sums_dir[3] = diff_array[0] + diff_array[8] + diff_array[10] +
-				diff_array[11] + (diff_array[12] << 1);
-			/* '| alt */
-			sum = diff_array[0] + diff_array[10] + diff_array[4] +
-				diff_array[15] + diff_array[11] + diff_array[22];
-			if (sum < sums_dir[3])
-				sums_dir[3] = sum;
-
-			/* |. */
-			sums_dir[4] = diff_array[2] + diff_array[6] + diff_array[13] +
-				diff_array[14] + (diff_array[15] << 1);
-			/* |. alt */
-			sum = diff_array[13] + diff_array[6] + diff_array[9] +
-				diff_array[12] + diff_array[14] + diff_array[23];
-			if (sum < sums_dir[4])
-				sums_dir[4] = sum;
-
-			/* \ 45 */
-			sums_dir[0] = diff_array[0] + diff_array[6] +
-				(diff_array[2] << 1) + (diff_array[8] << 1);
-
-			/* << top */
-			sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
-				((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
-				  labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
-				labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
-				labs(bptr[2] - bptr[8]);
-			sum = (sum * 6) / 13;
-			sums_dir[5] = sum;
-
-			/* >> top */
-			sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
-				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
-				  labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
-				labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
-				labs(bptr[0] - bptr[6]);
-			sum = (sum * 6) / 13;
-			sums_dir[6] = sum;
-
-			/* ^ top */
-			sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
-				((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
-				  labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
-				labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
-				labs(bptr[6] - bptr[8]);
-			sum = (sum * 6) / 13;
-			sums_dir[7] = sum;
-
-			/* v top */
-			sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
-				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
-				  labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
-				labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
-				labs(bptr[0] - bptr[2]);
-			sum = (sum * 6) / 13;
-			sums_dir[8] = sum;
-
-			best_sum = sums_dir[0];
-			for (i = 1; i < 9; i++)
-				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
-
-			best_dir = 0;
-			for (i = 0, n = 0; i < 9; i++)
-			{
-				if (sums_dir[i] == best_sum)
-				{
-					best_dir = i;
-					n++;
-				}
-			}
+			if (sim[0] &&
+			        labs(bptr[4] - bptr[0]) < ((int16)1 << (GREY_SHIFT - 3)))
+				return 3;   /* \ */
+			if (sim[7] &&
+			        labs(bptr[4] - bptr[8]) < ((int16)1 << (GREY_SHIFT - 3)))
+				return 3;   /* \ */
+		}
 
-			/* CHECK -- handle zig-zags */
-			if (_simSum == 3)
-			{
-				if ((best_dir == 0 || best_dir == 1) &&
-						sim[0] && sim[1] && sim[4])
-					return 1;				/* '- */
-				if ((best_dir == 0 || best_dir == 2) &&
-						sim[3] && sim[6] && sim[7])
-					return 2;				/* -. */
-				if ((best_dir == 0 || best_dir == 3) &&
-						sim[0] && sim[3] && sim[6])
-					return 4;				/* '| */
-				if ((best_dir == 0 || best_dir == 4) &&
-						sim[1] && sim[4] && sim[7])
-					return 5;				/* |. */
+		diff_array[0]  = labs(bptr[4] - bptr[0]);
+		diff_array[1]  = labs(bptr[4] - bptr[5]);
+		diff_array[2]  = labs(bptr[3] - bptr[7]);
+		diff_array[3]  = labs(bptr[7] - bptr[8]);
+		diff_array[4]  = labs(bptr[1] - bptr[2]);
+		diff_array[5]  = labs(bptr[4] - bptr[3]);
+		diff_array[6]  = labs(bptr[4] - bptr[8]);
+		diff_array[7]  = labs(bptr[0] - bptr[1]);
+		diff_array[8]  = labs(bptr[1] - bptr[5]);
+		diff_array[9]  = labs(bptr[6] - bptr[7]);
+		diff_array[10] = labs(bptr[4] - bptr[7]);
+		diff_array[11] = labs(bptr[5] - bptr[8]);
+		diff_array[12] = labs(bptr[3] - bptr[6]);
+		diff_array[13] = labs(bptr[4] - bptr[1]);
+		diff_array[14] = labs(bptr[0] - bptr[3]);
+		diff_array[15] = labs(bptr[2] - bptr[5]);
+		diff_array[20] = labs(bptr[0] - bptr[2]);
+		diff_array[21] = labs(bptr[6] - bptr[8]);
+		diff_array[22] = labs(bptr[0] - bptr[6]);
+		diff_array[23] = labs(bptr[2] - bptr[8]);
+		diff_array[16] = labs(bptr[4] - bptr[2]);
+		diff_array[18] = labs(bptr[4] - bptr[6]);
+
+		/* '- */
+		sums_dir[1] = diff_array[0] + diff_array[1] + diff_array[2] +
+		              diff_array[3] + (diff_array[4] << 1);
+		/* '- alt */
+		sum = diff_array[0] + diff_array[1] + diff_array[12] +
+		      diff_array[9] + diff_array[3] + diff_array[20];
+		if (sum < sums_dir[1])
+			sums_dir[1] = sum;
+
+		/* -. */
+		sums_dir[2] = diff_array[5] + diff_array[6] + diff_array[7] +
+		              diff_array[8] + (diff_array[9] << 1);
+		/* -. alt */
+		sum = diff_array[6] + diff_array[5] + diff_array[15] +
+		      diff_array[4] + diff_array[7] + diff_array[21];
+		if (sum < sums_dir[2])
+			sums_dir[2] = sum;
+
+		/* '| */
+		sums_dir[3] = diff_array[0] + diff_array[8] + diff_array[10] +
+		              diff_array[11] + (diff_array[12] << 1);
+		/* '| alt */
+		sum = diff_array[0] + diff_array[10] + diff_array[4] +
+		      diff_array[15] + diff_array[11] + diff_array[22];
+		if (sum < sums_dir[3])
+			sums_dir[3] = sum;
+
+		/* |. */
+		sums_dir[4] = diff_array[2] + diff_array[6] + diff_array[13] +
+		              diff_array[14] + (diff_array[15] << 1);
+		/* |. alt */
+		sum = diff_array[13] + diff_array[6] + diff_array[9] +
+		      diff_array[12] + diff_array[14] + diff_array[23];
+		if (sum < sums_dir[4])
+			sums_dir[4] = sum;
+
+		/* \ 45 */
+		sums_dir[0] = diff_array[0] + diff_array[6] +
+		              (diff_array[2] << 1) + (diff_array[8] << 1);
+
+		/* << top */
+		sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
+		      ((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
+		        labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
+		      labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
+		      labs(bptr[2] - bptr[8]);
+		sum = (sum * 6) / 13;
+		sums_dir[5] = sum;
+
+		/* >> top */
+		sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
+		      ((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
+		        labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
+		      labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
+		      labs(bptr[0] - bptr[6]);
+		sum = (sum * 6) / 13;
+		sums_dir[6] = sum;
+
+		/* ^ top */
+		sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
+		      ((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
+		        labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
+		      labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
+		      labs(bptr[6] - bptr[8]);
+		sum = (sum * 6) / 13;
+		sums_dir[7] = sum;
+
+		/* v top */
+		sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
+		      ((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
+		        labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
+		      labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
+		      labs(bptr[0] - bptr[2]);
+		sum = (sum * 6) / 13;
+		sums_dir[8] = sum;
+
+		best_sum = sums_dir[0];
+		for (i = 1; i < 9; i++)
+			if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+
+		best_dir = 0;
+		for (i = 0, n = 0; i < 9; i++) {
+			if (sums_dir[i] == best_sum) {
+				best_dir = i;
+				n++;
 			}
+		}
 
-			if (n > 1 && best_sum == sums_dir[0]) return 3;	/* \ */
-
-			/* best direction uncertain, return non-edge to avoid artifacts */
-			if (n > 1) return -1;
-
-			/* CHECK -- diagonal intersections */
-			if (best_dir == 0 &&
-					(sim[1] == sim[4] || sim[3] == sim[6]) &&
-					(sim[1] == sim[3] || sim[4] == sim[6]))
-			{
-				if ((pixels[1] == pixels[3] || pixels[5] == pixels[7]) ||
-						((pixels[0] == pixels[4]) ^ (pixels[8] == pixels[4])))
-				{
-					if (pixels[2] == pixels[4])
-						return 16;		/* \' */
-					if (pixels[6] == pixels[4])
-						return 17;		/* .\ */
-				}
+		/* CHECK -- handle zig-zags */
+		if (_simSum == 3) {
+			if ((best_dir == 0 || best_dir == 1) &&
+			        sim[0] && sim[1] && sim[4])
+				return 1;               /* '- */
+			if ((best_dir == 0 || best_dir == 2) &&
+			        sim[3] && sim[6] && sim[7])
+				return 2;               /* -. */
+			if ((best_dir == 0 || best_dir == 3) &&
+			        sim[0] && sim[3] && sim[6])
+				return 4;               /* '| */
+			if ((best_dir == 0 || best_dir == 4) &&
+			        sim[1] && sim[4] && sim[7])
+				return 5;               /* |. */
+		}
 
-				if (_simSum == 3 && sim[0] && sim[7] &&
-						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
-						pixels[5] == pixels[7])
-				{
-					if (sim[2])
-						return 16;		/* \' */
-					if (sim[5])
-						return 17;		/* .\ */
-				}
+		if (n > 1 && best_sum == sums_dir[0]) return 3; /* \ */
+
+		/* best direction uncertain, return non-edge to avoid artifacts */
+		if (n > 1) return -1;
+
+		/* CHECK -- diagonal intersections */
+		if (best_dir == 0 &&
+		        (sim[1] == sim[4] || sim[3] == sim[6]) &&
+		        (sim[1] == sim[3] || sim[4] == sim[6])) {
+			if ((pixels[1] == pixels[3] || pixels[5] == pixels[7]) ||
+			        ((pixels[0] == pixels[4]) ^ (pixels[8] == pixels[4]))) {
+				if (pixels[2] == pixels[4])
+					return 16;      /* \' */
+				if (pixels[6] == pixels[4])
+					return 17;      /* .\ */
+			}
 
-				if (_simSum == 3 && sim[2] && sim[5])
-				{
-					if (sim[0])
-						return 18;		/* '/ */
-					if (sim[7])
-						return 19;		/* /. */
-				}
+			if (_simSum == 3 && sim[0] && sim[7] &&
+			        pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+			        pixels[5] == pixels[7]) {
+				if (sim[2])
+					return 16;      /* \' */
+				if (sim[5])
+					return 17;      /* .\ */
 			}
 
-			if (best_dir >= 5)
-				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
-
-			switch(best_dir)
-			{
-				case 1:
-					return 1;		/* '- */
-					break;
-				case 2:
-					return 2;		/* -. */
-					break;
-				case 3:
-					return 4;		/* '| */
-					break;
-				case 4:
-					return 5;		/* |. */
-					break;
-				case 5:
-					if (ok_arrow_flag)
-						return 12;		/* < */
-					break;
-				case 6:
-					if (ok_arrow_flag)
-						return 13;		/* > */
-					break;
-				case 7:
-					if (ok_arrow_flag)
-						return 14;		/* ^ */
-					break;
-				case 8:
-					if (ok_arrow_flag)
-						return 15;		/* V */
-					break;
-				case 0:
-				default:
-					return 3;		/* \ */
-					break;
+			if (_simSum == 3 && sim[2] && sim[5]) {
+				if (sim[0])
+					return 18;      /* '/ */
+				if (sim[7])
+					return 19;      /* /. */
 			}
+		}
+
+		if (best_dir >= 5)
+			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
 
+		switch (best_dir) {
+		case 1:
+			return 1;       /* '- */
+			break;
+		case 2:
+			return 2;       /* -. */
+			break;
+		case 3:
+			return 4;       /* '| */
 			break;
+		case 4:
+			return 5;       /* |. */
+			break;
+		case 5:
+			if (ok_arrow_flag)
+				return 12;      /* < */
+			break;
+		case 6:
+			if (ok_arrow_flag)
+				return 13;      /* > */
+			break;
+		case 7:
+			if (ok_arrow_flag)
+				return 14;      /* ^ */
+			break;
+		case 8:
+			if (ok_arrow_flag)
+				return 15;      /* V */
+			break;
+		case 0:
+		default:
+			return 3;       /* \ */
+			break;
+		}
 
-		case '/':
-
-			/* CHECK -- handle noisy half-diags */
-			if (_simSum == 1)
-			{
-				if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
-						pixels[5] == pixels[7])
-				{
-					if (pixels[0] != pixels[1] && pixels[8] != pixels[1])
-					{
-						sum = labs(bptr[0] - bptr[4]) +
-							labs(bptr[8] - bptr[4]);
-
-						if (sim[2] && sum < (labs(bptr[6] - bptr[4]) << 1))
-						{
-							if (bptr[4] > bptr[6])
-							{
-								if (bptr[0] > bptr[4] &&
-										bptr[8] > bptr[4])
-									return 16;		/* \' */
-							}
-							else
-							{
-								if (bptr[0] < bptr[4] &&
-										bptr[8] < bptr[4])
-									return 16;		/* \' */
-							}
+		break;
+
+	case '/':
+
+		/* CHECK -- handle noisy half-diags */
+		if (_simSum == 1) {
+			if (pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+			        pixels[5] == pixels[7]) {
+				if (pixels[0] != pixels[1] && pixels[8] != pixels[1]) {
+					sum = labs(bptr[0] - bptr[4]) +
+					      labs(bptr[8] - bptr[4]);
+
+					if (sim[2] && sum < (labs(bptr[6] - bptr[4]) << 1)) {
+						if (bptr[4] > bptr[6]) {
+							if (bptr[0] > bptr[4] &&
+							        bptr[8] > bptr[4])
+								return 16;      /* \' */
+						} else {
+							if (bptr[0] < bptr[4] &&
+							        bptr[8] < bptr[4])
+								return 16;      /* \' */
 						}
+					}
 
-						if (sim[5] && sum < (labs(bptr[2] - bptr[4]) << 1))
-						{
-							if (bptr[4] > bptr[2])
-							{
-								if (bptr[0] > bptr[4] &&
-										bptr[8] > bptr[4])
-								{
-									if (pixels[6] == pixels[4])
-										return 17;		/* .\ */
-									return 3;			/* \ */
-								}
+					if (sim[5] && sum < (labs(bptr[2] - bptr[4]) << 1)) {
+						if (bptr[4] > bptr[2]) {
+							if (bptr[0] > bptr[4] &&
+							        bptr[8] > bptr[4]) {
+								if (pixels[6] == pixels[4])
+									return 17;      /* .\ */
+								return 3;           /* \ */
 							}
-							else
-							{
-								if (bptr[0] < bptr[4] &&
-										bptr[8] < bptr[4])
-								{
-									if (pixels[6] == pixels[4])
-										return 17;		/* .\ */
-									return 3;			/* \ */
-								}
+						} else {
+							if (bptr[0] < bptr[4] &&
+							        bptr[8] < bptr[4]) {
+								if (pixels[6] == pixels[4])
+									return 17;      /* .\ */
+								return 3;           /* \ */
 							}
 						}
 					}
 				}
-
-				if (sim[2] &&
-						labs(bptr[4] - bptr[2]) < ((int16)1<<(GREY_SHIFT-3)))
-					return 9;	/* / */
-				if (sim[5] &&
-						labs(bptr[4] - bptr[6]) < ((int16)1<<(GREY_SHIFT-3)))
-					return 9;	/* / */
 			}
 
-			diff_array[0]  = labs(bptr[4] - bptr[0]);
-			diff_array[1]  = labs(bptr[4] - bptr[5]);
-			diff_array[3]  = labs(bptr[7] - bptr[8]);
-			diff_array[4]  = labs(bptr[1] - bptr[2]);
-			diff_array[5]  = labs(bptr[4] - bptr[3]);
-			diff_array[6]  = labs(bptr[4] - bptr[8]);
-			diff_array[7]  = labs(bptr[0] - bptr[1]);
-			diff_array[9]  = labs(bptr[6] - bptr[7]);
-			diff_array[10] = labs(bptr[4] - bptr[7]);
-			diff_array[11] = labs(bptr[5] - bptr[8]);
-			diff_array[12] = labs(bptr[3] - bptr[6]);
-			diff_array[13] = labs(bptr[4] - bptr[1]);
-			diff_array[14] = labs(bptr[0] - bptr[3]);
-			diff_array[15] = labs(bptr[2] - bptr[5]);
-			diff_array[16] = labs(bptr[4] - bptr[2]);
-			diff_array[17] = labs(bptr[1] - bptr[3]);
-			diff_array[18] = labs(bptr[4] - bptr[6]);
-			diff_array[19] = labs(bptr[5] - bptr[7]);
-			diff_array[20] = labs(bptr[0] - bptr[2]);
-			diff_array[21] = labs(bptr[6] - bptr[8]);
-			diff_array[22] = labs(bptr[0] - bptr[6]);
-			diff_array[23] = labs(bptr[2] - bptr[8]);
-
-			/* |' */
-			sums_dir[1] = diff_array[10] + diff_array[12] + diff_array[16] +
-				diff_array[17] + (diff_array[11] << 1);
-			/* |' alt */
-			sum = diff_array[16] + diff_array[10] + diff_array[7] +
-				diff_array[14] + diff_array[12] + diff_array[23];
-			if (sum < sums_dir[1])
-				sums_dir[1] = sum;
-
-			/* .| */
-			sums_dir[2] = diff_array[13] + diff_array[15] + diff_array[18] +
-				diff_array[19] + (diff_array[14] << 1);
-			/* .| alt */
-			sum = diff_array[18] + diff_array[13] + diff_array[3] +
-				diff_array[11] + diff_array[15] + diff_array[22];
-			if (sum < sums_dir[2])
-				sums_dir[2] = sum;
-
-			/* -' */
-			sums_dir[3] = diff_array[5] + diff_array[9] + diff_array[16] +
-				diff_array[19] + (diff_array[7] << 1);
-			/* -' alt */
-			sum = diff_array[16] + diff_array[5] + diff_array[11] +
-				diff_array[3] + diff_array[9] + diff_array[20];
-			if (sum < sums_dir[3])
-				sums_dir[3] = sum;
-
-			/* .- */
-			sums_dir[4] = diff_array[1] + diff_array[4] + diff_array[17] +
-				diff_array[18] + (diff_array[3] << 1);
-			/* .- alt */
-			sum = diff_array[18] + diff_array[1] + diff_array[14] +
-				diff_array[7] + diff_array[4] + diff_array[21];
-			if (sum < sums_dir[4])
-				sums_dir[4] = sum;
-
-			/* / 135 */
-			sums_dir[0] = diff_array[16] + diff_array[18] +
-				(diff_array[17] << 1) + (diff_array[19] << 1);
-
-			/* << top */
-			sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
-				((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
-				  labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
-				labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
-				labs(bptr[2] - bptr[8]);
-			sum = (sum * 6) / 13;
-			sums_dir[5] = sum;
-
-			/* >> top */
-			sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
-				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
-				  labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
-				labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
-				labs(bptr[0] - bptr[6]);
-			sum = (sum * 6) / 13;
-			sums_dir[6] = sum;
-
-			/* ^ top */
-			sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
-				((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
-				  labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
-				labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
-				labs(bptr[6] - bptr[8]);
-			sum = (sum * 6) / 13;
-			sums_dir[7] = sum;
-
-			/* v top */
-			sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
-				((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
-				  labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
-				labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
-				labs(bptr[0] - bptr[2]);
-			sum = (sum * 6) / 13;
-			sums_dir[8] = sum;
-
-			best_sum = sums_dir[0];
-			for (i = 1; i < 9; i++)
-				if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
-
-			best_dir = 0;
-			for (i = 0, n = 0; i < 9; i++)
-			{
-				if (sums_dir[i] == best_sum)
-				{
-					best_dir = i;
-					n++;
-				}
-			}
+			if (sim[2] &&
+			        labs(bptr[4] - bptr[2]) < ((int16)1 << (GREY_SHIFT - 3)))
+				return 9;   /* / */
+			if (sim[5] &&
+			        labs(bptr[4] - bptr[6]) < ((int16)1 << (GREY_SHIFT - 3)))
+				return 9;   /* / */
+		}
 
-			/* CHECK -- handle zig-zags */
-			if (_simSum == 3)
-			{
-				if ((best_dir == 0 || best_dir == 1) &&
-						sim[2] && sim[4] && sim[6])
-					return 7;				/* |' */
-				if ((best_dir == 0 || best_dir == 2) &&
-						sim[1] && sim[3] && sim[5])
-					return 8;				/* .| */
-				if ((best_dir == 0 || best_dir == 3) &&
-						sim[1] && sim[2] && sim[3])
-					return 10;				/* -' */
-				if ((best_dir == 0 || best_dir == 4) &&
-						sim[4] && sim[5] && sim[6])
-					return 11;				/* .- */
+		diff_array[0]  = labs(bptr[4] - bptr[0]);
+		diff_array[1]  = labs(bptr[4] - bptr[5]);
+		diff_array[3]  = labs(bptr[7] - bptr[8]);
+		diff_array[4]  = labs(bptr[1] - bptr[2]);
+		diff_array[5]  = labs(bptr[4] - bptr[3]);
+		diff_array[6]  = labs(bptr[4] - bptr[8]);
+		diff_array[7]  = labs(bptr[0] - bptr[1]);
+		diff_array[9]  = labs(bptr[6] - bptr[7]);
+		diff_array[10] = labs(bptr[4] - bptr[7]);
+		diff_array[11] = labs(bptr[5] - bptr[8]);
+		diff_array[12] = labs(bptr[3] - bptr[6]);
+		diff_array[13] = labs(bptr[4] - bptr[1]);
+		diff_array[14] = labs(bptr[0] - bptr[3]);
+		diff_array[15] = labs(bptr[2] - bptr[5]);
+		diff_array[16] = labs(bptr[4] - bptr[2]);
+		diff_array[17] = labs(bptr[1] - bptr[3]);
+		diff_array[18] = labs(bptr[4] - bptr[6]);
+		diff_array[19] = labs(bptr[5] - bptr[7]);
+		diff_array[20] = labs(bptr[0] - bptr[2]);
+		diff_array[21] = labs(bptr[6] - bptr[8]);
+		diff_array[22] = labs(bptr[0] - bptr[6]);
+		diff_array[23] = labs(bptr[2] - bptr[8]);
+
+		/* |' */
+		sums_dir[1] = diff_array[10] + diff_array[12] + diff_array[16] +
+		              diff_array[17] + (diff_array[11] << 1);
+		/* |' alt */
+		sum = diff_array[16] + diff_array[10] + diff_array[7] +
+		      diff_array[14] + diff_array[12] + diff_array[23];
+		if (sum < sums_dir[1])
+			sums_dir[1] = sum;
+
+		/* .| */
+		sums_dir[2] = diff_array[13] + diff_array[15] + diff_array[18] +
+		              diff_array[19] + (diff_array[14] << 1);
+		/* .| alt */
+		sum = diff_array[18] + diff_array[13] + diff_array[3] +
+		      diff_array[11] + diff_array[15] + diff_array[22];
+		if (sum < sums_dir[2])
+			sums_dir[2] = sum;
+
+		/* -' */
+		sums_dir[3] = diff_array[5] + diff_array[9] + diff_array[16] +
+		              diff_array[19] + (diff_array[7] << 1);
+		/* -' alt */
+		sum = diff_array[16] + diff_array[5] + diff_array[11] +
+		      diff_array[3] + diff_array[9] + diff_array[20];
+		if (sum < sums_dir[3])
+			sums_dir[3] = sum;
+
+		/* .- */
+		sums_dir[4] = diff_array[1] + diff_array[4] + diff_array[17] +
+		              diff_array[18] + (diff_array[3] << 1);
+		/* .- alt */
+		sum = diff_array[18] + diff_array[1] + diff_array[14] +
+		      diff_array[7] + diff_array[4] + diff_array[21];
+		if (sum < sums_dir[4])
+			sums_dir[4] = sum;
+
+		/* / 135 */
+		sums_dir[0] = diff_array[16] + diff_array[18] +
+		              (diff_array[17] << 1) + (diff_array[19] << 1);
+
+		/* << top */
+		sum = labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7]) +
+		      ((labs(bptr[4] - bptr[2]) + labs(bptr[4] - bptr[8]) +
+		        labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7])) << 1) +
+		      labs(bptr[0] - bptr[6]) + labs(bptr[1] - bptr[7]) +
+		      labs(bptr[2] - bptr[8]);
+		sum = (sum * 6) / 13;
+		sums_dir[5] = sum;
+
+		/* >> top */
+		sum = labs(bptr[5] - bptr[1]) + labs(bptr[5] - bptr[7]) +
+		      ((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[6]) +
+		        labs(bptr[3] - bptr[1]) + labs(bptr[3] - bptr[7])) << 1) +
+		      labs(bptr[2] - bptr[8]) + labs(bptr[1] - bptr[7]) +
+		      labs(bptr[0] - bptr[6]);
+		sum = (sum * 6) / 13;
+		sums_dir[6] = sum;
+
+		/* ^ top */
+		sum = labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5]) +
+		      ((labs(bptr[4] - bptr[6]) + labs(bptr[4] - bptr[8]) +
+		        labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5])) << 1) +
+		      labs(bptr[0] - bptr[2]) + labs(bptr[3] - bptr[5]) +
+		      labs(bptr[6] - bptr[8]);
+		sum = (sum * 6) / 13;
+		sums_dir[7] = sum;
+
+		/* v top */
+		sum = labs(bptr[7] - bptr[3]) + labs(bptr[7] - bptr[5]) +
+		      ((labs(bptr[4] - bptr[0]) + labs(bptr[4] - bptr[2]) +
+		        labs(bptr[1] - bptr[3]) + labs(bptr[1] - bptr[5])) << 1) +
+		      labs(bptr[6] - bptr[8]) + labs(bptr[3] - bptr[5]) +
+		      labs(bptr[0] - bptr[2]);
+		sum = (sum * 6) / 13;
+		sums_dir[8] = sum;
+
+		best_sum = sums_dir[0];
+		for (i = 1; i < 9; i++)
+			if (sums_dir[i] < best_sum) best_sum = sums_dir[i];
+
+		best_dir = 0;
+		for (i = 0, n = 0; i < 9; i++) {
+			if (sums_dir[i] == best_sum) {
+				best_dir = i;
+				n++;
 			}
+		}
 
-			if (n > 1 && best_sum == sums_dir[0]) return 9;	/* / */
-
-			/* best direction uncertain, return non-edge to avoid artifacts */
-			if (n > 1) return -1;
-
-			/* CHECK -- diagonal intersections */
-			if (best_dir == 0 &&
-					(sim[1] == sim[3] || sim[4] == sim[6]) &&
-					(sim[1] == sim[4] || sim[3] == sim[6]))
-			{
-				if ((pixels[1] == pixels[5] || pixels[3] == pixels[7]) ||
-						((pixels[2] == pixels[4]) ^ (pixels[6] == pixels[4])))
-				{
-					if (pixels[0] == pixels[4])
-						return 18;		/* '/ */
-					if (pixels[8] == pixels[4])
-						return 19;		/* /. */
-				}
+		/* CHECK -- handle zig-zags */
+		if (_simSum == 3) {
+			if ((best_dir == 0 || best_dir == 1) &&
+			        sim[2] && sim[4] && sim[6])
+				return 7;               /* |' */
+			if ((best_dir == 0 || best_dir == 2) &&
+			        sim[1] && sim[3] && sim[5])
+				return 8;               /* .| */
+			if ((best_dir == 0 || best_dir == 3) &&
+			        sim[1] && sim[2] && sim[3])
+				return 10;              /* -' */
+			if ((best_dir == 0 || best_dir == 4) &&
+			        sim[4] && sim[5] && sim[6])
+				return 11;              /* .- */
+		}
 
-				if (_simSum == 3 && sim[2] && sim[5] &&
-						pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
-						pixels[5] == pixels[7])
-				{
-					if (sim[0])
-						return 18;		/* '/ */
-					if (sim[7])
-						return 19;		/* /. */
-				}
+		if (n > 1 && best_sum == sums_dir[0]) return 9; /* / */
+
+		/* best direction uncertain, return non-edge to avoid artifacts */
+		if (n > 1) return -1;
+
+		/* CHECK -- diagonal intersections */
+		if (best_dir == 0 &&
+		        (sim[1] == sim[3] || sim[4] == sim[6]) &&
+		        (sim[1] == sim[4] || sim[3] == sim[6])) {
+			if ((pixels[1] == pixels[5] || pixels[3] == pixels[7]) ||
+			        ((pixels[2] == pixels[4]) ^ (pixels[6] == pixels[4]))) {
+				if (pixels[0] == pixels[4])
+					return 18;      /* '/ */
+				if (pixels[8] == pixels[4])
+					return 19;      /* /. */
+			}
 
-				if (_simSum == 3 && sim[0] && sim[7])
-				{
-					if (sim[2])
-						return 16;		/* \' */
-					if (sim[5])
-						return 17;		/* .\ */
-				}
+			if (_simSum == 3 && sim[2] && sim[5] &&
+			        pixels[1] == pixels[3] && pixels[3] == pixels[5] &&
+			        pixels[5] == pixels[7]) {
+				if (sim[0])
+					return 18;      /* '/ */
+				if (sim[7])
+					return 19;      /* /. */
 			}
 
-			if (best_dir >= 5)
-				ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
-
-			switch(best_dir)
-			{
-				case 1:
-					return 7;		/* |' */
-					break;
-				case 2:
-					return 8;		/* .| */
-					break;
-				case 3:
-					return 10;		/* -' */
-					break;
-				case 4:
-					return 11;		/* .- */
-					break;
-				case 5:
-					if (ok_arrow_flag)
-						return 12;		/* < */
-					break;
-				case 6:
-					if (ok_arrow_flag)
-						return 13;		/* > */
-					break;
-				case 7:
-					if (ok_arrow_flag)
-						return 14;		/* ^ */
-					break;
-				case 8:
-					if (ok_arrow_flag)
-						return 15;		/* V */
-					break;
-					break;
-				case 0:
-				default:
-					return 9;		/* / */
-					break;
+			if (_simSum == 3 && sim[0] && sim[7]) {
+				if (sim[2])
+					return 16;      /* \' */
+				if (sim[5])
+					return 17;      /* .\ */
 			}
+		}
 
-			break;
+		if (best_dir >= 5)
+			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
 
-		case '*':
-			return 127;
+		switch (best_dir) {
+		case 1:
+			return 7;       /* |' */
 			break;
-
-		case '0':
+		case 2:
+			return 8;       /* .| */
+			break;
+		case 3:
+			return 10;      /* -' */
+			break;
+		case 4:
+			return 11;      /* .- */
+			break;
+		case 5:
+			if (ok_arrow_flag)
+				return 12;      /* < */
+			break;
+		case 6:
+			if (ok_arrow_flag)
+				return 13;      /* > */
+			break;
+		case 7:
+			if (ok_arrow_flag)
+				return 14;      /* ^ */
+			break;
+		case 8:
+			if (ok_arrow_flag)
+				return 15;      /* V */
+			break;
+			break;
+		case 0:
 		default:
-			return -1;
+			return 9;       /* / */
 			break;
+		}
+
+		break;
+
+	case '*':
+		return 127;
+		break;
+
+	case '0':
+	default:
+		return -1;
+		break;
 	}
 
 	return -1;
@@ -1754,132 +1672,121 @@ int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 	 */
 
 	/* check to see if original knight is ok */
-	switch(sub_type)
-	{
-		case 1:		/* '- */
-			if (sim[0] && sim[4] &&
-					!(_simSum == 3 && sim[5] &&
-						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 2:		/* -. */
-			if (sim[3] && sim[7] &&
-					!(_simSum == 3 && sim[2] &&
-						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 4:		/* '| */
-			if (sim[0] && sim[6] &&
-					!(_simSum == 3 && sim[2] &&
-						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 5:		/* |. */
-			if (sim[1] && sim[7] &&
-					!(_simSum == 3 && sim[5] &&
-						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 7:		/* |' */
-			if (sim[2] && sim[6] &&
-					!(_simSum == 3 && sim[0] &&
-						pixels[0] == pixels[4] && pixels[2] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 8:		/* .| */
-			if (sim[1] && sim[5] &&
-					!(_simSum == 3 && sim[7] &&
-						pixels[6] == pixels[4] && pixels[8] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 10:	/* -' */
-			if (sim[2] && sim[3] &&
-					!(_simSum == 3 && sim[7] &&
-						pixels[2] == pixels[4] && pixels[8] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		case 11:	/* .- */
-			if (sim[4] && sim[5] &&
-					!(_simSum == 3 && sim[0] &&
-						pixels[0] == pixels[4] && pixels[6] == pixels[4]))
-				ok_orig_flag = 1;
-			break;
-
-		default:	/* not a knight */
-			return sub_type;
-			break;
+	switch (sub_type) {
+	case 1:     /* '- */
+		if (sim[0] && sim[4] &&
+		        !(_simSum == 3 && sim[5] &&
+		          pixels[0] == pixels[4] && pixels[6] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 2:     /* -. */
+		if (sim[3] && sim[7] &&
+		        !(_simSum == 3 && sim[2] &&
+		          pixels[2] == pixels[4] && pixels[8] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 4:     /* '| */
+		if (sim[0] && sim[6] &&
+		        !(_simSum == 3 && sim[2] &&
+		          pixels[0] == pixels[4] && pixels[2] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 5:     /* |. */
+		if (sim[1] && sim[7] &&
+		        !(_simSum == 3 && sim[5] &&
+		          pixels[6] == pixels[4] && pixels[8] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 7:     /* |' */
+		if (sim[2] && sim[6] &&
+		        !(_simSum == 3 && sim[0] &&
+		          pixels[0] == pixels[4] && pixels[2] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 8:     /* .| */
+		if (sim[1] && sim[5] &&
+		        !(_simSum == 3 && sim[7] &&
+		          pixels[6] == pixels[4] && pixels[8] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 10:    /* -' */
+		if (sim[2] && sim[3] &&
+		        !(_simSum == 3 && sim[7] &&
+		          pixels[2] == pixels[4] && pixels[8] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	case 11:    /* .- */
+		if (sim[4] && sim[5] &&
+		        !(_simSum == 3 && sim[0] &&
+		          pixels[0] == pixels[4] && pixels[6] == pixels[4]))
+			ok_orig_flag = 1;
+		break;
+
+	default:    /* not a knight */
+		return sub_type;
+		break;
 	}
 
 	/* look for "better" knights */
-	if (center == pixels[0] && center == pixels[5])	/* '- */
-	{
+	if (center == pixels[0] && center == pixels[5]) { /* '- */
 		dir = 1;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[3] && center == pixels[8])	/* -. */
-	{
+	if (center == pixels[3] && center == pixels[8]) { /* -. */
 		dir = 2;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[0] && center == pixels[7])	/* '| */
-	{
+	if (center == pixels[0] && center == pixels[7]) { /* '| */
 		dir = 4;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[1] && center == pixels[8])	/* |. */
-	{
+	if (center == pixels[1] && center == pixels[8]) { /* |. */
 		dir = 5;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[2] && center == pixels[7])	/* |' */
-	{
+	if (center == pixels[2] && center == pixels[7]) { /* |' */
 		dir = 7;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[1] && center == pixels[6])	/* .| */
-	{
+	if (center == pixels[1] && center == pixels[6]) { /* .| */
 		dir = 8;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[3] && center == pixels[2])	/* -' */
-	{
+	if (center == pixels[3] && center == pixels[2]) { /* -' */
 		dir = 10;
 		flags[dir] = 1;
 		n++;
 	}
-	if (center == pixels[6] && center == pixels[5])	/* .- */
-	{
+	if (center == pixels[6] && center == pixels[5]) { /* .- */
 		dir = 11;
 		flags[dir] = 1;
 		n++;
 	}
 
-	if (n == 0)
-	{
+	if (n == 0) {
 		if (ok_orig_flag) return sub_type;
 		return -1;
 	}
 	if (n == 1) return dir;
-	if (n == 2)
-	{
+	if (n == 2) {
 		/* slanted W patterns */
-		if (flags[1] && flags[5]) return 3;	/* \ */
-		if (flags[2] && flags[4]) return 3;	/* \ */
-		if (flags[7] && flags[11]) return 9;	/* / */
-		if (flags[8] && flags[10]) return 9;	/* / */
+		if (flags[1] && flags[5]) return 3; /* \ */
+		if (flags[2] && flags[4]) return 3; /* \ */
+		if (flags[7] && flags[11]) return 9;    /* / */
+		if (flags[8] && flags[10]) return 9;    /* / */
 	}
 	if (flags[sub_type] && ok_orig_flag) return sub_type;
 
@@ -1889,17 +1796,17 @@ int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 
 
 /* From ScummVM HQ2x/HQ3x scalers (Maxim Stepin and Max Horn) */
-#define highBits	0xF7DEF7DE
-#define lowBits		0x08210821
-#define qhighBits	0xE79CE79C
-#define qlowBits	0x18631863
-#define redblueMask	0xF81F
-#define greenMask	0x07E0
+#define highBits    0xF7DEF7DE
+#define lowBits     0x08210821
+#define qhighBits   0xE79CE79C
+#define qlowBits    0x18631863
+#define redblueMask 0xF81F
+#define greenMask   0x07E0
 
 /* Fill pixel grid without interpolation, using the detected edge */
 template<typename ColorMask>
 void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
-		uint16 *pixels, int sub_type, int16 *bptr) {
+        uint16 *pixels, int sub_type, int16 *bptr) {
 	uint16 *dptr2;
 	int16 tmp_grey;
 	uint16 center = pixels[4];
@@ -1908,581 +1815,576 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 	uint16 *ptmp;
 	int i;
 
-	switch (sub_type)
-	{
-		case 1:		/* '- */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+	switch (sub_type) {
+	case 1:     /* '- */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[6] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[6] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[6] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[6] = pixels[7];
-				else
-					tmp[6] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[6]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[8]);
-				if (diff1 <= diff2)
-					tmp[7] = tmp[6];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[6] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[6] = pixels[7];
+			else
+				tmp[6] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[6]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[8]);
+			if (diff1 <= diff2)
+				tmp[7] = tmp[6];
+		}
 
-		case 2:		/* -. */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+	case 2:     /* -. */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[5];
-				else
-					tmp[2] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[2]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[0]);
-				if (diff1 <= diff2)
-					tmp[1] = tmp[2];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[5];
+			else
+				tmp[2] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[2]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[0]);
+			if (diff1 <= diff2)
+				tmp[1] = tmp[2];
+		}
 
-		case 3:		/* \ */
-		case 16:	/* \' */
-		case 17:	/* .\ */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
-
-			if (sub_type != 16)
-			{
-				tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[5];
-				else
-					tmp[2] = pixels[4];
-			}
+		break;
 
-			if (sub_type != 17)
-			{
-				tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[6] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[6] = pixels[7];
-				else
-					tmp[6] = pixels[4];
-			}
+	case 3:     /* \ */
+	case 16:    /* \' */
+	case 17:    /* .\ */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-			break;
+		if (sub_type != 16) {
+			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[5];
+			else
+				tmp[2] = pixels[4];
+		}
+
+		if (sub_type != 17) {
+			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
+			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[6] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[6] = pixels[7];
+			else
+				tmp[6] = pixels[4];
+		}
 
-		case 4:		/* '| */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+	case 4:     /* '| */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[5];
-				else
-					tmp[2] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[2]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[8]);
-				if (diff1 <= diff2)
-					tmp[5] = tmp[2];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[5];
+			else
+				tmp[2] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[2]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[8]);
+			if (diff1 <= diff2)
+				tmp[5] = tmp[2];
+		}
 
-		case 5:		/* |. */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[6]];
+	case 5:     /* |. */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[6] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[6] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[6] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[6] = pixels[7];
-				else
-					tmp[6] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[6]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[0]);
-				if (diff1 <= diff2)
-					tmp[3] = tmp[6];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[6] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[6] = pixels[7];
+			else
+				tmp[6] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[6]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[0]);
+			if (diff1 <= diff2)
+				tmp[3] = tmp[6];
+		}
 
-		case 7:		/* |' */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+	case 7:     /* |' */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[0]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[6]);
-				if (diff1 <= diff2)
-					tmp[3] = tmp[0];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[0]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[6]);
+			if (diff1 <= diff2)
+				tmp[3] = tmp[0];
+		}
 
-		case 8:		/* .| */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[8]];
+	case 8:     /* .| */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[8] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[8] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[8] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[8] = pixels[7];
-				else
-					tmp[8] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[8]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[2]);
-				if (diff1 <= diff2)
-					tmp[5] = tmp[8];
-			}
-
-			break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[8] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[8] = pixels[7];
+			else
+				tmp[8] = pixels[4];
 
-		case 9:		/* / */
-		case 18:	/* '/ */
-		case 19:	/* /. */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
-
-			if (sub_type != 18)
-			{
-				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-			}
+			tmp_grey = _chosenGreyscale[tmp[8]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[2]);
+			if (diff1 <= diff2)
+				tmp[5] = tmp[8];
+		}
 
-			if (sub_type != 19)
-			{
-				tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[8] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[8] = pixels[7];
-				else
-					tmp[8] = pixels[4];
-			}
+		break;
 
-			break;
+	case 9:     /* / */
+	case 18:    /* '/ */
+	case 19:    /* /. */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-		case 10:	/* -' */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		if (sub_type != 18) {
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+		}
 
+		if (sub_type != 19) {
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[8]];
+			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[8] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[8] = pixels[7];
+			else
+				tmp[8] = pixels[4];
+		}
+
+		break;
+
+	case 10:    /* -' */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[8] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[8] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[8] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[8] = pixels[7];
-				else
-					tmp[8] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[8]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[6]);
-				if (diff1 <= diff2)
-					tmp[7] = tmp[8];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[8] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[8] = pixels[7];
+			else
+				tmp[8] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[8]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[6]);
+			if (diff1 <= diff2)
+				tmp[7] = tmp[8];
+		}
 
-		case 11:	/* .- */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+	case 11:    /* .- */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[0]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[2]);
-				if (diff1 <= diff2)
-					tmp[1] = tmp[0];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
 
-			break;
+			tmp_grey = _chosenGreyscale[tmp[0]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[2]);
+			if (diff1 <= diff2)
+				tmp[1] = tmp[0];
+		}
 
-		case 12:	/* < */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+		break;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+	case 12:    /* < */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
+
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+		}
 
-			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[6] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[6] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[6] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[6] = pixels[7];
-				else
-					tmp[6] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[6] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[6] = pixels[7];
+			else
+				tmp[6] = pixels[4];
+		}
 
-			break;
+		break;
 
-		case 13:	/* > */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+	case 13:    /* > */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[5];
-				else
-					tmp[2] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[5];
+			else
+				tmp[2] = pixels[4];
+		}
 
-			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[8] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[8] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[8] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[8] = pixels[7];
-				else
-					tmp[8] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[8] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[8] = pixels[7];
+			else
+				tmp[8] = pixels[4];
+		}
 
-			break;
+		break;
 
-		case 14:	/* ^ */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+	case 14:    /* ^ */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+		}
 
-			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[5];
-				else
-					tmp[2] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[5];
+			else
+				tmp[2] = pixels[4];
+		}
 
-			break;
+		break;
 
-		case 15:	/* v */
-			for (i = 0; i < 9; i++)
-				tmp[i] = center;
+	case 15:    /* v */
+		for (i = 0; i < 9; i++)
+			tmp[i] = center;
 
-			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[6]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[6] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[6] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[6] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[6] = pixels[7];
-				else
-					tmp[6] = pixels[4];
-			}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[6] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[6] = pixels[7];
+			else
+				tmp[6] = pixels[4];
+		}
 
-			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[8]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[8] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[8] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[8] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[8] = pixels[7];
-				else
-					tmp[8] = pixels[4];
-			}
-
-			break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[8] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[8] = pixels[7];
+			else
+				tmp[8] = pixels[4];
+		}
 
-		case 127:	/* * */
-		case -1:	/* no edge */
-		case 0:		/* - */
-		case 6:		/* | */
-		default:
-			dptr2 = ((uint16 *) (dptr - dstPitch)) - 1;
-			*dptr2++ = center;
-			*dptr2++ = center;
-			*dptr2 = center;
-			dptr2 = ((uint16 *) dptr) - 1;
-			*dptr2++ = center;
-			*dptr2++ = center;
+		break;
+
+	case 127:   /* * */
+	case -1:    /* no edge */
+	case 0:     /* - */
+	case 6:     /* | */
+	default:
+		dptr2 = ((uint16 *)(dptr - dstPitch)) - 1;
+		*dptr2++ = center;
+		*dptr2++ = center;
+		*dptr2 = center;
+		dptr2 = ((uint16 *) dptr) - 1;
+		*dptr2++ = center;
+		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-			*dptr2 = center ^ (uint16) (dxorshift_128() * (1L<<16));
+		*dptr2 = center ^ (uint16)(dxorshift_128() * (1L << 16));
 #else
-			*dptr2 = center;
+		*dptr2 = center;
 #endif
-			dptr2 = ((uint16 *) (dptr + dstPitch)) - 1;
-			*dptr2++ = center;
-			*dptr2++ = center;
-			*dptr2 = center;
+		dptr2 = ((uint16 *)(dptr + dstPitch)) - 1;
+		*dptr2++ = center;
+		*dptr2++ = center;
+		*dptr2 = center;
 
-			return;
+		return;
 
-			break;
+		break;
 	}
 
 	ptmp = tmp;
-	dptr2 = ((uint16 *) (dptr - dstPitch)) - 1;
+	dptr2 = ((uint16 *)(dptr - dstPitch)) - 1;
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp++;
@@ -2490,11 +2392,11 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (uint16) (dxorshift_128() * (1L<<16));
+	*dptr2 = *ptmp++ ^ (uint16)(dxorshift_128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
-	dptr2 = ((uint16 *) (dptr + dstPitch)) - 1;
+	dptr2 = ((uint16 *)(dptr + dstPitch)) - 1;
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp;
@@ -2505,9 +2407,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 /* Fill pixel grid with or without interpolation, using the detected edge */
 template<typename ColorMask>
 void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
-		uint16 *pixels, int sub_type, int16 *bptr,
-		int8 *sim,
-		int interpolate_2x) {
+                                    uint16 *pixels, int sub_type, int16 *bptr,
+                                    int8 *sim,
+                                    int interpolate_2x) {
 	uint16 *dptr2;
 	uint16 center = pixels[4];
 	int32 diff1, diff2, diff3;
@@ -2515,948 +2417,851 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 	uint16 tmp[4];
 	uint16 *ptmp;
 
-	switch (sub_type)
-	{
-		case 1:		/* '- */
-			tmp[0] = tmp[1] = tmp[3] = center;
+	switch (sub_type) {
+	case 1:     /* '- */
+		tmp[0] = tmp[1] = tmp[3] = center;
 
-			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[7];
-				else
-					tmp[2] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[2]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[8]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[2];
-						tmp[2] = interpolate_3_1(tmp_pixel, center);
-						tmp[3] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[7];
+			else
+				tmp[2] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[2]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[8]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[2];
+					tmp[2] = interpolate_3_1(tmp_pixel, center);
+					tmp[3] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[2] = interpolate_1_1(tmp[2], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[2]])
-							tmp[2] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[2] = interpolate_1_1(tmp[2], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[2]])
+						tmp[2] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 2:		/* -. */
-			tmp[0] = tmp[2] = tmp[3] = center;
+	case 2:     /* -. */
+		tmp[0] = tmp[2] = tmp[3] = center;
 
-			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[1] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[1] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[1] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[1] = pixels[5];
-				else
-					tmp[1] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[1]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[0]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[1];
-						tmp[1] = interpolate_3_1(tmp_pixel, center);
-						tmp[0] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[1] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[1] = pixels[5];
+			else
+				tmp[1] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[1]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[0]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[1];
+					tmp[1] = interpolate_3_1(tmp_pixel, center);
+					tmp[0] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[1] = interpolate_1_1(tmp[1], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[1]])
-							tmp[1] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[1] = interpolate_1_1(tmp[1], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[1]])
+						tmp[1] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 3:		/* \ */
-		case 16:	/* \' */
-		case 17:	/* .\ */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
-
-			if (sub_type != 16)
-			{
-				tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[1] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[1] = pixels[5];
-				else
-					tmp[1] = pixels[4];
-
-				if (interpolate_2x)
-				{
-					tmp[1] = interpolate_1_1(tmp[1], center);
-				}
-				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
-				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
-				 * nearest-neighbor, so detect them and don't anti-alias them.
-				 */
-				else if (bptr[4] > _chosenGreyscale[tmp[1]] ||
-						(_simSum == 1 && (sim[0] || sim[7]) &&
-						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
-					tmp[1] = center;
+	case 3:     /* \ */
+	case 16:    /* \' */
+	case 17:    /* .\ */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+		if (sub_type != 16) {
+			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
+			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[1] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[1] = pixels[5];
+			else
+				tmp[1] = pixels[4];
+
+			if (interpolate_2x) {
+				tmp[1] = interpolate_1_1(tmp[1], center);
 			}
+			/* sim test is for hyper-cephalic kitten eyes and squeeze toy
+			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+			 * nearest-neighbor, so detect them and don't anti-alias them.
+			 */
+			else if (bptr[4] > _chosenGreyscale[tmp[1]] ||
+			         (_simSum == 1 && (sim[0] || sim[7]) &&
+			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
+				tmp[1] = center;
+		}
 
-			if (sub_type != 17)
-			{
-				tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[7];
-				else
-					tmp[2] = pixels[4];
-
-				if (interpolate_2x)
-				{
-					tmp[2] = interpolate_1_1(tmp[2], center);
-				}
-				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
-				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
-				 * nearest-neighbor, so detect them and don't anti-alias them.
-				 */
-				else if (bptr[4] > _chosenGreyscale[tmp[2]] ||
-						(_simSum == 1 && (sim[0] || sim[7]) &&
-						 pixels[1] == pixels[3] && pixels[5] == pixels[7]))
-					tmp[2] = center;
+		if (sub_type != 17) {
+			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[7];
+			else
+				tmp[2] = pixels[4];
+
+			if (interpolate_2x) {
+				tmp[2] = interpolate_1_1(tmp[2], center);
 			}
+			/* sim test is for hyper-cephalic kitten eyes and squeeze toy
+			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+			 * nearest-neighbor, so detect them and don't anti-alias them.
+			 */
+			else if (bptr[4] > _chosenGreyscale[tmp[2]] ||
+			         (_simSum == 1 && (sim[0] || sim[7]) &&
+			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
+				tmp[2] = center;
+		}
 
-			break;
+		break;
 
-		case 4:		/* '| */
-			tmp[0] = tmp[2] = tmp[3] = center;
+	case 4:     /* '| */
+		tmp[0] = tmp[2] = tmp[3] = center;
 
-			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[1] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[1] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[1] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[1] = pixels[5];
-				else
-					tmp[1] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[1]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[8]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[1];
-						tmp[1] = interpolate_3_1(tmp_pixel, center);
-						tmp[3] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[1] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[1] = pixels[5];
+			else
+				tmp[1] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[1]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[8]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[1];
+					tmp[1] = interpolate_3_1(tmp_pixel, center);
+					tmp[3] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[1] = interpolate_1_1(tmp[1], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[1]])
-							tmp[1] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[1] = interpolate_1_1(tmp[1], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[1]])
+						tmp[1] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 5:		/* |. */
-			tmp[0] = tmp[1] = tmp[3] = center;
+	case 5:     /* |. */
+		tmp[0] = tmp[1] = tmp[3] = center;
 
-			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[7];
-				else
-					tmp[2] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[2]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[0]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[2];
-						tmp[2] = interpolate_3_1(tmp_pixel, center);
-						tmp[0] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[7];
+			else
+				tmp[2] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[2]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[0]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[2];
+					tmp[2] = interpolate_3_1(tmp_pixel, center);
+					tmp[0] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[2] = interpolate_1_1(tmp[2], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[2]])
-							tmp[2] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[2] = interpolate_1_1(tmp[2], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[2]])
+						tmp[2] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 7:		/* |' */
-			tmp[1] = tmp[2] = tmp[3] = center;
+	case 7:     /* |' */
+		tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[0]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[6]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[0];
-						tmp[0] = interpolate_3_1(tmp_pixel, center);
-						tmp[2] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[0]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[6]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[0];
+					tmp[0] = interpolate_3_1(tmp_pixel, center);
+					tmp[2] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[0] = interpolate_1_1(tmp[0], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[0]])
-							tmp[0] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[0] = interpolate_1_1(tmp[0], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[0]])
+						tmp[0] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 8:		/* .| */
-			tmp[0] = tmp[1] = tmp[2] = center;
+	case 8:     /* .| */
+		tmp[0] = tmp[1] = tmp[2] = center;
 
-			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[3] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[3] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[3] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[3] = pixels[7];
-				else
-					tmp[3] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[3]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[2]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[3];
-						tmp[3] = interpolate_3_1(tmp_pixel, center);
-						tmp[1] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[3] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[3] = pixels[7];
+			else
+				tmp[3] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[3]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[2]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[3];
+					tmp[3] = interpolate_3_1(tmp_pixel, center);
+					tmp[1] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[3] = interpolate_1_1(tmp[3], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[3]])
-							tmp[3] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[3] = interpolate_1_1(tmp[3], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[3]])
+						tmp[3] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 9:		/* / */
-		case 18:	/* '/ */
-		case 19:	/* /. */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
-
-			if (sub_type != 18)
-			{
-				tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				if (interpolate_2x)
-				{
-					tmp[0] = interpolate_1_1(tmp[0], center);
-				}
-				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
-				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
-				 * nearest-neighbor, so detect them and don't anti-alias them.
-				 */
-				else if (bptr[4] > _chosenGreyscale[tmp[0]] ||
-						(_simSum == 1 && (sim[2] || sim[5]) &&
-						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
-					tmp[0] = center;
+	case 9:     /* / */
+	case 18:    /* '/ */
+	case 19:    /* /. */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+		if (sub_type != 18) {
+			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+
+			if (interpolate_2x) {
+				tmp[0] = interpolate_1_1(tmp[0], center);
 			}
+			/* sim test is for hyper-cephalic kitten eyes and squeeze toy
+			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+			 * nearest-neighbor, so detect them and don't anti-alias them.
+			 */
+			else if (bptr[4] > _chosenGreyscale[tmp[0]] ||
+			         (_simSum == 1 && (sim[2] || sim[5]) &&
+			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
+				tmp[0] = center;
+		}
 
-			if (sub_type != 19)
-			{
-				tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[3] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[3] = pixels[7];
-				else
-					tmp[3] = pixels[4];
-
-				if (interpolate_2x)
-				{
-					tmp[3] = interpolate_1_1(tmp[3], center);
-				}
-				/* sim test is for hyper-cephalic kitten eyes and squeeze toy 
-				 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
-				 * nearest-neighbor, so detect them and don't anti-alias them.
-				 */
-				else if (bptr[4] > _chosenGreyscale[tmp[3]] ||
-						(_simSum == 1 && (sim[2] || sim[5]) &&
-						 pixels[1] == pixels[5] && pixels[3] == pixels[7]))
-					tmp[3] = center;
+		if (sub_type != 19) {
+			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
+			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[3] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[3] = pixels[7];
+			else
+				tmp[3] = pixels[4];
+
+			if (interpolate_2x) {
+				tmp[3] = interpolate_1_1(tmp[3], center);
 			}
+			/* sim test is for hyper-cephalic kitten eyes and squeeze toy
+			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
+			 * nearest-neighbor, so detect them and don't anti-alias them.
+			 */
+			else if (bptr[4] > _chosenGreyscale[tmp[3]] ||
+			         (_simSum == 1 && (sim[2] || sim[5]) &&
+			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
+				tmp[3] = center;
+		}
 
-			break;
+		break;
 
-		case 10:	/* -' */
-			tmp[0] = tmp[1] = tmp[2] = center;
+	case 10:    /* -' */
+		tmp[0] = tmp[1] = tmp[2] = center;
 
-			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[3] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[3] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[3] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[3] = pixels[7];
-				else
-					tmp[3] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[3]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[6]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[3];
-						tmp[3] = interpolate_3_1(tmp_pixel, center);
-						tmp[2] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[3] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[3] = pixels[7];
+			else
+				tmp[3] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[3]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[6]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[3];
+					tmp[3] = interpolate_3_1(tmp_pixel, center);
+					tmp[2] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[3] = interpolate_1_1(tmp[3], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[3]])
-							tmp[3] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[3] = interpolate_1_1(tmp[3], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[3]])
+						tmp[3] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 11:	/* .- */
-			tmp[1] = tmp[2] = tmp[3] = center;
+	case 11:    /* .- */
+		tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_KNIGHTS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				tmp_grey = _chosenGreyscale[tmp[0]];
-				diff1 = labs(bptr[4] - tmp_grey);
-				diff2 = labs(bptr[4] - bptr[2]);
-				if (diff1 <= diff2)
-				{
-					if (interpolate_2x)
-					{
-						uint16 tmp_pixel = tmp[0];
-						tmp[0] = interpolate_3_1(tmp_pixel, center);
-						tmp[1] = interpolate_3_1(center, tmp_pixel);
-					}
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+
+			tmp_grey = _chosenGreyscale[tmp[0]];
+			diff1 = labs(bptr[4] - tmp_grey);
+			diff2 = labs(bptr[4] - bptr[2]);
+			if (diff1 <= diff2) {
+				if (interpolate_2x) {
+					uint16 tmp_pixel = tmp[0];
+					tmp[0] = interpolate_3_1(tmp_pixel, center);
+					tmp[1] = interpolate_3_1(center, tmp_pixel);
 				}
-				else
-				{
-					if (interpolate_2x)
-					{
-						tmp[0] = interpolate_1_1(tmp[0], center);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[0]])
-							tmp[0] = center;
-					}
+			} else {
+				if (interpolate_2x) {
+					tmp[0] = interpolate_1_1(tmp[0], center);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[0]])
+						tmp[0] = center;
 				}
 			}
+		}
 
-			break;
+		break;
 
-		case 12:	/* < */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+	case 12:    /* < */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[4] && sim[2])
-				{
-					if (interpolate_2x)
-					{
-						tmp[0] = interpolate_1_1(center, tmp[0]);
-						tmp[2] = interpolate_2_1(center, tmp[0]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[0]])
-							tmp[0] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[4] && sim[2]) {
+				if (interpolate_2x) {
+					tmp[0] = interpolate_1_1(center, tmp[0]);
+					tmp[2] = interpolate_2_1(center, tmp[0]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[0]])
+						tmp[0] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[0] = interpolate_2_1(center, tmp[0]);
-				else
-					tmp[0] = center;
+				break;
 			}
 
-			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+			if (interpolate_2x)
+				tmp[0] = interpolate_2_1(center, tmp[0]);
+			else
+				tmp[0] = center;
+		}
+
+		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[7];
-				else
-					tmp[2] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[4] && sim[7])
-				{
-					if (interpolate_2x)
-					{
-						tmp[2] = interpolate_1_1(center, tmp[2]);
-						tmp[0] = interpolate_2_1(center, tmp[2]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[2]])
-							tmp[2] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[7];
+			else
+				tmp[2] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[4] && sim[7]) {
+				if (interpolate_2x) {
+					tmp[2] = interpolate_1_1(center, tmp[2]);
+					tmp[0] = interpolate_2_1(center, tmp[2]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[2]])
+						tmp[2] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[2] = interpolate_2_1(center, tmp[2]);
-				else
-					tmp[2] = center;
+				break;
 			}
 
-			break;
+			if (interpolate_2x)
+				tmp[2] = interpolate_2_1(center, tmp[2]);
+			else
+				tmp[2] = center;
+		}
 
-		case 13:	/* > */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+		break;
 
-			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[1]];
+	case 13:    /* > */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[1] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[1] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[1] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[1] = pixels[5];
-				else
-					tmp[1] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[3] && sim[0])
-				{
-					if (interpolate_2x)
-					{
-						tmp[1] = interpolate_1_1(center, tmp[1]);
-						tmp[3] = interpolate_2_1(center, tmp[1]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[1]])
-							tmp[1] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[1] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[1] = pixels[5];
+			else
+				tmp[1] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[3] && sim[0]) {
+				if (interpolate_2x) {
+					tmp[1] = interpolate_1_1(center, tmp[1]);
+					tmp[3] = interpolate_2_1(center, tmp[1]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[1]])
+						tmp[1] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[1] = interpolate_2_1(center, tmp[1]);
-				else
-					tmp[1] = center;
+				break;
 			}
 
-			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[3]];
+			if (interpolate_2x)
+				tmp[1] = interpolate_2_1(center, tmp[1]);
+			else
+				tmp[1] = center;
+		}
+
+		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[3] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[3] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[3] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[3] = pixels[7];
-				else
-					tmp[3] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[3] && sim[5])
-				{
-					if (interpolate_2x)
-					{
-						tmp[3] = interpolate_1_1(center, tmp[3]);
-						tmp[1] = interpolate_2_1(center, tmp[3]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[3]])
-							tmp[3] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[3] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[3] = pixels[7];
+			else
+				tmp[3] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[3] && sim[5]) {
+				if (interpolate_2x) {
+					tmp[3] = interpolate_1_1(center, tmp[3]);
+					tmp[1] = interpolate_2_1(center, tmp[3]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[3]])
+						tmp[3] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[3] = interpolate_2_1(center, tmp[3]);
-				else
-					tmp[3] = center;
+				break;
 			}
 
-			break;
+			if (interpolate_2x)
+				tmp[3] = interpolate_2_1(center, tmp[3]);
+			else
+				tmp[3] = center;
+		}
 
-		case 14:	/* ^ */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+		break;
 
-			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			tmp_grey = _chosenGreyscale[tmp[0]];
+	case 14:    /* ^ */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
+		tmp_grey = _chosenGreyscale[tmp[0]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[1]);
-			diff2 = labs(bptr[4] - bptr[3]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[0] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[1]);
+		diff2 = labs(bptr[4] - bptr[3]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[0] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-				diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[0] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[0] = pixels[3];
-				else
-					tmp[0] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[6] && sim[5])
-				{
-					if (interpolate_2x)
-					{
-						tmp[0] = interpolate_1_1(center, tmp[0]);
-						tmp[1] = interpolate_2_1(center, tmp[0]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[0]])
-							tmp[0] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[0] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[0] = pixels[3];
+			else
+				tmp[0] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[6] && sim[5]) {
+				if (interpolate_2x) {
+					tmp[0] = interpolate_1_1(center, tmp[0]);
+					tmp[1] = interpolate_2_1(center, tmp[0]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[0]])
+						tmp[0] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[0] = interpolate_2_1(center, tmp[0]);
-				else
-					tmp[0] = center;
+				break;
 			}
 
-			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			tmp_grey = _chosenGreyscale[tmp[1]];
+			if (interpolate_2x)
+				tmp[0] = interpolate_2_1(center, tmp[0]);
+			else
+				tmp[0] = center;
+		}
+
+		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
+		tmp_grey = _chosenGreyscale[tmp[1]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[5]);
-			diff2 = labs(bptr[4] - bptr[1]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[1] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[5]);
+		diff2 = labs(bptr[4] - bptr[1]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[1] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-				diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-				diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[1] = pixels[1];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[1] = pixels[5];
-				else
-					tmp[1] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[6] && sim[7])
-				{
-					if (interpolate_2x)
-					{
-						tmp[1] = interpolate_1_1(center, tmp[1]);
-						tmp[0] = interpolate_2_1(center, tmp[1]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[1]])
-							tmp[1] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[1] = pixels[1];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[1] = pixels[5];
+			else
+				tmp[1] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[6] && sim[7]) {
+				if (interpolate_2x) {
+					tmp[1] = interpolate_1_1(center, tmp[1]);
+					tmp[0] = interpolate_2_1(center, tmp[1]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[1]])
+						tmp[1] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[1] = interpolate_2_1(center, tmp[1]);
-				else
-					tmp[1] = center;
+				break;
 			}
 
-			break;
+			if (interpolate_2x)
+				tmp[1] = interpolate_2_1(center, tmp[1]);
+			else
+				tmp[1] = center;
+		}
 
-		case 15:	/* v */
-			tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+		break;
 
-			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[2]];
+	case 15:    /* v */
+		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
+
+		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[2]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[3]);
-			diff2 = labs(bptr[4] - bptr[7]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[2] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[3]);
+		diff2 = labs(bptr[4] - bptr[7]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[2] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-				diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[2] = pixels[3];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[2] = pixels[7];
-				else
-					tmp[2] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[1] && sim[0])
-				{
-					if (interpolate_2x)
-					{
-						tmp[2] = interpolate_1_1(center, tmp[2]);
-						tmp[3] = interpolate_2_1(center, tmp[2]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[2]])
-							tmp[2] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[2] = pixels[3];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[2] = pixels[7];
+			else
+				tmp[2] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[1] && sim[0]) {
+				if (interpolate_2x) {
+					tmp[2] = interpolate_1_1(center, tmp[2]);
+					tmp[3] = interpolate_2_1(center, tmp[2]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[2]])
+						tmp[2] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[2] = interpolate_2_1(center, tmp[2]);
-				else
-					tmp[2] = center;
+				break;
 			}
 
-			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			tmp_grey = _chosenGreyscale[tmp[3]];
+			if (interpolate_2x)
+				tmp[2] = interpolate_2_1(center, tmp[2]);
+			else
+				tmp[2] = center;
+		}
+
+		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
+		tmp_grey = _chosenGreyscale[tmp[3]];
 #if PARANOID_ARROWS
-			diff1 = labs(bptr[4] - bptr[7]);
-			diff2 = labs(bptr[4] - bptr[5]);
-			diff3 = labs(bptr[4] - tmp_grey);
-			if (diff1 < diff3 || diff2 < diff3)
-				tmp[3] = center;
-			else	/* choose nearest pixel */
+		diff1 = labs(bptr[4] - bptr[7]);
+		diff2 = labs(bptr[4] - bptr[5]);
+		diff3 = labs(bptr[4] - tmp_grey);
+		if (diff1 < diff3 || diff2 < diff3)
+			tmp[3] = center;
+		else    /* choose nearest pixel */
 #endif
-			{
-				diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-				diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-				diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
-				if (diff1 <= diff2 && diff1 <= diff3)
-					tmp[3] = pixels[5];
-				else if (diff2 <= diff1 && diff2 <= diff3)
-					tmp[3] = pixels[7];
-				else
-					tmp[3] = pixels[4];
-
-				/* check for half-arrow */
-				if (_simSum == 2 && sim[1] && sim[2])
-				{
-					if (interpolate_2x)
-					{
-						tmp[3] = interpolate_1_1(center, tmp[3]);
-						tmp[2] = interpolate_2_1(center, tmp[3]);
-					}
-					else
-					{
-						if (bptr[4] > _chosenGreyscale[tmp[3]])
-							tmp[3] = center;
-					}
-
-					break;
+		{
+			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			if (diff1 <= diff2 && diff1 <= diff3)
+				tmp[3] = pixels[5];
+			else if (diff2 <= diff1 && diff2 <= diff3)
+				tmp[3] = pixels[7];
+			else
+				tmp[3] = pixels[4];
+
+			/* check for half-arrow */
+			if (_simSum == 2 && sim[1] && sim[2]) {
+				if (interpolate_2x) {
+					tmp[3] = interpolate_1_1(center, tmp[3]);
+					tmp[2] = interpolate_2_1(center, tmp[3]);
+				} else {
+					if (bptr[4] > _chosenGreyscale[tmp[3]])
+						tmp[3] = center;
 				}
 
-				if (interpolate_2x)
-					tmp[3] = interpolate_2_1(center, tmp[3]);
-				else
-					tmp[3] = center;
+				break;
 			}
 
-			break;
+			if (interpolate_2x)
+				tmp[3] = interpolate_2_1(center, tmp[3]);
+			else
+				tmp[3] = center;
+		}
+
+		break;
 
-		case -1:	/* no edge */
-		case 0:		/* - */
-		case 6:		/* | */
-		case 127:	/* * */
-		default:	/* no edge */
-			dptr2 = (uint16 *) dptr;
-			*dptr2++ = center;
+	case -1:    /* no edge */
+	case 0:     /* - */
+	case 6:     /* | */
+	case 127:   /* * */
+	default:    /* no edge */
+		dptr2 = (uint16 *) dptr;
+		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-			*dptr2 = center ^ (uint16) (dxorshift_128() * (1L<<16));
+		*dptr2 = center ^ (uint16)(dxorshift_128() * (1L << 16));
 #else
-			*dptr2 = center;
+		*dptr2 = center;
 #endif
-			dptr2 = (uint16 *) (dptr + dstPitch);
-			*dptr2++ = center;
-			*dptr2 = center;
+		dptr2 = (uint16 *)(dptr + dstPitch);
+		*dptr2++ = center;
+		*dptr2 = center;
 
-			return;
+		return;
 
-			break;
+		break;
 	}
 
 	ptmp = tmp;
 	dptr2 = (uint16 *) dptr;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (uint16) (dxorshift_128() * (1L<<16));
+	*dptr2 = *ptmp++ ^ (uint16)(dxorshift_128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
-	dptr2 = (uint16 *) (dptr + dstPitch);
+	dptr2 = (uint16 *)(dptr + dstPitch);
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp;
 }
@@ -3466,17 +3271,15 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 #if HANDLE_TRANSPARENT_OVERLAYS
 /* Deal with transparent pixels */
 void handle_transparent_overlay(uint16 transp, uint16 *pixels,
-		int16 *bplane, int16 *diffs)
-{
+                                int16 *bplane, int16 *diffs) {
 	int16 tmp_grey;
 	int16 max_diff;
-	int16 min_grey = ((int16)1<<RGB_SHIFT);
+	int16 min_grey = ((int16)1 << RGB_SHIFT);
 	int16 max_grey = 0;
 	int i;
 
 	/* find min and max grey values in window */
-	for (i = 0; i < 9; i++)
-	{
+	for (i = 0; i < 9; i++) {
 		if (bplane[i] < min_grey) min_grey = bplane[i];
 		if (bplane[i] > max_grey) max_grey = bplane[i];
 	}
@@ -3486,8 +3289,7 @@ void handle_transparent_overlay(uint16 transp, uint16 *pixels,
 	tmp_grey = (min_grey + max_grey + 1) >> 1;
 	max_diff = max_grey - min_grey;
 
-	if (pixels[4] == transp)	/* center pixel is transparent */
-	{
+	if (pixels[4] == transp) {  /* center pixel is transparent */
 		/* set all transparent pixels to middle grey */
 		if (pixels[0] == transp) bplane[0] = tmp_grey;
 		if (pixels[1] == transp) bplane[1] = tmp_grey;
@@ -3508,50 +3310,40 @@ void handle_transparent_overlay(uint16 transp, uint16 *pixels,
 		if (pixels[6] != transp) diffs[5] = max_diff;
 		if (pixels[7] != transp) diffs[6] = max_diff;
 		if (pixels[8] != transp) diffs[7] = max_diff;
-	}
-	else			/* center pixel is non-transparent */
-	{
+	} else {        /* center pixel is non-transparent */
 		/* choose transparent grey value to give largest contrast */
 		tmp_grey = (bplane[4] >= tmp_grey) ? min_grey : max_grey;
 
 		/* set new transparent pixel values and diffs */
-		if (pixels[0] == transp)
-		{
+		if (pixels[0] == transp) {
 			bplane[0] = tmp_grey;
 			diffs[0] = max_diff;
 		}
-		if (pixels[1] == transp)
-		{
+		if (pixels[1] == transp) {
 			bplane[1] = tmp_grey;
 			diffs[1] = max_diff;
 		}
-		if (pixels[2] == transp)
-		{
+		if (pixels[2] == transp) {
 			bplane[2] = tmp_grey;
 			diffs[2] = max_diff;
 		}
-		if (pixels[3] == transp)
-		{
+		if (pixels[3] == transp) {
 			bplane[3] = tmp_grey;
 			diffs[3] = max_diff;
 		}
-		if (pixels[5] == transp)
-		{
+		if (pixels[5] == transp) {
 			bplane[5] = tmp_grey;
 			diffs[4] = max_diff;
 		}
-		if (pixels[6] == transp)
-		{
+		if (pixels[6] == transp) {
 			bplane[6] = tmp_grey;
 			diffs[5] = max_diff;
 		}
-		if (pixels[7] == transp)
-		{
+		if (pixels[7] == transp) {
 			bplane[7] = tmp_grey;
 			diffs[6] = max_diff;
 		}
-		if (pixels[8] == transp)
-		{
+		if (pixels[8] == transp) {
 			bplane[8] = tmp_grey;
 			diffs[7] = max_diff;
 		}
@@ -3562,8 +3354,7 @@ void handle_transparent_overlay(uint16 transp, uint16 *pixels,
 
 
 /* Check for changed pixel grid, return 1 if unchanged. */
-int check_unchanged_pixels(uint16 *old_src_ptr, uint16 *pixels, int w)
-{
+int check_unchanged_pixels(uint16 *old_src_ptr, uint16 *pixels, int w) {
 	uint16 *dptr16;
 
 	dptr16 = old_src_ptr - w - 1;
@@ -3588,14 +3379,13 @@ int check_unchanged_pixels(uint16 *old_src_ptr, uint16 *pixels, int w)
 /* Draw unchanged pixel grid, 3x */
 /* old_dptr16 starts in top left of grid, dptr16 in center */
 void draw_unchanged_grid_3x(uint16 *dptr16, int dstPitch,
-		uint16 *old_dptr16, int old_dst_inc)
-{
+                            uint16 *old_dptr16, int old_dst_inc) {
 	uint16 *sptr;
 	uint16 *dptr;
 	uint8 *dptr8 = (uint8 *) dptr16;
 
 	sptr = old_dptr16;
-	dptr = (uint16 *) (dptr8 - dstPitch) - 1;
+	dptr = (uint16 *)(dptr8 - dstPitch) - 1;
 	*dptr++ = *sptr++;
 	*dptr++ = *sptr++;
 	*dptr = *sptr;
@@ -3607,7 +3397,7 @@ void draw_unchanged_grid_3x(uint16 *dptr16, int dstPitch,
 	*dptr = *sptr;
 
 	sptr = old_dptr16 + old_dst_inc + old_dst_inc;
-	dptr = (uint16 *) (dptr8 + dstPitch) - 1;
+	dptr = (uint16 *)(dptr8 + dstPitch) - 1;
 	*dptr++ = *sptr++;
 	*dptr++ = *sptr++;
 	*dptr = *sptr;
@@ -3617,8 +3407,7 @@ void draw_unchanged_grid_3x(uint16 *dptr16, int dstPitch,
 
 /* Draw unchanged pixel grid, 2x */
 void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
-		uint16 *old_dptr16, int old_dst_inc)
-{
+                            uint16 *old_dptr16, int old_dst_inc) {
 	uint16 *sptr;
 	uint16 *dptr;
 	uint8 *dptr8 = (uint8 *) dptr16;
@@ -3629,7 +3418,7 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 	*dptr = *sptr;
 
 	sptr = old_dptr16 + old_dst_inc;
-	dptr = (uint16 *) (dptr8 + dstPitch);
+	dptr = (uint16 *)(dptr8 + dstPitch);
 	*dptr++ = *sptr++;
 	*dptr = *sptr;
 }
@@ -3638,9 +3427,9 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 /* Perform edge detection, draw the new 3x pixels */
 template<typename ColorMask>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
-		int w, int h, int w_new, int h_new,
-		int srcPitch, int dstPitch,
-		int overlay_flag) {
+                                 int w, int h, int w_new, int h_new,
+                                 int srcPitch, int dstPitch,
+                                 int overlay_flag) {
 	int x, y;
 	int w2 = w + 2;
 	const uint8 *sptr8 = src;
@@ -3656,7 +3445,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 #if 0
 #if HANDLE_TRANSPARENT_OVERLAYS
-	uint16 transp = 0;	/* transparent color */
+	uint16 transp = 0;  /* transparent color */
 
 	/* assume bitmap is padded by a transparent border, take src-1 pixel */
 	if (overlay_flag) transp = *((const uint16 *) src - 1);
@@ -3673,31 +3462,30 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3) {
 		for (x = 0,
-				sptr16 = (const uint16 *) sptr8,
-				dptr16 = (uint16 *) dptr8;
-				x < w; x++, sptr16++, dptr16 += 3) {
+		        sptr16 = (const uint16 *) sptr8,
+		        dptr16 = (uint16 *) dptr8;
+		        x < w; x++, sptr16++, dptr16 += 3) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
 
-			sptr2 = ((const uint16 *) ((const uint8 *) sptr16 - srcPitch)) - 1;
-			addr3 = ((const uint16 *) ((const uint8 *) sptr16 + srcPitch)) + 1;
+			sptr2 = ((const uint16 *)((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const uint16 *)((const uint8 *) sptr16 + srcPitch)) + 1;
 
 			/* fill the 3x3 grid */
-			memcpy(pixels, sptr2, 3*sizeof(uint16));
-			memcpy(pixels+3, sptr16 - 1, 3*sizeof(uint16));
-			memcpy(pixels+6, addr3 - 2, 3*sizeof(uint16));
+			memcpy(pixels, sptr2, 3 * sizeof(uint16));
+			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
+			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
 
 #if 0
 			/* skip interior unchanged 3x3 blocks */
 			if (*sptr16 == *old_sptr16 &&
 #if DEBUG_DRAW_REFRESH_BORDERS
-					x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+			        x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-					check_unchanged_pixels(old_sptr16, pixels, old_src_inc))
-			{
+			        check_unchanged_pixels(old_sptr16, pixels, old_src_inc)) {
 				draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
-						old_dst_inc);
+				                       old_dst_inc);
 
 #if DEBUG_REFRESH_RANDOM_XOR
 				*(dptr16 + 1) = 0;
@@ -3709,10 +3497,9 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			diffs = chooseGreyscale(pixels);
 
 			/* block of solid color */
-			if (!diffs)
-			{
+			if (!diffs) {
 				anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
-						0, NULL);
+				                                    0, NULL);
 				continue;
 			}
 
@@ -3726,14 +3513,14 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,
-					sim, &angle);
+			                              sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
-					sim, angle);
+			                           sim, angle);
 			if (sub_type >= 0)
 				sub_type = fixKnights(sub_type, pixels, sim);
 
 			anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
-					sub_type, bplane);
+			                                    sub_type, bplane);
 		}
 	}
 }
@@ -3743,10 +3530,10 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 /* Perform edge detection, draw the new 2x pixels */
 template<typename ColorMask>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
-		int w, int h, int w_new, int h_new,
-		int srcPitch, int dstPitch,
-		int overlay_flag,
-		int interpolate_2x) {
+                                 int w, int h, int w_new, int h_new,
+                                 int srcPitch, int dstPitch,
+                                 int overlay_flag,
+                                 int interpolate_2x) {
 	int x, y;
 	int w2 = w + 2;
 	const uint8 *sptr8 = src;
@@ -3760,9 +3547,9 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch2 = dstPitch << 1;
 
-#if 0 
+#if 0
 #if HANDLE_TRANSPARENT_OVERLAYS
-	uint16 transp = 0;	/* transparent color */
+	uint16 transp = 0;  /* transparent color */
 
 	/* assume bitmap is padded by a transparent border, take src-1 pixel */
 	if (overlay_flag) transp = *((const uint16 *) src - 1);
@@ -3780,31 +3567,30 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2) {
 		for (x = 0,
-				sptr16 = (const uint16 *) sptr8,
-				dptr16 = (uint16 *) dptr8;
-				x < w; x++, sptr16++, dptr16 += 2) {
+		        sptr16 = (const uint16 *) sptr8,
+		        dptr16 = (uint16 *) dptr8;
+		        x < w; x++, sptr16++, dptr16 += 2) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
 
-			sptr2 = ((const uint16 *) ((const uint8 *) sptr16 - srcPitch)) - 1;
-			addr3 = ((const uint16 *) ((const uint8 *) sptr16 + srcPitch)) + 1;
+			sptr2 = ((const uint16 *)((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const uint16 *)((const uint8 *) sptr16 + srcPitch)) + 1;
 
 			/* fill the 3x3 grid */
-			memcpy(pixels, sptr2, 3*sizeof(uint16));
-			memcpy(pixels+3, sptr16 - 1, 3*sizeof(uint16));
-			memcpy(pixels+6, addr3 - 2, 3*sizeof(uint16));
+			memcpy(pixels, sptr2, 3 * sizeof(uint16));
+			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
+			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
 
 #if 0
 			/* skip interior unchanged 3x3 blocks */
 			if (*sptr16 == *old_sptr16 &&
 #if DEBUG_DRAW_REFRESH_BORDERS
-					x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+			        x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-					check_unchanged_pixels(old_sptr16, pixels, old_src_inc))
-			{
+			        check_unchanged_pixels(old_sptr16, pixels, old_src_inc)) {
 				draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
-						old_dst_inc);
+				                       old_dst_inc);
 
 #if DEBUG_REFRESH_RANDOM_XOR
 				*(dptr16 + 1) = 0;
@@ -3816,10 +3602,9 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			diffs = chooseGreyscale(pixels);
 
 			/* block of solid color */
-			if (!diffs)
-			{
+			if (!diffs) {
 				anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
-						0, NULL, NULL, 0);
+				                              0, NULL, NULL, 0);
 				continue;
 			}
 
@@ -3833,15 +3618,15 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,
-					sim, &angle);
+			                              sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
-					sim, angle);
+			                           sim, angle);
 			if (sub_type >= 0)
 				sub_type = fixKnights(sub_type, pixels, sim);
 
 			anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
-					sub_type, bplane, sim,
-					interpolate_2x);
+			                              sub_type, bplane, sim,
+			                              interpolate_2x);
 		}
 	}
 }
@@ -3850,7 +3635,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 /* Initialize various lookup tables */
 void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
-		int width, int height) {
+                            int width, int height) {
 	double r_float, g_float, b_float;
 	int r, g, b;
 	uint16 i;
@@ -3883,20 +3668,20 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
 				val[2] = 0.143 * r_float + 0.286 * g_float + 0.571 * b_float;
 
 				/* factor in a little intensity too, it helps */
-				val[0] = (intensity + 9*val[0]) / 10;
-				val[1] = (intensity + 9*val[1]) / 10;
-				val[2] = (intensity + 9*val[2]) / 10;
+				val[0] = (intensity + 9 * val[0]) / 10;
+				val[1] = (intensity + 9 * val[1]) / 10;
+				val[2] = (intensity + 9 * val[2]) / 10;
 
 				/* store the greyscale tables */
-				_greyscaleTable[0][i] = (int16) (val[0] * ((int16)1<<GREY_SHIFT) + 0.5);
-				_greyscaleTable[1][i] = (int16) (val[1] * ((int16)1<<GREY_SHIFT) + 0.5);
-				_greyscaleTable[2][i] = (int16) (val[2] * ((int16)1<<GREY_SHIFT) + 0.5);
+				_greyscaleTable[0][i] = (int16)(val[0] * ((int16)1 << GREY_SHIFT) + 0.5);
+				_greyscaleTable[1][i] = (int16)(val[1] * ((int16)1 << GREY_SHIFT) + 0.5);
+				_greyscaleTable[2][i] = (int16)(val[2] * ((int16)1 << GREY_SHIFT) + 0.5);
 
 				/* normalized RGB channel lookups */
 				rgb_ptr = _rgbTable[(r << 11) | (g << 5) | b];
-				rgb_ptr[0] = (int16) (r_float * ((int16)1<<RGB_SHIFT) + 0.5);
-				rgb_ptr[1] = (int16) (g_float * ((int16)1<<RGB_SHIFT) + 0.5);
-				rgb_ptr[2] = (int16) (b_float * ((int16)1<<RGB_SHIFT) + 0.5);
+				rgb_ptr[0] = (int16)(r_float * ((int16)1 << RGB_SHIFT) + 0.5);
+				rgb_ptr[1] = (int16)(g_float * ((int16)1 << RGB_SHIFT) + 0.5);
+				rgb_ptr[2] = (int16)(b_float * ((int16)1 << RGB_SHIFT) + 0.5);
 			}
 		}
 	}
@@ -3934,7 +3719,7 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
 	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
 #endif
 #endif
-#endif 
+#endif
 }
 
 
@@ -3951,8 +3736,7 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
  */
 #if 0
 void resize_old_arrays(const uint8 *src, uint8 *dst,
-		int w, int h, int scale)
-{
+                       int w, int h, int scale) {
 	int w2, h2;
 	int32 size, max_scaled_size;
 	const uint16 *sptr16 = (const uint16 *) src;
@@ -3965,30 +3749,27 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 	if (scale > max_scale) max_scale = scale;
 
 	/* Deal with overlays */
-	if (overlay_flag)
-	{
+	if (overlay_flag) {
 		skip_unchanged_pixels_flag = 1;
 
 		w2 = w + 2;
 		h2 = h + 2;
 		size = w2 * h2;
 
-		if (size > max_overlay_size)
-		{
+		if (size > max_overlay_size) {
 			max_overlay_size = size;
 			old_overlay = (uint16 *) realloc(old_overlay,
-					size * sizeof(uint16));
+			                                 size * sizeof(uint16));
 
 			skip_unchanged_pixels_flag = 0;
 		}
 
 		max_scaled_size = max_scale * max_scale * max_overlay_size;
 
-		if (max_scaled_size > max_dst_overlay_size)
-		{
+		if (max_scaled_size > max_dst_overlay_size) {
 			max_dst_overlay_size = max_scaled_size;
 			old_dst_overlay = (uint16 *) realloc(old_dst_overlay,
-					max_scaled_size * sizeof(uint16));
+			                                     max_scaled_size * sizeof(uint16));
 
 			skip_unchanged_pixels_flag = 0;
 		}
@@ -3998,10 +3779,9 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 		cur_dst_overlay_width = w * scale;
 		cur_dst_overlay_height = h * scale;
 		if (cur_overlay_width != old_overlay_width ||
-				cur_overlay_height != old_overlay_height ||
-				cur_dst_overlay_width != old_dst_overlay_width ||
-				cur_dst_overlay_height != old_dst_overlay_height)
-		{
+		        cur_overlay_height != old_overlay_height ||
+		        cur_dst_overlay_width != old_dst_overlay_width ||
+		        cur_dst_overlay_height != old_dst_overlay_height) {
 			skip_unchanged_pixels_flag = 0;
 		}
 		old_overlay_width = cur_overlay_width;
@@ -4009,14 +3789,11 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 		old_dst_overlay_width = cur_dst_overlay_width;
 		old_dst_overlay_height = cur_dst_overlay_height;
 
-		if (skip_unchanged_pixels_flag == 0)
-		{
+		if (skip_unchanged_pixels_flag == 0) {
 			memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
 			memset(old_dst_overlay, 0, max_dst_overlay_size * sizeof(uint16));
 		}
-	}
-	else
-	{
+	} else {
 		w2 = cur_screen_width + 2;
 		h2 = cur_screen_height + 2;
 		size = w2 * h2;
@@ -4024,8 +3801,7 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 
 	skip_unchanged_pixels_flag = 1;
 
-	if (overlay_flag == 0 && size > max_old_src_size)
-	{
+	if (overlay_flag == 0 && size > max_old_src_size) {
 		max_old_src_size = size;
 		old_src = (uint16 *) realloc(old_src, size * sizeof(uint16));
 
@@ -4034,19 +3810,17 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 
 	max_scaled_size = max_scale * max_scale * max_old_src_size;
 
-	if (overlay_flag == 0 && max_scaled_size > max_old_dst_size)
-	{
+	if (overlay_flag == 0 && max_scaled_size > max_old_dst_size) {
 		max_old_dst_size = max_scaled_size;
 		old_dst = (uint16 *) realloc(old_dst,
-				max_scaled_size * sizeof(uint16));
+		                             max_scaled_size * sizeof(uint16));
 
 		skip_unchanged_pixels_flag = 0;
 	}
 
 	/* screen dimensions have changed */
 	if (cur_screen_width != old_screen_width ||
-			cur_screen_height != old_screen_height)
-	{
+	        cur_screen_height != old_screen_height) {
 		skip_unchanged_pixels_flag = 0;
 	}
 	old_screen_width = cur_screen_width;
@@ -4055,16 +3829,14 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 	cur_dst_screen_width = cur_screen_width * scale;
 	cur_dst_screen_height = cur_screen_height * scale;
 	if (cur_dst_screen_width != old_dst_screen_width ||
-			cur_dst_screen_height != old_dst_screen_height)
-	{
+	        cur_dst_screen_height != old_dst_screen_height) {
 		skip_unchanged_pixels_flag = 0;
 	}
 	old_dst_screen_width = cur_dst_screen_width;
 	old_dst_screen_height = cur_dst_screen_height;
 
 	/* set all the buffers to 0, so that everything gets redrawn */
-	if (skip_unchanged_pixels_flag == 0)
-	{
+	if (skip_unchanged_pixels_flag == 0) {
 		memset(old_src, 0, max_old_src_size * sizeof(uint16));
 		memset(old_dst, 0, max_old_dst_size * sizeof(uint16));
 		memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
@@ -4075,8 +3847,7 @@ void resize_old_arrays(const uint8 *src, uint8 *dst,
 
 
 /* Fill old src array, which is used in checking for unchanged pixels */
-void fill_old_src(const uint8 *src, int srcPitch, int w, int h)
-{
+void fill_old_src(const uint8 *src, int srcPitch, int w, int h) {
 	int x, y;
 	int x2, y2;
 	const uint16 *sptr16 = (const uint16 *) src;
@@ -4088,19 +3859,16 @@ void fill_old_src(const uint8 *src, int srcPitch, int w, int h)
 	int32 src_fudge;
 
 	/* Deal with overlays */
-	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
-	{
+	if (sptr16 < src_addr_min || sptr16 > src_addr_max) {
 		w += 2;
 		h += 2;
 
 		optr16 = old_overlay;
-		sptr2 = (const uint16 *) (src - srcPitch) - 1;
+		sptr2 = (const uint16 *)(src - srcPitch) - 1;
 
 		x_fudge = 0;
 		src_fudge = srcPitch - w - w;
-	}
-	else
-	{
+	} else {
 		y = dist / srcPitch;
 		x = (dist - y * srcPitch) >> 1;
 
@@ -4111,13 +3879,12 @@ void fill_old_src(const uint8 *src, int srcPitch, int w, int h)
 		src_fudge = srcPitch - w - w;
 	}
 
-	for (y2 = 0; y2 < h; y2++)
-	{
+	for (y2 = 0; y2 < h; y2++) {
 		for (x2 = 0; x2 < w; x2++)
 			*optr16++ = *sptr2++;
 
 		optr16 += x_fudge;
-		sptr2 = (const uint16 *) ((const uint8 *) sptr2 + src_fudge);
+		sptr2 = (const uint16 *)((const uint8 *) sptr2 + src_fudge);
 	}
 }
 
@@ -4125,8 +3892,7 @@ void fill_old_src(const uint8 *src, int srcPitch, int w, int h)
 
 /* Fill old dst array, which is used in drawing unchanged pixels */
 void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
-		int w, int h, int scale)
-{
+                  int w, int h, int scale) {
 	int x, y;
 	int x2, y2;
 
@@ -4141,15 +3907,12 @@ void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
 	int32 dst_fudge;
 
 	/* Deal with overlays */
-	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
-	{
+	if (sptr16 < src_addr_min || sptr16 > src_addr_max) {
 		optr16 = old_dst_overlay;
 		dptr2 = (uint16 *) dst;
 		x_fudge = 0;
 		dst_fudge = dstPitch - w_new - w_new;
-	}
-	else
-	{
+	} else {
 		dist = src - (const uint8 *) src_addr_min;
 		y = dist / srcPitch;
 		x = (dist - y * srcPitch) >> 1;
@@ -4161,13 +3924,12 @@ void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
 		dst_fudge = dstPitch - w_new - w_new;
 	}
 
-	for (y2 = 0; y2 < h_new; y2++)
-	{
+	for (y2 = 0; y2 < h_new; y2++) {
 		for (x2 = 0; x2 < w_new; x2++)
 			*optr16++ = *dptr2++;
 
 		optr16 += x_fudge;
-		dptr2 = (uint16 *) ((uint8 *) dptr2 + dst_fudge);
+		dptr2 = (uint16 *)((uint8 *) dptr2 + dst_fudge);
 	}
 }
 #endif
@@ -4176,19 +3938,17 @@ void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
 /* 3x anti-aliased resize filter, nearest-neighbor anti-aliasing */
 #if 0
 void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
-		uint8 *dstPtr, uint32 dstPitch, int width, int height)
-{
+            uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	/* Initialize stuff */
-	if (!init_flag)
-	{
+	if (!init_flag) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 
 		/* set initial best guess on min/max screen addresses */
 		/* indent by 1 in case only the mouse is ever drawn */
 		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
-					srcPitch)) + (width - 1) - 1;
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
+		                                 srcPitch)) + (width - 1) - 1;
 
 		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
@@ -4196,23 +3956,21 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 
 	/* Uh oh, the screen size has changed */
 	if (cur_screen_width != g_system->getWidth() ||
-			cur_screen_height != g_system->getHeight())
-	{
+	        cur_screen_height != g_system->getHeight()) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
 	   src array for later bounds checking */
 	if (width == g_system->getWidth() &&
-			height == g_system->getHeight())
-	{
+	        height == g_system->getHeight()) {
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* resize and/or blank the old src and dst array */
@@ -4222,15 +3980,12 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 	 * We're probably drawing an overlay now.
 	 */
 	if ((const uint16 *) srcPtr < src_addr_min ||
-			(const uint16 *) srcPtr > src_addr_max)
-	{
+	        (const uint16 *) srcPtr > src_addr_max) {
 		antiAliasPass3x(srcPtr, dstPtr, width, height,
-				3*width, 3*height, srcPitch, dstPitch, 1);
-	}
-	else   /* Draw the regular screen, this isn't an overlay. */
-	{
+		                3 * width, 3 * height, srcPitch, dstPitch, 1);
+	} else { /* Draw the regular screen, this isn't an overlay. */
 		antiAliasPass3x(srcPtr, dstPtr, width, height,
-				3*width, 3*height, srcPitch, dstPitch, 0);
+		                3 * width, 3 * height, srcPitch, dstPitch, 0);
 	}
 
 	/* fill old src array */
@@ -4244,19 +3999,17 @@ void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
 /* 2x anti-aliased resize filter, nearest-neighbor anti-aliasing */
 #if 0
 void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
-		uint8 *dstPtr, uint32 dstPitch, int width, int height)
-{
+            uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	/* Initialize stuff */
-	if (!init_flag)
-	{
+	if (!init_flag) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 
 		/* set initial best guess on min/max screen addresses */
 		/* indent by 1 in case only the mouse is ever drawn */
 		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
-					srcPitch)) + (width - 1) - 1;
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
+		                                 srcPitch)) + (width - 1) - 1;
 
 		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
@@ -4264,23 +4017,21 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 
 	/* Uh oh, the screen size has changed */
 	if (cur_screen_width != g_system->getWidth() ||
-			cur_screen_height != g_system->getHeight())
-	{
+	        cur_screen_height != g_system->getHeight()) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
 	   src array for later bounds checking */
 	if (width == g_system->getWidth() &&
-			height == g_system->getHeight())
-	{
+	        height == g_system->getHeight()) {
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* resize and/or blank the old src and dst array */
@@ -4290,15 +4041,12 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
 	 * We're probably drawing an overlay now.
 	 */
 	if ((const uint16 *) srcPtr < src_addr_min ||
-			(const uint16 *) srcPtr > src_addr_max)
-	{
+	        (const uint16 *) srcPtr > src_addr_max) {
 		antiAliasPass2x(srcPtr, dstPtr, width, height,
-				2*width, 2*height, srcPitch, dstPitch, 1, 0);
-	}
-	else   /* Draw the regular screen, this isn't an overlay. */
-	{
+		                2 * width, 2 * height, srcPitch, dstPitch, 1, 0);
+	} else { /* Draw the regular screen, this isn't an overlay. */
 		antiAliasPass2x(srcPtr, dstPtr, width, height,
-				2*width, 2*height, srcPitch, dstPitch, 0, 0);
+		                2 * width, 2 * height, srcPitch, dstPitch, 0, 0);
 	}
 
 	/* fill old src array */
@@ -4318,19 +4066,17 @@ void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
  */
 #if 0
 void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
-		uint8 *dstPtr, uint32 dstPitch, int width, int height)
-{
+                   uint8 *dstPtr, uint32 dstPitch, int width, int height) {
 	/* Initialize stuff */
-	if (!init_flag)
-	{
+	if (!init_flag) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 
 		/* set initial best guess on min/max screen addresses */
 		/* indent by 1 in case only the mouse is ever drawn */
 		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) *
-					srcPitch)) + (width - 1) - 1;
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
+		                                 srcPitch)) + (width - 1) - 1;
 
 		initTables(srcPtr, srcPitch, width, height);
 		init_flag = 1;
@@ -4338,23 +4084,21 @@ void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 
 	/* Uh oh, the screen size has changed */
 	if (cur_screen_width != g_system->getWidth() ||
-			cur_screen_height != g_system->getHeight())
-	{
+	        cur_screen_height != g_system->getHeight()) {
 		cur_screen_width = g_system->getWidth();
 		cur_screen_height = g_system->getHeight();
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
 	   src array for later bounds checking */
 	if (width == g_system->getWidth() &&
-			height == g_system->getHeight())
-	{
+	        height == g_system->getHeight()) {
 		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *) (srcPtr + (height - 1) * srcPitch)) +
-			(width - 1);
+		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
+		               (width - 1);
 	}
 
 	/* resize and/or blank the old src and dst array */
@@ -4364,15 +4108,12 @@ void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
 	 * We're probably drawing an overlay now.
 	 */
 	if ((const uint16 *) srcPtr < src_addr_min ||
-			(const uint16 *) srcPtr > src_addr_max)
-	{
+	        (const uint16 *) srcPtr > src_addr_max) {
 		antiAliasPass2x(srcPtr, dstPtr, width, height,
-				2*width, 2*height, srcPitch, dstPitch, 1, 0);
-	}
-	else   /* Draw the regular screen, this isn't an overlay. */
-	{
+		                2 * width, 2 * height, srcPitch, dstPitch, 1, 0);
+	} else { /* Draw the regular screen, this isn't an overlay. */
 		antiAliasPass2x(srcPtr, dstPtr, width, height,
-				2*width, 2*height, srcPitch, dstPitch, 0, 1);
+		                2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
 	}
 
 	/* fill old src array */
@@ -4396,18 +4137,18 @@ void EdgePlugin::deinitialize() {
 }
 
 void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
-		uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
+                       uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2*width, 2*height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3*width, 3*height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
 		}
 	} else {
 		warning("FIXME: EdgePlugin 32bpp format");


Commit: a4741a7268441bbb5f595d6589b7afd2e78002e7
    https://github.com/scummvm/scummvm/commit/a4741a7268441bbb5f595d6589b7afd2e78002e7
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Template Edge plugin over pixel format

calcPixelDiffNosqrt is the only function with a valid implementation.
The rest of the templated functions just have skeleton templates (for
now).

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index a4f5cb74ad..2a78d57c9f 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -388,7 +388,27 @@ int16 *EdgePlugin::chooseGreyscale(uint16 *pixels) {
  * useful results.
  *
  */
-int32 EdgePlugin::calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2) {
+template<typename ColorMask, typename Pixel>
+int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
+	if (sizeof(Pixel) == 4) {
+		int r1, g1, b1, r2, g2, b2;
+
+		// Convert to 16 bit
+		r1 = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & pixel1)
+			>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
+		g1 = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & pixel1)
+			>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
+		b1 = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & pixel1);
+
+		r2 = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & pixel2)
+			>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
+		g2 = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & pixel2)
+			>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
+		b2 = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & pixel2);
+
+		pixel1 = r1 | g1 | b1;
+		pixel2 = r2 | g2 | b2;
+	}
 
 #if 1   /* distance between pixels, weighted by roughly luma proportions */
 	int32 sum = 0;
@@ -1804,7 +1824,7 @@ int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 #define greenMask   0x07E0
 
 /* Fill pixel grid without interpolation, using the detected edge */
-template<typename ColorMask>
+template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
         uint16 *pixels, int sub_type, int16 *bptr) {
 	uint16 *dptr2;
@@ -1831,9 +1851,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1865,9 +1885,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1892,9 +1912,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 16) {
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1905,9 +1925,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 17) {
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1933,9 +1953,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1967,9 +1987,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2001,9 +2021,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2035,9 +2055,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2062,9 +2082,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 18) {
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2075,9 +2095,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 19) {
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2103,9 +2123,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2137,9 +2157,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2171,9 +2191,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2193,9 +2213,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2221,9 +2241,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2243,9 +2263,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2271,9 +2291,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2293,9 +2313,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2321,9 +2341,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2343,9 +2363,9 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2405,7 +2425,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 
 /* Fill pixel grid with or without interpolation, using the detected edge */
-template<typename ColorMask>
+template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
                                     uint16 *pixels, int sub_type, int16 *bptr,
                                     int8 *sim,
@@ -2432,9 +2452,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2477,9 +2497,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2515,9 +2535,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 16) {
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2540,9 +2560,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 17) {
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2579,9 +2599,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2624,9 +2644,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2669,9 +2689,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2714,9 +2734,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2752,9 +2772,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 18) {
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2777,9 +2797,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 19) {
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2816,9 +2836,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2861,9 +2881,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2906,9 +2926,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2946,9 +2966,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2991,9 +3011,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3031,9 +3051,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3076,9 +3096,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3116,9 +3136,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3161,9 +3181,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3201,9 +3221,9 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3425,7 +3445,7 @@ void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
 
 
 /* Perform edge detection, draw the new 3x pixels */
-template<typename ColorMask>
+template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
                                  int srcPitch, int dstPitch,
@@ -3498,7 +3518,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 			/* block of solid color */
 			if (!diffs) {
-				anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
+				anti_alias_grid_clean_3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 				                                    0, NULL);
 				continue;
 			}
@@ -3519,7 +3539,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights(sub_type, pixels, sim);
 
-			anti_alias_grid_clean_3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
+			anti_alias_grid_clean_3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                                    sub_type, bplane);
 		}
 	}
@@ -3528,7 +3548,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 
 /* Perform edge detection, draw the new 2x pixels */
-template<typename ColorMask>
+template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
                                  int srcPitch, int dstPitch,
@@ -3603,7 +3623,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 			/* block of solid color */
 			if (!diffs) {
-				anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
+				anti_alias_grid_2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 				                              0, NULL, NULL, 0);
 				continue;
 			}
@@ -3624,7 +3644,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights(sub_type, pixels, sim);
 
-			anti_alias_grid_2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
+			anti_alias_grid_2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                              sub_type, bplane, sim,
 			                              interpolate_2x);
 		}
@@ -4141,14 +4161,14 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
 		}
 	} else {
 		warning("FIXME: EdgePlugin 32bpp format");
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 00965b6019..4151348811 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -40,7 +40,8 @@ public:
 	virtual const char *getPrettyName() const;
 private:
 	int16* chooseGreyscale(uint16 *pixels);
-	int32 calcPixelDiffNosqrt(uint16 pixel1, uint16 pixel2);
+	template<typename ColorMask, typename Pixel>
+	int32 calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2);
 	int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 		int8 *sim,
 		int32 *return_angle);
@@ -51,21 +52,21 @@ private:
 	void initTables(const uint8 *srcPtr, uint32 srcPitch,
 		int width, int height);
 
-	template<typename ColorMask>
+	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
 		int interpolate_2x);
-	template<typename ColorMask>
+	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr);
-	template<typename ColorMask>
+	template<typename ColorMask, typename Pixel>
 	void antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		int overlay_flag,
 		int interpolate_2x);
-	template<typename ColorMask>
+	template<typename ColorMask, typename Pixel>
 	void antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,


Commit: 18a099c7d3b10c6457369ad9e895a1c3a5de692a
    https://github.com/scummvm/scummvm/commit/18a099c7d3b10c6457369ad9e895a1c3a5de692a
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove unused variable in Edge Plugin

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 2a78d57c9f..c1743ad8a7 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3451,7 +3451,6 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int srcPitch, int dstPitch,
                                  int overlay_flag) {
 	int x, y;
-	int w2 = w + 2;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst + dstPitch + 2;
 	const uint16 *sptr16;
@@ -3555,7 +3554,6 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int overlay_flag,
                                  int interpolate_2x) {
 	int x, y;
-	int w2 = w + 2;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst;
 	const uint16 *sptr16;


Commit: 664ba9ae5d3b83ed08588d65fec9dd48f7fcfedf
    https://github.com/scummvm/scummvm/commit/664ba9ae5d3b83ed08588d65fec9dd48f7fcfedf
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Change variable name to remove shadowing

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index c1743ad8a7..8e02ad828f 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -516,7 +516,7 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	double best_val;
 	double a, b, c;
 	double ratio;
-	int32 scale;
+	int32 colorScale;
 
 	/* absolute value of differences */
 	for (i = 0; i < 8; i++)
@@ -532,9 +532,9 @@ int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
 	/* if (max_diff == 0) return '0'; */
 
 	/* normalize the differences */
-	scale = (1L << (GREY_SHIFT + GREY_SHIFT)) / max_diff;
+	colorScale = (1L << (GREY_SHIFT + GREY_SHIFT)) / max_diff;
 	for (i = 0; i < 8; i++)
-		diffs[i] = (diffs[i] * scale + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
+		diffs[i] = (diffs[i] * colorScale + ((int16)1 << (GREY_SHIFT - 1))) >> GREY_SHIFT;
 
 	/*
 	 * Some pixel patterns need to NOT be reversed, since the pixels of


Commit: 3574823828cc904892cc38ddb507e3a68eabe0b7
    https://github.com/scummvm/scummvm/commit/3574823828cc904892cc38ddb507e3a68eabe0b7
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove code from Edge plugin that will never be used.

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 8e02ad828f..6513debd8d 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3703,443 +3703,8 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
 			}
 		}
 	}
-
-#if 0
-#if INCREASE_WIN32_PRIORITY
-	/*
-	 * Greatly increase thread and process priority under Win32.
-	 *
-	 * -- WARNING -- WinXP has a rather poor priority system, it's basicly
-	 * "largely ignore me", "normal", or "interfere with some system processes".
-	 * At least Win9x/ME had "increased" and "decreased" modes, but now those API
-	 * functions aren't "supported" in WinNT/2K/XP, and we're stuck with the mess
-	 * we have now.  "Normal" is preempted by so much other unimportant stuff,
-	 * including the idle process, that ScummVM never gets above 75% CPU usage,
-	 * even when the filter is overworked and the frame rate is getting jerky.
-	 * Massively increasing BOTH the process and thread priorities (need to boost
-	 * the process priority to keep audio in sync with the increased thread
-	 * priority when the frame rate gets jerky) beats Windows into giving the
-	 * program the time it needs, however, doing things outside ScummVM may be
-	 * sluggish.  I recommend pausing ScummVM prior to shrinking from full-screen
-	 * to a window, or before switching from ScummVM to some other task.  Other
-	 * than the above mentioned sluggishness, I have not seen any other sort of
-	 * system instability while running with increased priorities.
-	 *
-	 */
-
-	/* Rather than check for all Win32 compiler flavors, just check for WIN32 and
-	 * PRIORITY defines.
-	 */
-#if defined WIN32 && defined(HIGH_PRIORITY_CLASS) && defined(THREAD_PRIORITY_TIME_CRITICAL)
-
-	/* Raise task and thread priority under Win32 */
-	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
-	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
-#endif
-#endif
-#endif
 }
 
-
-
-/*
- * Resize old src and dst arrays. Also check for problems that require full
- * redraws, rather than using the unchanged pixel buffering.  If the ScummVM
- * backend evolves to draw even more things outside the filter than it
- * currently does, some more checks may need to be added, or external flags
- * set in the backend to tell the filter to redraw everything without using
- * the unchanged pixel buffers.  The current checks seem to catch everything
- * for now....
- *
- */
-#if 0
-void resize_old_arrays(const uint8 *src, uint8 *dst,
-                       int w, int h, int scale) {
-	int w2, h2;
-	int32 size, max_scaled_size;
-	const uint16 *sptr16 = (const uint16 *) src;
-	int overlay_flag = 0;
-	int skip_unchanged_pixels_flag;
-
-	if (sptr16 < src_addr_min || sptr16 > src_addr_max)
-		overlay_flag = 1;
-
-	if (scale > max_scale) max_scale = scale;
-
-	/* Deal with overlays */
-	if (overlay_flag) {
-		skip_unchanged_pixels_flag = 1;
-
-		w2 = w + 2;
-		h2 = h + 2;
-		size = w2 * h2;
-
-		if (size > max_overlay_size) {
-			max_overlay_size = size;
-			old_overlay = (uint16 *) realloc(old_overlay,
-			                                 size * sizeof(uint16));
-
-			skip_unchanged_pixels_flag = 0;
-		}
-
-		max_scaled_size = max_scale * max_scale * max_overlay_size;
-
-		if (max_scaled_size > max_dst_overlay_size) {
-			max_dst_overlay_size = max_scaled_size;
-			old_dst_overlay = (uint16 *) realloc(old_dst_overlay,
-			                                     max_scaled_size * sizeof(uint16));
-
-			skip_unchanged_pixels_flag = 0;
-		}
-
-		cur_overlay_width = w;
-		cur_overlay_height = h;
-		cur_dst_overlay_width = w * scale;
-		cur_dst_overlay_height = h * scale;
-		if (cur_overlay_width != old_overlay_width ||
-		        cur_overlay_height != old_overlay_height ||
-		        cur_dst_overlay_width != old_dst_overlay_width ||
-		        cur_dst_overlay_height != old_dst_overlay_height) {
-			skip_unchanged_pixels_flag = 0;
-		}
-		old_overlay_width = cur_overlay_width;
-		old_overlay_height = cur_overlay_height;
-		old_dst_overlay_width = cur_dst_overlay_width;
-		old_dst_overlay_height = cur_dst_overlay_height;
-
-		if (skip_unchanged_pixels_flag == 0) {
-			memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
-			memset(old_dst_overlay, 0, max_dst_overlay_size * sizeof(uint16));
-		}
-	} else {
-		w2 = cur_screen_width + 2;
-		h2 = cur_screen_height + 2;
-		size = w2 * h2;
-	}
-
-	skip_unchanged_pixels_flag = 1;
-
-	if (overlay_flag == 0 && size > max_old_src_size) {
-		max_old_src_size = size;
-		old_src = (uint16 *) realloc(old_src, size * sizeof(uint16));
-
-		skip_unchanged_pixels_flag = 0;
-	}
-
-	max_scaled_size = max_scale * max_scale * max_old_src_size;
-
-	if (overlay_flag == 0 && max_scaled_size > max_old_dst_size) {
-		max_old_dst_size = max_scaled_size;
-		old_dst = (uint16 *) realloc(old_dst,
-		                             max_scaled_size * sizeof(uint16));
-
-		skip_unchanged_pixels_flag = 0;
-	}
-
-	/* screen dimensions have changed */
-	if (cur_screen_width != old_screen_width ||
-	        cur_screen_height != old_screen_height) {
-		skip_unchanged_pixels_flag = 0;
-	}
-	old_screen_width = cur_screen_width;
-	old_screen_height = cur_screen_height;
-
-	cur_dst_screen_width = cur_screen_width * scale;
-	cur_dst_screen_height = cur_screen_height * scale;
-	if (cur_dst_screen_width != old_dst_screen_width ||
-	        cur_dst_screen_height != old_dst_screen_height) {
-		skip_unchanged_pixels_flag = 0;
-	}
-	old_dst_screen_width = cur_dst_screen_width;
-	old_dst_screen_height = cur_dst_screen_height;
-
-	/* set all the buffers to 0, so that everything gets redrawn */
-	if (skip_unchanged_pixels_flag == 0) {
-		memset(old_src, 0, max_old_src_size * sizeof(uint16));
-		memset(old_dst, 0, max_old_dst_size * sizeof(uint16));
-		memset(old_overlay, 0, max_overlay_size * sizeof(uint16));
-		memset(old_dst_overlay, 0, max_dst_overlay_size * sizeof(uint16));
-	}
-}
-
-
-
-/* Fill old src array, which is used in checking for unchanged pixels */
-void fill_old_src(const uint8 *src, int srcPitch, int w, int h) {
-	int x, y;
-	int x2, y2;
-	const uint16 *sptr16 = (const uint16 *) src;
-	const uint16 *sptr2;
-	uint16 *optr16;
-	int32 screen_width2 = cur_screen_width + 2;
-	int32 dist = src - (const uint8 *) src_addr_min;
-	int32 x_fudge;
-	int32 src_fudge;
-
-	/* Deal with overlays */
-	if (sptr16 < src_addr_min || sptr16 > src_addr_max) {
-		w += 2;
-		h += 2;
-
-		optr16 = old_overlay;
-		sptr2 = (const uint16 *)(src - srcPitch) - 1;
-
-		x_fudge = 0;
-		src_fudge = srcPitch - w - w;
-	} else {
-		y = dist / srcPitch;
-		x = (dist - y * srcPitch) >> 1;
-
-		optr16 = old_src + (y + 1) * screen_width2 + x + 1;
-		sptr2 = (const uint16 *) src;
-
-		x_fudge = screen_width2 - w;
-		src_fudge = srcPitch - w - w;
-	}
-
-	for (y2 = 0; y2 < h; y2++) {
-		for (x2 = 0; x2 < w; x2++)
-			*optr16++ = *sptr2++;
-
-		optr16 += x_fudge;
-		sptr2 = (const uint16 *)((const uint8 *) sptr2 + src_fudge);
-	}
-}
-
-
-
-/* Fill old dst array, which is used in drawing unchanged pixels */
-void fill_old_dst(const uint8 *src, uint8 *dst, int srcPitch, int dstPitch,
-                  int w, int h, int scale) {
-	int x, y;
-	int x2, y2;
-
-	const uint16 *sptr16 = (const uint16 *) src;
-	uint16 *dptr2;
-	uint16 *optr16;
-	int32 screen_width_scaled = cur_screen_width * scale;
-	int32 dist;
-	int w_new = w * scale;
-	int h_new = h * scale;
-	int32 x_fudge;
-	int32 dst_fudge;
-
-	/* Deal with overlays */
-	if (sptr16 < src_addr_min || sptr16 > src_addr_max) {
-		optr16 = old_dst_overlay;
-		dptr2 = (uint16 *) dst;
-		x_fudge = 0;
-		dst_fudge = dstPitch - w_new - w_new;
-	} else {
-		dist = src - (const uint8 *) src_addr_min;
-		y = dist / srcPitch;
-		x = (dist - y * srcPitch) >> 1;
-
-		optr16 = old_dst + scale * (y * screen_width_scaled + x);
-		dptr2 = (uint16 *) dst;
-
-		x_fudge = screen_width_scaled - w_new;
-		dst_fudge = dstPitch - w_new - w_new;
-	}
-
-	for (y2 = 0; y2 < h_new; y2++) {
-		for (x2 = 0; x2 < w_new; x2++)
-			*optr16++ = *dptr2++;
-
-		optr16 += x_fudge;
-		dptr2 = (uint16 *)((uint8 *) dptr2 + dst_fudge);
-	}
-}
-#endif
-
-
-/* 3x anti-aliased resize filter, nearest-neighbor anti-aliasing */
-#if 0
-void Edge3x(const uint8 *srcPtr, uint32 srcPitch,
-            uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	/* Initialize stuff */
-	if (!init_flag) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-
-		/* set initial best guess on min/max screen addresses */
-		/* indent by 1 in case only the mouse is ever drawn */
-		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
-		                                 srcPitch)) + (width - 1) - 1;
-
-		initTables(srcPtr, srcPitch, width, height);
-		init_flag = 1;
-	}
-
-	/* Uh oh, the screen size has changed */
-	if (cur_screen_width != g_system->getWidth() ||
-	        cur_screen_height != g_system->getHeight()) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
-	   src array for later bounds checking */
-	if (width == g_system->getWidth() &&
-	        height == g_system->getHeight()) {
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* resize and/or blank the old src and dst array */
-	resize_old_arrays(srcPtr, dstPtr, width, height, 3);
-
-	/* Hmm, the src address isn't within the proper bounds.
-	 * We're probably drawing an overlay now.
-	 */
-	if ((const uint16 *) srcPtr < src_addr_min ||
-	        (const uint16 *) srcPtr > src_addr_max) {
-		antiAliasPass3x(srcPtr, dstPtr, width, height,
-		                3 * width, 3 * height, srcPitch, dstPitch, 1);
-	} else { /* Draw the regular screen, this isn't an overlay. */
-		antiAliasPass3x(srcPtr, dstPtr, width, height,
-		                3 * width, 3 * height, srcPitch, dstPitch, 0);
-	}
-
-	/* fill old src array */
-	fill_old_src(srcPtr, srcPitch, width, height);
-	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 3);
-}
-#endif
-
-
-
-/* 2x anti-aliased resize filter, nearest-neighbor anti-aliasing */
-#if 0
-void Edge2x(const uint8 *srcPtr, uint32 srcPitch,
-            uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	/* Initialize stuff */
-	if (!init_flag) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-
-		/* set initial best guess on min/max screen addresses */
-		/* indent by 1 in case only the mouse is ever drawn */
-		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
-		                                 srcPitch)) + (width - 1) - 1;
-
-		initTables(srcPtr, srcPitch, width, height);
-		init_flag = 1;
-	}
-
-	/* Uh oh, the screen size has changed */
-	if (cur_screen_width != g_system->getWidth() ||
-	        cur_screen_height != g_system->getHeight()) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
-	   src array for later bounds checking */
-	if (width == g_system->getWidth() &&
-	        height == g_system->getHeight()) {
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* resize and/or blank the old src and dst array */
-	resize_old_arrays(srcPtr, dstPtr, width, height, 2);
-
-	/* Hmm, the src address isn't within the proper bounds.
-	 * We're probably drawing an overlay now.
-	 */
-	if ((const uint16 *) srcPtr < src_addr_min ||
-	        (const uint16 *) srcPtr > src_addr_max) {
-		antiAliasPass2x(srcPtr, dstPtr, width, height,
-		                2 * width, 2 * height, srcPitch, dstPitch, 1, 0);
-	} else { /* Draw the regular screen, this isn't an overlay. */
-		antiAliasPass2x(srcPtr, dstPtr, width, height,
-		                2 * width, 2 * height, srcPitch, dstPitch, 0, 0);
-	}
-
-	/* fill old src array */
-	fill_old_src(srcPtr, srcPitch, width, height);
-	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
-}
-#endif
-
-
-
-/*
- * 2x anti-aliased resize filter, interpolation based anti-aliasing.
- * This is useful prior to upsizing with the 1.5x filter for the menu
- * overlay.  Nearest-neighbor looks bad with 1.5x resize.  Interpolated gives
- * results that look good, like a somewhat blurry Edge3x.
- *
- */
-#if 0
-void Edge2x_Interp(const uint8 *srcPtr, uint32 srcPitch,
-                   uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	/* Initialize stuff */
-	if (!init_flag) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-
-		/* set initial best guess on min/max screen addresses */
-		/* indent by 1 in case only the mouse is ever drawn */
-		src_addr_min = (const uint16 *) srcPtr + 1;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) *
-		                                 srcPitch)) + (width - 1) - 1;
-
-		initTables(srcPtr, srcPitch, width, height);
-		init_flag = 1;
-	}
-
-	/* Uh oh, the screen size has changed */
-	if (cur_screen_width != g_system->getWidth() ||
-	        cur_screen_height != g_system->getHeight()) {
-		cur_screen_width = g_system->getWidth();
-		cur_screen_height = g_system->getHeight();
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* Ah ha, we're doing the whole screen, so we can save the bounds of the
-	   src array for later bounds checking */
-	if (width == g_system->getWidth() &&
-	        height == g_system->getHeight()) {
-		src_addr_min = (const uint16 *) srcPtr;
-		src_addr_max = ((const uint16 *)(srcPtr + (height - 1) * srcPitch)) +
-		               (width - 1);
-	}
-
-	/* resize and/or blank the old src and dst array */
-	resize_old_arrays(srcPtr, dstPtr, width, height, 2);
-
-	/* Hmm, the src address isn't within the proper bounds.
-	 * We're probably drawing an overlay now.
-	 */
-	if ((const uint16 *) srcPtr < src_addr_min ||
-	        (const uint16 *) srcPtr > src_addr_max) {
-		antiAliasPass2x(srcPtr, dstPtr, width, height,
-		                2 * width, 2 * height, srcPitch, dstPitch, 1, 0);
-	} else { /* Draw the regular screen, this isn't an overlay. */
-		antiAliasPass2x(srcPtr, dstPtr, width, height,
-		                2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
-	}
-
-	/* fill old src array */
-	fill_old_src(srcPtr, srcPitch, width, height);
-	fill_old_dst(srcPtr, dstPtr, srcPitch, dstPitch, width, height, 2);
-}
-#endif
-
 EdgePlugin::EdgePlugin() {
 	_factor = 2;
 	_factors.push_back(2);


Commit: 917ef83043c817166ef1bb1a84f3ee05770ed206
    https://github.com/scummvm/scummvm/commit/917ef83043c817166ef1bb1a84f3ee05770ed206
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add more templating to Edge plugin

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 6513debd8d..54c835add3 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3374,73 +3374,76 @@ void handle_transparent_overlay(uint16 transp, uint16 *pixels,
 
 
 /* Check for changed pixel grid, return 1 if unchanged. */
-int check_unchanged_pixels(uint16 *old_src_ptr, uint16 *pixels, int w) {
-	uint16 *dptr16;
+template<typename Pixel>
+int check_unchanged_pixels(Pixel *old_src_ptr, Pixel *pixels, int w) {
+	Pixel *dptr;
 
-	dptr16 = old_src_ptr - w - 1;
-	if (*dptr16++ != pixels[0]) return 0;
-	if (*dptr16++ != pixels[1]) return 0;
-	if (*dptr16 != pixels[2]) return 0;
+	dptr = old_src_ptr - w - 1;
+	if (*dptr++ != pixels[0]) return 0;
+	if (*dptr++ != pixels[1]) return 0;
+	if (*dptr != pixels[2]) return 0;
 
-	dptr16 += w - 2;
-	if (*dptr16 != pixels[3]) return 0;
-	dptr16 += 2;
-	if (*dptr16 != pixels[5]) return 0;
+	dptr += w - 2;
+	if (*dptr != pixels[3]) return 0;
+	dptr += 2;
+	if (*dptr != pixels[5]) return 0;
 
-	dptr16 += w - 2;
-	if (*dptr16++ != pixels[6]) return 0;
-	if (*dptr16++ != pixels[7]) return 0;
-	if (*dptr16 != pixels[8]) return 0;
+	dptr += w - 2;
+	if (*dptr++ != pixels[6]) return 0;
+	if (*dptr++ != pixels[7]) return 0;
+	if (*dptr != pixels[8]) return 0;
 
 	return 1;
 }
 
 
 /* Draw unchanged pixel grid, 3x */
-/* old_dptr16 starts in top left of grid, dptr16 in center */
-void draw_unchanged_grid_3x(uint16 *dptr16, int dstPitch,
-                            uint16 *old_dptr16, int old_dst_inc) {
-	uint16 *sptr;
-	uint16 *dptr;
-	uint8 *dptr8 = (uint8 *) dptr16;
-
-	sptr = old_dptr16;
-	dptr = (uint16 *)(dptr8 - dstPitch) - 1;
-	*dptr++ = *sptr++;
-	*dptr++ = *sptr++;
-	*dptr = *sptr;
-
-	sptr = old_dptr16 + old_dst_inc;
-	dptr = dptr16 - 1;
-	*dptr++ = *sptr++;
-	*dptr++ = *sptr++;
-	*dptr = *sptr;
-
-	sptr = old_dptr16 + old_dst_inc + old_dst_inc;
-	dptr = (uint16 *)(dptr8 + dstPitch) - 1;
-	*dptr++ = *sptr++;
-	*dptr++ = *sptr++;
-	*dptr = *sptr;
+/* old_dptr starts in top left of grid, dptr in center */
+template<typename Pixel>
+void draw_unchanged_grid_3x(Pixel *dptr, int dstPitch,
+                            Pixel *old_dptr, int old_dst_inc) {
+	Pixel *sp;
+	Pixel *dp;
+	uint8 *dptr8 = (uint8 *) dptr;
+
+	sp = old_dptr;
+	dp = (Pixel *)(dptr8 - dstPitch) - 1;
+	*dp++ = *sp++;
+	*dp++ = *sp++;
+	*dp = *sp;
+
+	sp = old_dptr + old_dst_inc;
+	dp = dptr - 1;
+	*dp++ = *sp++;
+	*dp++ = *sp++;
+	*dp = *sp;
+
+	sp = old_dptr + old_dst_inc + old_dst_inc;
+	dp = (Pixel *)(dptr8 + dstPitch) - 1;
+	*dp++ = *sp++;
+	*dp++ = *sp++;
+	*dp = *sp;
 }
 
 
 
 /* Draw unchanged pixel grid, 2x */
-void draw_unchanged_grid_2x(uint16 *dptr16, int dstPitch,
-                            uint16 *old_dptr16, int old_dst_inc) {
-	uint16 *sptr;
-	uint16 *dptr;
-	uint8 *dptr8 = (uint8 *) dptr16;
-
-	sptr = old_dptr16;
-	dptr = dptr16;
-	*dptr++ = *sptr++;
-	*dptr = *sptr;
-
-	sptr = old_dptr16 + old_dst_inc;
-	dptr = (uint16 *)(dptr8 + dstPitch);
-	*dptr++ = *sptr++;
-	*dptr = *sptr;
+template<typename Pixel>
+void draw_unchanged_grid_2x(Pixel *dptr, int dstPitch,
+                            Pixel *old_dptr, int old_dst_inc) {
+	Pixel *sp;
+	Pixel *dp;
+	uint8 *dptr8 = (uint8 *) dptr;
+
+	sp = old_dptr;
+	dp = dptr;
+	*dp++ = *sp++;
+	*dp = *sp;
+
+	sp = old_dptr + old_dst_inc;
+	dp = (Pixel *)(dptr8 + dstPitch);
+	*dp++ = *sp++;
+	*dp = *sp;
 }
 
 


Commit: c9b63ba7e34fa0d88452ee408993e9a1d0a31330
    https://github.com/scummvm/scummvm/commit/c9b63ba7e34fa0d88452ee408993e9a1d0a31330
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove more unused code from the Edge plugin.

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 54c835add3..ace67a4ea5 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -119,7 +119,6 @@
 #define INCREASE_WIN32_PRIORITY         0   /* 1 for slow CPUs */
 #define PARANOID_KNIGHTS            1   /* avoid artifacts */
 #define PARANOID_ARROWS             1   /* avoid artifacts */
-#define HANDLE_TRANSPARENT_OVERLAYS     1   /* as it says */
 
 #define SIN45 0.7071067811865       /* sin of 45 degrees */
 #define GREY_SHIFT 12           /* bit shift for greyscale precision */
@@ -3287,92 +3286,6 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 }
 
 
-
-#if HANDLE_TRANSPARENT_OVERLAYS
-/* Deal with transparent pixels */
-void handle_transparent_overlay(uint16 transp, uint16 *pixels,
-                                int16 *bplane, int16 *diffs) {
-	int16 tmp_grey;
-	int16 max_diff;
-	int16 min_grey = ((int16)1 << RGB_SHIFT);
-	int16 max_grey = 0;
-	int i;
-
-	/* find min and max grey values in window */
-	for (i = 0; i < 9; i++) {
-		if (bplane[i] < min_grey) min_grey = bplane[i];
-		if (bplane[i] > max_grey) max_grey = bplane[i];
-	}
-
-	/* treat transparent pixels as the average of min_grey and max_grey */
-	/* set diff from center pixel to maximum difference within the window */
-	tmp_grey = (min_grey + max_grey + 1) >> 1;
-	max_diff = max_grey - min_grey;
-
-	if (pixels[4] == transp) {  /* center pixel is transparent */
-		/* set all transparent pixels to middle grey */
-		if (pixels[0] == transp) bplane[0] = tmp_grey;
-		if (pixels[1] == transp) bplane[1] = tmp_grey;
-		if (pixels[2] == transp) bplane[2] = tmp_grey;
-		if (pixels[3] == transp) bplane[3] = tmp_grey;
-		if (pixels[4] == transp) bplane[4] = tmp_grey;
-		if (pixels[5] == transp) bplane[5] = tmp_grey;
-		if (pixels[6] == transp) bplane[6] = tmp_grey;
-		if (pixels[7] == transp) bplane[7] = tmp_grey;
-		if (pixels[8] == transp) bplane[8] = tmp_grey;
-
-		/* set all diffs to non-transparent pixels to max diff */
-		if (pixels[0] != transp) diffs[0] = max_diff;
-		if (pixels[1] != transp) diffs[1] = max_diff;
-		if (pixels[2] != transp) diffs[2] = max_diff;
-		if (pixels[3] != transp) diffs[3] = max_diff;
-		if (pixels[5] != transp) diffs[4] = max_diff;
-		if (pixels[6] != transp) diffs[5] = max_diff;
-		if (pixels[7] != transp) diffs[6] = max_diff;
-		if (pixels[8] != transp) diffs[7] = max_diff;
-	} else {        /* center pixel is non-transparent */
-		/* choose transparent grey value to give largest contrast */
-		tmp_grey = (bplane[4] >= tmp_grey) ? min_grey : max_grey;
-
-		/* set new transparent pixel values and diffs */
-		if (pixels[0] == transp) {
-			bplane[0] = tmp_grey;
-			diffs[0] = max_diff;
-		}
-		if (pixels[1] == transp) {
-			bplane[1] = tmp_grey;
-			diffs[1] = max_diff;
-		}
-		if (pixels[2] == transp) {
-			bplane[2] = tmp_grey;
-			diffs[2] = max_diff;
-		}
-		if (pixels[3] == transp) {
-			bplane[3] = tmp_grey;
-			diffs[3] = max_diff;
-		}
-		if (pixels[5] == transp) {
-			bplane[5] = tmp_grey;
-			diffs[4] = max_diff;
-		}
-		if (pixels[6] == transp) {
-			bplane[6] = tmp_grey;
-			diffs[5] = max_diff;
-		}
-		if (pixels[7] == transp) {
-			bplane[7] = tmp_grey;
-			diffs[6] = max_diff;
-		}
-		if (pixels[8] == transp) {
-			bplane[8] = tmp_grey;
-			diffs[7] = max_diff;
-		}
-	}
-}
-#endif
-
-
-
 /* Check for changed pixel grid, return 1 if unchanged. */
 template<typename Pixel>
 int check_unchanged_pixels(Pixel *old_src_ptr, Pixel *pixels, int w) {
@@ -3465,23 +3378,6 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch3 = dstPitch * 3;
 
-#if 0
-#if HANDLE_TRANSPARENT_OVERLAYS
-	uint16 transp = 0;  /* transparent color */
-
-	/* assume bitmap is padded by a transparent border, take src-1 pixel */
-	if (overlay_flag) transp = *((const uint16 *) src - 1);
-#endif
-
-	/* The dirty rects optimizer in the SDL backend is helpful for frames
-	 * with few changes, but _REALLY_ bogs things down when the whole screen
-	 * changes.  Overall, the dirty rects optimizer isn't worth it, since
-	 * the Edge2x/3x unchanged pixel detection is far faster than full blown
-	 * dirty rects optimization.
-	 */
-	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
-#endif
-
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3) {
 		for (x = 0,
 		        sptr16 = (const uint16 *) sptr8,
@@ -3525,13 +3421,6 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 				continue;
 			}
 
-#if 0
-#if HANDLE_TRANSPARENT_OVERLAYS
-			if (overlay_flag)
-				handle_transparent_overlay(transp, pixels, bplane, diffs);
-#endif
-#endif
-
 			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,
@@ -3568,24 +3457,6 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch2 = dstPitch << 1;
 
-#if 0
-#if HANDLE_TRANSPARENT_OVERLAYS
-	uint16 transp = 0;  /* transparent color */
-
-	/* assume bitmap is padded by a transparent border, take src-1 pixel */
-	if (overlay_flag) transp = *((const uint16 *) src - 1);
-#endif
-
-
-	/* The dirty rects optimizer in the SDL backend is helpful for frames
-	 * with few changes, but _REALLY_ bogs things down when the whole screen
-	 * changes.  Overall, the dirty rects optimizer isn't worth it, since
-	 * the Edge2x/3x unchanged pixel detection is far faster than full blown
-	 * dirty rects optimization.
-	 */
-	g_system->setFeatureState(g_system->kFeatureAutoComputeDirtyRects, 0);
-#endif
-
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2) {
 		for (x = 0,
 		        sptr16 = (const uint16 *) sptr8,
@@ -3629,13 +3500,6 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 				continue;
 			}
 
-#if 0
-#if HANDLE_TRANSPARENT_OVERLAYS
-			if (overlay_flag)
-				handle_transparent_overlay(transp, pixels, bplane, diffs);
-#endif
-#endif
-
 			bplane = _bptr;
 
 			edge_type = findPrincipleAxis(pixels, diffs, bplane,


Commit: c84db891bf10d58a853a7ee49b2035fba541cb6c
    https://github.com/scummvm/scummvm/commit/c84db891bf10d58a853a7ee49b2035fba541cb6c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove unused define in edge.cpp.

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index ace67a4ea5..5bb0c088aa 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -116,7 +116,6 @@
  */
 #define DEBUG_DRAW_REFRESH_BORDERS      0   /* more redraw debug */
 
-#define INCREASE_WIN32_PRIORITY         0   /* 1 for slow CPUs */
 #define PARANOID_KNIGHTS            1   /* avoid artifacts */
 #define PARANOID_ARROWS             1   /* avoid artifacts */
 


Commit: 4552ec6138b56c99054c9370f48dae4e64040f24
    https://github.com/scummvm/scummvm/commit/4552ec6138b56c99054c9370f48dae4e64040f24
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add function to convert pixels to 16bpp format

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 5bb0c088aa..f223c9dd2b 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -281,6 +281,28 @@ double fast_atan(double x0) {
 }
 
 
+/**
+ * Convert a pixel to a 16bpp format. If called on
+ * a 16bpp pixel, nothing happens and the call should
+ * be removed at compile time by an optimizing compiler.
+ * This allows 16 and 32 bit functions to easily share
+ * the same code.
+ */
+template<typename ColorMask, typename Pixel>
+uint16 convertTo16Bit(Pixel p) {
+	if (sizeof(Pixel) == 2)
+		return p;
+
+	uint16 r, g, b;
+	r = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & p)
+		>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
+	g = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & p)
+		>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
+	b = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & p);
+
+	return r | g | b;
+}
+
 
 /*
  * Choose greyscale bitplane to use, return diff array.  Exit early and
@@ -388,25 +410,8 @@ int16 *EdgePlugin::chooseGreyscale(uint16 *pixels) {
  */
 template<typename ColorMask, typename Pixel>
 int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
-	if (sizeof(Pixel) == 4) {
-		int r1, g1, b1, r2, g2, b2;
-
-		// Convert to 16 bit
-		r1 = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & pixel1)
-			>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
-		g1 = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & pixel1)
-			>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
-		b1 = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & pixel1);
-
-		r2 = (ColorMask::kRedMask & (ColorMask::kRedMask << (8 - Graphics::ColorMasks<565>::kRedBits)) & pixel2)
-			>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
-		g2 = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & pixel2)
-			>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
-		b2 = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & pixel2);
-
-		pixel1 = r1 | g1 | b1;
-		pixel2 = r2 | g2 | b2;
-	}
+	pixel1 = convertTo16Bit<ColorMask, Pixel>(pixel1);
+	pixel2 = convertTo16Bit<ColorMask, Pixel>(pixel2);
 
 #if 1   /* distance between pixels, weighted by roughly luma proportions */
 	int32 sum = 0;


Commit: 0dcd6bae37e40cf5cea1dee2201c18f53f08b511
    https://github.com/scummvm/scummvm/commit/0dcd6bae37e40cf5cea1dee2201c18f53f08b511
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: 32bpp-ize chooseGreyscale for Edge plugin

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index f223c9dd2b..9d7092aae6 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -329,14 +329,15 @@ uint16 convertTo16Bit(Pixel p) {
  * bitplanes.  The increase in image quality is well worth the speed hit.
  *
  */
-int16 *EdgePlugin::chooseGreyscale(uint16 *pixels) {
+template<typename ColorMask, typename Pixel>
+int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
 	int i, j;
 	int32 scores[3];
 
 	for (i = 0; i < 3; i++) {
 		int16 *diff_ptr;
 		int16 *bptr;
-		uint16 *pptr;
+		Pixel *pptr;
 		int16 *grey_ptr;
 		int16 center;
 		int32 sum_diffs;
@@ -349,10 +350,10 @@ int16 *EdgePlugin::chooseGreyscale(uint16 *pixels) {
 		bptr = _bplanes[i];
 		pptr = pixels;
 		for (j = 9; j; --j)
-			*bptr++ = grey_ptr[*pptr++];
+			*bptr++ = grey_ptr[convertTo16Bit<ColorMask, Pixel>(*pptr++)];
 		bptr = _bplanes[i];
 
-		center = grey_ptr[pixels[4]];
+		center = grey_ptr[convertTo16Bit<ColorMask, Pixel>(pixels[4])];
 		diff_ptr = _greyscaleDiffs[i];
 
 		/* calculate the delta from center pixel */
@@ -3416,7 +3417,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			}
 
 #endif
-			diffs = chooseGreyscale(pixels);
+			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
 
 			/* block of solid color */
 			if (!diffs) {
@@ -3495,7 +3496,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			}
 #endif
 
-			diffs = chooseGreyscale(pixels);
+			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
 
 			/* block of solid color */
 			if (!diffs) {
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 4151348811..c85e778774 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -39,7 +39,8 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
-	int16* chooseGreyscale(uint16 *pixels);
+	template<typename ColorMask, typename Pixel>
+	int16* chooseGreyscale(Pixel *pixels);
 	template<typename ColorMask, typename Pixel>
 	int32 calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2);
 	int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,


Commit: a6e6f7676d2b2a6d81d63d99e53f30b5ee566b17
    https://github.com/scummvm/scummvm/commit/a6e6f7676d2b2a6d81d63d99e53f30b5ee566b17
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove unused parameter to findPricipleAxis in Edge plugin

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 9d7092aae6..ce4c441567 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -498,7 +498,7 @@ int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
  * since everything I have tried has lead to slight mis-detection errors.
  *
  */
-int EdgePlugin::findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
+int EdgePlugin::findPrincipleAxis(int16 *diffs, int16 *bplane,
                                   int8 *sim,
                                   int32 *return_angle) {
 	struct xy_point {
@@ -3428,7 +3428,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 			bplane = _bptr;
 
-			edge_type = findPrincipleAxis(pixels, diffs, bplane,
+			edge_type = findPrincipleAxis(diffs, bplane,
 			                              sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
 			                           sim, angle);
@@ -3507,7 +3507,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 			bplane = _bptr;
 
-			edge_type = findPrincipleAxis(pixels, diffs, bplane,
+			edge_type = findPrincipleAxis(diffs, bplane,
 			                              sim, &angle);
 			sub_type = refineDirection(edge_type, pixels, bplane,
 			                           sim, angle);
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index c85e778774..3cf176443f 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -43,7 +43,7 @@ private:
 	int16* chooseGreyscale(Pixel *pixels);
 	template<typename ColorMask, typename Pixel>
 	int32 calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2);
-	int findPrincipleAxis(uint16 *pixels, int16 *diffs, int16 *bplane,
+	int findPrincipleAxis(int16 *diffs, int16 *bplane,
 		int8 *sim,
 		int32 *return_angle);
 	int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag);


Commit: f40485ee3781dd4cf558cb2215e486b608b67e36
    https://github.com/scummvm/scummvm/commit/f40485ee3781dd4cf558cb2215e486b608b67e36
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Move and format comments from edge.cpp to edge.h

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index ce4c441567..57007b8443 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -304,31 +304,6 @@ uint16 convertTo16Bit(Pixel p) {
 }
 
 
-/*
- * Choose greyscale bitplane to use, return diff array.  Exit early and
- * return NULL for a block of solid color (all diffs zero).
- *
- * No matter how you do it, mapping 3 bitplanes into a single greyscale
- * bitplane will always result in colors which are very different mapping to
- * the same greyscale value.  Inevitably, these pixels will appear next to
- * each other at some point in some image, and edge detection on a single
- * bitplane will behave quite strangely due to them having the same or nearly
- * the same greyscale values.  Calculating distances between pixels using all
- * three RGB bitplanes is *way* too time consuming, so single bitplane
- * edge detection is used for speed's sake.  In order to try to avoid the
- * color mapping problems of using a single bitplane, 3 different greyscale
- * mappings are tested for each 3x3 grid, and the one with the most "signal"
- * (sum of squares difference from center pixel) is chosen.  This usually
- * results in useable contrast within the 3x3 grid.
- *
- * This results in a whopping 25% increase in overall runtime of the filter
- * over simply using luma or some other single greyscale bitplane, but it
- * does greatly reduce the amount of errors due to greyscale mapping
- * problems.  I think this is the best compromise between accuracy and
- * speed, and is still a lot faster than edge detecting over all three RGB
- * bitplanes.  The increase in image quality is well worth the speed hit.
- *
- */
 template<typename ColorMask, typename Pixel>
 int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
 	int i, j;
@@ -401,14 +376,6 @@ int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
 }
 
 
-
-/*
- * Calculate the distance between pixels in RGB space.  Greyscale isn't
- * accurate enough for choosing nearest-neighbors :(  Luma-like weighting
- * of the individual bitplane distances prior to squaring gives the most
- * useful results.
- *
- */
 template<typename ColorMask, typename Pixel>
 int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
 	pixel1 = convertTo16Bit<ColorMask, Pixel>(pixel1);
@@ -483,21 +450,6 @@ int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
 }
 
 
-
-/*
- * Create vectors of all delta grey values from center pixel, with magnitudes
- * ranging from [1.0, 0.0] (zero difference, maximum difference).  Find
- * the two principle axes of the grid by calculating the eigenvalues and
- * eigenvectors of the inertia tensor.  Use the eigenvectors to calculate the
- * edge direction.  In other words, find the angle of the line that optimally
- * passes through the 3x3 pattern of pixels.
- *
- * Return horizontal (-), vertical (|), diagonal (/,\), multi (*), or none '0'
- *
- * Don't replace any of the double math with integer-based approximations,
- * since everything I have tried has lead to slight mis-detection errors.
- *
- */
 int EdgePlugin::findPrincipleAxis(int16 *diffs, int16 *bplane,
                                   int8 *sim,
                                   int32 *return_angle) {
@@ -722,8 +674,6 @@ int EdgePlugin::findPrincipleAxis(int16 *diffs, int16 *bplane,
 }
 
 
-
-/* Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad). */
 int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag) {
 	uint16 center = pixels[4];
 
@@ -830,15 +780,6 @@ int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_fl
 }
 
 
-
-/*
- * Take original direction, refine it by testing different pixel difference
- * patterns based on the initial gross edge direction.
- *
- * The angle value is not currently used, but may be useful for future
- * refinement algorithms.
- *
- */
 int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
                                 int8 *sim, double angle) {
 	int32 sums_dir[9] = { 0 };
@@ -1679,8 +1620,6 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 }
 
 
-
-/* "Chess Knight" patterns can be mis-detected, fix easy cases. */
 int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 	uint16 center = pixels[4];
 	int dir = sub_type;
@@ -1827,7 +1766,6 @@ int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 #define redblueMask 0xF81F
 #define greenMask   0x07E0
 
-/* Fill pixel grid without interpolation, using the detected edge */
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
         uint16 *pixels, int sub_type, int16 *bptr) {
@@ -2427,8 +2365,6 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 }
 
 
-
-/* Fill pixel grid with or without interpolation, using the detected edge */
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
                                     uint16 *pixels, int sub_type, int16 *bptr,
@@ -3365,7 +3301,6 @@ void draw_unchanged_grid_2x(Pixel *dptr, int dstPitch,
 }
 
 
-/* Perform edge detection, draw the new 3x pixels */
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
@@ -3442,8 +3377,6 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 }
 
 
-
-/* Perform edge detection, draw the new 2x pixels */
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
@@ -3522,8 +3455,6 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 }
 
 
-
-/* Initialize various lookup tables */
 void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
                             int width, int height) {
 	double r_float, g_float, b_float;
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 3cf176443f..c052a41346 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -26,6 +26,7 @@
 
 class EdgePlugin : public ScalerPluginObject {
 public:
+
 	EdgePlugin();
 	virtual void initialize(Graphics::PixelFormat format);
 	virtual void deinitialize();
@@ -38,35 +39,117 @@ public:
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
+
 private:
+
+	/**
+	 * Choose greyscale bitplane to use, return diff array.  Exit early and
+	 * return NULL for a block of solid color (all diffs zero).
+	 *
+	 * No matter how you do it, mapping 3 bitplanes into a single greyscale
+	 * bitplane will always result in colors which are very different mapping to
+	 * the same greyscale value.  Inevitably, these pixels will appear next to
+	 * each other at some point in some image, and edge detection on a single
+	 * bitplane will behave quite strangely due to them having the same or nearly
+	 * the same greyscale values.  Calculating distances between pixels using all
+	 * three RGB bitplanes is *way* too time consuming, so single bitplane
+	 * edge detection is used for speed's sake.  In order to try to avoid the
+	 * color mapping problems of using a single bitplane, 3 different greyscale
+	 * mappings are tested for each 3x3 grid, and the one with the most "signal"
+	 * (sum of squares difference from center pixel) is chosen.  This usually
+	 * results in useable contrast within the 3x3 grid.
+	 *
+	 * This results in a whopping 25% increase in overall runtime of the filter
+	 * over simply using luma or some other single greyscale bitplane, but it
+	 * does greatly reduce the amount of errors due to greyscale mapping
+	 * problems.  I think this is the best compromise between accuracy and
+	 * speed, and is still a lot faster than edge detecting over all three RGB
+	 * bitplanes.  The increase in image quality is well worth the speed hit.
+	 */
 	template<typename ColorMask, typename Pixel>
 	int16* chooseGreyscale(Pixel *pixels);
+
+	/**
+	 * Calculate the distance between pixels in RGB space.  Greyscale isn't
+	 * accurate enough for choosing nearest-neighbors :(  Luma-like weighting
+	 * of the individual bitplane distances prior to squaring gives the most
+	 * useful results.
+	 */
 	template<typename ColorMask, typename Pixel>
 	int32 calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2);
+
+	/**
+	 * Create vectors of all delta grey values from center pixel, with magnitudes
+	 * ranging from [1.0, 0.0] (zero difference, maximum difference).  Find
+	 * the two principle axes of the grid by calculating the eigenvalues and
+	 * eigenvectors of the inertia tensor.  Use the eigenvectors to calculate the
+	 * edge direction.  In other words, find the angle of the line that optimally
+	 * passes through the 3x3 pattern of pixels.
+	 *
+	 * Return horizontal (-), vertical (|), diagonal (/,\), multi (*), or none '0'
+	 *
+	 * Don't replace any of the double math with integer-based approximations,
+	 * since everything I have tried has lead to slight mis-detection errors.
+	 */
 	int findPrincipleAxis(int16 *diffs, int16 *bplane,
 		int8 *sim,
 		int32 *return_angle);
+
+	/**
+	 * Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad).
+	 */
 	int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag);
+
+	/**
+	 * Take original direction, refine it by testing different pixel difference
+	 * patterns based on the initial gross edge direction.
+	 *
+	 * The angle value is not currently used, but may be useful for future
+	 * refinement algorithms.
+	 */
 	int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		int8 *sim, double angle);
+
+	/**
+	 * "Chess Knight" patterns can be mis-detected, fix easy cases.
+	 */
 	int fixKnights(int sub_type, uint16 *pixels, int8 *sim);
+
+	/**
+	 * Initialize various lookup tables
+	 */
 	void initTables(const uint8 *srcPtr, uint32 srcPitch,
 		int width, int height);
 
+	/**
+	 * Fill pixel grid with or without interpolation, using the detected edge
+	 */
 	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
 		int interpolate_2x);
+
+	/**
+	 * Fill pixel grid without interpolation, using the detected edge
+	 */
 	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		uint16 *pixels, int sub_type, int16 *bptr);
+
+	/**
+	 * Perform edge detection, draw the new 2x pixels
+	 */
 	template<typename ColorMask, typename Pixel>
 	void antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		int overlay_flag,
 		int interpolate_2x);
+
+	/**
+	 * Perform edge detection, draw the new 3x pixels
+	 */
 	template<typename ColorMask, typename Pixel>
 	void antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,


Commit: ffb52288a9a18c950bba2f6c074f7c6f848f5a09
    https://github.com/scummvm/scummvm/commit/ffb52288a9a18c950bba2f6c074f7c6f848f5a09
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add useOldSrc and oldSrcScale to scaler plugin API

This allows plugins and backends to optionally pass the previous frame's
source image so that the scaler can only update the pixels that need to
be updated.

Changed paths:
    graphics/scalerplugin.h


diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 252c77883c..9c103c1644 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -89,6 +89,29 @@ public:
 	 */
 	virtual const char *getPrettyName() const = 0;
 
+	/**
+	 * Computationally intense scalers can benefit from comparing new and old
+	 * source images and updating only the pixels necessary. If the function
+	 * returns true, this scaler prefers this method and the backend can
+	 * optionally call it.
+	 *
+	 * @see oldSourceScale
+	 */
+	virtual bool useOldSrc() const { return false; }
+
+	/**
+	 * Secondary scaling method for computationally intense scalers.
+	 *
+	 * @see useOldSource
+	 */
+	virtual void oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
+	                         uint8 *dstPtr, uint32 dstPitch,
+	                         const uint8 *oldSrcPtr, uint32 oldSrcPitch,
+	                         int width, int height, int x, int y) {
+		// Should not be called unless overriden
+		assert(0);
+	}
+
 protected:
 	uint _factor;
 	Common::Array<uint> _factors;


Commit: cfbc3e91cfea60b198238a8e6001f2800b489443
    https://github.com/scummvm/scummvm/commit/cfbc3e91cfea60b198238a8e6001f2800b489443
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Use oldSrcScale for scaler plugins that support it

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 5f083020da..24f3900122 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -120,9 +120,10 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #if defined(WIN32) && !SDL_VERSION_ATLEAST(2, 0, 0)
 	_originalBitsPerPixel(0),
 #endif
-	_screen(0), _tmpscreen(0),
+	_screen(0), _tmpscreen(0), _oldscreen(0),
 	_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
+	_useOldSrc(false),
 	_overlayscreen(0), _tmpscreen2(0),
 	_screenChangeCount(0),
 	_mouseData(nullptr), _mouseSurface(nullptr),
@@ -679,6 +680,7 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		convertSDLPixelFormat(_hwscreen->format, &format);
 		(*_scalerPlugin)->initialize(format);
 		_extraPixels = (*_scalerPlugin)->extraPixels();
+		_useOldSrc = (*_scalerPlugin)->useOldSrc();
 	}
 
 	(*_scalerPlugin)->setFactor(_videoMode.scaleFactor);
@@ -961,6 +963,19 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 	if (_tmpscreen == NULL)
 		error("allocating _tmpscreen failed");
 
+	// Create surface containing previous frame's data to pass to scaler
+	if (_useOldSrc) {
+		_oldscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
+							_videoMode.screenHeight + _maxExtraPixels * 2,
+							16,
+							_hwscreen->format->Rmask,
+							_hwscreen->format->Gmask,
+							_hwscreen->format->Bmask,
+							_hwscreen->format->Amask);
+		if (_oldscreen == NULL)
+			error("allocating _oldscreen failed");
+	}
+
 	_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth, _videoMode.overlayHeight,
 						16,
 						_hwScreen->format->Rmask,
@@ -1007,6 +1022,11 @@ void SurfaceSdlGraphicsManager::unloadGFXMode() {
 		_tmpscreen = NULL;
 	}
 
+	if (_oldscreen != NULL) {
+		SDL_FreeSurface(_oldscreen);
+		_oldscreen = NULL;
+	}
+
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1058,6 +1078,10 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 		SDL_FreeSurface(_tmpscreen);
 		_tmpscreen = NULL;
 	}
+	if (_oldscreen) {
+		SDL_FreeSurface(_oldscreen);
+        _oldscreen = NULL;
+	}
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1181,7 +1205,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 #endif
 
 	// Force a full redraw if requested
-	if (_forceRedraw) {
+	if (_forceRedraw || (_useOldSrc && !_overlayVisible)) {
 		_numDirtyRects = 1;
 		_dirtyRectList[0].x = 0;
 		_dirtyRectList[0].y = 0;
@@ -1247,8 +1271,14 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					// Revert state in case the normal plugin is used elsewhere
 					(*_normalPlugin)->setFactor(tmpFactor);
 				} else {
-					(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-						(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+					if (_useOldSrc)
+						(*_scalerPlugin)->oldSrcScale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+							(byte *)_oldscreen->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * _oldscreen->pitch, _oldscreen->pitch,
+							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch,
+							r->w, dst_h, r->x, r->y);
+					else
+						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
 			}
 
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 69f5cb4a31..2aa6cbdabb 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -216,10 +216,13 @@ protected:
 
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen;
+	/** Previous frame's screen (for scalers) */
+	SDL_Surface *_oldscreen;
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen2;
 
 	SDL_Surface *_overlayscreen;
+	bool _useOldSrc;
 	Graphics::PixelFormat _overlayFormat;
 
 	enum {


Commit: e2484a0fbadc79b64fdd7237f00ae029da2bad45
    https://github.com/scummvm/scummvm/commit/e2484a0fbadc79b64fdd7237f00ae029da2bad45
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Swap and lock the old screen

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 24f3900122..313bb5f3b2 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1231,6 +1231,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 		SDL_LockSurface(srcSurf);
 		SDL_LockSurface(_hwScreen);
+		if (_useOldSrc && !_overlayVisible) {
+			SDL_LockSurface(_oldscreen);
+		}
 
 		srcPitch = srcSurf->pitch;
 		dstPitch = _hwScreen->pitch;
@@ -1295,6 +1298,16 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		SDL_UnlockSurface(srcSurf);
 		SDL_UnlockSurface(_hwScreen);
 
+		if (_useOldSrc && !_overlayVisible) {
+			SDL_UnlockSurface(_oldscreen);
+
+			// Swap old and new screen
+			SDL_Surface *tmp;
+			tmp = _oldscreen;
+			_oldscreen = _tmpscreen;
+			_tmpscreen = tmp;
+		}
+
 		// Readjust the dirty rect list in case we are doing a full update.
 		// This is necessary if shaking is active.
 		if (_forceRedraw) {


Commit: 3b408c2d8709406476da5bf6fac31cdca68b7099
    https://github.com/scummvm/scummvm/commit/3b408c2d8709406476da5bf6fac31cdca68b7099
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Use an extra buffer for oldSrcScale so it is not confused by AR

If the two source images match, the scaler assumes there is not update
and does not draw to the surface. However, AR correction depends on a
redraw, so the scaler always writes to _destbuffer which is then copied
to _hwscreen to be AR corrected.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 313bb5f3b2..f1d6647ff3 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -120,7 +120,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #if defined(WIN32) && !SDL_VERSION_ATLEAST(2, 0, 0)
 	_originalBitsPerPixel(0),
 #endif
-	_screen(0), _tmpscreen(0), _oldscreen(0),
+	_screen(0), _tmpscreen(0), _oldscreen(0), _destbuffer(0),
 	_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_useOldSrc(false),
@@ -681,6 +681,30 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		(*_scalerPlugin)->initialize(format);
 		_extraPixels = (*_scalerPlugin)->extraPixels();
 		_useOldSrc = (*_scalerPlugin)->useOldSrc();
+		if (_useOldSrc) {
+			if (!_oldscreen) {
+				_oldscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
+									_videoMode.screenHeight + _maxExtraPixels * 2,
+									16,
+									_hwscreen->format->Rmask,
+									_hwscreen->format->Gmask,
+									_hwscreen->format->Bmask,
+									_hwscreen->format->Amask);
+				if (_oldscreen == NULL)
+					error("allocating _oldscreen failed");
+			}
+			if (!_destbuffer) {
+				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
+									_videoMode.screenHeight * _videoMode.scaleFactor,
+									16,
+									_hwscreen->format->Rmask,
+									_hwscreen->format->Gmask,
+									_hwscreen->format->Bmask,
+									_hwscreen->format->Amask);
+				if (_destbuffer == NULL)
+					error("allocating _destbuffer failed");
+			}
+		}
 	}
 
 	(*_scalerPlugin)->setFactor(_videoMode.scaleFactor);
@@ -963,8 +987,8 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 	if (_tmpscreen == NULL)
 		error("allocating _tmpscreen failed");
 
-	// Create surface containing previous frame's data to pass to scaler
 	if (_useOldSrc) {
+		// Create surface containing previous frame's data to pass to scaler
 		_oldscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
 							_videoMode.screenHeight + _maxExtraPixels * 2,
 							16,
@@ -974,6 +998,17 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 							_hwscreen->format->Amask);
 		if (_oldscreen == NULL)
 			error("allocating _oldscreen failed");
+
+		// Create surface containing the raw output from the scaler
+		_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
+							_videoMode.screenHeight * _videoMode.scaleFactor,
+							16,
+							_hwscreen->format->Rmask,
+							_hwscreen->format->Gmask,
+							_hwscreen->format->Bmask,
+							_hwscreen->format->Amask);
+		if (_destbuffer == NULL)
+			error("allocating _destbuffer failed");
 	}
 
 	_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth, _videoMode.overlayHeight,
@@ -1022,11 +1057,16 @@ void SurfaceSdlGraphicsManager::unloadGFXMode() {
 		_tmpscreen = NULL;
 	}
 
-	if (_oldscreen != NULL) {
+	if (_oldscreen) {
 		SDL_FreeSurface(_oldscreen);
 		_oldscreen = NULL;
 	}
 
+	if (_destbuffer) {
+		SDL_FreeSurface(_destbuffer);
+		_destbuffer = NULL;
+	}
+
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1082,6 +1122,10 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 		SDL_FreeSurface(_oldscreen);
         _oldscreen = NULL;
 	}
+	if (_destbuffer) {
+		SDL_FreeSurface(_destbuffer);
+        _destbuffer = NULL;
+	}
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1204,7 +1248,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 	updateOSD();
 #endif
 
-	// Force a full redraw if requested
+	// Force a full redraw if requested.
+	// If _useOldSrc, the scaler will do its own partial updates.
 	if (_forceRedraw || (_useOldSrc && !_overlayVisible)) {
 		_numDirtyRects = 1;
 		_dirtyRectList[0].x = 0;
@@ -1233,6 +1278,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		SDL_LockSurface(_hwScreen);
 		if (_useOldSrc && !_overlayVisible) {
 			SDL_LockSurface(_oldscreen);
+			SDL_LockSurface(_destbuffer);
 		}
 
 		srcPitch = srcSurf->pitch;
@@ -1274,12 +1320,13 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					// Revert state in case the normal plugin is used elsewhere
 					(*_normalPlugin)->setFactor(tmpFactor);
 				} else {
-					if (_useOldSrc)
+					if (_useOldSrc) {
+						// scale into _destbuffer instead of _hwscreen to avoid AR problems
 						(*_scalerPlugin)->oldSrcScale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch,
 							(byte *)_oldscreen->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * _oldscreen->pitch, _oldscreen->pitch,
-							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch,
 							r->w, dst_h, r->x, r->y);
-					else
+					} else
 						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
@@ -1291,8 +1338,25 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 			r->h = dst_h * scale1;
 
 #ifdef USE_SCALERS
-			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible)
-				r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
+			if (_useOldSrc) {
+				// Copy _destbuffer back into _hwscreen to be AR corrected
+				int y = orig_dst_y * scale1;
+				int h = r->h;
+				int w = r->w;
+				byte *dest = (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch;
+				byte *src = (byte *)_destbuffer->pixels + rx1 * 2 + y * _destbuffer->pitch;
+				while (h--) {
+					memcpy(dest, src, w*2);
+					dest += dstPitch;
+					src += _destbuffer->pitch;
+				}
+			}
+			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
+				if (_useOldSrc)
+					r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+				else
+					r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+			}
 #endif
 		}
 		SDL_UnlockSurface(srcSurf);
@@ -1300,6 +1364,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 		if (_useOldSrc && !_overlayVisible) {
 			SDL_UnlockSurface(_oldscreen);
+			SDL_UnlockSurface(_destbuffer);
 
 			// Swap old and new screen
 			SDL_Surface *tmp;
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 2aa6cbdabb..d81f64fa06 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -218,6 +218,8 @@ protected:
 	SDL_Surface *_tmpscreen;
 	/** Previous frame's screen (for scalers) */
 	SDL_Surface *_oldscreen;
+	/** Previous frame's raw scaled screen (for scalers) */
+	SDL_Surface *_destbuffer;
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen2;
 


Commit: 1e724e664081d6febff3b15f239b5c353432b7c5
    https://github.com/scummvm/scummvm/commit/1e724e664081d6febff3b15f239b5c353432b7c5
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Use new oldSrcScale API for EdgePlugin

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 57007b8443..a1eb4a2c87 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3229,8 +3229,8 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 
 /* Check for changed pixel grid, return 1 if unchanged. */
 template<typename Pixel>
-int check_unchanged_pixels(Pixel *old_src_ptr, Pixel *pixels, int w) {
-	Pixel *dptr;
+int check_unchanged_pixels(const Pixel *old_src_ptr, const Pixel *pixels, int w) {
+	const Pixel *dptr;
 
 	dptr = old_src_ptr - w - 1;
 	if (*dptr++ != pixels[0]) return 0;
@@ -3305,11 +3305,13 @@ template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
                                  int srcPitch, int dstPitch,
-                                 int overlay_flag) {
+                                 bool haveOldSrc,
+								 const uint8* oldSrc, int oldPitch) {
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst + dstPitch + 2;
 	const uint16 *sptr16;
+	const uint16 *oldSptr;
 	uint16 *dptr16;
 	int16 *bplane;
 	int8 sim[8];
@@ -3318,11 +3320,12 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch3 = dstPitch * 3;
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3) {
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3, oldSrc += oldPitch) {
 		for (x = 0,
 		        sptr16 = (const uint16 *) sptr8,
+		        oldSptr = (const uint16 *) oldSrc,
 		        dptr16 = (uint16 *) dptr8;
-		        x < w; x++, sptr16++, dptr16 += 3) {
+		        x < w; x++, sptr16++, dptr16 += 3, oldSptr++) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
@@ -3335,23 +3338,23 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
 			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
 
-#if 0
-			/* skip interior unchanged 3x3 blocks */
-			if (*sptr16 == *old_sptr16 &&
+			if (haveOldSrc) {
+				/* skip interior unchanged 3x3 blocks */
+				if (*sptr16 == *oldSptr &&
 #if DEBUG_DRAW_REFRESH_BORDERS
-			        x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-			        check_unchanged_pixels(old_sptr16, pixels, old_src_inc)) {
-				draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
-				                       old_dst_inc);
+						check_unchanged_pixels(oldSptr, pixels, oldPitch / sizeof(Pixel))) {
+					//draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
+					//					   old_dst_inc);
 
 #if DEBUG_REFRESH_RANDOM_XOR
-				*(dptr16 + 1) = 0;
+					*(dptr16 + 1) = 0;
 #endif
-				continue;
+					continue;
+				}
 			}
 
-#endif
 			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
 
 			/* block of solid color */
@@ -3381,12 +3384,14 @@ template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
                                  int srcPitch, int dstPitch,
-                                 int overlay_flag,
-                                 int interpolate_2x) {
+                                 int interpolate_2x,
+                                 bool haveOldSrc,
+								 const uint8 *oldSrc, int oldSrcPitch) {
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst;
 	const uint16 *sptr16;
+	const uint16 *oldSptr;
 	uint16 *dptr16;
 	int16 *bplane;
 	int8 sim[8];
@@ -3395,11 +3400,12 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int16 *diffs;
 	int dstPitch2 = dstPitch << 1;
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2) {
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2, oldSrc += oldSrcPitch) {
 		for (x = 0,
 		        sptr16 = (const uint16 *) sptr8,
-		        dptr16 = (uint16 *) dptr8;
-		        x < w; x++, sptr16++, dptr16 += 2) {
+		        dptr16 = (uint16 *) dptr8,
+				oldSptr = (const uint16 *) oldSrc;
+		        x < w; x++, sptr16++, dptr16 += 2, oldSptr++) {
 			const uint16 *sptr2, *addr3;
 			uint16 pixels[9];
 			char edge_type;
@@ -3412,22 +3418,22 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
 			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
 
-#if 0
-			/* skip interior unchanged 3x3 blocks */
-			if (*sptr16 == *old_sptr16 &&
+			if (haveOldSrc) {
+				/* skip interior unchanged 3x3 blocks */
+				if (*sptr16 == *oldSptr &&
 #if DEBUG_DRAW_REFRESH_BORDERS
-			        x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
+						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-			        check_unchanged_pixels(old_sptr16, pixels, old_src_inc)) {
-				draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
-				                       old_dst_inc);
+						check_unchanged_pixels<Pixel>(oldSptr, pixels, oldSrcPitch / sizeof(Pixel))) {
+					//draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
+					//					   old_dst_inc);
 
 #if DEBUG_REFRESH_RANDOM_XOR
-				*(dptr16 + 1) = 0;
+					*(dptr16 + 1) = 0;
 #endif
-				continue;
+					continue;
+				}
 			}
-#endif
 
 			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
 
@@ -3527,14 +3533,33 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, false, NULL, 0);
+			else
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, false, NULL, 0);
+		} else {
+			if (_format.gLoss == 2)
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, false, NULL, 0);
+			else
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, false, NULL, 0);
+		}
+	} else {
+		warning("FIXME: EdgePlugin 32bpp format");
+	}
+}
+
+void EdgePlugin::oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
+                       uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height, int x, int y) {
+	if (_format.bytesPerPixel == 2) {
+		if (_factor == 2) {
+			if (_format.gLoss == 2)
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 0, 1);
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, 0);
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
 		}
 	} else {
 		warning("FIXME: EdgePlugin 32bpp format");
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index c052a41346..4e13840637 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -32,10 +32,15 @@ public:
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+	virtual void oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
+						   uint8 *dstPtr, uint32 dstPitch,
+						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
+						   int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
+	virtual bool useOldSrc() const { return true; }
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
@@ -144,8 +149,9 @@ private:
 	void antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
-		int overlay_flag,
-		int interpolate_2x);
+		int interpolate_2x,
+		bool haveOldSrc,
+		const uint8 *oldSrc, int oldSrcPitch);
 
 	/**
 	 * Perform edge detection, draw the new 3x pixels
@@ -154,7 +160,8 @@ private:
 	void antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
-		int overlay_flag);
+		bool haveOldSrc,
+		const uint8* oldSrc, int oldPitch);
 
 	int16 _rgbTable[65536][3];       ///< table lookup for RGB
 	int16 _greyscaleTable[3][65536]; ///< greyscale tables


Commit: 6cfba9d56771e99059f51731451a1e9331302025
    https://github.com/scummvm/scummvm/commit/6cfba9d56771e99059f51731451a1e9331302025
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Only update if a change was made when using oldSrcScale

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f1d6647ff3..f8cc7278e7 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1250,7 +1250,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 	// Force a full redraw if requested.
 	// If _useOldSrc, the scaler will do its own partial updates.
-	if (_forceRedraw || (_useOldSrc && !_overlayVisible)) {
+	if (_forceRedraw || (_useOldSrc && !_overlayVisible && _numDirtyRects > 0)) {
 		_numDirtyRects = 1;
 		_dirtyRectList[0].x = 0;
 		_dirtyRectList[0].y = 0;


Commit: 8baa92a19083c9b1b03d572cffab65d25be3bc72
    https://github.com/scummvm/scummvm/commit/8baa92a19083c9b1b03d572cffab65d25be3bc72
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix overlay while using oldSrcScale

The _destbuffer was overwriting _hwscreen even when the overlay was
visible.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f8cc7278e7..37e1fabdc7 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1338,7 +1338,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 			r->h = dst_h * scale1;
 
 #ifdef USE_SCALERS
-			if (_useOldSrc) {
+			if (_useOldSrc && !_overlayVisible) {
 				// Copy _destbuffer back into _hwscreen to be AR corrected
 				int y = orig_dst_y * scale1;
 				int h = r->h;


Commit: 86c23073516c67a5f4c3064f109d84986c8d4b7e
    https://github.com/scummvm/scummvm/commit/86c23073516c67a5f4c3064f109d84986c8d4b7e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add some 32bpp abilty to Edge scalers

It is still broken, but it seems to be close.

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index a1eb4a2c87..74435be428 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -674,8 +674,9 @@ int EdgePlugin::findPrincipleAxis(int16 *diffs, int16 *bplane,
 }
 
 
-int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag) {
-	uint16 center = pixels[4];
+template<typename Pixel>
+int EdgePlugin::checkArrows(int best_dir, Pixel *pixels, int8 *sim, int half_flag) {
+	Pixel center = pixels[4];
 
 	if (center == pixels[0] && center == pixels[2] &&
 	        center == pixels[6] && center == pixels[8]) {
@@ -780,7 +781,8 @@ int EdgePlugin::checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_fl
 }
 
 
-int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
+template<typename Pixel>
+int EdgePlugin::refineDirection(char edge_type, Pixel *pixels, int16 *bptr,
                                 int8 *sim, double angle) {
 	int32 sums_dir[9] = { 0 };
 	int32 sum;
@@ -916,7 +918,7 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		if (n > 1) return 6;    /* | */
 
 		if (best_dir >= 5)
-			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
+			ok_arrow_flag = checkArrows<Pixel>(best_dir, pixels, sim, 1);
 
 		switch (best_dir) {
 		case 1:
@@ -1070,7 +1072,7 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		if (n > 1) return 0;    /* - */
 
 		if (best_dir >= 5)
-			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 1);
+			ok_arrow_flag = checkArrows<Pixel>(best_dir, pixels, sim, 1);
 
 		switch (best_dir) {
 		case 1:
@@ -1315,7 +1317,7 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		}
 
 		if (best_dir >= 5)
-			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
+			ok_arrow_flag = checkArrows<Pixel>(best_dir, pixels, sim, 0);
 
 		switch (best_dir) {
 		case 1:
@@ -1566,7 +1568,7 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 		}
 
 		if (best_dir >= 5)
-			ok_arrow_flag = checkArrows(best_dir, pixels, sim, 0);
+			ok_arrow_flag = checkArrows<Pixel>(best_dir, pixels, sim, 0);
 
 		switch (best_dir) {
 		case 1:
@@ -1620,8 +1622,9 @@ int EdgePlugin::refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
 }
 
 
-int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
-	uint16 center = pixels[4];
+template<typename Pixel>
+int EdgePlugin::fixKnights(int sub_type, Pixel *pixels, int8 *sim) {
+	Pixel center = pixels[4];
 	int dir = sub_type;
 	int n = 0;
 	int flags[12] = {0};
@@ -1768,13 +1771,13 @@ int EdgePlugin::fixKnights(int sub_type, uint16 *pixels, int8 *sim) {
 
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
-        uint16 *pixels, int sub_type, int16 *bptr) {
-	uint16 *dptr2;
+        Pixel *pixels, int sub_type, int16 *bptr) {
+	Pixel *dptr2;
 	int16 tmp_grey;
-	uint16 center = pixels[4];
+	Pixel center = pixels[4];
 	int32 diff1, diff2, diff3;
-	uint16 tmp[9];
-	uint16 *ptmp;
+	Pixel tmp[9];
+	Pixel *ptmp;
 	int i;
 
 	switch (sub_type) {
@@ -1783,7 +1786,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -1803,7 +1806,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[6] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2)
@@ -1817,7 +1820,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -1837,7 +1840,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2)
@@ -1885,7 +1888,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -1905,7 +1908,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2)
@@ -1919,7 +1922,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -1939,7 +1942,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[6] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[6]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2)
@@ -1953,7 +1956,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -1973,7 +1976,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2)
@@ -1987,7 +1990,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2007,7 +2010,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[8] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2)
@@ -2055,7 +2058,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2075,7 +2078,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[8] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[8]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2)
@@ -2089,7 +2092,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2109,7 +2112,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2)
@@ -2123,7 +2126,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2145,7 +2148,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2173,7 +2176,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2195,7 +2198,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2223,7 +2226,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2245,7 +2248,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2273,7 +2276,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[6]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2295,7 +2298,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[8]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2323,19 +2326,19 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 	case 0:     /* - */
 	case 6:     /* | */
 	default:
-		dptr2 = ((uint16 *)(dptr - dstPitch)) - 1;
+		dptr2 = ((Pixel *)(dptr - dstPitch)) - 1;
 		*dptr2++ = center;
 		*dptr2++ = center;
 		*dptr2 = center;
-		dptr2 = ((uint16 *) dptr) - 1;
+		dptr2 = ((Pixel *) dptr) - 1;
 		*dptr2++ = center;
 		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-		*dptr2 = center ^ (uint16)(dxorshift_128() * (1L << 16));
+		*dptr2 = center ^ (Pixel)(dxorshift_128() * (1L << 16));
 #else
 		*dptr2 = center;
 #endif
-		dptr2 = ((uint16 *)(dptr + dstPitch)) - 1;
+		dptr2 = ((Pixel *)(dptr + dstPitch)) - 1;
 		*dptr2++ = center;
 		*dptr2++ = center;
 		*dptr2 = center;
@@ -2346,19 +2349,19 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 	}
 
 	ptmp = tmp;
-	dptr2 = ((uint16 *)(dptr - dstPitch)) - 1;
+	dptr2 = ((Pixel *)(dptr - dstPitch)) - 1;
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp++;
-	dptr2 = ((uint16 *) dptr) - 1;
+	dptr2 = ((Pixel *) dptr) - 1;
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (uint16)(dxorshift_128() * (1L << 16));
+	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift_128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
-	dptr2 = ((uint16 *)(dptr + dstPitch)) - 1;
+	dptr2 = ((Pixel *)(dptr + dstPitch)) - 1;
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp;
@@ -2367,22 +2370,22 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
-                                    uint16 *pixels, int sub_type, int16 *bptr,
+                                    Pixel *pixels, int sub_type, int16 *bptr,
                                     int8 *sim,
                                     int interpolate_2x) {
-	uint16 *dptr2;
-	uint16 center = pixels[4];
+	Pixel *dptr2;
+	Pixel center = pixels[4];
 	int32 diff1, diff2, diff3;
 	int16 tmp_grey;
-	uint16 tmp[4];
-	uint16 *ptmp;
+	Pixel tmp[4];
+	Pixel *ptmp;
 
 	switch (sub_type) {
 	case 1:     /* '- */
 		tmp[0] = tmp[1] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2402,12 +2405,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[2];
+					Pixel tmp_pixel = tmp[2];
 					tmp[2] = interpolate_3_1(tmp_pixel, center);
 					tmp[3] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2415,7 +2418,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[2] = interpolate_1_1(tmp[2], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[2]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
 						tmp[2] = center;
 				}
 			}
@@ -2427,7 +2430,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2447,12 +2450,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[1] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[1];
+					Pixel tmp_pixel = tmp[1];
 					tmp[1] = interpolate_3_1(tmp_pixel, center);
 					tmp[0] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2460,7 +2463,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[1] = interpolate_1_1(tmp[1], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[1]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
 						tmp[1] = center;
 				}
 			}
@@ -2492,7 +2495,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[tmp[1]] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])] ||
 			         (_simSum == 1 && (sim[0] || sim[7]) &&
 			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 				tmp[1] = center;
@@ -2517,7 +2520,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[tmp[2]] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])] ||
 			         (_simSum == 1 && (sim[0] || sim[7]) &&
 			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 				tmp[2] = center;
@@ -2529,7 +2532,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2549,12 +2552,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[1] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[1]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[1];
+					Pixel tmp_pixel = tmp[1];
 					tmp[1] = interpolate_3_1(tmp_pixel, center);
 					tmp[3] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2562,7 +2565,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[1] = interpolate_1_1(tmp[1], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[1]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
 						tmp[1] = center;
 				}
 			}
@@ -2574,7 +2577,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2594,12 +2597,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[2]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[2];
+					Pixel tmp_pixel = tmp[2];
 					tmp[2] = interpolate_3_1(tmp_pixel, center);
 					tmp[0] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2607,7 +2610,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[2] = interpolate_1_1(tmp[2], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[2]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
 						tmp[2] = center;
 				}
 			}
@@ -2619,7 +2622,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2639,12 +2642,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[0];
+					Pixel tmp_pixel = tmp[0];
 					tmp[0] = interpolate_3_1(tmp_pixel, center);
 					tmp[2] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2652,7 +2655,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[0] = interpolate_1_1(tmp[0], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[0]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
 						tmp[0] = center;
 				}
 			}
@@ -2664,7 +2667,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = center;
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2684,12 +2687,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[3] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[3];
+					Pixel tmp_pixel = tmp[3];
 					tmp[3] = interpolate_3_1(tmp_pixel, center);
 					tmp[1] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2697,7 +2700,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[3] = interpolate_1_1(tmp[3], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[3]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
 						tmp[3] = center;
 				}
 			}
@@ -2729,7 +2732,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[tmp[0]] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])] ||
 			         (_simSum == 1 && (sim[2] || sim[5]) &&
 			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 				tmp[0] = center;
@@ -2754,7 +2757,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[tmp[3]] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])] ||
 			         (_simSum == 1 && (sim[2] || sim[5]) &&
 			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 				tmp[3] = center;
@@ -2766,7 +2769,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = center;
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2786,12 +2789,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[3] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[3]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[3];
+					Pixel tmp_pixel = tmp[3];
 					tmp[3] = interpolate_3_1(tmp_pixel, center);
 					tmp[2] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2799,7 +2802,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[3] = interpolate_1_1(tmp[3], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[3]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
 						tmp[3] = center;
 				}
 			}
@@ -2811,7 +2814,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2831,12 +2834,12 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[tmp[0]];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2) {
 				if (interpolate_2x) {
-					uint16 tmp_pixel = tmp[0];
+					Pixel tmp_pixel = tmp[0];
 					tmp[0] = interpolate_3_1(tmp_pixel, center);
 					tmp[1] = interpolate_3_1(center, tmp_pixel);
 				}
@@ -2844,7 +2847,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[0] = interpolate_1_1(tmp[0], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[0]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
 						tmp[0] = center;
 				}
 			}
@@ -2856,7 +2859,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2882,7 +2885,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = interpolate_1_1(center, tmp[0]);
 					tmp[2] = interpolate_2_1(center, tmp[0]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[0]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
 						tmp[0] = center;
 				}
 
@@ -2896,7 +2899,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2922,7 +2925,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[2] = interpolate_1_1(center, tmp[2]);
 					tmp[0] = interpolate_2_1(center, tmp[2]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[2]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
 						tmp[2] = center;
 				}
 
@@ -2941,7 +2944,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2967,7 +2970,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[1] = interpolate_1_1(center, tmp[1]);
 					tmp[3] = interpolate_2_1(center, tmp[1]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[1]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
 						tmp[1] = center;
 				}
 
@@ -2981,7 +2984,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -3007,7 +3010,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[3] = interpolate_1_1(center, tmp[3]);
 					tmp[1] = interpolate_2_1(center, tmp[3]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[3]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
 						tmp[3] = center;
 				}
 
@@ -3026,7 +3029,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[tmp[0]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -3052,7 +3055,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[0] = interpolate_1_1(center, tmp[0]);
 					tmp[1] = interpolate_2_1(center, tmp[0]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[0]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
 						tmp[0] = center;
 				}
 
@@ -3066,7 +3069,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[tmp[1]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -3092,7 +3095,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[1] = interpolate_1_1(center, tmp[1]);
 					tmp[0] = interpolate_2_1(center, tmp[1]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[1]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
 						tmp[1] = center;
 				}
 
@@ -3111,7 +3114,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[2]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -3137,7 +3140,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[2] = interpolate_1_1(center, tmp[2]);
 					tmp[3] = interpolate_2_1(center, tmp[2]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[2]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
 						tmp[2] = center;
 				}
 
@@ -3151,7 +3154,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[tmp[3]];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -3177,7 +3180,7 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 					tmp[3] = interpolate_1_1(center, tmp[3]);
 					tmp[2] = interpolate_2_1(center, tmp[3]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[tmp[3]])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
 						tmp[3] = center;
 				}
 
@@ -3197,14 +3200,14 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 	case 6:     /* | */
 	case 127:   /* * */
 	default:    /* no edge */
-		dptr2 = (uint16 *) dptr;
+		dptr2 = (Pixel *) dptr;
 		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-		*dptr2 = center ^ (uint16)(dxorshift_128() * (1L << 16));
+		*dptr2 = center ^ (Pixel)(dxorshift_128() * (1L << 16));
 #else
 		*dptr2 = center;
 #endif
-		dptr2 = (uint16 *)(dptr + dstPitch);
+		dptr2 = (Pixel *)(dptr + dstPitch);
 		*dptr2++ = center;
 		*dptr2 = center;
 
@@ -3214,14 +3217,14 @@ void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
 	}
 
 	ptmp = tmp;
-	dptr2 = (uint16 *) dptr;
+	dptr2 = (Pixel *) dptr;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (uint16)(dxorshift_128() * (1L << 16));
+	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift_128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
-	dptr2 = (uint16 *)(dptr + dstPitch);
+	dptr2 = (Pixel *)(dptr + dstPitch);
 	*dptr2++ = *ptmp++;
 	*dptr2 = *ptmp;
 }
@@ -3310,9 +3313,9 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst + dstPitch + 2;
-	const uint16 *sptr16;
-	const uint16 *oldSptr;
-	uint16 *dptr16;
+	const Pixel *sptr16;
+	const Pixel *oldSptr;
+	Pixel *dptr16;
 	int16 *bplane;
 	int8 sim[8];
 	int sub_type;
@@ -3322,21 +3325,21 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3, oldSrc += oldPitch) {
 		for (x = 0,
-		        sptr16 = (const uint16 *) sptr8,
-		        oldSptr = (const uint16 *) oldSrc,
-		        dptr16 = (uint16 *) dptr8;
+		        sptr16 = (const Pixel *) sptr8,
+		        oldSptr = (const Pixel *) oldSrc,
+		        dptr16 = (Pixel *) dptr8;
 		        x < w; x++, sptr16++, dptr16 += 3, oldSptr++) {
-			const uint16 *sptr2, *addr3;
-			uint16 pixels[9];
+			const Pixel *sptr2, *addr3;
+			Pixel pixels[9];
 			char edge_type;
 
-			sptr2 = ((const uint16 *)((const uint8 *) sptr16 - srcPitch)) - 1;
-			addr3 = ((const uint16 *)((const uint8 *) sptr16 + srcPitch)) + 1;
+			sptr2 = ((const Pixel *)((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const Pixel *)((const uint8 *) sptr16 + srcPitch)) + 1;
 
 			/* fill the 3x3 grid */
-			memcpy(pixels, sptr2, 3 * sizeof(uint16));
-			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
-			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
+			memcpy(pixels, sptr2, 3 * sizeof(Pixel));
+			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(Pixel));
+			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(Pixel));
 
 			if (haveOldSrc) {
 				/* skip interior unchanged 3x3 blocks */
@@ -3368,10 +3371,10 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 			edge_type = findPrincipleAxis(diffs, bplane,
 			                              sim, &angle);
-			sub_type = refineDirection(edge_type, pixels, bplane,
+			sub_type = refineDirection<Pixel>(edge_type, pixels, bplane,
 			                           sim, angle);
 			if (sub_type >= 0)
-				sub_type = fixKnights(sub_type, pixels, sim);
+				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
 			anti_alias_grid_clean_3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                                    sub_type, bplane);
@@ -3390,9 +3393,9 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst;
-	const uint16 *sptr16;
-	const uint16 *oldSptr;
-	uint16 *dptr16;
+	const Pixel *sptr16;
+	const Pixel *oldSptr;
+	Pixel *dptr16;
 	int16 *bplane;
 	int8 sim[8];
 	int sub_type;
@@ -3402,21 +3405,21 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2, oldSrc += oldSrcPitch) {
 		for (x = 0,
-		        sptr16 = (const uint16 *) sptr8,
-		        dptr16 = (uint16 *) dptr8,
-				oldSptr = (const uint16 *) oldSrc;
+		        sptr16 = (const Pixel *) sptr8,
+		        dptr16 = (Pixel *) dptr8,
+				oldSptr = (const Pixel *) oldSrc;
 		        x < w; x++, sptr16++, dptr16 += 2, oldSptr++) {
-			const uint16 *sptr2, *addr3;
-			uint16 pixels[9];
+			const Pixel *sptr2, *addr3;
+			Pixel pixels[9];
 			char edge_type;
 
-			sptr2 = ((const uint16 *)((const uint8 *) sptr16 - srcPitch)) - 1;
-			addr3 = ((const uint16 *)((const uint8 *) sptr16 + srcPitch)) + 1;
+			sptr2 = ((const Pixel *)((const uint8 *) sptr16 - srcPitch)) - 1;
+			addr3 = ((const Pixel *)((const uint8 *) sptr16 + srcPitch)) + 1;
 
 			/* fill the 3x3 grid */
-			memcpy(pixels, sptr2, 3 * sizeof(uint16));
-			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(uint16));
-			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(uint16));
+			memcpy(pixels, sptr2, 3 * sizeof(Pixel));
+			memcpy(pixels + 3, sptr16 - 1, 3 * sizeof(Pixel));
+			memcpy(pixels + 6, addr3 - 2, 3 * sizeof(Pixel));
 
 			if (haveOldSrc) {
 				/* skip interior unchanged 3x3 blocks */
@@ -3448,10 +3451,10 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 			edge_type = findPrincipleAxis(diffs, bplane,
 			                              sim, &angle);
-			sub_type = refineDirection(edge_type, pixels, bplane,
+			sub_type = refineDirection<Pixel>(edge_type, pixels, bplane,
 			                           sim, angle);
 			if (sub_type >= 0)
-				sub_type = fixKnights(sub_type, pixels, sim);
+				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
 			anti_alias_grid_2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                              sub_type, bplane, sim,
@@ -3543,7 +3546,17 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, false, NULL, 0);
 		}
 	} else {
-		warning("FIXME: EdgePlugin 32bpp format");
+		if (_factor == 2) {
+			if (_format.aLoss == 0)
+				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, false, NULL, 0);
+			else
+				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, false, NULL, 0);
+		} else {
+			if (_format.aLoss == 0)
+				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, false, NULL, 0);
+			else
+				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, false, NULL, 0);
+		}
 	}
 }
 
@@ -3562,7 +3575,17 @@ void EdgePlugin::oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
 				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
 		}
 	} else {
-		warning("FIXME: EdgePlugin 32bpp format");
+		if (_factor == 2) {
+			if (_format.aLoss == 0)
+				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+			else
+				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+		} else {
+			if (_format.aLoss == 0)
+				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+			else
+				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+		}
 	}
 }
 
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 4e13840637..dcf779c1b5 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -103,7 +103,8 @@ private:
 	/**
 	 * Check for mis-detected arrow patterns.  Return 1 (good), 0 (bad).
 	 */
-	int checkArrows(int best_dir, uint16 *pixels, int8 *sim, int half_flag);
+	template<typename Pixel>
+	int checkArrows(int best_dir, Pixel *pixels, int8 *sim, int half_flag);
 
 	/**
 	 * Take original direction, refine it by testing different pixel difference
@@ -112,13 +113,15 @@ private:
 	 * The angle value is not currently used, but may be useful for future
 	 * refinement algorithms.
 	 */
-	int refineDirection(char edge_type, uint16 *pixels, int16 *bptr,
+	template<typename Pixel>
+	int refineDirection(char edge_type, Pixel *pixels, int16 *bptr,
 		int8 *sim, double angle);
 
 	/**
 	 * "Chess Knight" patterns can be mis-detected, fix easy cases.
 	 */
-	int fixKnights(int sub_type, uint16 *pixels, int8 *sim);
+	template<typename Pixel>
+	int fixKnights(int sub_type, Pixel *pixels, int8 *sim);
 
 	/**
 	 * Initialize various lookup tables
@@ -131,7 +134,7 @@ private:
 	 */
 	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
-		uint16 *pixels, int sub_type, int16 *bptr,
+		Pixel *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
 		int interpolate_2x);
 
@@ -140,7 +143,7 @@ private:
 	 */
 	template<typename ColorMask, typename Pixel>
 	void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
-		uint16 *pixels, int sub_type, int16 *bptr);
+		Pixel *pixels, int sub_type, int16 *bptr);
 
 	/**
 	 * Perform edge detection, draw the new 2x pixels


Commit: 56d078e732fc3e541b61eb8d22a1507f19772010
    https://github.com/scummvm/scummvm/commit/56d078e732fc3e541b61eb8d22a1507f19772010
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix assignment of variable

GCC seemed to allow the previous syntax but clang rejected it.

Changed paths:
    backends/platform/sdl/sdl.cpp


diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index d6dc564a6a..40d65440c5 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -80,7 +80,7 @@ struct LegacyGraphicsMode {
 
 // Table for using old names for scalers in the configuration
 // to keep compatibiblity with old config files.
-static const LegacyGraphicsMode s_legacyGraphicsModes[] {
+static const LegacyGraphicsMode s_legacyGraphicsModes[] = {
 	{ "supereagle2x", "supereagle" },
 	{ "dotmatrix2x", "dotmatrix" },
 	{ "sai2x", "2xsai" },


Commit: 179705071dbf2e5edf1faa38c7b3d8c5c3259095
    https://github.com/scummvm/scummvm/commit/179705071dbf2e5edf1faa38c7b3d8c5c3259095
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Fix Edge3x scaler

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 74435be428..10d85fa5ca 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3312,7 +3312,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 								 const uint8* oldSrc, int oldPitch) {
 	int x, y;
 	const uint8 *sptr8 = src;
-	uint8 *dptr8 = dst + dstPitch + 2;
+	uint8 *dptr8 = dst + dstPitch + sizeof(Pixel);
 	const Pixel *sptr16;
 	const Pixel *oldSptr;
 	Pixel *dptr16;


Commit: c915efa4524ae46e01b4e6db279ea64ccb4a98c4
    https://github.com/scummvm/scummvm/commit/c915efa4524ae46e01b4e6db279ea64ccb4a98c4
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Respect alpha and rgba formats in interpolation and conversion

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/intern.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 10d85fa5ca..6080c9fc5d 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -298,7 +298,8 @@ uint16 convertTo16Bit(Pixel p) {
 		>> (ColorMask::kRedShift + (8 - Graphics::ColorMasks<565>::kRedBits) - Graphics::ColorMasks<565>::kRedShift);
 	g = (ColorMask::kGreenMask & (ColorMask::kGreenMask << (8 - Graphics::ColorMasks<565>::kGreenBits)) & p)
 		>> (ColorMask::kGreenShift + (8 - Graphics::ColorMasks<565>::kGreenBits) - Graphics::ColorMasks<565>::kGreenShift);
-	b = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & p);
+	b = (ColorMask::kBlueMask & (ColorMask::kBlueMask << (8 - Graphics::ColorMasks<565>::kBlueBits)) & p)
+		>> (ColorMask::kBlueShift + (8 - Graphics::ColorMasks<565>::kBlueBits) - Graphics::ColorMasks<565>::kBlueShift);
 
 	return r | g | b;
 }
diff --git a/graphics/scaler/intern.h b/graphics/scaler/intern.h
index 2a0e1f4d2f..c7e412b22c 100644
--- a/graphics/scaler/intern.h
+++ b/graphics/scaler/intern.h
@@ -293,24 +293,26 @@ uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3) {
  */
 
 template<typename ColorMask>
-uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3)
-{
+uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3) {
 	uint32 rsum, gsum, bsum;
 
-	rsum =  (pixel1 & ColorMask::kRedMask);
-	rsum += (pixel2 & ColorMask::kRedMask);
-	rsum += (pixel3 & ColorMask::kRedMask);
+	rsum =  ((pixel1 & ColorMask::kRedMask) >> ColorMask::kRedShift);
+	rsum += ((pixel2 & ColorMask::kRedMask) >> ColorMask::kRedShift);
+	rsum += ((pixel3 & ColorMask::kRedMask) >> ColorMask::kRedShift);
 	rsum /= 3;
+	rsum <<= ColorMask::kRedShift;
 
-	gsum =  (pixel1 & ColorMask::kGreenMask);
-	gsum += (pixel2 & ColorMask::kGreenMask);
-	gsum += (pixel3 & ColorMask::kGreenMask);
+	gsum =  ((pixel1 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
+	gsum += ((pixel2 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
+	gsum += ((pixel3 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
 	gsum /= 3;
+	gsum <<= ColorMask::kGreenShift;
 
-	bsum =  (pixel1 & ColorMask::kBlueMask);
-	bsum += (pixel2 & ColorMask::kBlueMask);
-	bsum += (pixel3 & ColorMask::kBlueMask);
+	bsum =  ((pixel1 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
+	bsum += ((pixel2 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
+	bsum += ((pixel3 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
 	bsum /= 3;
+	bsum <<= ColorMask::kBlueShift;
 
 	return (rsum & ColorMask::kRedMask) | (gsum & ColorMask::kGreenMask) | (bsum & ColorMask::kBlueMask);
 }


Commit: c5c56623307fec022e7de02eb16713e439819232
    https://github.com/scummvm/scummvm/commit/c5c56623307fec022e7de02eb16713e439819232
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS,SDL: Change oldSrcScale api

More bookkeeping is kept in common scaler code instead of the backend.
Plugins inheriting SourceScaler will have this functionality.

TODO: Some new functions may still be able to be combined to simplify
the API.

Changed paths:
  A graphics/scalerplugin.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h
    graphics/module.mk
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 37e1fabdc7..a92a721cce 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -120,7 +120,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #if defined(WIN32) && !SDL_VERSION_ATLEAST(2, 0, 0)
 	_originalBitsPerPixel(0),
 #endif
-	_screen(0), _tmpscreen(0), _oldscreen(0), _destbuffer(0),
+	_screen(0), _tmpscreen(0), _destbuffer(0),
 	_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_useOldSrc(false),
@@ -682,17 +682,8 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		_extraPixels = (*_scalerPlugin)->extraPixels();
 		_useOldSrc = (*_scalerPlugin)->useOldSrc();
 		if (_useOldSrc) {
-			if (!_oldscreen) {
-				_oldscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
-									_videoMode.screenHeight + _maxExtraPixels * 2,
-									16,
-									_hwscreen->format->Rmask,
-									_hwscreen->format->Gmask,
-									_hwscreen->format->Bmask,
-									_hwscreen->format->Amask);
-				if (_oldscreen == NULL)
-					error("allocating _oldscreen failed");
-			}
+			(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
+										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels, SRC_SCREEN);
 			if (!_destbuffer) {
 				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
 									_videoMode.screenHeight * _videoMode.scaleFactor,
@@ -989,15 +980,8 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 
 	if (_useOldSrc) {
 		// Create surface containing previous frame's data to pass to scaler
-		_oldscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + _maxExtraPixels * 2,
-							_videoMode.screenHeight + _maxExtraPixels * 2,
-							16,
-							_hwscreen->format->Rmask,
-							_hwscreen->format->Gmask,
-							_hwscreen->format->Bmask,
-							_hwscreen->format->Amask);
-		if (_oldscreen == NULL)
-			error("allocating _oldscreen failed");
+		(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
+									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels, SRC_SCREEN);
 
 		// Create surface containing the raw output from the scaler
 		_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
@@ -1057,11 +1041,6 @@ void SurfaceSdlGraphicsManager::unloadGFXMode() {
 		_tmpscreen = NULL;
 	}
 
-	if (_oldscreen) {
-		SDL_FreeSurface(_oldscreen);
-		_oldscreen = NULL;
-	}
-
 	if (_destbuffer) {
 		SDL_FreeSurface(_destbuffer);
 		_destbuffer = NULL;
@@ -1110,6 +1089,7 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 	_overlayscreen = NULL;
 
 	// Release the HW screen surface
+<<<<<<< HEAD
 	if (_hwScreen) {
 		SDL_FreeSurface(_hwScreen);
 		_hwScreen = NULL;
@@ -1118,10 +1098,6 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 		SDL_FreeSurface(_tmpscreen);
 		_tmpscreen = NULL;
 	}
-	if (_oldscreen) {
-		SDL_FreeSurface(_oldscreen);
-        _oldscreen = NULL;
-	}
 	if (_destbuffer) {
 		SDL_FreeSurface(_destbuffer);
         _destbuffer = NULL;
@@ -1277,7 +1253,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		SDL_LockSurface(srcSurf);
 		SDL_LockSurface(_hwScreen);
 		if (_useOldSrc && !_overlayVisible) {
-			SDL_LockSurface(_oldscreen);
 			SDL_LockSurface(_destbuffer);
 		}
 
@@ -1322,10 +1297,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				} else {
 					if (_useOldSrc) {
 						// scale into _destbuffer instead of _hwscreen to avoid AR problems
-						(*_scalerPlugin)->oldSrcScale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch,
-							(byte *)_oldscreen->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * _oldscreen->pitch, _oldscreen->pitch,
-							r->w, dst_h, r->x, r->y);
+						(*_scalerPlugin)->oldSrcScale((byte *)_destbuffer->pixels, _destbuffer->pitch, SRC_SCREEN);
 					} else
 						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
@@ -1363,14 +1335,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		SDL_UnlockSurface(_hwScreen);
 
 		if (_useOldSrc && !_overlayVisible) {
-			SDL_UnlockSurface(_oldscreen);
 			SDL_UnlockSurface(_destbuffer);
-
-			// Swap old and new screen
-			SDL_Surface *tmp;
-			tmp = _oldscreen;
-			_oldscreen = _tmpscreen;
-			_tmpscreen = tmp;
 		}
 
 		// Readjust the dirty rect list in case we are doing a full update.
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index d81f64fa06..9a4ff45a8f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -216,8 +216,6 @@ protected:
 
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen;
-	/** Previous frame's screen (for scalers) */
-	SDL_Surface *_oldscreen;
 	/** Previous frame's raw scaled screen (for scalers) */
 	SDL_Surface *_destbuffer;
 	/** Temporary screen (for scalers) */
diff --git a/graphics/module.mk b/graphics/module.mk
index 341c0e0d6f..a6f9008d94 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -31,6 +31,7 @@ MODULE_OBJS := \
 	primitives.o \
 	renderer.o \
 	scaler.o \
+	scalerplugin.o \
 	scaler/thumbnail_intern.o \
 	screen.o \
 	scaler/normal.o \
diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 6080c9fc5d..1a63ebaceb 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3561,8 +3561,8 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-void EdgePlugin::oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
-                       uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height, int x, int y) {
+void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
+                       uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height) {
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index dcf779c1b5..2c8ff5af01 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -24,7 +24,7 @@
 
 #include "graphics/scalerplugin.h"
 
-class EdgePlugin : public ScalerPluginObject {
+class EdgePlugin : public SourceScaler {
 public:
 
 	EdgePlugin();
@@ -32,10 +32,10 @@ public:
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual void oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
+	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 						   uint8 *dstPtr, uint32 dstPitch,
 						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-						   int width, int height, int x, int y);
+						   int width, int height);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
new file mode 100644
index 0000000000..1496b81345
--- /dev/null
+++ b/graphics/scalerplugin.cpp
@@ -0,0 +1,54 @@
+/* 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "graphics/scalerplugin.h"
+
+SourceScaler::SourceScaler() {
+	for (int i = 0; i < SRC_MAX; ++i) {
+		oldSrcs[i] = NULL;
+	}
+}
+
+void SourceScaler::setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type) {
+	widths[type] = width;
+	heights[type] = height;
+	pitches[type] = pitch;
+	newSrcs[type] = src;
+	paddings[type] = padding;
+
+	if (oldSrcs[type] != NULL)
+		free(oldSrcs[type]);
+
+	int size = (height + _format.bytesPerPixel * padding) * 2 * pitch;
+	oldSrcs[type] = new byte[size];
+	memset(oldSrcs[type], 0, size);
+}
+
+void SourceScaler::oldSrcScale(byte *dst, uint dstPitch, SourceType type) {
+	// Call user defined scale function
+	internScale(newSrcs[type] + paddings[type] * 2 + pitches[type] * paddings[type], pitches[type],
+	            dst, dstPitch,
+	            oldSrcs[type] + paddings[type] * 2 + pitches[type] * paddings[type], pitches[type],
+				widths[type], heights[type]);
+	// Update old src
+	memcpy(oldSrcs[type], newSrcs[type], (heights[type] + _format.bytesPerPixel * paddings[type])  * 2 * pitches[type]);
+}
+
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 9c103c1644..6a0451fb3c 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -25,6 +25,15 @@
 #include "base/plugins.h"
 #include "graphics/pixelformat.h"
 
+enum SourceType {
+	SRC_SCREEN = 0,
+	SRC_CURSOR,
+	SRC_OTHER,
+
+	SRC_MAX
+};
+
+
 class ScalerPluginObject : public PluginObject {
 public:
 
@@ -96,18 +105,29 @@ public:
 	 * optionally call it.
 	 *
 	 * @see oldSourceScale
+	 * @see setSource
 	 */
 	virtual bool useOldSrc() const { return false; }
 
 	/**
-	 * Secondary scaling method for computationally intense scalers.
+	 * Set the source to be used when scaling and copying to the old buffer.
 	 *
-	 * @see useOldSource
+	 * @param padding The number of pixels on the border (Used to prevent memory access crashes)
+	 * @param type    The surface type. This source will only be used when calling oldSrcScale with the same type.
 	 */
-	virtual void oldSrcScale(const uint8 *srcPtr, uint32 srcPitch,
-	                         uint8 *dstPtr, uint32 dstPitch,
-	                         const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-	                         int width, int height, int x, int y) {
+	virtual void setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type) {
+		// Should not be called unless overriden
+		assert(0);
+	}
+
+	/**
+	 * Scale using the source from setSource called with the same value as type.
+	 * The source will be compared against previous frames to avoid computations
+	 * on unchanged pixels.
+	 *
+	 * @param type    The surface type set previously with setSource
+	 */
+	virtual void oldSrcScale(byte *dst, uint dstPitch, SourceType type) {
 		// Should not be called unless overriden
 		assert(0);
 	}
@@ -118,6 +138,40 @@ protected:
 	Graphics::PixelFormat _format;
 };
 
+/**
+ * Convenience class that implements some bookkeeping for keeping track of
+ * old source images.
+ */
+class SourceScaler : public ScalerPluginObject {
+
+public:
+
+	SourceScaler();
+
+	virtual void setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type);
+	virtual void oldSrcScale(byte *dst, uint dstPitch, SourceType type);
+
+protected:
+
+	/**
+	 * Scalers must implement this function. It will be called by oldSrcScale.
+	 * If by comparing the src and oldsrc images it is discovered that no change
+	 * is necessary, do not write a pixel.
+	 */
+	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
+	                         uint8 *dstPtr, uint32 dstPitch,
+	                         const uint8 *oldSrcPtr, uint32 oldSrcPitch,
+	                         int width, int height) = 0;
+
+	int widths[SRC_MAX];
+	int heights[SRC_MAX];
+	uint paddings[SRC_MAX];
+	uint pitches[SRC_MAX];
+
+	byte *newSrcs[SRC_MAX];
+	byte *oldSrcs[SRC_MAX];
+};
+
 typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;
 
 /**


Commit: cb5c323b54fc41cb89ff59c4350b60c7940f93c1
    https://github.com/scummvm/scummvm/commit/cb5c323b54fc41cb89ff59c4350b60c7940f93c1
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Constify the src in setSource

Changed paths:
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index 1496b81345..2575be3002 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -27,7 +27,7 @@ SourceScaler::SourceScaler() {
 	}
 }
 
-void SourceScaler::setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type) {
+void SourceScaler::setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type) {
 	widths[type] = width;
 	heights[type] = height;
 	pitches[type] = pitch;
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 6a0451fb3c..5a849b8a0f 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -115,7 +115,7 @@ public:
 	 * @param padding The number of pixels on the border (Used to prevent memory access crashes)
 	 * @param type    The surface type. This source will only be used when calling oldSrcScale with the same type.
 	 */
-	virtual void setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type) {
+	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type) {
 		// Should not be called unless overriden
 		assert(0);
 	}
@@ -148,7 +148,7 @@ public:
 
 	SourceScaler();
 
-	virtual void setSource(byte *src, uint pitch, int width, int height, int padding, SourceType type);
+	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type);
 	virtual void oldSrcScale(byte *dst, uint dstPitch, SourceType type);
 
 protected:
@@ -168,7 +168,7 @@ protected:
 	uint paddings[SRC_MAX];
 	uint pitches[SRC_MAX];
 
-	byte *newSrcs[SRC_MAX];
+	const byte *newSrcs[SRC_MAX];
 	byte *oldSrcs[SRC_MAX];
 };
 


Commit: 4c967c5fbcd9c2c37c9e1e5bfe4f1895cded3ddf
    https://github.com/scummvm/scummvm/commit/4c967c5fbcd9c2c37c9e1e5bfe4f1895cded3ddf
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS,SDL: Revision to scaler API

oldSrcScale() is now unified with scale(). Use enableSource(bool enable)
to switch functionality. Partial screen updates are also posible because
of this.

initialize() had its prototype changes although it is functionally the
same.

setSource() no longer has a type parameter, removing support for
multiple source surfaces. They weren't useful anyway.

useOldSrc() was renamed to useOldSource() to conform to the names of
other functions.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scaler/hq.cpp
    graphics/scaler/hq.h
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scaler/pm.cpp
    graphics/scaler/pm.h
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h
    graphics/scaler/scalebit.cpp
    graphics/scaler/scalebit.h
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index a92a721cce..2f4bd868af 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -680,10 +680,11 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		convertSDLPixelFormat(_hwscreen->format, &format);
 		(*_scalerPlugin)->initialize(format);
 		_extraPixels = (*_scalerPlugin)->extraPixels();
-		_useOldSrc = (*_scalerPlugin)->useOldSrc();
+		_useOldSrc = (*_scalerPlugin)->useOldSource();
 		if (_useOldSrc) {
+			(*_scalerPlugin)->enableSource(true);
 			(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
-										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels, SRC_SCREEN);
+										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
 			if (!_destbuffer) {
 				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
 									_videoMode.screenHeight * _videoMode.scaleFactor,
@@ -981,7 +982,7 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 	if (_useOldSrc) {
 		// Create surface containing previous frame's data to pass to scaler
 		(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
-									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels, SRC_SCREEN);
+									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
 
 		// Create surface containing the raw output from the scaler
 		_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
@@ -1089,7 +1090,6 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 	_overlayscreen = NULL;
 
 	// Release the HW screen surface
-<<<<<<< HEAD
 	if (_hwScreen) {
 		SDL_FreeSurface(_hwScreen);
 		_hwScreen = NULL;
@@ -1226,7 +1226,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 	// Force a full redraw if requested.
 	// If _useOldSrc, the scaler will do its own partial updates.
-	if (_forceRedraw || (_useOldSrc && !_overlayVisible && _numDirtyRects > 0)) {
+	if (_forceFull) {
 		_numDirtyRects = 1;
 		_dirtyRectList[0].x = 0;
 		_dirtyRectList[0].y = 0;
@@ -1297,7 +1297,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				} else {
 					if (_useOldSrc) {
 						// scale into _destbuffer instead of _hwscreen to avoid AR problems
-						(*_scalerPlugin)->oldSrcScale((byte *)_destbuffer->pixels, _destbuffer->pitch, SRC_SCREEN);
+						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
 					} else
 						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index f5e8103adc..e2945b1886 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -27,7 +27,7 @@ DotMatrixPlugin::DotMatrixPlugin() {
 	_factors.push_back(2);
 }
 
-void DotMatrixPlugin::initialize(Graphics::PixelFormat format) {
+void DotMatrixPlugin::initialize(const Graphics::PixelFormat &format) {
 	if (format.bytesPerPixel == 2) {
 		uint16 *lookup16 = (uint16 *)lookup;
 		lookup16[0] = lookup16[10] = format.RGBToColor(0, 63, 0);
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 06d5801e3f..a6e52c70b1 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -27,7 +27,7 @@
 class DotMatrixPlugin : public ScalerPluginObject {
 public:
 	DotMatrixPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 1a63ebaceb..047fa50afc 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3518,20 +3518,20 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
 	}
 }
 
-EdgePlugin::EdgePlugin() {
+EdgePlugin::EdgePlugin() : SourceScaler() {
 	_factor = 2;
 	_factors.push_back(2);
 	_factors.push_back(3);
 }
 
-void EdgePlugin::initialize(Graphics::PixelFormat format) {
+void EdgePlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 	initTables(0, 0, 0, 0);
 }
 
 void EdgePlugin::deinitialize() {
 }
-
+#if 0
 void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
                        uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
@@ -3560,32 +3560,34 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		}
 	}
 }
+#endif
 
 void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
                        uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height) {
+	bool enable = oldSrcPtr != NULL;
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
 		}
 	} else {
 		if (_factor == 2) {
 			if (_format.aLoss == 0)
-				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
 		} else {
 			if (_format.aLoss == 0)
-				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, true, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
 		}
 	}
 }
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 2c8ff5af01..84193c7aac 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -28,10 +28,8 @@ class EdgePlugin : public SourceScaler {
 public:
 
 	EdgePlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 						   uint8 *dstPtr, uint32 dstPitch,
 						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
@@ -40,7 +38,7 @@ public:
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
-	virtual bool useOldSrc() const { return true; }
+	virtual bool useOldSource() const { return true; }
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index c28dfef981..0ba077b607 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -4990,7 +4990,7 @@ HQPlugin::HQPlugin() {
 	_factors.push_back(3);
 }
 
-void HQPlugin::initialize(Graphics::PixelFormat format) {
+void HQPlugin::initialize(const Graphics::PixelFormat &format) {
 	if (format.bytesPerPixel == 2) {
 		InitLUT(format);
 	} else {
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index 842f506a0d..bf4ed27629 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -27,7 +27,7 @@
 class HQPlugin : public ScalerPluginObject {
 public:
 	HQPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index cccec22789..4331c6918a 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -31,7 +31,7 @@ NormalPlugin::NormalPlugin() {
 #endif
 }
 
-void NormalPlugin::initialize(Graphics::PixelFormat format) {
+void NormalPlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index eeb3e11c97..56ebd8bc2d 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -27,7 +27,7 @@
 class NormalPlugin : public ScalerPluginObject {
 public:
 	NormalPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
diff --git a/graphics/scaler/pm.cpp b/graphics/scaler/pm.cpp
index 10446afbcb..77bd546e25 100644
--- a/graphics/scaler/pm.cpp
+++ b/graphics/scaler/pm.cpp
@@ -190,7 +190,7 @@ PMPlugin::PMPlugin() {
 	_factors.push_back(2);
 }
 
-void PMPlugin::initialize(Graphics::PixelFormat format) {
+void PMPlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
diff --git a/graphics/scaler/pm.h b/graphics/scaler/pm.h
index 3357072927..9f7d64c7a7 100644
--- a/graphics/scaler/pm.h
+++ b/graphics/scaler/pm.h
@@ -27,7 +27,7 @@
 class PMPlugin : public ScalerPluginObject {
 public:
 	PMPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index bcbf765050..1c5aabd0ab 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -391,7 +391,7 @@ SAIPlugin::SAIPlugin() {
 	_factors.push_back(2);
 }
 
-void SAIPlugin::initialize(Graphics::PixelFormat format) {
+void SAIPlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
@@ -438,7 +438,7 @@ SuperSAIPlugin::SuperSAIPlugin() {
 	_factors.push_back(2);
 }
 
-void SuperSAIPlugin::initialize(Graphics::PixelFormat format) {
+void SuperSAIPlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
@@ -485,7 +485,7 @@ SuperEaglePlugin::SuperEaglePlugin() {
 	_factors.push_back(2);
 }
 
-void SuperEaglePlugin::initialize(Graphics::PixelFormat format) {
+void SuperEaglePlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index bcfe870c5f..89bd6cb0f7 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -27,7 +27,7 @@
 class SAIPlugin : public ScalerPluginObject {
 public:
 	SAIPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
@@ -45,7 +45,7 @@ private:
 class SuperSAIPlugin : public ScalerPluginObject {
 public:
 	SuperSAIPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
@@ -63,7 +63,7 @@ private:
 class SuperEaglePlugin : public ScalerPluginObject {
 public:
 	SuperEaglePlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp
index 45794937b1..2546d19cee 100644
--- a/graphics/scaler/scalebit.cpp
+++ b/graphics/scaler/scalebit.cpp
@@ -359,7 +359,7 @@ AdvMamePlugin::AdvMamePlugin() {
 	_factors.push_back(4);
 }
 
-void AdvMamePlugin::initialize(Graphics::PixelFormat format) {
+void AdvMamePlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index 46f6dd95c9..4461a789ac 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -44,7 +44,7 @@ void scale(unsigned scale, void* void_dst, unsigned dst_slice, const void* void_
 class AdvMamePlugin : public ScalerPluginObject {
 public:
 	AdvMamePlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index 53dfa047b8..b1cb3a72c1 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -28,7 +28,7 @@ TVPlugin::TVPlugin() {
 	_factors.push_back(2);
 }
 
-void TVPlugin::initialize(Graphics::PixelFormat format) {
+void TVPlugin::initialize(const Graphics::PixelFormat &format) {
 	_format = format;
 }
 
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 7cabdd7659..560cdc69b2 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -27,7 +27,7 @@
 class TVPlugin : public ScalerPluginObject {
 public:
 	TVPlugin();
-	virtual void initialize(Graphics::PixelFormat format);
+	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index 2575be3002..fe9dd7aff4 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -21,34 +21,47 @@
 
 #include "graphics/scalerplugin.h"
 
-SourceScaler::SourceScaler() {
-	for (int i = 0; i < SRC_MAX; ++i) {
-		oldSrcs[i] = NULL;
-	}
+SourceScaler::SourceScaler() : _oldSrc(NULL), _enable(false) {
 }
 
-void SourceScaler::setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type) {
-	widths[type] = width;
-	heights[type] = height;
-	pitches[type] = pitch;
-	newSrcs[type] = src;
-	paddings[type] = padding;
+SourceScaler::~SourceScaler() {
+	if (_oldSrc != NULL)
+		delete[] _oldSrc;
+}
 
-	if (oldSrcs[type] != NULL)
-		free(oldSrcs[type]);
+void SourceScaler::setSource(const byte *src, uint pitch, int width, int height, int padding) {
+	if (_oldSrc != NULL)
+		delete[] _oldSrc;
 
-	int size = (height + _format.bytesPerPixel * padding) * 2 * pitch;
-	oldSrcs[type] = new byte[size];
-	memset(oldSrcs[type], 0, size);
+	_padding = padding;
+	// Give _oldSrc same pitch
+	int size = (height + padding * 2) * pitch;
+	_oldSrc = new byte[size];
+	memset(_oldSrc, 0, size);
 }
 
-void SourceScaler::oldSrcScale(byte *dst, uint dstPitch, SourceType type) {
+void SourceScaler::scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+                         uint32 dstPitch, int width, int height, int x, int y) {
+	if (!_enable) {
+		// Do not pass _oldSrc, do not update _oldSrc
+		internScale(srcPtr, srcPitch,
+					dstPtr, dstPitch,
+					NULL, 0,
+					width, height);
+		return;
+	}
+	int offset = (_padding + x) * _format.bytesPerPixel + (_padding + y) * srcPitch;
 	// Call user defined scale function
-	internScale(newSrcs[type] + paddings[type] * 2 + pitches[type] * paddings[type], pitches[type],
-	            dst, dstPitch,
-	            oldSrcs[type] + paddings[type] * 2 + pitches[type] * paddings[type], pitches[type],
-				widths[type], heights[type]);
+	internScale(srcPtr, srcPitch,
+				dstPtr, dstPitch,
+				_oldSrc + offset, srcPitch,
+				width, height);
 	// Update old src
-	memcpy(oldSrcs[type], newSrcs[type], (heights[type] + _format.bytesPerPixel * paddings[type])  * 2 * pitches[type]);
+	byte *oldSrc = _oldSrc + offset;
+	while (height--) {
+		memcpy(oldSrc, srcPtr, width * _format.bytesPerPixel);
+		oldSrc += srcPitch;
+		srcPtr += srcPitch;
+	}
 }
 
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 5a849b8a0f..f6cbe6fc58 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -25,15 +25,6 @@
 #include "base/plugins.h"
 #include "graphics/pixelformat.h"
 
-enum SourceType {
-	SRC_SCREEN = 0,
-	SRC_CURSOR,
-	SRC_OTHER,
-
-	SRC_MAX
-};
-
-
 class ScalerPluginObject : public PluginObject {
 public:
 
@@ -44,7 +35,7 @@ public:
 	 * Precomputed data should be generated here.
 	 * @param format The pixel format to scale.
 	 */
-	virtual void initialize(Graphics::PixelFormat format) = 0;
+	virtual void initialize(const Graphics::PixelFormat &format) = 0;
 
 	/**
 	 * This is called when the plugin is not needed. It should clean
@@ -102,12 +93,12 @@ public:
 	 * Computationally intense scalers can benefit from comparing new and old
 	 * source images and updating only the pixels necessary. If the function
 	 * returns true, this scaler prefers this method and the backend can
-	 * optionally call it.
+	 * optionally use it.
 	 *
-	 * @see oldSourceScale
+	 * @see enableSource
 	 * @see setSource
 	 */
-	virtual bool useOldSrc() const { return false; }
+	virtual bool useOldSource() const { return false; }
 
 	/**
 	 * Set the source to be used when scaling and copying to the old buffer.
@@ -115,19 +106,18 @@ public:
 	 * @param padding The number of pixels on the border (Used to prevent memory access crashes)
 	 * @param type    The surface type. This source will only be used when calling oldSrcScale with the same type.
 	 */
-	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type) {
+	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding) {
 		// Should not be called unless overriden
 		assert(0);
 	}
 
 	/**
-	 * Scale using the source from setSource called with the same value as type.
-	 * The source will be compared against previous frames to avoid computations
-	 * on unchanged pixels.
+	 * Enable or disable the old Source functionality. It is initially
+	 * disabled. When disabled, the old source data is preserved until re-enabled.
 	 *
-	 * @param type    The surface type set previously with setSource
+	 * Useful for scaling a different surface (e.g. the cursor).
 	 */
-	virtual void oldSrcScale(byte *dst, uint dstPitch, SourceType type) {
+	virtual void enableSource(bool enable) {
 		// Should not be called unless overriden
 		assert(0);
 	}
@@ -147,9 +137,14 @@ class SourceScaler : public ScalerPluginObject {
 public:
 
 	SourceScaler();
+	virtual ~SourceScaler();
+
+	virtual void scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                   uint32 dstPitch, int width, int height, int x, int y);
+
+	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding);
 
-	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding, SourceType type);
-	virtual void oldSrcScale(byte *dst, uint dstPitch, SourceType type);
+	virtual void enableSource(bool enable) { _enable = enable; }
 
 protected:
 
@@ -157,19 +152,20 @@ protected:
 	 * Scalers must implement this function. It will be called by oldSrcScale.
 	 * If by comparing the src and oldsrc images it is discovered that no change
 	 * is necessary, do not write a pixel.
+	 *
+	 * If oldSrcPtr is NULL, do not read from it. Scale every pixel.
 	 */
 	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 	                         uint8 *dstPtr, uint32 dstPitch,
 	                         const uint8 *oldSrcPtr, uint32 oldSrcPitch,
 	                         int width, int height) = 0;
 
-	int widths[SRC_MAX];
-	int heights[SRC_MAX];
-	uint paddings[SRC_MAX];
-	uint pitches[SRC_MAX];
 
-	const byte *newSrcs[SRC_MAX];
-	byte *oldSrcs[SRC_MAX];
+private:
+
+	int _padding;
+	bool _enable;
+	byte *_oldSrc;
 };
 
 typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;


Commit: 4c4028cf1db34591f70ec54842f98ce6426c7e36
    https://github.com/scummvm/scummvm/commit/4c4028cf1db34591f70ec54842f98ce6426c7e36
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Various formatting fixes

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 047fa50afc..51f75f55f4 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -1771,7 +1771,7 @@ int EdgePlugin::fixKnights(int sub_type, Pixel *pixels, int8 *sim) {
 #define greenMask   0x07E0
 
 template<typename ColorMask, typename Pixel>
-void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
+void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
         Pixel *pixels, int sub_type, int16 *bptr) {
 	Pixel *dptr2;
 	int16 tmp_grey;
@@ -2370,7 +2370,7 @@ void EdgePlugin::anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
 
 
 template<typename ColorMask, typename Pixel>
-void EdgePlugin::anti_alias_grid_2x(uint8 *dptr, int dstPitch,
+void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
                                     Pixel *pixels, int sub_type, int16 *bptr,
                                     int8 *sim,
                                     int interpolate_2x) {
@@ -3363,7 +3363,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 			/* block of solid color */
 			if (!diffs) {
-				anti_alias_grid_clean_3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+				antiAliasGridClean3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 				                                    0, NULL);
 				continue;
 			}
@@ -3377,7 +3377,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
-			anti_alias_grid_clean_3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+			antiAliasGridClean3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                                    sub_type, bplane);
 		}
 	}
@@ -3443,7 +3443,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 
 			/* block of solid color */
 			if (!diffs) {
-				anti_alias_grid_2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+				antiAliasGrid2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 				                              0, NULL, NULL, 0);
 				continue;
 			}
@@ -3457,7 +3457,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
-			anti_alias_grid_2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+			antiAliasGrid2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
 			                              sub_type, bplane, sim,
 			                              interpolate_2x);
 		}
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 84193c7aac..20d76e5fb6 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -70,7 +70,7 @@ private:
 	 * bitplanes.  The increase in image quality is well worth the speed hit.
 	 */
 	template<typename ColorMask, typename Pixel>
-	int16* chooseGreyscale(Pixel *pixels);
+	int16 *chooseGreyscale(Pixel *pixels);
 
 	/**
 	 * Calculate the distance between pixels in RGB space.  Greyscale isn't
@@ -131,7 +131,7 @@ private:
 	 * Fill pixel grid with or without interpolation, using the detected edge
 	 */
 	template<typename ColorMask, typename Pixel>
-	void anti_alias_grid_2x(uint8 *dptr, int dstPitch,
+	void antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		Pixel *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
 		int interpolate_2x);
@@ -140,7 +140,7 @@ private:
 	 * Fill pixel grid without interpolation, using the detected edge
 	 */
 	template<typename ColorMask, typename Pixel>
-	void anti_alias_grid_clean_3x(uint8 *dptr, int dstPitch,
+	void antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		Pixel *pixels, int sub_type, int16 *bptr);
 
 	/**
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index fe9dd7aff4..378b34563f 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -45,17 +45,17 @@ void SourceScaler::scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 	if (!_enable) {
 		// Do not pass _oldSrc, do not update _oldSrc
 		internScale(srcPtr, srcPitch,
-					dstPtr, dstPitch,
-					NULL, 0,
-					width, height);
+		            dstPtr, dstPitch,
+		            NULL, 0,
+		            width, height);
 		return;
 	}
 	int offset = (_padding + x) * _format.bytesPerPixel + (_padding + y) * srcPitch;
 	// Call user defined scale function
 	internScale(srcPtr, srcPitch,
-				dstPtr, dstPitch,
-				_oldSrc + offset, srcPitch,
-				width, height);
+	            dstPtr, dstPitch,
+	            _oldSrc + offset, srcPitch,
+	            width, height);
 	// Update old src
 	byte *oldSrc = _oldSrc + offset;
 	while (height--) {
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index f6cbe6fc58..1f557a02d2 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -29,7 +29,7 @@ class ScalerPluginObject : public PluginObject {
 public:
 
 	virtual ~ScalerPluginObject() {}
-	
+
 	/**
 	 * This function will be called before any scaler is used.
 	 * Precomputed data should be generated here.
@@ -61,7 +61,7 @@ public:
 
 	virtual uint getFactor() const = 0;
 
-	virtual const Common::Array<uint>& getFactors() const { return _factors; }
+	virtual const Common::Array<uint> &getFactors() const { return _factors; }
 
 	/**
 	 * Set the scaling factor.


Commit: 3e9bd387e7b104b80164d6626dde928d2d66440e
    https://github.com/scummvm/scummvm/commit/3e9bd387e7b104b80164d6626dde928d2d66440e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Clean up Edge plugin source

Add static keyword to some functions.

Rename functions to camelCase style.

Changed paths:
    graphics/scaler/edge.cpp


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 51f75f55f4..d8453e56af 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -123,8 +123,8 @@
 #define GREY_SHIFT 12           /* bit shift for greyscale precision */
 #define RGB_SHIFT 13            /* bit shift for RGB precision */
 
-const int16 one_sqrt2 = (int16)(((int16)1 << GREY_SHIFT) / sqrt(2.0) + 0.5);
-const int16 int32_sqrt3 = (int16)(((int16)1 << GREY_SHIFT) * sqrt(3.0) + 0.5);
+static const int16 one_sqrt2 = (int16)(((int16)1 << GREY_SHIFT) / sqrt(2.0) + 0.5);
+static const int16 int32_sqrt3 = (int16)(((int16)1 << GREY_SHIFT) * sqrt(3.0) + 0.5);
 
 
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
@@ -159,7 +159,7 @@ const int16 int32_sqrt3 = (int16)(((int16)1 << GREY_SHIFT) * sqrt(3.0) + 0.5);
  * the simple purposes we require here, so I've not included it :)
  */
 
-uint32 seed0, seed1, seed2, seed3;
+static uint32 seed0, seed1, seed2, seed3;
 
 /* period 2^32 - 1 */
 /* fails Gorilla test, binary rank matrix */
@@ -177,7 +177,7 @@ uint32 seed0, seed1, seed2, seed3;
  *
  * all others, including the "favorite" (13, 17, 5), fail some Monkey tests
  */
-uint32 xorshift_32(void) {
+static uint32 xorshift32(void) {
 	seed0 ^= seed0 << 6;
 	seed0 ^= seed0 << 5;
 	seed0 ^= seed0 >> 13;
@@ -187,7 +187,7 @@ uint32 xorshift_32(void) {
 
 /* period 2^128 - 1 */
 /* None of the other published 2^128-1 xorshift RNGs passed OPERM5 */
-uint32 xorshift_128(void) {
+static uint32 xorshift128(void) {
 	uint32 temp;
 
 	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
@@ -201,7 +201,7 @@ uint32 xorshift_128(void) {
 }
 
 /* return a random fraction over the range [0, 1) */
-double dxorshift_128(void) {
+static double dxorshift128(void) {
 	uint32 temp;
 
 	temp = (seed0 ^ (seed0 << 20)) ^ (seed1 ^ (seed1 >> 11)) ^
@@ -214,15 +214,15 @@ double dxorshift_128(void) {
 	return (temp / 4294967296.0);
 }
 
-void initialize_xorshift_128(uint32 seed) {
-	/* seed0 needs to be initialized prior to calling xorshift_32() */
+static void initializeXorshift128(uint32 seed) {
+	/* seed0 needs to be initialized prior to calling xorshift32() */
 	seed0 = seed;
 
-	/* initialize with xorshift_32() */
-	seed0 = xorshift_32();
-	seed1 = xorshift_32();
-	seed2 = xorshift_32();
-	seed3 = xorshift_32();
+	/* initialize with xorshift32() */
+	seed0 = xorshift32();
+	seed1 = xorshift32();
+	seed2 = xorshift32();
+	seed3 = xorshift32();
 }
 #endif
 
@@ -262,7 +262,7 @@ void initialize_xorshift_128(uint32 seed) {
  *          J = 0.6784840295980, K = 0.0859554204018
  *
  */
-double fast_atan(double x0) {
+static double fastAtan(double x0) {
 	double x2;
 	double x;
 
@@ -646,7 +646,7 @@ int EdgePlugin::findPrincipleAxis(int16 *diffs, int16 *bplane,
 
 	/* calculate angle in degrees * 100 */
 	if (x) {
-		angle = (int32) floor(5729.577951307 * fast_atan(ratio) + 0.5);
+		angle = (int32) floor(5729.577951307 * fastAtan(ratio) + 0.5);
 		if (x < 0.0) angle += 18000;
 	} else {
 		if (y > 0.0) angle = 9000;
@@ -2335,7 +2335,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		*dptr2++ = center;
 		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-		*dptr2 = center ^ (Pixel)(dxorshift_128() * (1L << 16));
+		*dptr2 = center ^ (Pixel)(dxorshift128() * (1L << 16));
 #else
 		*dptr2 = center;
 #endif
@@ -2358,7 +2358,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 	*dptr2++ = *ptmp++;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift_128() * (1L << 16));
+	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
@@ -3204,7 +3204,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		dptr2 = (Pixel *) dptr;
 		*dptr2++ = center;
 #if DEBUG_REFRESH_RANDOM_XOR
-		*dptr2 = center ^ (Pixel)(dxorshift_128() * (1L << 16));
+		*dptr2 = center ^ (Pixel)(dxorshift128() * (1L << 16));
 #else
 		*dptr2 = center;
 #endif
@@ -3221,7 +3221,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 	dptr2 = (Pixel *) dptr;
 	*dptr2++ = *ptmp++;
 #if DEBUG_REFRESH_RANDOM_XOR
-	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift_128() * (1L << 16));
+	*dptr2 = *ptmp++ ^ (Pixel)(dxorshift128() * (1L << 16));
 #else
 	*dptr2 = *ptmp++;
 #endif
@@ -3233,7 +3233,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 
 /* Check for changed pixel grid, return 1 if unchanged. */
 template<typename Pixel>
-int check_unchanged_pixels(const Pixel *old_src_ptr, const Pixel *pixels, int w) {
+int checkUnchangedPixels(const Pixel *old_src_ptr, const Pixel *pixels, int w) {
 	const Pixel *dptr;
 
 	dptr = old_src_ptr - w - 1;
@@ -3348,7 +3348,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 #if DEBUG_DRAW_REFRESH_BORDERS
 						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-						check_unchanged_pixels(oldSptr, pixels, oldPitch / sizeof(Pixel))) {
+						checkUnchangedPixels(oldSptr, pixels, oldPitch / sizeof(Pixel))) {
 					//draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
 					//					   old_dst_inc);
 
@@ -3428,7 +3428,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 #if DEBUG_DRAW_REFRESH_BORDERS
 						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
-						check_unchanged_pixels<Pixel>(oldSptr, pixels, oldSrcPitch / sizeof(Pixel))) {
+						checkUnchangedPixels<Pixel>(oldSptr, pixels, oldSrcPitch / sizeof(Pixel))) {
 					//draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
 					//					   old_dst_inc);
 
@@ -3476,7 +3476,7 @@ void EdgePlugin::initTables(const uint8 *srcPtr, uint32 srcPitch,
 
 #if DEBUG_REFRESH_RANDOM_XOR
 	/* seed the random number generator, we don't care if the seed is random */
-	initialize_xorshift_128(42);
+	initializeXorshift128(42);
 #endif
 
 	/* initialize greyscale table */


Commit: a41cd52bbfb839b8879f8c34803cd8cfd929dff0
    https://github.com/scummvm/scummvm/commit/a41cd52bbfb839b8879f8c34803cd8cfd929dff0
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Use a simple blit function instead of normal scaler plugin

For places where a blit is needed and no scaling is required, this
function is used instead.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 2f4bd868af..ef4c907620 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1145,6 +1145,19 @@ void SurfaceSdlGraphicsManager::updateScreen() {
 	internUpdateScreen();
 }
 
+/**
+ * Simple blit function. Copies a rectangle from one surface to another.
+ *
+ * @param byteWidth   The width of the region to copy in bytes.
+ */
+static void blitSurface(byte * srcPtr, int srcPitch, byte * dstPtr, int dstPitch, int byteWidth, int height) {
+	while (height--) {
+		memcpy(dstPtr, srcPtr, byteWidth);
+		dstPtr += dstPitch;
+		srcPtr += srcPitch;
+	}
+}
+
 void SurfaceSdlGraphicsManager::internUpdateScreen() {
 	SDL_Surface *srcSurf, *origSurf;
 	int height, width;
@@ -1287,13 +1300,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					dst_y = real2Aspect(dst_y);
 
 				if (_overlayVisible) {
-					// Use the Normal plugin so the overlay is not scaled/filtered
-					uint tmpFactor = (*_normalPlugin)->getFactor();
-					(*_normalPlugin)->setFactor(1);
-					(*_normalPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-						(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
-					// Revert state in case the normal plugin is used elsewhere
-					(*_normalPlugin)->setFactor(tmpFactor);
+					blitSurface((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+					            (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w * 2, dst_h);
 				} else {
 					if (_useOldSrc) {
 						// scale into _destbuffer instead of _hwscreen to avoid AR problems
@@ -2177,13 +2185,10 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 			(*_normalPlugin)->setFactor(tmpFactor);
 		}
 	} else {
-        int tmpFactor = (*_normalPlugin)->getFactor();
-        (*_normalPlugin)->setFactor(1);
-        (*_normalPlugin)->scale(
+        blitSurface(
                 (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
                 _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-                _mouseCurState.w, _mouseCurState.h, 0, 0);
-        (*_normalPlugin)->setFactor(tmpFactor);
+			_mouseCurState.w * 2, _mouseCurState.h);
 	}
 #endif
 


Commit: 853d5452847766332a5401e168b82984405a0b62
    https://github.com/scummvm/scummvm/commit/853d5452847766332a5401e168b82984405a0b62
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Make sure normal scaler plugin is initialized

If a game and graphics mode were chosen from the command line, the normal
plugin might not have been loaded and it would crash upon being used to
scale the cursor. Now the normal plugin is initialized with the first
plugin to be initialized.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index ef4c907620..2ea67a7324 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -671,13 +671,19 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 	if (!_screen || !_hwscreen)
 		return;
 
+
 	// If the _scalerIndex has changed, change scaler plugins
 	if (_scalerPlugins[_scalerIndex] != _scalerPlugin) {
-		if (_scalerPlugin)
-			(*_scalerPlugin)->deinitialize();
-		_scalerPlugin = _scalerPlugins[_scalerIndex];
 		Graphics::PixelFormat format;
 		convertSDLPixelFormat(_hwscreen->format, &format);
+		if (_scalerPlugin) {
+			(*_scalerPlugin)->deinitialize();
+		} else if (_scalerPlugins[_scalerIndex] != _normalPlugin) {
+			// This is the first time plugins have been initialized
+			// _normalPlugin might be needed and needs to be initialized
+			(*_normalPlugin)->initialize(format);
+		}
+		_scalerPlugin = _scalerPlugins[_scalerIndex];
 		(*_scalerPlugin)->initialize(format);
 		_extraPixels = (*_scalerPlugin)->extraPixels();
 		_useOldSrc = (*_scalerPlugin)->useOldSource();


Commit: ab8dd417a955f125e5475d46da941dbd5015d078
    https://github.com/scummvm/scummvm/commit/ab8dd417a955f125e5475d46da941dbd5015d078
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Use enableSource() when scaling to the overlay

This fixes an issue where the screen would have little or no changes
and nothing would be drawn to the overlay.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 2ea67a7324..2a7177db76 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1807,9 +1807,17 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 
 	SDL_LockSurface(_tmpscreen);
 	SDL_LockSurface(_overlayscreen);
+
+	// The plugin won't write anything if _useOldSrc
+	if (_useOldSrc)
+		(*_scalerPlugin)->enableSource(false);
+
 	(*_scalerPlugin)->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
+	if (_useOldSrc)
+		(*_scalerPlugin)->enableSource(true);
+
 #ifdef USE_SCALERS
 	if (_videoMode.aspectRatioCorrection)
 		stretch200To240((uint8 *)_overlayscreen->pixels, _overlayscreen->pitch,


Commit: cbf9072e45ffa01ec36ec878e169dfafca52865e
    https://github.com/scummvm/scummvm/commit/cbf9072e45ffa01ec36ec878e169dfafca52865e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix leaks related to graphics modes

supportedGraphicsModes() now returns to OSystem_SDL a pointer to a deep copy of
surfacesdl's internal graphics mode array. Therefore each can free its
own copy without interfering with the other's.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/platform/sdl/sdl.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 2a7177db76..7807d16528 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -77,6 +77,8 @@ const OSystem::GraphicsMode s_supportedStretchModes[] = {
 };
 #endif
 
+static void initGraphicsModes();
+
 DECLARE_TRANSLATION_ADDITIONAL_CONTEXT("Normal (no scaling)", "lowres")
 
 AspectRatio::AspectRatio(int w, int h) {
@@ -170,6 +172,9 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	_videoMode.stretchMode = STRETCH_FIT;
 #endif
+
+	if (!s_supportedGraphicsModes)
+		initGraphicsModes();
 }
 
 SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
@@ -186,6 +191,16 @@ SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
 	free(_currentPalette);
 	free(_cursorPalette);
 	delete[] _mouseData;
+
+	for (uint i = 0; i < s_supportedGraphicsModes->size() - 1; ++i) {
+		OSystem::GraphicsMode &gm = (*s_supportedGraphicsModes)[i];
+		free(const_cast<char *>(gm.name));
+		free(const_cast<char *>(gm.description));
+	}
+	delete s_supportedGraphicsModes;
+	delete s_supportedGraphicsModesData;
+	s_supportedGraphicsModes = NULL;
+	s_supportedGraphicsModesData = NULL;
 }
 
 bool SurfaceSdlGraphicsManager::hasFeature(OSystem::Feature f) const {
@@ -259,12 +274,8 @@ void static initGraphicsModes () {
 		for (uint j = 0; j < factors.size(); ++j) {
 			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
 			Common::String n2 = Common::String::format("%s%dx", prettyName, factors[j]);
-			char *s1 = new char[n1.size()+1];
-			char *s2 = new char[n2.size()+1];
-			strcpy(s1, n1.c_str());
-			strcpy(s2, n2.c_str());
-			gm.name = s1;
-			gm.description = s2;
+			gm.name = strdup(n1.c_str());
+			gm.description = strdup(n2.c_str());
 			gm.id = s_supportedGraphicsModes->size();
 			s_supportedGraphicsModes->push_back(gm);
 			gmd.scaleFactor = factors[j];
@@ -280,7 +291,20 @@ void static initGraphicsModes () {
 const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() {
 	if (!s_supportedGraphicsModes)
 		initGraphicsModes();
-	return &(*s_supportedGraphicsModes)[0];
+
+	int size = s_supportedGraphicsModes->size();
+	OSystem::GraphicsMode *modes = new OSystem::GraphicsMode[size];
+	memcpy(modes, &(*s_supportedGraphicsModes)[0], size * sizeof(OSystem::GraphicsMode));
+
+	// Do deep copy. Each can be freed independently of the other.
+	OSystem::GraphicsMode *gm = modes;
+	while (gm->name) {
+		gm->name = strdup(gm->name);
+		gm->description = strdup(gm->description);
+		++gm;
+	}
+
+	return modes;
 }
 
 const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index 40d65440c5..a106e5c219 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -153,6 +153,15 @@ OSystem_SDL::~OSystem_SDL() {
 	delete _mutexManager;
 	_mutexManager = 0;
 
+#ifdef USE_OPENGL
+	for (int i = 0; i < _sdlModesCount; ++i) {
+		// SurfaceSDL needs us to free these
+		free(const_cast<char *>(_graphicsModes[i].name));
+		free(const_cast<char *>(_graphicsModes[i].description));
+	}
+	delete[] _graphicsModes;
+#endif
+
 	delete _logger;
 	_logger = 0;
 
@@ -939,6 +948,9 @@ void OSystem_SDL::setupGraphicsModes() {
 		mode->id = i++;
 		mode++;
 	}
+
+	// SurfaceSDLGraphicsManager expects us to delete[] this
+	delete[] sdlGraphicsModes;
 }
 #endif
 


Commit: ee008a44b1796ac8ed7e172aecb1ab6f634c329c
    https://github.com/scummvm/scummvm/commit/ee008a44b1796ac8ed7e172aecb1ab6f634c329c
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Formatting fix

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 7807d16528..8fcab9a17b 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -260,7 +260,7 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 	}
 }
 
-void static initGraphicsModes () {
+static void initGraphicsModes () {
 	s_supportedGraphicsModes = new Common::Array<OSystem::GraphicsMode>;
 	s_supportedGraphicsModesData = new Common::Array<GraphicsModeData>;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();


Commit: f9569ea2ff5d164795bca244d149858ab5bdbcf6
    https://github.com/scummvm/scummvm/commit/f9569ea2ff5d164795bca244d149858ab5bdbcf6
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix formatting

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 8fcab9a17b..529ae618a2 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -260,7 +260,7 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 	}
 }
 
-static void initGraphicsModes () {
+static void initGraphicsModes() {
 	s_supportedGraphicsModes = new Common::Array<OSystem::GraphicsMode>;
 	s_supportedGraphicsModesData = new Common::Array<GraphicsModeData>;
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();


Commit: f172718b795f1216f6108bd13552e7f1fa424ff5
    https://github.com/scummvm/scummvm/commit/f172718b795f1216f6108bd13552e7f1fa424ff5
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
CONFIGURE: Added configure option for Edge2x-3x scaler plugin

Changed paths:
    base/plugins.cpp
    configure
    graphics/module.mk


diff --git a/base/plugins.cpp b/base/plugins.cpp
index 99d672ba20..c74de42b62 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -166,6 +166,8 @@ public:
 #ifdef USE_SCALERS
 #ifdef USE_HQ_SCALERS
 		LINK_PLUGIN(HQ)
+#endif
+#ifdef USE_EDGE_SCALERS
 		LINK_PLUGIN(EDGE)
 #endif
 		LINK_PLUGIN(ADVMAME)
diff --git a/configure b/configure
index ffc3febd65..46ce9c730a 100755
--- a/configure
+++ b/configure
@@ -187,6 +187,7 @@ _mt32emu=yes
 _lua=yes
 _build_scalers=yes
 _build_hq_scalers=yes
+_build_edge_scalers=yes
 _enable_prof=no
 _enable_asan=no
 _enable_tsan=no
@@ -1057,7 +1058,8 @@ Optional Features:
   --disable-highres        don't enable support for high resolution engines >320x240
   --disable-savegame-timestamp don't use timestamps for blank savegame descriptions
   --disable-scalers        exclude scalers
-  --disable-hq-scalers     exclude HQ2x and HQ3x scalers
+  --disable-hq-scalers     exclude HQ2x and HQ3x scalers (disables Edge scalers as well)
+  --disable-edge-scalers   exclude Edge2x and Edge3x scalers
   --disable-translation    don't build support for translated messages
   --disable-taskbar        don't build support for taskbar and launcher integration
   --disable-cloud          don't build cloud support
@@ -1252,6 +1254,7 @@ for ac_option in $@; do
 	--disable-savegame-timestamp) _savegame_timestamp=no ;;
 	--disable-scalers)            _build_scalers=no      ;;
 	--disable-hq-scalers)         _build_hq_scalers=no   ;;
+	--disable-edge-scalers)       _build_edge_scalers=no ;;
 	--enable-alsa)                _alsa=yes              ;;
 	--disable-alsa)               _alsa=no               ;;
 	--enable-seq-midi)            _seq_midi=yes          ;;
@@ -3598,6 +3601,7 @@ if test -n "$_host"; then
 			append_var ASFLAGS "-mfloat-abi=softfp"
 			_backend="openpandora"
 			_build_hq_scalers=yes
+			_build_edge_scalers=no
 			_vkeybd=no
 			_mt32emu=no
 			_seq_midi=no
@@ -4341,15 +4345,21 @@ define_in_config_if_yes "$_highres" 'USE_HIGHRES'
 define_in_config_if_yes "$_savegame_timestamp" 'USE_SAVEGAME_TIMESTAMP'
 
 #
-# Check whether to enable the (hq) scalers
+# Check whether to enable the (hq (and edge)) scalers
 #
 if test "$_build_scalers" = no ; then
 	_build_hq_scalers=no
+	_build_edge_scalers=no
+elif test "$_build_hq_scalers" = no ; then
+	_build_edge_scalers=no
 fi
+
 define_in_config_if_yes "$_build_scalers" 'USE_SCALERS'
 
 define_in_config_if_yes "$_build_hq_scalers" 'USE_HQ_SCALERS'
 
+define_in_config_if_yes "$_build_edge_scalers" 'USE_EDGE_SCALERS'
+
 #
 # Check for math lib
 #
@@ -5985,7 +5995,11 @@ fi
 
 if test "$_build_scalers" = yes ; then
 	if test "$_build_hq_scalers" = yes ; then
-		echo_n ", HQ scalers"
+		if test "$_build_edge_scalers" = yes ; then
+			echo_n ", HQ and Edge scalers"
+		else
+			echo_n ", HQ scalers"
+		fi
 	else
 		echo_n ", scalers"
 	fi
diff --git a/graphics/module.mk b/graphics/module.mk
index a6f9008d94..6bdaf92dac 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -104,8 +104,7 @@ endif
 
 ifdef USE_HQ_SCALERS
 MODULE_OBJS += \
-	scaler/edge.o \
-	scaler/hq.o \
+	scaler/hq.o
 
 ifdef USE_NASM
 MODULE_OBJS += \
@@ -115,6 +114,11 @@ endif
 
 endif
 
+ifdef USE_EDGE_SCALERS
+MODULE_OBJS += \
+	scaler/edge.o
+endif
+
 endif
 
 # Include common rules


Commit: 88363d5d361f04f47f7028bc7401029682e59095
    https://github.com/scummvm/scummvm/commit/88363d5d361f04f47f7028bc7401029682e59095
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix error when USE_SCALERS is undefined

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 529ae618a2..e0716b3c83 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -312,7 +312,7 @@ const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsMode
 }
 
 int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
-	for (uint i = 0; i < s_supportedGraphicsModes->size(); ++i) {
+	for (uint i = 0; i < s_supportedGraphicsModes->size() - 1; ++i) {
 		// if normal2x exists, it is the default
 		if (strcmp((*s_supportedGraphicsModes)[i].name, "normal2x") == 0)
 			return (*s_supportedGraphicsModes)[i].id;


Commit: e35a7c72c081f5e5696db7066322f817d4e34df6
    https://github.com/scummvm/scummvm/commit/e35a7c72c081f5e5696db7066322f817d4e34df6
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Cache default graphics mode upon initialization.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index e0716b3c83..8a7875666f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -65,6 +65,7 @@ struct GraphicsModeData {
 
 static Common::Array<OSystem::GraphicsMode> *s_supportedGraphicsModes = NULL;
 static Common::Array<GraphicsModeData> *s_supportedGraphicsModesData = NULL;
+static int s_defaultGraphicsMode;
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 const OSystem::GraphicsMode s_supportedStretchModes[] = {
@@ -266,6 +267,8 @@ static void initGraphicsModes() {
 	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
 	OSystem::GraphicsMode gm;
 	GraphicsModeData gmd;
+	// 0 should be the normal1x mode
+	s_defaultGraphicsMode = 0;
 	for (uint i = 0; i < plugins.size(); ++i) {
 		const Common::Array<uint> &factors = (*plugins[i])->getFactors();
 		const char *name = (*plugins[i])->getName();
@@ -277,6 +280,11 @@ static void initGraphicsModes() {
 			gm.name = strdup(n1.c_str());
 			gm.description = strdup(n2.c_str());
 			gm.id = s_supportedGraphicsModes->size();
+
+			// if normal2x exists, it is the default
+			if (strcmp(gm.name, "normal2x") == 0)
+				s_defaultGraphicsMode = gm.id;
+
 			s_supportedGraphicsModes->push_back(gm);
 			gmd.scaleFactor = factors[j];
 			s_supportedGraphicsModesData->push_back(gmd);
@@ -312,13 +320,7 @@ const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsMode
 }
 
 int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
-	for (uint i = 0; i < s_supportedGraphicsModes->size() - 1; ++i) {
-		// if normal2x exists, it is the default
-		if (strcmp((*s_supportedGraphicsModes)[i].name, "normal2x") == 0)
-			return (*s_supportedGraphicsModes)[i].id;
-	}
-	// 0 should be the normal1x mode
-	return 0;
+	return s_defaultGraphicsMode;
 }
 
 void SurfaceSdlGraphicsManager::beginGFXTransaction() {


Commit: c7121fae6549266f8ca9911cbfa6e73c8c86a2d3
    https://github.com/scummvm/scummvm/commit/c7121fae6549266f8ca9911cbfa6e73c8c86a2d3
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix formatting for pointers.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 8a7875666f..f132155829 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1182,7 +1182,7 @@ void SurfaceSdlGraphicsManager::updateScreen() {
  *
  * @param byteWidth   The width of the region to copy in bytes.
  */
-static void blitSurface(byte * srcPtr, int srcPitch, byte * dstPtr, int dstPitch, int byteWidth, int height) {
+static void blitSurface(byte *srcPtr, int srcPitch, byte *dstPtr, int dstPitch, int byteWidth, int height) {
 	while (height--) {
 		memcpy(dstPtr, srcPtr, byteWidth);
 		dstPtr += dstPitch;


Commit: d8240fa73406b55b2094691a3fb2154141e49933
    https://github.com/scummvm/scummvm/commit/d8240fa73406b55b2094691a3fb2154141e49933
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Initialize plugins when formats change.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index f132155829..648068b21f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -699,13 +699,13 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 
 
 	// If the _scalerIndex has changed, change scaler plugins
-	if (_scalerPlugins[_scalerIndex] != _scalerPlugin) {
+	if (_scalerPlugins[_scalerIndex] != _scalerPlugin || _transactionDetails.formatChanged) {
 		Graphics::PixelFormat format;
 		convertSDLPixelFormat(_hwscreen->format, &format);
-		if (_scalerPlugin) {
+		if (_scalerPlugin)
 			(*_scalerPlugin)->deinitialize();
-		} else if (_scalerPlugins[_scalerIndex] != _normalPlugin) {
-			// This is the first time plugins have been initialized
+
+		if (_scalerPlugins[_scalerIndex] != _normalPlugin) {
 			// _normalPlugin might be needed and needs to be initialized
 			(*_normalPlugin)->initialize(format);
 		}


Commit: b80c7908fa6f48fdba549374cc9f3e149dd0b46e
    https://github.com/scummvm/scummvm/commit/b80c7908fa6f48fdba549374cc9f3e149dd0b46e
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Update comments for scaling mouse cursor

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 648068b21f..442f7a9d7f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1994,7 +1994,7 @@ void SurfaceSdlGraphicsManager::setMouseCursor(const void *buf, uint w, uint h,
 		} else {
 			assert(!_mouseOrigSurface);
 
-			// Allocate bigger surface because AdvMame2x adds black pixel at [0,0]
+            // Allocate bigger surface because scalers will read past the boudaries.
 			_mouseOrigSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
 							_mouseCurState.w + _maxExtraPixels * 2,
 							_mouseCurState.h + _maxExtraPixels * 2,
@@ -2148,7 +2148,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 		}
 	}
 
-	// Draw from [1,1] since AdvMame2x adds artefact at 0,0
+	// Draw from [_maxExtraPixels,_maxExtraPixels] since scalers will read past boudaries
 	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel;
 
 	SDL_Color *palette;


Commit: 4674c7c7b03eece5716ce6d398da9317b381f9fd
    https://github.com/scummvm/scummvm/commit/4674c7c7b03eece5716ce6d398da9317b381f9fd
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Check bytesPerPixel when USE_SCALERS is undefined

Changed paths:
    graphics/scaler/normal.cpp


diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 4331c6918a..0464f31dcd 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -230,7 +230,11 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 		}
 	}
 #else
-	Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	if (_format.bytesPerPixel == 2) {
+		Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	} else {
+		Normal1x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+	}
 #endif
 }
 


Commit: 39608f391ccebd85f369004e377b804cfc62aaaa
    https://github.com/scummvm/scummvm/commit/39608f391ccebd85f369004e377b804cfc62aaaa
Author: Eric Culp (eculperic at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL,GRAPHICS: Add option for AR correction without scalers

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    configure
    graphics/module.mk


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 442f7a9d7f..325d9d4bb4 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -89,7 +89,7 @@ AspectRatio::AspectRatio(int w, int h) {
 	_kh = h;
 }
 
-#if !defined(__SYMBIAN32__) && defined(USE_SCALERS)
+#if !defined(__SYMBIAN32__) && defined(USE_ASPECT)
 static AspectRatio getDesiredAspectRatio() {
 	const size_t AR_COUNT = 4;
 	const char *desiredAspectRatioAsStrings[AR_COUNT] = {	"auto",				"4/3",				"16/9",				"16/10" };
@@ -152,15 +152,22 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 		_enableFocusRectDebugCode = ConfMan.getBool("use_sdl_debug_focusrect");
 #endif
 
-#if !defined(__SYMBIAN32__) && defined(USE_SCALERS)
+#if !defined(__SYMBIAN32__)
+
+#if defined(USE_SCALERS)
 	_videoMode.mode = GFX_DOUBLESIZE;
 	_videoMode.scaleFactor = 2;
-	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
-	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 #else // for small screen platforms
 	_videoMode.mode = GFX_NORMAL;
 	_videoMode.scaleFactor = 1;
+#endif
+#if defined(USE_ASPECT)
+	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
+	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
+#else // for small screen platforms
 	_videoMode.aspectRatioCorrection = false;
+#endif
+
 #endif
 	// HACK: just pick first scaler plugin
 	_normalPlugin = _scalerPlugins.front();
@@ -250,6 +257,7 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 	switch (f) {
 	case OSystem::kFeatureFullscreenMode:
 		return _videoMode.fullscreen;
+#ifdef USE_ASPECT
 	case OSystem::kFeatureAspectRatioCorrection:
 		return _videoMode.aspectRatioCorrection;
 	case OSystem::kFeatureFilteringMode:
@@ -1364,6 +1372,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 					src += _destbuffer->pitch;
 				}
 			}
+#endif
+
+#ifdef USE_ASPECT
 			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
 				if (_useOldSrc)
 					r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
@@ -1683,9 +1694,10 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 		h = height - y;
 	}
 
-#ifdef USE_SCALERS
-	if (_videoMode.aspectRatioCorrection && !_overlayVisible && !realCoordinates)
+#ifdef USE_ASPECT
+	if (_videoMode.aspectRatioCorrection && !_overlayVisible && !realCoordinates) {
 		makeRectStretchable(x, y, w, h, _videoMode.filtering);
+	}
 #endif
 
 	if (w == width && h == height) {
@@ -1844,7 +1856,7 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	if (_useOldSrc)
 		(*_scalerPlugin)->enableSource(true);
 
-#ifdef USE_SCALERS
+#ifdef USE_ASPECT
 	if (_videoMode.aspectRatioCorrection)
 		stretch200To240((uint8 *)_overlayscreen->pixels, _overlayscreen->pitch,
 						_videoMode.overlayWidth, _videoMode.screenHeight * _videoMode.scaleFactor, 0, 0, 0,
@@ -2232,7 +2244,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	}
 #endif
 
-#ifdef USE_SCALERS
+#ifdef USE_ASPECT
 	if (!_cursorDontScale && _videoMode.aspectRatioCorrection)
 		stretch200To240Nearest((uint8 *)_mouseSurface->pixels, _mouseSurface->pitch, rW, rH1, 0, 0, 0);
 #endif
diff --git a/configure b/configure
index 46ce9c730a..d0e30fc0ca 100755
--- a/configure
+++ b/configure
@@ -188,6 +188,7 @@ _lua=yes
 _build_scalers=yes
 _build_hq_scalers=yes
 _build_edge_scalers=yes
+_build_aspect=yes
 _enable_prof=no
 _enable_asan=no
 _enable_tsan=no
@@ -1060,6 +1061,7 @@ Optional Features:
   --disable-scalers        exclude scalers
   --disable-hq-scalers     exclude HQ2x and HQ3x scalers (disables Edge scalers as well)
   --disable-edge-scalers   exclude Edge2x and Edge3x scalers
+  --disable-aspect         exclude aspect ratio correction
   --disable-translation    don't build support for translated messages
   --disable-taskbar        don't build support for taskbar and launcher integration
   --disable-cloud          don't build cloud support
@@ -1255,6 +1257,7 @@ for ac_option in $@; do
 	--disable-scalers)            _build_scalers=no      ;;
 	--disable-hq-scalers)         _build_hq_scalers=no   ;;
 	--disable-edge-scalers)       _build_edge_scalers=no ;;
+	--disable-aspect)             _build_aspect=no       ;;
 	--enable-alsa)                _alsa=yes              ;;
 	--disable-alsa)               _alsa=no               ;;
 	--enable-seq-midi)            _seq_midi=yes          ;;
@@ -3282,12 +3285,14 @@ if test -n "$_host"; then
 			fi
 			_port_mk="backends/platform/$_backend/android.mk"
 			_build_scalers=no
+			_build_aspect=no
 			_seq_midi=no
 			_timidity=no
 			;;
 		androidsdl | androidsdl-armeabi | androidsdl-armeabi-v7a | androidsdl-mips | androidsdl-x86 | androidsdl-arm64-v8a | androidsdl-x86_64)
 			DEFINES="$DEFINES -DANDROIDSDL"
 			_unix=yes
+			_build_aspect=no
 			_seq_midi=no
 			_timidity=no
 			_backend="androidsdl"
@@ -3409,6 +3414,7 @@ if test -n "$_host"; then
 			append_var CXXFLAGS "-fdelete-null-pointer-checks"
 			_backend="dc"
 			_build_scalers=no
+			_build_aspect=no
 			# These two are needed due to shortcomings of the
 			# detection systems.  Do not remove until auto-
 			# detection works correctly.
@@ -3436,6 +3442,7 @@ if test -n "$_host"; then
 			append_var DEFINES "-DVECTOR_RENDERER_FORMAT=1555"
 			_backend="ds"
 			_build_scalers=no
+			_build_aspect=no
 			_mt32emu=no
 			_nuked_opl=no
 			_tinygl=no
@@ -3448,6 +3455,7 @@ if test -n "$_host"; then
 		gamecube)
 			_backend="wii"
 			_build_scalers=no
+			_build_aspect=no
 			_vkeybd=yes
 			_mt32emu=no
 			_port_mk="backends/platform/wii/wii.mk"
@@ -3510,6 +3518,7 @@ if test -n "$_host"; then
 			append_var ASFLAGS "-arch armv6"
 			_backend="iphone"
 			_build_scalers=no
+			_build_aspect=no
 			_mt32emu=no
 			_seq_midi=no
 			_timidity=no
@@ -3569,6 +3578,7 @@ if test -n "$_host"; then
 			_backend="n64"
 			_mt32emu=no
 			_build_scalers=no
+			_build_aspect=no
 			_savegame_timestamp=no
 			_translation=no
 			_text_console=no
@@ -3647,6 +3657,7 @@ if test -n "$_host"; then
 		psp)
 			_backend="psp"
 			_build_scalers=no
+			_build_aspect=no
 			_mt32emu=no
 			_port_mk="backends/platform/psp/psp.mk"
 			;;
@@ -3673,6 +3684,7 @@ if test -n "$_host"; then
 		wii)
 			_backend="wii"
 			_build_scalers=no
+			_build_aspect=no
 			_vkeybd=yes
 			_port_mk="backends/platform/wii/wii.mk"
 			add_line_to_config_mk 'GAMECUBE = 0'
@@ -4360,6 +4372,8 @@ define_in_config_if_yes "$_build_hq_scalers" 'USE_HQ_SCALERS'
 
 define_in_config_if_yes "$_build_edge_scalers" 'USE_EDGE_SCALERS'
 
+define_in_config_if_yes "$_build_aspect" 'USE_ASPECT'
+
 #
 # Check for math lib
 #
@@ -6005,6 +6019,10 @@ if test "$_build_scalers" = yes ; then
 	fi
 fi
 
+if test "$_build_aspect" = yes; then
+	echo_n ", aspect ratio correction"
+fi
+
 if test "$_mt32emu" = yes ; then
 	echo_n ", MT-32 emulator"
 fi
diff --git a/graphics/module.mk b/graphics/module.mk
index 6bdaf92dac..1baf22ff9d 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -83,12 +83,16 @@ MODULE_OBJS += \
 	tinygl/zdirtyrect.o
 endif
 
+ifdef USE_ASPECT
+MODULE_OBJS += \
+	scaler/aspect.o
+endif
+
 ifdef USE_SCALERS
 MODULE_OBJS += \
 	scaler/dotmatrix.o \
 	scaler/sai.o \
 	scaler/pm.o \
-	scaler/aspect.o \
 	scaler/downscaler.o \
 	scaler/scale2x.o \
 	scaler/scale3x.o \


Commit: f4f9957f85e476ccd7c575c50b62bf1eaef66f2b
    https://github.com/scummvm/scummvm/commit/f4f9957f85e476ccd7c575c50b62bf1eaef66f2b
Author: Martin T. H. Sandsmark (martin.sandsmark at kde.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
scalerplugin: port to new plugin api

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h
    backends/platform/sdl/sdl.cpp
    base/plugins.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 325d9d4bb4..e90cd15fc2 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -170,7 +170,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 
 #endif
 	// HACK: just pick first scaler plugin
-	_normalPlugin = _scalerPlugins.front();
+	_normalPlugin = &_scalerPlugins.front()->get<ScalerPluginObject>();
 	_scalerPlugin = NULL;
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
@@ -260,6 +260,8 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 #ifdef USE_ASPECT
 	case OSystem::kFeatureAspectRatioCorrection:
 		return _videoMode.aspectRatioCorrection;
+#endif
+#if SDL_VERSION_ATLEAST(2, 0, 0)
 	case OSystem::kFeatureFilteringMode:
 		return _videoMode.filtering;
 	case OSystem::kFeatureCursorPalette:
@@ -272,15 +274,16 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 static void initGraphicsModes() {
 	s_supportedGraphicsModes = new Common::Array<OSystem::GraphicsMode>;
 	s_supportedGraphicsModesData = new Common::Array<GraphicsModeData>;
-	const ScalerPlugin::List &plugins = ScalerMan.getPlugins();
+	const PluginList &plugins = ScalerMan.getPlugins();
 	OSystem::GraphicsMode gm;
 	GraphicsModeData gmd;
 	// 0 should be the normal1x mode
 	s_defaultGraphicsMode = 0;
 	for (uint i = 0; i < plugins.size(); ++i) {
-		const Common::Array<uint> &factors = (*plugins[i])->getFactors();
-		const char *name = (*plugins[i])->getName();
-		const char *prettyName = (*plugins[i])->getPrettyName();
+        ScalerPluginObject &plugin = plugins[i]->get<ScalerPluginObject>();
+		const Common::Array<uint> &factors = plugin.getFactors();
+		const char *name = plugin.getName();
+		const char *prettyName = plugin.getPrettyName();
 		gmd.pluginName = name;
 		for (uint j = 0; j < factors.size(); ++j) {
 			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
@@ -304,7 +307,7 @@ static void initGraphicsModes() {
 	s_supportedGraphicsModes->push_back(gm);
 }
 
-const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() {
+const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() const {
 	if (!s_supportedGraphicsModes)
 		initGraphicsModes();
 
@@ -680,7 +683,7 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 
 	// Find which plugin corresponds to the desired mode and set
 	// _scalerIndex accordingly. _scalerPlugin will be updated later.
-	while (strcmp(name, (*_scalerPlugins[_scalerIndex])->getName()) != 0) {
+	while (strcmp(name, _scalerPlugins[_scalerIndex]->get<ScalerPluginObject>().getName()) != 0) {
 		_scalerIndex++;
 		if (_scalerIndex >= _scalerPlugins.size()) {
 			_scalerIndex = 0;
@@ -707,23 +710,23 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 
 
 	// If the _scalerIndex has changed, change scaler plugins
-	if (_scalerPlugins[_scalerIndex] != _scalerPlugin || _transactionDetails.formatChanged) {
+	if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _scalerPlugin || _transactionDetails.formatChanged) {
 		Graphics::PixelFormat format;
 		convertSDLPixelFormat(_hwscreen->format, &format);
 		if (_scalerPlugin)
-			(*_scalerPlugin)->deinitialize();
+			_scalerPlugin->deinitialize();
 
-		if (_scalerPlugins[_scalerIndex] != _normalPlugin) {
+		if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _normalPlugin) {
 			// _normalPlugin might be needed and needs to be initialized
-			(*_normalPlugin)->initialize(format);
+			_normalPlugin->initialize(format);
 		}
-		_scalerPlugin = _scalerPlugins[_scalerIndex];
-		(*_scalerPlugin)->initialize(format);
-		_extraPixels = (*_scalerPlugin)->extraPixels();
-		_useOldSrc = (*_scalerPlugin)->useOldSource();
+		_scalerPlugin = &_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>();
+		_scalerPlugin->initialize(format);
+		_extraPixels = _scalerPlugin->extraPixels();
+		_useOldSrc = _scalerPlugin->useOldSource();
 		if (_useOldSrc) {
-			(*_scalerPlugin)->enableSource(true);
-			(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
+			_scalerPlugin->enableSource(true);
+			_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
 										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
 			if (!_destbuffer) {
 				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
@@ -739,7 +742,7 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		}
 	}
 
-	(*_scalerPlugin)->setFactor(_videoMode.scaleFactor);
+	_scalerPlugin->setFactor(_videoMode.scaleFactor);
 
 	// Blit everything to the screen
 	_forceRedraw = true;
@@ -1021,7 +1024,7 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 
 	if (_useOldSrc) {
 		// Create surface containing previous frame's data to pass to scaler
-		(*_scalerPlugin)->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
+		_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
 									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
 
 		// Create surface containing the raw output from the scaler
@@ -1345,10 +1348,10 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				} else {
 					if (_useOldSrc) {
 						// scale into _destbuffer instead of _hwscreen to avoid AR problems
-						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
 					} else
-						(*_scalerPlugin)->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
 			}
@@ -1848,13 +1851,13 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 
 	// The plugin won't write anything if _useOldSrc
 	if (_useOldSrc)
-		(*_scalerPlugin)->enableSource(false);
+		_scalerPlugin->enableSource(false);
 
-	(*_scalerPlugin)->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
+	_scalerPlugin->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
 	if (_useOldSrc)
-		(*_scalerPlugin)->enableSource(true);
+		_scalerPlugin->enableSource(true);
 
 #ifdef USE_ASPECT
 	if (_videoMode.aspectRatioCorrection)
@@ -2220,21 +2223,21 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	// otherwise use the Normal scaler
 #ifdef USE_SCALERS
 	if (_cursorTargetScale == 1) {
-		if ((*_scalerPlugin)->canDrawCursor()) {
+		if (_scalerPlugin->canDrawCursor()) {
 #endif
-            (*_scalerPlugin)->scale(
+            _scalerPlugin->scale(
                     (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
                     _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
                     _mouseCurState.w, _mouseCurState.h, 0, 0);
 #ifdef USE_SCALERS
 		} else {
-			int tmpFactor = (*_normalPlugin)->getFactor();
-			(*_normalPlugin)->setFactor(_videoMode.scaleFactor);
-			(*_normalPlugin)->scale(
+			int tmpFactor = _normalPlugin->getFactor();
+			_normalPlugin->setFactor(_videoMode.scaleFactor);
+			_normalPlugin->scale(
 				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
 				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 				_mouseCurState.w, _mouseCurState.h, 0, 0);
-			(*_normalPlugin)->setFactor(tmpFactor);
+			_normalPlugin->setFactor(tmpFactor);
 		}
 	} else {
         blitSurface(
@@ -2552,9 +2555,9 @@ void SurfaceSdlGraphicsManager::handleResizeImpl(const int width, const int heig
  * @param factor      The scale factor to match
  * @return            The graphics mode
  */
-int findGraphicsMode(uint factor, ScalerPlugin *plugin) {
+int findGraphicsMode(uint factor, ScalerPluginObject &plugin) {
 	for (uint i = 0; i < s_supportedGraphicsModesData->size(); ++i) {
-		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, (*plugin)->getName()) == 0
+		if (strcmp((*s_supportedGraphicsModesData)[i].pluginName, plugin.getName()) == 0
 				&& (*s_supportedGraphicsModesData)[i].scaleFactor == factor) {
 			return i;
 		}
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 9a4ff45a8f..0a4e5692dd 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -80,6 +80,7 @@ public:
 	virtual void setFeatureState(OSystem::Feature f, bool enable) override;
 	virtual bool getFeatureState(OSystem::Feature f) const override;
 
+	const OSystem::GraphicsMode *supportedGraphicsModes() const;
 	virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
 	virtual int getDefaultGraphicsMode() const override;
 	virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
@@ -330,9 +331,9 @@ protected:
 	// Indicates whether it is needed to free _hwSurface in destructor
 	bool _displayDisabled;
 
-	const ScalerPlugin::List &_scalerPlugins;
-	ScalerPlugin *_scalerPlugin;
-	ScalerPlugin *_normalPlugin;
+	const PluginList &_scalerPlugins;
+	ScalerPluginObject *_scalerPlugin;
+	ScalerPluginObject *_normalPlugin;
 	uint _scalerIndex;
 	uint _maxExtraPixels;
 	uint _extraPixels;
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index a106e5c219..4267c1d38e 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -154,12 +154,11 @@ OSystem_SDL::~OSystem_SDL() {
 	_mutexManager = 0;
 
 #ifdef USE_OPENGL
-	for (int i = 0; i < _sdlModesCount; ++i) {
+	for (uint i = 0; i < _graphicsModes.size(); ++i) {
 		// SurfaceSDL needs us to free these
 		free(const_cast<char *>(_graphicsModes[i].name));
 		free(const_cast<char *>(_graphicsModes[i].description));
 	}
-	delete[] _graphicsModes;
 #endif
 
 	delete _logger;
@@ -273,9 +272,10 @@ void OSystem_SDL::initBackend() {
 		}
 	}
 	// Look in all game domains as well
+#if 0
 	Common::ConfigManager::DomainMap &dm = ConfMan.getGameDomains();
 	for (Common::ConfigManager::DomainMap::iterator domain = dm.begin(); domain != dm.end(); ++domain) {
-		Common::ConfigManager::Domain::iterator gm = domain->_value.find("gfx_mode");
+		Common::ConfigManager::Domain::const_iterator gm = domain->_value.find("gfx_mode");
 		if (gm != domain->_value.end()) {
 			for (uint i = 0; i < ARRAYSIZE(s_legacyGraphicsModes); ++i) {
 				if (gm->_value == s_legacyGraphicsModes[i].oldName) {
@@ -285,6 +285,7 @@ void OSystem_SDL::initBackend() {
 			}
 		}
 	}
+#endif
 
 	if (_graphicsManager == 0) {
 #ifdef USE_OPENGL
@@ -948,9 +949,6 @@ void OSystem_SDL::setupGraphicsModes() {
 		mode->id = i++;
 		mode++;
 	}
-
-	// SurfaceSDLGraphicsManager expects us to delete[] this
-	delete[] sdlGraphicsModes;
 }
 #endif
 
diff --git a/base/plugins.cpp b/base/plugins.cpp
index c74de42b62..e677f57827 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -970,16 +970,16 @@ namespace Common {
 DECLARE_SINGLETON(ScalerManager);
 }
 
-const ScalerPlugin::List &ScalerManager::getPlugins() const {
-	return (const ScalerPlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_SCALER);
+const PluginList &ScalerManager::getPlugins() const {
+	return PluginManager::instance().getPlugins(PLUGIN_TYPE_SCALER);
 }
 
 uint ScalerManager::getMaxExtraPixels() const {
 	uint maxPixels = 0;
-	ScalerPlugin::List plugins = getPlugins();
-	ScalerPlugin::List::iterator i = plugins.begin();
+	PluginList plugins = getPlugins();
+	PluginList::iterator i = plugins.begin();
 	for (; i != plugins.end(); ++i) {
-		uint n = (**i)->extraPixels();
+		uint n = (*i)->get<ScalerPluginObject>().extraPixels();
 		if (n > maxPixels) {
 			maxPixels = n;
 		}
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 1f557a02d2..39cc221b0b 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -168,8 +168,6 @@ private:
 	byte *_oldSrc;
 };
 
-typedef PluginSubclass<ScalerPluginObject> ScalerPlugin;
-
 /**
  * Singleton class to manage scaler plugins
  */
@@ -178,7 +176,7 @@ private:
 	friend class Common::Singleton<SingletonBaseType>;
 
 public:
-	const ScalerPlugin::List &getPlugins() const;
+	const PluginList &getPlugins() const;
 
 	/**
 	 * Queries all scaler plugins for the maximum number of pixels they


Commit: fcff230fe8da1d3e0906073c324217bd3b5a7110
    https://github.com/scummvm/scummvm/commit/fcff230fe8da1d3e0906073c324217bd3b5a7110
Author: Martin T. H. Sandsmark (martin.sandsmark at kde.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
fix build

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index e90cd15fc2..7fd05d33dd 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -598,7 +598,7 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() {
 		// Get our currently set hardware format
 		Graphics::PixelFormat hwFormat = convertSDLPixelFormat(_hwScreen->format);
 
-		_supportedFormats.push_back(hwFormat);
+		_supportedFormats.push_back(format);
 
 #if !SDL_VERSION_ATLEAST(2, 0, 0)
 		format = hwFormat;
@@ -690,7 +690,6 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 		}
 	}
 
-	_transactionDetails.normal1xScaler = (newScaleFactor == 1);
 	if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
 		_transactionDetails.needHotswap = true;
 
@@ -705,14 +704,14 @@ bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {
 void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 	Common::StackLock lock(_graphicsMutex);
 
-	if (!_screen || !_hwscreen)
+	if (!_screen || !_hwScreen)
 		return;
 
 
 	// If the _scalerIndex has changed, change scaler plugins
 	if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _scalerPlugin || _transactionDetails.formatChanged) {
 		Graphics::PixelFormat format;
-		convertSDLPixelFormat(_hwscreen->format, &format);
+		convertSDLPixelFormat(_hwScreen->format, &format);
 		if (_scalerPlugin)
 			_scalerPlugin->deinitialize();
 
@@ -732,10 +731,10 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
 									_videoMode.screenHeight * _videoMode.scaleFactor,
 									16,
-									_hwscreen->format->Rmask,
-									_hwscreen->format->Gmask,
-									_hwscreen->format->Bmask,
-									_hwscreen->format->Amask);
+									_hwScreen->format->Rmask,
+									_hwScreen->format->Gmask,
+									_hwScreen->format->Bmask,
+									_hwScreen->format->Amask);
 				if (_destbuffer == NULL)
 					error("allocating _destbuffer failed");
 			}
@@ -1031,10 +1030,10 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 		_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
 							_videoMode.screenHeight * _videoMode.scaleFactor,
 							16,
-							_hwscreen->format->Rmask,
-							_hwscreen->format->Gmask,
-							_hwscreen->format->Bmask,
-							_hwscreen->format->Amask);
+							_hwScreen->format->Rmask,
+							_hwScreen->format->Gmask,
+							_hwScreen->format->Bmask,
+							_hwScreen->format->Amask);
 		if (_destbuffer == NULL)
 			error("allocating _destbuffer failed");
 	}
@@ -1282,7 +1281,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 	// Force a full redraw if requested.
 	// If _useOldSrc, the scaler will do its own partial updates.
-	if (_forceFull) {
+	if (_forceRedraw) {
 		_numDirtyRects = 1;
 		_dirtyRectList[0].x = 0;
 		_dirtyRectList[0].y = 0;
@@ -1344,15 +1343,15 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 				if (_overlayVisible) {
 					blitSurface((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-					            (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w * 2, dst_h);
+					            (byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w * 2, dst_h);
 				} else {
 					if (_useOldSrc) {
-						// scale into _destbuffer instead of _hwscreen to avoid AR problems
+						// scale into _destbuffer instead of _hwScreen to avoid AR problems
 						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
 							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
 					} else
 						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+							(byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
 			}
 
@@ -1363,11 +1362,11 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 #ifdef USE_SCALERS
 			if (_useOldSrc && !_overlayVisible) {
-				// Copy _destbuffer back into _hwscreen to be AR corrected
+				// Copy _destbuffer back into _hwScreen to be AR corrected
 				int y = orig_dst_y * scale1;
 				int h = r->h;
 				int w = r->w;
-				byte *dest = (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch;
+				byte *dest = (byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch;
 				byte *src = (byte *)_destbuffer->pixels + rx1 * 2 + y * _destbuffer->pitch;
 				while (h--) {
 					memcpy(dest, src, w*2);
@@ -1380,9 +1379,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 #ifdef USE_ASPECT
 			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
 				if (_useOldSrc)
-					r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
 				else
-					r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
 			}
 #endif
 		}
@@ -2155,16 +2154,16 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	uint32 color;
 
 	// Make whole surface transparent
-	for (int i = 0; i < h + _maxExtraPixels * 2; i++) {
+	for (uint i = 0; i < h + _maxExtraPixels * 2; i++) {
 		dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * i;
-		for (int j = 0; j < w + _maxExtraPixels * 2; j++) {
+		for (uint j = 0; j < w + _maxExtraPixels * 2; j++) {
 			*(uint16 *)dstPtr = kMouseColorKey;
 			dstPtr += _cursorFormat.bytesPerPixel;
 		}
 	}
 
 	// Draw from [_maxExtraPixels,_maxExtraPixels] since scalers will read past boudaries
-	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel;
+	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel;
 
 	SDL_Color *palette;
 
@@ -2173,8 +2172,8 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	else
 		palette = _cursorPalette;
 
-	for (int i = 0; i < h; i++) {
-		for (int j = 0; j < w; j++) {
+	for (uint i = 0; i < h; i++) {
+		for (uint j = 0; j < w; j++) {
 			if (_cursorFormat.bytesPerPixel == 2) {
 				color = *(const uint16 *)srcPtr;
 				if (color != _mouseKeyColor) {
@@ -2182,7 +2181,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 					_cursorFormat.colorToRGB(color, r, g, b);
 					*(uint16 *)dstPtr = SDL_MapRGB(_mouseOrigSurface->format, r, g, b);
 				}
-				dstPtr += bytesPerPixel;
+				dstPtr += _cursorFormat.bytesPerPixel;
 				srcPtr += _cursorFormat.bytesPerPixel;
 			} else {
 				color = *srcPtr;
@@ -2222,11 +2221,11 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	// the game. This only works well with the non-blurring scalers so we
 	// otherwise use the Normal scaler
 #ifdef USE_SCALERS
-	if (_cursorTargetScale == 1) {
+	if (!_cursorDontScale) {
 		if (_scalerPlugin->canDrawCursor()) {
 #endif
             _scalerPlugin->scale(
-                    (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+                    (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
                     _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
                     _mouseCurState.w, _mouseCurState.h, 0, 0);
 #ifdef USE_SCALERS
@@ -2234,14 +2233,14 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 			int tmpFactor = _normalPlugin->getFactor();
 			_normalPlugin->setFactor(_videoMode.scaleFactor);
 			_normalPlugin->scale(
-				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
 				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 				_mouseCurState.w, _mouseCurState.h, 0, 0);
 			_normalPlugin->setFactor(tmpFactor);
 		}
 	} else {
         blitSurface(
-                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * bytesPerPixel,
+                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
                 _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 			_mouseCurState.w * 2, _mouseCurState.h);
 	}


Commit: ab000b6ae3a543b9363ad64deab54c15f89e2f6a
    https://github.com/scummvm/scummvm/commit/ab000b6ae3a543b9363ad64deab54c15f89e2f6a
Author: Martin T. H. Sandsmark (martin.sandsmark at kde.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
fix cursor

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 7fd05d33dd..db592495c7 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2043,8 +2043,8 @@ void SurfaceSdlGraphicsManager::setMouseCursor(const void *buf, uint w, uint h,
 }
 
 void SurfaceSdlGraphicsManager::blitCursor() {
-	const uint w = _mouseCurState.w;
-	const uint h = _mouseCurState.h;
+	const int w = _mouseCurState.w;
+	const int h = _mouseCurState.h;
 
 	if (!w || !h || !_mouseOrigSurface) {
 		return;
@@ -2158,12 +2158,12 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 		dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * i;
 		for (uint j = 0; j < w + _maxExtraPixels * 2; j++) {
 			*(uint16 *)dstPtr = kMouseColorKey;
-			dstPtr += _cursorFormat.bytesPerPixel;
+			dstPtr += _mouseOrigSurface->format->BytesPerPixel;
 		}
 	}
 
 	// Draw from [_maxExtraPixels,_maxExtraPixels] since scalers will read past boudaries
-	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel;
+	dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel;
 
 	SDL_Color *palette;
 
@@ -2172,8 +2172,8 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	else
 		palette = _cursorPalette;
 
-	for (uint i = 0; i < h; i++) {
-		for (uint j = 0; j < w; j++) {
+	for (int i = 0; i < h; i++) {
+		for (int j = 0; j < w; j++) {
 			if (_cursorFormat.bytesPerPixel == 2) {
 				color = *(const uint16 *)srcPtr;
 				if (color != _mouseKeyColor) {
@@ -2181,7 +2181,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 					_cursorFormat.colorToRGB(color, r, g, b);
 					*(uint16 *)dstPtr = SDL_MapRGB(_mouseOrigSurface->format, r, g, b);
 				}
-				dstPtr += _cursorFormat.bytesPerPixel;
+				dstPtr += _mouseOrigSurface->format->BytesPerPixel;
 				srcPtr += _cursorFormat.bytesPerPixel;
 			} else {
 				color = *srcPtr;
@@ -2193,7 +2193,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 				srcPtr++;
 			}
 		}
-		dstPtr += _mouseOrigSurface->pitch - w * _cursorFormat.bytesPerPixel;
+		dstPtr += _mouseOrigSurface->pitch - w * _mouseOrigSurface->format->BytesPerPixel;
 	}
 
 	if (sizeChanged || !_mouseSurface) {
@@ -2225,7 +2225,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 		if (_scalerPlugin->canDrawCursor()) {
 #endif
             _scalerPlugin->scale(
-                    (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
+                    (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
                     _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
                     _mouseCurState.w, _mouseCurState.h, 0, 0);
 #ifdef USE_SCALERS
@@ -2233,14 +2233,14 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 			int tmpFactor = _normalPlugin->getFactor();
 			_normalPlugin->setFactor(_videoMode.scaleFactor);
 			_normalPlugin->scale(
-				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
+				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
 				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 				_mouseCurState.w, _mouseCurState.h, 0, 0);
 			_normalPlugin->setFactor(tmpFactor);
 		}
 	} else {
         blitSurface(
-                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _cursorFormat.bytesPerPixel,
+                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
                 _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 			_mouseCurState.w * 2, _mouseCurState.h);
 	}


Commit: 9296b92d8f24024fc8ddf537020a4f53d1775ab3
    https://github.com/scummvm/scummvm/commit/9296b92d8f24024fc8ddf537020a4f53d1775ab3
Author: Martin T. H. Sandsmark (martin.sandsmark at kde.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
fix crash on exit

Changed paths:
    backends/platform/sdl/sdl.cpp


diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index 4267c1d38e..285cf09240 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -153,14 +153,6 @@ OSystem_SDL::~OSystem_SDL() {
 	delete _mutexManager;
 	_mutexManager = 0;
 
-#ifdef USE_OPENGL
-	for (uint i = 0; i < _graphicsModes.size(); ++i) {
-		// SurfaceSDL needs us to free these
-		free(const_cast<char *>(_graphicsModes[i].name));
-		free(const_cast<char *>(_graphicsModes[i].description));
-	}
-#endif
-
 	delete _logger;
 	_logger = 0;
 


Commit: b78323897dd7432f765c94b8d3b91851690659ee
    https://github.com/scummvm/scummvm/commit/b78323897dd7432f765c94b8d3b91851690659ee
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix compilation

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scaler/intern.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index db592495c7..d1901d0ae8 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -264,6 +264,7 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	case OSystem::kFeatureFilteringMode:
 		return _videoMode.filtering;
+#endif
 	case OSystem::kFeatureCursorPalette:
 		return !_cursorPaletteDisabled;
 	default:
@@ -288,8 +289,8 @@ static void initGraphicsModes() {
 		for (uint j = 0; j < factors.size(); ++j) {
 			Common::String n1 = Common::String::format("%s%dx", name, factors[j]);
 			Common::String n2 = Common::String::format("%s%dx", prettyName, factors[j]);
-			gm.name = strdup(n1.c_str());
-			gm.description = strdup(n2.c_str());
+			gm.name = scumm_strdup(n1.c_str());
+			gm.description = scumm_strdup(n2.c_str());
 			gm.id = s_supportedGraphicsModes->size();
 
 			// if normal2x exists, it is the default
@@ -318,8 +319,8 @@ const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes()
 	// Do deep copy. Each can be freed independently of the other.
 	OSystem::GraphicsMode *gm = modes;
 	while (gm->name) {
-		gm->name = strdup(gm->name);
-		gm->description = strdup(gm->description);
+		gm->name = scumm_strdup(gm->name);
+		gm->description = scumm_strdup(gm->description);
 		++gm;
 	}
 
@@ -598,7 +599,7 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() {
 		// Get our currently set hardware format
 		Graphics::PixelFormat hwFormat = convertSDLPixelFormat(_hwScreen->format);
 
-		_supportedFormats.push_back(format);
+		_supportedFormats.push_back(hwFormat);
 
 #if !SDL_VERSION_ATLEAST(2, 0, 0)
 		format = hwFormat;
@@ -710,8 +711,7 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 
 	// If the _scalerIndex has changed, change scaler plugins
 	if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _scalerPlugin || _transactionDetails.formatChanged) {
-		Graphics::PixelFormat format;
-		convertSDLPixelFormat(_hwScreen->format, &format);
+		Graphics::PixelFormat format = convertSDLPixelFormat(_hwScreen->format);
 		if (_scalerPlugin)
 			_scalerPlugin->deinitialize();
 
@@ -1343,15 +1343,15 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 				if (_overlayVisible) {
 					blitSurface((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-					            (byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w * 2, dst_h);
+					            (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w * 2, dst_h);
 				} else {
 					if (_useOldSrc) {
 						// scale into _destbuffer instead of _hwScreen to avoid AR problems
 						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_destbuffer->pixels + rx1 * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
+							(byte *)_destbuffer->pixels + dst_x * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
 					} else
 						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+							(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
 			}
 
@@ -1366,8 +1366,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				int y = orig_dst_y * scale1;
 				int h = r->h;
 				int w = r->w;
-				byte *dest = (byte *)_hwScreen->pixels + rx1 * 2 + dst_y * dstPitch;
-				byte *src = (byte *)_destbuffer->pixels + rx1 * 2 + y * _destbuffer->pitch;
+				byte *dest = (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch;
+				byte *src = (byte *)_destbuffer->pixels + dst_x * 2 + y * _destbuffer->pitch;
 				while (h--) {
 					memcpy(dest, src, w*2);
 					dest += dstPitch;
@@ -1379,9 +1379,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 #ifdef USE_ASPECT
 			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
 				if (_useOldSrc)
-					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
 				else
-					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
 			}
 #endif
 		}
@@ -1697,9 +1697,8 @@ void SurfaceSdlGraphicsManager::addDirtyRect(int x, int y, int w, int h, bool re
 	}
 
 #ifdef USE_ASPECT
-	if (_videoMode.aspectRatioCorrection && !_overlayVisible && !realCoordinates) {
+	if (_videoMode.aspectRatioCorrection && !_overlayVisible && !realCoordinates)
 		makeRectStretchable(x, y, w, h, _videoMode.filtering);
-	}
 #endif
 
 	if (w == width && h == height) {
diff --git a/graphics/scaler/intern.h b/graphics/scaler/intern.h
index c7e412b22c..dbe68a84ed 100644
--- a/graphics/scaler/intern.h
+++ b/graphics/scaler/intern.h
@@ -288,35 +288,6 @@ uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3) {
 	return (rsum & ColorMask::kRedMask) | (gsum & ColorMask::kGreenMask) | (bsum & ColorMask::kBlueMask);
 }
 
-/**
- * Interpolate three 32 bit pixels with weights 1, 1, and 1, i.e., (p1+p2+p3)/3.
- */
-
-template<typename ColorMask>
-uint32 interpolate32_1_1_1(uint32 pixel1, uint32 pixel2, uint32 pixel3) {
-	uint32 rsum, gsum, bsum;
-
-	rsum =  ((pixel1 & ColorMask::kRedMask) >> ColorMask::kRedShift);
-	rsum += ((pixel2 & ColorMask::kRedMask) >> ColorMask::kRedShift);
-	rsum += ((pixel3 & ColorMask::kRedMask) >> ColorMask::kRedShift);
-	rsum /= 3;
-	rsum <<= ColorMask::kRedShift;
-
-	gsum =  ((pixel1 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
-	gsum += ((pixel2 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
-	gsum += ((pixel3 & ColorMask::kGreenMask) >> ColorMask::kGreenShift);
-	gsum /= 3;
-	gsum <<= ColorMask::kGreenShift;
-
-	bsum =  ((pixel1 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
-	bsum += ((pixel2 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
-	bsum += ((pixel3 & ColorMask::kBlueMask) >> ColorMask::kBlueShift);
-	bsum /= 3;
-	bsum <<= ColorMask::kBlueShift;
-
-	return (rsum & ColorMask::kRedMask) | (gsum & ColorMask::kGreenMask) | (bsum & ColorMask::kBlueMask);
-}
-
 /**
  * Interpolate four 32 bit pixels with weights 1, 1, 1, and 1, i.e., (p1+p2+p3+p4)/4.
  *


Commit: ee438bebc2144c1c049ac3a2e4ef7ad87b60f800
    https://github.com/scummvm/scummvm/commit/ee438bebc2144c1c049ac3a2e4ef7ad87b60f800
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Disable more AR related code when USE_ASPECT is not defined.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index d1901d0ae8..97455357af 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -214,7 +214,9 @@ SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
 bool SurfaceSdlGraphicsManager::hasFeature(OSystem::Feature f) const {
 	return
 		(f == OSystem::kFeatureFullscreenMode) ||
+#ifdef USE_ASPECT
 		(f == OSystem::kFeatureAspectRatioCorrection) ||
+#endif
 		(f == OSystem::kFeatureFilteringMode) ||
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 		(f == OSystem::kFeatureFullscreenToggleKeepsContext) ||
@@ -229,9 +231,11 @@ void SurfaceSdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable)
 	case OSystem::kFeatureFullscreenMode:
 		setFullscreenMode(enable);
 		break;
+#ifdef USE_ASPECT
 	case OSystem::kFeatureAspectRatioCorrection:
 		setAspectRatioCorrection(enable);
 		break;
+#endif
 	case OSystem::kFeatureFilteringMode:
 		setFilteringMode(enable);
 		break;


Commit: b225858e41ac1c7f568b01666de9559a2b979457
    https://github.com/scummvm/scummvm/commit/b225858e41ac1c7f568b01666de9559a2b979457
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Let ScalerPluginObject::setFactor return the old factor.

This simplifies things a bit.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 97455357af..3c30b3dd34 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2233,13 +2233,12 @@ void SurfaceSdlGraphicsManager::blitCursor() {
                     _mouseCurState.w, _mouseCurState.h, 0, 0);
 #ifdef USE_SCALERS
 		} else {
-			int tmpFactor = _normalPlugin->getFactor();
-			_normalPlugin->setFactor(_videoMode.scaleFactor);
+			int oldFactor = _normalPlugin->setFactor(_videoMode.scaleFactor);
 			_normalPlugin->scale(
 				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
 				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
 				_mouseCurState.w, _mouseCurState.h, 0, 0);
-			_normalPlugin->setFactor(tmpFactor);
+			_normalPlugin->setFactor(oldFactor);
 		}
 	} else {
         blitSurface(
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 39cc221b0b..1519827b5d 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -68,9 +68,13 @@ public:
 	 * Intended to be used with GUI to set a known valid factor.
 	 * Plugins should override if they require additional state changes.
 	 * @param factor A valid scaling factor for the plugin
-	 * @return The factor set
+	 * @return The old factor.
 	 */
-	virtual uint setFactor(uint factor) { return _factor = factor; }
+	virtual uint setFactor(uint factor) {
+		uint oldFactor = _factor;
+		_factor = factor;
+		return oldFactor;
+	}
 
 	/**
 	 * Indicates how far outside the scaling region this scaler "looks"


Commit: 6942dba3da0e248ef494571eabb8b02916eb7f40
    https://github.com/scummvm/scummvm/commit/6942dba3da0e248ef494571eabb8b02916eb7f40
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Let each scaler handle 1x scaling.

Changed paths:
    graphics/scaler/dotmatrix.cpp
    graphics/scaler/dotmatrix.h
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scaler/hq.cpp
    graphics/scaler/hq.h
    graphics/scaler/normal.cpp
    graphics/scaler/normal.h
    graphics/scaler/pm.cpp
    graphics/scaler/pm.h
    graphics/scaler/sai.cpp
    graphics/scaler/sai.h
    graphics/scaler/scalebit.cpp
    graphics/scaler/scalebit.h
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.cpp b/graphics/scaler/dotmatrix.cpp
index e2945b1886..6e5931dfa3 100644
--- a/graphics/scaler/dotmatrix.cpp
+++ b/graphics/scaler/dotmatrix.cpp
@@ -28,6 +28,8 @@ DotMatrixPlugin::DotMatrixPlugin() {
 }
 
 void DotMatrixPlugin::initialize(const Graphics::PixelFormat &format) {
+	ScalerPluginObject::initialize(format);
+
 	if (format.bytesPerPixel == 2) {
 		uint16 *lookup16 = (uint16 *)lookup;
 		lookup16[0] = lookup16[10] = format.RGBToColor(0, 63, 0);
@@ -49,10 +51,9 @@ void DotMatrixPlugin::initialize(const Graphics::PixelFormat &format) {
 			lookup32[9] = lookup32[13] =
 			lookup32[15] = lookup32[16] = format.ARGBToColor(0, 0, 0, 0);
 	}
-	_format = format;
 }
 
-void DotMatrixPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void DotMatrixPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		scaleIntern<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index a6e52c70b1..19ca5fc0f2 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -28,8 +28,6 @@ class DotMatrixPlugin : public ScalerPluginObject {
 public:
 	DotMatrixPlugin();
 	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -37,6 +35,9 @@ public:
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 private:
 	// Allocate enough for 32bpp formats
 	uint32 lookup[16];
diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index d8453e56af..087d6650fa 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3525,12 +3525,10 @@ EdgePlugin::EdgePlugin() : SourceScaler() {
 }
 
 void EdgePlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
+	SourceScaler::initialize(format);
 	initTables(0, 0, 0, 0);
 }
 
-void EdgePlugin::deinitialize() {
-}
 #if 0
 void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
                        uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 20d76e5fb6..242b50eef1 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -29,11 +29,6 @@ public:
 
 	EdgePlugin();
 	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
-						   uint8 *dstPtr, uint32 dstPitch,
-						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-						   int width, int height);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -43,6 +38,13 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 
+protected:
+
+	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
+						   uint8 *dstPtr, uint32 dstPitch,
+						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
+						   int width, int height);
+
 private:
 
 	/**
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 0ba077b607..62f4268951 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -4991,6 +4991,8 @@ HQPlugin::HQPlugin() {
 }
 
 void HQPlugin::initialize(const Graphics::PixelFormat &format) {
+	ScalerPluginObject::initialize(format);
+
 	if (format.bytesPerPixel == 2) {
 		InitLUT(format);
 	} else {
@@ -5000,7 +5002,6 @@ void HQPlugin::initialize(const Graphics::PixelFormat &format) {
 		                               11, 5, 0, 0);
 		InitLUT(format16);
 	}
-	_format = format;
 }
 
 void HQPlugin::deinitialize() {
@@ -5008,7 +5009,7 @@ void HQPlugin::deinitialize() {
 	RGBtoYUV = 0;
 }
 
-void HQPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void HQPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		switch (_factor) {
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index bf4ed27629..4b0b98721e 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -29,8 +29,6 @@ public:
 	HQPlugin();
 	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -38,6 +36,9 @@ public:
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 };
 
 
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 0464f31dcd..358e0e5422 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -31,30 +31,6 @@ NormalPlugin::NormalPlugin() {
 #endif
 }
 
-void NormalPlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-/**
- * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
- * source to the destination.
- */
-template<typename Pixel>
-void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
-							int width, int height) {
-	// Spot the case when it can all be done in 1 hit
-	int BytesPerPixel = sizeof(Pixel);
-	if ((srcPitch == BytesPerPixel * (uint)width) && (dstPitch == BytesPerPixel * (uint)width)) {
-		memcpy(dstPtr, srcPtr, BytesPerPixel * width * height);
-		return;
-	}
-	while (height--) {
-		memcpy(dstPtr, srcPtr, BytesPerPixel * width);
-		srcPtr += srcPitch;
-		dstPtr += dstPitch;
-	}
-}
-
 #ifdef USE_SCALERS
 
 /**
@@ -190,14 +166,11 @@ void Normal4x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPit
 }
 #endif
 
-void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void NormalPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 #ifdef USE_SCALERS
 	if (_format.bytesPerPixel == 2) {
 		switch (_factor) {
-		case 1:
-			Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
 		case 2:
 #ifdef USE_ARM_SCALER_ASM
 			Normal2xARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
@@ -215,9 +188,6 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 	} else {
 		assert(_format.bytesPerPixel == 4);
 		switch (_factor) {
-		case 1:
-			Normal1x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-			break;
 		case 2:
 			Normal2x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 			break;
@@ -229,12 +199,6 @@ void NormalPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 			break;
 		}
 	}
-#else
-	if (_format.bytesPerPixel == 2) {
-		Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	} else {
-		Normal1x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	}
 #endif
 }
 
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 56ebd8bc2d..ebf1490634 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -27,9 +27,6 @@
 class NormalPlugin : public ScalerPluginObject {
 public:
 	NormalPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -37,6 +34,9 @@ public:
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 };
 
 
diff --git a/graphics/scaler/pm.cpp b/graphics/scaler/pm.cpp
index 77bd546e25..c8548c574c 100644
--- a/graphics/scaler/pm.cpp
+++ b/graphics/scaler/pm.cpp
@@ -190,25 +190,18 @@ PMPlugin::PMPlugin() {
 	_factors.push_back(2);
 }
 
-void PMPlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-void PMPlugin::deinitialize() {
-}
-
-void PMPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void PMPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 }
 
diff --git a/graphics/scaler/pm.h b/graphics/scaler/pm.h
index 9f7d64c7a7..a58ca991a2 100644
--- a/graphics/scaler/pm.h
+++ b/graphics/scaler/pm.h
@@ -27,9 +27,7 @@
 class PMPlugin : public ScalerPluginObject {
 public:
 	PMPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
@@ -38,8 +36,6 @@ public:
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
-private:
-	Graphics::PixelFormat _format;
 };
 
 #endif
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 1c5aabd0ab..07daa26495 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -391,14 +391,7 @@ SAIPlugin::SAIPlugin() {
 	_factors.push_back(2);
 }
 
-void SAIPlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-void SAIPlugin::deinitialize() {
-}
-
-void SAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void SAIPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
@@ -438,14 +431,8 @@ SuperSAIPlugin::SuperSAIPlugin() {
 	_factors.push_back(2);
 }
 
-void SuperSAIPlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
 
-void SuperSAIPlugin::deinitialize() {
-}
-
-void SuperSAIPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void SuperSAIPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
@@ -485,14 +472,7 @@ SuperEaglePlugin::SuperEaglePlugin() {
 	_factors.push_back(2);
 }
 
-void SuperEaglePlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-void SuperEaglePlugin::deinitialize() {
-}
-
-void SuperEaglePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void SuperEaglePlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index 89bd6cb0f7..eb3e4cdd12 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -27,10 +27,6 @@
 class SAIPlugin : public ScalerPluginObject {
 public:
 	SAIPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -38,17 +34,14 @@ public:
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
-private:
-	Graphics::PixelFormat _format;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 };
 
 class SuperSAIPlugin : public ScalerPluginObject {
 public:
 	SuperSAIPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -56,17 +49,14 @@ public:
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
-private:
-	Graphics::PixelFormat _format;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 };
 
 class SuperEaglePlugin : public ScalerPluginObject {
 public:
 	SuperEaglePlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -74,8 +64,9 @@ public:
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
-private:
-	Graphics::PixelFormat _format;
+protected:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 };
 
 #endif
diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp
index 2546d19cee..e7b74c5edc 100644
--- a/graphics/scaler/scalebit.cpp
+++ b/graphics/scaler/scalebit.cpp
@@ -359,14 +359,7 @@ AdvMamePlugin::AdvMamePlugin() {
 	_factors.push_back(4);
 }
 
-void AdvMamePlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-void AdvMamePlugin::deinitialize() {
-}
-
-void AdvMamePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void AdvMamePlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_factor != 4)
 		::scale(_factor, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, _format.bytesPerPixel, width, height);
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index 4461a789ac..b1a28f9d25 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -44,9 +44,7 @@ void scale(unsigned scale, void* void_dst, unsigned dst_slice, const void* void_
 class AdvMamePlugin : public ScalerPluginObject {
 public:
 	AdvMamePlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index b1cb3a72c1..5f18a48e2f 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -28,11 +28,7 @@ TVPlugin::TVPlugin() {
 	_factors.push_back(2);
 }
 
-void TVPlugin::initialize(const Graphics::PixelFormat &format) {
-	_format = format;
-}
-
-void TVPlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
+void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 560cdc69b2..70a34a79b0 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -27,9 +27,6 @@
 class TVPlugin : public ScalerPluginObject {
 public:
 	TVPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual uint getFactor() const { return _factor; }
@@ -38,6 +35,8 @@ public:
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 private:
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	template<typename ColorMask, typename Pixel>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index 378b34563f..30d147ca6d 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -21,6 +21,45 @@
 
 #include "graphics/scalerplugin.h"
 
+void ScalerPluginObject::initialize(const Graphics::PixelFormat &format) {
+	_format = format;
+}
+
+namespace {
+/**
+ * Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
+ * source to the destination.
+ */
+template<typename Pixel>
+void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
+							int width, int height) {
+	// Spot the case when it can all be done in 1 hit
+	int BytesPerPixel = sizeof(Pixel);
+	if ((srcPitch == BytesPerPixel * (uint)width) && (dstPitch == BytesPerPixel * (uint)width)) {
+		memcpy(dstPtr, srcPtr, BytesPerPixel * width * height);
+		return;
+	}
+	while (height--) {
+		memcpy(dstPtr, srcPtr, BytesPerPixel * width);
+		srcPtr += srcPitch;
+		dstPtr += dstPitch;
+	}
+}
+} // End of anonymous namespace
+
+void ScalerPluginObject::scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                           uint32 dstPitch, int width, int height, int x, int y) {
+	if (_factor == 1) {
+		if (_format.bytesPerPixel == 2) {
+			Normal1x<uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		} else {
+			Normal1x<uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+		}
+	} else {
+		scaleIntern(srcPtr, srcPitch, dstPtr, dstPitch, width, height, x, y);
+	}
+}
+
 SourceScaler::SourceScaler() : _oldSrc(NULL), _enable(false) {
 }
 
@@ -40,7 +79,7 @@ void SourceScaler::setSource(const byte *src, uint pitch, int width, int height,
 	memset(_oldSrc, 0, size);
 }
 
-void SourceScaler::scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+void SourceScaler::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
                          uint32 dstPitch, int width, int height, int x, int y) {
 	if (!_enable) {
 		// Do not pass _oldSrc, do not update _oldSrc
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 1519827b5d..a1e9316021 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -35,7 +35,7 @@ public:
 	 * Precomputed data should be generated here.
 	 * @param format The pixel format to scale.
 	 */
-	virtual void initialize(const Graphics::PixelFormat &format) = 0;
+	virtual void initialize(const Graphics::PixelFormat &format);
 
 	/**
 	 * This is called when the plugin is not needed. It should clean
@@ -43,9 +43,20 @@ public:
 	 */
 	virtual void deinitialize() {}
 
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                   uint32 dstPitch, int width, int height, int x, int y) = 0;
-
+	/**
+	 * Scale a rect.
+	 *
+	 * @param srcPtr   Pointer to the source buffer.
+	 * @param srcPitch The number of bytes in a scanline of the source.
+	 * @param dstPtr   Pointer to the destination buffer.
+	 * @param dstPitch The number of bytes in a scanline of the destination.
+	 * @param width    The width of the source rect to scale.
+	 * @param height   The height of the source rect to scale.
+	 * @param x        The x position of the source rect.
+	 * @param y        The y position of the source rect.
+	 */
+	void scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	           uint32 dstPitch, int width, int height, int x, int y);
 
 	/**
 	 * Increase the factor of scaling.
@@ -127,6 +138,12 @@ public:
 	}
 
 protected:
+	/**
+	 * @see scale
+	 */
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                         uint32 dstPitch, int width, int height, int x, int y) = 0;
+
 	uint _factor;
 	Common::Array<uint> _factors;
 	Graphics::PixelFormat _format;
@@ -143,15 +160,15 @@ public:
 	SourceScaler();
 	virtual ~SourceScaler();
 
-	virtual void scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                   uint32 dstPitch, int width, int height, int x, int y);
-
 	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding);
 
 	virtual void enableSource(bool enable) { _enable = enable; }
 
 protected:
 
+	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+	                         uint32 dstPitch, int width, int height, int x, int y);
+
 	/**
 	 * Scalers must implement this function. It will be called by oldSrcScale.
 	 * If by comparing the src and oldsrc images it is discovered that no change


Commit: e8d0182cd58f37071fde2c8deba1eb0d9ccd75a3
    https://github.com/scummvm/scummvm/commit/e8d0182cd58f37071fde2c8deba1eb0d9ccd75a3
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Take advantage that each ScalerPluginObject can use a scale factor of 1.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 3c30b3dd34..abfa6d7d82 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1191,19 +1191,6 @@ void SurfaceSdlGraphicsManager::updateScreen() {
 	internUpdateScreen();
 }
 
-/**
- * Simple blit function. Copies a rectangle from one surface to another.
- *
- * @param byteWidth   The width of the region to copy in bytes.
- */
-static void blitSurface(byte *srcPtr, int srcPitch, byte *dstPtr, int dstPitch, int byteWidth, int height) {
-	while (height--) {
-		memcpy(dstPtr, srcPtr, byteWidth);
-		dstPtr += dstPitch;
-		srcPtr += srcPitch;
-	}
-}
-
 void SurfaceSdlGraphicsManager::internUpdateScreen() {
 	SDL_Surface *srcSurf, *origSurf;
 	int height, width;
@@ -1258,18 +1245,20 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		_forceRedraw = true;
 	}
 
+	int oldScaleFactor;
 	if (!_overlayVisible) {
 		origSurf = _screen;
 		srcSurf = _tmpscreen;
 		width = _videoMode.screenWidth;
 		height = _videoMode.screenHeight;
-		scale1 = _videoMode.scaleFactor;
+		oldScaleFactor = scale1 = _videoMode.scaleFactor;
 	} else {
 		origSurf = _overlayscreen;
 		srcSurf = _tmpscreen2;
 		width = _videoMode.overlayWidth;
 		height = _videoMode.overlayHeight;
 		scale1 = 1;
+		oldScaleFactor = _scalerPlugin->setFactor(1);
 	}
 
 	// Add the area covered by the mouse cursor to the list of dirty rects if
@@ -1345,17 +1334,13 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				if (_videoMode.aspectRatioCorrection && !_overlayVisible)
 					dst_y = real2Aspect(dst_y);
 
-				if (_overlayVisible) {
-					blitSurface((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-					            (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, dst_w * 2, dst_h);
+				if (_useOldSrc && !_overlayVisible) {
+					// scale into _destbuffer instead of _hwScreen to avoid AR problems
+					_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						(byte *)_destbuffer->pixels + dst_x * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
 				} else {
-					if (_useOldSrc) {
-						// scale into _destbuffer instead of _hwScreen to avoid AR problems
-						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_destbuffer->pixels + dst_x * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
-					} else
-						_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-							(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
+					_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+						(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 				}
 			}
 
@@ -1494,6 +1479,9 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		}
 	}
 
+	// Set up the old scale factor
+	_scalerPlugin->setFactor(oldScaleFactor);
+
 	_numDirtyRects = 0;
 	_forceRedraw = false;
 	_cursorNeedsRedraw = false;
@@ -2241,10 +2229,12 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 			_normalPlugin->setFactor(oldFactor);
 		}
 	} else {
-        blitSurface(
-                (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
-                _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-			_mouseCurState.w * 2, _mouseCurState.h);
+		int oldScaleFactor = _scalerPlugin->setFactor(1);
+		_scalerPlugin->scale(
+			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
+			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+			_mouseCurState.w, _mouseCurState.h, 0, 0);
+		_scalerPlugin->setFactor(oldScaleFactor);
 	}
 #endif
 


Commit: abbdadf869196c64624fc48893068aec44ffb037
    https://github.com/scummvm/scummvm/commit/abbdadf869196c64624fc48893068aec44ffb037
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add possibility to search for a scaler via its name.

Changed paths:
    base/plugins.cpp
    graphics/scalerplugin.h


diff --git a/base/plugins.cpp b/base/plugins.cpp
index e677f57827..407f0a71ff 100644
--- a/base/plugins.cpp
+++ b/base/plugins.cpp
@@ -986,3 +986,14 @@ uint ScalerManager::getMaxExtraPixels() const {
 	}
 	return maxPixels;
 }
+
+Plugin *ScalerManager::findScalerPlugin(const char *name) const {
+	const PluginList &plugins = getPlugins();
+	for (PluginList::const_iterator i = plugins.begin(); i != plugins.end(); ++i) {
+		if (!strcmp((*i)->get<ScalerPluginObject>().getName(), name)) {
+			return *i;
+		}
+	}
+
+	return 0;
+}
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index a1e9316021..f8d073a814 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -205,6 +205,11 @@ public:
 	 * to surfaces.
 	 */
 	uint getMaxExtraPixels() const;
+
+	/**
+	 * Search the scaler plugins for a special plugin based on its name.
+	 */
+	Plugin *findScalerPlugin(const char *name) const;
 };
 
 /** Convenience shortcut for accessing singleton */


Commit: 23bee4de30f99db2c1cc910927039aabd579d2b1
    https://github.com/scummvm/scummvm/commit/23bee4de30f99db2c1cc910927039aabd579d2b1
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Properly look up the normal scaler plugin.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index abfa6d7d82..36fb67b7ae 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -169,8 +169,8 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #endif
 
 #endif
-	// HACK: just pick first scaler plugin
-	_normalPlugin = &_scalerPlugins.front()->get<ScalerPluginObject>();
+	_normalPlugin = &ScalerMan.findScalerPlugin("normal")->get<ScalerPluginObject>();
+	assert(_normalPlugin);
 	_scalerPlugin = NULL;
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();


Commit: 5420a7f6bd2d73f831b2fd547140a8c1ca2d851d
    https://github.com/scummvm/scummvm/commit/5420a7f6bd2d73f831b2fd547140a8c1ca2d851d
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Add a default implementation for ScalerPluginObject::getFactor.

Changed paths:
    graphics/scaler/dotmatrix.h
    graphics/scaler/edge.h
    graphics/scaler/hq.h
    graphics/scaler/normal.h
    graphics/scaler/pm.h
    graphics/scaler/sai.h
    graphics/scaler/scalebit.h
    graphics/scaler/tv.h
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 19ca5fc0f2..15095a1415 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -30,7 +30,6 @@ public:
 	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 242b50eef1..51e094112f 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -31,7 +31,6 @@ public:
 	virtual void initialize(const Graphics::PixelFormat &format);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual bool useOldSource() const { return true; }
 	virtual uint extraPixels() const { return 1; }
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index 4b0b98721e..7a5833fe0c 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -31,7 +31,6 @@ public:
 	virtual void deinitialize();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index ebf1490634..970baec3ff 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -29,7 +29,6 @@ public:
 	NormalPlugin();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return true; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/pm.h b/graphics/scaler/pm.h
index a58ca991a2..4f9e7966a5 100644
--- a/graphics/scaler/pm.h
+++ b/graphics/scaler/pm.h
@@ -31,7 +31,6 @@ public:
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 1; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index eb3e4cdd12..eb549e00af 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -29,7 +29,6 @@ public:
 	SAIPlugin();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
@@ -44,7 +43,6 @@ public:
 	SuperSAIPlugin();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
@@ -59,7 +57,6 @@ public:
 	SuperEaglePlugin();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index b1a28f9d25..0c74ebfccd 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -48,7 +48,6 @@ public:
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return true; }
 	virtual uint extraPixels() const { return 2; }
 	virtual const char *getName() const;
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 70a34a79b0..f31f0d3efc 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -29,7 +29,6 @@ public:
 	TVPlugin();
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
-	virtual uint getFactor() const { return _factor; }
 	virtual bool canDrawCursor() const { return false; }
 	virtual uint extraPixels() const { return 0; }
 	virtual const char *getName() const;
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index f8d073a814..a89fe901b0 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -70,7 +70,7 @@ public:
 	 */
 	virtual uint decreaseFactor() = 0;
 
-	virtual uint getFactor() const = 0;
+	virtual uint getFactor() const { return _factor; }
 
 	virtual const Common::Array<uint> &getFactors() const { return _factors; }
 


Commit: 8825c9e3b476bd32be01ffda55d377bcf784c701
    https://github.com/scummvm/scummvm/commit/8825c9e3b476bd32be01ffda55d377bcf784c701
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Keep destination buffer in SourceScaler instead of SDL backend.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 36fb67b7ae..333181f1e5 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -123,7 +123,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 #if defined(WIN32) && !SDL_VERSION_ATLEAST(2, 0, 0)
 	_originalBitsPerPixel(0),
 #endif
-	_screen(0), _tmpscreen(0), _destbuffer(0),
+	_screen(0), _tmpscreen(0),
 	_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
 	_useOldSrc(false),
@@ -731,17 +731,6 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 			_scalerPlugin->enableSource(true);
 			_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
 										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
-			if (!_destbuffer) {
-				_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
-									_videoMode.screenHeight * _videoMode.scaleFactor,
-									16,
-									_hwScreen->format->Rmask,
-									_hwScreen->format->Gmask,
-									_hwScreen->format->Bmask,
-									_hwScreen->format->Amask);
-				if (_destbuffer == NULL)
-					error("allocating _destbuffer failed");
-			}
 		}
 	}
 
@@ -1029,17 +1018,6 @@ bool SurfaceSdlGraphicsManager::loadGFXMode() {
 		// Create surface containing previous frame's data to pass to scaler
 		_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
 									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
-
-		// Create surface containing the raw output from the scaler
-		_destbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth * _videoMode.scaleFactor,
-							_videoMode.screenHeight * _videoMode.scaleFactor,
-							16,
-							_hwScreen->format->Rmask,
-							_hwScreen->format->Gmask,
-							_hwScreen->format->Bmask,
-							_hwScreen->format->Amask);
-		if (_destbuffer == NULL)
-			error("allocating _destbuffer failed");
 	}
 
 	_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth, _videoMode.overlayHeight,
@@ -1088,11 +1066,6 @@ void SurfaceSdlGraphicsManager::unloadGFXMode() {
 		_tmpscreen = NULL;
 	}
 
-	if (_destbuffer) {
-		SDL_FreeSurface(_destbuffer);
-		_destbuffer = NULL;
-	}
-
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1144,10 +1117,6 @@ bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
 		SDL_FreeSurface(_tmpscreen);
 		_tmpscreen = NULL;
 	}
-	if (_destbuffer) {
-		SDL_FreeSurface(_destbuffer);
-        _destbuffer = NULL;
-	}
 	if (_tmpscreen2) {
 		SDL_FreeSurface(_tmpscreen2);
 		_tmpscreen2 = NULL;
@@ -1300,9 +1269,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 		SDL_LockSurface(srcSurf);
 		SDL_LockSurface(_hwScreen);
-		if (_useOldSrc && !_overlayVisible) {
-			SDL_LockSurface(_destbuffer);
-		}
 
 		srcPitch = srcSurf->pitch;
 		dstPitch = _hwScreen->pitch;
@@ -1334,14 +1300,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				if (_videoMode.aspectRatioCorrection && !_overlayVisible)
 					dst_y = real2Aspect(dst_y);
 
-				if (_useOldSrc && !_overlayVisible) {
-					// scale into _destbuffer instead of _hwScreen to avoid AR problems
-					_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-						(byte *)_destbuffer->pixels + dst_x * 2 + orig_dst_y * scale1 * _destbuffer->pitch, _destbuffer->pitch, r->w, dst_h, r->x, r->y);
-				} else {
-					_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
-						(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
-				}
+				_scalerPlugin->scale((byte *)srcSurf->pixels + (r->x + _maxExtraPixels) * 2 + (r->y + _maxExtraPixels) * srcPitch, srcPitch,
+					(byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h, r->x, r->y);
 			}
 
 			r->x = dst_x;
@@ -1349,22 +1309,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 			r->w = dst_w * scale1;
 			r->h = dst_h * scale1;
 
-#ifdef USE_SCALERS
-			if (_useOldSrc && !_overlayVisible) {
-				// Copy _destbuffer back into _hwScreen to be AR corrected
-				int y = orig_dst_y * scale1;
-				int h = r->h;
-				int w = r->w;
-				byte *dest = (byte *)_hwScreen->pixels + dst_x * 2 + dst_y * dstPitch;
-				byte *src = (byte *)_destbuffer->pixels + dst_x * 2 + y * _destbuffer->pitch;
-				while (h--) {
-					memcpy(dest, src, w*2);
-					dest += dstPitch;
-					src += _destbuffer->pitch;
-				}
-			}
-#endif
-
 #ifdef USE_ASPECT
 			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
 				if (_useOldSrc)
@@ -1377,10 +1321,6 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 		SDL_UnlockSurface(srcSurf);
 		SDL_UnlockSurface(_hwScreen);
 
-		if (_useOldSrc && !_overlayVisible) {
-			SDL_UnlockSurface(_destbuffer);
-		}
-
 		// Readjust the dirty rect list in case we are doing a full update.
 		// This is necessary if shaking is active.
 		if (_forceRedraw) {
@@ -1839,16 +1779,9 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	SDL_LockSurface(_tmpscreen);
 	SDL_LockSurface(_overlayscreen);
 
-	// The plugin won't write anything if _useOldSrc
-	if (_useOldSrc)
-		_scalerPlugin->enableSource(false);
-
 	_scalerPlugin->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
-	if (_useOldSrc)
-		_scalerPlugin->enableSource(true);
-
 #ifdef USE_ASPECT
 	if (_videoMode.aspectRatioCorrection)
 		stretch200To240((uint8 *)_overlayscreen->pixels, _overlayscreen->pitch,
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 0a4e5692dd..371795529a 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -217,8 +217,6 @@ protected:
 
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen;
-	/** Previous frame's raw scaled screen (for scalers) */
-	SDL_Surface *_destbuffer;
 	/** Temporary screen (for scalers) */
 	SDL_Surface *_tmpscreen2;
 
diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 087d6650fa..b59dd961a4 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3258,26 +3258,25 @@ int checkUnchangedPixels(const Pixel *old_src_ptr, const Pixel *pixels, int w) {
 /* Draw unchanged pixel grid, 3x */
 /* old_dptr starts in top left of grid, dptr in center */
 template<typename Pixel>
-void draw_unchanged_grid_3x(Pixel *dptr, int dstPitch,
-                            Pixel *old_dptr, int old_dst_inc) {
-	Pixel *sp;
+void drawUnchangedGrid3x(byte *dptr, int dstPitch,
+                         const byte *old_dptr, int old_dst_inc) {
+	const Pixel *sp;
 	Pixel *dp;
-	uint8 *dptr8 = (uint8 *) dptr;
 
-	sp = old_dptr;
-	dp = (Pixel *)(dptr8 - dstPitch) - 1;
+	sp = (const Pixel *)old_dptr;
+	dp = (Pixel *)(dptr - dstPitch) - 1;
 	*dp++ = *sp++;
 	*dp++ = *sp++;
 	*dp = *sp;
 
-	sp = old_dptr + old_dst_inc;
-	dp = dptr - 1;
+	sp = (const Pixel *)(old_dptr + old_dst_inc);
+	dp = (Pixel *)dptr - 1;
 	*dp++ = *sp++;
 	*dp++ = *sp++;
 	*dp = *sp;
 
-	sp = old_dptr + old_dst_inc + old_dst_inc;
-	dp = (Pixel *)(dptr8 + dstPitch) - 1;
+	sp = (const Pixel *)(old_dptr + old_dst_inc + old_dst_inc);
+	dp = (Pixel *)(dptr + dstPitch) - 1;
 	*dp++ = *sp++;
 	*dp++ = *sp++;
 	*dp = *sp;
@@ -3287,19 +3286,18 @@ void draw_unchanged_grid_3x(Pixel *dptr, int dstPitch,
 
 /* Draw unchanged pixel grid, 2x */
 template<typename Pixel>
-void draw_unchanged_grid_2x(Pixel *dptr, int dstPitch,
-                            Pixel *old_dptr, int old_dst_inc) {
-	Pixel *sp;
+void drawUnchangedGrid2x(byte *dptr, int dstPitch,
+                         const byte *old_dptr, int old_dst_inc) {
+	const Pixel *sp;
 	Pixel *dp;
-	uint8 *dptr8 = (uint8 *) dptr;
 
-	sp = old_dptr;
-	dp = dptr;
+	sp = (const Pixel *)old_dptr;
+	dp = (Pixel *)dptr;
 	*dp++ = *sp++;
 	*dp = *sp;
 
-	sp = old_dptr + old_dst_inc;
-	dp = (Pixel *)(dptr8 + dstPitch);
+	sp = (const Pixel *)(old_dptr + old_dst_inc);
+	dp = (Pixel *)(dptr + dstPitch);
 	*dp++ = *sp++;
 	*dp = *sp;
 }
@@ -3310,12 +3308,14 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int w, int h, int w_new, int h_new,
                                  int srcPitch, int dstPitch,
                                  bool haveOldSrc,
-								 const uint8* oldSrc, int oldPitch) {
+                                 const uint8* oldSrc, int oldPitch,
+                                 const uint8 *buffer, int bufferPitch) {
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst + dstPitch + sizeof(Pixel);
 	const Pixel *sptr16;
 	const Pixel *oldSptr;
+	const Pixel *oldDptr;
 	Pixel *dptr16;
 	int16 *bplane;
 	int8 sim[8];
@@ -3323,13 +3323,15 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 	int32 angle;
 	int16 *diffs;
 	int dstPitch3 = dstPitch * 3;
+	int bufferPitch3 = bufferPitch * 3;
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3, oldSrc += oldPitch) {
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch3, oldSrc += oldPitch, buffer += bufferPitch3) {
 		for (x = 0,
 		        sptr16 = (const Pixel *) sptr8,
 		        oldSptr = (const Pixel *) oldSrc,
+		        oldDptr = (const Pixel *) buffer,
 		        dptr16 = (Pixel *) dptr8;
-		        x < w; x++, sptr16++, dptr16 += 3, oldSptr++) {
+		        x < w; x++, sptr16++, dptr16 += 3, oldDptr += 3, oldSptr++) {
 			const Pixel *sptr2, *addr3;
 			Pixel pixels[9];
 			char edge_type;
@@ -3349,8 +3351,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
 						checkUnchangedPixels(oldSptr, pixels, oldPitch / sizeof(Pixel))) {
-					//draw_unchanged_grid_3x(dptr16, dstPitch, old_dptr16,
-					//					   old_dst_inc);
+					drawUnchangedGrid3x<Pixel>((byte *)dptr16, dstPitch, (const byte *)oldDptr, bufferPitch);
 
 #if DEBUG_REFRESH_RANDOM_XOR
 					*(dptr16 + 1) = 0;
@@ -3390,12 +3391,14 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int srcPitch, int dstPitch,
                                  int interpolate_2x,
                                  bool haveOldSrc,
-								 const uint8 *oldSrc, int oldSrcPitch) {
+                                 const uint8 *oldSrc, int oldSrcPitch,
+                                 const uint8 *buffer, int bufferPitch) {
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst;
 	const Pixel *sptr16;
 	const Pixel *oldSptr;
+	const Pixel *oldDptr;
 	Pixel *dptr16;
 	int16 *bplane;
 	int8 sim[8];
@@ -3403,13 +3406,15 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 	int32 angle;
 	int16 *diffs;
 	int dstPitch2 = dstPitch << 1;
+	int bufferPitch2 = bufferPitch * 2;
 
-	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2, oldSrc += oldSrcPitch) {
+	for (y = 0; y < h; y++, sptr8 += srcPitch, dptr8 += dstPitch2, oldSrc += oldSrcPitch, buffer += bufferPitch2) {
 		for (x = 0,
 		        sptr16 = (const Pixel *) sptr8,
 		        dptr16 = (Pixel *) dptr8,
-				oldSptr = (const Pixel *) oldSrc;
-		        x < w; x++, sptr16++, dptr16 += 2, oldSptr++) {
+				oldSptr = (const Pixel *) oldSrc,
+				oldDptr = (const Pixel *) buffer;
+		        x < w; x++, sptr16++, dptr16 += 2, oldDptr += 2, oldSptr++) {
 			const Pixel *sptr2, *addr3;
 			Pixel pixels[9];
 			char edge_type;
@@ -3429,8 +3434,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 						x > 0 && x < w - 1 && y > 0 && y < h - 1 &&
 #endif
 						checkUnchangedPixels<Pixel>(oldSptr, pixels, oldSrcPitch / sizeof(Pixel))) {
-					//draw_unchanged_grid_2x(dptr16, dstPitch, old_dptr16,
-					//					   old_dst_inc);
+					drawUnchangedGrid2x<Pixel>((byte *)dptr16, dstPitch, (const byte *)oldDptr, bufferPitch);
 
 #if DEBUG_REFRESH_RANDOM_XOR
 					*(dptr16 + 1) = 0;
@@ -3561,31 +3565,31 @@ void EdgePlugin::scale(const uint8 *srcPtr, uint32 srcPitch,
 #endif
 
 void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
-                       uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height) {
+                       uint8 *dstPtr, uint32 dstPitch, const uint8 *oldSrcPtr, uint32 oldSrcPitch, int width, int height, const uint8 *buffer, uint32 bufferPitch) {
 	bool enable = oldSrcPtr != NULL;
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	} else {
 		if (_factor == 2) {
 			if (_format.aLoss == 0)
-				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.aLoss == 0)
-				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch);
+				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	}
 }
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 51e094112f..285c053b6c 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -42,7 +42,7 @@ protected:
 	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 						   uint8 *dstPtr, uint32 dstPitch,
 						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-						   int width, int height);
+						   int width, int height, const uint8 *buffer, uint32 bufferPitch);
 
 private:
 
@@ -153,7 +153,8 @@ private:
 		int srcPitch, int dstPitch,
 		int interpolate_2x,
 		bool haveOldSrc,
-		const uint8 *oldSrc, int oldSrcPitch);
+		const uint8 *oldSrc, int oldSrcPitch,
+		const uint8 *buffer, int bufferPitch);
 
 	/**
 	 * Perform edge detection, draw the new 3x pixels
@@ -163,7 +164,8 @@ private:
 		int w, int h, int w_new, int h_new,
 		int srcPitch, int dstPitch,
 		bool haveOldSrc,
-		const uint8* oldSrc, int oldPitch);
+		const uint8* oldSrc, int oldPitch,
+		const uint8 *buffer, int bufferPitch);
 
 	int16 _rgbTable[65536][3];       ///< table lookup for RGB
 	int16 _greyscaleTable[3][65536]; ///< greyscale tables
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index 30d147ca6d..9f677dfb82 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -68,6 +68,11 @@ SourceScaler::~SourceScaler() {
 		delete[] _oldSrc;
 }
 
+void SourceScaler::deinitialize() {
+	_bufferedOutput.free();
+	ScalerPluginObject::deinitialize();
+}
+
 void SourceScaler::setSource(const byte *src, uint pitch, int width, int height, int padding) {
 	if (_oldSrc != NULL)
 		delete[] _oldSrc;
@@ -77,6 +82,8 @@ void SourceScaler::setSource(const byte *src, uint pitch, int width, int height,
 	int size = (height + padding * 2) * pitch;
 	_oldSrc = new byte[size];
 	memset(_oldSrc, 0, size);
+
+	_bufferedOutput.create(width * _factor, height * _factor, _format);
 }
 
 void SourceScaler::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
@@ -86,7 +93,8 @@ void SourceScaler::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstP
 		internScale(srcPtr, srcPitch,
 		            dstPtr, dstPitch,
 		            NULL, 0,
-		            width, height);
+		            width, height,
+		            NULL, 0);
 		return;
 	}
 	int offset = (_padding + x) * _format.bytesPerPixel + (_padding + y) * srcPitch;
@@ -94,7 +102,17 @@ void SourceScaler::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstP
 	internScale(srcPtr, srcPitch,
 	            dstPtr, dstPitch,
 	            _oldSrc + offset, srcPitch,
-	            width, height);
+	            width, height,
+	            (uint8 *)_bufferedOutput.getBasePtr(x * _factor, y * _factor), _bufferedOutput.pitch);
+
+	// Update the destination buffer
+	byte *buffer = (byte *)_bufferedOutput.getBasePtr(x * _factor, y * _factor);
+	for (uint i = 0; i < height * _factor; ++i) {
+		memcpy(buffer, dstPtr, width * _factor * _format.bytesPerPixel);
+		buffer += _bufferedOutput.pitch;
+		dstPtr += dstPitch;
+	}
+
 	// Update old src
 	byte *oldSrc = _oldSrc + offset;
 	while (height--) {
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index a89fe901b0..1d50bb98fd 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -24,6 +24,7 @@
 
 #include "base/plugins.h"
 #include "graphics/pixelformat.h"
+#include "graphics/surface.h"
 
 class ScalerPluginObject : public PluginObject {
 public:
@@ -160,6 +161,8 @@ public:
 	SourceScaler();
 	virtual ~SourceScaler();
 
+	virtual void deinitialize();
+
 	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding);
 
 	virtual void enableSource(bool enable) { _enable = enable; }
@@ -179,14 +182,14 @@ protected:
 	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 	                         uint8 *dstPtr, uint32 dstPitch,
 	                         const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-	                         int width, int height) = 0;
-
+	                         int width, int height, const uint8 *buffer, uint32 bufferPitch) = 0;
 
 private:
 
 	int _padding;
 	bool _enable;
 	byte *_oldSrc;
+	Graphics::Surface _bufferedOutput;
 };
 
 /**


Commit: 088119b57c1c34d27c3aa74887e3c0c4111e1a2c
    https://github.com/scummvm/scummvm/commit/088119b57c1c34d27c3aa74887e3c0c4111e1a2c
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Very small cleanup for Edge scaler code.

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index b59dd961a4..0ddb3aec7f 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3305,7 +3305,7 @@ void drawUnchangedGrid2x(byte *dptr, int dstPitch,
 
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
-                                 int w, int h, int w_new, int h_new,
+                                 int w, int h,
                                  int srcPitch, int dstPitch,
                                  bool haveOldSrc,
                                  const uint8* oldSrc, int oldPitch,
@@ -3387,7 +3387,7 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 
 template<typename ColorMask, typename Pixel>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
-                                 int w, int h, int w_new, int h_new,
+                                 int w, int h,
                                  int srcPitch, int dstPitch,
                                  int interpolate_2x,
                                  bool haveOldSrc,
@@ -3570,26 +3570,26 @@ void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	} else {
 		if (_factor == 2) {
 			if (_format.aLoss == 0)
-				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 2 * width, 2 * height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.aLoss == 0)
-				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, 3 * width, 3 * height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	}
 }
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 285c053b6c..8a9cec60d4 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -149,7 +149,7 @@ private:
 	 */
 	template<typename ColorMask, typename Pixel>
 	void antiAliasPass2x(const uint8 *src, uint8 *dst,
-		int w, int h, int w_new, int h_new,
+		int w, int h,
 		int srcPitch, int dstPitch,
 		int interpolate_2x,
 		bool haveOldSrc,
@@ -161,7 +161,7 @@ private:
 	 */
 	template<typename ColorMask, typename Pixel>
 	void antiAliasPass3x(const uint8 *src, uint8 *dst,
-		int w, int h, int w_new, int h_new,
+		int w, int h,
 		int srcPitch, int dstPitch,
 		bool haveOldSrc,
 		const uint8* oldSrc, int oldPitch,


Commit: 5dc207a3864bf726b966b5d1b568183ce6c1d536
    https://github.com/scummvm/scummvm/commit/5dc207a3864bf726b966b5d1b568183ce6c1d536
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Properly handle extra pixels for scalers in showOverlay.

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 333181f1e5..bb2627683f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1770,7 +1770,7 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	// Clear the overlay by making the game screen "look through" everywhere.
 	SDL_Rect src, dst;
 	src.x = src.y = 0;
-	dst.x = dst.y = 1;
+	dst.x = dst.y = _maxExtraPixels;
 	src.w = dst.w = _videoMode.screenWidth;
 	src.h = dst.h = _videoMode.screenHeight;
 	if (SDL_BlitSurface(_screen, &src, _tmpscreen, &dst) != 0)
@@ -1779,7 +1779,7 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	SDL_LockSurface(_tmpscreen);
 	SDL_LockSurface(_overlayscreen);
 
-	_scalerPlugin->scale((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
+	_scalerPlugin->scale((byte *)(_tmpscreen->pixels) + _maxExtraPixels * _tmpscreen->pitch + _maxExtraPixels * 2, _tmpscreen->pitch,
 	(byte *)_overlayscreen->pixels, _overlayscreen->pitch, _videoMode.screenWidth, _videoMode.screenHeight, 0, 0);
 
 #ifdef USE_ASPECT


Commit: 5df18f7d86c867d50a88d87eed8e690404db8876
    https://github.com/scummvm/scummvm/commit/5df18f7d86c867d50a88d87eed8e690404db8876
Author: Johannes Schickel (lordhoto at scummvm.org)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Take advantage of ColorMasks::PixelType in scaler code.

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/edge.h
    graphics/scaler/hq.cpp
    graphics/scaler/pm.cpp
    graphics/scaler/sai.cpp
    graphics/scaler/tv.cpp
    graphics/scaler/tv.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 0ddb3aec7f..580026e427 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -288,9 +288,9 @@ static double fastAtan(double x0) {
  * This allows 16 and 32 bit functions to easily share
  * the same code.
  */
-template<typename ColorMask, typename Pixel>
-uint16 convertTo16Bit(Pixel p) {
-	if (sizeof(Pixel) == 2)
+template<typename ColorMask>
+uint16 convertTo16Bit(const typename ColorMask::PixelType p) {
+	if (sizeof(typename ColorMask::PixelType) == 2)
 		return p;
 
 	uint16 r, g, b;
@@ -305,15 +305,15 @@ uint16 convertTo16Bit(Pixel p) {
 }
 
 
-template<typename ColorMask, typename Pixel>
-int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
+template<typename ColorMask>
+int16 *EdgePlugin::chooseGreyscale(typename ColorMask::PixelType *pixels) {
 	int i, j;
 	int32 scores[3];
 
 	for (i = 0; i < 3; i++) {
 		int16 *diff_ptr;
 		int16 *bptr;
-		Pixel *pptr;
+		typename ColorMask::PixelType *pptr;
 		int16 *grey_ptr;
 		int16 center;
 		int32 sum_diffs;
@@ -326,10 +326,10 @@ int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
 		bptr = _bplanes[i];
 		pptr = pixels;
 		for (j = 9; j; --j)
-			*bptr++ = grey_ptr[convertTo16Bit<ColorMask, Pixel>(*pptr++)];
+			*bptr++ = grey_ptr[convertTo16Bit<ColorMask>(*pptr++)];
 		bptr = _bplanes[i];
 
-		center = grey_ptr[convertTo16Bit<ColorMask, Pixel>(pixels[4])];
+		center = grey_ptr[convertTo16Bit<ColorMask>(pixels[4])];
 		diff_ptr = _greyscaleDiffs[i];
 
 		/* calculate the delta from center pixel */
@@ -377,10 +377,10 @@ int16 *EdgePlugin::chooseGreyscale(Pixel *pixels) {
 }
 
 
-template<typename ColorMask, typename Pixel>
-int32 EdgePlugin::calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2) {
-	pixel1 = convertTo16Bit<ColorMask, Pixel>(pixel1);
-	pixel2 = convertTo16Bit<ColorMask, Pixel>(pixel2);
+template<typename ColorMask>
+int32 EdgePlugin::calcPixelDiffNosqrt(typename ColorMask::PixelType pixel1, typename ColorMask::PixelType pixel2) {
+	pixel1 = convertTo16Bit<ColorMask>(pixel1);
+	pixel2 = convertTo16Bit<ColorMask>(pixel2);
 
 #if 1   /* distance between pixels, weighted by roughly luma proportions */
 	int32 sum = 0;
@@ -1770,9 +1770,11 @@ int EdgePlugin::fixKnights(int sub_type, Pixel *pixels, int8 *sim) {
 #define redblueMask 0xF81F
 #define greenMask   0x07E0
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
-        Pixel *pixels, int sub_type, int16 *bptr) {
+        typename ColorMask::PixelType *pixels, int sub_type, int16 *bptr) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	Pixel *dptr2;
 	int16 tmp_grey;
 	Pixel center = pixels[4];
@@ -1787,7 +1789,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -1797,9 +1799,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1807,7 +1809,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[6] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2)
@@ -1821,7 +1823,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -1831,9 +1833,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1841,7 +1843,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2)
@@ -1858,9 +1860,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 16) {
 			tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1871,9 +1873,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 17) {
 			tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1889,7 +1891,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -1899,9 +1901,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1909,7 +1911,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2)
@@ -1923,7 +1925,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -1933,9 +1935,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1943,7 +1945,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[6] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2)
@@ -1957,7 +1959,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -1967,9 +1969,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -1977,7 +1979,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2)
@@ -1991,7 +1993,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2001,9 +2003,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2011,7 +2013,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[8] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2)
@@ -2028,9 +2030,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 18) {
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2041,9 +2043,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 19) {
 			tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2059,7 +2061,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2069,9 +2071,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2079,7 +2081,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[8] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2)
@@ -2093,7 +2095,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2103,9 +2105,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2113,7 +2115,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2)
@@ -2127,7 +2129,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2137,9 +2139,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2149,7 +2151,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2159,9 +2161,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2177,7 +2179,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2187,9 +2189,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2199,7 +2201,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2209,9 +2211,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2227,7 +2229,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2237,9 +2239,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2249,7 +2251,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[2] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2259,9 +2261,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2277,7 +2279,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 			tmp[i] = center;
 
 		tmp[6] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[6])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[6])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2287,9 +2289,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[6], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[6], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[6] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2299,7 +2301,7 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[8] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[8])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[8])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2309,9 +2311,9 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[8], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[8], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[8] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2369,11 +2371,13 @@ void EdgePlugin::antiAliasGridClean3x(uint8 *dptr, int dstPitch,
 }
 
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
-                                    Pixel *pixels, int sub_type, int16 *bptr,
+                                    typename ColorMask::PixelType *pixels, int sub_type, int16 *bptr,
                                     int8 *sim,
                                     int interpolate_2x) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	Pixel *dptr2;
 	Pixel center = pixels[4];
 	int32 diff1, diff2, diff3;
@@ -2386,7 +2390,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2396,9 +2400,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2406,7 +2410,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2) {
@@ -2419,7 +2423,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[2] = interpolate_1_1(tmp[2], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])])
 						tmp[2] = center;
 				}
 			}
@@ -2431,7 +2435,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2441,9 +2445,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2451,7 +2455,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[1] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2) {
@@ -2464,7 +2468,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[1] = interpolate_1_1(tmp[1], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])])
 						tmp[1] = center;
 				}
 			}
@@ -2479,9 +2483,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 16) {
 			tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2496,7 +2500,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])] ||
 			         (_simSum == 1 && (sim[0] || sim[7]) &&
 			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 				tmp[1] = center;
@@ -2504,9 +2508,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 17) {
 			tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2521,7 +2525,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])] ||
 			         (_simSum == 1 && (sim[0] || sim[7]) &&
 			          pixels[1] == pixels[3] && pixels[5] == pixels[7]))
 				tmp[2] = center;
@@ -2533,7 +2537,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2543,9 +2547,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2553,7 +2557,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[1] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[8]);
 			if (diff1 <= diff2) {
@@ -2566,7 +2570,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[1] = interpolate_1_1(tmp[1], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])])
 						tmp[1] = center;
 				}
 			}
@@ -2578,7 +2582,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2588,9 +2592,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2598,7 +2602,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[2] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[0]);
 			if (diff1 <= diff2) {
@@ -2611,7 +2615,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[2] = interpolate_1_1(tmp[2], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])])
 						tmp[2] = center;
 				}
 			}
@@ -2623,7 +2627,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2633,9 +2637,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2643,7 +2647,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2) {
@@ -2656,7 +2660,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[0] = interpolate_1_1(tmp[0], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])])
 						tmp[0] = center;
 				}
 			}
@@ -2668,7 +2672,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = center;
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2678,9 +2682,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2688,7 +2692,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[3] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2) {
@@ -2701,7 +2705,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[3] = interpolate_1_1(tmp[3], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])])
 						tmp[3] = center;
 				}
 			}
@@ -2716,9 +2720,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 18) {
 			tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2733,7 +2737,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])] ||
 			         (_simSum == 1 && (sim[2] || sim[5]) &&
 			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 				tmp[0] = center;
@@ -2741,9 +2745,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 
 		if (sub_type != 19) {
 			tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2758,7 +2762,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			 * mouse pointer in Sam&Max.  Half-diags can be too thin in 2x
 			 * nearest-neighbor, so detect them and don't anti-alias them.
 			 */
-			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])] ||
+			else if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])] ||
 			         (_simSum == 1 && (sim[2] || sim[5]) &&
 			          pixels[1] == pixels[5] && pixels[3] == pixels[7]))
 				tmp[3] = center;
@@ -2770,7 +2774,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = center;
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2780,9 +2784,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2790,7 +2794,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[3] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[6]);
 			if (diff1 <= diff2) {
@@ -2803,7 +2807,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[3] = interpolate_1_1(tmp[3], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])])
 						tmp[3] = center;
 				}
 			}
@@ -2815,7 +2819,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_KNIGHTS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2825,9 +2829,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2835,7 +2839,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 			else
 				tmp[0] = pixels[4];
 
-			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+			tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 			diff1 = labs(bptr[4] - tmp_grey);
 			diff2 = labs(bptr[4] - bptr[2]);
 			if (diff1 <= diff2) {
@@ -2848,7 +2852,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 				if (interpolate_2x) {
 					tmp[0] = interpolate_1_1(tmp[0], center);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])])
 						tmp[0] = center;
 				}
 			}
@@ -2860,7 +2864,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -2870,9 +2874,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2886,7 +2890,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[0] = interpolate_1_1(center, tmp[0]);
 					tmp[2] = interpolate_2_1(center, tmp[0]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])])
 						tmp[0] = center;
 				}
 
@@ -2900,7 +2904,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -2910,9 +2914,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2926,7 +2930,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[2] = interpolate_1_1(center, tmp[2]);
 					tmp[0] = interpolate_2_1(center, tmp[2]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])])
 						tmp[2] = center;
 				}
 
@@ -2945,7 +2949,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -2955,9 +2959,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -2971,7 +2975,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[1] = interpolate_1_1(center, tmp[1]);
 					tmp[3] = interpolate_2_1(center, tmp[1]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])])
 						tmp[1] = center;
 				}
 
@@ -2985,7 +2989,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -2995,9 +2999,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3011,7 +3015,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[3] = interpolate_1_1(center, tmp[3]);
 					tmp[1] = interpolate_2_1(center, tmp[3]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])])
 						tmp[3] = center;
 				}
 
@@ -3030,7 +3034,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[0] = interpolate_1_1_1(pixels[1], pixels[3], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[1]);
 		diff2 = labs(bptr[4] - bptr[3]);
@@ -3040,9 +3044,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[3]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[0], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[3]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[0], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[0] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3056,7 +3060,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[0] = interpolate_1_1(center, tmp[0]);
 					tmp[1] = interpolate_2_1(center, tmp[0]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[0])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[0])])
 						tmp[0] = center;
 				}
 
@@ -3070,7 +3074,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[1] = interpolate_1_1_1(pixels[1], pixels[5], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[5]);
 		diff2 = labs(bptr[4] - bptr[1]);
@@ -3080,9 +3084,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[1]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[5]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[1], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[1]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[5]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[1], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[1] = pixels[1];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3096,7 +3100,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[1] = interpolate_1_1(center, tmp[1]);
 					tmp[0] = interpolate_2_1(center, tmp[1]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[1])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[1])])
 						tmp[1] = center;
 				}
 
@@ -3115,7 +3119,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		tmp[0] = tmp[1] = tmp[2] = tmp[3] = center;
 
 		tmp[2] = interpolate_1_1_1(pixels[3], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[3]);
 		diff2 = labs(bptr[4] - bptr[7]);
@@ -3125,9 +3129,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[3]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[2], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[3]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[2], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[2] = pixels[3];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3141,7 +3145,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[2] = interpolate_1_1(center, tmp[2]);
 					tmp[3] = interpolate_2_1(center, tmp[2]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[2])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[2])])
 						tmp[2] = center;
 				}
 
@@ -3155,7 +3159,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		}
 
 		tmp[3] = interpolate_1_1_1(pixels[5], pixels[7], center);
-		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])];
+		tmp_grey = _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])];
 #if PARANOID_ARROWS
 		diff1 = labs(bptr[4] - bptr[7]);
 		diff2 = labs(bptr[4] - bptr[5]);
@@ -3165,9 +3169,9 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 		else    /* choose nearest pixel */
 #endif
 		{
-			diff1 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[5]);
-			diff2 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[7]);
-			diff3 = calcPixelDiffNosqrt<ColorMask, Pixel>(tmp[3], pixels[4]);
+			diff1 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[5]);
+			diff2 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[7]);
+			diff3 = calcPixelDiffNosqrt<ColorMask>(tmp[3], pixels[4]);
 			if (diff1 <= diff2 && diff1 <= diff3)
 				tmp[3] = pixels[5];
 			else if (diff2 <= diff1 && diff2 <= diff3)
@@ -3181,7 +3185,7 @@ void EdgePlugin::antiAliasGrid2x(uint8 *dptr, int dstPitch,
 					tmp[3] = interpolate_1_1(center, tmp[3]);
 					tmp[2] = interpolate_2_1(center, tmp[3]);
 				} else {
-					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask, Pixel>(tmp[3])])
+					if (bptr[4] > _chosenGreyscale[convertTo16Bit<ColorMask>(tmp[3])])
 						tmp[3] = center;
 				}
 
@@ -3303,13 +3307,15 @@ void drawUnchangedGrid2x(byte *dptr, int dstPitch,
 }
 
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
                                  int w, int h,
                                  int srcPitch, int dstPitch,
                                  bool haveOldSrc,
                                  const uint8* oldSrc, int oldPitch,
                                  const uint8 *buffer, int bufferPitch) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst + dstPitch + sizeof(Pixel);
@@ -3360,11 +3366,11 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 				}
 			}
 
-			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
+			diffs = chooseGreyscale<ColorMask>(pixels);
 
 			/* block of solid color */
 			if (!diffs) {
-				antiAliasGridClean3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+				antiAliasGridClean3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 				                                    0, NULL);
 				continue;
 			}
@@ -3378,14 +3384,14 @@ void EdgePlugin::antiAliasPass3x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
-			antiAliasGridClean3x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+			antiAliasGridClean3x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 			                                    sub_type, bplane);
 		}
 	}
 }
 
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  int w, int h,
                                  int srcPitch, int dstPitch,
@@ -3393,6 +3399,8 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
                                  bool haveOldSrc,
                                  const uint8 *oldSrc, int oldSrcPitch,
                                  const uint8 *buffer, int bufferPitch) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	int x, y;
 	const uint8 *sptr8 = src;
 	uint8 *dptr8 = dst;
@@ -3443,11 +3451,11 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 				}
 			}
 
-			diffs = chooseGreyscale<ColorMask, Pixel>(pixels);
+			diffs = chooseGreyscale<ColorMask>(pixels);
 
 			/* block of solid color */
 			if (!diffs) {
-				antiAliasGrid2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+				antiAliasGrid2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 				                              0, NULL, NULL, 0);
 				continue;
 			}
@@ -3461,7 +3469,7 @@ void EdgePlugin::antiAliasPass2x(const uint8 *src, uint8 *dst,
 			if (sub_type >= 0)
 				sub_type = fixKnights<Pixel>(sub_type, pixels, sim);
 
-			antiAliasGrid2x<ColorMask, Pixel>((uint8 *) dptr16, dstPitch, pixels,
+			antiAliasGrid2x<ColorMask>((uint8 *) dptr16, dstPitch, pixels,
 			                              sub_type, bplane, sim,
 			                              interpolate_2x);
 		}
@@ -3570,26 +3578,26 @@ void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
 	if (_format.bytesPerPixel == 2) {
 		if (_factor == 2) {
 			if (_format.gLoss == 2)
-				antiAliasPass2x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.gLoss == 2)
-				antiAliasPass3x<Graphics::ColorMasks<565>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<565> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<555>, uint16>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<555> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	} else {
 		if (_factor == 2) {
 			if (_format.aLoss == 0)
-				antiAliasPass2x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<8888> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass2x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass2x<Graphics::ColorMasks<888> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, 1, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		} else {
 			if (_format.aLoss == 0)
-				antiAliasPass3x<Graphics::ColorMasks<8888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<8888> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 			else
-				antiAliasPass3x<Graphics::ColorMasks<888>, uint32>(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
+				antiAliasPass3x<Graphics::ColorMasks<888> >(srcPtr, dstPtr, width, height, srcPitch, dstPitch, enable, oldSrcPtr, oldSrcPitch, buffer, bufferPitch);
 		}
 	}
 }
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 8a9cec60d4..4fbceff118 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -70,8 +70,8 @@ private:
 	 * speed, and is still a lot faster than edge detecting over all three RGB
 	 * bitplanes.  The increase in image quality is well worth the speed hit.
 	 */
-	template<typename ColorMask, typename Pixel>
-	int16 *chooseGreyscale(Pixel *pixels);
+	template<typename ColorMask>
+	int16 *chooseGreyscale(typename ColorMask::PixelType *pixels);
 
 	/**
 	 * Calculate the distance between pixels in RGB space.  Greyscale isn't
@@ -79,8 +79,8 @@ private:
 	 * of the individual bitplane distances prior to squaring gives the most
 	 * useful results.
 	 */
-	template<typename ColorMask, typename Pixel>
-	int32 calcPixelDiffNosqrt(Pixel pixel1, Pixel pixel2);
+	template<typename ColorMask>
+	int32 calcPixelDiffNosqrt(typename ColorMask::PixelType pixel1, typename ColorMask::PixelType pixel2);
 
 	/**
 	 * Create vectors of all delta grey values from center pixel, with magnitudes
@@ -131,23 +131,23 @@ private:
 	/**
 	 * Fill pixel grid with or without interpolation, using the detected edge
 	 */
-	template<typename ColorMask, typename Pixel>
+	template<typename ColorMask>
 	void antiAliasGrid2x(uint8 *dptr, int dstPitch,
-		Pixel *pixels, int sub_type, int16 *bptr,
+		typename ColorMask::PixelType *pixels, int sub_type, int16 *bptr,
 		int8 *sim,
 		int interpolate_2x);
 
 	/**
 	 * Fill pixel grid without interpolation, using the detected edge
 	 */
-	template<typename ColorMask, typename Pixel>
+	template<typename ColorMask>
 	void antiAliasGridClean3x(uint8 *dptr, int dstPitch,
-		Pixel *pixels, int sub_type, int16 *bptr);
+		typename ColorMask::PixelType *pixels, int sub_type, int16 *bptr);
 
 	/**
 	 * Perform edge detection, draw the new 2x pixels
 	 */
-	template<typename ColorMask, typename Pixel>
+	template<typename ColorMask>
 	void antiAliasPass2x(const uint8 *src, uint8 *dst,
 		int w, int h,
 		int srcPitch, int dstPitch,
@@ -159,7 +159,7 @@ private:
 	/**
 	 * Perform edge detection, draw the new 3x pixels
 	 */
-	template<typename ColorMask, typename Pixel>
+	template<typename ColorMask>
 	void antiAliasPass3x(const uint8 *src, uint8 *dst,
 		int w, int h,
 		int srcPitch, int dstPitch,
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index 62f4268951..da254fb075 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -219,8 +219,10 @@ static inline uint32 ConvertYUV(uint32 x) {
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq2x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	register int w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
@@ -2154,8 +2156,10 @@ void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
  * Original author Maxim Stepin (see http://www.hiend3d.com/hq3x.html).
  * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
  */
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	register int  w1, w2, w3, w4, w5, w6, w7, w8, w9;
 
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
@@ -5023,18 +5027,18 @@ void HQPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 #else
 		case 2:
 			if (_format.gLoss == 2)
-				HQ2x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
+				HQ2x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ2x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
+				HQ2x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			break;
 		case 3:
 			if (_format.gLoss == 2)
-				HQ3x_implementation<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr,
+				HQ3x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ3x_implementation<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr,
+				HQ3x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			break;
 #endif
@@ -5043,18 +5047,18 @@ void HQPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 		switch (_factor) {
 		case 2:
 			if (_format.aLoss == 0)
-				HQ2x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
+				HQ2x_implementation<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ2x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
+				HQ2x_implementation<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			break;
 		case 3:
 			if (_format.aLoss == 0)
-				HQ3x_implementation<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr,
+				HQ3x_implementation<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			else
-				HQ3x_implementation<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr,
+				HQ3x_implementation<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr,
 						dstPitch, width, height);
 			break;
 		}
diff --git a/graphics/scaler/pm.cpp b/graphics/scaler/pm.cpp
index c8548c574c..2ffabff34c 100644
--- a/graphics/scaler/pm.cpp
+++ b/graphics/scaler/pm.cpp
@@ -32,8 +32,10 @@
 #define interpolate_1_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1<ColorMask>(a,b) : interpolate32_1_1<ColorMask>(a,b))
 #define interpolate_3_1(a,b)         (ColorMask::kBytesPerPixel == 2 ? interpolate16_3_1<ColorMask>(a,b) : interpolate32_3_1<ColorMask>(a,b))
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	uint32 nextLineSrc = srcPitch / sizeof(Pixel);
 	uint32 nextLine = dstPitch / sizeof(Pixel);
 	uint32 completeLineSrc = nextLineSrc - width;
@@ -194,14 +196,14 @@ void PMPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			::scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			::scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			::scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			::scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			::scaleIntern<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 }
 
diff --git a/graphics/scaler/sai.cpp b/graphics/scaler/sai.cpp
index 07daa26495..cccb4501f2 100644
--- a/graphics/scaler/sai.cpp
+++ b/graphics/scaler/sai.cpp
@@ -41,8 +41,10 @@ static inline int GetResult(uint32 A, uint32 B, uint32 C, uint32 D) {
 #define interpolate_6_1_1(a,b,c)     (ColorMask::kBytesPerPixel == 2 ? interpolate16_6_1_1<ColorMask>(a,b,c) : interpolate32_6_1_1<ColorMask>(a,b,c))
 #define interpolate_1_1_1_1(a,b,c,d) (ColorMask::kBytesPerPixel == 2 ? interpolate16_1_1_1_1<ColorMask>(a,b,c,d) : interpolate32_1_1_1_1<ColorMask>(a,b,c,d))
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	const Pixel *bP;
 	Pixel *dP;
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
@@ -148,8 +150,10 @@ void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	const Pixel *bP;
 	Pixel *dP;
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
@@ -251,8 +255,10 @@ void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	}
 }
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	const Pixel *bP;
 	Pixel *dP;
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
@@ -395,14 +401,14 @@ void SAIPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			_2xSaITemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			_2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			_2xSaITemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			_2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			_2xSaITemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			_2xSaITemplate<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			_2xSaITemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			_2xSaITemplate<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 }
 
@@ -436,14 +442,14 @@ void SuperSAIPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			Super2xSaITemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			Super2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			Super2xSaITemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			Super2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			Super2xSaITemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			Super2xSaITemplate<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			Super2xSaITemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			Super2xSaITemplate<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 }
 
@@ -476,14 +482,14 @@ void SuperEaglePlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			SuperEagleTemplate<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			SuperEagleTemplate<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			SuperEagleTemplate<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			SuperEagleTemplate<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			SuperEagleTemplate<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			SuperEagleTemplate<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 }
 
diff --git a/graphics/scaler/tv.cpp b/graphics/scaler/tv.cpp
index 5f18a48e2f..dd37582563 100644
--- a/graphics/scaler/tv.cpp
+++ b/graphics/scaler/tv.cpp
@@ -32,14 +32,14 @@ void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) {
 	if (_format.bytesPerPixel == 2) {
 		if (_format.gLoss == 2)
-			scaleIntern<Graphics::ColorMasks<565>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			scaleIntern<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			scaleIntern<Graphics::ColorMasks<555>, uint16>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			scaleIntern<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	} else {
 		if (_format.aLoss == 0)
-			scaleIntern<Graphics::ColorMasks<8888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			scaleIntern<Graphics::ColorMasks<8888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 		else
-			scaleIntern<Graphics::ColorMasks<888>, uint32>(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
+			scaleIntern<Graphics::ColorMasks<888> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	}
 
 }
@@ -60,9 +60,11 @@ const char *TVPlugin::getPrettyName() const {
 	return "TV";
 }
 
-template<typename ColorMask, typename Pixel>
+template<typename ColorMask>
 void TVPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
 					int width, int height) {
+	typedef typename ColorMask::PixelType Pixel;
+
 	const uint32 nextlineSrc = srcPitch / sizeof(Pixel);
 	const Pixel *p = (const Pixel *)srcPtr;
 
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index f31f0d3efc..70a5650186 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -36,7 +36,7 @@ public:
 private:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	template<typename ColorMask, typename Pixel>
+	template<typename ColorMask>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
 };


Commit: 9030ac7305e1f1a4de7935a3daacc46bf3ce30b7
    https://github.com/scummvm/scummvm/commit/9030ac7305e1f1a4de7935a3daacc46bf3ce30b7
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove unused code

Changed paths:
  R graphics/scaler/2xsai.cpp
  R graphics/scaler/hq2x.cpp
  R graphics/scaler/hq3x.cpp
    graphics/scaler.cpp
    graphics/scaler.h
    graphics/scaler/downscaler.h


diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
index fb6eff7c3e..bbd82b3833 100644
--- a/graphics/scaler.cpp
+++ b/graphics/scaler.cpp
@@ -27,6 +27,3 @@
 #include "common/textconsole.h"
 
 int gBitFormat = 565;
-
-void DestroyScalers() {
-}
diff --git a/graphics/scaler.h b/graphics/scaler.h
index 3a878c13cc..44f38ba5ba 100644
--- a/graphics/scaler.h
+++ b/graphics/scaler.h
@@ -26,16 +26,6 @@
 #include "common/scummsys.h"
 #include "graphics/surface.h"
 
-extern void InitScalers(uint32 BitFormat);
-extern void DestroyScalers();
-
-typedef void ScalerProc(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height);
-
-#define DECLARE_SCALER(x)	\
-	extern void x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, \
-					uint32 dstPitch, int width, int height)
-
 // creates a 160x100 thumbnail for 320x200 games
 // and 160x120 thumbnail for 320x240 and 640x480 games
 // only 565 mode
diff --git a/graphics/scaler/2xsai.cpp b/graphics/scaler/2xsai.cpp
deleted file mode 100644
index 4389d52745..0000000000
--- a/graphics/scaler/2xsai.cpp
+++ /dev/null
@@ -1,412 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "graphics/scaler/intern.h"
-
-
-
-static inline int GetResult(uint32 A, uint32 B, uint32 C, uint32 D) {
-	const bool ac = (A==C);
-	const bool bc = (B==C);
-	const int x1 = ac;
-	const int y1 = (bc & !ac);
-	const bool ad = (A==D);
-	const bool bd = (B==D);
-	const int x2 = ad;
-	const int y2 = (bd & !ad);
-	const int x = x1+x2;
-	const int y = y1+y2;
-	return (y>>1) - (x>>1);
-}
-
-#define interpolate_1_1		interpolate16_1_1<ColorMask>
-#define interpolate_3_1		interpolate16_3_1<ColorMask>
-#define interpolate_6_1_1	interpolate16_6_1_1<ColorMask>
-#define interpolate_1_1_1_1	interpolate16_1_1_1_1<ColorMask>
-
-template<typename ColorMask>
-void Super2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
-
-	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
-
-		for (int i = 0; i < width; ++i) {
-			unsigned color4, color5, color6;
-			unsigned color1, color2, color3;
-			unsigned colorA0, colorA1, colorA2, colorA3;
-			unsigned colorB0, colorB1, colorB2, colorB3;
-			unsigned colorS1, colorS2;
-			unsigned product1a, product1b, product2a, product2b;
-
-//---------------------------------------    B1 B2
-//                                         4  5  6 S2
-//                                         1  2  3 S1
-//                                           A1 A2
-
-			colorB0 = *(bP - nextlineSrc - 1);
-			colorB1 = *(bP - nextlineSrc);
-			colorB2 = *(bP - nextlineSrc + 1);
-			colorB3 = *(bP - nextlineSrc + 2);
-
-			color4 = *(bP - 1);
-			color5 = *(bP);
-			color6 = *(bP + 1);
-			colorS2 = *(bP + 2);
-
-			color1 = *(bP + nextlineSrc - 1);
-			color2 = *(bP + nextlineSrc);
-			color3 = *(bP + nextlineSrc + 1);
-			colorS1 = *(bP + nextlineSrc + 2);
-
-			colorA0 = *(bP + 2 * nextlineSrc - 1);
-			colorA1 = *(bP + 2 * nextlineSrc);
-			colorA2 = *(bP + 2 * nextlineSrc + 1);
-			colorA3 = *(bP + 2 * nextlineSrc + 2);
-
-//--------------------------------------
-			if (color2 == color6 && color5 != color3) {
-				product2b = product1b = color2;
-			} else if (color5 == color3 && color2 != color6) {
-				product2b = product1b = color5;
-			} else if (color5 == color3 && color2 == color6) {
-				int r = 0;
-
-				r += GetResult(color6, color5, color1, colorA1);
-				r += GetResult(color6, color5, color4, colorB1);
-				r += GetResult(color6, color5, colorA2, colorS1);
-				r += GetResult(color6, color5, colorB2, colorS2);
-
-				if (r > 0)
-					product2b = product1b = color6;
-				else if (r < 0)
-					product2b = product1b = color5;
-				else {
-					product2b = product1b = interpolate_1_1(color5, color6);
-				}
-			} else {
-				if (color6 == color3 && color3 == colorA1 && color2 != colorA2 && color3 != colorA0)
-					product2b = interpolate_3_1(color3, color2);
-				else if (color5 == color2 && color2 == colorA2 && colorA1 != color3 && color2 != colorA3)
-					product2b = interpolate_3_1(color2, color3);
-				else
-					product2b = interpolate_1_1(color2, color3);
-
-				if (color6 == color3 && color6 == colorB1 && color5 != colorB2 && color6 != colorB0)
-					product1b = interpolate_3_1(color6, color5);
-				else if (color5 == color2 && color5 == colorB2 && colorB1 != color6 && color5 != colorB3)
-					product1b = interpolate_3_1(color5, color6);
-				else
-					product1b = interpolate_1_1(color5, color6);
-			}
-
-			if (color5 == color3 && color2 != color6 && color4 == color5 && color5 != colorA2)
-				product2a = interpolate_1_1(color2, color5);
-			else if (color5 == color1 && color6 == color5 && color4 != color2 && color5 != colorA0)
-				product2a = interpolate_1_1(color2, color5);
-			else
-				product2a = color2;
-
-			if (color2 == color6 && color5 != color3 && color1 == color2 && color2 != colorB2)
-				product1a = interpolate_1_1(color2, color5);
-			else if (color4 == color2 && color3 == color2 && color1 != color5 && color2 != colorB0)
-				product1a = interpolate_1_1(color2, color5);
-			else
-				product1a = color5;
-
-			*(dP + 0) = (uint16) product1a;
-			*(dP + 1) = (uint16) product1b;
-			*(dP + dstPitch/2 + 0) = (uint16) product2a;
-			*(dP + dstPitch/2 + 1) = (uint16) product2b;
-
-			bP += 1;
-			dP += 2;
-		}
-
-		srcPtr += srcPitch;
-		dstPtr += dstPitch * 2;
-	}
-}
-
-void Super2xSaI(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		Super2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		Super2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-template<typename ColorMask>
-void SuperEagleTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
-
-	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
-		for (int i = 0; i < width; ++i) {
-			unsigned color4, color5, color6;
-			unsigned color1, color2, color3;
-			unsigned colorA1, colorA2, colorB1, colorB2, colorS1, colorS2;
-			unsigned product1a, product1b, product2a, product2b;
-
-			colorB1 = *(bP - nextlineSrc);
-			colorB2 = *(bP - nextlineSrc + 1);
-
-			color4 = *(bP - 1);
-			color5 = *(bP);
-			color6 = *(bP + 1);
-			colorS2 = *(bP + 2);
-
-			color1 = *(bP + nextlineSrc - 1);
-			color2 = *(bP + nextlineSrc);
-			color3 = *(bP + nextlineSrc + 1);
-			colorS1 = *(bP + nextlineSrc + 2);
-
-			colorA1 = *(bP + 2 * nextlineSrc);
-			colorA2 = *(bP + 2 * nextlineSrc + 1);
-
-			// --------------------------------------
-			if (color5 != color3) {
-				if (color2 == color6) {
-					product1b = product2a = color2;
-					if ((color1 == color2) || (color6 == colorB2)) {
-						product1a = interpolate_3_1(color2, color5);
-					} else {
-						product1a = interpolate_1_1(color5, color6);
-					}
-
-					if ((color6 == colorS2) || (color2 == colorA1)) {
-						product2b = interpolate_3_1(color2, color3);
-					} else {
-						product2b = interpolate_1_1(color2, color3);
-					}
-				} else {
-					product2b = interpolate_6_1_1(color3, color2, color6);
-					product1a = interpolate_6_1_1(color5, color2, color6);
-
-					product2a = interpolate_6_1_1(color2, color5, color3);
-					product1b = interpolate_6_1_1(color6, color5, color3);
-				}
-			} else {
-				if (color2 != color6) {
-					product2b = product1a = color5;
-
-					if ((colorB1 == color5) || (color3 == colorS1)) {
-						product1b = interpolate_3_1(color5, color6);
-					} else {
-						product1b = interpolate_1_1(color5, color6);
-					}
-
-					if ((color3 == colorA2) || (color4 == color5)) {
-						product2a = interpolate_3_1(color5, color2);
-					} else {
-						product2a = interpolate_1_1(color2, color3);
-					}
-				} else {
-					int r = 0;
-
-					r += GetResult(color6, color5, color1, colorA1);
-					r += GetResult(color6, color5, color4, colorB1);
-					r += GetResult(color6, color5, colorA2, colorS1);
-					r += GetResult(color6, color5, colorB2, colorS2);
-
-					if (r > 0) {
-						product1b = product2a = color2;
-						product1a = product2b = interpolate_1_1(color5, color6);
-					} else if (r < 0) {
-						product2b = product1a = color5;
-						product1b = product2a = interpolate_1_1(color5, color6);
-					} else {
-						product2b = product1a = color5;
-						product1b = product2a = color2;
-					}
-				}
-			}
-
-			*(dP + 0) = (uint16) product1a;
-			*(dP + 1) = (uint16) product1b;
-			*(dP + dstPitch/2 + 0) = (uint16) product2a;
-			*(dP + dstPitch/2 + 1) = (uint16) product2b;
-
-			bP += 1;
-			dP += 2;
-		}
-
-		srcPtr += srcPitch;
-		dstPtr += dstPitch * 2;
-	}
-}
-
-
-void SuperEagle(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		SuperEagleTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		SuperEagleTemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-template<typename ColorMask>
-void _2xSaITemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	const uint16 *bP;
-	uint16 *dP;
-	const uint32 nextlineSrc = srcPitch >> 1;
-
-	while (height--) {
-		bP = (const uint16 *)srcPtr;
-		dP = (uint16 *)dstPtr;
-
-		for (int i = 0; i < width; ++i) {
-
-			unsigned colorA, colorB, colorC, colorD,
-				colorE, colorF, colorG, colorH, colorI, colorJ, colorK, colorL, colorM, colorN, colorO;
-			unsigned product, product1, product2;
-
-//---------------------------------------
-// Map of the pixels:                    I|E F|J
-//                                       G|A B|K
-//                                       H|C D|L
-//                                       M|N O|P
-			colorI = *(bP - nextlineSrc - 1);
-			colorE = *(bP - nextlineSrc);
-			colorF = *(bP - nextlineSrc + 1);
-			colorJ = *(bP - nextlineSrc + 2);
-
-			colorG = *(bP - 1);
-			colorA = *(bP);
-			colorB = *(bP + 1);
-			colorK = *(bP + 2);
-
-			colorH = *(bP + nextlineSrc - 1);
-			colorC = *(bP + nextlineSrc);
-			colorD = *(bP + nextlineSrc + 1);
-			colorL = *(bP + nextlineSrc + 2);
-
-			colorM = *(bP + 2 * nextlineSrc - 1);
-			colorN = *(bP + 2 * nextlineSrc);
-			colorO = *(bP + 2 * nextlineSrc + 1);
-
-			if ((colorA == colorD) && (colorB != colorC)) {
-				if (((colorA == colorE) && (colorB == colorL)) ||
-					((colorA == colorC) && (colorA == colorF) && (colorB != colorE) && (colorB == colorJ))) {
-					product = colorA;
-				} else {
-					product = interpolate_1_1(colorA, colorB);
-				}
-
-				if (((colorA == colorG) && (colorC == colorO)) ||
-					((colorA == colorB) && (colorA == colorH) && (colorG != colorC)  && (colorC == colorM))) {
-					product1 = colorA;
-				} else {
-					product1 = interpolate_1_1(colorA, colorC);
-				}
-				product2 = colorA;
-			} else if ((colorB == colorC) && (colorA != colorD)) {
-				if (((colorB == colorF) && (colorA == colorH)) ||
-					((colorB == colorE) && (colorB == colorD) && (colorA != colorF) && (colorA == colorI))) {
-					product = colorB;
-				} else {
-					product = interpolate_1_1(colorA, colorB);
-				}
-
-				if (((colorC == colorH) && (colorA == colorF)) ||
-					((colorC == colorG) && (colorC == colorD) && (colorA != colorH) && (colorA == colorI))) {
-					product1 = colorC;
-				} else {
-					product1 = interpolate_1_1(colorA, colorC);
-				}
-				product2 = colorB;
-			} else if ((colorA == colorD) && (colorB == colorC)) {
-				if (colorA == colorB) {
-					product = colorA;
-					product1 = colorA;
-					product2 = colorA;
-				} else {
-					int r = 0;
-
-					product1 = interpolate_1_1(colorA, colorC);
-					product = interpolate_1_1(colorA, colorB);
-
-					r += GetResult(colorA, colorB, colorG, colorE);
-					r -= GetResult(colorB, colorA, colorK, colorF);
-					r -= GetResult(colorB, colorA, colorH, colorN);
-					r += GetResult(colorA, colorB, colorL, colorO);
-
-					if (r > 0)
-						product2 = colorA;
-					else if (r < 0)
-						product2 = colorB;
-					else {
-						product2 = interpolate_1_1_1_1(colorA, colorB, colorC, colorD);
-					}
-				}
-			} else {
-				product2 = interpolate_1_1_1_1(colorA, colorB, colorC, colorD);
-
-				if ((colorA == colorC) && (colorA == colorF)
-						&& (colorB != colorE) && (colorB == colorJ)) {
-					product = colorA;
-				} else if ((colorB == colorE) && (colorB == colorD)
-									 && (colorA != colorF) && (colorA == colorI)) {
-					product = colorB;
-				} else {
-					product = interpolate_1_1(colorA, colorB);
-				}
-
-				if ((colorA == colorB) && (colorA == colorH)
-						&& (colorG != colorC) && (colorC == colorM)) {
-					product1 = colorA;
-				} else if ((colorC == colorG) && (colorC == colorD)
-									 && (colorA != colorH) && (colorA == colorI)) {
-					product1 = colorC;
-				} else {
-					product1 = interpolate_1_1(colorA, colorC);
-				}
-			}
-
-			*(dP + 0) = (uint16) colorA;
-			*(dP + 1) = (uint16) product;
-			*(dP + dstPitch/2 + 0) = (uint16) product1;
-			*(dP + dstPitch/2 + 1) = (uint16) product2;
-
-			bP += 1;
-			dP += 2;
-		}
-
-		srcPtr += srcPitch;
-		dstPtr += dstPitch * 2;
-	}
-}
-
-
-void _2xSaI(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		_2xSaITemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		_2xSaITemplate<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
diff --git a/graphics/scaler/downscaler.h b/graphics/scaler/downscaler.h
index 286879915a..2120a1f7b7 100644
--- a/graphics/scaler/downscaler.h
+++ b/graphics/scaler/downscaler.h
@@ -24,12 +24,12 @@
 #define GRAPHICS_SCALER_DOWNSCALER_H
 
 #include "common/scummsys.h"
-#include "graphics/scaler.h"
 
 /**
  * This filter (down)scales the source image by a factor of 1/2.
  * For example, a 320x200 image is scaled to 160x100.
  */
-DECLARE_SCALER(DownscaleAllByHalf);
+extern void DownscaleAllByHalf(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
+                               uint32 dstPitch, int width, int height);
 
 #endif
diff --git a/graphics/scaler/hq2x.cpp b/graphics/scaler/hq2x.cpp
deleted file mode 100644
index 04babb2c5b..0000000000
--- a/graphics/scaler/hq2x.cpp
+++ /dev/null
@@ -1,1970 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "graphics/scaler/intern.h"
-
-#ifdef USE_NASM
-// Assembly version of HQ2x
-
-extern "C" {
-
-#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
-#define hq2x_16 _hq2x_16
-#endif
-
-void hq2x_16(const byte *, byte *, uint32, uint32, uint32, uint32);
-
-}
-
-void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	hq2x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
-}
-
-#else
-
-#define PIXEL00_0	*(q) = w5;
-#define PIXEL00_10	*(q) = interpolate16_3_1<ColorMask >(w5, w1);
-#define PIXEL00_11	*(q) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL00_12	*(q) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL00_20	*(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_21	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w2);
-#define PIXEL00_22	*(q) = interpolate16_2_1_1<ColorMask >(w5, w1, w4);
-#define PIXEL00_60	*(q) = interpolate16_5_2_1<ColorMask >(w5, w2, w4);
-#define PIXEL00_61	*(q) = interpolate16_5_2_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_70	*(q) = interpolate16_6_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_90	*(q) = interpolate16_2_3_3<ColorMask >(w5, w4, w2);
-#define PIXEL00_100	*(q) = interpolate16_14_1_1<ColorMask >(w5, w4, w2);
-
-#define PIXEL01_0	*(q+1) = w5;
-#define PIXEL01_10	*(q+1) = interpolate16_3_1<ColorMask >(w5, w3);
-#define PIXEL01_11	*(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL01_12	*(q+1) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL01_20	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_21	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w6);
-#define PIXEL01_22	*(q+1) = interpolate16_2_1_1<ColorMask >(w5, w3, w2);
-#define PIXEL01_60	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w6, w2);
-#define PIXEL01_61	*(q+1) = interpolate16_5_2_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_70	*(q+1) = interpolate16_6_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL01_90	*(q+1) = interpolate16_2_3_3<ColorMask >(w5, w2, w6);
-#define PIXEL01_100	*(q+1) = interpolate16_14_1_1<ColorMask >(w5, w2, w6);
-
-#define PIXEL10_0	*(q+nextlineDst) = w5;
-#define PIXEL10_10	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w7);
-#define PIXEL10_11	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL10_12	*(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL10_20	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_21	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w4);
-#define PIXEL10_22	*(q+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w7, w8);
-#define PIXEL10_60	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w4, w8);
-#define PIXEL10_61	*(q+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_70	*(q+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL10_90	*(q+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w8, w4);
-#define PIXEL10_100	*(q+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w8, w4);
-
-#define PIXEL11_0	*(q+1+nextlineDst) = w5;
-#define PIXEL11_10	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w9);
-#define PIXEL11_11	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL11_12	*(q+1+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL11_20	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_21	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w8);
-#define PIXEL11_22	*(q+1+nextlineDst) = interpolate16_2_1_1<ColorMask >(w5, w9, w6);
-#define PIXEL11_60	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w8, w6);
-#define PIXEL11_61	*(q+1+nextlineDst) = interpolate16_5_2_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_70	*(q+1+nextlineDst) = interpolate16_6_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL11_90	*(q+1+nextlineDst) = interpolate16_2_3_3<ColorMask >(w5, w6, w8);
-#define PIXEL11_100	*(q+1+nextlineDst) = interpolate16_14_1_1<ColorMask >(w5, w6, w8);
-
-extern "C" uint32   *RGBtoYUV;
-#define YUV(x)	RGBtoYUV[w ## x]
-
-/*
- * The HQ2x high quality 2x graphics filter.
- * Original author Maxim Stepin (see http://www.hiend3d.com/hq2x.html).
- * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
- */
-template<typename ColorMask>
-static void HQ2x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	int w1, w2, w3, w4, w5, w6, w7, w8, w9;
-
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
-
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	uint16 *q = (uint16 *)dstPtr;
-
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w1 | w2 | w3 |
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w4 | w5 | w6 |
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w7 | w8 | w9 |
-	//	 +----+----+----+
-
-	while (height--) {
-		w1 = *(p - 1 - nextlineSrc);
-		w4 = *(p - 1);
-		w7 = *(p - 1 + nextlineSrc);
-
-		w2 = *(p - nextlineSrc);
-		w5 = *(p);
-		w8 = *(p + nextlineSrc);
-
-		int tmpWidth = width;
-		while (tmpWidth--) {
-			p++;
-
-			w3 = *(p - nextlineSrc);
-			w6 = *(p);
-			w9 = *(p + nextlineSrc);
-
-			int pattern = 0;
-			const int yuv5 = YUV(5);
-			if (w5 != w1 && diffYUV(yuv5, YUV(1))) pattern |= 0x0001;
-			if (w5 != w2 && diffYUV(yuv5, YUV(2))) pattern |= 0x0002;
-			if (w5 != w3 && diffYUV(yuv5, YUV(3))) pattern |= 0x0004;
-			if (w5 != w4 && diffYUV(yuv5, YUV(4))) pattern |= 0x0008;
-			if (w5 != w6 && diffYUV(yuv5, YUV(6))) pattern |= 0x0010;
-			if (w5 != w7 && diffYUV(yuv5, YUV(7))) pattern |= 0x0020;
-			if (w5 != w8 && diffYUV(yuv5, YUV(8))) pattern |= 0x0040;
-			if (w5 != w9 && diffYUV(yuv5, YUV(9))) pattern |= 0x0080;
-
-			switch (pattern) {
-			case 0:
-			case 1:
-			case 4:
-			case 32:
-			case 128:
-			case 5:
-			case 132:
-			case 160:
-			case 33:
-			case 129:
-			case 36:
-			case 133:
-			case 164:
-			case 161:
-			case 37:
-			case 165:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 2:
-			case 34:
-			case 130:
-			case 162:
-				PIXEL00_22
-				PIXEL01_21
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 16:
-			case 17:
-			case 48:
-			case 49:
-				PIXEL00_20
-				PIXEL01_22
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 64:
-			case 65:
-			case 68:
-			case 69:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_21
-				PIXEL11_22
-				break;
-			case 8:
-			case 12:
-			case 136:
-			case 140:
-				PIXEL00_21
-				PIXEL01_20
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 3:
-			case 35:
-			case 131:
-			case 163:
-				PIXEL00_11
-				PIXEL01_21
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 6:
-			case 38:
-			case 134:
-			case 166:
-				PIXEL00_22
-				PIXEL01_12
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 20:
-			case 21:
-			case 52:
-			case 53:
-				PIXEL00_20
-				PIXEL01_11
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 144:
-			case 145:
-			case 176:
-			case 177:
-				PIXEL00_20
-				PIXEL01_22
-				PIXEL10_20
-				PIXEL11_12
-				break;
-			case 192:
-			case 193:
-			case 196:
-			case 197:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_21
-				PIXEL11_11
-				break;
-			case 96:
-			case 97:
-			case 100:
-			case 101:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_12
-				PIXEL11_22
-				break;
-			case 40:
-			case 44:
-			case 168:
-			case 172:
-				PIXEL00_21
-				PIXEL01_20
-				PIXEL10_11
-				PIXEL11_20
-				break;
-			case 9:
-			case 13:
-			case 137:
-			case 141:
-				PIXEL00_12
-				PIXEL01_20
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 18:
-			case 50:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 80:
-			case 81:
-				PIXEL00_20
-				PIXEL01_22
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 72:
-			case 76:
-				PIXEL00_21
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 10:
-			case 138:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 66:
-				PIXEL00_22
-				PIXEL01_21
-				PIXEL10_21
-				PIXEL11_22
-				break;
-			case 24:
-				PIXEL00_21
-				PIXEL01_22
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 7:
-			case 39:
-			case 135:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 148:
-			case 149:
-			case 180:
-				PIXEL00_20
-				PIXEL01_11
-				PIXEL10_20
-				PIXEL11_12
-				break;
-			case 224:
-			case 228:
-			case 225:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 41:
-			case 169:
-			case 45:
-				PIXEL00_12
-				PIXEL01_20
-				PIXEL10_11
-				PIXEL11_20
-				break;
-			case 22:
-			case 54:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 208:
-			case 209:
-				PIXEL00_20
-				PIXEL01_22
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 104:
-			case 108:
-				PIXEL00_21
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 11:
-			case 139:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 19:
-			case 51:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_11
-					PIXEL01_10
-				} else {
-					PIXEL00_60
-					PIXEL01_90
-				}
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 146:
-			case 178:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-					PIXEL11_12
-				} else {
-					PIXEL01_90
-					PIXEL11_61
-				}
-				PIXEL10_20
-				break;
-			case 84:
-			case 85:
-				PIXEL00_20
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL01_11
-					PIXEL11_10
-				} else {
-					PIXEL01_60
-					PIXEL11_90
-				}
-				PIXEL10_21
-				break;
-			case 112:
-			case 113:
-				PIXEL00_20
-				PIXEL01_22
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL10_12
-					PIXEL11_10
-				} else {
-					PIXEL10_61
-					PIXEL11_90
-				}
-				break;
-			case 200:
-			case 204:
-				PIXEL00_21
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-					PIXEL11_11
-				} else {
-					PIXEL10_90
-					PIXEL11_60
-				}
-				break;
-			case 73:
-			case 77:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_12
-					PIXEL10_10
-				} else {
-					PIXEL00_61
-					PIXEL10_90
-				}
-				PIXEL01_20
-				PIXEL11_22
-				break;
-			case 42:
-			case 170:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-					PIXEL10_11
-				} else {
-					PIXEL00_90
-					PIXEL10_60
-				}
-				PIXEL01_21
-				PIXEL11_20
-				break;
-			case 14:
-			case 142:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-					PIXEL01_12
-				} else {
-					PIXEL00_90
-					PIXEL01_61
-				}
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 67:
-				PIXEL00_11
-				PIXEL01_21
-				PIXEL10_21
-				PIXEL11_22
-				break;
-			case 70:
-				PIXEL00_22
-				PIXEL01_12
-				PIXEL10_21
-				PIXEL11_22
-				break;
-			case 28:
-				PIXEL00_21
-				PIXEL01_11
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 152:
-				PIXEL00_21
-				PIXEL01_22
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 194:
-				PIXEL00_22
-				PIXEL01_21
-				PIXEL10_21
-				PIXEL11_11
-				break;
-			case 98:
-				PIXEL00_22
-				PIXEL01_21
-				PIXEL10_12
-				PIXEL11_22
-				break;
-			case 56:
-				PIXEL00_21
-				PIXEL01_22
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 25:
-				PIXEL00_12
-				PIXEL01_22
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 26:
-			case 31:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 82:
-			case 214:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 88:
-			case 248:
-				PIXEL00_21
-				PIXEL01_22
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 74:
-			case 107:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 27:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_10
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 86:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_21
-				PIXEL11_10
-				break;
-			case 216:
-				PIXEL00_21
-				PIXEL01_22
-				PIXEL10_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 106:
-				PIXEL00_10
-				PIXEL01_21
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 30:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 210:
-				PIXEL00_22
-				PIXEL01_10
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 120:
-				PIXEL00_21
-				PIXEL01_22
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_10
-				break;
-			case 75:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				PIXEL10_10
-				PIXEL11_22
-				break;
-			case 29:
-				PIXEL00_12
-				PIXEL01_11
-				PIXEL10_22
-				PIXEL11_21
-				break;
-			case 198:
-				PIXEL00_22
-				PIXEL01_12
-				PIXEL10_21
-				PIXEL11_11
-				break;
-			case 184:
-				PIXEL00_21
-				PIXEL01_22
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 99:
-				PIXEL00_11
-				PIXEL01_21
-				PIXEL10_12
-				PIXEL11_22
-				break;
-			case 57:
-				PIXEL00_12
-				PIXEL01_22
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 71:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_21
-				PIXEL11_22
-				break;
-			case 156:
-				PIXEL00_21
-				PIXEL01_11
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 226:
-				PIXEL00_22
-				PIXEL01_21
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 60:
-				PIXEL00_21
-				PIXEL01_11
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 195:
-				PIXEL00_11
-				PIXEL01_21
-				PIXEL10_21
-				PIXEL11_11
-				break;
-			case 102:
-				PIXEL00_22
-				PIXEL01_12
-				PIXEL10_12
-				PIXEL11_22
-				break;
-			case 153:
-				PIXEL00_12
-				PIXEL01_22
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 58:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 83:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 92:
-				PIXEL00_21
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 202:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				PIXEL01_21
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				PIXEL11_11
-				break;
-			case 78:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				PIXEL11_22
-				break;
-			case 154:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 114:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 89:
-				PIXEL00_12
-				PIXEL01_22
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 90:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 55:
-			case 23:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_11
-					PIXEL01_0
-				} else {
-					PIXEL00_60
-					PIXEL01_90
-				}
-				PIXEL10_20
-				PIXEL11_21
-				break;
-			case 182:
-			case 150:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-					PIXEL11_12
-				} else {
-					PIXEL01_90
-					PIXEL11_61
-				}
-				PIXEL10_20
-				break;
-			case 213:
-			case 212:
-				PIXEL00_20
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL01_11
-					PIXEL11_0
-				} else {
-					PIXEL01_60
-					PIXEL11_90
-				}
-				PIXEL10_21
-				break;
-			case 241:
-			case 240:
-				PIXEL00_20
-				PIXEL01_22
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL10_12
-					PIXEL11_0
-				} else {
-					PIXEL10_61
-					PIXEL11_90
-				}
-				break;
-			case 236:
-			case 232:
-				PIXEL00_21
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-					PIXEL11_11
-				} else {
-					PIXEL10_90
-					PIXEL11_60
-				}
-				break;
-			case 109:
-			case 105:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_12
-					PIXEL10_0
-				} else {
-					PIXEL00_61
-					PIXEL10_90
-				}
-				PIXEL01_20
-				PIXEL11_22
-				break;
-			case 171:
-			case 43:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-					PIXEL10_11
-				} else {
-					PIXEL00_90
-					PIXEL10_60
-				}
-				PIXEL01_21
-				PIXEL11_20
-				break;
-			case 143:
-			case 15:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-					PIXEL01_12
-				} else {
-					PIXEL00_90
-					PIXEL01_61
-				}
-				PIXEL10_22
-				PIXEL11_20
-				break;
-			case 124:
-				PIXEL00_21
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_10
-				break;
-			case 203:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				PIXEL10_10
-				PIXEL11_11
-				break;
-			case 62:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 211:
-				PIXEL00_11
-				PIXEL01_10
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 118:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_12
-				PIXEL11_10
-				break;
-			case 217:
-				PIXEL00_12
-				PIXEL01_22
-				PIXEL10_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 110:
-				PIXEL00_10
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 155:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_10
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 188:
-				PIXEL00_21
-				PIXEL01_11
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 185:
-				PIXEL00_12
-				PIXEL01_22
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 61:
-				PIXEL00_12
-				PIXEL01_11
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 157:
-				PIXEL00_12
-				PIXEL01_11
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 103:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_12
-				PIXEL11_22
-				break;
-			case 227:
-				PIXEL00_11
-				PIXEL01_21
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 230:
-				PIXEL00_22
-				PIXEL01_12
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 199:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_21
-				PIXEL11_11
-				break;
-			case 220:
-				PIXEL00_21
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 158:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 234:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				PIXEL01_21
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_11
-				break;
-			case 242:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 59:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 121:
-				PIXEL00_12
-				PIXEL01_22
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 87:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 79:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				PIXEL11_22
-				break;
-			case 122:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 94:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 218:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 91:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 229:
-				PIXEL00_20
-				PIXEL01_20
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 167:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_20
-				PIXEL11_20
-				break;
-			case 173:
-				PIXEL00_12
-				PIXEL01_20
-				PIXEL10_11
-				PIXEL11_20
-				break;
-			case 181:
-				PIXEL00_20
-				PIXEL01_11
-				PIXEL10_20
-				PIXEL11_12
-				break;
-			case 186:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 115:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 93:
-				PIXEL00_12
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 206:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				PIXEL11_11
-				break;
-			case 205:
-			case 201:
-				PIXEL00_12
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_10
-				} else {
-					PIXEL10_70
-				}
-				PIXEL11_11
-				break;
-			case 174:
-			case 46:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_10
-				} else {
-					PIXEL00_70
-				}
-				PIXEL01_12
-				PIXEL10_11
-				PIXEL11_20
-				break;
-			case 179:
-			case 147:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_10
-				} else {
-					PIXEL01_70
-				}
-				PIXEL10_20
-				PIXEL11_12
-				break;
-			case 117:
-			case 116:
-				PIXEL00_20
-				PIXEL01_11
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_10
-				} else {
-					PIXEL11_70
-				}
-				break;
-			case 189:
-				PIXEL00_12
-				PIXEL01_11
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 231:
-				PIXEL00_11
-				PIXEL01_12
-				PIXEL10_12
-				PIXEL11_11
-				break;
-			case 126:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_10
-				break;
-			case 219:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_10
-				PIXEL10_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 125:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_12
-					PIXEL10_0
-				} else {
-					PIXEL00_61
-					PIXEL10_90
-				}
-				PIXEL01_11
-				PIXEL11_10
-				break;
-			case 221:
-				PIXEL00_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL01_11
-					PIXEL11_0
-				} else {
-					PIXEL01_60
-					PIXEL11_90
-				}
-				PIXEL10_10
-				break;
-			case 207:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-					PIXEL01_12
-				} else {
-					PIXEL00_90
-					PIXEL01_61
-				}
-				PIXEL10_10
-				PIXEL11_11
-				break;
-			case 238:
-				PIXEL00_10
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-					PIXEL11_11
-				} else {
-					PIXEL10_90
-					PIXEL11_60
-				}
-				break;
-			case 190:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-					PIXEL11_12
-				} else {
-					PIXEL01_90
-					PIXEL11_61
-				}
-				PIXEL10_11
-				break;
-			case 187:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-					PIXEL10_11
-				} else {
-					PIXEL00_90
-					PIXEL10_60
-				}
-				PIXEL01_10
-				PIXEL11_12
-				break;
-			case 243:
-				PIXEL00_11
-				PIXEL01_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL10_12
-					PIXEL11_0
-				} else {
-					PIXEL10_61
-					PIXEL11_90
-				}
-				break;
-			case 119:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_11
-					PIXEL01_0
-				} else {
-					PIXEL00_60
-					PIXEL01_90
-				}
-				PIXEL10_12
-				PIXEL11_10
-				break;
-			case 237:
-			case 233:
-				PIXEL00_12
-				PIXEL01_20
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				PIXEL11_11
-				break;
-			case 175:
-			case 47:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				PIXEL01_12
-				PIXEL10_11
-				PIXEL11_20
-				break;
-			case 183:
-			case 151:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_20
-				PIXEL11_12
-				break;
-			case 245:
-			case 244:
-				PIXEL00_20
-				PIXEL01_11
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 250:
-				PIXEL00_10
-				PIXEL01_10
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 123:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_10
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_10
-				break;
-			case 95:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_10
-				PIXEL11_10
-				break;
-			case 222:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 252:
-				PIXEL00_21
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 249:
-				PIXEL00_12
-				PIXEL01_22
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 235:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_21
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				PIXEL11_11
-				break;
-			case 111:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_22
-				break;
-			case 63:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_11
-				PIXEL11_21
-				break;
-			case 159:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_22
-				PIXEL11_12
-				break;
-			case 215:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_21
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 246:
-				PIXEL00_22
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 254:
-				PIXEL00_10
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 253:
-				PIXEL00_12
-				PIXEL01_11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 251:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				PIXEL01_10
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 239:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				PIXEL01_12
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				PIXEL11_11
-				break;
-			case 127:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_20
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_20
-				}
-				PIXEL11_10
-				break;
-			case 191:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_11
-				PIXEL11_12
-				break;
-			case 223:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_20
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_10
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_20
-				}
-				break;
-			case 247:
-				PIXEL00_11
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				PIXEL10_12
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			case 255:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_0
-				} else {
-					PIXEL00_100
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_0
-				} else {
-					PIXEL01_100
-				}
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_0
-				} else {
-					PIXEL10_100
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL11_0
-				} else {
-					PIXEL11_100
-				}
-				break;
-			default:
-				break;
-			}
-
-			w1 = w2;
-			w4 = w5;
-			w7 = w8;
-
-			w2 = w3;
-			w5 = w6;
-			w8 = w9;
-
-			q += 2;
-		}
-		p += nextlineSrc - width;
-		q += (nextlineDst - width) * 2;
-	}
-}
-
-void HQ2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		HQ2x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		HQ2x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-#endif // Assembly version
diff --git a/graphics/scaler/hq3x.cpp b/graphics/scaler/hq3x.cpp
deleted file mode 100644
index 713c4406cf..0000000000
--- a/graphics/scaler/hq3x.cpp
+++ /dev/null
@@ -1,2947 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "graphics/scaler/intern.h"
-
-#ifdef USE_NASM
-// Assembly version of HQ3x
-
-extern "C" {
-
-#if !defined(_WIN32) && !defined(MACOSX) && !defined(__OS2__)
-#define hq3x_16 _hq3x_16
-#endif
-
-
-void hq3x_16(const byte *, byte *, uint32, uint32, uint32, uint32);
-
-}
-
-void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	hq3x_16(srcPtr, dstPtr, width, height, srcPitch, dstPitch);
-}
-
-#else
-
-#define PIXEL00_1M  *(q) = interpolate16_3_1<ColorMask >(w5, w1);
-#define PIXEL00_1U  *(q) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL00_1L  *(q) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL00_2   *(q) = interpolate16_2_1_1<ColorMask >(w5, w4, w2);
-#define PIXEL00_4   *(q) = interpolate16_2_7_7<ColorMask >(w5, w4, w2);
-#define PIXEL00_5   *(q) = interpolate16_1_1<ColorMask >(w4, w2);
-#define PIXEL00_C   *(q) = w5;
-
-#define PIXEL01_1   *(q+1) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL01_3   *(q+1) = interpolate16_7_1<ColorMask >(w5, w2);
-#define PIXEL01_6   *(q+1) = interpolate16_3_1<ColorMask >(w2, w5);
-#define PIXEL01_C   *(q+1) = w5;
-
-#define PIXEL02_1M  *(q+2) = interpolate16_3_1<ColorMask >(w5, w3);
-#define PIXEL02_1U  *(q+2) = interpolate16_3_1<ColorMask >(w5, w2);
-#define PIXEL02_1R  *(q+2) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL02_2   *(q+2) = interpolate16_2_1_1<ColorMask >(w5, w2, w6);
-#define PIXEL02_4   *(q+2) = interpolate16_2_7_7<ColorMask >(w5, w2, w6);
-#define PIXEL02_5   *(q+2) = interpolate16_1_1<ColorMask >(w2, w6);
-#define PIXEL02_C   *(q+2) = w5;
-
-#define PIXEL10_1   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL10_3   *(q+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w4);
-#define PIXEL10_6   *(q+nextlineDst) = interpolate16_3_1<ColorMask >(w4, w5);
-#define PIXEL10_C   *(q+nextlineDst) = w5;
-
-#define PIXEL11     *(q+1+nextlineDst) = w5;
-
-#define PIXEL12_1   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL12_3   *(q+2+nextlineDst) = interpolate16_7_1<ColorMask >(w5, w6);
-#define PIXEL12_6   *(q+2+nextlineDst) = interpolate16_3_1<ColorMask >(w6, w5);
-#define PIXEL12_C   *(q+2+nextlineDst) = w5;
-
-#define PIXEL20_1M  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w7);
-#define PIXEL20_1D  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL20_1L  *(q+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w4);
-#define PIXEL20_2   *(q+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w8, w4);
-#define PIXEL20_4   *(q+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w8, w4);
-#define PIXEL20_5   *(q+nextlineDst2) = interpolate16_1_1<ColorMask >(w8, w4);
-#define PIXEL20_C   *(q+nextlineDst2) = w5;
-
-#define PIXEL21_1   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL21_3   *(q+1+nextlineDst2) = interpolate16_7_1<ColorMask >(w5, w8);
-#define PIXEL21_6   *(q+1+nextlineDst2) = interpolate16_3_1<ColorMask >(w8, w5);
-#define PIXEL21_C   *(q+1+nextlineDst2) = w5;
-
-#define PIXEL22_1M  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w9);
-#define PIXEL22_1D  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w8);
-#define PIXEL22_1R  *(q+2+nextlineDst2) = interpolate16_3_1<ColorMask >(w5, w6);
-#define PIXEL22_2   *(q+2+nextlineDst2) = interpolate16_2_1_1<ColorMask >(w5, w6, w8);
-#define PIXEL22_4   *(q+2+nextlineDst2) = interpolate16_2_7_7<ColorMask >(w5, w6, w8);
-#define PIXEL22_5   *(q+2+nextlineDst2) = interpolate16_1_1<ColorMask >(w6, w8);
-#define PIXEL22_C   *(q+2+nextlineDst2) = w5;
-
-extern "C" uint32   *RGBtoYUV;
-#define YUV(x)	RGBtoYUV[w ## x]
-
-/*
- * The HQ3x high quality 3x graphics filter.
- * Original author Maxim Stepin (see http://www.hiend3d.com/hq3x.html).
- * Adapted for ScummVM to 16 bit output and optimized by Max Horn.
- */
-template<typename ColorMask>
-static void HQ3x_implementation(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	int  w1, w2, w3, w4, w5, w6, w7, w8, w9;
-
-	const uint32 nextlineSrc = srcPitch / sizeof(uint16);
-	const uint16 *p = (const uint16 *)srcPtr;
-
-	const uint32 nextlineDst = dstPitch / sizeof(uint16);
-	const uint32 nextlineDst2 = 2 * nextlineDst;
-	uint16 *q = (uint16 *)dstPtr;
-
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w1 | w2 | w3 |
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w4 | w5 | w6 |
-	//	 +----+----+----+
-	//	 |    |    |    |
-	//	 | w7 | w8 | w9 |
-	//	 +----+----+----+
-
-	while (height--) {
-		w1 = *(p - 1 - nextlineSrc);
-		w4 = *(p - 1);
-		w7 = *(p - 1 + nextlineSrc);
-
-		w2 = *(p - nextlineSrc);
-		w5 = *(p);
-		w8 = *(p + nextlineSrc);
-
-		int tmpWidth = width;
-		while (tmpWidth--) {
-			p++;
-
-			w3 = *(p - nextlineSrc);
-			w6 = *(p);
-			w9 = *(p + nextlineSrc);
-
-			int pattern = 0;
-			const int yuv5 = YUV(5);
-			if (w5 != w1 && diffYUV(yuv5, YUV(1))) pattern |= 0x0001;
-			if (w5 != w2 && diffYUV(yuv5, YUV(2))) pattern |= 0x0002;
-			if (w5 != w3 && diffYUV(yuv5, YUV(3))) pattern |= 0x0004;
-			if (w5 != w4 && diffYUV(yuv5, YUV(4))) pattern |= 0x0008;
-			if (w5 != w6 && diffYUV(yuv5, YUV(6))) pattern |= 0x0010;
-			if (w5 != w7 && diffYUV(yuv5, YUV(7))) pattern |= 0x0020;
-			if (w5 != w8 && diffYUV(yuv5, YUV(8))) pattern |= 0x0040;
-			if (w5 != w9 && diffYUV(yuv5, YUV(9))) pattern |= 0x0080;
-
-			switch (pattern) {
-			case 0:
-			case 1:
-			case 4:
-			case 32:
-			case 128:
-			case 5:
-			case 132:
-			case 160:
-			case 33:
-			case 129:
-			case 36:
-			case 133:
-			case 164:
-			case 161:
-			case 37:
-			case 165:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 2:
-			case 34:
-			case 130:
-			case 162:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 16:
-			case 17:
-			case 48:
-			case 49:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 64:
-			case 65:
-			case 68:
-			case 69:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 8:
-			case 12:
-			case 136:
-			case 140:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 3:
-			case 35:
-			case 131:
-			case 163:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 6:
-			case 38:
-			case 134:
-			case 166:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 20:
-			case 21:
-			case 52:
-			case 53:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 144:
-			case 145:
-			case 176:
-			case 177:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 192:
-			case 193:
-			case 196:
-			case 197:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 96:
-			case 97:
-			case 100:
-			case 101:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 40:
-			case 44:
-			case 168:
-			case 172:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 9:
-			case 13:
-			case 137:
-			case 141:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 18:
-			case 50:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_1M
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 80:
-			case 81:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_1M
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 72:
-			case 76:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_1M
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 10:
-			case 138:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 66:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 24:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 7:
-			case 39:
-			case 135:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 148:
-			case 149:
-			case 180:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 224:
-			case 228:
-			case 225:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 41:
-			case 169:
-			case 45:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 22:
-			case 54:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 208:
-			case 209:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 104:
-			case 108:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 11:
-			case 139:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 19:
-			case 51:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_1L
-					PIXEL01_C
-					PIXEL02_1M
-					PIXEL12_C
-				} else {
-					PIXEL00_2
-					PIXEL01_6
-					PIXEL02_5
-					PIXEL12_1
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 146:
-			case 178:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_1M
-					PIXEL12_C
-					PIXEL22_1D
-				} else {
-					PIXEL01_1
-					PIXEL02_5
-					PIXEL12_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				break;
-			case 84:
-			case 85:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL02_1U
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_1M
-				} else {
-					PIXEL02_2
-					PIXEL12_6
-					PIXEL21_1
-					PIXEL22_5
-				}
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				break;
-			case 112:
-			case 113:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL20_1L
-					PIXEL21_C
-					PIXEL22_1M
-				} else {
-					PIXEL12_1
-					PIXEL20_2
-					PIXEL21_6
-					PIXEL22_5
-				}
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				break;
-			case 200:
-			case 204:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_1M
-					PIXEL21_C
-					PIXEL22_1R
-				} else {
-					PIXEL10_1
-					PIXEL20_5
-					PIXEL21_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				break;
-			case 73:
-			case 77:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_1U
-					PIXEL10_C
-					PIXEL20_1M
-					PIXEL21_C
-				} else {
-					PIXEL00_2
-					PIXEL10_6
-					PIXEL20_5
-					PIXEL21_1
-				}
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				PIXEL22_1M
-				break;
-			case 42:
-			case 170:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-					PIXEL01_C
-					PIXEL10_C
-					PIXEL20_1D
-				} else {
-					PIXEL00_5
-					PIXEL01_1
-					PIXEL10_6
-					PIXEL20_2
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 14:
-			case 142:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-					PIXEL01_C
-					PIXEL02_1R
-					PIXEL10_C
-				} else {
-					PIXEL00_5
-					PIXEL01_6
-					PIXEL02_2
-					PIXEL10_1
-				}
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 67:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 70:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 28:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 152:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 194:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 98:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 56:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 25:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 26:
-			case 31:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL10_3
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 82:
-			case 214:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 88:
-			case 248:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL22_4
-				}
-				break;
-			case 74:
-			case 107:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-				}
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 27:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 86:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 216:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 106:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 30:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 210:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 120:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 75:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 29:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 198:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 184:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 99:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 57:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 71:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 156:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 226:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 60:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 195:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 102:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 153:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 58:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 83:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 92:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 202:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 78:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 154:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 114:
-				PIXEL00_1M
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 89:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 90:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 55:
-			case 23:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_1L
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL00_2
-					PIXEL01_6
-					PIXEL02_5
-					PIXEL12_1
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 182:
-			case 150:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-					PIXEL22_1D
-				} else {
-					PIXEL01_1
-					PIXEL02_5
-					PIXEL12_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_2
-				PIXEL21_1
-				break;
-			case 213:
-			case 212:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL02_1U
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL02_2
-					PIXEL12_6
-					PIXEL21_1
-					PIXEL22_5
-				}
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				break;
-			case 241:
-			case 240:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL20_1L
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_1
-					PIXEL20_2
-					PIXEL21_6
-					PIXEL22_5
-				}
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				break;
-			case 236:
-			case 232:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-					PIXEL22_1R
-				} else {
-					PIXEL10_1
-					PIXEL20_5
-					PIXEL21_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				break;
-			case 109:
-			case 105:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_1U
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL00_2
-					PIXEL10_6
-					PIXEL20_5
-					PIXEL21_1
-				}
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL11
-				PIXEL12_1
-				PIXEL22_1M
-				break;
-			case 171:
-			case 43:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-					PIXEL20_1D
-				} else {
-					PIXEL00_5
-					PIXEL01_1
-					PIXEL10_6
-					PIXEL20_2
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 143:
-			case 15:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL02_1R
-					PIXEL10_C
-				} else {
-					PIXEL00_5
-					PIXEL01_6
-					PIXEL02_2
-					PIXEL10_1
-				}
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 124:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 203:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 62:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 211:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 118:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 217:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 110:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 155:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 188:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 185:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 61:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 157:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 103:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 227:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 230:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 199:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 220:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 158:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 234:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1R
-				break;
-			case 242:
-				PIXEL00_1M
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1L
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 59:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 121:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 87:
-				PIXEL00_1L
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 79:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1R
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 122:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 94:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_C
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 218:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 91:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 229:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 167:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 173:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 181:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 186:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 115:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 93:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 206:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 205:
-			case 201:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_1M
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 174:
-			case 46:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_1M
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 179:
-			case 147:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_1M
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 117:
-			case 116:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_1M
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 189:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 231:
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 126:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 219:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-					PIXEL10_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 125:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL00_1U
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL00_2
-					PIXEL10_6
-					PIXEL20_5
-					PIXEL21_1
-				}
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL11
-				PIXEL12_C
-				PIXEL22_1M
-				break;
-			case 221:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL02_1U
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL02_2
-					PIXEL12_6
-					PIXEL21_1
-					PIXEL22_5
-				}
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1M
-				break;
-			case 207:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL02_1R
-					PIXEL10_C
-				} else {
-					PIXEL00_5
-					PIXEL01_6
-					PIXEL02_2
-					PIXEL10_1
-				}
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 238:
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-					PIXEL22_1R
-				} else {
-					PIXEL10_1
-					PIXEL20_5
-					PIXEL21_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL11
-				PIXEL12_1
-				break;
-			case 190:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-					PIXEL22_1D
-				} else {
-					PIXEL01_1
-					PIXEL02_5
-					PIXEL12_6
-					PIXEL22_2
-				}
-				PIXEL00_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1D
-				PIXEL21_1
-				break;
-			case 187:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-					PIXEL20_1D
-				} else {
-					PIXEL00_5
-					PIXEL01_1
-					PIXEL10_6
-					PIXEL20_2
-				}
-				PIXEL02_1M
-				PIXEL11
-				PIXEL12_C
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 243:
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL20_1L
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_1
-					PIXEL20_2
-					PIXEL21_6
-					PIXEL22_5
-				}
-				PIXEL00_1L
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL10_1
-				PIXEL11
-				break;
-			case 119:
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL00_1L
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL00_2
-					PIXEL01_6
-					PIXEL02_5
-					PIXEL12_1
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL20_1L
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 237:
-			case 233:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_2
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 175:
-			case 47:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_2
-				break;
-			case 183:
-			case 151:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_2
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 245:
-			case 244:
-				PIXEL00_2
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 250:
-				PIXEL00_1M
-				PIXEL01_C
-				PIXEL02_1M
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL22_4
-				}
-				break;
-			case 123:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-				}
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 95:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL10_3
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL11
-				PIXEL20_1M
-				PIXEL21_C
-				PIXEL22_1M
-				break;
-			case 222:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 252:
-				PIXEL00_1M
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 249:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL22_4
-				}
-				break;
-			case 235:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-				}
-				PIXEL02_1M
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 111:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 63:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1M
-				break;
-			case 159:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL10_3
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 215:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 246:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 254:
-				PIXEL00_1M
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-				} else {
-					PIXEL01_3
-					PIXEL02_4
-				}
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-				} else {
-					PIXEL10_3
-					PIXEL20_4
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL21_3
-					PIXEL22_2
-				}
-				break;
-			case 253:
-				PIXEL00_1U
-				PIXEL01_1
-				PIXEL02_1U
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 251:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-				} else {
-					PIXEL00_4
-					PIXEL01_3
-				}
-				PIXEL02_1M
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL10_C
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL10_3
-					PIXEL20_2
-					PIXEL21_3
-				}
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL12_C
-					PIXEL22_C
-				} else {
-					PIXEL12_3
-					PIXEL22_4
-				}
-				break;
-			case 239:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				PIXEL02_1R
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_1
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				PIXEL22_1R
-				break;
-			case 127:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL01_C
-					PIXEL10_C
-				} else {
-					PIXEL00_2
-					PIXEL01_3
-					PIXEL10_3
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL02_4
-					PIXEL12_3
-				}
-				PIXEL11
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-					PIXEL21_C
-				} else {
-					PIXEL20_4
-					PIXEL21_3
-				}
-				PIXEL22_1M
-				break;
-			case 191:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1D
-				PIXEL21_1
-				PIXEL22_1D
-				break;
-			case 223:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-					PIXEL10_C
-				} else {
-					PIXEL00_4
-					PIXEL10_3
-				}
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL01_C
-					PIXEL02_C
-					PIXEL12_C
-				} else {
-					PIXEL01_3
-					PIXEL02_2
-					PIXEL12_3
-				}
-				PIXEL11
-				PIXEL20_1M
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL21_C
-					PIXEL22_C
-				} else {
-					PIXEL21_3
-					PIXEL22_4
-				}
-				break;
-			case 247:
-				PIXEL00_1L
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_1
-				PIXEL11
-				PIXEL12_C
-				PIXEL20_1L
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			case 255:
-				if (diffYUV(YUV(4), YUV(2))) {
-					PIXEL00_C
-				} else {
-					PIXEL00_2
-				}
-				PIXEL01_C
-				if (diffYUV(YUV(2), YUV(6))) {
-					PIXEL02_C
-				} else {
-					PIXEL02_2
-				}
-				PIXEL10_C
-				PIXEL11
-				PIXEL12_C
-				if (diffYUV(YUV(8), YUV(4))) {
-					PIXEL20_C
-				} else {
-					PIXEL20_2
-				}
-				PIXEL21_C
-				if (diffYUV(YUV(6), YUV(8))) {
-					PIXEL22_C
-				} else {
-					PIXEL22_2
-				}
-				break;
-			default:
-				break;
-			}
-
-			w1 = w2;
-			w4 = w5;
-			w7 = w8;
-
-			w2 = w3;
-			w5 = w6;
-			w8 = w9;
-
-			q += 3;
-		}
-		p += nextlineSrc - width;
-		q += (nextlineDst - width) * 3;
-	}
-}
-
-void HQ3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
-	if (gBitFormat == 565)
-		HQ3x_implementation<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-	else
-		HQ3x_implementation<Graphics::ColorMasks<555> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
-}
-
-#endif // Assembly version


Commit: 97e176a098f3f0ca90ef551d121d8e403bec12b3
    https://github.com/scummvm/scummvm/commit/97e176a098f3f0ca90ef551d121d8e403bec12b3
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Increase the number of extra pixels required by AdvMame4x

Changed paths:
    graphics/scaler/scalebit.h


diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index 0c74ebfccd..bf17d0ee8a 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -49,7 +49,7 @@ public:
 	virtual uint increaseFactor();
 	virtual uint decreaseFactor();
 	virtual bool canDrawCursor() const { return true; }
-	virtual uint extraPixels() const { return 2; }
+	virtual uint extraPixels() const { return 4; }
 	virtual const char *getName() const;
 	virtual const char *getPrettyName() const;
 };


Commit: 12878afc06ebe33e2571554b0d37f0bfbb9edad4
    https://github.com/scummvm/scummvm/commit/12878afc06ebe33e2571554b0d37f0bfbb9edad4
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix building without scalers

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index bb2627683f..1c9e084bc3 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -832,7 +832,7 @@ void SurfaceSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFo
 			// 0 will currently always be Normal1x scaling
 			setGraphicsMode(0);
 		} else {
-			setGraphicsMode(getGraphicsModeIdByName(ConfMan.get("gfx_mode")));
+			g_system->setGraphicsMode(ConfMan.get("gfx_mode").c_str());
 		}
 	}
 
@@ -1278,7 +1278,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 			int dst_y = r->y + _currentShakeYOffset;
 			int dst_w = 0;
 			int dst_h = 0;
-#ifdef USE_SCALERS
+#ifdef USE_ASPECT
 			int orig_dst_y = 0;
 #endif
 
@@ -1291,7 +1291,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 				if (dst_h > height - dst_y)
 					dst_h = height - dst_y;
 
-#ifdef USE_SCALERS
+#ifdef USE_ASPECT
 				orig_dst_y = dst_y;
 #endif
 				dst_x *= scale1;
@@ -2002,7 +2002,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	_mouseCurState.vHotX = _mouseCurState.hotX;
 	_mouseCurState.vHotY = _mouseCurState.hotY;
 
-#ifdef USE_SCALERS
+#ifdef USE_ASPECT
 	// store original to pass to aspect-correction function later
 	const int rH1 = rH;
 #endif


Commit: e50e010a9e6c32f78ec33dd50a5e8d0cbf8a2349
    https://github.com/scummvm/scummvm/commit/e50e010a9e6c32f78ec33dd50a5e8d0cbf8a2349
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix compilation when building without RGB colour support

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 1c9e084bc3..0c66ab4057 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -714,7 +714,11 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 
 
 	// If the _scalerIndex has changed, change scaler plugins
-	if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _scalerPlugin || _transactionDetails.formatChanged) {
+	if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _scalerPlugin
+#ifdef USE_RGB_COLOR
+		|| _transactionDetails.formatChanged
+#endif
+		) {
 		Graphics::PixelFormat format = convertSDLPixelFormat(_hwScreen->format);
 		if (_scalerPlugin)
 			_scalerPlugin->deinitialize();


Commit: 6a6cdc2bce288c1fc27100d63fdd4c61d4f065e2
    https://github.com/scummvm/scummvm/commit/6a6cdc2bce288c1fc27100d63fdd4c61d4f065e2
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Use override and final keywords

Changed paths:
    graphics/scaler/dotmatrix.h
    graphics/scaler/edge.h
    graphics/scaler/hq.h
    graphics/scaler/normal.h
    graphics/scaler/pm.h
    graphics/scaler/sai.h
    graphics/scaler/scalebit.h
    graphics/scaler/tv.h
    graphics/scalerplugin.h


diff --git a/graphics/scaler/dotmatrix.h b/graphics/scaler/dotmatrix.h
index 15095a1415..27dbc1267d 100644
--- a/graphics/scaler/dotmatrix.h
+++ b/graphics/scaler/dotmatrix.h
@@ -27,16 +27,16 @@
 class DotMatrixPlugin : public ScalerPluginObject {
 public:
 	DotMatrixPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 0; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual void initialize(const Graphics::PixelFormat &format) override;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 0; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 private:
 	// Allocate enough for 32bpp formats
 	uint32 lookup[16];
diff --git a/graphics/scaler/edge.h b/graphics/scaler/edge.h
index 4fbceff118..69e4a5d9ea 100644
--- a/graphics/scaler/edge.h
+++ b/graphics/scaler/edge.h
@@ -28,21 +28,21 @@ class EdgePlugin : public SourceScaler {
 public:
 
 	EdgePlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual bool useOldSource() const { return true; }
-	virtual uint extraPixels() const { return 1; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual void initialize(const Graphics::PixelFormat &format) override;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual bool useOldSource() const override { return true; }
+	virtual uint extraPixels() const override { return 1; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 
 protected:
 
 	virtual void internScale(const uint8 *srcPtr, uint32 srcPitch,
 						   uint8 *dstPtr, uint32 dstPitch,
 						   const uint8 *oldSrcPtr, uint32 oldSrcPitch,
-						   int width, int height, const uint8 *buffer, uint32 bufferPitch);
+						   int width, int height, const uint8 *buffer, uint32 bufferPitch) override;
 
 private:
 
diff --git a/graphics/scaler/hq.h b/graphics/scaler/hq.h
index 7a5833fe0c..563bdb3d44 100644
--- a/graphics/scaler/hq.h
+++ b/graphics/scaler/hq.h
@@ -27,17 +27,17 @@
 class HQPlugin : public ScalerPluginObject {
 public:
 	HQPlugin();
-	virtual void initialize(const Graphics::PixelFormat &format);
-	virtual void deinitialize();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 1; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual void initialize(const Graphics::PixelFormat &format) override;
+	virtual void deinitialize() override;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 1; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 };
 
 
diff --git a/graphics/scaler/normal.h b/graphics/scaler/normal.h
index 970baec3ff..a4a4e38cc7 100644
--- a/graphics/scaler/normal.h
+++ b/graphics/scaler/normal.h
@@ -27,15 +27,15 @@
 class NormalPlugin : public ScalerPluginObject {
 public:
 	NormalPlugin();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return true; }
-	virtual uint extraPixels() const { return 0; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return true; }
+	virtual uint extraPixels() const override { return 0; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 };
 
 
diff --git a/graphics/scaler/pm.h b/graphics/scaler/pm.h
index 4f9e7966a5..d24dd42bd9 100644
--- a/graphics/scaler/pm.h
+++ b/graphics/scaler/pm.h
@@ -28,13 +28,13 @@ class PMPlugin : public ScalerPluginObject {
 public:
 	PMPlugin();
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 1; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 1; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 };
 
 #endif
diff --git a/graphics/scaler/sai.h b/graphics/scaler/sai.h
index eb549e00af..c509b5842d 100644
--- a/graphics/scaler/sai.h
+++ b/graphics/scaler/sai.h
@@ -27,43 +27,43 @@
 class SAIPlugin : public ScalerPluginObject {
 public:
 	SAIPlugin();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 2; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 2; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 };
 
 class SuperSAIPlugin : public ScalerPluginObject {
 public:
 	SuperSAIPlugin();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 2; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 2; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 };
 
 class SuperEaglePlugin : public ScalerPluginObject {
 public:
 	SuperEaglePlugin();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 2; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 2; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 protected:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 };
 
 #endif
diff --git a/graphics/scaler/scalebit.h b/graphics/scaler/scalebit.h
index bf17d0ee8a..3ccdf71c45 100644
--- a/graphics/scaler/scalebit.h
+++ b/graphics/scaler/scalebit.h
@@ -45,13 +45,13 @@ class AdvMamePlugin : public ScalerPluginObject {
 public:
 	AdvMamePlugin();
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return true; }
-	virtual uint extraPixels() const { return 4; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return true; }
+	virtual uint extraPixels() const override { return 4; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 };
 
 #endif
diff --git a/graphics/scaler/tv.h b/graphics/scaler/tv.h
index 70a5650186..c56b97d4f9 100644
--- a/graphics/scaler/tv.h
+++ b/graphics/scaler/tv.h
@@ -27,15 +27,15 @@
 class TVPlugin : public ScalerPluginObject {
 public:
 	TVPlugin();
-	virtual uint increaseFactor();
-	virtual uint decreaseFactor();
-	virtual bool canDrawCursor() const { return false; }
-	virtual uint extraPixels() const { return 0; }
-	virtual const char *getName() const;
-	virtual const char *getPrettyName() const;
+	virtual uint increaseFactor() override;
+	virtual uint decreaseFactor() override;
+	virtual bool canDrawCursor() const override { return false; }
+	virtual uint extraPixels() const override { return 0; }
+	virtual const char *getName() const override;
+	virtual const char *getPrettyName() const override;
 private:
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
-							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y);
+							uint8 *dstPtr, uint32 dstPitch, int width, int height, int x, int y) override;
 	template<typename ColorMask>
 	void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
 			uint32 dstPitch, int width, int height);
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index 1d50bb98fd..e44eaaab53 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -161,16 +161,16 @@ public:
 	SourceScaler();
 	virtual ~SourceScaler();
 
-	virtual void deinitialize();
+	virtual void deinitialize() override;
 
-	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding);
+	virtual void setSource(const byte *src, uint pitch, int width, int height, int padding) final;
 
-	virtual void enableSource(bool enable) { _enable = enable; }
+	virtual void enableSource(bool enable) final { _enable = enable; }
 
 protected:
 
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
-	                         uint32 dstPitch, int width, int height, int x, int y);
+	                         uint32 dstPitch, int width, int height, int x, int y) final;
 
 	/**
 	 * Scalers must implement this function. It will be called by oldSrcScale.


Commit: 99acdb00f9bbeae045c21782dc274a00b2527bf9
    https://github.com/scummvm/scummvm/commit/99acdb00f9bbeae045c21782dc274a00b2527bf9
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix crash when switching from OpenGL to Edge3x

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 0c66ab4057..6b45edc0b5 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -729,16 +729,16 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		}
 		_scalerPlugin = &_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>();
 		_scalerPlugin->initialize(format);
-		_extraPixels = _scalerPlugin->extraPixels();
-		_useOldSrc = _scalerPlugin->useOldSource();
-		if (_useOldSrc) {
-			_scalerPlugin->enableSource(true);
-			_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
-										_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
-		}
 	}
 
 	_scalerPlugin->setFactor(_videoMode.scaleFactor);
+	_extraPixels = _scalerPlugin->extraPixels();
+	_useOldSrc = _scalerPlugin->useOldSource();
+	if (_useOldSrc) {
+		_scalerPlugin->enableSource(true);
+		_scalerPlugin->setSource((byte *)_tmpscreen->pixels, _tmpscreen->pitch,
+									_videoMode.screenWidth, _videoMode.screenHeight, _maxExtraPixels);
+	}
 
 	// Blit everything to the screen
 	_forceRedraw = true;


Commit: b68dbf9eec6d059a88e57d3318ca721c4dec4624
    https://github.com/scummvm/scummvm/commit/b68dbf9eec6d059a88e57d3318ca721c4dec4624
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Ensure that _bufferedOutput is resized when the scale factor changes

Changed paths:
    graphics/scaler/edge.cpp
    graphics/scaler/hq.cpp
    graphics/scaler/normal.cpp
    graphics/scaler/scalebit.cpp
    graphics/scalerplugin.cpp
    graphics/scalerplugin.h


diff --git a/graphics/scaler/edge.cpp b/graphics/scaler/edge.cpp
index 580026e427..28851816ff 100644
--- a/graphics/scaler/edge.cpp
+++ b/graphics/scaler/edge.cpp
@@ -3604,13 +3604,13 @@ void EdgePlugin::internScale(const uint8 *srcPtr, uint32 srcPitch,
 
 uint EdgePlugin::increaseFactor() {
 	if (_factor == 2)
-		++_factor;
+		setFactor(_factor + 1);
 	return _factor;
 }
 
 uint EdgePlugin::decreaseFactor() {
 	if (_factor == 3)
-		--_factor;
+		setFactor(_factor - 1);
 	return _factor;
 }
 
diff --git a/graphics/scaler/hq.cpp b/graphics/scaler/hq.cpp
index da254fb075..68419979ae 100644
--- a/graphics/scaler/hq.cpp
+++ b/graphics/scaler/hq.cpp
@@ -5067,13 +5067,13 @@ void HQPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 
 uint HQPlugin::increaseFactor() {
 	if (_factor < 3)
-		++_factor;
+		setFactor(_factor + 1);
 	return _factor;
 }
 
 uint HQPlugin::decreaseFactor() {
 	if (_factor > 2)
-		--_factor;
+		setFactor(_factor - 1);
 	return _factor;
 }
 
diff --git a/graphics/scaler/normal.cpp b/graphics/scaler/normal.cpp
index 358e0e5422..a627a819be 100644
--- a/graphics/scaler/normal.cpp
+++ b/graphics/scaler/normal.cpp
@@ -205,7 +205,7 @@ void NormalPlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 uint NormalPlugin::increaseFactor() {
 #ifdef USE_SCALERS
 	if (_factor < 4)
-		++_factor;
+		setFactor(_factor + 1);
 #endif
 	return _factor;
 }
@@ -213,7 +213,7 @@ uint NormalPlugin::increaseFactor() {
 uint NormalPlugin::decreaseFactor() {
 #ifdef USE_SCALERS
 	if (_factor > 1)
-		--_factor;
+		setFactor(_factor - 1);
 #endif
 	return _factor;
 }
diff --git a/graphics/scaler/scalebit.cpp b/graphics/scaler/scalebit.cpp
index e7b74c5edc..eecceef6e8 100644
--- a/graphics/scaler/scalebit.cpp
+++ b/graphics/scaler/scalebit.cpp
@@ -369,13 +369,13 @@ void AdvMamePlugin::scaleIntern(const uint8 *srcPtr, uint32 srcPitch,
 
 uint AdvMamePlugin::increaseFactor() {
 	if (_factor < 4)
-		++_factor;
+		setFactor(_factor + 1);
 	return _factor;
 }
 
 uint AdvMamePlugin::decreaseFactor() {
 	if (_factor > 2)
-		--_factor;
+		setFactor(_factor - 1);
 	return _factor;
 }
 
diff --git a/graphics/scalerplugin.cpp b/graphics/scalerplugin.cpp
index 9f677dfb82..a5b7c69235 100644
--- a/graphics/scalerplugin.cpp
+++ b/graphics/scalerplugin.cpp
@@ -60,7 +60,7 @@ void ScalerPluginObject::scale(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstP
 	}
 }
 
-SourceScaler::SourceScaler() : _oldSrc(NULL), _enable(false) {
+SourceScaler::SourceScaler() : _width(0), _height(0), _oldSrc(NULL), _enable(false) {
 }
 
 SourceScaler::~SourceScaler() {
@@ -77,13 +77,27 @@ void SourceScaler::setSource(const byte *src, uint pitch, int width, int height,
 	if (_oldSrc != NULL)
 		delete[] _oldSrc;
 
+	_width = width;
+	_height = height;
 	_padding = padding;
+
 	// Give _oldSrc same pitch
 	int size = (height + padding * 2) * pitch;
 	_oldSrc = new byte[size];
 	memset(_oldSrc, 0, size);
 
-	_bufferedOutput.create(width * _factor, height * _factor, _format);
+	_bufferedOutput.create(_width * _factor, _height * _factor, _format);
+}
+
+uint SourceScaler::setFactor(uint factor) {
+	uint oldFactor = _factor;
+	_factor = factor;
+
+	if (factor != oldFactor && _width != 0 && _height != 0) {
+		_bufferedOutput.create(_width * _factor, _height * _factor, _format);
+	}
+
+	return oldFactor;
 }
 
 void SourceScaler::scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
diff --git a/graphics/scalerplugin.h b/graphics/scalerplugin.h
index e44eaaab53..44b446f364 100644
--- a/graphics/scalerplugin.h
+++ b/graphics/scalerplugin.h
@@ -167,6 +167,8 @@ public:
 
 	virtual void enableSource(bool enable) final { _enable = enable; }
 
+	virtual uint setFactor(uint factor) final;
+
 protected:
 
 	virtual void scaleIntern(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr,
@@ -186,7 +188,7 @@ protected:
 
 private:
 
-	int _padding;
+	int _width, _height, _padding;
 	bool _enable;
 	byte *_oldSrc;
 	Graphics::Surface _bufferedOutput;


Commit: 4df5e9ed51c9a717799bb2e264d694499f30de6b
    https://github.com/scummvm/scummvm/commit/4df5e9ed51c9a717799bb2e264d694499f30de6b
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix crash with small cursors when using AdvMame4x

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 6b45edc0b5..aeba3112c3 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2150,7 +2150,9 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	// otherwise use the Normal scaler
 #ifdef USE_SCALERS
 	if (!_cursorDontScale) {
-		if (_scalerPlugin->canDrawCursor()) {
+		// HACK: AdvMame4x requires a height of at least 4 pixels, so we
+		// fall back on the Normal scaler when a smaller cursor is supplied.
+		if (_scalerPlugin->canDrawCursor() && (uint)_mouseCurState.h >= _extraPixels) {
 #endif
             _scalerPlugin->scale(
                     (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,


Commit: 21c1f0d007cb4368f0b32b387ae0d12297c634df
    https://github.com/scummvm/scummvm/commit/21c1f0d007cb4368f0b32b387ae0d12297c634df
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Restore keyboard shortcuts for switching scalers

Changed paths:
    backends/graphics/sdl/sdl-graphics.cpp
    backends/graphics/sdl/sdl-graphics.h
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp
index 9074b33f5f..8edce253b1 100644
--- a/backends/graphics/sdl/sdl-graphics.cpp
+++ b/backends/graphics/sdl/sdl-graphics.cpp
@@ -433,32 +433,15 @@ Common::Keymap *SdlGraphicsManager::getKeymap() {
 	act->setCustomBackendActionEvent(kActionDecreaseScaleFactor);
 	keymap->addAction(act);
 
-#ifdef USE_SCALERS
-	struct ActionEntry {
-		const char *id;
-		const char *description;
-	};
-	static const ActionEntry filters[] = {
-			{ "FLT1", _s("Switch to nearest neighbour scaling") },
-			{ "FLT2", _s("Switch to AdvMame 2x/3x scaling")     },
-#ifdef USE_HQ_SCALERS
-			{ "FLT3", _s("Switch to HQ 2x/3x scaling")          },
-#endif
-			{ "FLT4", _s("Switch to 2xSai scaling")             },
-			{ "FLT5", _s("Switch to Super2xSai scaling")        },
-			{ "FLT6", _s("Switch to SuperEagle scaling")        },
-			{ "FLT7", _s("Switch to TV 2x scaling")             },
-			{ "FLT8", _s("Switch to DotMatrix scaling")         }
-	};
-
-	for (uint i = 0; i < ARRAYSIZE(filters); i++) {
-		act = new Action(filters[i].id, _(filters[i].description));
-		act->addDefaultInputMapping(String::format("C+A+%d", i + 1));
-		act->addDefaultInputMapping(String::format("C+A+KP%d", i + 1));
-		act->setCustomBackendActionEvent(kActionSetScaleFilter1 + i);
-		keymap->addAction(act);
-	}
-#endif
+	act = new Action("FLTN", _("Switch to the next scaler"));
+	act->addDefaultInputMapping("C+A+0");
+	act->setCustomBackendActionEvent(kActionNextScaleFilter);
+	keymap->addAction(act);
+
+	act = new Action("FLTP", _("Switch to the previous scaler"));
+	act->addDefaultInputMapping("C+A+9");
+	act->setCustomBackendActionEvent(kActionPreviousScaleFilter);
+	keymap->addAction(act);
 
 	return keymap;
 }
diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h
index 3d2baefc7f..92fc4d32bf 100644
--- a/backends/graphics/sdl/sdl-graphics.h
+++ b/backends/graphics/sdl/sdl-graphics.h
@@ -147,14 +147,8 @@ protected:
 		kActionCycleStretchMode,
 		kActionIncreaseScaleFactor,
 		kActionDecreaseScaleFactor,
-		kActionSetScaleFilter1,
-		kActionSetScaleFilter2,
-		kActionSetScaleFilter3,
-		kActionSetScaleFilter4,
-		kActionSetScaleFilter5,
-		kActionSetScaleFilter6,
-		kActionSetScaleFilter7,
-		kActionSetScaleFilter8
+		kActionNextScaleFilter,
+		kActionPreviousScaleFilter
 	};
 
 	virtual int getGraphicsModeScale(int mode) const = 0;
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index aeba3112c3..39c0fc6306 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -2495,6 +2495,42 @@ int findGraphicsMode(uint factor, ScalerPluginObject &plugin) {
 	return -1;
 }
 
+void SurfaceSdlGraphicsManager::handleScalerHotkeys(int factor) {
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+	bool sizeChanged = _videoMode.scaleFactor != factor;
+#endif
+
+	int newMode = findGraphicsMode(factor, _scalerPlugins[_scalerIndex]->get<ScalerPluginObject>());
+	if (newMode >= 0) {
+		beginGFXTransaction();
+			setGraphicsMode(newMode);
+		endGFXTransaction();
+#ifdef USE_OSD
+		const char *newScalerName = _scalerPlugin->getPrettyName();
+		if (newScalerName) {
+			const Common::U32String message = Common::U32String::format(
+				"%S %s%d\n%d x %d -> %d x %d",
+				_("Active graphics filter:").c_str(),
+				newScalerName,
+				_scalerPlugin->getFactor(),
+				_videoMode.screenWidth, _videoMode.screenHeight,
+				_hwScreen->w, _hwScreen->h);
+			displayMessageOnOSD(message);
+		}
+#endif
+
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+		if (sizeChanged) {
+			// Forcibly resizing the window here since a user switching scaler
+			// size will not normally cause the window to update
+			_window->createOrUpdateWindow(_hwScreen->w, _hwScreen->h, _lastFlags);
+		}
+#endif
+
+		internUpdateScreen();
+	}
+}
+
 bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 	if (event.type != Common::EVENT_CUSTOM_BACKEND_ACTION_START) {
 		return SdlGraphicsManager::notifyEvent(event);
@@ -2573,6 +2609,32 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 	}
 #endif
 
+	case kActionIncreaseScaleFactor:
+		handleScalerHotkeys(_scalerPlugin->increaseFactor());
+		return true;
+
+	case kActionDecreaseScaleFactor:
+		handleScalerHotkeys(_scalerPlugin->decreaseFactor());
+		return true;
+
+	case kActionNextScaleFilter:
+		_scalerIndex++;
+		if (_scalerIndex >= _scalerPlugins.size()) {
+			_scalerIndex = 0;
+		}
+
+		handleScalerHotkeys(_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>().getFactor());
+		return true;
+
+	case kActionPreviousScaleFilter:
+		if (_scalerIndex == 0) {
+			_scalerIndex = _scalerPlugins.size();
+		}
+		_scalerIndex--;
+
+		handleScalerHotkeys(_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>().getFactor());
+		return true;
+
 	default:
 		return SdlGraphicsManager::notifyEvent(event);
 	}
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 371795529a..42296eb556 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -431,6 +431,7 @@ protected:
 
 private:
 	void setFullscreenMode(bool enable);
+	void handleScalerHotkeys(int factor);
 
 	/**
 	 * Converts the given point from the overlay's coordinate space to the


Commit: 621c4c2e9e88f104ca258b82400c720cdb14b507
    https://github.com/scummvm/scummvm/commit/621c4c2e9e88f104ca258b82400c720cdb14b507
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix implementation of getGraphicsModeScale

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 39c0fc6306..a6d8a3202d 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -635,37 +635,10 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() {
 #endif
 
 int SurfaceSdlGraphicsManager::getGraphicsModeScale(int mode) const {
-	int scale;
-	switch (mode) {
-	case GFX_NORMAL:
-		scale = 1;
-		break;
-#ifdef USE_SCALERS
-	case GFX_DOUBLESIZE:
-	case GFX_2XSAI:
-	case GFX_SUPER2XSAI:
-	case GFX_SUPEREAGLE:
-	case GFX_ADVMAME2X:
-	case GFX_TV2X:
-	case GFX_DOTMATRIX:
-#ifdef USE_HQ_SCALERS
-	case GFX_HQ2X:
-#endif
-		scale = 2;
-		break;
-	case GFX_TRIPLESIZE:
-	case GFX_ADVMAME3X:
-#ifdef USE_HQ_SCALERS
-	case GFX_HQ3X:
-#endif
-		scale = 3;
-		break;
-#endif
-	default:
-		scale = -1;
-	}
+	if (mode >= (int)s_supportedGraphicsModes->size())
+		return -1;
 
-	return scale;
+	return (*s_supportedGraphicsModesData)[mode].scaleFactor;
 }
 
 bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode, uint flags) {


Commit: 035f5798c4bfc3d6555954e2cdf5c91b4a5fc18b
    https://github.com/scummvm/scummvm/commit/035f5798c4bfc3d6555954e2cdf5c91b4a5fc18b
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Remove the old graphics mode enum

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index a6d8a3202d..e9976f3808 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -152,23 +152,13 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 		_enableFocusRectDebugCode = ConfMan.getBool("use_sdl_debug_focusrect");
 #endif
 
-#if !defined(__SYMBIAN32__)
-
-#if defined(USE_SCALERS)
-	_videoMode.mode = GFX_DOUBLESIZE;
-	_videoMode.scaleFactor = 2;
-#else // for small screen platforms
-	_videoMode.mode = GFX_NORMAL;
-	_videoMode.scaleFactor = 1;
-#endif
-#if defined(USE_ASPECT)
+#if !defined(__SYMBIAN32__) && defined(USE_ASPECT)
 	_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
 	_videoMode.desiredAspectRatio = getDesiredAspectRatio();
 #else // for small screen platforms
 	_videoMode.aspectRatioCorrection = false;
 #endif
 
-#endif
 	_normalPlugin = &ScalerMan.findScalerPlugin("normal")->get<ScalerPluginObject>();
 	assert(_normalPlugin);
 	_scalerPlugin = NULL;
@@ -183,6 +173,9 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 
 	if (!s_supportedGraphicsModes)
 		initGraphicsModes();
+
+	_videoMode.mode = s_defaultGraphicsMode;
+	_videoMode.scaleFactor = getGraphicsModeScale(_videoMode.mode);
 }
 
 SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 42296eb556..11783495d0 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -40,21 +40,6 @@
 #define USE_SDL_DEBUG_FOCUSRECT
 #endif
 
-enum {
-	GFX_NORMAL = 0,
-	GFX_DOUBLESIZE = 1,
-	GFX_TRIPLESIZE = 2,
-	GFX_2XSAI = 3,
-	GFX_SUPER2XSAI = 4,
-	GFX_SUPEREAGLE = 5,
-	GFX_ADVMAME2X = 6,
-	GFX_ADVMAME3X = 7,
-	GFX_HQ2X = 8,
-	GFX_HQ3X = 9,
-	GFX_TV2X = 10,
-	GFX_DOTMATRIX = 11
-};
-
 
 class AspectRatio {
 	int _kw, _kh;


Commit: f6c8207684a76686862880489156dfda2a3238e1
    https://github.com/scummvm/scummvm/commit/f6c8207684a76686862880489156dfda2a3238e1
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Minor cleanup to the aspect ratio code

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index e9976f3808..1ebe84a07c 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1280,12 +1280,8 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 			r->h = dst_h * scale1;
 
 #ifdef USE_ASPECT
-			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible) {
-				if (_useOldSrc)
-					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
-				else
-					r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
-			}
+			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible)
+				r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
 #endif
 		}
 		SDL_UnlockSurface(srcSurf);


Commit: 40ee8ae527d75368f44ff77e0fb7ddb8e169640c
    https://github.com/scummvm/scummvm/commit/40ee8ae527d75368f44ff77e0fb7ddb8e169640c
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Fix filtering with SDL 1.2

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 1ebe84a07c..36601b6828 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -258,10 +258,8 @@ bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) const {
 	case OSystem::kFeatureAspectRatioCorrection:
 		return _videoMode.aspectRatioCorrection;
 #endif
-#if SDL_VERSION_ATLEAST(2, 0, 0)
 	case OSystem::kFeatureFilteringMode:
 		return _videoMode.filtering;
-#endif
 	case OSystem::kFeatureCursorPalette:
 		return !_cursorPaletteDisabled;
 	default:
@@ -2523,7 +2521,6 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 		return true;
 	}
 
-#if SDL_VERSION_ATLEAST(2, 0, 0)
 	case kActionToggleFilteredScaling:
 		beginGFXTransaction();
 			setFeatureState(OSystem::kFeatureFilteringMode, !_videoMode.filtering);
@@ -2538,7 +2535,6 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
 		_forceRedraw = true;
 		internUpdateScreen();
 		return true;
-#endif
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	case kActionCycleStretchMode: {


Commit: b596ebf8b858680cc8b9b299ca06de52cebf604e
    https://github.com/scummvm/scummvm/commit/b596ebf8b858680cc8b9b299ca06de52cebf604e
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
GRAPHICS: Remove use of gBitFormat from the aspect ratio code

Changed paths:
  R graphics/scaler.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    graphics/module.mk
    graphics/scaler/aspect.cpp
    graphics/scaler/aspect.h
    graphics/scaler/downscaler.cpp


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 36601b6828..d3c4eeac18 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -1279,7 +1279,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() {
 
 #ifdef USE_ASPECT
 			if (_videoMode.aspectRatioCorrection && orig_dst_y < height && !_overlayVisible)
-				r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering);
+				r->h = stretch200To240((uint8 *) _hwScreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1, _videoMode.filtering, convertSDLPixelFormat(_hwScreen->format));
 #endif
 		}
 		SDL_UnlockSurface(srcSurf);
@@ -1750,7 +1750,7 @@ void SurfaceSdlGraphicsManager::clearOverlay() {
 	if (_videoMode.aspectRatioCorrection)
 		stretch200To240((uint8 *)_overlayscreen->pixels, _overlayscreen->pitch,
 						_videoMode.overlayWidth, _videoMode.screenHeight * _videoMode.scaleFactor, 0, 0, 0,
-						_videoMode.filtering);
+						_videoMode.filtering, _overlayFormat);
 #endif
 	SDL_UnlockSurface(_tmpscreen);
 	SDL_UnlockSurface(_overlayscreen);
@@ -2139,7 +2139,7 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 
 #ifdef USE_ASPECT
 	if (!_cursorDontScale && _videoMode.aspectRatioCorrection)
-		stretch200To240Nearest((uint8 *)_mouseSurface->pixels, _mouseSurface->pitch, rW, rH1, 0, 0, 0);
+		stretch200To240Nearest((uint8 *)_mouseSurface->pixels, _mouseSurface->pitch, rW, rH1, 0, 0, 0, convertSDLPixelFormat(_mouseSurface->format));
 #endif
 
 	SDL_UnlockSurface(_mouseSurface);
diff --git a/graphics/module.mk b/graphics/module.mk
index 1baf22ff9d..3902844873 100644
--- a/graphics/module.mk
+++ b/graphics/module.mk
@@ -30,7 +30,6 @@ MODULE_OBJS := \
 	pixelformat.o \
 	primitives.o \
 	renderer.o \
-	scaler.o \
 	scalerplugin.o \
 	scaler/thumbnail_intern.o \
 	screen.o \
diff --git a/graphics/scaler.cpp b/graphics/scaler.cpp
deleted file mode 100644
index bbd82b3833..0000000000
--- a/graphics/scaler.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/* 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "graphics/scaler/intern.h"
-#include "graphics/scaler/scalebit.h"
-#include "common/util.h"
-#include "common/system.h"
-#include "common/textconsole.h"
-
-int gBitFormat = 565;
diff --git a/graphics/scaler/aspect.cpp b/graphics/scaler/aspect.cpp
index 1c2844955f..71608a9af8 100644
--- a/graphics/scaler/aspect.cpp
+++ b/graphics/scaler/aspect.cpp
@@ -206,17 +206,17 @@ void makeRectStretchable(int &x, int &y, int &w, int &h, bool interpolate) {
  * through real2Aspect(srcY + height - 1).
  */
 
-int stretch200To240Nearest(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY) {
+int stretch200To240Nearest(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY, const Graphics::PixelFormat &format) {
 	int maxDstY = real2Aspect(origSrcY + height - 1);
 	int y;
-	const uint8 *startSrcPtr = buf + srcX * 2 + (srcY - origSrcY) * pitch;
-	uint8 *dstPtr = buf + srcX * 2 + maxDstY * pitch;
+	const uint8 *startSrcPtr = buf + srcX * format.bytesPerPixel + (srcY - origSrcY) * pitch;
+	uint8 *dstPtr = buf + srcX * format.bytesPerPixel + maxDstY * pitch;
 
 	for (y = maxDstY; y >= srcY; y--) {
 		const uint8 *srcPtr = startSrcPtr + aspect2Real(y) * pitch;
 		if (srcPtr == dstPtr)
 			break;
-		memcpy(dstPtr, srcPtr, sizeof(uint16) * width);
+		memcpy(dstPtr, srcPtr, format.bytesPerPixel * width);
 		dstPtr -= pitch;
 	}
 
@@ -259,18 +259,15 @@ int stretch200To240Interpolated(uint8 *buf, uint32 pitch, int width, int height,
 	return 1 + maxDstY - srcY;
 }
 
-int stretch200To240(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY, bool interpolate) {
+int stretch200To240(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY, bool interpolate, const Graphics::PixelFormat &format) {
 #if ASPECT_MODE != kSuperFastAndUglyAspectMode
-	extern int gBitFormat;
-	if (interpolate) {
-		if (gBitFormat == 565)
+	if (interpolate && format.bytesPerPixel == 2) {
+		if (format.gLoss == 2)
 			return stretch200To240Interpolated<Graphics::ColorMasks<565> >(buf, pitch, width, height, srcX, srcY, origSrcY);
-		else // gBitFormat == 555
+		else if (format.gLoss == 3)
 			return stretch200To240Interpolated<Graphics::ColorMasks<555> >(buf, pitch, width, height, srcX, srcY, origSrcY);
-	} else {
-#endif
-		return stretch200To240Nearest(buf, pitch, width, height, srcX, srcY, origSrcY);
-#if ASPECT_MODE != kSuperFastAndUglyAspectMode
 	}
 #endif
+
+	return stretch200To240Nearest(buf, pitch, width, height, srcX, srcY, origSrcY, format);
 }
diff --git a/graphics/scaler/aspect.h b/graphics/scaler/aspect.h
index ee1dfc1460..05ed7c1af5 100644
--- a/graphics/scaler/aspect.h
+++ b/graphics/scaler/aspect.h
@@ -56,8 +56,9 @@ int stretch200To240(uint8 *buf,
                     int srcX,
                     int srcY,
                     int origSrcY,
-                    bool interpolate);
+                    bool interpolate,
+                    const Graphics::PixelFormat &format);
 
-int stretch200To240Nearest(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY);
+int stretch200To240Nearest(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY, const Graphics::PixelFormat &format);
 
 #endif
diff --git a/graphics/scaler/downscaler.cpp b/graphics/scaler/downscaler.cpp
index 35c4389c28..8cbf9d4dca 100644
--- a/graphics/scaler/downscaler.cpp
+++ b/graphics/scaler/downscaler.cpp
@@ -22,6 +22,8 @@
 #include "graphics/scaler/downscaler.h"
 #include "graphics/scaler/intern.h"
 
+int gBitFormat = 565;
+
 #ifdef USE_ARM_SCALER_ASM
 extern "C" {
 	void DownscaleAllByHalfARM(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height, int mask, int round);
@@ -32,8 +34,6 @@ void DownscaleAllByHalf(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uin
 	static const int roundingconstants[] = { 0x00200802, 0x00201002 };
 	static const int redbluegreenMasks[] = { 0x03E07C1F, 0x07E0F81F };
 
-	extern int gBitFormat;
-
 	const int maskUsed = (gBitFormat == 565);
 	DownscaleAllByHalfARM(srcPtr, srcPitch, dstPtr, dstPitch, width, height, redbluegreenMasks[maskUsed], roundingconstants[maskUsed]);
 }
@@ -64,7 +64,6 @@ void DownscaleAllByHalfTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dst
 }
 
 void DownscaleAllByHalf(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height) {
-	extern int gBitFormat;
 	if (gBitFormat == 565)
 		DownscaleAllByHalfTemplate<Graphics::ColorMasks<565> >(srcPtr, srcPitch, dstPtr, dstPitch, width, height);
 	else


Commit: 8aa3e5313263481353334bff62c06c4174c1aa38
    https://github.com/scummvm/scummvm/commit/8aa3e5313263481353334bff62c06c4174c1aa38
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-03-15T16:48:40+02:00

Commit Message:
SDL: Replace the use of the normal scaler for the cursor

Changed paths:
    backends/graphics/surfacesdl/surfacesdl-graphics.cpp
    backends/graphics/surfacesdl/surfacesdl-graphics.h


diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index d3c4eeac18..9a80ea7b99 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -35,6 +35,7 @@
 #ifdef USE_RGB_COLOR
 #include "common/list.h"
 #endif
+#include "graphics/conversion.h"
 #include "graphics/font.h"
 #include "graphics/fontman.h"
 #include "graphics/scaler.h"
@@ -159,8 +160,6 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
 	_videoMode.aspectRatioCorrection = false;
 #endif
 
-	_normalPlugin = &ScalerMan.findScalerPlugin("normal")->get<ScalerPluginObject>();
-	assert(_normalPlugin);
 	_scalerPlugin = NULL;
 	_scalerIndex = 0;
 	_maxExtraPixels = ScalerMan.getMaxExtraPixels();
@@ -687,10 +686,6 @@ void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
 		if (_scalerPlugin)
 			_scalerPlugin->deinitialize();
 
-		if (&_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>() != _normalPlugin) {
-			// _normalPlugin might be needed and needs to be initialized
-			_normalPlugin->initialize(format);
-		}
 		_scalerPlugin = &_scalerPlugins[_scalerIndex]->get<ScalerPluginObject>();
 		_scalerPlugin->initialize(format);
 	}
@@ -2108,34 +2103,29 @@ void SurfaceSdlGraphicsManager::blitCursor() {
 	// If possible, use the same scaler for the cursor as for the rest of
 	// the game. This only works well with the non-blurring scalers so we
 	// otherwise use the Normal scaler
-#ifdef USE_SCALERS
 	if (!_cursorDontScale) {
+#ifdef USE_SCALERS
 		// HACK: AdvMame4x requires a height of at least 4 pixels, so we
 		// fall back on the Normal scaler when a smaller cursor is supplied.
 		if (_scalerPlugin->canDrawCursor() && (uint)_mouseCurState.h >= _extraPixels) {
-#endif
             _scalerPlugin->scale(
                     (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
                     _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
                     _mouseCurState.w, _mouseCurState.h, 0, 0);
-#ifdef USE_SCALERS
-		} else {
-			int oldFactor = _normalPlugin->setFactor(_videoMode.scaleFactor);
-			_normalPlugin->scale(
-				(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
-				_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-				_mouseCurState.w, _mouseCurState.h, 0, 0);
-			_normalPlugin->setFactor(oldFactor);
+		} else
+#endif
+		{
+			Graphics::scaleBlit((byte *)_mouseSurface->pixels, (const byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
+			                    _mouseSurface->pitch, _mouseOrigSurface->pitch,
+				                _mouseCurState.w * _videoMode.scaleFactor, _mouseCurState.h * _videoMode.scaleFactor,
+			                    _mouseCurState.w, _mouseCurState.h, convertSDLPixelFormat(_mouseSurface->format));
+
 		}
 	} else {
-		int oldScaleFactor = _scalerPlugin->setFactor(1);
-		_scalerPlugin->scale(
-			(byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
-			_mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
-			_mouseCurState.w, _mouseCurState.h, 0, 0);
-		_scalerPlugin->setFactor(oldScaleFactor);
+		Graphics::copyBlit((byte *)_mouseSurface->pixels, (const byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * _maxExtraPixels + _maxExtraPixels * _mouseOrigSurface->format->BytesPerPixel,
+		                   _mouseSurface->pitch, _mouseOrigSurface->pitch,
+		                   _mouseCurState.w, _mouseCurState.h, _mouseSurface->format->BytesPerPixel);
 	}
-#endif
 
 #ifdef USE_ASPECT
 	if (!_cursorDontScale && _videoMode.aspectRatioCorrection)
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 11783495d0..4afba7f371 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -316,7 +316,6 @@ protected:
 
 	const PluginList &_scalerPlugins;
 	ScalerPluginObject *_scalerPlugin;
-	ScalerPluginObject *_normalPlugin;
 	uint _scalerIndex;
 	uint _maxExtraPixels;
 	uint _extraPixels;




More information about the Scummvm-git-logs mailing list