0

Hey,I'm trying to make a program which loads a colored image as grayscale on canvas and then returns the color to the pixels clicked. I'm stuck here when the setrgb() method is not doing what its supposed to. I have copied the color from original image by getRGB() and am using setRGB() to assign it to a new image. I have tried to output both the pixel color values but they are not same. Please help me out with this. Here's the code so far:

import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import java.applet.*;

@SuppressWarnings("serial")
public class BlackWHite extends Applet implements MouseListener 
{
    String str;
    int x=0,y=0;
    BufferedImage bimg = null;
    BufferedImage nimg = null;
    BufferedImage image=null;
    double image_width =300;
    double image_height=300;
    private Image img;

    public void init()
    {
        img = null;
        str = JOptionPane.showInputDialog(null, "Enter file location : ", 
        "Choose Image", 1);  
        BufferedImage image=null;
        try {
            image = ImageIO.read(new File(str));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //getting width and height of image
        image_width = image.getWidth();
        image_height = image.getHeight();

        BufferedImage img = image;

        //drawing a new image      
        bimg = new BufferedImage((int)image_width, (int)image_height,
                             BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D gg = bimg.createGraphics();
        gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

        nimg = new BufferedImage((int)image_width, (int)image_height,
             BufferedImage.TYPE_INT_RGB);
        Graphics2D g = nimg.createGraphics();
        g.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
        addMouseListener(this); 
    }

    public void loadImage()
    {
        try {
            img = getImage(getCodeBase(), "blackwhiteimage.jpg");
        } catch(Exception e) {
        }
    }

    public void paint(Graphics g)
    {       
        try {
            image = ImageIO.read(new File(str));
        } catch (IOException e) {
            e.printStackTrace();
        }
        convert(); //converting the image
        if (img == null)
            loadImage(); //draw
        g.drawImage(img, 0, 0, this);
        int c = image.getRGB(x,y);
        int red = (c & 0x0000FFFF) >> 16;  
        int green = (c & 0x0000FFFF) >> 8;  
        int blue = c & 0x0000FFFF;  
        c=(red << 16) | (green << 8) | blue;
        System.out.print("c="+c);
        nimg=bimg;
        nimg.setRGB(x,y,c);
        try {
            ImageIO.write(nimg, "jpg", new File("pixelcolor.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(nimg, 0, 0, this);
        int f = nimg.getRGB(x,y);
        System.out.println("f="+f);
    }

    void convert() {
        try {
             //saving black and white image onto drive
            ImageIO.write(bimg, "jpg", new File("blackwhiteimage.jpg"));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        x = me.getX(); 
        y = me.getY();
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {     
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}
kiheru
  • 6,588
  • 25
  • 31
Bhawna39
  • 11
  • 3
  • 1
    I'm on a hand held and so can't read your code well, but it looks like you may be trying to do file I/O from within a paint method. If so, start over, use Swing, and get all file I/O out of any paint or paintComponent methods. – Hovercraft Full Of Eels Jul 21 '13 at 15:23
  • Also please consider improving your posted code's formatting especially by standardizing the indentations of all code blocks thereby allowing us to be able to better see and understand your code. Three spaces has worked well for me. – Hovercraft Full Of Eels Jul 21 '13 at 15:28

1 Answers1

1

The fundamental problem is that you are trying to set the colour of the image in gray scale colour space. That is not the only bug in the applet, but you can start with it. Try using another way to convert the image to gray scale while keeping it in RGBA, such as those shown here: convert a RGB image to grayscale Image reducing the memory in java

Community
  • 1
  • 1
kiheru
  • 6,588
  • 25
  • 31
  • Actually i'm not much experienced with this all, the post you referred has three ways to convert and one of them is the same as mine. What am I supposed to do? – Bhawna39 Aug 11 '13 at 08:43
  • @Bhawna39 There's nothing wrong about the method to grayscale, except that it does not fit your purposes. The problem is that the resulting image will be in grayscale color space and can't have any color afterwards (any subsequent drawing to it, just like the original, will be in grayscale). I'd prefer using the `GrayFilter` approach. It keeps the RGB color space. Also, the simplest is to keep the original image in memory rather than loading it at every draw event. Just fetch the color from the stored image with `getRGB()` and use it unmodified to the target image with `setRGB()` – kiheru Aug 11 '13 at 11:27