GRASS GIS 8 Programmer's Manual 8.2.1(2023)-exported
color_table.c
Go to the documentation of this file.
1
2#include <stdio.h>
3#include <stdlib.h>
4
5#include <grass/gis.h>
6#include <grass/colors.h>
7#include "pngdriver.h"
8
9static int r_shift, g_shift, b_shift, a_shift;
10static int Red[256], Grn[256], Blu[256];
11
12static void set_color(int i, int red, int grn, int blu)
13{
14 png.palette[i][0] = red;
15 png.palette[i][1] = grn;
16 png.palette[i][2] = blu;
17 png.palette[i][3] = 0;
18}
19
20static void init_colors_rgb(void)
21{
22 if (G_is_little_endian()) {
23 b_shift = 0;
24 g_shift = 8;
25 r_shift = 16;
26 a_shift = 24;
27 }
28 else {
29 b_shift = 24;
30 g_shift = 16;
31 r_shift = 8;
32 a_shift = 0;
33 }
34}
35
36static void init_colors_indexed(void)
37{
38 int n_pixels;
39 int r, g, b;
40 int i;
41
42 n_pixels = 0;
43
44 if (png.has_alpha)
45 /* transparent color should be the first!
46 * Its RGB value doesn't matter since we fake RGB-to-index. */
47 set_color(n_pixels++, 0, 0, 0);
48
49 for (r = 0; r < 6; r++) {
50 for (g = 0; g < 6; g++) {
51 for (b = 0; b < 6; b++) {
52 int red = r * 0xFF / 5;
53 int grn = g * 0xFF / 5;
54 int blu = b * 0xFF / 5;
55
56 set_color(n_pixels++, red, grn, blu);
57 }
58 }
59 }
60
61 while (n_pixels < 256)
62 set_color(n_pixels++, 0, 0, 0);
63
64 for (i = 0; i < 256; i++) {
65 int k = i * 6 / 256;
66
67 Red[i] = k * 6 * 6;
68 Grn[i] = k * 6;
69 Blu[i] = k;
70 }
71}
72
74{
75 if (png.true_color)
76 init_colors_rgb();
77 else
78 init_colors_indexed();
79}
80
81static int get_color_rgb(int r, int g, int b, int a)
82{
83 return (r << r_shift) + (g << g_shift) + (b << b_shift) + (a << a_shift);
84}
85
86static int get_color_indexed(int r, int g, int b, int a)
87{
88 if (png.has_alpha && a >= 128)
89 return 0;
90
91 return Red[r] + Grn[g] + Blu[b] + png.has_alpha;
92}
93
94static void get_pixel_rgb(unsigned int pixel, int *r, int *g, int *b, int *a)
95{
96 *r = (pixel >> r_shift) & 0xFF;
97 *g = (pixel >> g_shift) & 0xFF;
98 *b = (pixel >> b_shift) & 0xFF;
99 *a = (pixel >> a_shift) & 0xFF;
100}
101
102static void get_pixel_indexed(unsigned int pixel, int *r, int *g, int *b,
103 int *a)
104{
105 *r = png.palette[pixel][0];
106 *g = png.palette[pixel][1];
107 *b = png.palette[pixel][2];
108 *a = png.palette[pixel][3];
109}
110
111
112void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
113{
114 if (png.true_color)
115 get_pixel_rgb(pixel, r, g, b, a);
116 else
117 get_pixel_indexed(pixel, r, g, b, a);
118}
119
120unsigned int png_get_color(int r, int g, int b, int a)
121{
122 return png.true_color
123 ? get_color_rgb(r, g, b, a)
124 : get_color_indexed(r, g, b, a);
125}
126
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:120
void png_init_color_table(void)
Definition: color_table.c:73
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
Definition: color_table.c:112
double b
double r
int G_is_little_endian(void)
Tests for little ENDIAN.
Definition: endian.c:24
float g
Definition: named_colr.c:8
struct png_state png
GRASS png display driver - header file.
int has_alpha
Definition: pngdriver.h:36
int true_color
Definition: pngdriver.h:35
unsigned char palette[256][4]
Definition: pngdriver.h:45