color_out.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*!
  2. \file lib/raster/color_out.c
  3. \brief Raster Library - Print color table
  4. (C) 2010-2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Glynn Clements
  9. */
  10. #include <grass/raster.h>
  11. static void write_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, int b,
  12. FILE *fp, int perc)
  13. {
  14. static DCELL v0;
  15. static int r0 = -1, g0 = -1, b0 = -1;
  16. if (v0 == *val && r0 == r && g0 == g && b0 == b)
  17. return;
  18. v0 = *val, r0 = r, g0 = g, b0 = b;
  19. if (perc)
  20. fprintf(fp, "%g%% %d:%d:%d\n", 100 * (*val - *min) / (*max - *min), r, g, b);
  21. else
  22. fprintf(fp, "%g %d:%d:%d\n", *val, r, g, b);
  23. }
  24. /*!
  25. \brief Print color table
  26. \param colors pointer to Colors structure
  27. \param min,max minimum and maximum value for percentage output (used only when \p perc is non-zero)
  28. \param fp file where to print color table rules
  29. \param perc TRUE for percentage output
  30. */
  31. void Rast_print_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp,
  32. int perc)
  33. {
  34. int i, count;
  35. count = 0;
  36. if (colors->version < 0) {
  37. /* 3.0 format */
  38. CELL lo, hi;
  39. Rast_get_c_color_range(&lo, &hi, colors);
  40. for (i = lo; i <= hi; i++) {
  41. unsigned char r, g, b, set;
  42. DCELL val = (DCELL) i;
  43. Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
  44. write_rule(&val, &min, &max, r, g, b, fp, perc);
  45. }
  46. }
  47. else {
  48. count = Rast_colors_count(colors);
  49. for (i = 0; i < count; i++) {
  50. DCELL val1, val2;
  51. unsigned char r1, g1, b1, r2, g2, b2;
  52. Rast_get_fp_color_rule(
  53. &val1, &r1, &g1, &b1,
  54. &val2, &r2, &g2, &b2,
  55. colors, count - 1 - i);
  56. write_rule(&val1, &min, &max, r1, g1, b1, fp, perc);
  57. write_rule(&val2, &min, &max, r2, g2, b2, fp, perc);
  58. }
  59. }
  60. {
  61. int r, g, b;
  62. Rast_get_null_value_color(&r, &g, &b, colors);
  63. fprintf(fp, "nv %d:%d:%d\n", r, g, b);
  64. Rast_get_default_color(&r, &g, &b, colors);
  65. fprintf(fp, "default %d:%d:%d\n", r, g, b);
  66. }
  67. if (fp != stdout)
  68. fclose(fp);
  69. }