1 // ****************************************************************************
2 // * This file is part of the HqMAME project. It is distributed under         *
3 // * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0          *
4 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved          *
5 // ****************************************************************************
6 
7 #ifndef XBRZ_HEADER_3847894708239054
8 #define XBRZ_HEADER_3847894708239054
9 
10 #include <cstddef> //size_t
11 #include <cstdint> //uint32_t
12 #include <limits>
13 #include "config.h"
14 
15 namespace xbrz
16 {
17 /*
18 -------------------------------------------------------------------------
19 | xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju |
20 -------------------------------------------------------------------------
21 using a modified approach of xBR:
22 http://board.byuu.org/viewtopic.php?f=10&t=2248
23 - new rule set preserving small image features
24 - highly optimized for performance
25 - support alpha channel
26 - support multithreading
27 - support 64-bit architectures
28 - support processing image slices
29 - support scaling up to 6xBRZ
30 */
31 
32 enum class ColorFormat //from high bits -> low bits, 8 bit per channel
33 {
34     RGB,  //8 bit for each red, green, blue, upper 8 bits unused
35     ARGB, //including alpha channel, BGRA byte order on little-endian machines
36 };
37 
38 /*
39 -> map source (srcWidth * srcHeight) to target (scale * width x scale * height) image, optionally processing a half-open slice of rows [yFirst, yLast) only
40 -> support for source/target pitch in bytes!
41 -> if your emulator changes only a few image slices during each cycle (e.g. DOSBox) then there's no need to run xBRZ on the complete image:
42    Just make sure you enlarge the source image slice by 2 rows on top and 2 on bottom (this is the additional range the xBRZ algorithm is using during analysis)
43    Caveat: If there are multiple changed slices, make sure they do not overlap after adding these additional rows in order to avoid a memory race condition
44    in the target image data if you are using multiple threads for processing each enlarged slice!
45 
46 THREAD-SAFETY: - parts of the same image may be scaled by multiple threads as long as the [yFirst, yLast) ranges do not overlap!
47                - there is a minor inefficiency for the first row of a slice, so avoid processing single rows only; suggestion: process 8-16 rows at least
48 */
49 void scale(size_t factor, //valid range: 2 - 6
50            const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight,
51            ColorFormat colFmt,
52            const ScalerCfg& cfg = ScalerCfg(),
53            int yFirst = 0, int yLast = std::numeric_limits<int>::max()); //slice of source image
54 
55 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
56                           uint32_t* trg, int trgWidth, int trgHeight);
57 
58 enum SliceType
59 {
60     NN_SCALE_SLICE_SOURCE,
61     NN_SCALE_SLICE_TARGET,
62 };
63 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitch, //pitch in bytes!
64                           uint32_t* trg, int trgWidth, int trgHeight, int trgPitch,
65                           SliceType st, int yFirst, int yLast);
66 
67 //parameter tuning
68 bool equalColorTest(uint32_t col1, uint32_t col2, ColorFormat colFmt, double luminanceWeight, double equalColorTolerance);
69 
70 
71 
72 
73 
74 //########################### implementation ###########################
75 inline
nearestNeighborScale(const uint32_t * src,int srcWidth,int srcHeight,uint32_t * trg,int trgWidth,int trgHeight)76 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
77                           uint32_t* trg, int trgWidth, int trgHeight)
78 {
79     nearestNeighborScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
80                          trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
81                          NN_SCALE_SLICE_TARGET, 0, trgHeight);
82 }
83 }
84 
85 #endif
86