Fixed issue with 256 colors palette

I wasn't checking for color indices <16 so first 16 colors in palette
were completely wrong and that was causing wrong indices to be
calculated.
This commit is contained in:
Casey 2022-02-05 20:11:21 +03:00
parent c9807d3e64
commit 21e7310ae1
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
4 changed files with 75 additions and 49 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
yaitaa
yaitaa.exe
prepared.png
obj/*.o

View File

@ -18,58 +18,58 @@ palette_t c_palette_256 = {
palette_t c_palette_ansi_discord = {
.n_colors = 8,
.palette = {
{ 0x4f, 0x54, 0x5c, 0 },
{ 0xd1, 0x31, 0x35, 0 },
{ 0x85, 0x99, 0x00, 0 },
{ 0xb5, 0x89, 0x00, 0 },
{ 0x26, 0x8b, 0xd2, 0 },
{ 0xd3, 0x36, 0x82, 0 },
{ 0xd3, 0x36, 0x82, 0 },
{ 0xff, 0xff, 0xff, 0 },
{ 0x4f, 0x54, 0x5c, 255 },
{ 0xd1, 0x31, 0x35, 255 },
{ 0x85, 0x99, 0x00, 255 },
{ 0xb5, 0x89, 0x00, 255 },
{ 0x26, 0x8b, 0xd2, 255 },
{ 0xd3, 0x36, 0x82, 255 },
{ 0xd3, 0x36, 0x82, 255 },
{ 0xff, 0xff, 0xff, 255 },
}
};
palette_t c_palette_ansi_vga = {
.n_colors = 16,
.palette = {
{ 0, 0, 0, 0 },
{ 170, 0, 0, 0 },
{ 0, 170, 0, 0 },
{ 170, 85, 0, 0 },
{ 0, 0, 170, 0 },
{ 170, 0, 170, 0 },
{ 0, 170, 170, 0 },
{ 170, 170, 170, 0 },
{ 85, 85, 85, 0 },
{ 255, 85, 85, 0 },
{ 85, 255, 85, 0 },
{ 255, 255, 85, 0 },
{ 85, 85, 255, 0 },
{ 255, 85, 255, 0 },
{ 85, 255, 255, 0 },
{ 255, 255, 255, 0 }
{ 0, 0, 0, 255 },
{ 170, 0, 0, 255 },
{ 0, 170, 0, 255 },
{ 170, 85, 0, 255 },
{ 0, 0, 170, 255 },
{ 170, 0, 170, 255 },
{ 0, 170, 170, 255 },
{ 170, 170, 170, 255 },
{ 85, 85, 85, 255 },
{ 255, 85, 85, 255 },
{ 85, 255, 85, 255 },
{ 255, 255, 85, 255 },
{ 85, 85, 255, 255 },
{ 255, 85, 255, 255 },
{ 85, 255, 255, 255 },
{ 255, 255, 255, 255 }
}
};
palette_t c_palette_ansi_xterm = {
.n_colors = 16,
.palette = {
{ 0, 0, 0, 0 },
{ 205, 0, 0, 0 },
{ 0, 205, 0, 0 },
{ 205, 205, 0, 0 },
{ 0, 0, 238, 0 },
{ 205, 0, 205, 0 },
{ 0, 205, 205, 0 },
{ 229, 229, 229, 0 },
{ 127, 127, 127, 0 },
{ 255, 0, 0, 0 },
{ 0, 255, 0, 0 },
{ 255, 255, 0, 0 },
{ 0, 0, 252, 0 },
{ 255, 0, 255, 0 },
{ 0, 255, 255, 0 },
{ 255, 255, 255, 0 },
{ 0, 0, 0, 255 },
{ 205, 0, 0, 255 },
{ 0, 205, 0, 255 },
{ 205, 205, 0, 255 },
{ 0, 0, 238, 255 },
{ 205, 0, 205, 255 },
{ 0, 205, 205, 255 },
{ 229, 229, 229, 255 },
{ 127, 127, 127, 255 },
{ 255, 0, 0, 255 },
{ 0, 255, 0, 255 },
{ 255, 255, 0, 255 },
{ 0, 0, 252, 255 },
{ 255, 0, 255, 255 },
{ 0, 255, 255, 255 },
{ 255, 255, 255, 255 },
}
};
@ -89,7 +89,7 @@ int closest_color(palette_t pal, rgba8 color)
{
rgba8 pal_color = pal.palette[i];
int32_t distance = color_difference(pal_color, color);
if (distance < min_distance)
if (distance <= min_distance)
{
min_distance = distance;
nearest = i;
@ -102,7 +102,9 @@ rgba8 pal256_to_rgb(palette_t pal, int ndx)
{
(void)pal;
rgba8 out = { 0, 0, 0, 255 };
if (ndx >= 232)
if (ndx < 16)
return pal.palette[ndx];
else if (ndx >= 232)
{
int l = (ndx - 232) * 255 / 24;
out.r = out.g = out.b = l;

View File

@ -23,10 +23,17 @@ image_t *image_resize(image_t *img, int width, int height)
res->width = width;
res->height = height;
res->pixels = calloc(width * height, sizeof(rgba8));
stbir_resize_uint8((const unsigned char *)img->pixels,
img->width, img->height, 0,
(unsigned char *)res->pixels,
res->width, res->height, 0, STBI_rgb_alpha);
if (img->width != width && img->height != height)
{
stbir_resize_uint8((const unsigned char *)img->pixels,
img->width, img->height, 0,
(unsigned char *)res->pixels,
res->width, res->height, 0, STBI_rgb_alpha);
}
else
{
memcpy(res->pixels, img->pixels, width * height * sizeof(rgba8));
}
return res;
}

View File

@ -30,17 +30,32 @@ int main(int argc, char **argv)
int res = parse_args(argc, argv, &args);
if (res == 1) return 0;
if (res < 0) return -res;
asc_state_t state;
res = prepare_state(argc, argv, args, &state);
if (res == 1) return 0;
if (res < 0) return -res;
if (args.verbose)
{
fprintf(stderr, "Source image size: %dx%d\n",
state.source_image->width,
state.source_image->height);
}
asc_handler_t handler = asc_handlers[args.mode];
if (handler.prepare == NULL)
c_fatal(12, "this mode is not implemented yet");
handler.prepare(&state);
if (args.verbose)
{
fprintf(stderr, "Resized image size: %dx%d\n",
state.image->width,
state.image->height);
}
handler.main(state);
return 0;
}