⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 319-323.html

📁 dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh ghghf hgf hghg gh fg hg hfg hfh f hg hgfh gkjh kjkh g yj f
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:An Introduction to Image Processing</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=7//-->
<!--PAGES=319-323//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="315-319.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="323-329.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Image Instancing in the ImageFrame</FONT></H4>
<P>The <I>ImageFrame</I> class has a <I>getImage</I> method that prompts the user for an input file and then opens the image file, waiting until it is read and the resources needed to store the <I>Image</I> instance are allocated. We have decided to defeat this design pattern to simplify the job of reading an image. We find the code to be harder to understand, write, and teach and prefer a simpler API for reading an image. To defeat the callback routine, we make a temporary <I>ImageProducer</I> that is invoked with <I>getImage</I>. Methods such as <I>getImage</I> greatly simplify user code, but they assume that the image resides on a low latency storage device. The <I>fileName</I> and <I>image</I> variables are class variables in the <I>ImageFrame</I> class. The <I>getImage</I> method follows:</P>
<!-- CODE //-->
<PRE>
public void getImage() {
     // Open the image by file name and wait for it!
     fileName = Futil.getReadFileName();
     System.out.println("Get image:"+fileName);
     try {
               image = getToolkit().getImage(fileName);
               waitForImage(this,image);
               }
          catch (Exception e) {
               System.out.println("Get Image could not open "+e);
          }
          // move the image, change its dimensions
          reshape(100, 100,
               image.getWidth(this), image.getHeight(this));
          //set image title
          setTitle(fileName);
          show();
}
</PRE>
<!-- END CODE //-->
<P>The <I>waitForImage</I> method avoids the callback method and stops the thread before the <I>getImage</I> method returns. The <I>paint</I> method for the <I>ImageFrame</I> invokes the <I>drawImage</I> method on the <I>Graphics</I> instance, <I>g</I>.</P>
<!-- CODE SNIP //-->
<PRE>
public void paint(Graphics g) {
       g.drawImage(image,0,0,this);
}
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">The PixelPlane Class</FONT></H3>
<P>The <I>PixelPlane</I> class resides in the <I>lyon.ipl</I> package and is used to give the user a high-level interface to pixel-based operations. This class permits you to process an array of data without having to follow the <I>ImageProducer</I>-<I>ImageConsumer</I> design pattern. This approach makes the software much easier to teach, write, and understand. We have found that Java programmers prefer the simpler array model for manipulating images. An instance of a <I>PixelPlane</I> class stores its pixels in a 1-D array of int. A <I>PixelPlane</I> instance does not do image processing, but is a handy way to access pixels and allocate <I>Image</I> instance memory.</P>
<P>The basic idea is that we do not need to keep an instance of an <I>Image</I> around. All the image processing occurs on and between instances of the <I>PixelPlane</I> class. If we need an <I>Image</I> instance (for display), we can always make one from an instance of the <I>PixelPlane</I> class. This approach&#151;making <I>Image</I> instances on-demand&#151;represents a typical trade-off between space and time. An <I>Image</I> instance takes up so much space that dynamic allocation can free enough space during processing to make otherwise unfeasible operations feasible (from a memory usage point of view).</P>
<P>Because Java does not have (as of this writing) any notion of row-major or column-major order, we cannot use doubly nested <I>for</I> loops to access 2-D arrays. To do so might cause disk thrashing. (This is particularly slow and is typical when memory is accessed nonsequentially.) To ease the burden of thrashing, a single-dimensioned array is stored internally in the <I>PixelPlane</I> instance. The 2-D array is implemented by multiplying the row index by the column index. Methods are also provided that permit linear indexing into the array, resulting in slightly higher speed for some image processing operations.</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
package lyon.ipl;
import futils.*;
import futils.utils.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
public class PixelPlane {
public int pels[]
public PixelPlane(int w, int h)
public PixelPlane(double _x, double _y)
public PixelPlane copy()
public Image makeImage()
public int getLength()
public int getHeight()
public int getWidth()
public boolean inrange(int x, int y)
public int getRed(int i)
public int getGreen(int i)
public int getBlue(int i)
public int getRed(int x, int y)
public int getGreen(int x, int y)
public int getBlue(int x, int y)
public int getAlpha(int x, int y)
public int MakePixel(int r, int g, int b, int a)
public void setPixel(int x, int y, int r, int g, int b, int a)
public void setPixel(int x, int y, int pel)
public int getPixel(int x, int y)
public int getPixel(int i)
public void printSize()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
PixelPlane pp, ppCopy;
int width, height;
double w, h;
int pixelArray[];
Image image;
boolean aBoolean;
int x,y;
int i, c, a, r, g, b, pel;
</PRE>
<!-- END CODE SNIP //-->
<P>To make an instance of a <I>PixelPlane</I> that is <I>width</I> by <I>height</I>, use this code:</P>
<!-- CODE SNIP //-->
<PRE>
pp = new PixelPlane(width, height);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>PixelPlane</I> constructor is overloaded to be used with double-type dimensions:</P>
<!-- CODE SNIP //-->
<PRE>
pp = new PixelPlane(w, h);
</PRE>
<!-- END CODE SNIP //-->
<P>To get at the internal pixel array (this is not suggested but is permitted to maintain flexibility), use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
pixelArray = pp.pels;
</PRE>
<!-- END CODE SNIP //-->
<P>To make a copy of the <I>PixelPlane</I> (this uses the <I>System.arraycopy</I> method, so it should be fast):</P>
<!-- CODE SNIP //-->
<PRE>
ppCopy = pp.copy();
</PRE>
<!-- END CODE SNIP //-->
<P>To make an <I>Image</I> instance from a file (makes a standard file open dialog box), use this:</P>
<!-- CODE SNIP //-->
<PRE>
image = PixelPlane.openImage();
</PRE>
<!-- END CODE SNIP //-->
<P><I>openImage</I> is a static method, so you don&#146;t need to make a <I>PixelPlane</I> instance. To get the total number of pixels in the <I>PixelPlane</I> instance, use this:</P>
<!-- CODE SNIP //-->
<PRE>
int numberOfPixels = pp.getLength();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the <I>width</I> and <I>height</I>, in pixels, from the <I>PixelPlane</I> instance:</P>
<!-- CODE SNIP //-->
<PRE>
height = pp.getHeight();
width = pp.getWidth();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether two (x,y) coordinates are in range:
</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = inrange(x,y);
</PRE>
<!-- END CODE SNIP //-->
<P>To get a color stored in an <I>int</I> from a location in the <I>PixelPlane</I> instance (using the linear array):</P>
<!-- CODE SNIP //-->
<PRE>
c = pp.getRed(i);
</PRE>
<!-- END CODE SNIP //-->
<P>The color value, c, always varies from 0 to 255.
</P>
<!-- CODE SNIP //-->
<PRE>
c = pp.getGreen(i);
c = pp.getBlue(i);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the same color values using the (x,y) coordinates:
</P>
<!-- CODE SNIP //-->
<PRE>
c = pp.getRed(x, y);
</PRE>
<!-- END CODE SNIP //-->
<P>Each access into the <I>PixelPlane</I> costs one multiplication. After Sun releases an API that stores multidimensional arrays, in known order, the multiplication can be eliminated, speeding pixel access:</P>
<!-- CODE SNIP //-->
<PRE>
c = pp.getGreen(x, y);
c = pp.getBlue(x, y);
c = pp.getAlpha(x, y);
</PRE>
<!-- END CODE SNIP //-->
<P>A pixel is stored in a packed-in, 4 bytes per pixel, ARGB (alpha, red, green, blue) format. To pack a pixel:
</P>
<!-- CODE SNIP //-->
<PRE>
pel = pp.MakePixel(r, g, b, a);
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="315-319.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="323-329.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>

<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright &copy; <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -