2014-10-06 21 views
2

我有一個類像素,它具有方法getRed,getBlue,getGreen,setRed,setBlue,setGreen(r,g,b) 並且到目前爲止所有東西都單獨工作,但是當我把它放在一起時setGreen和setBlue不工作。我是否在掩蔽方面做錯了什麼?在java中編輯像素值

public class Pixel { 
    int pixel; 
    public Pixel (int pixel) { 
     this.pixel = pixel; 
    } 
    public int getRed() { 
     int red = pixel >> 16; 
     red = red & (0xFF); 
     return red; 
    } 
    public int getGreen() { 
     int green = pixel >> 8; 
     green = green & (0xFF); 
     return green; 
    } 
    public int getBlue() { 
     int blue = pixel; 
     blue = blue & (0xFF); 
     return blue; 
    } 
    public void setRed(int red) { 
     pixel = (pixel & ~(0xFFFF0000)) << 16; 
     pixel= ((red << 16))|pixel ; 
    } 
    public void setGreen(int value) { 
     pixel = (pixel & ~(0xFF00FF00)) << 8; 
     pixel= (value << 8) |pixel; 
    } 
    public void setBlue(int value) { 
     pixel = (pixel & ~(0xFFFFFF00)); 
     pixel= (value) |pixel; 
    } 
    public static void main(String[] args) { 
     Pixel p3 = new Pixel(0xFF000000); 
     System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue()); 

     p3.setRed(42); 
     p3.setGreen(18); 
     p3.setBlue(225); 
     System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue()); 

     p3.setRed(-1); 
     p3.setGreen(500); 
     p3.setBlue(1000); 
     System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue()); 
    } 
} 

我應該得到這個作爲我的結果:

rgb = (0, 0, 0) 
rgb = (42, 18, 225) 
rgb = (255, 244, 232) 

,但是這是即時得到

rgb = (0, 0, 0) 
rgb = (0, 0, 225) 
rgb = (0, 3, 232) 
+0

你怎麼想在你的'int'位佈局?它應該是標準的'RRGGBB'(其中一個字母表示四位)? – 5gon12eder 2014-10-06 01:53:41

+0

一個像素是一個32位的int值,可以解釋爲四個8位值的序列。第一個8位值是alpha通道。我們會忽略這一點。接下來的三個8位值分別是像素的紅色,綠色和藍色分量。 – Panthy 2014-10-06 01:54:14

+0

他們是8位而不是4我相信 – Panthy 2014-10-06 01:56:39

回答

1

比如考慮您的setGreen方法:

public void setGreen(int value) { 
    pixel = (pixel & ~(0xFF00FF00)) << 8; 
    pixel = (value << 8) | pixel; 
} 

最初,您的pixelAARRGGBB。首先,您將GG零件的當前值pixel設置爲0(現在忽略alpha)。 (pixel & ~(0xFF00FF00)) == (AARRGGBB & 0x00FF00FF) == 00RR00BB。然而,接下來你將這個值向左移8位,所以你得到了RR00BB00。接下來,你(正確地)向左移動你得到的綠色值作爲參數。你會得到(000000GG << 8) == 0000GG00。最後,你或以前的價值:(RR00BB00 | 0000GG00) == RR00XX00其中XES是一些垃圾,它們起源於舊的藍色或新的綠色位。你能看到要修復的東西嗎?

setBlue方法存在另一個問題。 (pixel & ~(0xFFFFFF00)) == (AARRGGBB & 000000FF) == 000000BB。將其與上面的setGreen的(正確)掩碼進行比較。

最後,您的方法似乎應該優雅地處理無效值(-1,500,1000)。所以你應該把你的所有參數的最後8位設置爲0.我只是建議因爲你所需要的輸出似乎要求這樣。在寫得很好的Java程序中,如果超出範圍的值被傳遞,拋出異常將更好,而不是靜靜地忽略無效位。或者,您可以更改方法以接受byte而不是int。這將是最好的解決方案,因爲現在不可能通過無效論證。

把它放在一起,這是它如何工作。我還對代碼進行了一些修改,以便我們可以更輕鬆地看到發生的事情。 0位的移位當然是無用的,但是出於光學原因我將它們包括在內。

public final class Pixel { 

    private int value; 

    public Pixel (int rgb) { 
     this.value = value; 
    } 

    public int getRed() { 
     return (this.value >> 16) & 0xFF; 
    } 

    public int getGreen() { 
     return (this.value >> 8) & 0xFF; 
    } 

    public int getBlue() { 
     return (this.value >> 0) & 0xFF; 
    } 

    public void setRed(final int r) { 
     this.value = (this.value & 0xFF00FFFF) | ((r & 0xFF) << 16); 
    } 

    public void setGreen(final int g) { 
     this.value = (this.value & 0xFFFF00FF) | ((g & 0xFF) << 8); 
    } 

    public void setBlue(final int b) { 
     this.value = (this.value & 0xFFFFFF00) | ((b & 0xFF) << 0); 
    } 

    @Override 
    public String toString() { 
     return String.format("rgb = (%3d, %3d, %3d)", 
          this.getRed(), 
          this.getGreen(), 
          this.getBlue()); 
    } 

    public static void main(String[] args) { 
     final Pixel pixel = new Pixel(0); 
     System.out.println(pixel); 
     pixel.setRed(42); 
     pixel.setGreen(18); 
     pixel.setBlue(225); 
     System.out.println(pixel); 
     pixel.setRed(-1); 
     pixel.setGreen(500); 
     pixel.setBlue(1000); 
     System.out.println(pixel); 
    } 
} 

運行上述程序的輸出:

rgb = ( 0, 0, 0) 
rgb = (42, 18, 225) 
rgb = (255, 244, 232) 
+0

是我轉移價值的問題? – Panthy 2014-10-06 02:16:46

+0

是的,「像素」的左移(或者在某些部分出來之後仍然存在)是錯誤的。 – 5gon12eder 2014-10-06 02:18:36

+0

我剛纔拿出第一個左移,然後它仍然不起作用 – Panthy 2014-10-06 02:19:40