Androidで.ppm画像(ポータブルピックスマップ)を開こうとしています。私はこれを作成するのに十分なフォーマットを解読しました:
public static Bitmap ReadBitmapFromPPM(String file) throws IOException
{
//FileInputStream fs = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new FileReader(file));
if (reader.read() != 'P' || reader.read() != '6')
return null;
reader.read(); //Eat newline
String widths = "", heights = "";
char temp;
while ((temp = (char)reader.read()) != ' ')
widths += temp;
while ((temp = (char)reader.read()) >= '0' && temp <= '9')
heights += temp;
if (reader.read() != '2' || reader.read() != '5' || reader.read() != '5')
return null;
reader.read(); //Eat the last newline
int width = Integer.parseInt(widths);
int height = Integer.parseInt(heights);
int[] colors = new int[width*height];
//Read in the pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char[] pixel = new char[3];
reader.read(pixel);
/*
int red = reader.read();
int green = reader.read();
int blue = reader.read();
byte r = (byte)red;
byte g = (byte)green;
byte b = (byte)blue;*/
colors[y*width + x] = //(255 << 24) | //A
(pixel[0]&0x0ff << 16) | //R
(pixel[1]&0x0ff << 8) | //G
(pixel[2]&0x0ff); //B
}
}
Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
ピクセルをデコードしているところまで来ましたが、最初のピクセルの緑と青の値のASCII値は最大16ビット値です(.read()を使用する場合は65535)。ご覧のとおり、私は色の適切な値にドリルダウンするために多くのことを試みましたが、運がありません。
ppmの値を見ると、2番目と3番目のフィールドの文字がおかしいです。私がここでどこを迷うのか誰か知っていますか?ppmはフォトショップで正しく開きます...