cell_funcs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /***************************************************************************
  2. *
  3. * MODULE: r.fill.stats
  4. * FILE: cell_funcs.c
  5. * AUTHOR(S): Benjamin Ducke
  6. * PURPOSE: Provide cell type dependent functions for cell data operations.
  7. * At program initialization, a function pointer is set to point to
  8. * the right one of the three alternatives.
  9. * This function pointer can later be used to work with cell data of
  10. * different types without having to go through any switching logics.
  11. *
  12. * COPYRIGHT: (C) 2011 by the GRASS Development Team
  13. *
  14. * This program is free software under the GPL (>=v2)
  15. * Read the file COPYING that comes with GRASS for details.
  16. *
  17. ****************************************************************************
  18. */
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. #include "cell_funcs.h"
  25. RASTER_MAP_TYPE IN_TYPE;
  26. RASTER_MAP_TYPE OUT_TYPE;
  27. unsigned char CELL_IN_SIZE;
  28. unsigned char CELL_IN_PTR_SIZE;
  29. unsigned char CELL_OUT_SIZE;
  30. unsigned char CELL_OUT_PTR_SIZE;
  31. unsigned char CELL_ERR_SIZE;
  32. void (*WRITE_CELL_VAL) (void *, void *);
  33. void (*WRITE_DOUBLE_VAL) (void *, double);
  34. int (*IS_NULL) (void *);
  35. void (*SET_NULL) (void *, unsigned long);
  36. /*
  37. * Write cell values.
  38. */
  39. void write_cell_value_c(void *cell_output, void *cell_input)
  40. {
  41. Rast_set_c_value(cell_output, Rast_get_c_value(cell_input, IN_TYPE),
  42. OUT_TYPE);
  43. }
  44. void write_cell_value_f(void *cell_output, void *cell_input)
  45. {
  46. Rast_set_f_value(cell_output, Rast_get_f_value(cell_input, IN_TYPE),
  47. OUT_TYPE);
  48. }
  49. void write_cell_value_d(void *cell_output, void *cell_input)
  50. {
  51. Rast_set_d_value(cell_output, Rast_get_d_value(cell_input, IN_TYPE),
  52. OUT_TYPE);
  53. }
  54. /*
  55. * Write a double value into a cell (truncates for CELL type output).
  56. */
  57. void write_double_value_c(void *cell, double val)
  58. {
  59. Rast_set_c_value(cell, (CELL) val, OUT_TYPE);
  60. }
  61. void write_double_value_f(void *cell, double val)
  62. {
  63. Rast_set_f_value(cell, (FCELL) val, OUT_TYPE);
  64. }
  65. void write_double_value_d(void *cell, double val)
  66. {
  67. Rast_set_d_value(cell, (DCELL) val, OUT_TYPE);
  68. }
  69. /*
  70. * Check for "no data".
  71. */
  72. int is_null_value_c(void *cell)
  73. {
  74. return (Rast_is_c_null_value((CELL *) cell));
  75. }
  76. int is_null_value_f(void *cell)
  77. {
  78. return (Rast_is_f_null_value((FCELL *) cell));
  79. }
  80. int is_null_value_d(void *cell)
  81. {
  82. return (Rast_is_d_null_value((DCELL *) cell));
  83. }
  84. /*
  85. * Set consecutive cells to "no data".
  86. */
  87. void set_null_c(void *cells, unsigned long count)
  88. {
  89. Rast_set_c_null_value((CELL *) cells, count);
  90. }
  91. void set_null_f(void *cells, unsigned long count)
  92. {
  93. Rast_set_f_null_value((FCELL *) cells, count);
  94. }
  95. void set_null_d(void *cells, unsigned long count)
  96. {
  97. Rast_set_d_null_value((DCELL *) cells, count);
  98. }
  99. /*
  100. * Call this init function once it is clear which input
  101. * and output type will be used (CELL, DCELL or FCELL).
  102. * I.e. right after IN_TYPE and OUT_TYPE have been set
  103. * to valid values;
  104. */
  105. void init_cell_funcs()
  106. {
  107. CELL_ERR_SIZE = sizeof(FCELL);
  108. if (IN_TYPE == CELL_TYPE) {
  109. CELL_IN_SIZE = sizeof(CELL);
  110. CELL_IN_PTR_SIZE = sizeof(CELL *);
  111. IS_NULL = &is_null_value_c;
  112. }
  113. if (IN_TYPE == FCELL_TYPE) {
  114. CELL_IN_SIZE = sizeof(FCELL);
  115. CELL_IN_PTR_SIZE = sizeof(FCELL *);
  116. IS_NULL = &is_null_value_f;
  117. }
  118. if (IN_TYPE == DCELL_TYPE) {
  119. CELL_IN_SIZE = sizeof(DCELL);
  120. CELL_IN_PTR_SIZE = sizeof(DCELL *);
  121. IS_NULL = &is_null_value_d;
  122. }
  123. if (OUT_TYPE == CELL_TYPE) {
  124. CELL_OUT_SIZE = sizeof(CELL);
  125. CELL_OUT_PTR_SIZE = sizeof(CELL *);
  126. WRITE_CELL_VAL = &write_cell_value_c;
  127. WRITE_DOUBLE_VAL = &write_double_value_c;
  128. SET_NULL = &set_null_c;
  129. }
  130. if (OUT_TYPE == FCELL_TYPE) {
  131. CELL_OUT_SIZE = sizeof(FCELL);
  132. CELL_OUT_PTR_SIZE = sizeof(FCELL *);
  133. WRITE_CELL_VAL = &write_cell_value_f;
  134. WRITE_DOUBLE_VAL = &write_double_value_f;
  135. SET_NULL = &set_null_f;
  136. }
  137. if (OUT_TYPE == DCELL_TYPE) {
  138. CELL_OUT_SIZE = sizeof(DCELL);
  139. CELL_OUT_PTR_SIZE = sizeof(DCELL *);
  140. WRITE_CELL_VAL = &write_cell_value_d;
  141. WRITE_DOUBLE_VAL = &write_double_value_d;
  142. SET_NULL = &set_null_d;
  143. }
  144. }