Color_table.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <grass/gis.h>
  4. #include <grass/colors.h>
  5. #include "pngdriver.h"
  6. static int r_shift, g_shift, b_shift, a_shift;
  7. static int Red[256], Grn[256], Blu[256];
  8. static void set_color(int i, int red, int grn, int blu)
  9. {
  10. png.palette[i][0] = red;
  11. png.palette[i][1] = grn;
  12. png.palette[i][2] = blu;
  13. png.palette[i][3] = 0;
  14. }
  15. static void init_colors_rgb(void)
  16. {
  17. if (G_is_little_endian()) {
  18. b_shift = 0;
  19. g_shift = 8;
  20. r_shift = 16;
  21. a_shift = 24;
  22. }
  23. else {
  24. b_shift = 24;
  25. g_shift = 16;
  26. r_shift = 8;
  27. a_shift = 0;
  28. }
  29. }
  30. static void init_colors_indexed(void)
  31. {
  32. int n_pixels;
  33. int r, g, b;
  34. int i;
  35. n_pixels = 0;
  36. if (png.has_alpha)
  37. /* transparent color should be the first!
  38. * Its RGB value doesn't matter since we fake RGB-to-index. */
  39. set_color(n_pixels++, 0, 0, 0);
  40. for (r = 0; r < 6; r++) {
  41. for (g = 0; g < 6; g++) {
  42. for (b = 0; b < 6; b++) {
  43. int red = r * 0xFF / 5;
  44. int grn = g * 0xFF / 5;
  45. int blu = b * 0xFF / 5;
  46. set_color(n_pixels++, red, grn, blu);
  47. }
  48. }
  49. }
  50. while (n_pixels < 256)
  51. set_color(n_pixels++, 0, 0, 0);
  52. for (i = 0; i < 256; i++) {
  53. int k = i * 6 / 256;
  54. Red[i] = k * 6 * 6;
  55. Grn[i] = k * 6;
  56. Blu[i] = k;
  57. }
  58. }
  59. void png_init_color_table(void)
  60. {
  61. if (png.true_color)
  62. init_colors_rgb();
  63. else
  64. init_colors_indexed();
  65. }
  66. static int get_color_rgb(int r, int g, int b, int a)
  67. {
  68. return (r << r_shift) + (g << g_shift) + (b << b_shift) + (a << a_shift);
  69. }
  70. static int get_color_indexed(int r, int g, int b, int a)
  71. {
  72. if (png.has_alpha && a >= 128)
  73. return 0;
  74. return Red[r] + Grn[g] + Blu[b] + png.has_alpha;
  75. }
  76. static void get_pixel_rgb(unsigned int pixel, int *r, int *g, int *b, int *a)
  77. {
  78. *r = (pixel >> r_shift) & 0xFF;
  79. *g = (pixel >> g_shift) & 0xFF;
  80. *b = (pixel >> b_shift) & 0xFF;
  81. *a = (pixel >> a_shift) & 0xFF;
  82. }
  83. static void get_pixel_indexed(unsigned int pixel, int *r, int *g, int *b,
  84. int *a)
  85. {
  86. *r = png.palette[pixel][0];
  87. *g = png.palette[pixel][1];
  88. *b = png.palette[pixel][2];
  89. *a = png.palette[pixel][3];
  90. }
  91. void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
  92. {
  93. if (png.true_color)
  94. get_pixel_rgb(pixel, r, g, b, a);
  95. else
  96. get_pixel_indexed(pixel, r, g, b, a);
  97. }
  98. unsigned int png_get_color(int r, int g, int b, int a)
  99. {
  100. return png.true_color
  101. ? get_color_rgb(r, g, b, a)
  102. : get_color_indexed(r, g, b, a);
  103. }