Search code examples
javargbbufferedimage

RGB value is always returned same of pixel even if the sample says its not


What I have here is a Buffer image called blackWhite which is completely either black or white. What I am trying to do here is that I take a 2d array of pixel coordinates (in this case, tog) and find out whether it is black or white, assign it to be 0 of black and 1 if not, then save it back to the 3rd column of the 2d array. While it does not through any error, it is not giving me the right colors and saying all of it is white when I know it is not. I have used DDA to pick the coordinates of each pixel between 2 click points for this case and that's what is in the array tog here. Even when I pick just black-to-black pixels, it still says all is white.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class W3_Img_Process_Class_Only extends JPanel {
    //--------------variables
    int[][] tog;    int step;
    String inTitle="L1";
    int imageCount=0;
    int[] x= new int[2], y= new int[2];
    int[] black= new int[2]; //-------------if black is 0, then the pixel is black
    int clr;    int flag=0;
    
    private BufferedImage master;
    private BufferedImage blackWhite;        

    public W3_Img_Process_Class_Only() {
        //----------------------try/catch for (pure black || pure white)
        try {
            master = ImageIO.read(new File(Image_Path));
            blackWhite = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            Graphics2D g2d = blackWhite.createGraphics();
            g2d.drawImage(master, 0, 0, this);
            g2d.dispose();
        }catch (IOException ex) {ex.printStackTrace();}
        //--------------------------1st and 2nd click point data and color
            this.addMouseListener(new MouseListener() {
                int[] isFristEmpty;
                @Override
                public void mouseClicked(MouseEvent e1) {
                        int[] temp =new int[3];     //external container so i can get 1st and 2nd separately
                        temp[0] = (int) e1.getX();
                        temp[1] = (int) e1.getY();
                        clr = blackWhite.getRGB(temp[0], temp[1]);
                        temp[2] = (clr & 0x00ff0000) >> 16;//--------------------bit map to find if red is there or not.
                        //-------------------------------------------------------since its pure b/w, if red 0, its white.
                        if(isFristEmpty==null) {
                            isFristEmpty=temp;
                            x[0] = temp[0]; y[0] = temp[1]; black[0]=temp[2];//------1st click
                        }else {
                            x[1] = temp[0]; y[1] = temp[1]; black[1]=temp[2];//-----2nd click
                            isFristEmpty=null;  //so the 3rd click is considered 1st click again
                            flag=1;
                        }
                        if (flag==1) {
                            
                            System.out.print("X1: "+x[0]+" & "+"Y1: "+y[0]+" "+"(225 if white): "+black[0]+"\t");
                            System.out.println("X2: "+x[1]+" & "+"Y2: "+y[1]+" "+"(225 if white): "+black[1]);
                            counter();
                        }
                }
                @Override   public void mousePressed(MouseEvent e) {}
                @Override   public void mouseReleased(MouseEvent e) {}
                @Override   public void mouseEntered(MouseEvent e) {}
                @Override   public void mouseExited(MouseEvent e) {}
            });
    }
    
    //--------------------------------------------DDA block
    private void counter() {
        int dark;
        if(flag!=1) return;//-------------------to only go to counter method after it takes that 2nd click
        int dx = (x[1] - x[0]);
        int dy = (y[1] - y[0]);//---------makes it applicable for both inclinations (we do not have math.abs implies-> -ve goes as -ve)
        step = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
        System.out.println("Steps: "+step);
        float Xinc = dx / (float) step;//----slope change with respect to x axis
        float Yinc = dy / (float) step;//----slope change with respect to y axis
        
        tog= new int[step][3];
        tog[0][0]=x[0]; tog[0][1]=y[0];
        tog[0][2]= (black[0]!=0) ? 1 : 0;//------------Tertiary operator where condition is true, then while is true
        //---------------------------------------------------------------send value of x1 and y1 to listOfCoordinates
        float xt=x[0],yt=y[0];  int i=0, j=1;
        //-------------to get all the coordinates between the 2 points1111
        System.out.println(tog[0][0]+" "+tog[0][1]+" "+tog[0][2]);
        while (j<step){
            if(i==2) i=0;
            xt += Xinc;
            yt += Yinc;
            tog[j][i] = (int)xt;
            tog[j][i+1] = (int)yt;
            j++;
        }
        for (i = 0; i < step; i++) {
            for (j = 0; j<= 1; j++) {
                System.out.print(tog[i][j]+" ");
            }//****************issue zone here till...

            clr = blackWhite.getRGB(tog[i][j-1], tog[i][j]);
            dark = clr & 0x000000ff;//----for blue but if it is 255, its white
            System.out.print(dark);
            tog[i][2]= (dark!=0) ? 1 : 0;
            //System.out.print(tog[i][2]);
            System.out.println();
        }
        //********issue zone till here..
    }
    //------------image size and such stuff. don't touch it
    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        if (master != null) {
            size = new Dimension(master.getWidth(), master.getHeight());
        }
        return size;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (master != null) {
            int x = (getWidth() - (master.getWidth())) / 2;
            int y = (getHeight() - master.getHeight()) / 2;
            g.drawImage(blackWhite, x, y, this);
        }
    }
}

It's the well and fully functional code of it all. Hope it's good enough to be helpful.

The pixel is white if it gives 255 and 0 if black, if u r looking into already substituted to 1 and 0 part then it's 1 for white. it is supposed to give the value of pixel but it gives all white and that's the issue. the x1 y1 and x2 y2 are the coordinates of 1st and 2nd click. the list u get as output is the list of coordinates of pixels between those points and its color (1 of white adn 0 of black)


Solution

  • Apparently, the problem was with the loop where instead of:

    for (i = 0; i < step; i++) {
        for (j = 0; j<= 1; j++) {
            System.out.print(tog[i][j]+" ");
        }
    }
    

    I should have just used a single loop and reset the value of the y coordinate at the start of the loop again as follows:

    for (i = 1; i < step; i++) {
       j=0;
       clr = blackWhite.getRGB(tog[i][j], tog[i][j+1]);
       System.out.print(tog[i][j]+" "+tog[i][j+1]+" ");
    }
    

    works as intended.