git.y1.nz

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.cpp

      1 #include <vector>
      2 #include <string>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <set>
      6 #include <stdio.h>
      7 #include <fstream>
      8 #include <cstdint>
      9 
     10 #include "lodepng.h"
     11 #include "mttile.h"
     12 
     13 #include "cmp_int_color.h"
     14 
     15 using namespace std;
     16 
     17 #include "png2asset.h"
     18 #include "png_image.h"
     19 #include "image_utils.h"
     20 
     21 SetPal GetPaletteColors(const PNGImage& image, bool isPackModeSGB, int x, int y, int w, int h)
     22 {
     23     SetPal ret;
     24 
     25     // If this is SGB mode then every 16 color palette must have a
     26     // transparent color as the first entry.
     27     //
     28     // For most SGB Borders the Game Boy screen region is transparent and those
     29     // tiles will all be uniform and therefore using only the first palette where
     30     // they already have a transparent entry. Which means any additional SGB palettes
     31     // will lack the needed transparent entry at their start.
     32     //
     33     // The solution is to automatically insert a transparent color to every palette
     34     // created for an SGB tile. If another transparent entry is present then they
     35     // will get automatically merged (regardless of RGB value *fully* transparent
     36     // colors are now considered identical. see the CmpIntColor functor for details).
     37     //
     38     // The use of RGB(255,255,255) (0xFFFFFF) for the color is due to colors being sorted
     39     // light to dark, and historical use of transparent white in existing examples.
     40     if (isPackModeSGB) {
     41         ret.insert( RGBA32_TRANSPARENT_WHITE );
     42     }
     43 
     44     // Now scan the tile for colors
     45     for(int j = y; j < (y + h); ++j)
     46     {
     47         for(int i = x; i < (x + w); ++i)
     48         {
     49             const unsigned char* color = &image.data[(j * image.w + i) * RGBA32_SZ];
     50             int color_int = RGBA32(color[RGBA8_R], color[RGBA8_G], color[RGBA8_B], color[RGBA8_ALPHA]);
     51             ret.insert(color_int);
     52         }
     53     }
     54 
     55     for(SetPal::iterator it = ret.begin(); it != ret.end(); ++it)
     56     {
     57         if(it != ret.begin() && ((0xFF & *it) != 0xFF)) //ret.begin() should be the only one transparent
     58             printf("Warning: found more than one transparent color in tile at x:%d, y:%d of size w:%d, h:%d\n", x, y, w, h);
     59     }
     60 
     61     return ret;
     62 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.