Tk3813 Lecture6

23
Lecture 5: Images in Java Lecture 5: Images in Java (2) (2) TK3813 TK3813

Transcript of Tk3813 Lecture6

Page 1: Tk3813 Lecture6

Lecture 5: Images in Java (2)Lecture 5: Images in Java (2)Lecture 5: Images in Java (2)Lecture 5: Images in Java (2)

TK3813TK3813

Page 2: Tk3813 Lecture6

April 12, 2023 TK3813 2

Contents

The Java2D APIJava Advanced Imaging

Page 3: Tk3813 Lecture6

April 12, 2023 TK3813 3

The Java2D APIThe producer-consumer approach is conceptually sound but rather clumsy for straightforward image processing applications.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.The Java2D API consists of a number of classes, distributed amongst the old packages java.awt and java.awt.image and six new packages:

java.awt.color.java.awt. font java.awt.geom java.awt.printjava.awt.image.renderablecom.sun.image.codec.jpeg

Page 4: Tk3813 Lecture6

April 12, 2023 TK3813 4

Java2D API

Our discussion of Java 2D API imaging includes the following topics:

BufferedImage class - concrete subclass of the abstract Image subclass. It offers far more sophisticated and elaborate features than Image.

Raster and WritableRaste.

Reading a BufferedImage.

Page 5: Tk3813 Lecture6

April 12, 2023 TK3813 5

BufferedImage

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.

BufferedImage

DataBuffer

ColorModelRaster

SampleModel

Composition of a BufferedImage object

Page 6: Tk3813 Lecture6

April 12, 2023 TK3813 6

BufferedImage

BufferedImage includes a ColorModel object and a Raster object, which in turn contains instances of SampleModel and Databuffer.

A color model refers to how to the data describes the colors as they will appear in the display. 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. The raster refers to the data that specifies the colors of the individual pixels. 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.

Page 7: Tk3813 Lecture6

April 12, 2023 TK3813 7

BufferedImage

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. All BufferedImage objects have an upper left corner coordinate of (0, 0). Any Raster used to construct a BufferedImage must therefore have minX=0 and minY=0.

Page 8: Tk3813 Lecture6

April 12, 2023 TK3813 8

BufferedImage

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.

Constant Description

TYPE_BYTE_BINARY 1-bit sample for each pixel; 8 samples packed into a byte.

TYPE_BYTE_GRAY 8-bit sample for each pixel; stored in a byte.

TYPE_UNSHORT_GRAY 16-bit sample for each pixel; stored in a short.

TYPE_3BYTE_BGR 8-bit blue, green and red samples; each stored in one byte.

TYPE_INT_RGB 8-bit red, green and blue samples; packed into an int.

*

*

Selected types of BufferedImage, * the most useful types for our purposes.

Page 9: Tk3813 Lecture6

April 12, 2023 TK3813 9

BufferedImage

The dimension and type of an existing BufferedImage object can be acquired using:

getWidth()getHeight()getType()

Its colour can be retrieve with getColorModel().Pixel values can be inspected or modify:

getRGB()setRGB()

Page 10: Tk3813 Lecture6

April 12, 2023 TK3813 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);

Page 11: Tk3813 Lecture6

April 12, 2023 TK3813 11

Raster and WritableRaster

Java2D’s Raster class has methods that manipulate the samples of a pixel directly, without wasteful interpretation imposed by a ColorModel.

Read-only class.A subclass of Raster, i.e. WritableRaster can change pixel’s values.

Page 12: Tk3813 Lecture6

April 12, 2023 TK3813 12

Raster and WritableRaster

The pixel access method of Raster and WritableRaster have 2 broad classes:

Pixel methods:getPixel()setPixel()

Sample methods: operate on a particular sample of each pixel in a rectangular block.

getSample()setSample()

Page 13: Tk3813 Lecture6

April 12, 2023 TK3813 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.

Page 14: Tk3813 Lecture6

April 12, 2023 TK3813 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.

Page 15: Tk3813 Lecture6

April 12, 2023 TK3813 15

Raster and WritableRaster

Parameter band used by sample-based methos:

Always 0 for greyscale images.0,1 or 2 corresponding to red, green and blue bands

Page 16: Tk3813 Lecture6

April 12, 2023 TK3813 16

Example

Suppose that we have a greyscale BufferedImage called image and we wish to divide all its grey levels by two.

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);

}

Page 17: Tk3813 Lecture6

April 12, 2023 TK3813 17

Reading a BufferedImage

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.

JPEGImageDecoder(reading)JPEGImageEncoder(writing)

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);

Page 18: Tk3813 Lecture6

April 12, 2023 TK3813 18

More info

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Image.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html

Page 19: Tk3813 Lecture6

April 12, 2023 TK3813 19

Java Advanced Imaging (JAI)

The JAI, provides tools for "sophisticated, high performance image processing." 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.For example, many high end imaging tasks involve dealing with extremely large images. JAI allows for "tiling" , in which users can grab a section of an image for display and thus save lots of bandwidth.

Page 20: Tk3813 Lecture6

April 12, 2023 TK3813 20

Java Advanced Imaging (JAI)

Features include: Delay execution of image processing task until they are needed. This "just-in-time" approach provides for faster response to the user since not time is wasted on unneeded computation. Built-in RMI (Remote Method Invocation) allows for distributing image tasks and client interaction over the network. Extensible framework allows for expansion of capabilities via plug-in modules developed by users and third parties.

Page 21: Tk3813 Lecture6

April 12, 2023 TK3813 21

Applications of JAI

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.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 "a system that combines interactive visual and expert system approaches to assist astronomers in planning observations." See the papers and presentations describing the program.

Page 22: Tk3813 Lecture6

April 12, 2023 TK3813 22

Applications of JAI

GeoVirgil is a "planetary data analysis tool works as a map program (or Graphical Information System) for NASA planetary imagery. Developed by Steve McDonald, it "combines imagery, elevation and other data sets into an integrated map view." Various image processing functions are available. A 3D version is under development.AstroVirgil is another Java program from Steve McDonald that is used to analyze data from the space-based Chandra X-ray telescope.JadeDisplay (http://www.openchannelfoundation.org/projects/JadeDisplay/) is "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)."

Page 23: Tk3813 Lecture6

April 12, 2023 TK3813 23

Questions ???