[GRASS-SVN] r40464 - in grass/trunk: include lib/gis lib/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jan 16 01:06:01 EST 2010


Author: glynn
Date: 2010-01-16 01:06:00 -0500 (Sat, 16 Jan 2010)
New Revision: 40464

Modified:
   grass/trunk/include/gisdefs.h
   grass/trunk/include/rasterdefs.h
   grass/trunk/lib/gis/alloc.c
   grass/trunk/lib/raster/alloc_cell.c
   grass/trunk/lib/raster/cats.c
   grass/trunk/lib/raster/color_look.c
   grass/trunk/lib/raster/get_row.c
   grass/trunk/lib/raster/null_val.c
   grass/trunk/lib/raster/range.c
   grass/trunk/lib/raster/set_window.c
   grass/trunk/lib/raster/zero_cell.c
Log:
Optimisations:
 Make Rast_is_*_null_value() macros
 Make G_incr_void_ptr() a macro
 Replace Rast__check_null_bit() with macro in get_row.c
 Move Rast_cell_size() calls out of loops


Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/include/gisdefs.h	2010-01-16 06:06:00 UTC (rev 40464)
@@ -54,11 +54,16 @@
 const char *G_align_window(struct Cell_head *, const struct Cell_head *);
 
 /* alloc.c */
+#define G_incr_void_ptr(ptr, size) \
+    ((void *)((const unsigned char *)(ptr) + (size)))
+
 void *G__malloc(const char *, int, size_t);
 void *G__calloc(const char *, int, size_t, size_t);
 void *G__realloc(const char *, int, void *, size_t);
 void G_free(void *);
-void *G_incr_void_ptr(const void *, const size_t);
+#ifndef G_incr_void_ptr
+void *G_incr_void_ptr(const void *, size_t);
+#endif
 
 #define G_malloc(n)     G__malloc(__FILE__, __LINE__, (n))
 #define G_calloc(m, n)  G__calloc(__FILE__, __LINE__, (m), (n))

Modified: grass/trunk/include/rasterdefs.h
===================================================================
--- grass/trunk/include/rasterdefs.h	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/include/rasterdefs.h	2010-01-16 06:06:00 UTC (rev 40464)
@@ -364,15 +364,28 @@
 int Rast_maskfd(void);
 
 /* null_val.c */
+#define Rast_is_c_null_value(cellVal)	\
+    (*(const CELL *)(cellVal) == (CELL) 0x80000000)
+#define Rast_is_f_null_value(fcellVal)	\
+    (*(const FCELL *)(fcellVal) != *(const FCELL *)(fcellVal))
+#define Rast_is_d_null_value(dcellVal)	\
+    (*(const DCELL *)(dcellVal) != *(const DCELL *)(dcellVal))
+
 void Rast__set_null_value(void *, int, int, RASTER_MAP_TYPE);
 void Rast_set_null_value(void *, int, RASTER_MAP_TYPE);
 void Rast_set_c_null_value(CELL *, int);
 void Rast_set_f_null_value(FCELL *, int);
 void Rast_set_d_null_value(DCELL *, int);
 int Rast_is_null_value(const void *, RASTER_MAP_TYPE);
+#ifndef Rast_is_c_null_value
 int Rast_is_c_null_value(const CELL *);
+#endif
+#ifndef Rast_is_f_null_value
 int Rast_is_f_null_value(const FCELL *);
+#endif
+#ifndef Rast_is_f_null_value
 int Rast_is_d_null_value(const DCELL *);
+#endif
 void Rast_insert_null_values(void *, char *, int, RASTER_MAP_TYPE);
 void Rast_insert_c_null_values(CELL *, char *, int);
 void Rast_insert_f_null_values(FCELL *, char *, int);

Modified: grass/trunk/lib/gis/alloc.c
===================================================================
--- grass/trunk/lib/gis/alloc.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/gis/alloc.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -161,8 +161,10 @@
  *
  * \return pointer to the data
  */
-void *G_incr_void_ptr(const void *ptr, const size_t size)
+#ifndef G_incr_void_ptr
+void *G_incr_void_ptr(const void *ptr, size_t size)
 {
     /* assuming that the size of unsigned char is 1 */
     return (void *)((const unsigned char *)ptr + size);
 }
+#endif

Modified: grass/trunk/lib/raster/alloc_cell.c
===================================================================
--- grass/trunk/lib/raster/alloc_cell.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/alloc_cell.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -15,6 +15,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/glocale.h>
 
 /* convert type "RASTER_MAP_TYPE" into index */
 #define F2I(map_type) \
@@ -142,13 +143,13 @@
  *
  * \param cols number of columns
  *
- * \return -1 if <i>cols</i> is invalid (<= 0)
  * \return size of null bistream
  */
 int Rast__null_bitstream_size(int cols)
 {
     if (cols <= 0)
-	return -1;
+	G_fatal_error(_("Rast__null_bitstream_size: cols (%d) is negative"),
+		      cols);
 
-    return (cols / 8 + (cols % 8 != 0));
+    return (cols + 7) / 8;
 }

Modified: grass/trunk/lib/raster/cats.c
===================================================================
--- grass/trunk/lib/raster/cats.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/cats.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -554,6 +554,7 @@
 		   int ncols, struct Categories *pcats,
 		   RASTER_MAP_TYPE data_type)
 {
+    size_t size = Rast_cell_size(data_type);
     CELL i;
 
     while (ncols-- > 0) {
@@ -564,7 +565,7 @@
 	if (i > pcats->ncats)
 	    return -1;
 	pcats->marks[i]++;
-	rast_row = G_incr_void_ptr(rast_row, Rast_cell_size(data_type));
+	rast_row = G_incr_void_ptr(rast_row, size);
     }
     return 1;
 }

Modified: grass/trunk/lib/raster/color_look.c
===================================================================
--- grass/trunk/lib/raster/color_look.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/color_look.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -206,7 +206,8 @@
     int found, r, g, b;
     int cell_type;
     int lookup, max_ind, min_ind, try;
-    int (*lower) ();
+    int (*lower)();
+    size_t size = Rast_cell_size(data_type);
 
     if (mod)
 	cp = &colors->modular;
@@ -246,9 +247,8 @@
     ptr = raster;
 
     for (; n-- > 0;
-	 ptr =
-	 G_incr_void_ptr(ptr, Rast_cell_size(data_type)), red++, grn++, blu++,
-	 *set++ = found) {
+	 ptr = G_incr_void_ptr(ptr, size),
+	     red++, grn++, blu++, *set++ = found) {
 	/* if the cell is the same as last one, use the prev color values */
 	if (ptr != raster && Rast_raster_cmp(ptr, last_ptr, data_type) == 0) {
 	    *red = *(red - 1);

Modified: grass/trunk/lib/raster/get_row.c
===================================================================
--- grass/trunk/lib/raster/get_row.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/get_row.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -872,6 +872,8 @@
     return 1;
 }
 
+#define check_null_bit(flags, bit_num) ((flags)[(bit_num)>>3] & ((unsigned char)0x80>>((bit_num)&7)) ? 1 : 0)
+
 static void get_null_value_row_nomask(int fd, char *flags, int row)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
@@ -913,9 +915,7 @@
 	if (!fcb->col_map[j])
 	    flags[j] = 1;
 	else
-	    flags[j] = Rast__check_null_bit(fcb->null_bits,
-					    fcb->col_map[j] - 1,
-					    fcb->cellhd.cols);
+	    flags[j] = check_null_bit(fcb->null_bits, fcb->col_map[j] - 1);
     }
 }
 
@@ -991,6 +991,7 @@
 			int null_is_zero, int with_mask)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
+    size_t size = Rast_cell_size(map_type);
     char *null_buf;
     int i;
 
@@ -1012,7 +1013,7 @@
 	       is not set and calls G_set_[f/d]_null_value() otherwise */
 	    Rast__set_null_value(buf, 1, null_is_zero, map_type);
 	}
-	buf = G_incr_void_ptr(buf, Rast_cell_size(map_type));
+	buf = G_incr_void_ptr(buf, size);
     }
 
     G__freea(null_buf);

Modified: grass/trunk/lib/raster/null_val.c
===================================================================
--- grass/trunk/lib/raster/null_val.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/null_val.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -205,11 +205,13 @@
    \return TRUE if CELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_c_null_value
 int Rast_is_c_null_value(const CELL * cellVal)
 {
     /* Check if the CELL value matches the null pattern */
     return *cellVal == (CELL) 0x80000000;
 }
+#endif
 
 /*!
    \brief To check if a FCELL raster value is set to NULL
@@ -236,10 +238,12 @@
    \return TRUE if FCELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_f_null_value
 int Rast_is_f_null_value(const FCELL * fcellVal)
 {
     return *fcellVal != *fcellVal;
 }
+#endif
 
 /*!
    \brief To check if a DCELL raster value is set to NULL
@@ -253,10 +257,12 @@
    \return TRUE if DCELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_f_null_value
 int Rast_is_d_null_value(const DCELL * dcellVal)
 {
     return *dcellVal != *dcellVal;
 }
+#endif
 
 /*!
    \brief To insert null values into a map.
@@ -323,32 +329,29 @@
 
    Note: Only for internal use.
 
-   \param flags ?
-   \param bit_num ?
-   \param n ?
+   \param flags null bitmap
+   \param bit_num index of bit to check
+   \param n size of null bitmap (in bits)
 
-   \return -1 on error
+   \return 1 if set, 0 if unset
  */
 int Rast__check_null_bit(const unsigned char *flags, int bit_num, int n)
 {
     int ind;
     int offset;
 
+    /* check that bit_num is in range */
+    if (bit_num < 0 || bit_num >= n)
+	G_fatal_error("Rast__check_null_bit: index %d out of range (size = %d).",
+		      bit_num, n);
+
+
     /* find the index of the unsigned char in which this bit appears */
-    ind = Rast__null_bitstream_size(bit_num + 1) - 1;
+    ind = bit_num / 8;
 
-    /* find how many unsigned chars the buffer with bit_num+1 (counting from 0
-       has and subtract 1 to get unsigned char index */
-    if (ind > Rast__null_bitstream_size(n) - 1) {
-	G_warning("Rast__check_null_bit: Unable to access index %d. "
-		  "Size of flags is %d (bit # is %d)",
-		  ind, Rast__null_bitstream_size(n) - 1, bit_num);
-	return -1;
-    }
+    offset = bit_num & 7;
 
-    offset = (ind + 1) * 8 - bit_num - 1;
-
-    return ((flags[ind] & ((unsigned char)1 << offset)) != 0);
+    return ((flags[ind] & ((unsigned char)0x80 >> offset)) != 0);
 }
 
 /*!

Modified: grass/trunk/lib/raster/range.c
===================================================================
--- grass/trunk/lib/raster/range.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/range.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -463,7 +463,8 @@
 			      struct FPRange *range,
 			      RASTER_MAP_TYPE data_type)
 {
-    DCELL val = 0L;
+    size_t size = Rast_cell_size(data_type);
+    DCELL val = 0.0;
 
     while (n-- > 0) {
 	switch (data_type) {
@@ -479,7 +480,7 @@
 	}
 
 	if (Rast_is_null_value(rast, data_type)) {
-	    rast = G_incr_void_ptr(rast, Rast_cell_size(data_type));
+	    rast = G_incr_void_ptr(rast, size);
 	    continue;
 	}
 	if (range->first_time) {
@@ -494,7 +495,7 @@
 		range->max = val;
 	}
 
-	rast = G_incr_void_ptr(rast, Rast_cell_size(data_type));
+	rast = G_incr_void_ptr(rast, size);
     }
 }
 

Modified: grass/trunk/lib/raster/set_window.c
===================================================================
--- grass/trunk/lib/raster/set_window.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/set_window.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -96,9 +96,8 @@
 	else {
 	    /* opened for writing */
 	    G_free(fcb->data);
-	    fcb->data = (unsigned char *)G_calloc(G__.window.cols,
-						  Rast_cell_size(fcb->
-								 map_type));
+	    fcb->data = G_calloc(G__.window.cols,
+				 Rast_cell_size(fcb-> map_type));
 	}
 
 	/* allocate null bitstream buffers for reading/writing null rows */

Modified: grass/trunk/lib/raster/zero_cell.c
===================================================================
--- grass/trunk/lib/raster/zero_cell.c	2010-01-16 03:32:30 UTC (rev 40463)
+++ grass/trunk/lib/raster/zero_cell.c	2010-01-16 06:06:00 UTC (rev 40464)
@@ -11,6 +11,7 @@
  * \author Original author CERL
  */
 
+#include <string.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
 
@@ -40,17 +41,9 @@
  * using Rast_allocate_c_buf().
  *
  * \param rast data buffer
- * \param data_type raster type (CELL, FCELL, DCELL)
+ * \param data_type raster type (CELL_TYPE, FCELL_TYPE, DCELL_TYPE)
  */
 void Rast_zero_buf(void *rast, RASTER_MAP_TYPE data_type)
 {
-    int i;
-    unsigned char *ptr;
-
-    /* assuming that the size of unsigned char is 1 byte */
-    i = G_window_cols() * Rast_cell_size(data_type);
-    ptr = (unsigned char *)rast;
-
-    while (i--)
-	*ptr++ = 0;
+    memset(rast, 0, G_window_cols() * Rast_cell_size(data_type));
 }



More information about the grass-commit mailing list