public static java . awt . image . BufferedImage makeGrayscaleImage ( Array ma , IsMissingEvaluator missEval ) { if ( ma . getRank ( ) < 2 ) return null ; if ( ma . getRank ( ) == 3 ) ma = ma . reduce ( ) ; if ( ma . getRank ( ) == 3 ) ma = ma . slice ( 0 , 0 ) ; // we need 2D int h = ma . getShape ( ) [ 0 ] ; int w = ma . getShape ( ) [ 1 ] ; DataBuffer dataBuffer = makeDataBuffer ( ma , missEval ) ; WritableRaster raster = WritableRaster . createInterleavedRaster ( dataBuffer , w , h , // int w, int h, w , // int scanlineStride, 1 , // int pixelStride, new int [ ] { 0 } , // int bandOffsets[], null ) ; // Point location) ColorSpace cs = ColorSpace . getInstance ( ColorSpace . CS_GRAY ) ; ComponentColorModel colorModel = new ComponentColorModel ( cs , new int [ ] { 8 } , false , false , Transparency . OPAQUE , DataBuffer . TYPE_BYTE ) ; return new BufferedImage ( colorModel , raster , false , null ) ; }
Adapt a rank 2 array into a java . awt . image . BufferedImage . If passed a rank 3 array take first 2D slice .