private static byte[] From4bpp(this IImageRead image, byte[] channelOrder) { var size = image.Size; var data = image.GetData(); var clut = image.GetClut(); var dstData = new byte[size.Width * size.Height * sizeof(uint)]; var srcIndex = 0; var dstIndex = 0; for (var y = 0; y < size.Height; y++) { for (var i = 0; i < size.Width / 2; i++) { var ch = data[srcIndex++]; var palIndex1 = (ch & 15); var palIndex2 = (ch >> 4); dstData[dstIndex++] = clut[palIndex1 * 4 + channelOrder[0]]; dstData[dstIndex++] = clut[palIndex1 * 4 + channelOrder[1]]; dstData[dstIndex++] = clut[palIndex1 * 4 + channelOrder[2]]; dstData[dstIndex++] = clut[palIndex1 * 4 + channelOrder[3]]; dstData[dstIndex++] = clut[palIndex2 * 4 + channelOrder[0]]; dstData[dstIndex++] = clut[palIndex2 * 4 + channelOrder[1]]; dstData[dstIndex++] = clut[palIndex2 * 4 + channelOrder[2]]; dstData[dstIndex++] = clut[palIndex2 * 4 + channelOrder[3]]; } } return(dstData); }
private static byte[] GetDataResource4bpp(IImageRead image) { var size = image.Size; var data = image.GetData(); var clut = image.GetClut(); var dstData = new byte[size.Width * size.Height * sizeof(uint)]; var srcIndex = 0; var dstIndex = 0; for (var y = 0; y < size.Height; y++) { for (var i = 0; i < size.Width / 2; i++) { var ch = data[srcIndex++]; var palIndex1 = (ch >> 4); var palIndex2 = (ch & 15); dstData[dstIndex++] = clut[palIndex1 * 4 + 2]; dstData[dstIndex++] = clut[palIndex1 * 4 + 1]; dstData[dstIndex++] = clut[palIndex1 * 4 + 0]; dstData[dstIndex++] = clut[palIndex1 * 4 + 3]; dstData[dstIndex++] = clut[palIndex2 * 4 + 2]; dstData[dstIndex++] = clut[palIndex2 * 4 + 1]; dstData[dstIndex++] = clut[palIndex2 * 4 + 0]; dstData[dstIndex++] = clut[palIndex2 * 4 + 3]; } } return(dstData); }
public static byte[] ToBgra32(this IImageRead imageRead) { switch (imageRead.PixelFormat) { case PixelFormat.Indexed4: return(ImageDataHelpers.FromIndexed4ToBgra(imageRead.GetData(), imageRead.GetClut())); case PixelFormat.Indexed8: return(ImageDataHelpers.FromIndexed8ToBgra(imageRead.GetData(), imageRead.GetClut())); case PixelFormat.Rgba8888: return(imageRead.GetData()); default: throw new NotImplementedException($"The PixelFormat {imageRead.PixelFormat} cannot be converted to a Bgra32."); } }
public static byte[] AsRgba8888(this IImageRead image) { switch (image.PixelFormat) { case PixelFormat.Indexed4: return(ImageDataHelpers.FromIndexed4ToBitmap32( image.GetData(), image.GetClut(), Rgba)); case PixelFormat.Indexed8: return(ImageDataHelpers.FromIndexed8ToBitmap32( image.GetData(), image.GetClut(), Rgba)); case PixelFormat.Rgba8888: return(ImageDataHelpers.FromBitmap32(image.GetData(), Bgra)); default: throw new ArgumentException($"The pixel format {image.PixelFormat} is not supported."); } }
public static Imgd AsImgd(this IImageRead image, bool isSwizzled = false) { if (image is Imgd imgd) { return(imgd); } else { switch (image.PixelFormat) { case PixelFormat.Rgba8888: case PixelFormat.Rgbx8888: return(new Imgd(image.Size, image.PixelFormat, GetAsRGBA(image.GetData()), image.GetClut(), isSwizzled)); default: return(new Imgd(image.Size, image.PixelFormat, image.GetData(), image.GetClut(), isSwizzled)); } } }
public static BitmapSource GetWindowsMediaImage(this IImageRead image) { var size = image.Size; return(BitmapSource.Create( size.Width, size.Height, 96.0, 96.0, GetPixelFormat(image.PixelFormat), GetPalette(image), image.GetData(), size.Width * GetBpp(image.PixelFormat) / 8)); }
private static byte[] From32bpp(this IImageRead image, byte[] channelOrder) { var srcData = image.GetData(); var dstData = new byte[srcData.Length]; for (var i = 0; i < srcData.Length; i += 4) { dstData[i + 0] = srcData[i + channelOrder[0]]; dstData[i + 1] = srcData[i + channelOrder[1]]; dstData[i + 2] = srcData[i + channelOrder[2]]; dstData[i + 3] = srcData[i + channelOrder[3]]; } return(dstData); }
internal static Bitmap CreateBitmap(this IImageRead imageRead) { var drawingPixelFormat = imageRead.PixelFormat.GetDrawingPixelFormat(); Bitmap bitmap = new Bitmap(imageRead.Size.Width, imageRead.Size.Height, drawingPixelFormat); var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, drawingPixelFormat); var srcData = imageRead.GetData(); var dstLength = Math.Min(srcData.Length, bitmapData.Stride * bitmapData.Height); Marshal.Copy(srcData, 0, bitmapData.Scan0, dstLength); bitmap.UnlockBits(bitmapData); var isIndexed = imageRead.PixelFormat.IsIndexed(); if (isIndexed) { var palette = bitmap.Palette; var clut = imageRead.GetClut(); var colorsCount = Math.Min(clut.Length / 4, palette.Entries.Length); for (var i = 0; i < colorsCount; i++) { palette.Entries[i] = Color.FromArgb( clut[i * 4 + 3], clut[i * 4 + 0], clut[i * 4 + 1], clut[i * 4 + 2]); } bitmap.Palette = palette; } return(bitmap); }
public static Imgd AsImgd(this IImageRead image, bool isSwizzled = false) => new Imgd(image.Size, image.PixelFormat, image.GetData(), image.GetClut(), isSwizzled);