File: Color_table.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (134 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#include <stdio.h>
#include <stdlib.h>

#include <grass/gis.h>
#include <grass/colors.h>
#include "pngdriver.h"

static int r_shift, g_shift, b_shift, a_shift;
static int Red[256], Grn[256], Blu[256];

static void set_color(int i, int red, int grn, int blu)
{
    png_palette[i][0] = red;
    png_palette[i][1] = grn;
    png_palette[i][2] = blu;
    png_palette[i][3] = 0;
}

static void init_colors_rgb(void)
{
    NCOLORS = 1 << 24;

    if (G_is_little_endian()) {
	b_shift = 0;
	g_shift = 8;
	r_shift = 16;
	a_shift = 24;
    }
    else {
	b_shift = 24;
	g_shift = 16;
	r_shift = 8;
	a_shift = 0;
    }
}

static void init_colors_indexed(void)
{
    int n_pixels;
    int r, g, b;
    int i;

    NCOLORS = 256;

    n_pixels = 0;

    if (has_alpha)
	/* transparent color should be the first!
	 * Its RGB value doesn't matter since we fake RGB-to-index. */
	set_color(n_pixels++, 0, 0, 0);

    for (r = 0; r < 6; r++) {
	for (g = 0; g < 6; g++) {
	    for (b = 0; b < 6; b++) {
		int red = r * 0xFF / 5;
		int grn = g * 0xFF / 5;
		int blu = b * 0xFF / 5;

		set_color(n_pixels++, red, grn, blu);
	    }
	}
    }

    while (n_pixels < NCOLORS)
	set_color(n_pixels++, 0, 0, 0);

    for (i = 0; i < 256; i++) {
	int k = i * 6 / 256;

	Red[i] = k * 6 * 6;
	Grn[i] = k * 6;
	Blu[i] = k;
    }
}

void init_color_table(void)
{
    if (true_color)
	init_colors_rgb();
    else
	init_colors_indexed();
}

static int get_color_rgb(int r, int g, int b, int a)
{
    return (r << r_shift) + (g << g_shift) + (b << b_shift) + (a << a_shift);
}

static int get_color_indexed(int r, int g, int b, int a)
{
    if (has_alpha && a >= 128)
	return 0;

    return Red[r] + Grn[g] + Blu[b] + has_alpha;
}

static void get_pixel_rgb(unsigned int pixel, int *r, int *g, int *b, int *a)
{
    *r = (pixel >> r_shift) & 0xFF;
    *g = (pixel >> g_shift) & 0xFF;
    *b = (pixel >> b_shift) & 0xFF;
    *a = (pixel >> a_shift) & 0xFF;
}

static void get_pixel_indexed(unsigned int pixel, int *r, int *g, int *b,
			      int *a)
{
    *r = png_palette[pixel][0];
    *g = png_palette[pixel][1];
    *b = png_palette[pixel][2];
    *a = png_palette[pixel][3];
}


void get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
{
    if (true_color)
	get_pixel_rgb(pixel, r, g, b, a);
    else
	get_pixel_indexed(pixel, r, g, b, a);
}

unsigned int get_color(int r, int g, int b, int a)
{
    return true_color ? get_color_rgb(r, g, b, a)
	: get_color_indexed(r, g, b, a);
}

int PNG_lookup_color(int r, int g, int b)
{
    return true_color ? ((r << 16) | (g << 8) | (b << 0))
	: Red[r] + Grn[g] + Blu[b] + has_alpha;
}