26
Lecture10 Lecture10 Java Advanced Imaging Java Advanced Imaging (JAI) API (JAI) API

Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Embed Size (px)

Citation preview

Page 1: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Lecture10Lecture10

Java Advanced Imaging (JAI) Java Advanced Imaging (JAI) APIAPI

Page 2: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Example FirstExample Firstimport java.awt.image.renderable.*;import java.awt.image.renderable.*;import javax.media.jai.*;import javax.media.jai.*;

public class RenderedChainTest {public class RenderedChainTest { public static void main(String[] args) {public static void main(String[] args) {

// Load the image// Load the image// Do Convolution // Do Convolution // Multiply the coefficients// Multiply the coefficients// Save the image// Save the image

}}}}

Page 3: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered Chain Operators: FileLoad

ParameterBlock pb = new ParameterBlock();pb.add(“lena_512.jpg"); RenderedOp originalSourceImage = JAI.create("FileLoad",pb);

Java Advanced Imaging API has this phylosophy that the parameters required for a particular operation are put into ParameterBlock object and calls a some kind of method with operation name and a ParameterBlock object as the parameters.

This ParameterBlock object must be developed to suit the operation to be undertaken.

JAI - JAI - A convenience class for instantiating operations. It has A convenience class for instantiating operations. It has all sorts of create methods to create various operator nodes all sorts of create methods to create various operator nodes in the rendering chain.in the rendering chain.

Page 4: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered chain: Convolve

pb = new ParameterBlock();pb.addSource(originalSourceImage); // supply the source

// to convolve// construct the kernel ( filter really)float oneBys2 = 1/2.0f;float[] kernelData = {oneBys2, -oneBys2, -oneBys2,

oneBys2};KernelJAI haarKernel = new KernelJAI(2,2,0,0,kernelData);pb.add(haarKernel);RenderedOp convolvedImage = JAI.create("Convolve",pb);

Page 5: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

KernelJAIKernelJAI

public public KernelJAIKernelJAI(int width, int height, (int width, int height, int xOrigin, int yOrigin, float[] data) int xOrigin, int yOrigin, float[] data) • Constructs a KernelJAI with the given Constructs a KernelJAI with the given

parameters.parameters.• Parameters:Parameters:

width - the width of the kernel. width - the width of the kernel. height - the height of the kernel. height - the height of the kernel. xOrigin - the X coordinate of the key kernel element. xOrigin - the X coordinate of the key kernel element. yOrigin - the Y coordinate of the key kernel element. yOrigin - the Y coordinate of the key kernel element. data - the float data in row-major format.data - the float data in row-major format.

Page 6: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered Chain: MultiplyConstpb = new ParameterBlock();pb.addSource(convolvedImage);int height = convolvedImage.getHeight();int width = convolvedImage.getWidth();double[height * width] constants;for(int i=0;i < height * width; i++) constants[i] = 40.0;pb.add(constants);RenderedOp constantMultipliedImage =

JAI.create("MultiplyConst",pb);

The MultiplyConst operation takes one rendered or renderable image and an array of double constants, and multiplies every pixel of the same band of the source by the constant from the corresponding array entry.

Page 7: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered Chain: Saving an image

pb = new ParameterBlock();pb.addSource(constantMultipliedImage);

// add source for "FileStore"

pb.add("outputImage.jpg"); // output file namepb.add("JPEG"); // output file formatJAI.create("FileStore",pb);

Page 8: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Babara_512.jpgBabara_512.jpg

Page 9: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

outputImageBabara.jpg outputImageBabara.jpg

Page 10: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered Chain

Page 11: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Rendered Chain Two types of chains: one the RenderedOp chain

and the other OpImage chain. OpImage is the base class for all image

operations. It provides a home for information and functionalities common to all the op-image classes, and implements various utility methods that may be useful to a specific operation.

A RenderedOp stores an operation name, a ParameterBlock containing sources and parameters for the operation, and a RenderingHints containing hints which may be used in rendering the node.

Page 12: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Pull Imaging ModelPull Imaging Model In the original AWT imaging Push Imaging Model was used In the original AWT imaging Push Imaging Model was used

and the center of this model was the Image class.and the center of this model was the Image class. Similarly at the center of Java2D Immediate Mode Imaging Similarly at the center of Java2D Immediate Mode Imaging

Model was the BufferedImage class which implements the Model was the BufferedImage class which implements the RenderedImage interface.RenderedImage interface.

But at the center Java Advanced Imaging API is the But at the center Java Advanced Imaging API is the PlanarImage class which implements the Pull Imaging PlanarImage class which implements the Pull Imaging Model.Model.

When a request is made at the end of the rendered chain to When a request is made at the end of the rendered chain to render the image, this request travels through the chain render the image, this request travels through the chain upto the source and then image data is pulled through the upto the source and then image data is pulled through the chain as needed.chain as needed.

An imaging operator can have zero or more parameters. An imaging operator can have zero or more parameters. Each time the image passes the node, an imaging operator Each time the image passes the node, an imaging operator is applied with appropriate parameters. The transformed is applied with appropriate parameters. The transformed image is passed to the nest node until it reaches a “Sink”.image is passed to the nest node until it reaches a “Sink”.

In general when you apply imaging operators this way, it In general when you apply imaging operators this way, it forms a Directed Acyclic Graph (DAG). JAI has facilities to forms a Directed Acyclic Graph (DAG). JAI has facilities to manipulate this graph. manipulate this graph.

Page 13: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

RenderedImage InterfaceRenderedImage Interface

A rendered image is divided into a number A rendered image is divided into a number of rectangular tiles. Each tile consists of of rectangular tiles. Each tile consists of image data covered by the rectangular image data covered by the rectangular region of the tile.region of the tile.

To map tiles in the image, an imaginary To map tiles in the image, an imaginary grid is thrown over it. Each cell in this grid grid is thrown over it. Each cell in this grid represents a tile in the image. The size of represents a tile in the image. The size of this grid need not exactly match the size this grid need not exactly match the size of the image. of the image.

Page 14: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

RenderedImage AttributesRenderedImage Attributes Geometry related attributes:Geometry related attributes:

• width, height – width and height of the width, height – width and height of the rendered image.rendered image.

• minX, minY – The upper left-hand coordinates minX, minY – The upper left-hand coordinates of a rendered image.of a rendered image.

• numXTiles, numYTiles – The number of tiles in numXTiles, numYTiles – The number of tiles in the x and y directions.the x and y directions.

• minTileX, minTileY – The minimum tile indices minTileX, minTileY – The minimum tile indices in the x and y directions.in the x and y directions.

• tileWidth, tileHeight – Tile width and heighttileWidth, tileHeight – Tile width and height• tileGridOffSetX, tileGridOffSetY – The x and y tileGridOffSetX, tileGridOffSetY – The x and y

offsets of the first tile with respect to the origin offsets of the first tile with respect to the origin of the coordinate system.of the coordinate system.

Page 15: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

RenderedImage AttributesRenderedImage Attributes Image data related attributes:Image data related attributes:

• data – Image data available as a Raster object.data – Image data available as a Raster object.• ColorModel – Color Model.ColorModel – Color Model.• SampleModel – Sample Model.SampleModel – Sample Model.• sources – Sources of image data available as a sources – Sources of image data available as a

Vector object.Vector object.• property – Property of a rendered image. The property – Property of a rendered image. The

getProperties() method returns all the getProperties() method returns all the properties of the rendered image.properties of the rendered image.

• tile – A tile available as a Raster object. The tile – A tile available as a Raster object. The getTile(xIndex,yIndex) method returns the tile getTile(xIndex,yIndex) method returns the tile as a Raster object at the tile index as a Raster object at the tile index (xIndex,yIndex).(xIndex,yIndex).

Page 16: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Renderable ChainRenderable Chain Geometric operators such as translate, scale, Geometric operators such as translate, scale,

rotate etc can be concatenated without rendering rotate etc can be concatenated without rendering the intermediate images in between. the intermediate images in between.

If the image need not rendered at the If the image need not rendered at the intermediate stages, you can use renderable intermediate stages, you can use renderable chain.chain.

In a renderable chain, the image data is In a renderable chain, the image data is computed only at the end of the chain, i.e. the computed only at the end of the chain, i.e. the Sink.Sink.

RenderableImage interface defines the behaviour RenderableImage interface defines the behaviour of such renderable nodes. of such renderable nodes.

At the source and the sink of a renderable chain At the source and the sink of a renderable chain must be RenderedImage’s.must be RenderedImage’s.

Page 17: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

RenderableImage interfaceRenderableImage interface

RenderableImage has fewer attributes RenderableImage has fewer attributes than RenderedImage because the pixels of than RenderedImage because the pixels of RenderableImage have no pixel RenderableImage have no pixel dimensions, except for the aspect ratio.dimensions, except for the aspect ratio.

The attributes of RenderableImageThe attributes of RenderableImage• width, height – Width and Height defines the width, height – Width and Height defines the

aspect ratio. The getHeight() method always aspect ratio. The getHeight() method always returns 1.0f, and getWidth() method returns returns 1.0f, and getWidth() method returns the aspect ratio.the aspect ratio.

• minX, minY – The upper left-hand coordinates minX, minY – The upper left-hand coordinates of a RenderableImageof a RenderableImage

Page 18: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Renderable Chain This technique is suitable for rendering

independent operations where the RenderingContext is either not required or not known until the final node in the imaging chain.

One of the rendering attribute is the resolution and we can achieve resolution independence as we follow the renderable chain.

The problem with the rendered chain is that rendering hints such as emphasis on speed or emphasis on quality have to be decided before knowing the final information for intermediate nodes. But in the renderable chain these issues are decided only after final rendering hints are known.

Page 19: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Package javax.media.jai.operator Package javax.media.jai.operator

Large number of classes which Large number of classes which implements OperationDescriptor interface.implements OperationDescriptor interface.

Each image operation in JAI must have a Each image operation in JAI must have a descriptor that implements this interface. descriptor that implements this interface.

This interface provides a comprehensive This interface provides a comprehensive description of a specific image operation. description of a specific image operation. • All information regarding the operation, such All information regarding the operation, such

as its name, version, input, and properties as its name, version, input, and properties should be listed. should be listed.

Page 20: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

OperationRegistry classOperationRegistry class The OperationRegistry class maps a descriptor The OperationRegistry class maps a descriptor

name (for example, an image operation name) name (for example, an image operation name) into the particular kind of factory object into the particular kind of factory object requested, capable of implementing the requested, capable of implementing the functionality described by the descriptor. functionality described by the descriptor.

At the highest level all objects are registered At the highest level all objects are registered against some mode. against some mode. • A mode is specified by a String which must be one of A mode is specified by a String which must be one of

those returned by RegistryMode.getModeNames(). those returned by RegistryMode.getModeNames(). • Examples of known registry modes include "rendered", Examples of known registry modes include "rendered",

"renderable", "collection", "renderableCollection", "renderable", "collection", "renderableCollection", "tileEncoder", "tileDecoder", "remoteRendered", "tileEncoder", "tileDecoder", "remoteRendered", "remoteRenderable", etc. "remoteRenderable", etc.

Page 21: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Renderable Chain: ExampleRenderable Chain: Example

public RenderedImage processChain() {public RenderedImage processChain() {// Load the image into a RenderedOp// Load the image into a RenderedOp// Create a RenderableOp image from RenderedOp// Create a RenderableOp image from RenderedOp// Perform the invert operation// Perform the invert operation// Perform addconst operation// Perform addconst operation// Create the final RenderedImage// Create the final RenderedImagereturn finalRenderedImage;return finalRenderedImage;

}}

Page 22: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Create a RenderableOp image from Create a RenderableOp image from RenderedOpRenderedOp

ParameterBlock pb1 = new ParameterBlock();ParameterBlock pb1 = new ParameterBlock(); pb1.add("baboon_256.jpg"); pb1.add("baboon_256.jpg"); RenderedOp originalSourceImage = JAI.create("fileLoad",pb1);RenderedOp originalSourceImage = JAI.create("fileLoad",pb1);

ParameterBlock pb2 = new ParameterBlock();ParameterBlock pb2 = new ParameterBlock(); pb2.addSource(originalSourceImage);pb2.addSource(originalSourceImage); pb2.add(null); pb2.add(null); // use default for the operation chain for lower // use default for the operation chain for lower

// resolution images// resolution images pb2.add(null); pb2.add(null); // use default for maximum dimension for lowest // use default for maximum dimension for lowest

// resolution image// resolution image pb2.add(0.0f); pb2.add(0.0f); // use default for minimum rendering // use default for minimum rendering

independent independent // x of destination// x of destination pb2.add(0.0f); pb2.add(0.0f); // use default for minimum rendering // use default for minimum rendering

independent independent // y of destination// y of destination pb2.add(1.0f); // rendering-independent heightpb2.add(1.0f); // rendering-independent height RenderableOp renderableImg = RenderableOp renderableImg =

JAI.createRenderable("renderable",pb2);JAI.createRenderable("renderable",pb2);

Page 23: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Perform the invert operation & Perform the invert operation & addconst operationaddconst operation

ParameterBlock pb3 = new ParameterBlock();ParameterBlock pb3 = new ParameterBlock();pb3.addSource(renderableImg);pb3.addSource(renderableImg);RenderableOp op1 = RenderableOp op1 =

JAI.createRenderable("invert",pb3);JAI.createRenderable("invert",pb3);

ParameterBlock pb4 = new ParameterBlock();ParameterBlock pb4 = new ParameterBlock();pb4.addSource(op1); // Op1 as the sourcepb4.addSource(op1); // Op1 as the sourcedouble[] c = {2.0d};double[] c = {2.0d};pb4.add(c); // 2.0f as the constantpb4.add(c); // 2.0f as the constantRenderableOp op2 = RenderableOp op2 =

JAI.createRenderable("addconst", pb4);JAI.createRenderable("addconst", pb4);

Page 24: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Create the final RenderedImageCreate the final RenderedImage

RenderedImage finalRenderedImage = RenderedImage finalRenderedImage = op2.createDefaultRendering();op2.createDefaultRendering();

Page 25: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

outputImageBaboon.jpgoutputImageBaboon.jpg

Page 26: Lecture10 Java Advanced Imaging (JAI) API. Example First import java.awt.image.renderable.*; import javax.media.jai.*; public class RenderedChainTest

Renderable ChainRenderable Chain

The createRendering method does not actually compute any pixels, bit it does instantiate a RenderedOp chain that will produce a rendering at the appropriate pixel dimensions.

The Renderable graph can be thought of as a template that, when rendered, causes the instantiation of a parallel Rendered graph to accomplish the actual processing.