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