alloc_cell.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. * \file lib/raster/alloc_cell.c
  3. *
  4. * \brief Raster Library - Raster allocation routines.
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include <grass/raster.h>
  16. #include <grass/glocale.h>
  17. /* convert type "RASTER_MAP_TYPE" into index */
  18. #define F2I(map_type) \
  19. (map_type == CELL_TYPE ? 0 : (map_type == FCELL_TYPE ? 1 : 2))
  20. static const int type_size[3] =
  21. { sizeof(CELL), sizeof(FCELL), sizeof(DCELL) };
  22. /*!
  23. * \brief Returns size of a raster cell in bytes.
  24. *
  25. * - If <i>data_type</i> is CELL_TYPE, returns sizeof(CELL)
  26. * - If <i>data_type</i> is FCELL_TYPE, returns sizeof(FCELL)
  27. * - If <i>data_type</i> is DCELL_TYPE, returns sizeof(DCELL)
  28. *
  29. * \param data_type raster type (CELL, FCELL, DCELL)
  30. *
  31. * \return raster type size
  32. */
  33. size_t Rast_cell_size(RASTER_MAP_TYPE data_type)
  34. {
  35. return (type_size[F2I(data_type)]);
  36. }
  37. /*!
  38. * \brief Allocate memory for a raster map of given type
  39. *
  40. * Allocate an array of CELL, FCELL, or DCELL (depending on
  41. * <i>data_type</i>) based on the number of columns in the current
  42. * region.
  43. *
  44. * \param data_type raster type (CELL, FCELL, DCELL)
  45. *
  46. * \return pointer to allocated buffer
  47. */
  48. void *Rast_allocate_buf(RASTER_MAP_TYPE data_type)
  49. {
  50. return (void *)G_calloc(Rast_window_cols() + 1, Rast_cell_size(data_type));
  51. }
  52. /*!
  53. * \brief Allocate memory for a CELL type raster map.
  54. *
  55. * Allocate an array of CELL based on the number of columns in the
  56. * current region.
  57. *
  58. * This routine allocates a buffer of type CELL just large enough to
  59. * hold one row of raster data based on the number of columns in the
  60. * active region.
  61. *
  62. \code
  63. CELL *cell;
  64. cell = Rast_allocate_c_buf();
  65. \endcode
  66. *
  67. * If larger buffers are required, the routine G_malloc() can be used.
  68. * The routine is generally used with each open cell file.
  69. *
  70. * Prints error message and calls exit() on error.
  71. *
  72. * \return pointer to allocated buffer
  73. */
  74. CELL *Rast_allocate_c_buf(void)
  75. {
  76. return (CELL *) G_calloc(Rast_window_cols() + 1, sizeof(CELL));
  77. }
  78. /*!
  79. * \brief Allocates memory for a raster map of type FCELL.
  80. *
  81. * Allocate an array of FCELL based on the number of columns in the
  82. * current region.
  83. *
  84. * \return pointer to allocated buffer
  85. */
  86. FCELL *Rast_allocate_f_buf(void)
  87. {
  88. return (FCELL *) G_calloc(Rast_window_cols() + 1, sizeof(FCELL));
  89. }
  90. /*!
  91. * \brief Allocates memory for a raster map of type DCELL.
  92. *
  93. * Allocate an array of DCELL based on the number of columns in the
  94. * current region.
  95. *
  96. * \return pointer to allocated buffer
  97. */
  98. DCELL *Rast_allocate_d_buf(void)
  99. {
  100. return (DCELL *) G_calloc(Rast_window_cols() + 1, sizeof(DCELL));
  101. }
  102. /*!
  103. * \brief Allocates memory for a null buffer.
  104. *
  105. * Allocate an array of char based on the number of columns in the
  106. * current region.
  107. *
  108. * \return pointer to allocated buffer
  109. */
  110. char *Rast_allocate_null_buf(void)
  111. {
  112. return (char *)G_calloc(Rast_window_cols() + 1, sizeof(char));
  113. }
  114. /*!
  115. * \brief Allocates memory for null bits.
  116. *
  117. * Allocates an array of unsigned char based on <i>cols</i>.
  118. *
  119. * \param cols number of columns in region
  120. *
  121. * \return pointer to allocated buffer
  122. */
  123. unsigned char *Rast__allocate_null_bits(int cols)
  124. {
  125. return (unsigned char *)G_calloc(Rast__null_bitstream_size(cols) + 1,
  126. sizeof(unsigned char));
  127. }
  128. /*!
  129. * \brief Determines null bitstream size.
  130. *
  131. * \param cols number of columns
  132. *
  133. * \return size of null bistream
  134. */
  135. int Rast__null_bitstream_size(int cols)
  136. {
  137. if (cols <= 0)
  138. G_fatal_error(_("Rast__null_bitstream_size: cols (%d) is negative"),
  139. cols);
  140. return (cols + 7) / 8;
  141. }
  142. void *Rast_allocate_input_buf(RASTER_MAP_TYPE data_type)
  143. {
  144. return G_calloc(Rast_input_window_cols() + 1, Rast_cell_size(data_type));
  145. }
  146. CELL *Rast_allocate_c_input_buf(void)
  147. {
  148. return (CELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(CELL));
  149. }
  150. FCELL *Rast_allocate_f_input_buf(void)
  151. {
  152. return (FCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(FCELL));
  153. }
  154. DCELL *Rast_allocate_d_input_buf(void)
  155. {
  156. return (DCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(DCELL));
  157. }
  158. char *Rast_allocate_null_input_buf(void)
  159. {
  160. return (char *)G_calloc(Rast_input_window_cols() + 1, sizeof(char));
  161. }
  162. void *Rast_allocate_output_buf(RASTER_MAP_TYPE data_type)
  163. {
  164. return G_calloc(Rast_output_window_cols() + 1, Rast_cell_size(data_type));
  165. }
  166. CELL *Rast_allocate_c_output_buf(void)
  167. {
  168. return (CELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(CELL));
  169. }
  170. FCELL *Rast_allocate_f_output_buf(void)
  171. {
  172. return (FCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(FCELL));
  173. }
  174. DCELL *Rast_allocate_d_output_buf(void)
  175. {
  176. return (DCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(DCELL));
  177. }
  178. char *Rast_allocate_null_output_buf(void)
  179. {
  180. return (char *)G_calloc(Rast_output_window_cols() + 1, sizeof(char));
  181. }