alloc_cell.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * \file alloc_cell.c
  3. *
  4. * \brief GIS Library - Raster allocation routines.
  5. *
  6. * (C) 2001-2008 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 GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <math.h>
  16. #include <grass/gis.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 int type_size[3] = { sizeof(CELL), sizeof(FCELL), sizeof(DCELL) };
  21. /**
  22. * \brief Returns size of a raster CELL in bytes.
  23. *
  24. * If <b>data_type</b> is CELL_TYPE, returns sizeof(CELL)
  25. * If <b>data_type</b> is FCELL_TYPE, returns sizeof(FCELL)
  26. * If <b>data_type</b> is DCELL_TYPE, returns sizeof(DCELL)
  27. *
  28. * \param[in] data_type
  29. * \return int
  30. */
  31. size_t G_raster_size(RASTER_MAP_TYPE data_type)
  32. {
  33. return (type_size[F2I(data_type)]);
  34. }
  35. /**
  36. * \brief Allocate memory for a CELL type raster map.
  37. *
  38. * This routine allocates a buffer of type CELL just large enough to
  39. * hold one row of raster data based on the number of columns in the
  40. * active region.<br>
  41. \code
  42. CELL *cell;
  43. cell = G_allocate_cell_buf ();
  44. \endcode
  45. * If larger buffers are required, the routine <i>G_malloc</i> can be used.
  46. * The routine is generally used with each open cell file.<br>
  47. * <b>Note:</b> <i>G_allocate_raster_buf()</i> or
  48. * <i>G_alloc_c_raster_buf()</i> is preferred over
  49. * <i>G_allocate_cell_buf()</i>.
  50. *
  51. * \return CELL * Pointer to allocated buffer
  52. * \return Prints error message and calls <i>exit()</i> on error
  53. */
  54. CELL *G_allocate_cell_buf(void)
  55. {
  56. return (CELL *) G_calloc(G_window_cols() + 1, sizeof(CELL));
  57. }
  58. /**
  59. * \brief Allocate memory for a raster map of type <b>data_type</b>.
  60. *
  61. * Allocate an array of CELL, FCELL, or DCELL (depending on
  62. * <b>data_type</b>) based on the number of columns in the current
  63. * region.
  64. *
  65. * \param[in] data_type
  66. * \return void *
  67. */
  68. void *G_allocate_raster_buf(RASTER_MAP_TYPE data_type)
  69. {
  70. return (void *)G_calloc(G_window_cols() + 1, G_raster_size(data_type));
  71. }
  72. /**
  73. * \brief Allocates memory for a raster map of type CELL.
  74. *
  75. * Allocate an array of CELL based on the number of columns in the
  76. * current region.
  77. *
  78. * \return CELL *
  79. */
  80. CELL *G_allocate_c_raster_buf(void)
  81. {
  82. return (CELL *) G_calloc(G_window_cols() + 1, sizeof(CELL));
  83. }
  84. /**
  85. * \brief Allocates memory for a raster map of type FCELL.
  86. *
  87. * Allocate an array of FCELL based on the number of columns in the
  88. * current region.
  89. *
  90. * \return FCELL *
  91. */
  92. FCELL *G_allocate_f_raster_buf(void)
  93. {
  94. return (FCELL *) G_calloc(G_window_cols() + 1, sizeof(FCELL));
  95. }
  96. /**
  97. * \brief Allocates memory for a raster map of type DCELL.
  98. *
  99. * Allocate an array of DCELL based on the number of columns in the
  100. * current region.
  101. *
  102. * \return DCELL *
  103. */
  104. DCELL *G_allocate_d_raster_buf(void)
  105. {
  106. return (DCELL *) G_calloc(G_window_cols() + 1, sizeof(DCELL));
  107. }
  108. /**
  109. * \brief Allocates memory for a null buffer.
  110. *
  111. * Allocate an array of char based on the number of columns in the
  112. * current region.
  113. *
  114. * \return char *
  115. */
  116. char *G_allocate_null_buf(void)
  117. {
  118. return (char *)G_calloc(G_window_cols() + 1, sizeof(char));
  119. }
  120. /**
  121. * \brief Allocates memory for null bits.
  122. *
  123. * Allocates an array of unsigned char based on <b>cols</b>.
  124. *
  125. * \param[in] cols number of columns in region
  126. * \return unsigned char *
  127. */
  128. unsigned char *G__allocate_null_bits(int cols)
  129. {
  130. return (unsigned char *)G_calloc(G__null_bitstream_size(cols) + 1,
  131. sizeof(unsigned char));
  132. }
  133. /**
  134. * \brief Determines null bitstream size.
  135. *
  136. * \param[in] cols number of columns
  137. * \return -1 if <b>cols</b> is invalid (<= 0)
  138. * \return size of null bistream
  139. */
  140. int G__null_bitstream_size(int cols)
  141. {
  142. if (cols <= 0)
  143. return -1;
  144. return (cols / 8 + (cols % 8 != 0));
  145. }