rast_to_img_string.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. *
  3. * Function: Rast_map_to_img_str() based on r.to.ppm
  4. * AUTHOR(S): Bill Brown, USA-CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Jachym Cepicky <jachym les-ejk.cz>,
  9. * Jan-Oliver Wagner <jan intevation.de>
  10. * Soeren Gebbert
  11. * PURPOSE: converts a GRASS raster map into an ARGB or
  12. * gray scale unsigned char string
  13. * COPYRIGHT: (C) 1999-2015 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. *****************************************************************************/
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #define DEF_RED 255
  26. #define DEF_GRN 255
  27. #define DEF_BLU 255
  28. /* \brief Convert a raster map layer into a string with
  29. * 32Bit ARGB, 32Bit RGB or 8Bit Gray little endian encoding.
  30. *
  31. * The raster color table is used for coloring the image. Null values are
  32. * marked as transparent. Only little endian encoding is supported.
  33. *
  34. * This function uses Rast_window_rows() and Rast_window_cols() to
  35. * get rows and cols, hence use Rast_set_window() to set the required
  36. * region for raster access.
  37. *
  38. * \param name The name of the raster map layer to convert
  39. * \param color_mode The color modes to use:
  40. * Color mode 1 -> 32Bit ARGB (0xAARRGGBB)
  41. * Color mode 2 -> 32Bit RGB (0xffRRGGBB)
  42. * Color mode 3 -> grey scale formular: .33R+ .5G+ .17B
  43. * Color mode 4 -> grey scale formular: .30R+ .59G+ .11B
  44. *
  45. * \param result: An unsigned char pointer to store the result.
  46. * It must have size 4*cols*rows in case of
  47. * ARGB and RGB,
  48. * rows*cols in case of gray scale.
  49. *
  50. * \return: 0 in case map not found, -1 in case the color mode is incorrect, 1 on success
  51. *
  52. */
  53. int Rast_map_to_img_str(char *name, int color_mode, unsigned char* result)
  54. {
  55. unsigned char *set = NULL, *red = NULL, *green = NULL,
  56. *blue = NULL;
  57. unsigned char alpha;
  58. const char *mapset = NULL;
  59. CELL *cell_buf = NULL;
  60. FCELL *fcell_buf = NULL;
  61. DCELL *dcell_buf = NULL;
  62. void *voidc = NULL;
  63. int rtype, row, col;
  64. size_t i;
  65. int map = 0;
  66. struct Colors colors;
  67. int rows = Rast_window_rows();
  68. int cols = Rast_window_cols();
  69. if(color_mode > 3 || color_mode < 1)
  70. return(-1);
  71. mapset = G_find_raster2(name, "");
  72. if(!mapset)
  73. return(0);
  74. map = Rast_open_old(name, "");
  75. cell_buf = Rast_allocate_c_buf();
  76. fcell_buf = Rast_allocate_f_buf();
  77. dcell_buf = Rast_allocate_d_buf();
  78. red = G_malloc(cols);
  79. green = G_malloc(cols);
  80. blue = G_malloc(cols);
  81. set = G_malloc(cols);
  82. Rast_read_colors(name, mapset, &colors);
  83. rtype = Rast_get_map_type(map);
  84. if (rtype == CELL_TYPE)
  85. voidc = (CELL *) cell_buf;
  86. else if (rtype == FCELL_TYPE)
  87. voidc = (FCELL *) fcell_buf;
  88. else if (rtype == DCELL_TYPE)
  89. voidc = (DCELL *) dcell_buf;
  90. i = 0;
  91. if(color_mode == 1 || color_mode == 2) {/* 32BIT ARGB COLOR IMAGE with transparency */
  92. for (row = 0; row < rows; row++) {
  93. Rast_get_row(map, (void *)voidc, row, rtype);
  94. Rast_lookup_colors((void *)voidc, red, green, blue, set,
  95. cols, &colors, rtype);
  96. alpha = (unsigned char)255;
  97. if ( color_mode == 1 && Rast_is_null_value( voidc, rtype ) )
  98. {
  99. alpha = (unsigned char)0;
  100. }
  101. for (col = 0; col < cols; col++) {
  102. /* Only little endian */
  103. if (set[col]) {
  104. result[i++] = blue[col];
  105. result[i++] = green[col];
  106. result[i++] = red[col];
  107. result[i++] = alpha;
  108. }
  109. else {
  110. result[i++] = DEF_BLU;
  111. result[i++] = DEF_GRN;
  112. result[i++] = DEF_RED;
  113. result[i++] = alpha;
  114. }
  115. }
  116. }
  117. }
  118. else {/* GREYSCALE IMAGE */
  119. for (row = 0; row < rows; row++) {
  120. Rast_get_row(map, (void *)voidc, row, rtype);
  121. Rast_lookup_colors((void *)voidc, red, green, blue, set,
  122. cols, &colors, rtype);
  123. if(color_mode == 3) {
  124. for (col = 0; col < cols; col++) {
  125. /*.33R+ .5G+ .17B */
  126. result[i++] = ((red[col]) * 11 +
  127. (green[col]) * 16 +
  128. (blue[col]) * 5) >> 5;
  129. }
  130. } else {
  131. for (col = 0; col < cols; col++) {
  132. /*NTSC Y equation: .30R+ .59G+ .11B */
  133. result[i++] = ((red[col]) * 19 +
  134. (green[col]) * 38 +
  135. (blue[col]) * 7) >> 6;
  136. }
  137. }
  138. }
  139. }
  140. Rast_free_colors(&colors);
  141. G_free(cell_buf);
  142. G_free(fcell_buf);
  143. G_free(dcell_buf);
  144. G_free(red);
  145. G_free(green);
  146. G_free(blue);
  147. G_free(set);
  148. Rast_close(map);
  149. return(1);
  150. }