📄 345-352.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:Digital Images and Image Formats</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=8//-->
<!--PAGES=345-352//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="341-345.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="352-354.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">PPM</FONT></H4>
<P>PPM is a bitmap format that is used as an intermediate format when you’re converting from one system or file format to another. A set of portable freeware utilities written by Jeff Poskanzer converts to and from PPM to many other graphics file formats. For example, the ppmtogif utility converts from PPM to GIF.
</P>
<P>The file organization for PPM is extremely simple. It begins with an ASCII header; the bitmap data follows as either ASCII or binary data. No compression is used. Data elements are separated by whitespace (a space, tab, carriage return, or linefeed).</P>
<P>The PPM header looks like the following:</P>
<!-- CODE SNIP //-->
<PRE>
MagicValue P3= ASCII data, P6= binary data
ImageWidth Width of image in pixels (ASCII decimal value)
ImageHeight Height of image in pixels (ASCII decimal value)
MaxGrey Maximum color value (ASCII decimal value)
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>MaxGrey</I> value specifies the maximum value for a color component. Each pixel is specified by three values for R, G, and B components.</P>
<P>Here is an example file:</P>
<!-- CODE SNIP //-->
<PRE>
# example of a 3 x 3 bitmap
P3
3 3
255
0 0 0 0 0 0 0 0 255
0 0 128 0 7 0 0 1 89
9 0 0 0 9 9 0 0 0
</PRE>
<!-- END CODE SNIP //-->
<P>Comments can be included starting with the # character. The bitmap is 3 pixels high by 3 pixels wide. The third pixel of the second row has RGB values of
</P>
<!-- CODE SNIP //-->
<PRE>
(0, 1, 89).
</PRE>
<!-- END CODE SNIP //-->
<P>Because PPM is a simple format, here is the entire source code showing how DiffCAD implements a PPM reader.
</P>
<!-- CODE //-->
<PRE>
/**
* ReadPPM is a class that reads an image from
* a PPM format file.
*
* Victor Silva (victor@cse.bridgeport.edu).
*
*/
import java.io.*;
import java.awt.image.*;
public class ReadPPM
{
public ReadPPM(InputStream in)
{
}
private int type;
private static final int PBM_ASCII = 1;
private static final int PGM_ASCII = 2;
private static final int PPM_ASCII = 3;
private static final int PBM_RAW = 4;
private static final int PGM_RAW = 5;
private static final int PPM_RAW = 6;
private int width = -1, height = -1;
private int maxval;
/// Subclasses implement this to read in enough of the image stream
// to figure out the width and height.
void readHeader(InputStream in) throws IOException
{
char c1, c2;
c1 = (char) readByte( in );
c2 = (char) readByte( in );
if (c1 != `P')
{
throw new IOException( "not a PBM/PGM/PPM file" );
}
switch(c2)
{
case `1':
type = PBM_ASCII;
break;
case `2':
type = PGM_ASCII;
break;
case `3':
type = PPM_ASCII;
break;
case `4':
type = PBM_RAW;
break;
case `5':
type = PGM_RAW;
break;
case `6':
type = PPM_RAW;
break;
default:
throw new IOException( "not a standard PBM/PGM/PPM file" );
}
width = readInt( in );
height = readInt( in );
if ( type != PBM_ASCII && type != PBM_RAW )
{
maxval = readInt( in );
}
}
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
void readRow( InputStream in, int row, int[] rgbRow ) throws IOException
{
int col, r, g, b;
int rgb = 0;
char c;
for(col=0; col<width; col++)
{
switch(type)
{
case PBM_ASCII:
c = readChar( in );
if ( c == `1' )
{
rgb = 0xff000000;
}
else
{
if ( c == `0' )
{
rgb = 0xffffffff;
}
else
{
throw new IOException( "illegal PBM bit" );
}
}
break;
case PGM_ASCII:
g = readInt(in);
rgb = makeRgb(g, g, g);
break;
case PPM_ASCII:
r = readInt( in );
g = readInt( in );
b = readInt( in );
rgb = makeRgb( r, g, b );
break;
case PBM_RAW:
if ( readBit( in ) )
{
rgb = 0xff000000;
}
else
{
rgb = 0xffffffff;
}
break;
case PGM_RAW:
g = readByte( in );
if ( maxval != 255 )
{
g = fixDepth( g );
}
rgb = makeRgb( g, g, g );
break;
case PPM_RAW:
r = readByte( in );
g = readByte( in );
b = readByte( in );
if ( maxval != 255 )
{
r = fixDepth( r );
g = fixDepth( g );
b = fixDepth( b );
}
rgb = makeRgb( r, g, b );
break;
default:
break;
}
rgbRow[col] = rgb;
}
}
private static int readByte(InputStream in) throws IOException
{
int b = in.read();
// if end of file
if (b == -1)
{
throw new EOFException();
}
return b;
}
private int bitshift = -1;
private int bits;
private boolean readBit( InputStream in ) throws IOException
{
if ( bitshift == -1 )
{
bits = readByte( in );
bitshift = 7;
}
boolean bit = ( ( ( bits >> bitshift ) & 1 ) != 0 );
--bitshift;
return bit;
}
/// Utility routine to read a character, ignoring comments.
private static char readChar( InputStream in ) throws IOException
{
char c;
c = (char) readByte( in );
if ( c == `#' )
{
do
{
c = (char) readByte( in );
}
while ( c != `\n' && c != `\r' );
}
return c;
}
/// Utility routine to read the first non-whitespace character.
private static char readNonwhiteChar( InputStream in ) throws
IOException
{
char c;
do
{
c = readChar( in );
}
while ( c == ` ` || c == `\t' || c == `\n' || c == `\r' );
return c;
}
/// Utility routine to read an ASCII integer, ignoring comments.
private static int readInt( InputStream in ) throws IOException
{
char c;
int i;
c = readNonwhiteChar( in );
if ( c < `0' || c > `9' )
{
throw new IOException( "junk in file where integer should be" );
}
i = 0;
do
{
i = i * 10 + c - `0';
c = readChar( in );
}
while ( c >= `0' && c <= `9' );
return i;
}
/// Utility routine to rescale a pixel value from a non-eight-bit maxval.
private int fixDepth( int p )
{
return ( p * 255 + maxval / 2 ) / maxval;
}
/// Utility routine make an RGBdefault pixel from three color values.
private static int makeRgb( int r, int g, int b )
{
return 0xff000000 | ( r << 16 ) | ( g << 8 ) | b;
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="341-345.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="352-354.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <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 + -