2014-02-13 96 views
1

我有TIFF 256字節的調色板。在Java中,我讀到TIFFBufferedImage。這BufferedImageIndexColorModel。當我遍歷BufferedImage中的像素時,我只能得到RGB。我想寫的方法,這爲x,y從調色板使用BufferedImage(不是RGB顏色,只是從TIFF的調色板的原始索引)獲取原始顏色索引。我怎樣才能做到這一點?使用調色板從TIFF獲取像素顏色索引

我知道我可以遍歷IndexColorModel並檢查RBG是否相等,但如果TIFF至少有2個索引具有相同的顏色(例如,索引0 - 黑色,132 - 黑色;假設該像素10x10有黑色顏色[rgb=0,0,0] - 那麼我不知道應該採用哪個索引 - 它們具有相同的RGB值)。 我也可以讀取原始TIFF,然後計算像素在字節數組中的位置,但我不想這樣做 - 我想使用JAI

有沒有辦法做到這一點與BufferedImageJAI沒有外部庫?

感謝

+0

你能提供一個例子形象與適當的格式? – Marco13

+0

我創建了鏈接http://www.filedropper.com/exampletiff256下的示例文件(我使用簡單文件上傳服務進行搜索,所以如果這個鏈接不會被激活 - 對不起:)) –

回答

3

OK,那麼你可以從BufferedImage的

  • 是DataBuffer從Raster是DataBuffer
  • 獲得

    • 光柵
    • 數據陣列從該數據數組中獲取實際使用的索引。

      這個例子讀取索引,查找相應的顏色在顏色模型,並將結果寫入到一個「標準」的BufferedImage(僅作爲驗證)

      import java.awt.image.BufferedImage; 
      import java.awt.image.ColorModel; 
      import java.awt.image.DataBuffer; 
      import java.awt.image.DataBufferByte; 
      import java.awt.image.IndexColorModel; 
      import java.io.File; 
      import java.io.IOException; 
      
      import javax.imageio.ImageIO; 
      
      public class IndexedBufferedImage 
      { 
          public static void main(String[] args) throws IOException 
          { 
           BufferedImage image = ImageIO.read(new File("exampleTiff256.tif")); 
           System.out.println(image); 
           System.out.println(image.getColorModel()); 
      
           ColorModel colorModel = image.getColorModel(); 
           IndexColorModel indexColorModel = null; 
           if (colorModel instanceof IndexColorModel) 
           { 
            indexColorModel = (IndexColorModel)colorModel; 
           } 
           else 
           { 
            System.out.println("No IndexColorModel"); 
            return; 
           } 
      
           DataBuffer dataBuffer = image.getRaster().getDataBuffer(); 
           DataBufferByte dataBufferByte = null; 
           if (dataBuffer instanceof DataBufferByte) 
           { 
            dataBufferByte = (DataBufferByte)dataBuffer; 
           } 
           else 
           { 
            System.out.println("No DataBufferByte"); 
            return; 
           } 
      
           int w = image.getWidth(); 
           int h = image.getHeight(); 
           BufferedImage test = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
           byte data[] = dataBufferByte.getData(); 
           for (int y=0; y<h; y++) 
           { 
            for (int x=0; x<w; x++) 
            { 
             int arrayIndex = x + y * w; 
             int colorIndex = data[arrayIndex]; 
             int color = indexColorModel.getRGB(colorIndex); 
             System.out.println("At "+x+" "+y+" index is "+colorIndex+ 
              " with color "+Integer.toHexString(color)); 
             test.setRGB(x, y, color); 
            } 
           } 
           ImageIO.write(test, "PNG", new File("exampleTiff256.png")); 
          } 
      } 
      
    +0

    謝謝,這個工程!我發現了一些相似的作品,如果你在這裏是鏈接:http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image再次感謝! –