java.awt.image.BufferedImage で扱っているピクセルデータの初期値を知りたい and 透明画像のピクセルデータはどういう値になっているか知りたい、ということで調べてみた。

透明じゃない画像


BufferedImage image =
  new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
int rgb = image.getRGB(0,0);
System.out.println("rgb=" + rgb);

結果: rgb=-16777216

透明な画像


BufferedImage image =
  new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
int rgb = image.getRGB(0,0);
System.out.println("rgb=" + rgb);

結果: rgb=0

# 環境依存かもしれない……

PNGファイルに出力 (追記: 2006-05-09)

で、これでPNGで出力したときに、透明色として出力されるか、アルファ値で出てくるのか、を調べようと思っていたハズなんだけど……エンコーダによって違うとか。

とりあえず javax.imageio.ImageIO クラスでやってみる。


import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
 
public class PNGTest{
 
  public static void main(String[] args) throws Exception{
 
    BufferedImage rgbimage =
      new BufferedImage(
        100, 100, BufferedImage.TYPE_INT_RGB);
    ImageIO.write(
      rgbimage,
      "png",
      new File("RGB.png"));
 
    BufferedImage argbimage =
      new BufferedImage(
        100, 100, BufferedImage.TYPE_INT_ARGB);
    ImageIO.write(
      argbimage,
      "png",
      new File("ARGB.png"));
  }
 
}

PNGファイルの中身は pngcheck で調べる。


C:\>java -version
java version "1.4.2_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_07-b05)
Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode)
 
C:\>javac PNGTest.java
 
C:\>java PNGTest
 
C:\>pngcheck -vv RGB.png
File: RGB.png (109 bytes)
  chunk IHDR at offset 0x0000c, length 13
    100 x 100 image, 24-bit RGB, non-interlaced
  chunk IDAT at offset 0x00025, length 52
    zlib:  deflated, 32K window, maximum compression
    zlib line filters (0 none, 1 sub, 2 up, 3 avg, 4 paeth):
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      (100 out of 100)
  chunk IEND at offset 0x00065, length 0
No errors detected in RGB.png (99.6% compression).
 
C:\>pngcheck -vv ARGB.png
File: ARGB.png (119 bytes)
  chunk IHDR at offset 0x0000c, length 13
    100 x 100 image, 32-bit RGB+alpha, non-interlaced
  chunk IDAT at offset 0x00025, length 62
    zlib:  deflated, 32K window, maximum compression
    zlib line filters (0 none, 1 sub, 2 up, 3 avg, 4 paeth):
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      (100 out of 100)
  chunk IEND at offset 0x0006f, length 0
No errors detected in ARGB.png (99.7% compression).

"RGB+alpha" と出力されている。透明色ではなく、alpha値を使っているということで良いのかな? よくわからない。

Ref. PNG (Portable Network Graphics) Specification


コメント

NI-Lab.は、ネットで透明とか、初期や大きい透明などを依存しなかったの?


tags: zlashdot Java Java

Posted by NI-Lab. (@nilab)