|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
The paint() method can be seen at lines 2633. It simply draws both images. Refer to Figure 13.1 for the output of the program. The processImage() method is the workhorse of the programit does most of the work. Lines 3941 get the width and height of the original image and calculate the number of pixels that are in it. Line 43 allocates the integer array that will contain the pixel data. Lines 45 and 46 is where the PixelGrabber object is created. And line 50 is where the pixels are actually extracted with the grabPixels() method. Notice that we catch the InterruptedException exceptionthis is required! Next we need to get the color model, and we do this at line 52 with the getRGBdefault() method. Then, a MemoryImageSource object is created at lines 54 and 55this will be used to create the new image after the data has been processed. Now to the image processing. We enter a loop thats going to iterate through all the image pixels. At lines 5961 we get the red, green, and blue color components for the current pixel. At line 63 we calculate the grayscale value. Red is factored in by 30 percent, green by 69 percent, and blue by 11 percent. At lines 69 and 70 we store the color components for this pixel in the pixel data array. At line 74 we create the new image. We use a MediaTracker object to wait for the image to be completely created before we proceed (lines 7678). Finally, at line 84 we return the newly created image. Changing the Brightness of an ImageOne of the things everyone does with his or her television set is to change the brightness. Individual tastes vary, and the brightness of the television picture will change accordingly. Some computer images need a brightness change because theyve been acquired through maladjusted hardware, or during less than optimal circumstances. By changing the brightness of the RGB values in an image, you can adjust the brightness of an image.
The algorithm for changing the brightness of an image is to simply change each color component (red, green, and blue) by the same percentage. For instance, if you want to double the brightness, you double the red, green, and blue component values. The following short source-code example shows you how to increase the brightness by 20 percent: // Get the red, green, and blue components. int r = cm.getRed( nRawPixels[nPixel]; int g = cm.getGreen( nRawPixels[nPixel]; int b = cm.getBlue( nRawPixels[nPixel]; // Adjust them so that they're 20 percent brighter. r = ( r * 120 ) / 100; g = ( g * 120 ) / 100; b = ( b * 120 ) / 100; // Make sure they're not greater than 255. if( r > 255 ) r = 255; if( g > 255 ) g = 255; if( b > 255 ) b = 255; // Store the value back in the pixel array. nRawPixels[nPixel] = 0xff000000 | ( r << 16 ) | ( g << 8 ) | b; Ive taken the last applet we wrote, ImagePr1, and simply altered the processImage() method so that it makes the image 40 percent brighter. Ive also loaded a different image in; so instead of showing you the entire listing for the applet, Im showing you only the processImage() method. Listing 13.6 shows you the entire processImage() method that brightens the image by 40 percent. Listing 13.6 The processImage() Method That Brightens the Image by 40 Percent 1 public void processImage() 2 { 3 int nWidth = m_Original.getWidth( this ); 4 int nHeight = m_Original.getHeight( this ); 5 int nNumPixels = nWidth * nHeight; 6 7 int nRawPixels[] = new int[nNumPixels]; 8 9 PixelGrabber Grabber = 10 new PixelGrabber( m_Original, 0, 0, nWidth, nHeight, ⇒nRawPixels, 0, nWidth ); 11 12 try 13 { 14 Grabber.grabPixels(); 15 16 ColorModel cm = ColorModel.getRGBdefault(); 17 MemoryImageSource ImageSource = 18 new MemoryImageSource( nWidth, nHeight, cm, ⇒nRawPixels, 0, nWidth ); 19 20 for( int nPixel=0; nPixel<nNumPixels; nPixel++ ) 21 { 22 int r = cm.getRed( nRawPixels[nPixel] ); 23 int g = cm.getGreen( nRawPixels[nPixel] ); 24 int b = cm.getBlue( nRawPixels[nPixel] ); 25 26 r = ( r * 140 ) / 100; 27 g = ( g * 140 ) / 100; 28 b = ( b * 140 ) / 100; 29 30 if( r > 255 ) 31 r = 255; 32 if( g > 255 ) 33 g = 255; 34 if( b > 255 ) 35 b = 255; 36 nRawPixels[nPixel] = 0xff000000 | 37 ( r << 16 ) | ( g << 8 ) | b; 38 39 } 40 41 m_Processed = createImage( ImageSource ); 42 43 MediaTracker Tracker = new MediaTracker( this ); 44 Tracker.addImage( m_Processed, 1 ); 45 Tracker.waitForID( 1 ); 46 } 47 catch( InterruptedException e ) 48 { 49 } 50 51 } You can see the applet running in Figure 13.2. On the left is the original image with two sailboats. On the right is the brightened image of the same sailboats.
Colorizing ImagesWhen you have the RGB values of a pixel, you can also colorize them. For instance, you might want to make them all shades of red so that your image, instead of being a full spectrum of red, green, and blue shades, would be only shades of red. Or you might simply want the image to be all shades of green.
|
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. |