📄 client-server imaging.htm
字号:
* Run a remote imaging test.
*
* @param serverName The name of the remote server to use.
* @param fileName1 The first addend image file to use.
* @param fileName2 The second addend image file to use.
*/
RemoteImagingTest(String serverName, String fileName1, String
fileName2) {
// Create the operations to load the images from files.
RenderedOp src1 = JAI.create("fileload", fileName1);
RenderedOp src2 = JAI.create("fileload", fileName2);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Render the sources without freezing the nodes.
PlanarImage ren1 = src1.createInstance();
PlanarImage ren2 = src2.createInstance();
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="20"
colspan="1"><PRE> // Create TiledImages with the file images as their sources
// thereby ensuring that the serialized images are truly tiled.
SampleModel sampleModel1 =
ren1.getSampleModel().createCompatibleSampleModel(TILE_WIDTH,
TILE_HEIGHT);
TiledImage ti1 = new TiledImage(ren1.getMinX(), ren1.getMinY(),
ren1.getWidth(), ren1.getHeight(),
ren1.getTileGridXOffset(),
ren1.getTileGridYOffset(),
sampleModel1, ren1.getColorModel());
ti1.set(src1);
SampleModel sampleModel2 =
ren2.getSampleModel().createCompatibleSampleModel(TILE_WIDTH,
TILE_HEIGHT);
TiledImage ti2 = new TiledImage(ren2.getMinX(), ren2.getMinY(),
ren2.getWidth(), ren2.getHeight(),
ren2.getTileGridXOffset(),
ren2.getTileGridYOffset(),
sampleModel2, ren2.getColorModel());
ti2.set(src2);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="4" colspan="1"><PRE> // Create a hint to specify the tile dimensions.
ImageLayout layout = new ImageLayout();
layout.setTileWidth(TILE_WIDTH).setTileHeight(TILE_HEIGHT);
RenderingHints rh = new
RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="9" colspan="1"><PRE> // Rescale the images to the range [0, 127].
ParameterBlock pb = (new ParameterBlock());
pb.addSource(ti1);
pb.add(new double[] {0.5}).add(new double[] {0.0});
RenderedOp addend1 = JAI.create("rescale", pb, rh);
pb = (new ParameterBlock());
pb.addSource(ti2);
pb.add(new double[] {0.5}).add(new double[] {0.0});
RenderedOp addend2 = JAI.create("rescale", pb, rh);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="3" colspan="1"><PRE> // Add the rescaled images.
pb = (new
ParameterBlock()).addSource(addend1).addSource(addend2);
RenderedOp sum = JAI.create("add", pb, rh);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="4"
colspan="1"><PRE> // Dither the sum of the rescaled images.
pb = (new ParameterBlock()).addSource(sum);
pb.add(ColorCube.BYTE_496).add(KernelJAI.DITHER_MASK_443);
RenderedOp dithered = JAI.create("ordereddither", pb, rh);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="2" colspan="1"><PRE> // Construct a RemoteImage from the RenderedOp chain.
RemoteImage remoteImage = new RemoteImage(serverName, sum);
</PRE><TR valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Set the display title and window layout.
setTitle(getClass().getName());
setLayout(new GridLayout(2, 2));
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="4"
colspan="1"><PRE> // Local rendering.
add(new ScrollingImagePanel(sum,
sum.getWidth(),
sum.getHeight()));
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="4" colspan="1"><PRE> // RenderedOp remote rendering.
add(new ScrollingImagePanel(remoteImage,
remoteImage.getWidth(),
remoteImage.getHeight()));
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="6" colspan="1"><PRE> // RenderedImage remote rendering
PlanarImage sumImage = sum.getRendering();
remoteImage = new RemoteImage(serverName, sumImage);
add(new ScrollingImagePanel(remoteImage,
remoteImage.getWidth(),
remoteImage.getHeight()));
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TD rowspan="16" colspan="1"><PRE> // RenderableOp remote rendering.
pb = new ParameterBlock();
pb.addSource(dithered);
RenderableOp absImage = JAI.createRenderable("absolute", pb);
pb = new ParameterBlock();
pb.addSource(absImage).add(ColorCube.BYTE_496);
RenderableOp lutImage = JAI.createRenderable("lookup", pb);
AffineTransform tf =
AffineTransform.getScaleInstance(384/dithered.getWidth(),
256/dithered.getHeight());
Rectangle aoi = new Rectangle(128, 128, 384, 256);
RenderContext rc = new RenderContext(tf, aoi, rh);
remoteImage = new RemoteImage(serverName, lutImage, rc);
add(new ScrollingImagePanel(remoteImage,
remoteImage.getWidth(),
remoteImage.getHeight()));
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="5" colspan="1"><PRE> // Finally display everything
pack();
show();
}
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top">
<HR>
<P><A name=52700>
<H3>12.4.2 <IMG src="Client-Server Imaging.files/space.gif">RemoteImaging
Example Across Two Nodes</H3></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Client-server.doc.html#52766">Listing
12-2</A> shows an example of a RemoteImaging chain spread across two remote
nodes, and displays the results locally.
<P><CAPTION><FONT size=-1><B><A name=52766>
<CENTER><FONT size=-1><B><I>Listing 12-2 </I><IMG
src="Client-Server Imaging.files/sm-blank.gif" border=0> RemoteImaging Example
Program Using Two Nodes (Sheet 1 of 2)</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="4" colspan="1"><PRE> import java.awt.image.*;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.*;
import javax.media.jai.widget.*;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="4" colspan="1"><PRE> /**
* This test creates an imaging chain spread across two remote
* nodes and displays the result locally.
*/
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="6" colspan="1"><PRE> public class MultiNodeTest extends WindowContainer {
public static void main(String[] args) {
if(args.length != 3) {
throw new RuntimeException("Usage: java MultiNodeTest "+
"file node1 node2");
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> new MultiNodeTest(args[0], args[1], args[2]);
}
public MultiNodeTest(String fileName, String node1, String
node2) {
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="7"
colspan="1"><PRE> // Create a chain on node 1.
System.out.println("Creating dst1 = log(invert(fileload("+
fileName+"))) on "+node1);
RenderedOp src = JAI.create("fileload", fileName);
RenderedOp op1 = JAI.create("invert", src);
RenderedOp op2 = JAI.create("log", op1);
RemoteImage rmt1 = new RemoteImage(node1, op2);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="5" colspan="1"><PRE> // Create a chain on node 2.
System.out.println("Creating dst2 = not(exp(dst1)) on "+node2);
RenderedOp op3 = JAI.create("exp", rmt1);
RenderedOp op4 = JAI.create("not", op3);
RemoteImage rmt2 = new RemoteImage(node2, op4);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="8" colspan="1"><PRE> // Display the result of node 2.
System.out.println("Displaying results");
setTitle(getClass().getName()+" "+fileName);
add(new ScrollingImagePanel(rmt2, rmt2.getWidth(),
rmt2.getHeight()));
pack();
show();
}
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top">
<HR>
<P>
<TABLE border=0>
<TBODY>
<TR>
<TD><IMG src="Client-Server Imaging.files/cistine.gif"></TD>
<TD>
<HR>
<B>API:</B> <CODE>javax.media.jai.RemoteImage </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>int getWidth()
<P></P></LI></UL></PRE>
<DL><A name=54733>
<DT>
<DD>returns the width of the <CODE>RemoteImage</CODE>. </A>
<P></P></DD></DL><PRE><UL>
<LI>int getHeight()
<P></P></LI></UL></PRE>
<DL><A name=54739>
<DT>
<DD>returns the height of the <CODE>RemoteImage</CODE>. </A>
<P></P></DD></DL><PRE><UL>
<LI>Raster getData()
<P></P></LI></UL></PRE>
<DL><A name=51312>
<DT>
<DD>returns the entire image as one large tile. </A>
<P></P></DD></DL><PRE><UL>
<LI>Raster getData(Rectangle rect)
<P></P></LI></UL></PRE>
<DL><A name=54711>
<DT>
<DD>returns an arbitrary rectangular region of the <CODE>RemoteImage</CODE>.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD>Parameters:
<P></P>
<TD><CODE>rect</CODE>
<P></P>
<TD>The region of the <CODE>RemoteImage</CODE> to be returned.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><PRE><UL>
<LI>WritableRaster copyData(WritableRaster raster)
<P></P></LI></UL></PRE>
<DL><A name=54713>
<DT>
<DD>returns an arbitrary rectangular region of the <CODE>RemoteImage</CODE>
in a user-supplied <CODE>WritableRaster</CODE>. The rectangular region is
the entire image if the argument is null or the intersection of the argument
bounds with the image bounds if the region is non-null. If the argument is
non-null but has bounds that have an empty intersection with the image
bounds, the return value will be null. The return value may also be null if
the argument is non-null but is incompatible with the <CODE>Raster</CODE>
returned from the remote image.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD><EM>Parameters</EM>:
<P></P>
<TD><CODE>raster</CODE>
<P></P>
<TD>A <CODE>WritableRaster</CODE> to hold the returned portion of the
image.
<P></P></TR></TBODY></TABLE></A>
<P><A name=51331></P>
<DT>
<DD>If the <CODE>raster</CODE> argument is null, the entire image will be
copied into a newly-created WritableRaster with a SampleModel that is
compatible with that of the image. </A>
<P></P></DD></DL><PRE><UL>
<LI>Raster getTile(int x, int y)
<P></P></LI></UL></PRE>
<DL><A name=51333>
<DT>
<DD>returns a tile (<EM>x</EM>, <EM>y</EM>). Note that <EM>x</EM> and
<EM>y</EM> are indices into the tile array, not pixel locations. Unlike in
the true <CODE>RenderedImage</CODE> interface, the <CODE>Raster</CODE> that
is returned should be considered a copy.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD rowSpan=2><EM>Parameters</EM>:
<P></P>
<TD><CODE>x</CODE>
<P></P>
<TD>The <EM>x</EM> index of the requested tile in the tile array
<P></P>
<TR vAlign=top>
<TD>y
<P></P>
<TD>The <EM>y</EM> index of the requested tile in the tile array
<P></P></TR></TBODY></TABLE></A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -