Skip to content

Commit 9da4ebd

Browse files
committed
More image viewer improvements
1 parent 2fcdeaf commit 9da4ebd

File tree

20 files changed

+335
-363
lines changed

20 files changed

+335
-363
lines changed

gbapatcher/arm9/source/gbaswitch.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ void loadGbaBorder(void) {
2828
for (uint i = 0; i < image.size()/4; i++) {
2929
image[(i*4)+3] = 0;
3030
if (alternatePixel) {
31-
if (image[(i*4)] < 0xFC) {
31+
if (image[(i*4)] >= 0x4 && image[(i*4)] < 0xFC) {
3232
image[(i*4)] += 0x4;
3333
image[(i*4)+3] |= BIT(0);
3434
}
35-
if (image[(i*4)+1] < 0xFC) {
35+
if (image[(i*4)+1] >= 0x4 && image[(i*4)+1] < 0xFC) {
3636
image[(i*4)+1] += 0x4;
3737
image[(i*4)+3] |= BIT(1);
3838
}
39-
if (image[(i*4)+2] < 0xFC) {
39+
if (image[(i*4)+2] >= 0x4 && image[(i*4)+2] < 0xFC) {
4040
image[(i*4)+2] += 0x4;
4141
image[(i*4)+3] |= BIT(2);
4242
}
@@ -61,13 +61,13 @@ void loadGbaBorder(void) {
6161
image[(i*4)+2] -= 0x4;
6262
}
6363
} else {
64-
if (image[(i*4)] < 0xFC) {
64+
if (image[(i*4)] >= 0x4 && image[(i*4)] < 0xFC) {
6565
image[(i*4)] += 0x4;
6666
}
67-
if (image[(i*4)+1] < 0xFC) {
67+
if (image[(i*4)+1] >= 0x4 && image[(i*4)+1] < 0xFC) {
6868
image[(i*4)+1] += 0x4;
6969
}
70-
if (image[(i*4)+2] < 0xFC) {
70+
if (image[(i*4)+2] >= 0x4 && image[(i*4)+2] < 0xFC) {
7171
image[(i*4)+2] += 0x4;
7272
}
7373
}

imageview/arm9/source/graphics/color.h

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,51 @@
77
using std::min;
88
using std::max;
99

10-
u16 grayscale(u16 val) {
11-
u16 newVal = ((val >> 10) & 31) | (val & 31 << 5) | (val & 31) << 10 | BIT(15);
12-
13-
u8 b, g, r, max, min;
14-
b = ((newVal) >> 10) & 31;
15-
g = ((newVal) >> 5) & 31;
16-
r = (newVal)&31;
17-
// Value decomposition of hsv
18-
max = (b > g) ? b : g;
19-
max = (max > r) ? max : r;
20-
21-
// Desaturate
22-
min = (b < g) ? b : g;
23-
min = (min < r) ? min : r;
24-
max = (max + min) / 2;
25-
26-
newVal = 32768 | (max << 10) | (max << 5) | (max);
27-
28-
b = ((newVal) >> 10) & (31);
29-
g = ((newVal) >> 5) & (31);
30-
r = (newVal)&31;
31-
32-
return 32768 | (b << 10) | (g << 5) | (r);
10+
u16 rgb8ToRgb565(const u8 r, const u8 g, const u8 b) {
11+
const u16 green = (g >> 2) << 5;
12+
u16 color = r >> 3 | (b >> 3) << 10;
13+
if (green & BIT(5)) {
14+
color |= BIT(15);
15+
}
16+
for (int gBit = 6; gBit <= 10; gBit++) {
17+
if (green & BIT(gBit)) {
18+
color |= BIT(gBit-1);
19+
}
20+
}
21+
return color;
3322
}
3423

3524

3625
/**
3726
* Adapted from https://stackoverflow.com/questions/18937701/
3827
* applies alphablending with the given
39-
* RGB555 foreground, RGB555 background, and alpha from
28+
* RGB565 foreground, RGB565 background, and alpha from
4029
* 0 to 255 (0, 1.0).
4130
* The lower the alpha the more transparent, but
4231
* this function does not produce good results when blending
4332
* less than 128 (50%) alpha due to overflow.
4433
*/
45-
u16 alphablend(const u16 fg, const u16 bg, const u8 alpha) {
46-
47-
u16 fg_b, fg_g, fg_r, bg_b, bg_g, bg_r;
48-
fg_b = ((fg) >> 10) & 31;
49-
fg_g = ((fg) >> 5) & 31;
50-
fg_r = (fg)&31;
51-
52-
bg_b = ((bg) >> 10) & 31;
53-
bg_g = ((bg) >> 5) & 31;
54-
bg_r = (bg)&31;
34+
u16 rgb8ToRgb565_alphablend(const u8 fg_r, const u8 fg_g, const u8 fg_b, const u8 bg_r, const u8 bg_g, const u8 bg_b, const u8 alpha, const u8 alphaG) {
5535

5636
// Alpha blend components
5737
u16 out_r = fg_r * alpha + bg_r * (255 - alpha);
58-
u16 out_g = fg_g * alpha + bg_g * (255 - alpha);
38+
u16 out_g = fg_g * alphaG + bg_g * (255 - alphaG);
5939
u16 out_b = fg_b * alpha + bg_b * (255 - alpha);
6040
out_r = min((out_r + 1 + (out_r >> 8)) >> 8, 255);
6141
out_g = min((out_g + 1 + (out_g >> 8)) >> 8, 255);
6242
out_b = min((out_b + 1 + (out_b >> 8)) >> 8, 255);
6343

64-
return (u16) (BIT(15) | (out_b << 10) | (out_g << 5) | out_r);
65-
}
66-
67-
68-
69-
/**
70-
* Adapted from https://stackoverflow.com/questions/18937701/
71-
* applies alphablending with the given
72-
* RGB555 foreground, RGB555 background, and alpha from
73-
* 0 to 255 (0, 1.0).
74-
*/
75-
u16 blend(const u16 fg, const u16 bg) {
76-
77-
u8 fg_b, fg_g, fg_r, bg_b, bg_g, bg_r;
78-
fg_b = ((fg) >> 10) & 31;
79-
fg_g = ((fg) >> 5) & 31;
80-
fg_r = (fg)&31;
81-
82-
bg_b = ((bg) >> 10) & 31;
83-
bg_g = ((bg) >> 5) & 31;
84-
bg_r = (bg)&31;
85-
86-
87-
// Alpha blend components
88-
unsigned out_r = min((fg_r + bg_r) >> 1, 255);
89-
unsigned out_g = min((fg_g + bg_g) >> 1, 255);
90-
unsigned out_b = min((fg_b + bg_b) >> 1, 255);
91-
return (u16) ((out_b << 10) | (out_g << 5) | out_r | BIT(15));
44+
const u16 green = (out_g >> 2) << 5;
45+
u16 color = out_r >> 3 | (out_b >> 3) << 10;
46+
if (green & BIT(5)) {
47+
color |= BIT(15);
48+
}
49+
for (int gBit = 6; gBit <= 10; gBit++) {
50+
if (green & BIT(gBit)) {
51+
color |= BIT(gBit-1);
52+
}
53+
}
54+
return color;
9255
}
9356

9457

0 commit comments

Comments
 (0)