aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/jpg/rgbrgbv.c
blob: bc55947a06aaef921d5bf27ca77031a63d12a3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <u.h>
#include <libc.h>
#include <draw.h>

/*
 * This version of closest() is now (feb 20, 2001) installed as rgb2cmap in libdraw
 */

int
closest(int cr, int cg, int cb)
{
	int i, r, g, b, sq;
	ulong rgb;
	int best, bestsq;

	best = 0;
	bestsq = 0x7FFFFFFF;
	for(i=0; i<256; i++){
		rgb = cmap2rgb(i);
		r = (rgb>>16) & 0xFF;
		g = (rgb>>8) & 0xFF;
		b = (rgb>>0) & 0xFF;
		sq = (r-cr)*(r-cr)+(g-cg)*(g-cg)+(b-cb)*(b-cb);
		if(sq < bestsq){
			bestsq = sq;
			best = i;
		}
	}
	return best;
}

void
main(int argc, char *argv[])
{
	int i, rgb;
	int r, g, b;
	uchar close[16*16*16];

	/* rgbmap */
	print("uint rgbmap[256] = {\n");
	for(i=0; i<256; i++){
		if(i%8 == 0)
			print("\t");
		rgb = cmap2rgb(i);
		r = (rgb>>16) & 0xFF;
		g = (rgb>>8) & 0xFF;
		b = (rgb>>0) & 0xFF;
		print("0x%.6ulX, ", (r<<16) | (g<<8) | b);
		if(i%8 == 7)
			print("\n");
	}
	print("};\n\n");

	/* closestrgb */
	print("uchar closestrgb[16*16*16] = {\n");
	for(r=0; r<256; r+=16)
	for(g=0; g<256; g+=16)
	for(b=0; b<256; b+=16)
		close[(b/16)+16*((g/16)+16*(r/16))] = closest(r+8, g+8, b+8);
	for(i=0; i<16*16*16; i++){
		if(i%16 == 0)
			print("\t");
		print("%d,", close[i]);
		if(i%16 == 15)
			print("\n");
	}
	print("};\n\n");
	exits(nil);
}