get_row_colr.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <grass/gis.h>
  2. #include "G.h"
  3. /*!
  4. * \brief
  5. *
  6. * Reads a row of raster data and converts it to red,
  7. * green and blue components according to the <em>colors</em> parameter.
  8. * This provides a convenient way to treat a raster layer as a color
  9. * image without having to explictly cater for each of <tt>CELL</tt>, <tt>FCELL</tt> and <tt>DCELL</tt> types
  10. *
  11. * \param fd
  12. * \param row
  13. * \param colors
  14. * \param red
  15. * \param grn
  16. * \param blu
  17. * \param nul
  18. * \return int
  19. */
  20. int
  21. G_get_raster_row_colors(int fd, int row, struct Colors *colors,
  22. unsigned char *red, unsigned char *grn,
  23. unsigned char *blu, unsigned char *nul)
  24. {
  25. static void *array;
  26. static int array_size;
  27. static unsigned char *set;
  28. static int set_size;
  29. int cols = G__.window.cols;
  30. int type = G__.fileinfo[fd].map_type;
  31. int size = G_raster_size(type);
  32. void *p;
  33. int i;
  34. if (array_size < cols * size) {
  35. array_size = cols * size;
  36. array = (DCELL *) G_realloc(array, array_size);
  37. }
  38. if (set_size < cols) {
  39. set_size = cols;
  40. set = G_realloc(set, set_size);
  41. }
  42. if (G_get_raster_row(fd, array, row, type) < 0)
  43. return -1;
  44. if (nul)
  45. for (i = 0, p = array; i < cols; i++, p = G_incr_void_ptr(p, size))
  46. nul[i] = G_is_null_value(p, type);
  47. G_lookup_raster_colors(array, red, grn, blu, set, cols, colors, type);
  48. return 0;
  49. }