The Battle for Wesnoth  1.19.0-dev
xbrz.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10  See the COPYING file for more details.
11 
12  This is a derivative work of the xBRZ component of the HqMAME project
13  by Zenju. The original Licensing statement follows, indented with //
14  The primary changes are, syntactic to make it compile with C99+Boost,
15  and to make it handle an alpha channel in the image in a manner proper
16  for SDL.
17 
18  It is not possible to extend the MAME 'special exception' to all of
19  the Battle for Wesnoth project, however, the special exception is
20  granted for my derivative forms of this work.
21 */
22 
23 // ****************************************************************************
24 // * This file is part of the HqMAME project. It is distributed under *
25 // * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
26 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
27 // * *
28 // * Additionally and as a special exception, the author gives permission *
29 // * to link the code of this program with the MAME library (or with modified *
30 // * versions of MAME that use the same license as MAME), and distribute *
31 // * linked combinations including the two. You must obey the GNU General *
32 // * Public License in all respects for all of the code used other than MAME. *
33 // * If you modify this file, you may extend this exception to your version *
34 // * of the file, but you are not obligated to do so. If you do not wish to *
35 // * do so, delete this exception statement from your version. *
36 // ****************************************************************************
37 
38 #ifndef XBRZ_HEADER_3847894708239054
39 #define XBRZ_HEADER_3847894708239054
40 
41 #include <cstddef> //size_t
42 #include <cstdint> //uint32_t
43 #include <limits>
44 #include "config.hpp"
45 
46 namespace xbrz
47 {
48 /*
49 -------------------------------------------------------------------------
50 | xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju |
51 -------------------------------------------------------------------------
52 using a modified approach of xBR:
53 http://board.byuu.org/viewtopic.php?f=10&t=2248
54 - new rule set preserving small image features
55 - support multithreading
56 - support 64 bit architectures
57 - support processing image slices
58 */
59 
60 /*
61 -> map source (srcWidth * srcHeight) to target (scale * width x scale * height) image, optionally processing a half-open slice of rows [yFirst, yLast) only
62 -> color format: ARGB (BGRA byte order), alpha channel unused
63 -> support for source/target pitch in bytes!
64 -> 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:
65  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)
66  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
67  if you are using multiple threads for processing each enlarged slice!
68 
69 THREAD-SAFETY: - parts of the same image may be scaled by multiple threads as long as the [yFirst, yLast) ranges do not overlap!
70  - there is a minor inefficiency for the first row of a slice, so avoid processing single rows only
71 
72 
73 */
74 void scale(size_t factor, //valid range: 2 - 5
75  const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight,
76  const ScalerCfg& cfg = ScalerCfg(),
77  int yFirst = 0, int yLast = std::numeric_limits<int>::max()); //slice of source image
78 
79 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
80  uint32_t* trg, int trgWidth, int trgHeight);
81 
83 {
86 };
87 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitch, //pitch in bytes!
88  uint32_t* trg, int trgWidth, int trgHeight, int trgPitch,
89  SliceType st, int yFirst, int yLast);
90 
91 //parameter tuning
92 bool equalColor(uint32_t col1, uint32_t col2, double luminanceWeight, double equalColorTolerance);
93 
94 
95 
96 
97 
98 //########################### implementation ###########################
99 inline
100 void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
101  uint32_t* trg, int trgWidth, int trgHeight)
102 {
103  nearestNeighborScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
104  trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
105  NN_SCALE_SLICE_TARGET, 0, trgHeight);
106 }
107 }
108 
109 #endif
Definition: config.hpp:44
bool equalColor(uint32_t col1, uint32_t col2, double luminanceWeight, double equalColorTolerance)
Definition: xbrz.cpp:1206
SliceType
Definition: xbrz.hpp:83
@ NN_SCALE_SLICE_TARGET
Definition: xbrz.hpp:85
@ NN_SCALE_SLICE_SOURCE
Definition: xbrz.hpp:84
void nearestNeighborScale(const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight)
Definition: xbrz.hpp:100
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Definition: xbrz.cpp:1189