我正在尝试在 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 中的值时,第二个和第三个字段中的字符很奇怪。有谁知道我在这里误入歧途?ppm 在 Photoshop 中正确打开...