-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaler.cpp
More file actions
144 lines (134 loc) · 3.85 KB
/
scaler.cpp
File metadata and controls
144 lines (134 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* REminiscence - Flashback interpreter
* Copyright (C) 2005-2018 Gregory Montoir (cyx@users.sourceforge.net)
*/
#include "scaler.h"
#include "dynlib.h"
#include "util.h"
static void scale2x(uint32_t *dst, int dstPitch, const uint32_t *src, int srcPitch, int w, int h) {
const int dstPitch2 = dstPitch * 2;
for (int y = 0; y < h; ++y) {
uint32_t *p = dst;
for (int x = 0; x < w; ++x, p += 2) {
const uint32_t E = *(src + x);
const uint32_t B = (y == 0) ? E : *(src + x - srcPitch);
const uint32_t D = (x == 0) ? E : *(src + x - 1);
const uint32_t F = (x == w - 1) ? E : *(src + x + 1);
const uint32_t H = (y == h - 1) ? E : *(src + x + srcPitch);
if (B != H && D != F) {
*(p) = D == B ? D : E;
*(p + 1) = B == F ? F : E;
*(p + dstPitch) = D == H ? D : E;
*(p + dstPitch + 1) = H == F ? F : E;
} else {
*(p) = E;
*(p + 1) = E;
*(p + dstPitch) = E;
*(p + dstPitch + 1) = E;
}
}
dst += dstPitch2;
src += srcPitch;
}
}
static void scale3x(uint32_t *dst, int dstPitch, const uint32_t *src, int srcPitch, int w, int h) {
const int dstPitch2 = dstPitch * 2;
const int dstPitch3 = dstPitch * 3;
for (int y = 0; y < h; ++y) {
uint32_t *p = dst;
for (int x = 0; x < w; ++x, p += 3) {
const uint32_t E = *(src + x);
const uint32_t B = (y == 0) ? E : *(src + x - srcPitch);
const uint32_t D = (x == 0) ? E : *(src + x - 1);
const uint32_t F = (x == w - 1) ? E : *(src + x + 1);
const uint32_t H = (y == h - 1) ? E : *(src + x + srcPitch);
uint32_t A, C;
if (y == 0) {
A = D;
C = F;
} else {
A = (x == 0) ? B : *(src + x - srcPitch - 1);
C = (x == w - 1) ? B : *(src + x - srcPitch + 1);
}
uint32_t G, I;
if (y == h - 1) {
G = D;
I = F;
} else {
G = (x == 0) ? H : *(src + x + srcPitch - 1);
I = (x == w - 1) ? H : *(src + x + srcPitch + 1);
}
if (B != H && D != F) {
*(p) = D == B ? D : E;
*(p + 1) = (D == B && E != C) || (B == F && E != A) ? B : E;
*(p + 2) = B == F ? F : E;
*(p + dstPitch) = (D == B && E != G) || (D == B && E != A) ? D : E;
*(p + dstPitch + 1) = E;
*(p + dstPitch + 2) = (B == F && E != I) || (H == F && E != C) ? F : E;
*(p + dstPitch2) = D == H ? D : E;
*(p + dstPitch2 + 1) = (D == H && E != I) || (H == F && E != G) ? H : E;
*(p + dstPitch2 + 2) = H == F ? F : E;
} else {
*(p) = E;
*(p + 1) = E;
*(p + 2) = E;
*(p + dstPitch) = E;
*(p + dstPitch + 1) = E;
*(p + dstPitch + 2) = E;
*(p + dstPitch2) = E;
*(p + dstPitch2 + 1) = E;
*(p + dstPitch2 + 2) = E;
}
}
dst += dstPitch3;
src += srcPitch;
}
}
static void scale4x(uint32_t *dst, int dstPitch, const uint32_t *src, int srcPitch, int w, int h) {
static struct {
uint32_t *ptr;
int w, h, pitch;
int size;
} buf;
const int size = (w * 2) * (h * 2) * sizeof(uint32_t);
if (buf.size < size) {
free(buf.ptr);
buf.size = size;
buf.w = w * 2;
buf.h = h * 2;
buf.pitch = buf.w;
buf.ptr = (uint32_t *)malloc(buf.size);
if (!buf.ptr) {
error("Unable to allocate scale4x intermediate buffer");
}
}
scale2x(buf.ptr, buf.pitch, src, srcPitch, w, h);
scale2x(dst, dstPitch, buf.ptr, buf.pitch, buf.w, buf.h);
}
static void scaleNx(int factor, uint32_t *dst, int dstPitch, const uint32_t *src, int srcPitch, int w, int h) {
switch (factor) {
case 2:
return scale2x(dst, dstPitch, src, srcPitch, w, h);
case 3:
return scale3x(dst, dstPitch, src, srcPitch, w, h);
case 4:
return scale4x(dst, dstPitch, src, srcPitch, w, h);
}
}
const Scaler _internalScaler = {
SCALER_TAG,
"scaleNx",
2, 4,
scaleNx,
};
static DynLib *dynLib;
static const char *kSoSym = "getScaler";
const Scaler *findScaler(const char *name) {
dynLib = new DynLib(name);
void *symbol = dynLib->getSymbol(kSoSym);
if (symbol) {
typedef const Scaler *(*GetScalerProc)();
return ((GetScalerProc)symbol)();
}
return 0;
}