/* * @(#) ImageInfoUtil.java 1.0 2000/2/10 * Copyright (c) 2000 Larry Rodrigues */ package com.vistech.util; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.swing.*; import javax.swing.event.*; import java.awt.color.*; import com.sun.image.codec.jpeg.*; /** A utility class for obtaining a variety of image information. * @version 1.0 10 Feb 2000 * @author Lawrence Rodrigues */ public class ImageInfoUtil{ public final static int RED = 0; public final static int GREEN = 1; public final static int BLUE = 2; public final static int ALPHA = 3; public final static int DIRECT = 0; public final static int INDEX = 1; public final static int COMPONENT = 2; public final static int COMPONENT_SAMPLE = 0; public final static int BANDED = 1; public final static int PIXEL_INTERLEAVED = 2; public final static int SINGLE_PIXEL_PACKED =3; public final static int MULTI_PIXEL_PACKED = 4; public static String[][] getImageInfo(BufferedImage img) { SampleModel sm = img.getSampleModel(); WritableRaster wr = img.getRaster(); DataBuffer db = wr.getDataBuffer(); ColorModel cm = img.getColorModel(); String str = getColorModelAsText(getColorModelType(cm)); ColorSpace cs = cm.getColorSpace(); String[][] rowData = new String[10][2]; int i =0; rowData[i][0] = "Color Model"; rowData[i++][1] = str; rowData[i][0] = "Color space"; rowData[i++][1] = getColorSpaceAsText(cs.getType()); rowData[i][0] = "Sample Model"; rowData[i++][1] = getSampleModelAsText(getSampleModelType(sm)); //DataBuffer db = wr.getDataBuffer(); rowData[i][0] = "Data type"; rowData[i++][1] = getDataTypeAsText(db.getDataType()); int numbands = sm.getNumBands(); rowData[i][0] = "Number of Bands"; rowData[i++][1] = Integer.toString(numbands); int numbanks = db.getNumBanks(); rowData[i][0] = "Number of Banks"; rowData[i++][1] = Integer.toString(numbanks); rowData[i][0] = "Width"; rowData[i++][1] = Integer.toString(img.getWidth()); rowData[i][0] = "Height"; rowData[i++][1] = Integer.toString(img.getHeight()); int scanlineStride=0; if(sm instanceof ComponentSampleModel) { ComponentSampleModel csm = (ComponentSampleModel)sm; scanlineStride = csm.getScanlineStride(); rowData[i][0] = "Scanline stride"; rowData[i++][1] = Integer.toString(scanlineStride); int pixelStride = csm.getPixelStride(); rowData[i][0] = "Pixel stride"; rowData[i++][1] = Integer.toString(pixelStride); //int[] bankIndices = csm.getBankIndices(); //System.out.println("bank indices = " ); //for(int j=0; j max) max = pix; if(pix < min ) min = pix; sum += pix; } double average = sum/pixels.length; stats[j][0] = min; stats[j][1] = max; stats[j][2] = (int)average; } return stats; } public static int[][] getImageStats(BufferedImage img){ if(img == null) return null; SampleModel sm = img.getSampleModel(); WritableRaster wr = img.getRaster(); DataBuffer db = wr.getDataBuffer(); ColorModel cm = img.getColorModel(); return ImageInfoUtil.getImageStats(sm, db, new Dimension(img.getWidth(), img.getHeight())); } /** * @return a two dimensional array. First dimension is the component and second is the stats **/ public static int[][] getImageStats(SampleModel sm, DataBuffer db, Dimension imageSize){ int imageWidth = imageSize.width; int imageHeight = imageSize.height; int pixel[][] = new int[imageHeight*imageWidth][]; for(int i=0;i max[j]) max[j] = pix; if(pix < min[j] ) min[j] = pix; sum[j] += pix; } //System.out.println("min = "+min[j]+ " max = "+max[j]+ " average = "+ sum[j]/(imageHeight*imageWidth)); imageStats[j][0] = min[j]; imageStats[j][1] = max[j]; imageStats[j][2] = sum[j]/(imageHeight*imageWidth); } return imageStats; } public static int[][] getPixelSamples(BufferedImage img) { WritableRaster wr = img.getRaster(); Dimension size = new Dimension(img.getWidth(), img.getHeight()); return getPixelSamples(wr, size); } public static int[][] getPixelSamples(Raster raster, Dimension imageSize){ if((raster == null) || (imageSize ==null)) return null; SampleModel sm = raster.getSampleModel(); DataBuffer db = raster.getDataBuffer(); int imageWidth = imageSize.width; int imageHeight = imageSize.height; int totalPix = imageHeight*imageWidth; int sample[][] = new int[totalPix][]; for(int i=0;i