|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
The first method well be talking about is the setPixel() method that spans lines 118. At lines 57 we calculate the byte width for each scan line and store it in the nByteWidth variable. In line 9 we take into account the fact that BMP file data is stored upside downwe simply transform the y coordinate. At lines 11 and 12 we declare and calculate the nOffset variable. This is the offset into the PixelData buffer. Finally, we set the three color components in the PixelData buffer at lines 1416. The getRed(), getGreen(), and getBlue() methods all call the getPixelComponent() method to get the data for the appropriate color component at a specified pixel. These three methods can be seen in lines 2039. The method that does the work in retrieving color component values is the getPixelComponent() method found at lines 4157. At lines 4648 we calculate the width in bytes for each scan line. At lines 52 and 53 we calculate the offset into the PixelData array based on the x coordinate, the y coordinate, and the scan line width. Lastly, at line 55 we return the color component value. Image Point ProcessingPoint processes are techniques that are performed on single pixels. Because single pixels are all considered points of an image, the term point processes is used to describe operations to single pixels. Point processes are fundamental image processing operations. They are the simplest and probably the most frequently used of the image processing algorithms. Because they are less complex than other image processing algorithms, they are a natural starting place for this chapters image processing discussion. Point processes are algorithms that modify a pixels value in an image based solely on the pixels value and sometimes its location. No other pixel values are involved in the transformation. Individual pixels are replaced with new values that are algorithmically related to the pixels original value. As a result of the algorithmic relationship between the original and the new pixel value, point processes can generally be reversed. Converting Images to GrayscaleMany times youll have images that are in the full range of RGB colors, but for whatever reason you want to convert them to grayscale. The algorithm is extremely simple. What youre actually going to be doing when you perform this operation is to recalculate the red, green, and blue components so that they are equal for each pixel. Gray pixels always have an equal value of red, green, and blue, but the lightness of the gray pixel will depend on the original values of the red, green, and blue components. The following single line of source code calculates the relative gray level given a red, a green, and a blue color component: int nGrayLevel = ( r * 30 + g * 59 + b * 11 ) / 100; Were now going to create a simple program that loads an image, creates a second image, converts the second image to gray, and displays both images side by side in a window. Listing 13.5 gives you the entire source code for the ImagePr1 applet. This applet loads in an image (which is mountain scenery), converts it into grayscale as a second image, and then displays both images side by side. You can see the program running in Figure 13.1. Listing 13.5 The ImagePr1 Applet Source Code import java.awt.*; 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.image.*; 4 5 public class ImagePr1 extends Applet 6 { 7 Image m_Original, m_Processed; 8 9 public void init() 10 { 11 12 MediaTracker Tracker = new MediaTracker( this ); 13 m_Original = getImage( getCodeBase(), "Image6.jpg" ); 14 Tracker.addImage( m_Original, 0 ); 15 try 16 { 17 Tracker.waitForID( 0 ); 18 m_Processed = processImage(); 19 } 20 catch( InterruptedException e ) 21 { 22 } 23 24 } 25 26 public void paint( Graphics g ) 27 { 28 g.drawImage( m_Original, 10, 10, this ); 29 30 g.drawImage( m_P1, 10 + m_Original.getWidth( this ) + 10, 31 10, this ); 32 33 } 34 35 public Image processImage() 36 { 37 Image img = null; 38 39 int nWidth = m_Original.getWidth( this ); 40 int nHeight = m_Original.getHeight( this ); 41 int nNumPixels = nWidth * nHeight; 42 43 int nRawPixels[] = new int[nNumPixels]; 44 45 PixelGrabber Grabber = 46 new PixelGrabber( m_Original, 0, 0, nWidth, nHeight, ⇒nRawPixels, 0, nWidth ); 47 48 try 49 { 50 Grabber.grabPixels(); 51 52 ColorModel cm = ColorModel.getRGBdefault(); 53 54 MemoryImageSource ImageSource = 55 new MemoryImageSource( nWidth, nHeight, cm, ⇒nRawPixels, 0, nWidth ); 56 57 for( int nPixel=0; nPixel<nNumPixels; nPixel++ ) 58 { 59 int r = cm.getRed( nRawPixels[nPixel] ); 60 int g = cm.getGreen( nRawPixels[nPixel] ); 61 int b = cm.getBlue( nRawPixels[nPixel] ); 62 63 int nNewGray = ( ( r * 30 ) + ( g * 69 ) + ( b * 11 ) ) / 100; 64 65 r = nNewGray; 66 g = nNewGray; 67 b = nNewGray; 68 69 nRawPixels[nPixel] = 0xff000000 | 70 ( r << 16 ) | ( g << 8 ) | b; 71 72 } 73 74 img = createImage( ImageSource ); 75 76 MediaTracker Tracker = new MediaTracker( this ); 77 Tracker.addImage( img, 1 ); 78 Tracker.waitForID( 1 ); 79 } 80 catch( InterruptedException e ) 81 { 82 } 83 84 return( img ); 85 86 } 87 88 }
The first thing you should notice about this program is that at line 7 two Image classes are declared. The first will contain the original image, and the second will contain the processed images. The init() method occupies lines 924. It does two things: load the original image and wait for its completion (lines 1217), and call the processImage() method so that the processed Image object is created (line 18).
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |