?
Solved

Convert and image from colour to black and white

Posted on 2003-03-04
6
Medium Priority
?
496 Views
Last Modified: 2008-02-01
Hi,
As I'm new to java I believe that this question is quite easy - I have probably just made a silly mistake.
I found this code at the following website ( http://cmpa.ca.scad.edu/faculty/kesson/Ca300/tutorials/11_pixel_grabber/pixel_grabber.html ), and I tried putting it together. It compiles, but the image is not showing. I have tried all kinds of different things, but I just can't make it work. :(
When I change the update() to paint() the image is visible, but in colours. Why is that? And would anyone be able to correct this code so that it works???
Thanks a lot in advance!!! :)

Here is the code:

====================================

import java.awt.image.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Image;


public class BasicApplet extends Applet {

      private Image    image;
      private int[]    pixels;
      private int      picWidth, picHeight;
      private ColorModel  cm = ColorModel.getRGBdefault();

       private Image    picture;
       private Graphics  offscreen;
       
public void init()
       {
       String  name = getParameter("imagename");
       picture = getImage(getDocumentBase(), name);

       image = createImage(300, 300);    
       offscreen = image.getGraphics();
       
       
       MediaTracker  mt = new MediaTracker(this);
try {
    mt.addImage(picture, 0);
    mt.waitForID(0);
    }
catch (Exception e)
    {
    /* failure to import the image */
    }

       
       }
       
public void prepareImage(Graphics g) {
       picWidth = picture.getWidth(this);
       picHeight = picture.getHeight(this);

       pixels = new int[picWidth * picHeight];
       try {
           PixelGrabber grabber = new PixelGrabber(
  picture,0, 0, picWidth, picHeight, pixels, 0, picWidth);
   
          grabber.grabPixels();
          }
      catch (Exception e) { }
      }
       
       public void paint(Graphics g)
      {
      offscreen.drawImage(picture, 0, 0, this);

       for(int x = 0; x < picWidth; x++)
           for(int y = 0; y < picHeight; y++)
               {
               int pix = pixels[y * picWidth + x];
               
               int gray = (cm.getRed(pix) +
                           cm.getGreen(pix) +
                           cm.getBlue(pix)) / 3;
               
               if(gray > 128)
                   {
                   offscreen.setColor(Color.white);
                   offscreen.fillRect(x, y, 1, 1);
                   }
               else
                   {
                   offscreen.setColor(Color.black);
                   offscreen.fillRect(x, y, 1, 1);
                   }
               }
             g.drawImage(image, 50, 50, this);
           }
       
}//end class
=====================================================
0
Comment
Question by:katkuta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 8135440
import java.applet.* ;
import java.awt.* ;
import java.awt.image.* ;

class GrayScale extends RGBImageFilter
  implements PlugInFilter
{
  public Image filter ( Applet a, Image in )
  {
    return a.createImage ( new FilteredImageSource ( in.getSource (), this ) ;

  } // end of filter ()

  public int filterRGB ( int x, int y, int rgb )
  {
    int r = ( rgb >> 16 ) & 0xff ;
    int g = ( rgb >> 8 ) & 0xff ;
    int b = rgb & 0xff ;
    int k = ( int ) ( 0.56 * g + 0.33 * r + 0.11 * b ) ;

    return ( 0xff000000 | k << 16 | k << 8 | k ) ;

  } // end of filterRGB ()

} // class definition over


This class is a sub-class of RGBImageFilter, so it can use itself as the ImageFilter parameter to FilteredImageSource's constructor. Then, it just needs to over-ride the filterRGB () method to change the incoming pixel values, and return a grey pixel which is of the same brightness as the colour source.


Hope that helps!

Mayank.

[PS: Since you're new, I've provided some code - next time, no codes!! Cheers!]

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544885
How about rating it now?
0
 

Expert Comment

by:CleanupPing
ID: 9058930
katkuta:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9762160
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept mayankeagle's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9762937
Please proceed with that recommendation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question