gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2asset/cmp_int_color.h
1 #pragma once
2
3 #include <set>
4 #include "png_image.h"
5
6 using namespace std;
7
8 //Functor to compare entries in SetPal
9 struct CmpIntColor {
10 // Values accessed as bytes here will be in ABGR8 byte array format. This is
11 // due to RGBA:32 packed ints being accessed as bytes on what is assumed to
12 // be a little-endian system.
13 // So, [3] = Red ... [0] = Alpha
14
15 bool operator() (unsigned int const& c1, unsigned int const& c2) const
16 {
17 unsigned char* c1_ptr = (unsigned char*)&c1;
18 unsigned char* c2_ptr = (unsigned char*)&c2;
19
20 //Compare alpha first, transparent color is considered smaller
21 if(c1_ptr[ABGR8_ALPHA] != c2_ptr[ABGR8_ALPHA])
22 {
23 return c1_ptr[ABGR8_ALPHA] < c2_ptr[ABGR8_ALPHA];
24 }
25 else
26 {
27 // If a color is fully transparent then consider it identical to any other
28 // fully transparent color and do not insert.
29 // If this test is reached then alpha channels are identical for the two entries
30 // and entry c1 is the one being tested for insertion.
31 if (c1_ptr[ABGR8_ALPHA] == ALPHA_FULLY_TRANSPARENT) return false;
32
33 // Do a compare with luminance in upper bits, and original rgb24 in lower bits.
34 // This prefers luminance, but considers RGB values for equal-luminance cases to
35 // make sure the compare functor satisifed the strictly weak ordering requirement
36 uint64_t lum_1 = (unsigned int)(c1_ptr[ABGR8_R] * 299 + c1_ptr[ABGR8_G] * 587 + c1_ptr[ABGR8_B] * 114);
37 uint64_t lum_2 = (unsigned int)(c2_ptr[ABGR8_R] * 299 + c2_ptr[ABGR8_G] * 587 + c2_ptr[ABGR8_B] * 114);
38 uint64_t rgb_1 = RGB24(c1_ptr[ABGR8_R], c1_ptr[ABGR8_G], c1_ptr[ABGR8_B]);
39 uint64_t rgb_2 = RGB24(c2_ptr[ABGR8_R], c2_ptr[ABGR8_G], c2_ptr[ABGR8_B]);
40 uint64_t all_1 = (lum_1 << 24) | rgb_1;
41 uint64_t all_2 = (lum_2 << 24) | rgb_2;
42 return all_1 > all_2;
43 }
44 }
45 };
46
47 //This set will keep colors in the palette ordered based on their grayscale values to ensure they look good on DMG
48 //This assumes the palette used in DMG will be 00 01 10 11
49 typedef set< unsigned int, CmpIntColor > SetPal;
50
51
52 SetPal GetPaletteColors(const PNGImage& image, bool packModeSGB, int x, int y, int w, int h);
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.