[GRASS-SVN] r53965 - in grass-addons/grass7/imagery: . i.rotate

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 21 21:21:52 PST 2012


Author: ychemin
Date: 2012-11-21 21:21:51 -0800 (Wed, 21 Nov 2012)
New Revision: 53965

Modified:
   grass-addons/grass7/imagery/Makefile
   grass-addons/grass7/imagery/i.rotate/main.c
Log:
replaced matrix alloc, updated Makefile

Modified: grass-addons/grass7/imagery/Makefile
===================================================================
--- grass-addons/grass7/imagery/Makefile	2012-11-21 22:39:14 UTC (rev 53964)
+++ grass-addons/grass7/imagery/Makefile	2012-11-22 05:21:51 UTC (rev 53965)
@@ -8,9 +8,11 @@
 	i.evapo.potrad \
 	i.evapo.senay \
 	i.evapo.zk \
+	i.flip \
 	i.histo.match \
 	i.lmf \
 	i.points.auto \
+	i.rotate \
 	i.segment \
 	i.vi.mpi
 

Modified: grass-addons/grass7/imagery/i.rotate/main.c
===================================================================
--- grass-addons/grass7/imagery/i.rotate/main.c	2012-11-21 22:39:14 UTC (rev 53964)
+++ grass-addons/grass7/imagery/i.rotate/main.c	2012-11-22 05:21:51 UTC (rev 53965)
@@ -22,22 +22,26 @@
 #include <grass/raster.h>
 #include <grass/glocale.h>
 #include <math.h>
-#include <grass/la.h>
 
+
+double** ad2d(int X, int Y) {
+	int i;
+	double** A = (double**) malloc(X*sizeof(double*));
+	for (i = 0; i < X; i++)
+   		A[i] = (double*) malloc(Y*sizeof(double));
+   	return A;
+} 
+
 int main(int argc, char *argv[]) 
 {
     int nrows, ncols;
     int row, col;
     struct GModule *module;
-    struct Option *input1, *output1;
+    struct Option *in, *out;
     struct History history;	/*metadata */
-    char *result1;		/*output raster name */
-    int infd;
-    int outfd1;
-    char *rnetday;
-    void *inrast;
+    int infd, outfd;
+    void *inrast, *outrast;
 
-    DCELL * outrast1;
     G_gisinit(argv[0]);
 
     module = G_define_module();
@@ -48,46 +52,53 @@
 	_("Rotates the image around the centre of the computational window");
     
     /* Define the different options */ 
-    input1 = G_define_standard_option(G_OPT_R_INPUT);
-    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+    in = G_define_standard_option(G_OPT_R_INPUT);
+    out = G_define_standard_option(G_OPT_R_OUTPUT);
 
     if (G_parser(argc, argv))
         exit(EXIT_FAILURE);
 
-    rnetday = input1->answer;
-    result1 = output1->answer;
-    
-    infd = Rast_open_old(rnetday, "");
+    infd = Rast_open_old(in->answer, "");
     inrast = Rast_allocate_d_buf();
     
     nrows = Rast_window_rows();
     ncols = Rast_window_cols();
-    outrast1 = Rast_allocate_d_buf();
-    outfd1 = Rast_open_new(result1, DCELL_TYPE);
 
+    outrast = Rast_allocate_d_buf();
+    outfd = Rast_open_new(out->answer, DCELL_TYPE);
+
     /*mat_struct *G_matrix_init(int rows, int cols, int ldim)*/
     /*Increase dimension of image to > of sqrt(2) to fit rotated angles*/
-    int matnrows=nrows*1;
-    int matncols=ncols*1;
-    int matN=matnrows*matncols;
-    mat_struct *matin = G_matrix_init (matnrows, matncols, matN);
-    G_matrix_zero(matin);
-    mat_struct *matout = G_matrix_init (matnrows, matncols, matN);
-    G_matrix_zero(matout);
+    int matnrows=nrows*2;
+    int matncols=ncols*2;
     int deltarow=0.5*(matnrows-nrows);
     int deltacol=0.5*(matncols-ncols);
+    double **matin = ad2d (matnrows, matncols);
+    double **matout = ad2d (matnrows, matncols);
+    for (row=0;row<matnrows;row++){
+        for(col=0;col<matncols;col++){
+	    matin[row][col]=-999.99;
+	    matout[row][col]=-999.99;
+        }
+    }
     DCELL d;
     /* Load input matrix with row&col shift to keep center of image*/ 
     for (row = 0; row < nrows; row++)
+    {
 	Rast_get_d_row(infd,inrast,row);
-    for (col = 0; col < ncols; col++)
-    {
+        for (col = 0; col < ncols; col++)
+        {
             d = ((DCELL *) inrast)[col];
 	    if (Rast_is_d_null_value(&d))
-		matin[row+deltarow][col+deltacol]=-999.99;
-	    else {
+	    {
+	        //SKIPIT 
+		//matin[row+deltarow][col+deltacol]=-999.99;
+	    } 
+	    else 
+	    {
 		matin[row+deltarow][col+deltacol] = d;
 	    }
+        }
     }
     double theta=20.0;
     double thetarad=theta*3.1415927/180;
@@ -96,30 +107,36 @@
     int newrow,newcol;
     /*Rotate the matrix*/
     for (row = 0; row < nrows; row++)
-    for (col = 0; col < ncols; col++)
     {
-	newcol=(col-0.5*ncols)*costheta+(row-0.5*nrows)*sintheta+0.5*ncols;
-	newrow=(row-0.5*nrows)*costheta-(col-0.5*ncols)*sintheta+0.5*nrows;
-        matout[newrow][newcol] = matin[row][col];
+        for (col = 0; col < ncols; col++)
+        {
+            newcol=(col-0.5*ncols)*costheta+(row-0.5*nrows)*sintheta+0.5*ncols;
+            newrow=(row-0.5*nrows)*costheta-(col-0.5*ncols)*sintheta+0.5*nrows;
+            matout[newrow][newcol] = matin[row][col];
+        }
     }
 
     /*Output to raster file*/
     for (row = 0; row < nrows; row++)
     {
-    for (col = 0; col < ncols; col++)
-    {
-        outrast1[col]=matout[row][col];
+        for (col = 0; col < ncols; col++)
+        {
+	    if (matout[row][col]==-999.99){
+	        Rast_set_d_null_value(&outrast[col]);
+	    } else {
+                ((DCELL *)outrast)[col] = matout[row][col];
+	    }
+        }
+        Rast_put_d_row(outfd,outrast);
     }
-    Rast_put_d_row(outfd1,outrast1);
-    }
 
     G_free(inrast);
     Rast_close(infd);
-    G_free(outrast1);
-    Rast_close(outfd1);
-    Rast_short_history(result1, "raster", &history);
+    G_free(outrast);
+    Rast_close(outfd);
+    Rast_short_history(outName, "raster", &history);
     Rast_command_history(&history);
-    Rast_write_history(result1, &history);
+    Rast_write_history(outName, &history);
     exit(EXIT_SUCCESS);
 }
 



More information about the grass-commit mailing list