Changing RGB in a pixel

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3729 Views - Last Post: 25 January 2011 - 02:01 PM Rate Topic: -----

#1 Guest_Pureblood*


Reputation:

Changing RGB in a pixel

Posted 24 January 2011 - 07:51 PM

OK I am trying to be able to change colors a pixel.
I want to be able to be like alterRGB(1.5,3.0,1.5)
Here is what I have.
public void alterRGB(double red, double green, double blue){
   Pixel[] pixelArray = this.getPixels();
   

    // loop through all the pixels
  for (Pixel pixelObj : pixelArray){ 
      red = pixelObj.getRed();
      green = pixelObj.getGreen();
      blue = pixelObj.getBlue();
  }
}
  public static void main(String[] args)
  {
  
    Picture p =  new Picture(FileChooser.pickAFile());
      Scanner kybd = new Scanner(System.in);
    double factor = kybd.next
      Double();
    p.alterRGB(2,1,1);
    
    p.explore();  
  }


Everything compiles but when I run the program 1. It doesn't change the pixel. and 2. I can't enter more than one number. What am I doing wrong?

Is This A Good Question/Topic? 0

Replies To: Changing RGB in a pixel

#2 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Changing RGB in a pixel

Posted 24 January 2011 - 08:29 PM

You need to give us more information. Mainly, where do Pixel and Picture come from? They're not standard Java classes, so they're either part of a separate library or you made them yourself. We need to know where and what they are in order to offer help.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Changing RGB in a pixel

Posted 24 January 2011 - 09:24 PM

If you use the standard BufferedImage class, you can use the setRGB() method. Without the documentation for your specific classes, we can't advise you on the API you are using. :)
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 24 January 2011 - 09:31 PM

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;


public class Picture extends SimplePicture { 

  ///////////////////// constructors //////////////////////////////////
  
  /**
   * Constructor that takes no arguments 
   */
  public Picture () {
    
    /* not needed but use it to show students the implicit call to super()
     * child constructors always call a parent constructor 
     */
    super();  
  }
  
  /**
   * Constructor that takes a file name and creates the picture 
   * @param fileName the name of the file to create the picture from
   */
  public Picture(String fileName) {
    // let the parent class handle this fileName
    super(fileName);
  }
  
  /**
   * Constructor that takes the width and height
   * @param width the width of the desired picture
   * @param height the height of the desired picture
   */
  public Picture(int width, int height) {
    // let the parent class handle this width and height
    super(width,height);
  }
  
  /**
   * Constructor that takes a picture and creates a 
   * copy of that picture
   */
  public Picture(Picture copyPicture) {
    // let the parent class do the copy
    super(copyPicture);
  }
  
  /**
   * Constructor that takes a buffered image
   * @param image the buffered image to use
   */
  public Picture(BufferedImage image) {
    super(image);
  }
  
  ////////////////////// methods ///////////////////////////////////////
  
  /**
   * Method to return a string with information about this picture.
   * @return a string with information about the picture such as fileName,
   * height and width.
   */
  public String toString() {
    String output = "Picture, filename " + getFileName() + 
      " height " + getHeight() 
      + " width " + getWidth();
    return output;
    
  }
  
   /**
   * Class method to let the user pick a file name and then create the picture 
   * and show it
   * @return the picture object
   */
  public static Picture pickAndShow() {
    String fileName = FileChooser.pickAFile();
    Picture picture = new Picture(fileName);
    picture.show();
    return picture;
  }
  
  /**
   * Class method to create a picture object from the passed file name and 
   * then show it
   * @param fileName the name of the file that has a picture in it
   * @return the picture object
   */
  public static Picture showNamed(String fileName) {
    Picture picture = new Picture(fileName);
    picture.show();
    return picture;
  }
  
  /**
   * A method create a copy of the current picture and return it
   * @return the copied picture
   */
  public Picture copy()
  {
    return new Picture(this);
  }
  
  /**
   * Method to increase the red in a picture.
   */
  public void increaseRed() {
    Pixel [] pixelArray = this.getPixels();
    for (Pixel pixelObj : pixelArray) {
      pixelObj.setRed(pixelObj.getRed()*2);
    }
  }
  
  /**
   * Method to negate a picture
   */
  public void negate() {
    Pixel [] pixelArray = this.getPixels();
    int red,green,blue;
    
    for (Pixel pixelObj : pixelArray) {
      red = pixelObj.getRed();
      green = pixelObj.getGreen();
      blue = pixelObj.getBlue();
      pixelObj.setColor(new Color(255-red, 255-green, 255-blue));
    }
  }
  
  /**
   * Method to flip a picture 
   */
  public Picture flip() {
    Pixel currPixel = null;
    Pixel targetPixel = null;
    Picture target = 
      new Picture(this.getWidth(),this.getHeight());
    
    for (int srcX = 0, trgX = getWidth()-1; 
         srcX < getWidth();
         srcX++, trgX--) {
      for (int srcY = 0, trgY = 0; 
           srcY < getHeight();
           srcY++, trgY++) {
        
        // get the current pixel
        currPixel = this.getPixel(srcX,srcY);
        targetPixel = target.getPixel(trgX,trgY);
        
        // copy the color of currPixel into target
        targetPixel.setColor(currPixel.getColor());
      }
    }
    return target;
  }
  
  /**
   * Method to decrease the red by half in the current picture
   */
  public void decreaseRed() {
  
    Pixel pixel = null; // the current pixel
    int redValue = 0;       // the amount of red

    // get the array of pixels for this picture object
    Pixel[] pixels = this.getPixels();

    // start the index at 0
    int index = 0;

    // loop while the index is less than the length of the pixels array
    while (index < pixels.length) {

      // get the current pixel at this index
      pixel = pixels[index];
      // get the red value at the pixel
      redValue = pixel.getRed();
      // set the red value to half what it was
      redValue = (int) (redValue * 0.5);
      // set the red for this pixel to the new value
      pixel.setRed(redValue);
      // increment the index
      index++;
    }
  }
  
  /**
   * Method to decrease the red by an amount
   * @param amount the amount to change the red by
   */
  public void decreaseRed(double amount) {
 
    Pixel[] pixels = this.getPixels();
    Pixel p = null;
    int value = 0;

    // loop through all the pixels
    for (int i = 0; i < pixels.length; i++) {
 
      // get the current pixel
      p = pixels[i];
      // get the value
      value = p.getRed();
      // set the red value the passed amount time what it was
      p.setRed((int) (value * amount));
    }
  }
  
  /**
   * Method to compose (copy) this picture onto a target picture
   * at a given point.
   * @param target the picture onto which we copy this picture
   * @param targetX target X position to start at
   * @param targetY target Y position to start at
   */
  public void compose(Picture target, int targetX, int targetY) {
 
    Pixel currPixel = null;
    Pixel newPixel = null;

    // loop through the columns
    for (int srcX=0, trgX = targetX; srcX < this.getWidth();
         srcX++, trgX++) {
  
      // loop through the rows
      for (int srcY=0, trgY=targetY; srcY < this.getHeight();
           srcY++, trgY++) {

        // get the current pixel
        currPixel = this.getPixel(srcX,srcY);

        /* copy the color of currPixel into target,
         * but only if it'll fit.
         */
        if (trgX < target.getWidth() && trgY < target.getHeight()) {
          newPixel = target.getPixel(trgX,trgY);
          newPixel.setColor(currPixel.getColor());
        }
      }
    }
  }
  
  /**
   * Method to scale the picture by a factor, and return the result
   * @param factor the factor to scale by (1.0 stays the same,
   *    0.5 decreases each side by 0.5, 2.0 doubles each side)
   * @return the scaled picture
   */
  public Picture scale(double factor) {
    
    Pixel sourcePixel, targetPixel;
    Picture canvas = new Picture(
                                 (int) (factor*this.getWidth())+1,
                                 (int) (factor*this.getHeight())+1);
    // loop through the columns
    for (double sourceX = 0, targetX=0;
         sourceX < this.getWidth();
         sourceX+=(1/factor), targetX++) {
      
      // loop through the rows
      for (double sourceY=0, targetY=0;
           sourceY < this.getHeight();
           sourceY+=(1/factor), targetY++) {
        
        sourcePixel = this.getPixel((int) sourceX,(int) sourceY);
        targetPixel = canvas.getPixel((int) targetX, (int) targetY);
        targetPixel.setColor(sourcePixel.getColor());
      }
    }
    return canvas;
  }
  
  /**
   * Method to do chromakey using an input color for the background
   * and a point for the upper left corner of where to copy
   * @param target the picture onto which we chromakey this picture
   * @param bgColor the color to make transparent
   * @param threshold within this distance from bgColor, make transparent
   * @param targetX target X position to start at
   * @param targetY target Y position to start at
   */
  public void chromakey(Picture target, Color bgColor, int threshold,
                        int targetX, int targetY) {
 
    Pixel currPixel = null;
    Pixel newPixel = null;

    // loop through the columns
    for (int srcX=0, trgX=targetX;
        srcX<getWidth() && trgX<target.getWidth();
        srcX++, trgX++) {

      // loop through the rows
      for (int srcY=0, trgY=targetY;
        srcY<getHeight() && trgY<target.getHeight();
        srcY++, trgY++) {

        // get the current pixel
        currPixel = this.getPixel(srcX,srcY);

        /* if the color at the current pixel is within threshold of
         * the input color, then don't copy the pixel
         */
        if (currPixel.colorDistance(bgColor)>threshold) {
          target.getPixel(trgX,trgY).setColor(currPixel.getColor());
        }
      }
    }
  }
  
    /**
   * Method to do chromakey assuming a blue background 
   * @param target the picture onto which we chromakey this picture
   * @param targetX target X position to start at
   * @param targetY target Y position to start at
   */
  public void blueScreen(Picture target,
                        int targetX, int targetY) {

    Pixel currPixel = null;
    Pixel newPixel = null;

    // loop through the columns
    for (int srcX=0, trgX=targetX;
         srcX<getWidth() && trgX<target.getWidth();
         srcX++, trgX++) {

      // loop through the rows
      for (int srcY=0, trgY=targetY;
           srcY<getHeight() && trgY<target.getHeight();
           srcY++, trgY++) {

        // get the current pixel
        currPixel = this.getPixel(srcX,srcY);

        /* if the color at the current pixel mostly blue (blue value is
         * greater than red and green combined), then don't copy pixel
         */
        if (currPixel.getRed() + currPixel.getGreen() > currPixel.getBlue()) {
          target.getPixel(trgX,trgY).setColor(currPixel.getColor());
        }
      }
    }
  }
  
  /**
   * Method to change the picture to gray scale with luminance
   */
  public void grayscaleWithLuminance()
  {
    Pixel[] pixelArray = this.getPixels();
    Pixel pixel = null;
    int luminance = 0;
    double redValue = 0;
    double greenValue = 0;
    double blueValue = 0;

    // loop through all the pixels
    for (int i = 0; i < pixelArray.length; i++)
    {
      // get the current pixel
      pixel = pixelArray[i];

      // get the corrected red, green, and blue values
      redValue = pixel.getRed() * 0.299;
      greenValue = pixel.getGreen() * 0.587;
      blueValue = pixel.getBlue() * 0.114;

      // compute the intensity of the pixel (average value)
      luminance = (int) (redValue + greenValue + blueValue);

      // set the pixel color to the new color
      pixel.setColor(new Color(luminance,luminance,luminance));

    }
  }
  
  /** 
   * Method to do an oil paint effect on a picture
   * @param dist the distance from the current pixel 
   * to use in the range
   * @return the new picture
   */
  public Picture oilPaint(int dist) {
    
    // create the picture to return
    Picture retPict = new Picture(this.getWidth(),this.getHeight());
    
    // declare pixels
    Pixel currPixel = null;
    Pixel retPixel = null;
    
    // loop through the pixels
    for (int x = 0; x < this.getWidth(); x++) {
      for (int y = 0; y < this.getHeight(); y++) {
        currPixel = this.getPixel(x,y);
        retPixel = retPict.getPixel(x,y);
        retPixel.setColor(currPixel.getMostCommonColorInRange(dist));
      }
    }
    return retPict;
  }
  

Was This Post Helpful? 0

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Changing RGB in a pixel

Posted 24 January 2011 - 09:35 PM

1) You need to invoke the setRed(), setBlue(), and setGreen() methods respectively on the Pixel objects to modify the color components of each Pixel.

2) You only invoke nextDouble() once.
Was This Post Helpful? 1
  • +
  • -

#6 Guest_Purebloof*


Reputation:

Re: Changing RGB in a pixel

Posted 24 January 2011 - 09:46 PM

View Postmacosxnerd101, on 24 January 2011 - 09:35 PM, said:

1) You need to invoke the setRed(), setBlue(), and setGreen() methods respectively on the Pixel objects to modify the color components of each Pixel.

2) You only invoke nextDouble() once.

Maybe it is because im tired but i dont under stand. sorry.
Was This Post Helpful? 0

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Changing RGB in a pixel

Posted 24 January 2011 - 09:49 PM

Here, you only invoke the Pixel getRed(), getBlue(), getGreen() methods. You need to invoke the corresponding setter methods to modify the Pixel's RGB values.
public void alterRGB(double red, double green, double blue){

  for (Pixel pixelObj : pixelArray){ 
      red = pixelObj.getRed();
      green = pixelObj.getGreen();
      blue = pixelObj.getBlue();
  }
}




You only invoke nextDouble() one time. So only one double will be read in. If you want to read more doubles in, invoke the method multiple times.
      Scanner kybd = new Scanner(System.in);
    double factor = kybd.next
      Double();
    p.alterRGB(2,1,1);


Was This Post Helpful? 0
  • +
  • -

#8 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 25 January 2011 - 08:22 AM

Ok so what i did is below. I have some errors because it will not compile right. Here is the errors I am getting.
3 errors found:
File: C:\Users\Desktop\drjava\java-source\Picture.java [line: 446]
Error: C:\Users\Desktop\drjava\java-source\Picture.java:446: cannot find symbol
symbol : variable factor
location: class Picture
File: C:\Users\Desktop\drjava\java-source\Picture.java [line: 448]
Error: C:\User\Desktop\drjava\java-source\Picture.java:448: cannot find symbol
symbol : variable factor
location: class Picture
File: C:\Users\Desktop\drjava\java-source\Picture.java [line: 450]
Error: C:\Users\Desktop\drjava\java-source\Picture.java:450: cannot find symbol
symbol : variable factor
location: class Picture

public void alterRGB(double red, double green, double blue){
   Pixel[] pixelArray = this.getPixels();
   

    // loop through all the pixels
  for (Pixel pixelObj : pixelArray){ 
      red = pixelObj.getRed();
      red = pixelObj.setRed();
      green = pixelObj.getGreen();
      green = pixelObj.setGreen();
      blue = pixelObj.getBlue();
      blue = pixelObj.setBlue();
  }
}
  public static void main(String[] args)
  {
  
    Picture p =  new Picture(FileChooser.pickAFile());
      Scanner kybd = new Scanner(System.in);
    double factor = kybd.nextDouble(); 
    p.alterRGB(factor,factor,factor);
    
    p.explore();  
  }

Was This Post Helpful? 0

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Changing RGB in a pixel

Posted 25 January 2011 - 08:26 AM

You have to pass a parameter to the setter methods. As for factor, it is a local variable in your main() method. As such, it isn't accessible outside of main(). What happens when you pass it as a parameter is that the value it holds (3.5 as an example) is copied into the method as a local variable.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 25 January 2011 - 08:35 AM

Gosh I am such a noob. How do I pass a parameter to to the setters? I tried looking it up, but I couldnt find anything.
Was This Post Helpful? 0

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Changing RGB in a pixel

Posted 25 January 2011 - 08:42 AM

In the alterRGB() method, you probably mean to do pixelObj.setGreen(green);, and the same for red and green. No need for the getter methods at all.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 25 January 2011 - 08:46 AM

Yes you would be right. Ok so I know what I have to do next. I need to typecast the red green and blue because they are doubles and I need to make them ints.
My question is where do I put the code? Do I put it about the forloop?
Was This Post Helpful? 0

#13 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 25 January 2011 - 10:53 AM

Ok I got everything to compile but it isn't changing the pixels like it is supposed to. It is also accepting more than 3 numbers. The only number that changed anything is the first number.
public void alterRGB(double red, double green, double blue){
   Pixel[] pixelArray = this.getPixels();
   int red2 = (int)red;
   int green2 = (int)green;
   int blue2 = (int)blue;
       
    // loop through all the pixels
  for (Pixel pixelObj : pixelArray){
      pixelObj.setRed(pixelObj.getRed()*red2);
      pixelObj.setGreen(pixelObj.getGreen()*green2);
      pixelObj.setBlue(pixelObj.getBlue()*blue2);
      
      
  }
}
  public static void main(String[] args)
  {
  
    Picture p =  new Picture(FileChooser.pickAFile());
      Scanner kybd = new Scanner(System.in);
    double factor = kybd.nextDouble(); 
    p.alterRGB(factor, factor, factor);
    
    p.explore();  
  }


This post has been edited by g00se: 25 January 2011 - 12:06 PM

Was This Post Helpful? 0

#14 Guest_Pureblood*


Reputation:

Re: Changing RGB in a pixel

Posted 25 January 2011 - 11:27 AM

I need to separate the factors for R B and B. How do I do that?

And I need to cast it to an int after I multiply but I am having trouble doing that also.
Was This Post Helpful? 0

#15 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Changing RGB in a pixel

Posted 25 January 2011 - 12:43 PM

I used pixel and picture when I had to manipulate pictures using Java.

Basically, you need to create an array of Pixel objects.

I put all the neccessary things into a class so that I can make instances for temporary editing.

Picture picture = new Picture();
String filename = "filename.jpg";
Pixel[] pix = picture.getPixels();
picture.load(filename);



So to change the RGB values, you need to use the getRed() etc methods in an algorithm, and use setRed() etc to change the values.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2