Revision: 5904
          http://sourceforge.net/p/jump-pilot/code/5904
Author:   ma15569
Date:     2018-06-26 18:21:04 +0000 (Tue, 26 Jun 2018)
Log Message:
-----------
Add amethod to convert a java.awt.image.Raster to 2DArray (Matrix) as double

Modified Paths:
--------------
    
core/trunk/src/org/openjump/core/rasterimage/sextante/rasterWrappers/GridRasterWrapper.java

Modified: 
core/trunk/src/org/openjump/core/rasterimage/sextante/rasterWrappers/GridRasterWrapper.java
===================================================================
--- 
core/trunk/src/org/openjump/core/rasterimage/sextante/rasterWrappers/GridRasterWrapper.java
 2018-06-26 16:26:40 UTC (rev 5903)
+++ 
core/trunk/src/org/openjump/core/rasterimage/sextante/rasterWrappers/GridRasterWrapper.java
 2018-06-26 18:21:04 UTC (rev 5904)
@@ -1,7 +1,6 @@
 package org.openjump.core.rasterimage.sextante.rasterWrappers;
 
 import java.awt.Point;
-import java.awt.image.BufferedImage;
 import java.awt.image.Raster;
 import java.awt.image.SampleModel;
 import java.awt.image.WritableRaster;
@@ -96,7 +95,7 @@
     }
 
     /**
-     * Convert a 2DArray (Matrix) as double to java.awt.image.Raster
+     * Convert a 2DArray (Matrix) as double to java.awt.image.Raster, to band 0
      * 
      * @param 2D Array as double
      * @param SampleModel
@@ -103,6 +102,22 @@
      * @return java.awt.image.Raster
      */
     public static Raster matrixToRaster(double[][] matrix, SampleModel model) {
+
+        return matrixToRaster(matrix, model, 0);
+    }
+
+    /**
+     * Convert a 2DArray (Matrix) as double to java.awt.image.Raster, defining
+     * the band number
+     * 
+     * @param 2D Array as double
+     * @param SampleModel
+     * @param band
+     *            number
+     * @return java.awt.image.Raster
+     */
+    public static Raster matrixToRaster(double[][] matrix, SampleModel model,
+            int band) {
         final int w = matrix.length;
         final int h = matrix[0].length;
         final WritableRaster raster = Raster.createWritableRaster(model,
@@ -109,7 +124,7 @@
                 new Point(0, 0));
         for (int i = 0; i < w; i++) {
             for (int j = 0; j < h; j++) {
-                raster.setSample(i, j, 0, matrix[i][j]);
+                raster.setSample(i, j, band, matrix[i][j]);
             }
         }
         return raster;
@@ -122,18 +137,20 @@
      * @return 2DArray (Matrix) as double[][]
      */
 
-    public static Raster matrixToRaster(double[][] matrix) {
-        final int w = matrix.length;
-        final int h = matrix[0].length;
-        final BufferedImage image = new BufferedImage(w, h,
-                BufferedImage.TYPE_USHORT_GRAY);
-        final WritableRaster raster = (WritableRaster) image.getData();
-        for (int i = 0; i < w; i++) {
-            for (int j = 0; j < h; j++) {
-                raster.setSample(i, j, 0, matrix[i][j]);
+    public static double[][] rasterToMatrix(Raster raster) {
+        try {
+            final int w = raster.getWidth(), h = raster.getHeight();
+            final double pixels[][] = new double[w][h];
+            for (int x = 0; x < w; x++) {
+                for (int y = 0; y < h; y++) {
+                    pixels[x][y] = raster.getSampleDouble(x, y, 0);
+                }
             }
+            return pixels;
+        } catch (final Exception e) {
+            e.printStackTrace();
         }
-        return raster;
+        return null;
     }
 
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to