Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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.");
            }
        }
Ejemplo n.º 4
0
        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.");
            }
        }
Ejemplo n.º 5
0
        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));
                }
            }
        }
Ejemplo n.º 6
0
        private static BitmapPalette GetPalette(IImageRead image)
        {
            var clut        = image.GetClut();
            int colorsCount = clut.Length / 4;

            if (colorsCount == 0)
            {
                return(null);
            }

            var colors = new List <Color>(colorsCount);

            for (var i = 0; i < colorsCount; i++)
            {
                colors.Add(GetColor(clut, i * 4));
            }

            return(new BitmapPalette(colors));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
 public static Imgd AsImgd(this IImageRead image, bool isSwizzled = false) =>
 new Imgd(image.Size, image.PixelFormat, image.GetData(), image.GetClut(), isSwizzled);