Java Advanced Imaging II - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Java Advanced Imaging II

Description:

The policy files define security policies. There is by default a single system-wide policy ... RemoteRenderedOp convolvedImage = rc.create('Convolve',pb2,null) ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 25
Provided by: turing7
Category:

less

Transcript and Presenter's Notes

Title: Java Advanced Imaging II


1
Lecture11
  • Java Advanced Imaging II

2
JAI Applications
  • We will look at how JAI is used for the following
    application areas.
  • Network Imaging
  • Dealing with large images - Tiling

3
Java security
  • The policy files define security policies. There
    is by default a single system-wide policy file,
    and a single user policy file.
  • The system policy file is by default located at
  • java.home/lib/security/java.policy (Linux)
  • java.home\lib\security\java.policy (Windows)
  • Here, java.home is a system property specifying
    the directory into which the Java 2 SDK was
    installed.
  • The user policy file is by default located at
  • user.home/.java.policy (Linux)
  • user.home\.java.policy (Windows)
  • Here, user.home is a system property specifying
    the users home directory.

4
Policy Files
  • When the Policy is initialized, the system policy
    is loaded in first, and then the user policy is
    added to it. If neither policy is present, a
    built-in policy is used.
  • The policy file locations are specified as the
    values of properties whose names are of the form
  • policy.url.n
  • Here, n is a number. You specify each such
    property value in a line of the following form
  • policy.url.nURL
  • Here, URL is a URL specification. For example,
    the default system and user policy files are
    defined in the security properties file as
  • policy.url.1filejava.home/lib/security/java.p
    olicy
  • policy.url.2fileuser.home/.java.policy

5
Additional Policy files
  • java -Djava.security.manager
  • -Djava.security.policypURL SomeApp
  • Here, pURL is a URL specifying the location of a
    policy file, then the specified policy file will
    be loaded in addition to all the policy files
    that are specified in the security properties
    file.
  • (The -Djava.security.manager argument ensures
    that the default security manager is installed,
    and thus the application is subject to policy
    checks.
  • If you want to pass a policy file to the
    appletviewer, again use a -Djava.security.policy
    argument as follows
  • appletviewer -J-Djava.security.policypURL
    myApplet
  • Please note The -Djava.security.policy policy
    file value will be ignored (for both java and
    appletviewer commands) if the policy.allowSystemP
    roperty property in the security properties file
    is set to false. The default is true.

6
Remote Method Invocation
  • Remote Method Invocation allows programming at a
    higher level abstraction in the client-server
    paradigm.
  • RMI architecture consists of clients and servers
    linked through a registry, called the RMI
    registry. RMI registry maintains active
    references to remote objects.
  • With RMI, methods in remote objects are invoked
    as if they were in a local object. Such a method
    has two parts to it
  • one on the client side (local side) and
  • the other on the server side (remote side).
  • The client side methods are called stubs and the
    server side methods are called skeletons
    (skeletons are only required for Java1.1).
  • A stub acts as a proxy to the remote method and
    has no application logic in it.
  • The skeleton is the stubs counterpart on the
    server side. To invoke a method a client calls
    the stub, which passes the request to the
    skeleton over the network.
  • The methods may require parameter objects which
    must be serializable in order for them to be
    passed on to the remote side.

7
Steps for building and Running an RMI
Client/Server application
  • Enter and compile the source (both client side
    and server side and interfaces). E.g.
  • AshokaImageClient.java It simply loads an
    display an image from the server.
  • AshokaImageLoader.java An interface which
    defines all the interface methods between client
    and the server.
  • AshokaImageServer.java The server source
    program which implement the AshokaImageLoader
    interface.
  • Generate Stubs and Skeletons.
  • Run RMI compiler
  • rmic AshokaImageServer
  • Install Files on the Client and Server machines.
  • AshokaImageClient.class, AshokaImageServer_Stub.cl
    ass, and AshokaImageLoader.class must be on the
    client machine.
  • AshokaImageServer.class, AshokaImageServer_Stub.cl
    ass, AshokaImageServer_Skeleton.class, and
    AshokaImageLoader.class must be on the server.
  • Start the RMI Registry on the server machine.
  • start rmiregistry
  • Start the server.
  • java AshokaImageServer
  • Start the client.
  • java AshokaImageClient

8
Client-Server Example
  • Let write a simple application which loads an
    image from the server. We first need an interface
    file, say AshokaImageLoader.java, which contain
    the following
  • import javax.media.jai.RenderedOp
  • public interface AshokaImageLoader extends
    java.rmi.Remote
  • public RenderedOp loadImage(String filename)
  • throws java.rmi.RemoteException
  • The client talk to the server through the above
    interface.
  • We are using JAI for the server program since JAI
    RenderedOp class is serializable
  • Object communication require such objects to be
    serializable
  • Note that Core Java BufferedImage is not
    serializable!
  • But client program do not need to use JAI since
    RenderedOp can be casted to RenderedImage which
    is defined in Core Java.

9
Server Code
  • import java.rmi. import java.rmi.server.
  • import javax.media.jai.
  • public class AshokaImageServer extends
    UnicastRemoteObject
  • implements AshokaImageLoader
  • public AshokaImageServer() throws
    java.rmi.RemoteException
  • public synchronized RenderedOp loadImage(String
    filename)
  • throws java.rmi.RemoteException
  • try
  • return JAI.create("fileload",filename)
  • catch (Exception e) return null
  • public static void main(String args)
  • try
  • AshokaImageServer jserver new
    AshokaImageServer()
  • Naming.rebind(AshokaImageServer",jserver)
  • catch (Exception e) System.out.println("SRun
    "e)

10
Creating Stubs and Skeletons
  • After compiling the above server program you can
    run rmic (The RMI compiler) to create stubs and
    skeletons as follows
  • rmic AshokaImageServer

11
Client Program
  • // import core java packages only!
  • public class AshokaImageClient extends JFrame
  • protected AshokaImageLoader loader null
  • private RenderedImage rimage
  • public AshokaImageClient()
  • setTitle("Remote Image Viewer")
  • activate()
  • try
  • rimage (RenderedImage) loader.loadIm
    age("lena_256.jpg")
  • catch (Exception e) System.out.println(e)
  • // Work with the rimage
  • // Continued Next Slide

12
Client Program
  • public void activate()
  • System.setSecurityManager(new RMISecurityMa
    nager())
  • try
  • loader (AshokaImageLoader)
  • Naming.lookup("AshokaImageServer")
  • catch (Exception e)
  • System.out.println(e)

13
Network Imaging
  • Any of Java extensions can be used with RMI.
  • But JAI provides direct facilities for remote
    imaging.
  • E.g. A rendered chain can be done in a remote JAI
    image server.

14
Remote JAI Image Server
  • First we need the remote JAI image server
    running. For that we run the following shell
    script in Linux platform
  • !/bin/sh
  • CLASSPATH
  • rmiregistry
  • JAI/usr/java/java/jre/lib/ext
  • CLASSPATHJAI/jai_core.jarJAI/jai_codec.jarJA
    I/mlibwrapper_jai.jar
  • java -Djava.rmi.server.codebase"fileJAI/jai_cor
    e.jar\
  • fileJAI/jai_codec.jar fileJAI/mlibwrapper_jai
    .jar"\
  • -Djava.rmi.server.useCodebaseOnlyfalse\
  • -Djava.security.policyfilePWD/policy\
  • com.sun.media.jai.rmi.JAIRMIImageServer

15
Load the image in the Remote Server
  • RemoteJAI rc new RemoteJAI("jairmi",
    "JAIRMIRemoteServer1.1")
  • ParameterBlock pb1 new ParameterBlock()
  • pb1.add(fileName)
  • pb1.add(null)
  • pb1.add(Boolean.FALSE)
  • RemoteRenderedOp remoteImage rc.create("fil
    eload",pb1,null)

16
Convolution in the remote server
  • ParameterBlock pb2 new ParameterBlock()
  • pb2.addSource(remoteImage)
  • float oneBys2 1/2.0f
  • float kernelData oneBys2, -oneBys2,
    - oneBys2, oneBys2
  • KernelJAI haarKernel new KernelJAI(2,2,0,0
    ,kernelData)
  • pb2.add(haarKernel)
  • RemoteRenderedOp convolvedImage
    rc.create("Convolve",pb2,null)
  • Note that RemoteRenderedOp is a subclass of
    RenderedOp

17
Tiled Imaging
  • Note that RenderedOp, OpImage, RemoteImage,
    TiledImage classes are all subclasses of
    PlanarImage which implements the RenderedImage
    interface.
  • We have seen that the RenderedImage works in
    terms of tiles.
  • Converting Image class object to a PlanarImage
    object
  • Image img
  • // Read and load img
  • PlanarImage pimg JAI.create(awtimage,img)
  • Tiling is useful to optimize speed and memory.
  • If the tiles are too small, say compared to
    viewport, there will be too many tile
    computations, possibly degrading the performance.
  • Too large a tile requires a large amount of
    memory.

18
Reformatting PlanarImage
  • public static RenderedOp reformatImage(PlanarImage
    img, Dimension tileDim)
  • ImageLayout tileLayout new ImageLayout(img)
  • tileLayout.setTileWidth(tileDim.width)
  • tileLayout.setTileHeight(tileDim.height)
  • HashMap map new HashMap()
  • map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout)
  • map.put(JAI.KEY_INTERPOLATION,
  • Interpolation.getInstance(Interpolation.INTERP_B
    ICUBIC))
  • RenderingHints tileHints new
    RenderingHints(map)
  • ParameterBlock pb new ParameterBlock()
  • pb.addSource(img)
  • return JAI.create("format", pb, tileHints)

19
Rendering Hints in JAI
  • The rendering-hints concept was introduced in
    Java2D.
  • The java.awt.RenderingHints class represents
    rendering hints.
  • In the representation each rendering hint is a
    key-value pair.
  • When a particular method, say a rendering related
    method of Graphics2D, require multiple rendering
    hints they are passed to these methods as the Map
    object of a key-value pair.
  • The rendering hints key is of type
    RenderingHints.Key, and the value is an object.
  • JAI doesnt use the Java2D rendering hints for
    rendering a node. Instead it uses its own
    rendering hints, the keys for which are defined
    in the JAI class. A JAI rendering hint key is of
    type JAI.RenderingKey, which is a inner class of
    JAI.
  • E.g. Check the previous slide about reformatting
    a planarImage

20
Viewing a tile of a large image
  • PlanarImage img
  • // Load the img
  • // Say we want to display a tile indexed by
    (ti,tj)
  • Raster tile img.getTile(ti,tj)
  • DataBuffer databuf tile.getDataBuffer()
  • SampleModel sm img.getSampleModel()
  • WritableRaster wr tile.createWritableRaster(sm,
    databuf, new Point(0,0))
  • ColorModel cm img.getColorModel()
  • BufferedImage bi (cm,wr, cm.isAlphaPremul
    tiplied(),null)
  • // Display bi at the right location of the
    viewport

21
Writing to pixels
  • The PlanarImage is read-only.
  • To write to pixels you need to use TiledImage
    class which extends and implements
    WritableRenderedImage interface which handles
    writing of pixel data to tiles.
  • To write pixels, you need to obtain a
    WritableRaster object for the desired rectangular
    region.
  • You cannot write to this instance of
    WritableRaster immediately because unlike
    BufferedImage, TiledImage does not have all its
    data resident in the memory. You need to get all
    the tiles that cover the rectangular region.

22
Writing to a pixel (x,y)
  • TiledImage image
  • // Work with image
  • Int xIndex image.XtoTileX(x)
  • Int yIndex image.YtoTileY(y)
  • WritableRaster tr image.getWritableTile(xInd
    ex,yIndex)
  • if (tr ! null) tr.setPixel(x,y,pixelValue)
  • image.releaseWritableTile(xIndex,yIndex)
  • //

23
TiledImage from a PlanarImage
  • public static TiledImage createTiledImage(PlanarIm
    age img)
  • SampleModel sm img.getSampleModel()
  • ColorModel cm img.getColorModel()
  • TiledImage ti new TiledImage(
  • img.getMinX(), img.getMinY(),
  • img.getWidth(),img.getheight(),
  • img.getTileGridXOffset(),
  • img.getTileGridYOffset(),
  • sm, cm)
  • ti.setData(img.copyData())
  • return ti
  • Check the JAI specification for other TiledImage
    constructors.

24
TileCache interface
  • TileCache provides a mechanism by which an
    OpImage may cache its computed tiles.
  • There may be multiple TileCaches used in an
    application up to the point of having a different
    TileCache for each OpImage.
  • The default tile cache
  • TileCache tilecache JAI.getDefaultInstance().get
    TileCache()
  • The default cache size is 64MB. You can change
    this
  • Tilecache.setMemoryCapacity(20482048L)
  • A new tile cache objects can be obtained
  • TileCache newtilecache JAI.getTileCache()
  • The TileCache used for a particular OpImage is
    derived from the RenderingHints assigned to the
    associated imaging chain node.
  • The rendering hints key associated with tile
    cache is JAI.KEY_TILE_CACHE
  • Please read the TileCache interface specification
    given in JAI specification.
Write a Comment
User Comments (0)
About PowerShow.com