Featured

# Coordinate Transformation And Image Manipulation.

## Q. How Coordinate Transformation and Image Manipulation is done in AWT ?

Ans. Coordinate Transformation :  Java 2D API maintains two coordinates systems :
(i) User space is a device-independent, logical coordinate system.  Applications use this coordinate system exclusively; all geometries passed into Java 2D rendering routines are specified in user space.

(ii) Device space is a device-dependent coordinate system that varies according to the target rendering device.  In a multi-screen environment with a virtual desktop where a window can span more than one physical screen device, that device coordinate system that's used in the coordinate system of the virtual desktop that encompasses all of the screens.

The easiest way to picture what is happening in a transformation is to imagine that the person doing the drawing has a picture frame that he lays down on top of a sheet of paper.  The drawer always sits at the bottom of the frame.  To apply a translation, you move the frame (also moving the drawer) and do the drawing in the new location.  You then move the frame back to its original location, and what you now see is the final result.  Similarly, for a rotation, you spin the frame ( and the drawer ) draw, then spin back to see the result.  Similarly for scaling and shears : modify the frame without touching the underlying sheet of paper, draw, then reverse the process to see the final result.

You can also perform complex transformation by directly manipulating the underlying arrays that control the transformations.  This type of manipulation is a bit more complicated to envision that the basis translation, rotation, scaling and shear transformations.  The idea is that a new point(X2,Y2) can be derived from an original point (X1,Y1) as follows :

There are four fundamental transformations :
(i) Scaling : Blowing up or shrinking, all distances from a fixed point ;
(ii) Rotation : Rotating all points around a fixed center ;
(ii) Translation : Moving all points by a fixed amount ;
(iv) Shear : Leaving one line fixed and "sliding" the lines parallel to it by an amount that is proportional to the distance from fixed line.

Image Manipulation : If we have an image and we want to improve its appearance.  We then need to access the individual pixels of the image and replace them with other pixels.  Or perhaps we want to compute the pixels of an image from scratch, for example, to show the result of physical measurements or a mathematical computation.  The BufferedImage class gives the control over the pixels in an image, and classes that implement the BufferImageOp Interface let us transform images.

This is a major cahange from the image support in JDK 1.0.  At that time, the image classes were optimized to support incremental rendering.  The original purpose of the classes was to render GIF and JPEG images that are downloaded from the Web, a scan line at a time, as soon as partial image data is available.  In fact, scan lines can be interlaced, with all even scan lines coming first, followed by the odd scan lines.  This mechanism lets a browser quickly display an approximation of the image while fatching the remainder of the image data.  The ImageProducer, ImageFilter, and ImageConsumer interfaces in JDK 1.0 expose all the complexities of incremental rendering.  Writing an image manipulation that fit well into that framework was quite complex.  Fortunately, the need for using these classes has completely gone away.  JMD 1.2 replaced the "push model" of JDK 1.0 with "direct" model that lets us access pixels directly and conveniently.  The only disadvantage of the direct model is that it requires all image pixels to be in memory.  Future versions of the Java Platform may support a "pull" model with which a processing pipeline can reduce memory consumption and increase speed by fetching and processing pixels only when they are actually needed.

Most of the images that we manipulate are simply read in from an image file.  They were either produced by a device such as a digital camera or scanner, or constructed by a drawing program.  To create an image, construct a BufferedImage object in the usual way.
Image   =    New BufferedImage ( width, height, BufferedImage.TYPE_INT_ARGB);

Now, call the greatest method to obtain an object of type WritableRaster.  You use this object to access and modify the pixels of the image.
WritabelRaster raster  =   image.getRaster( ) ;

The setPixel method lets us set an individual pixel.  The complexity here is that we can't simply set the pixel to a Color value.  If the image has a type of TYPE_INT_ARGB, then each pixel is described by four values, for red, green, blue, and alpha, each of which is between 0 and 255.  We supply them in any array of four integers.
int[] black =  {0,0,0,255} ;
raster.setPixel (i,j, black);