2013-05-10 23:02:43 +00:00
|
|
|
/*
|
|
|
|
convert.c
|
|
|
|
|
|
|
|
Image/color conversion routins (RGB to paletted)
|
|
|
|
|
|
|
|
Copyright (C) 2013 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2013/5/10
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/alloc.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/image.h"
|
|
|
|
#include "QF/mathlib.h"
|
|
|
|
|
|
|
|
struct colcache_s {
|
|
|
|
struct colcache_s *next;
|
|
|
|
hashtab_t *tab;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct colcache_color_s {
|
|
|
|
struct colcache_color_s *next;
|
|
|
|
byte rgb[3];
|
|
|
|
byte col;
|
|
|
|
} colcache_color_t;
|
|
|
|
|
2023-03-05 07:26:37 +00:00
|
|
|
ALLOC_STATE (colcache_t, colcache);
|
|
|
|
ALLOC_STATE (colcache_color_t, colcache_color);
|
2013-05-10 23:02:43 +00:00
|
|
|
|
|
|
|
static colcache_color_t *
|
|
|
|
colcache_new_color (const byte *rgb, byte ind)
|
|
|
|
{
|
|
|
|
colcache_color_t *col;
|
|
|
|
ALLOC (256, colcache_color_t, colcache_color, col);
|
|
|
|
VectorCopy (rgb, col->rgb);
|
|
|
|
col->col = ind;
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
colcache_free_color (void *_col, void *unused)
|
|
|
|
{
|
|
|
|
colcache_color_t *col = (colcache_color_t *) _col;
|
|
|
|
FREE (colcache_color, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uintptr_t
|
|
|
|
colcache_get_hash (const void *_col, void *unused)
|
|
|
|
{
|
|
|
|
colcache_color_t *col = (colcache_color_t *) _col;
|
|
|
|
uintptr_t r, g, b;
|
|
|
|
r = col->rgb[0];
|
|
|
|
g = col->rgb[1];
|
|
|
|
b = col->rgb[2];
|
|
|
|
return (r << 8) ^ (g << 4) ^ b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
colcache_compare (const void *_cola, const void *_colb, void *unused)
|
|
|
|
{
|
|
|
|
colcache_color_t *cola = (colcache_color_t *) _cola;
|
|
|
|
colcache_color_t *colb = (colcache_color_t *) _colb;
|
|
|
|
|
|
|
|
return VectorCompare (cola->rgb, colb->rgb);
|
|
|
|
}
|
|
|
|
|
|
|
|
colcache_t *
|
|
|
|
ColorCache_New (void)
|
|
|
|
{
|
|
|
|
colcache_t *cache;
|
|
|
|
|
|
|
|
ALLOC (16, colcache_t, colcache, cache);
|
2021-03-25 13:01:31 +00:00
|
|
|
cache->tab = Hash_NewTable (1023, 0, colcache_free_color, 0, 0);
|
2013-05-10 23:02:43 +00:00
|
|
|
Hash_SetHashCompare (cache->tab, colcache_get_hash, colcache_compare);
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ColorCache_Delete (colcache_t *cache)
|
|
|
|
{
|
|
|
|
Hash_DelTable (cache->tab);
|
|
|
|
FREE (colcache, cache);
|
|
|
|
}
|
|
|
|
|
2023-03-05 08:14:25 +00:00
|
|
|
void
|
|
|
|
ColorCache_Shutdown (void)
|
|
|
|
{
|
|
|
|
ALLOC_FREE_BLOCKS (colcache);
|
|
|
|
ALLOC_FREE_BLOCKS (colcache_color);
|
|
|
|
}
|
|
|
|
|
2013-05-10 23:02:43 +00:00
|
|
|
byte
|
|
|
|
ConvertColor (const byte *rgb, const byte *pal, colcache_t *cache)
|
|
|
|
{
|
|
|
|
//FIXME slow!
|
|
|
|
int dist[3];
|
|
|
|
int d, bestd = 256 * 256 * 3, bestc = -1;
|
|
|
|
int i;
|
|
|
|
colcache_color_t *col;
|
|
|
|
|
|
|
|
if (cache) {
|
|
|
|
colcache_color_t search;
|
|
|
|
VectorCopy (rgb, search.rgb);
|
|
|
|
col = Hash_FindElement (cache->tab, &search);
|
|
|
|
if (col)
|
|
|
|
return col->col;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
VectorSubtract (pal + i * 3, rgb, dist);
|
|
|
|
d = DotProduct (dist, dist);
|
|
|
|
if (d < bestd) {
|
|
|
|
bestd = d;
|
|
|
|
bestc = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cache) {
|
|
|
|
col = colcache_new_color (rgb, bestc);
|
|
|
|
Hash_AddElement (cache->tab, col);
|
|
|
|
}
|
|
|
|
return bestc;
|
|
|
|
}
|
|
|
|
|
|
|
|
tex_t *
|
|
|
|
ConvertImage (const tex_t *tex, const byte *pal)
|
|
|
|
{
|
|
|
|
tex_t *new;
|
|
|
|
int pixels;
|
|
|
|
int bpp = 3;
|
|
|
|
int i;
|
|
|
|
colcache_t *cache;
|
|
|
|
|
|
|
|
pixels = tex->width * tex->height;
|
2021-03-25 13:01:31 +00:00
|
|
|
new = malloc (sizeof (tex_t) + pixels);
|
|
|
|
new->data = (byte *) (new + 1);
|
2013-05-10 23:02:43 +00:00
|
|
|
new->width = tex->width;
|
|
|
|
new->height = tex->height;
|
|
|
|
new->format = tex_palette;
|
|
|
|
new->palette = pal;
|
|
|
|
switch (tex->format) {
|
|
|
|
case tex_palette:
|
2021-03-25 13:01:31 +00:00
|
|
|
case tex_l: // will not work as expected FIXME
|
|
|
|
case tex_a: // will not work as expected FIXME
|
|
|
|
case tex_frgba: // will not work as expected FIXME
|
2013-05-10 23:02:43 +00:00
|
|
|
memcpy (new->data, tex->data, pixels);
|
|
|
|
break;
|
2021-03-25 13:01:31 +00:00
|
|
|
case tex_la: // will not work as expected FIXME
|
2013-05-10 23:02:43 +00:00
|
|
|
for (i = 0; i < pixels; i++)
|
|
|
|
new->data[i] = tex->data[i * 2];
|
|
|
|
break;
|
|
|
|
case tex_rgba:
|
|
|
|
bpp = 4;
|
|
|
|
case tex_rgb:
|
|
|
|
cache = ColorCache_New ();
|
|
|
|
for (i = 0; i < pixels; i++)
|
|
|
|
new->data[i] = ConvertColor (tex->data + i * bpp, pal, cache);
|
|
|
|
ColorCache_Delete (cache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|