Tk3813 Lecture6


Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Tk3813 Lecture6

  1. 1. Lecture 5: Images in Java (2) TK3813
  2. 2. Contents <ul><li>The Java2D API </li></ul><ul><li>Java Advanced Imaging </li></ul>
  3. 3. The Java2D API <ul><li>The producer-consumer approach is conceptually sound but rather clumsy for straightforward image processing applications. </li></ul><ul><li>This deficiency has been addressed with the introduction of the Java2D Application Programming Interface (API) into Java. With the release of Java 2, Java2D has become one of the standard APIs provided with the language. </li></ul><ul><li>The Java2D API consists of a number of classes, distributed amongst the old packages java.awt and java.awt.image and six new packages: </li></ul><ul><ul><li>java.awt.color. </li></ul></ul><ul><ul><li>java.awt. font </li></ul></ul><ul><ul><li>java.awt.geom </li></ul></ul><ul><ul><li>java.awt.print </li></ul></ul><ul><ul><li>java.awt.image.renderable </li></ul></ul><ul><ul><li>com.sun.image.codec.jpeg </li></ul></ul>
  4. 4. Java2D API <ul><li>Our discussion of Java 2D API imaging includes the following topics: </li></ul><ul><ul><li>BufferedImage class - concrete subclass of the abstract Image subclass. It offers far more sophisticated and elaborate features than Image. </li></ul></ul><ul><ul><li>Raster and WritableRaste. </li></ul></ul><ul><ul><li>Reading a BufferedImage. </li></ul></ul>
  5. 5. BufferedImage <ul><li>With Java 1.2 and the Java2D classes, came the java.awt.image.BufferedImage, which is a subclass of Image. It provides a more open and structured internal design with much greater access to the image data. </li></ul>BufferedImage DataBuffer ColorModel Raster SampleModel Composition of a BufferedImage object
  6. 6. BufferedImage <ul><li>BufferedImage includes a ColorModel object and a Raster object, which in turn contains instances of SampleModel and Databuffer. </li></ul><ul><ul><li>A color model refers to how to the data describes the colors as they will appear in the display. </li></ul></ul><ul><ul><li>A common type of color model is RGB model. Java offers an RGB model where an alpha or transparency factor is included with the RGB values. One can also have grey scale models and index models where a data value holds an index into a palette of a fixed number of colors. </li></ul></ul><ul><ul><li>The raster refers to the data that specifies the colors of the individual pixels. </li></ul></ul><ul><ul><li>The Raster instance is actually composed of the raw data in a Databuffer and a SampleModel that details how the raw data values get translated into the color specifications for the model. </li></ul></ul>
  7. 7. BufferedImage <ul><ul><li>The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. </li></ul></ul><ul><ul><li>All BufferedImage objects have an upper left corner coordinate of (0, 0). </li></ul></ul><ul><ul><li>Any Raster used to construct a BufferedImage must therefore have minX=0 and minY=0. </li></ul></ul>
  8. 8. BufferedImage <ul><li>The constructor of BufferedImage takes image dimensions and image type as it parameters. There are 13 different standard image type, representing different combination of ColorModel and SampleModel. </li></ul>* * Selected types of BufferedImage, * the most useful types for our purposes. 8-bit red, green and blue samples; packed into an int. TYPE_INT_RGB 8-bit blue, green and red samples; each stored in one byte. TYPE_3BYTE_BGR 16-bit sample for each pixel; stored in a short. TYPE_UNSHORT_GRAY 8-bit sample for each pixel; stored in a byte. TYPE_BYTE_GRAY 1-bit sample for each pixel; 8 samples packed into a byte. TYPE_BYTE_BINARY Description Constant
  9. 9. BufferedImage <ul><li>The dimension and type of an existing BufferedImage object can be acquired using: </li></ul><ul><ul><li>getWidth() </li></ul></ul><ul><ul><li>getHeight() </li></ul></ul><ul><ul><li>getType() </li></ul></ul><ul><li>Its colour can be retrieve with getColorModel(). </li></ul><ul><li>Pixel values can be inspected or modify: </li></ul><ul><ul><li>getRGB() </li></ul></ul><ul><ul><li>setRGB() </li></ul></ul>
  10. 10. Method getRGB() and setRGB() provided by the BufferedImage class int getRGB(int x, int y); int[] getRGB(int x, int y, int w, int h, int[] data, int offset,int scansize); void setRGB(int x, int y, int value); void setRGB(int x, int y, int w, int h, int[] data, int offset,int scansize);
  11. 11. Raster and WritableRaster <ul><li>Java2D’s Raster class has methods that manipulate the samples of a pixel directly, without wasteful interpretation imposed by a ColorModel. </li></ul><ul><ul><li>Read-only class. </li></ul></ul><ul><ul><li>A subclass of Raster, i.e. WritableRaster can change pixel’s values. </li></ul></ul>
  12. 12. Raster and WritableRaster <ul><li>The pixel access method of Raster and WritableRaster have 2 broad classes: </li></ul><ul><ul><li>Pixel methods: </li></ul></ul><ul><ul><ul><li>getPixel() </li></ul></ul></ul><ul><ul><ul><li>setPixel() </li></ul></ul></ul><ul><ul><li>Sample methods: operate on a particular sample of each pixel in a rectangular block. </li></ul></ul><ul><ul><ul><li>getSample() </li></ul></ul></ul><ul><ul><ul><li>setSample() </li></ul></ul></ul>
  13. 13. int[] getPixel(int x, int y, int[] data) float[] getPixel(int x, int y, float[] data) double[] getPixel(int x, int y, double[] data) int[] getPixels(int x, int y, mt w, mt h, int[] data) float[] getPixels(int x, int y, int w, int h, float[] data) double[] getPixels(int x, int y, int w, int h, double[] data) int getSample(int x, int y, int band) float getSampleFloat(int x, int y, int band) double getSampleDouble(int x, int y, int band) int[] getSamples(int x, int y, int w, int h, int band, int[] data) float[] getsamples(int x, int y, int w, int h, int band, float[] data) double[] getSamples(int x, int y, int w, int h, int band, double[] data) Table 4.4 Methods provided by the Raster class to inspect pixel values.
  14. 14. void setPixel(int x, int y, int[] data) void setPixel(int x, int y, float[] data) void setPixel(int x, int y, double[] data) void setPixels(int x, int y, int w, int h, int[] data) void setPixels(int x, int y, int w, int h, float[] data) void setPixels(int x, int y, int w, int h, double[] data) void setSample(int x, int y, int band, int value) void setSample(int x, int y, int band, float value) void setSample(int x, int y, int band, double value) void setSample(int x, int y, int w, int h, int band, int[] data) void setSample(int x, int y, int w, int h, int band, float[] data) void setSample(int x, int y, int w, int h, int band, double[] data) Table 4.5 Methods provided by WritableRaster to modify pixel values.
  15. 15. Raster and WritableRaster <ul><li>Parameter band used by sample-based methos: </li></ul><ul><ul><li>Always 0 for greyscale images. </li></ul></ul><ul><ul><li>0,1 or 2 corresponding to red, green and blue bands </li></ul></ul>
  16. 16. Example <ul><li>Suppose that we have a greyscale BufferedImage called image and we wish to divide all its grey levels by two. </li></ul>WritableRaster raster = image.getRaster(); int value; for(int y=0;y<image.getHeight();++y) for(int x=0;x<image.getWidth();++x) { value = raster.getSample(x,y,0)/2; raster.setDample(x,y,0,value); }
  17. 17. Reading a BufferedImage <ul><li>The package com.sun.image.coded.jpeg contains classes to support the reading of images from, and writing images to, datastreams that have been compressed using JPEG compression. </li></ul><ul><ul><li>JPEGImageDecoder(reading) </li></ul></ul><ul><ul><li>JPEGImageEncoder(writing) </li></ul></ul>Example: To load an image from file “in.jpg” FileInputStream fileStream = new FileInputStream(“in.jpg”); JPEGImageDecoder input=JPEGCodec.createJPEGDecoder(fileStream); BufferedImage image=input.decodeAsBufferedImage(); Example: Writing the image out to a file “out.jpg” FileOutputStream fileStream = new FileOutputStream(“out.jpg”); JPEGImageEncoder output=JPEGCodec.createJPEGEncoder(fileStream); output.encode(image);
  18. 18. More info <ul><li> </li></ul><ul><li> </li></ul>
  19. 19. Java Advanced Imaging (JAI) <ul><li>The JAI, provides tools for &quot;sophisticated, high performance image processing.&quot; </li></ul><ul><li>First developed in the late 1990s some of the JAI features are now available in J2SE5.0 (e.g. I/O for a wide range of image formats) but it still provides a large and robust set of imaging tools. </li></ul><ul><li>For example, many high end imaging tasks involve dealing with extremely large images. JAI allows for &quot;tiling&quot; , in which users can grab a section of an image for display and thus save lots of bandwidth. </li></ul>
  20. 20. Java Advanced Imaging (JAI) <ul><li>Features include: </li></ul><ul><ul><li>Delay execution of image processing task until they are needed. This &quot;just-in-time&quot; approach provides for faster response to the user since not time is wasted on unneeded computation. </li></ul></ul><ul><ul><li>Built-in RMI (Remote Method Invocation) allows for distributing image tasks and client interaction over the network. </li></ul></ul><ul><ul><li>Extensible framework allows for expansion of capabilities via plug-in modules developed by users and third parties. </li></ul></ul>
  21. 21. Applications of JAI <ul><li>JSky is used by astronomers to plan observations for the Gemini Telescope . The JSky home site describes how the program manages catalogs of imagery obtained in many formats and provides imaging tools to analyze individual images as well as to combine and compare multiple images. </li></ul><ul><li>JAI is used as part of the Scientist's Expert Assistant developed at NASA Goddard Space Flight Center in conjunction with the Space Telescope Science Institute. This is &quot;a system that combines interactive visual and expert system approaches to assist astronomers in planning observations.&quot; See the papers and presentations describing the program. </li></ul>
  22. 22. Applications of JAI <ul><li>GeoVirgil is a &quot;planetary data analysis tool works as a map program (or Graphical Information System) for NASA planetary imagery. Developed by Steve McDonald, it &quot;combines imagery, elevation and other data sets into an integrated map view.&quot; Various image processing functions are available. A 3D version is under development. </li></ul><ul><li>AstroVirgil is another Java program from Steve McDonald that is used to analyze data from the space-based Chandra X-ray telescope . </li></ul><ul><li>JadeDisplay ( is &quot;a high-performance image display component for serious imaging applications using JAI. It works asynchronously, loading/computing image tiles in background threads. This frees up the GUI thread so the user doesn't have to wait for the entire image to load, and scrolling is fast regardless of image size (as long as it is tiled).&quot; </li></ul>
  23. 23. Questions ???