1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-11-05 04:49:44 +02:00
ben-blinkenlights/ubb-vga/ccube.c
Werner Almesberger 6558f56de6 ubb-vga2: replaced threshold-based color mapping with color cube model
- ubb-vga.h (ccube_init, ccube_map), ccube.c: color mapper based on
  proximity in color cube
- grabfb.c (pattern, grabfb), ppmimg.c (pattern, convert): use the color
  cube mapper instead of inferios threshold-based mapping
- ubb-vga2.c (session): initialize the color cube
- ubb-vga.h (thres), grabfb.c (thres), ubb-vga2.c (usage, main): removed
  the threshold along with the option (-l) to set it
- Makefile (OBJS): added ccube.o
2011-04-28 01:18:03 -03:00

59 lines
1.1 KiB
C

/*
* ccube.c - Color mapper based on color cube model
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
*
* 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.
*/
#include <stdint.h>
#include "ubb-vga.h"
#define W_RGB (256*0.5)
#define W_Y (256*0.25)
#define W_0 (256*0.125)
static struct col {
uint8_t r, g, b;
} col[16];
uint8_t ccube_map(uint8_t r, uint8_t g, uint8_t b)
{
unsigned best = 0;
int best_i = 0;
unsigned d;
int i;
for (i = 0; i != 16; i++) {
d = (r-col[i].r)*(r-col[i].r)+
(g-col[i].g)*(g-col[i].g)+
(b-col[i].b)*(b-col[i].b);
if (!i || d < best) {
best = d;
best_i = i;
}
}
return best_i;
}
void ccube_init(void)
{
int i;
for (i = 0; i != 16; i++) {
col[i].r = W_0+(i & R_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
col[i].g = W_0+(i & G_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
col[i].b = W_0+(i & B_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
}
}