2014-02-13 56 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再次感谢! –