I create a GrayScale Buffered Image (hereafter referred to as GBI) of type
BufferedImage.TYPE_BYTE_GRAY. I display the GBI using
drawRenderedImage(BufferedImage, AffineTransform) method passing the GBI and
the scaling AffineTransform with scaling factor of >=1.0. The display looks
fine. Now I display the same GBI with a scaling factor of 0.5 (or any
scaling factor < 1) but the display of the image looks quite different from
the expected result. It seems that the gray level values have changed on
scaling down the image which should not happen. Also the repainting of the
image becomes very slow (which can be tested by bringing another window of
some other application on top of the image and moving that window over the
image) as compared to painting when the scaling factor is >= 1.0 .
Also if I create a GrayScale Buffered Image (hereafter referred to as GBI)
of type BufferedImage.TYPE_USHORT_GRAY, the same problem is still there for
scaling factor < 1.0. Moreover, a new problem crops up with the scaling
factor of > 1.0. The display of the image looks very weird. It looks like
that the dimensions of the scaled image are not correct and the gray level
values of the entire image change. Also the painting of the image is very
slow. But the image looks fine for scaling factor equal to 1.0 .

Here is the code for creating and displaying BYTE and USHORT type image:

At the command line, the scaling factor (in float) can be entered to see the
image scaled at

that scaling factor.

I have used JDK1.2 platform.


BYTE GrayScaleImage
------------------

import java.awt.color.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class MyClass1  extends JFrame
{

    BufferedImage bf = null;
    float scalingFactor = 1.0f;
    public MyClass1(float scalingFactor)
    {
        this.scalingFactor = scalingFactor;
        int width = 256;
        int height = 256;

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int number = cs.getNumComponents();
        System.out.println("Number of Components "+number);
        int[] nBits = {8};
        ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
true,

Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
        SampleModel sampleModel =
colorModel.createCompatibleSampleModel(width,height);

DataBuffer dataBuffer = sampleModel.createDataBuffer();
        byte count=0;
        byte[] b = new byte[1];
        for(int y=0;y<height;y++)
        {
           for(int x=0;x<width;x++)
           {
               b[0] = count;
               sampleModel.setDataElements(x,y,b,dataBuffer);
           }
           count++;
        }
        WritableRaster wr = Raster.createWritableRaster(sampleModel,
dataBuffer, new

Point(0,0));
       bf = new BufferedImage(colorModel, wr,true,null);
        setSize(200,200);
        setBackground(Color.darkGray);
        setVisible(true);
   }

    public void paint(Graphics g)
    {
        Graphics2D g2D = (Graphics2D)g;
        g2D.drawRenderedImage(bf,
AffineTransform.getScaleInstance(scalingFactor,

scalingFactor));
    }

    public static void main(String[] args) throws Exception
    {
        float scalingFactor = Float.parseFloat(args[0]);
        MyClass1 myClass1 = new MyClass1(scalingFactor);

    }

}// end class


USHORT GrayScaleImage
------------------

import java.awt.color.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class MyClass1  extends JFrame
{

    BufferedImage bf = null;
    float scalingFactor = 1.0f;
    public MyClass1(float scalingFactor)
    {
        this.scalingFactor = scalingFactor;
        int width = 512
        int height = 512

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int number = cs.getNumComponents();
        System.out.println("Number of Components "+number);
        int[] nBits = {9};
        ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
true,

Transparency.OPAQUE,DataBuffer.TYPE_USHORT);
        SampleModel sampleModel =
colorModel.createCompatibleSampleModel(width,height);

DataBuffer dataBuffer = sampleModel.createDataBuffer();
        short count=0;
        short[] b = new short[1];
        for(int y=0;y<height;y++)
        {
           for(int x=0;x<width;x++)
           {
               b[0] = count;
               sampleModel.setDataElements(x,y,b,dataBuffer);
           }
           count++;
        }
        WritableRaster wr = Raster.createWritableRaster(sampleModel,
dataBuffer, new

Point(0,0));
       bf = new BufferedImage(colorModel, wr,true,null);
        setSize(200,200);
        setBackground(Color.darkGray);
        setVisible(true);
   }

    public void paint(Graphics g)
    {
        Graphics2D g2D = (Graphics2D)g;
        g2D.drawRenderedImage(bf,
AffineTransform.getScaleInstance(scalingFactor,

scalingFactor));
    }

    public static void main(String[] args) throws Exception
    {
        float scalingFactor = Float.parseFloat(args[0]);
        MyClass1 myClass1 = new MyClass1(scalingFactor);

    }

}// end class


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

Reply via email to