GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 00005 #include <grass/gis.h> 00006 #include <grass/colors.h> 00007 #include "pngdriver.h" 00008 00009 static int r_shift, g_shift, b_shift, a_shift; 00010 static int Red[256], Grn[256], Blu[256]; 00011 00012 static void set_color(int i, int red, int grn, int blu) 00013 { 00014 png_palette[i][0] = red; 00015 png_palette[i][1] = grn; 00016 png_palette[i][2] = blu; 00017 png_palette[i][3] = 0; 00018 } 00019 00020 static void init_colors_rgb(void) 00021 { 00022 NCOLORS = 1 << 24; 00023 00024 if (G_is_little_endian()) { 00025 b_shift = 0; 00026 g_shift = 8; 00027 r_shift = 16; 00028 a_shift = 24; 00029 } 00030 else { 00031 b_shift = 24; 00032 g_shift = 16; 00033 r_shift = 8; 00034 a_shift = 0; 00035 } 00036 } 00037 00038 static void init_colors_indexed(void) 00039 { 00040 int n_pixels; 00041 int r, g, b; 00042 int i; 00043 00044 NCOLORS = 256; 00045 00046 n_pixels = 0; 00047 00048 if (has_alpha) 00049 /* transparent color should be the first! 00050 * Its RGB value doesn't matter since we fake RGB-to-index. */ 00051 set_color(n_pixels++, 0, 0, 0); 00052 00053 for (r = 0; r < 6; r++) { 00054 for (g = 0; g < 6; g++) { 00055 for (b = 0; b < 6; b++) { 00056 int red = r * 0xFF / 5; 00057 int grn = g * 0xFF / 5; 00058 int blu = b * 0xFF / 5; 00059 00060 set_color(n_pixels++, red, grn, blu); 00061 } 00062 } 00063 } 00064 00065 while (n_pixels < NCOLORS) 00066 set_color(n_pixels++, 0, 0, 0); 00067 00068 for (i = 0; i < 256; i++) { 00069 int k = i * 6 / 256; 00070 00071 Red[i] = k * 6 * 6; 00072 Grn[i] = k * 6; 00073 Blu[i] = k; 00074 } 00075 } 00076 00077 void init_color_table(void) 00078 { 00079 if (true_color) 00080 init_colors_rgb(); 00081 else 00082 init_colors_indexed(); 00083 } 00084 00085 static int get_color_rgb(int r, int g, int b, int a) 00086 { 00087 return (r << r_shift) + (g << g_shift) + (b << b_shift) + (a << a_shift); 00088 } 00089 00090 static int get_color_indexed(int r, int g, int b, int a) 00091 { 00092 if (has_alpha && a >= 128) 00093 return 0; 00094 00095 return Red[r] + Grn[g] + Blu[b] + has_alpha; 00096 } 00097 00098 static void get_pixel_rgb(unsigned int pixel, int *r, int *g, int *b, int *a) 00099 { 00100 *r = (pixel >> r_shift) & 0xFF; 00101 *g = (pixel >> g_shift) & 0xFF; 00102 *b = (pixel >> b_shift) & 0xFF; 00103 *a = (pixel >> a_shift) & 0xFF; 00104 } 00105 00106 static void get_pixel_indexed(unsigned int pixel, int *r, int *g, int *b, 00107 int *a) 00108 { 00109 *r = png_palette[pixel][0]; 00110 *g = png_palette[pixel][1]; 00111 *b = png_palette[pixel][2]; 00112 *a = png_palette[pixel][3]; 00113 } 00114 00115 00116 void get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a) 00117 { 00118 if (true_color) 00119 get_pixel_rgb(pixel, r, g, b, a); 00120 else 00121 get_pixel_indexed(pixel, r, g, b, a); 00122 } 00123 00124 unsigned int get_color(int r, int g, int b, int a) 00125 { 00126 return true_color ? get_color_rgb(r, g, b, a) 00127 : get_color_indexed(r, g, b, a); 00128 } 00129 00130 int PNG_lookup_color(int r, int g, int b) 00131 { 00132 return true_color ? ((r << 16) | (g << 8) | (b << 0)) 00133 : Red[r] + Grn[g] + Blu[b] + has_alpha; 00134 }