Skip to content

Embedded palettes can be mis-sorted if some palettes share colors #1704

Closed as not planned
@Rangi42

Description

@Rangi42

Here's an example test.png:

test.png

Its eight-color indexed PLTE defines two GBC palettes:

% pngcheck -p test.png
File: test.png (159 bytes)
  PLTE chunk: 8 palette entries
    0:  (255,255,255) = (0xff,0xff,0xff)
    1:  (255,222,  0) = (0xff,0xde,0x00)
    2:  (  0, 33,255) = (0x00,0x21,0xff)
    3:  (  0,  0,  0) = (0x00,0x00,0x00)
    4:  (255,255,255) = (0xff,0xff,0xff)
    5:  (255,222,  0) = (0xff,0xde,0x00)
    6:  (181,  0,255) = (0xb5,0x00,0xff)
    7:  (  0,  0,  0) = (0x00,0x00,0x00)
OK: test.png (16x8, 8-bit palette, non-interlaced, -24.2%).

These should stay sorted as-is when converting. However, the second palette's last two colors, black and purple, are swapped:

% rgbgfx -o test.2bpp -p test.pal -q test.palmap test.png
% xxd test.pal                                           
00000000: ff7f 7f03 807c 0000 ff7f 7f03 0000 167c  .....|.........|

The problem is that each 4-color GBC palette is sorted by finding its colors in the PNG's whole indexed palette. So if an earlier GBC palette shares some of the colors, those colors will get sorted before others in this palette, even if this palette has those colors later.

What we want:

% xxd test.pal                                             
00000000: ff7f 7f03 807c 0000 ff7f 7f03 167c 0000  .....|.......|..

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected behavior / crashes; to be fixed ASAP!rgbgfxThis affects RGBGFX

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions