assign RGB values in java with nested for function - java

I have some question, here is my code :
int W = img.getWidth();
int H = img.getHeight();
int [][] pixels = new int [W][H];
int [][][] rgb = new int [3][H][W];
for(int i=0;i<W;i++)
for(int j=0;j<H;j++){
pixels[i][j] = img.getRGB(i,j);
Color clr = new Color(pixels[i][j]);
rgb[0][j][i] = clr.getRed();
rgb[1][j][i] = clr.getGreen();
rgb[2][j][i] = clr.getBlue();
}
/*
pixels changing process
*/
//1st for
for(int[] asd : rgb[0])
System.out.println(Arrays.toString(asd));
//2nd for
for(int i=0;i<W;i++)
for(int j=0;j<H;j++){
/*Color myColor = new Color (rgb[0][i][j],rgb[1][i][j],rgb[2][i][j]);
int newrgb = myColor.getRGB();
img.setRGB(W,H,newrgb);*/
}
}
printing the red values with 1st works normally, but why I can't put that values using 2nd for ?
when I run the code, it issues ByteInterleavedRaster.setDataElements(int, int, Object) line: not available
I want to assign image colors with new values of rgb[0] (red), rgb[1] (green), rgb[2] (blue) that printed by using 1st for. When I expected it could work with 2nd for, it threw an error.
thanks in advance :)

I think this looks correct
int W = img.getWidth();
int H = img.getHeight();
int [][] pixels = new int [W][H];
int [][][] rgb = new int [3][W][H];
for(int i=0; i<W; i++)
for(int j=0; j<H; j++) {
pixels[i][j] = img.getRGB(i,j);
Color clr = new Color(pixels[i][j]);
rgb[0][i][j] = clr.getRed();
rgb[1][i][j] = clr.getGreen();
rgb[2][i][j] = clr.getBlue();
}
/*
pixels changing process
*/
for(int i=0; i<W; i++)
for(int j=0; j<H; j++){
Color myColor = new Color (rgb[0][i][j],
rgb[1][i][j],
rgb[2][i][j]);
int newrgb = myColor.getRGB();
img.setRGB(i, j, newrgb);
}
}

You have to change this line img.setRGB(W,H,newrgb) to this one img.setRGB(j,i,newrgb).

Related

Get Color using integer

The title is a bit confusing but I want to add an integer to a color method. I mean if I had three Colors and want to go through them with a for loop (Something like that below):
Color color1 = something;
Color color2 = something;
Color color3 = something;
for (int i = 1; i < 4; i++) {
int r = color(i).getRed();
int g = color(i).getGreen();
int b = color(i).getBlue();
}
You can store the three colors in an array and access that array inside your loop.
// take these three colors for example
Color[] colors = {Color.BLACK, Color.WHITE, Color.YELLOW};
for (int i=0; i<3; i++) {
int r = colors[i].getRed();
int g = colors[i].getGreen();
int b = colors[i].getBlue();
}

Cannot find symbol - method setColor(java.awt.Color)?

I am attempting to create a new color palette for an image turned to gray scale, and then apply the palette to the gray scale image. I began the method that I wanted to use to apply the palette, but I ran into the error mentioned in the title. I used "java.awt.Color" already in my code, so I am not sure why I am getting the error. Also, as you will see, I placed a color inside the parenthesis.
/**
* This program takes an image, converts it to grayscale, and uses a color palette to create new colors for the image.
*
* #author Dylan Hubbs
* #version 08/02/16
*/
import java.awt.Color ;
class ColorPalette
{
public void grayscaleEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel grayscaleTargetPixel = new Pixel(pictureObj, 0,0);
Color grayscalePixelColor = null;
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
grayscaleTargetPixel = pictureObj.getPixel(x,y);
grayscalePixelColor = grayscaleTargetPixel.getColor(); //gets the color of the target pixel
grayscalePixelColor = new Color((grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3);
grayscaleTargetPixel.setColor(grayscalePixelColor); //sets the new color of the target pixel
}//end of the inner for loop
}//end of the outer for loop
pictureObj.explore(); //explore the Picture object which is now the altered image
pictureObj.write("grayscaleWashingtonMonument.jpg"); //write the altered Picture object to a new file
pictureObj.show();
}
public void paletteEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel paletteTargetPixel = new Pixel(pictureObj, 0,0);
Color palettePixelColor = null;
Color [] palette = {Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.GRAY, Color.PINK, Color.ORANGE};
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
paletteTargetPixel = pictureObj.getPixel(x,y);
palettePixelColor = paletteTargetPixel.getColor();
if(paletteTargetPixel.getRed() >= 1 && paletteTargetPixel.getRed() <= 31)
palettePixelColor.setColor(palette[0]);
else if(paletteTargetPixel.getRed() >= 32 && paletteTargetPixel.getRed() <= 62)
palettePixelColor.setColor(palette[1]);
else if(paletteTargetPixel.retRed() >= 63 && paletteTargetPixel.getRed() <=93)
palettePixelColor.setColor(palette[2]);
}
}
}
}
public class ColorPaletteTester
{
public static void main(String[] args)
{
Picture pictureObj = new Picture("washingtonmonument.jpg"); //creates a new Picture object representing the file in the parameter list
pictureObj.explore();
ColorPalette cp = new ColorPalette();
cp.grayscaleEffect(pictureObj);
cp.paletteEffect(pictureObj);
}
}
So, the error is coming at
palettePixelColor.setColor(palette[0]);
Does anyone know why this would be happening?
palettePixelColor is declared as java.awt.Color, which happens to be an immutable class with no setters. Depending on what Pixel is, it may have such a method.
You are probably trying to do something like
palettePixelColor = palette[0];
or
paletteTargetPixel.setColor(palette[0]);

Getting the color of a pixel [duplicate]

Given an image file, say of PNG format, how to I get an array of int [r,g,b,a] representing the pixel located at row i, column j?
So far I am starting here:
private static int[][][] getPixels(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][][] result = new int[height][width][4];
// SOLUTION GOES HERE....
}
Thanks in advance!
You need to get the packed pixel value as an int, you can then use Color(int, boolean) to build a color object from which you can extract the RGBA values, for example...
private static int[][][] getPixels(BufferedImage image) {
int[][][] result = new int[height][width][4];
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(i, j), true);
result[y][x][0] = c.getRed();
result[y][x][1] = c.getGreen();
result[y][x][2] = c.getBlue();
result[y][x][3] = c.getAlpha();
}
}
}
It's not the most efficient method, but it is one of the simplest
BufferedImages have a method called getRGB(int x, int y) which returns an int where each byte is the components of the pixel (alpha, red, green and blue). If you dont want to do the bitwise operators yourself you can use Colors.getRed/Green/Blue methods by creating a new instance of Java.awt.Color with the int from getRGB.
You can do this in a loop to fill the three-dimensional array.
This is my code for this problem:
File f = new File(filePath);//image path with image name like "lena.jpg"
img = ImageIO.read(f);
if (img==null) //if img null return
return;
//3d array [x][y][a,r,g,b]
int [][][]pixel3DArray= new int[img.getWidth()][img.getHeight()][4];
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int px = img.getRGB(x,y); //get pixel on x,y location
//get alpha;
pixel3DArray[x][y][0] =(px >> 24)& 0xff; //shift number and mask
//get red
pixel3DArray[x][y][1] =(px >> 16)& 0xff;
//get green
pixel3DArray[x][y][2] =(px >> 8)& 0xff;
//get blue
pixel3DArray[x][y][3] =(px >> 0)& 0xff;
}
}

Manipulate pixel colors to create a sunset

I have to create a program that manipulates the color pixels of an image to create a look of a sunset in the background. I know to do this I have to increase the amount of the color red in the picture but whenever I program it it results in an error saying:
java.lang.IllegalArgumentException:
Color parameter outside of expected range: Red(in java.awt.Color)
My program looks like this:
import java.awt.*;
public class TrueColors2
{
public static void main(String[] args)
{
Picture pictureObj = new Picture("WashingtonMonument.png");
//picutreObj.explore();
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel targetPixel = new Pixel(pictureObj, 0, 0);
Color pixelColor = null;
for(int y = 0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
targetPixel = pictureObj.getPixel(x,y);
pixelColor = targetPixel.getColor();
redValue = pixelColor.getRed();
greenValue = pixelColor.getGreen();
blueValue = pixelColor.getBlue();
pixelColor = new Color(redValue, greenValue, blueValue);
pixelColor = new Color(100 + pixelColor.getRed(),
pixelColor.getGreen(),
pixelColor.getBlue());
targetPixel.setColor(pixelColor);
}
}
pictureObj.write("ThirdWashingtonMonument.png");
pictureObj.show();
}
}
Can someone help me figure out what is wrong and help me figure out how to create an effect of a sunset?
Thanks
When you add 100 to your red value, you're probably exceeding the maximum value of 255 at some point. You should consider inspecting the current value of pixelColor.getRed() before you add 100 to it.

negative values in the writableraster

I'm trying to implement averaging filter with different sizes 3x3 , 5x5 , 7x7 and 11x11 .. I did the calculations and the results are correct while debugging but the problem is that it is saved in the writable raster in negative, so I'm getting weird results. The second weird thing is that when I want to get the value of the same pixel that was saved in negative, it is retrieved with positive value !!
I'm using int.
What's wrong? any help ?!!
Here is my code for the 5x5 averaging filter.
public static BufferedImage filter5x5_2D(BufferedImage paddedBI , BufferedImage bi , double[][]filter)
{
WritableRaster myImage = paddedBI.copyData(null);
BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
WritableRaster myImage2 = img.copyData(null);
for(int i =2; i< myImage.getHeight()-2; i++)
{
for(int j =2; j< myImage.getWidth()-2; j++)
{
int value = 0;
int copyi = i-2;
for (int m = 0 ; m<5 ; m++)
{
int copyj = j-2;
for (int n = 0; n<5; n++)
{
int result = myImage.getSample(copyj , copyi, 0);
double f = filter[m][n];
double add = result * filter[m][n];
value += (int) (filter[m][n] * myImage.getSample(copyj , copyi, 0));
copyj ++;
}
copyi++;
//myImage2.setSample(j-1 , i-1, 0, value);
}
myImage2.setSample(j-2 , i-2, 0, value);
//int checkResult = myImage2.getSample(j-1,i-1,0);
}
}
BufferedImage res= new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
res.setData(myImage2);
return res;
}
I do not find any negative values. Here is my main with what I have tested this code:
public static void main(String[] args) throws IOException {
BufferedImage bi = ImageIO.read(new File("C:/Tmp/test.bmp"));
BufferedImage newImage = new BufferedImage(bi.getWidth()+4, bi.getHeight()+4, bi.getType());
Graphics g = newImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,bi.getWidth()+4,bi.getHeight()+4);
g.drawImage(bi, 2, 2, null);
g.dispose();
double[][] filter = new double[5][5];
for( int i = 0; i < 5; ++i){
for( int j = 0; j < 5; ++j){
filter[i][j] = 1.0/(5*5);
}
}
BufferedImage filtered = filter5x5_2D(newImage, bi, filter);
ImageIO.write(filtered, "bmp", new File("C:/tmp/filtered.bmp"));
}
You should consider that your variables result, f and add are unused. Also it would be better if value would be of type double instead of int. In a worst case, you would get 25 times a value of 11 which will be rounded to zero after multiplying with 1/25. This would result in your code as a grey value of zero while it should result in 11.

Categories

Resources