Beispiel #1
0
        public static Bitmap ToBitmap(Imgd imgd)
        {
            switch (imgd.PixelFormat)
            {
            case Imaging.PixelFormat.Indexed4:
            {
                var bitmap = new Bitmap(imgd.Size.Width, imgd.Size.Height, PixelFormat.Format4bppIndexed);
                var dest   = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, bitmap.PixelFormat);
                try
                {
                    var sourceBits   = imgd.GetData();
                    var sourceWidth  = imgd.Size.Width;
                    var sourceStride = ((sourceWidth + 1) / 2) & (~1);
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Marshal.Copy(sourceBits, sourceStride * y, dest.Scan0 + dest.Stride * y, sourceStride);
                    }
                }
                finally
                {
                    bitmap.UnlockBits(dest);
                }

                {
                    var clut    = imgd.GetClut();
                    var palette = bitmap.Palette;
                    for (int index = 0; index < 16; index++)
                    {
                        palette.Entries[index] = Color.FromArgb(
                            clut[4 * index + 3],
                            clut[4 * index + 0],
                            clut[4 * index + 1],
                            clut[4 * index + 2]
                            );
                    }
                    bitmap.Palette = palette;
                }

                return(bitmap);
            }

            case Imaging.PixelFormat.Indexed8:
            {
                var bitmap = new Bitmap(imgd.Size.Width, imgd.Size.Height, PixelFormat.Format8bppIndexed);
                var dest   = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, bitmap.PixelFormat);
                try
                {
                    var sourceBits  = imgd.GetData();
                    var sourceWidth = imgd.Size.Width;
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Marshal.Copy(sourceBits, sourceWidth * y, dest.Scan0 + dest.Stride * y, sourceWidth);
                    }
                }
                finally
                {
                    bitmap.UnlockBits(dest);
                }

                {
                    var clut    = imgd.GetClut();
                    var palette = bitmap.Palette;
                    for (int index = 0; index < 256; index++)
                    {
                        palette.Entries[index] = Color.FromArgb(
                            clut[4 * index + 3],
                            clut[4 * index + 0],
                            clut[4 * index + 1],
                            clut[4 * index + 2]
                            );
                    }
                    bitmap.Palette = palette;
                }

                return(bitmap);
            }

            case Imaging.PixelFormat.Rgba8888:
            {
                var bitmap = new Bitmap(imgd.Size.Width, imgd.Size.Height, PixelFormat.Format32bppPArgb);
                var dest   = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, bitmap.PixelFormat);
                try
                {
                    var sourceBits  = imgd.GetData();
                    var sourceWidth = 4 * imgd.Size.Width;
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Marshal.Copy(sourceBits, sourceWidth * y, dest.Scan0 + dest.Stride * y, sourceWidth);
                    }
                }
                finally
                {
                    bitmap.UnlockBits(dest);
                }

                return(bitmap);
            }
            }
            throw new NotSupportedException($"{imgd.PixelFormat} not recognized!");
        }
Beispiel #2
0
        public static Imgd NormalizeImageSize(Imgd imgd)
        {
            if (false ||
                !AllowedTextureWidthList.Contains(imgd.Size.Width) ||
                !AllowedTextureHeightList.Contains(imgd.Size.Height)
                )
            {
                var newWidth = (imgd.Size.Width <= 128) ? 128
                    : (imgd.Size.Width <= 256) ? 256
                    : 512;

                var newHeight = (imgd.Size.Height <= 64) ? 64
                    : (imgd.Size.Height <= 128) ? 128
                    : (imgd.Size.Height <= 256) ? 256
                    : 512;

                var width  = imgd.Size.Width;
                var height = imgd.Size.Height;

                logger.Info($"This image will be resized due to comformance of texture size. Resizing from ({width}, {height}) to ({newWidth}, {newHeight}).");

                if (imgd.PixelFormat == Imaging.PixelFormat.Indexed8)
                {
                    var bits      = imgd.GetData();
                    var newBits   = new byte[newWidth * newHeight];
                    var dstToSrcX = Enumerable.Range(0, newWidth)
                                    .Select(xPos => (int)(xPos / (float)newWidth * width))
                                    .ToArray();
                    var dstToSrcY = Enumerable.Range(0, newHeight)
                                    .Select(yPos => (int)(yPos / (float)newHeight * height))
                                    .ToArray();

                    for (var y = 0; y < newHeight; y++)
                    {
                        var dstOffset = newWidth * y;
                        var srcOffset = width * dstToSrcY[y];

                        for (var x = 0; x < newWidth; x++)
                        {
                            newBits[dstOffset + x] = bits[srcOffset + dstToSrcX[x]];
                        }
                    }

                    return(new Imgd(
                               new Size(newWidth, newHeight),
                               Imaging.PixelFormat.Indexed8,
                               newBits,
                               imgd.GetClut(),
                               false
                               ));
                }
                else
                {
                    throw new NotSupportedException($"{imgd}");
                }
            }
            else
            {
                return(imgd);
            }
        }