private ImageData ReadBitmapWithAlpha(IBinaryStream file, BmpMetaData info) { file.Position = info.ImageLength; var alpha = new byte[info.Width * info.Height]; if (alpha.Length != file.Read(alpha, 0, alpha.Length)) { throw new EndOfStreamException(); } file.Position = info.ImageOffset; int dst_stride = (int)info.Width * 4; var pixels = new byte[(int)info.Height * dst_stride]; int a_src = 0; for (int y = (int)info.Height - 1; y >= 0; --y) { int dst = dst_stride * y; for (int x = 0; x < dst_stride; x += 4) { file.Read(pixels, dst + x, 3); pixels[dst + x + 3] = alpha[a_src++]; } } return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride)); }
public ImageData Read(IBinaryStream file, BmpMetaData info) { if (file.AsStream.CanSeek) { var width_x_height = info.Width * info.Height; uint bmp_length = width_x_height * (uint)info.BPP / 8 + info.ImageOffset; if (bmp_length == info.ImageLength || bmp_length + 2 == info.ImageLength) { if (0x20 == info.BPP) { return(ReadBitmapBGRA(file, info)); } else if (0x18 == info.BPP) { uint length_with_alpha = info.ImageLength + width_x_height; if (length_with_alpha == file.Length || length_with_alpha + info.Width == file.Length) { return(ReadBitmapWithAlpha(file, info)); } } } else if (0x20 == info.BPP && (info.ImageLength - (width_x_height * 3 + info.ImageOffset)) <= 2) { return(ReadBitmapBGRA(file, info)); } } return(null); }
private ImageData ReadBitmapBGRA(IBinaryStream file, BmpMetaData info) { file.Position = info.ImageOffset; int stride = (int)info.Width * 4; var pixels = new byte[(int)info.Height * stride]; bool has_alpha = false; for (int y = (int)info.Height - 1; y >= 0; --y) { int dst = stride * y; file.Read(pixels, dst, stride); for (int x = 3; !has_alpha && x < stride; x += 4) { has_alpha = pixels[dst + x] != 0; } } PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32; return(ImageData.Create(info, format, null, pixels, stride)); }
private ImageData ReadBitmapWithAlpha(Stream file, BmpMetaData info) { file.Position = info.ImageLength; var alpha = new byte[info.Width*info.Height]; if (alpha.Length != file.Read (alpha, 0, alpha.Length)) throw new EndOfStreamException(); file.Position = info.HeaderLength; int dst_stride = (int)info.Width * 4; var pixels = new byte[(int)info.Height * dst_stride]; int a_src = 0; for (int y = (int)info.Height-1; y >= 0; --y) { int dst = dst_stride * y; for (int x = 0; x < dst_stride; x += 4) { file.Read (pixels, dst+x, 3); pixels[dst+x+3] = alpha[a_src++]; } } return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride); }
private ImageData ReadBitmapBGRA(Stream file, BmpMetaData info) { file.Position = info.HeaderLength; int stride = (int)info.Width * 4; var pixels = new byte[(int)info.Height * stride]; bool has_alpha = false; for (int y = (int)info.Height-1; y >= 0; --y) { int dst = stride * y; file.Read (pixels, dst, stride); for (int x = 3; !has_alpha && x < stride; x += 4) has_alpha = pixels[dst+x] != 0; } PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32; return ImageData.Create (info, format, null, pixels, stride); }