Beispiel #1
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            KsltImageData imageData = ImageData[imageIndex];

            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width           = imageData.Width;
            imageBinary.Height          = imageData.Height;
            imageBinary.InputEndianness = Endian.LittleEndian;

            switch (imageData.PixelFormat)
            {
            case KsltPixelFormat.Argb8888:
                imageBinary.InputPixelFormat = PixelDataFormat.FormatArgb8888;
                break;

            case KsltPixelFormat.DXT5:
                imageBinary.InputPixelFormat  = PixelDataFormat.FormatDXT5;
                imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
                break;

            default: throw new NotImplementedException(string.Format("KSLT format 0x{0:X8}", (uint)imageData.PixelFormat));
            }

            imageBinary.AddInputPixels(imageData.PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #2
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width           = (int)Width;
            imageBinary.Height          = (int)Height;
            imageBinary.InputEndianness = Endian.LittleEndian;

            if (PaletteData != null)
            {
                imageBinary.InputPaletteFormat = PixelDataFormat.FormatAbgr8888;
                imageBinary.AddInputPalette(PaletteData);
            }

            switch (PixelFormat)
            {
            case TxgPixelFormat.Abgr8888:
                imageBinary.InputPixelFormat = PixelDataFormat.FormatAbgr8888 | PixelDataFormat.PixelOrderingSwizzledPSP;
                break;

            case TxgPixelFormat.Bgr565:
                imageBinary.InputPixelFormat = PixelDataFormat.FormatBgr565 | PixelDataFormat.PixelOrderingTiled;
                break;

            case TxgPixelFormat.Indexed8bpp:
                imageBinary.InputPixelFormat = PixelDataFormat.FormatIndexed8 | PixelDataFormat.PixelOrderingSwizzledPSP;
                break;

            default: throw new NotImplementedException();
            }

            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #3
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat = PixelDataFormat.Undefined;

            switch (PixelFormat)
            {
            case NmtPixelFormat.Indexed4bpp: pixelDataFormat = PixelDataFormat.FormatIndexed4; break;

            case NmtPixelFormat.Indexed8bpp: pixelDataFormat = PixelDataFormat.FormatIndexed8; break;

            case NmtPixelFormat.Argb8888: pixelDataFormat = PixelDataFormat.FormatArgb8888; break;
            }

            imageBinary.Width              = Width;
            imageBinary.Height             = Height;
            imageBinary.InputPaletteFormat = PixelDataFormat.FormatArgb8888;
            imageBinary.InputPixelFormat   = pixelDataFormat;
            imageBinary.InputEndianness    = Endian.LittleEndian;

            foreach (byte[] palette in PaletteData)
            {
                imageBinary.AddInputPalette(palette);
            }
            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #4
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            List <PSSGNode> allTextures    = RootNode.FindNodes("TEXTURE");
            PSSGNode        texNode        = allTextures[imageIndex];
            Int32           width          = Convert.ToInt32(texNode.Attributes["width"].Value);
            Int32           height         = Convert.ToInt32(texNode.Attributes["height"].Value);
            Int32           numBlocks      = Convert.ToInt32(texNode.Attributes["imageBlockCount"].Value);
            string          texelFormat    = texNode.Attributes["texelFormat"].Value;
            PixelDataFormat pixelFormat    = PixelDataFormat.Undefined;
            bool            flipY          = false;
            List <PSSGNode> texImageBlocks = texNode.FindNodes("TEXTUREIMAGEBLOCK");

            switch (texelFormat)
            {
            case "ui8x4":
                pixelFormat = PixelDataFormat.FormatArgb8888 | PixelDataFormat.PixelOrderingLinear;
                flipY       = true;
                break;

            case "u8x4":
                pixelFormat = PixelDataFormat.FormatAbgr8888 | PixelDataFormat.PixelOrderingLinear;
                flipY       = true;
                break;

            case "dxt1":
                pixelFormat = PixelDataFormat.FormatDXT1Rgba;
                flipY       = true;
                break;

            case "dxt5":
                pixelFormat = PixelDataFormat.FormatDXT5;
                flipY       = true;
                break;

            default:
                throw new NotSupportedException(String.Format("Unsupported PSSG texel Format: {0}", texelFormat));
            }

            /* find out how many raw data blocks there are */
            if (numBlocks > 1)
            {
                throw new NotSupportedException("Loading PSSG cube maps is not yet supported");
            }
            else
            {
                /* we only have a single block. use that */
                ImageBinary imgbin = new ImageBinary();
                imgbin.Width            = width;
                imgbin.Height           = height;
                imgbin.InputPixelFormat = pixelFormat;
                imgbin.InputEndianness  = Endian.LittleEndian;
                imgbin.AddInputPixels(texImageBlocks[0].FindNodes("TEXTUREIMAGEBLOCKDATA")[0].Data);
                Bitmap bmp = imgbin.GetBitmap();
                if (flipY)
                {
                    bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }
                return(bmp);
            }
        }
Beispiel #5
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            ImageWidth    = reader.ReadUInt16();
            ImageHeight   = reader.ReadUInt16();
            ColorCount    = reader.ReadUInt16();
            Unknown0x06   = reader.ReadUInt16();
            PaletteWidth  = reader.ReadUInt16();
            PaletteHeight = reader.ReadUInt16();
            Padding       = reader.ReadUInt32();

            PaletteData = new byte[PaletteHeight][];
            for (int py = 0; py < PaletteHeight; py++)
            {
                PaletteData[py] = PS2.ReadPaletteData(reader, (ColorCount == 256 ? PS2PixelFormat.PSMT8 : PS2PixelFormat.PSMT4), PS2PixelFormat.PSMCT32);
            }

            PixelData = reader.ReadBytes((ImageWidth * ImageHeight) / (ColorCount == 256 ? 1 : 2));

            /* Initialize ImageBinary */
            imageBinary                    = new ImageBinary();
            imageBinary.Width              = ImageWidth;
            imageBinary.Height             = ImageHeight;
            imageBinary.InputPaletteFormat = PixelDataFormat.FormatBgra8888;
            imageBinary.InputPixelFormat   = (ColorCount == 256 ? PixelDataFormat.FormatIndexed8 : PixelDataFormat.FormatIndexed4);
            imageBinary.InputEndianness    = Endian.LittleEndian;

            foreach (byte[] palette in PaletteData)
            {
                imageBinary.AddInputPalette(palette);
            }
            imageBinary.AddInputPixels(PixelData);
        }
Beispiel #6
0
        public void TestAsBytes(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            const byte Threshold = 0x7F;

            // Get bytes for image
            byte[] imageBytes = ImageBinary.AsBytes(sourceImage, Threshold);

            // Check we have some bytes
            Assert.IsNotNull(imageBytes);

            // Check all converted to binary (0 or 1)
            Assert.IsTrue(imageBytes.All(b => b < 0x02));

            // Check correct number of bytes after conversion
            // (Same per source image)
            Assert.AreEqual(118287, imageBytes.Length);

            // Count 1's and 0's
            int countZero = imageBytes.Count(b => b == 0x00);
            int countOne  = imageBytes.Count(b => b == 0x01);

            // May be slight variances due to compression
            // but should be similar.
            Assert.IsTrue(countZero > 52000 && countZero < 52100);
            Assert.IsTrue(countOne > 66200 && countOne < 66300);
        }
Beispiel #7
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat = PixelDataFormat.Undefined;

            switch (Header.PixelFormat)
            {
            case TxfPixelFormat.Argb8888: pixelDataFormat = PixelDataFormat.FormatArgb8888; break;

            case TxfPixelFormat.RgbaDxt1: pixelDataFormat = PixelDataFormat.FormatDXT1Rgba; break;

            case TxfPixelFormat.RgbaDxt3: pixelDataFormat = PixelDataFormat.FormatDXT3; break;

            case TxfPixelFormat.RgbaDxt5: pixelDataFormat = PixelDataFormat.FormatDXT5; break;

            case TxfPixelFormat.Argb1555: pixelDataFormat = PixelDataFormat.FormatArgb1555; break;

            case TxfPixelFormat.Argb4444: pixelDataFormat = PixelDataFormat.FormatArgb4444; break;

            case TxfPixelFormat.Rgb565: pixelDataFormat = PixelDataFormat.FormatRgb565; break;
            }

            imageBinary.Width  = Header.Width;
            imageBinary.Height = Header.Height;
            //imageBinary.InputPaletteFormat = PixelDataFormat.FormatArgb8888;
            imageBinary.InputPixelFormat = pixelDataFormat;
            imageBinary.InputEndianness  = Endian.BigEndian;

            //imageBinary.AddInputPalette(PaletteData);
            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #8
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            PixelDataFormat inputPixelFormat = PixelDataFormat.Undefined;

            int physicalWidth, physicalHeight;

            if (DDSHeader.PixelFormat.Flags.HasFlag(DDPF.FourCC))
            {
                switch (DDSHeader.PixelFormat.FourCC)
                {
                case "DXT1": inputPixelFormat = PixelDataFormat.FormatDXT1Rgba; break;

                case "DXT3": inputPixelFormat = PixelDataFormat.FormatDXT3; break;

                case "DXT5": inputPixelFormat = PixelDataFormat.FormatDXT5; break;

                case "PVR3": inputPixelFormat = PixelDataFormat.FormatPVRT4_Vita; break;        // TODO: verify, probably not quite like PVRT4?

                case "PVR4": inputPixelFormat = PixelDataFormat.FormatPVRT4_Vita; break;
                }

                physicalWidth = physicalHeight = 1;
                while (physicalWidth < DDSHeader.Width)
                {
                    physicalWidth *= 2;
                }
                while (physicalHeight < DDSHeader.Height)
                {
                    physicalHeight *= 2;
                }
            }
            else
            {
                physicalWidth  = (int)DDSHeader.Width;
                physicalHeight = (int)DDSHeader.Height;
            }

            if (inputPixelFormat == PixelDataFormat.Undefined)
            {
                throw new NotImplementedException("DDS pixel format not implemented");
            }

            // TODO: total guesswork, verify me!
            if (DDSHeader.Reserved1[0x0A] == 0x00020008)
            {
                inputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
            }

            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width            = (int)DDSHeader.Width;
            imageBinary.Height           = (int)DDSHeader.Height;
            imageBinary.PhysicalWidth    = physicalWidth;
            imageBinary.PhysicalHeight   = physicalHeight;
            imageBinary.InputPixelFormat = inputPixelFormat;
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #9
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat = PixelDataFormat.Undefined;

            switch (PixelFormat)
            {
            case DMPBMPixelFormat.Alpha8: pixelDataFormat = PixelDataFormat.FormatAlpha8; break;

            case DMPBMPixelFormat.Rgba4444: pixelDataFormat = PixelDataFormat.FormatRgba4444; break;

            case DMPBMPixelFormat.Rgba5551: pixelDataFormat = PixelDataFormat.FormatRgba5551; break;

            case DMPBMPixelFormat.Rgba8888: pixelDataFormat = PixelDataFormat.FormatRgba8888; break;

            default: throw new NotImplementedException(string.Format("DMPBM format 0x{0:X}", PixelFormat));
            }
            pixelDataFormat |= PixelDataFormat.PixelOrderingTiled3DS;

            imageBinary.Width            = (int)Width;
            imageBinary.Height           = (int)Height;
            imageBinary.InputPixelFormat = pixelDataFormat;
            imageBinary.InputEndianness  = Endian.LittleEndian;

            imageBinary.AddInputPixels(PixelData);

            Bitmap bitmap = imageBinary.GetBitmap();

            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return(bitmap);
        }
Beispiel #10
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            VtxpImageHeader imageHeader = ImageHeaders[imageIndex];

            bool isIndexed = (imageHeader.PaletteOffset != 0);

            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width           = imageHeader.Width;
            imageBinary.Height          = imageHeader.Height;
            imageBinary.InputEndianness = Endian.LittleEndian;

            if (isIndexed)
            {
                imageBinary.InputPaletteFormat = PSVita.GetPaletteFormat(imageHeader.TextureFormat);
                imageBinary.AddInputPalette(imageHeader.PaletteData);
            }

            imageBinary.InputPixelFormat = PSVita.GetPixelDataFormat(imageHeader.TextureFormat);
            imageBinary.AddInputPixels(imageHeader.PixelData);

            if (imageHeader.TextureType == VtxpTextureType.Swizzled)
            {
                imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
            }

            return(imageBinary.GetBitmap());
        }
Beispiel #11
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width            = mipmapData[imageIndex].Width;
            imageBinary.Height           = mipmapData[imageIndex].Height;
            imageBinary.InputPixelFormat = (mappingInfo.Item1 /*| PixelDataFormat.PixelOrderingSwizzledSwitch*/);
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(mipmapData[imageIndex].PixelData);

            return(imageBinary.GetBitmap(0, 0));
        }
Beispiel #12
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            G1TGImageDataShim imageDataShim = imageDataShims[imageIndex];

            imageBinary = new ImageBinary();
            imageBinary.AddInputPixels(imageDataShim.Data);
            imageBinary.InputPixelFormat = imageDataShim.Format;
            imageBinary.Width            = imageDataShim.Width;
            imageBinary.Height           = imageDataShim.Height;

            return(imageBinary.GetBitmap(0, 0));
        }
Beispiel #13
0
        /* Shim functions */
        public static byte[] Decompress(EndianBinaryReader reader, int width, int height, PixelDataFormat inputFormat, long readLength)
        {
            byte[] outPixels = new byte[readLength * 8];

            PixelOrderingDelegate pixelOrderingFunc = ImageBinary.GetPixelOrderingFunction(inputFormat & PixelDataFormat.MaskPixelOrdering);

            for (int y = 0; y < height; y += 4)
            {
                for (int x = 0; x < width; x += 4)
                {
                    ushort[] decompressedBlock = new ushort[(4 * 4) * 4];
                    if ((inputFormat & PixelDataFormat.MaskSpecial) == PixelDataFormat.SpecialFormatBPTC_Float)
                    {
                        DecompressBlockBPTCFloatShared(reader, false, ref decompressedBlock);
                    }
                    else if ((inputFormat & PixelDataFormat.MaskSpecial) == PixelDataFormat.SpecialFormatBPTC_SignedFloat)
                    {
                        DecompressBlockBPTCFloatShared(reader, true, ref decompressedBlock);
                    }
                    else
                    {
                        throw new Exception("Trying to decode BPTC Float/SignedFloat with format set to non-BPTC");
                    }

                    int rx, ry;
                    pixelOrderingFunc(x / 4, y / 4, width / 4, height / 4, inputFormat, out rx, out ry);
                    rx *= 4; ry *= 4;

                    for (int py = 0; py < 4; py++)
                    {
                        for (int px = 0; px < 4; px++)
                        {
                            int ix = (rx + px);
                            int iy = (ry + py);

                            if (ix >= width || iy >= height)
                            {
                                continue;
                            }

                            for (int c = 0; c < 4; c++)
                            {
                                float value = Float16toFloat32(decompressedBlock[(((py * 4) + px) * 4) + c]);
                                outPixels[(((iy * width) + ix) * 4) + c] = (byte)(value * 255);
                            }
                        }
                    }
                }
            }

            return(outPixels);
        }
Beispiel #14
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width            = (int)Width;
            imageBinary.Height           = (int)Height;
            imageBinary.InputPixelFormat = pixelDataFormat;
            imageBinary.InputEndianness  = Endian.LittleEndian;

            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #15
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary
            {
                Width            = mipmapData[imageIndex].Width,
                Height           = mipmapData[imageIndex].Height,
                InputPixelFormat = pixelDataFormat
            };

            imageBinary.AddInputPixels(mipmapData[imageIndex].PixelData);

            return(imageBinary.GetBitmap(0, 0));
        }
Beispiel #16
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat = PixelDataFormat.Undefined;

            switch (PixelFormat)
            {
            case DMPBMPixelFormat.Alpha8: pixelDataFormat = PixelDataFormat.FormatAlpha8; break;

            case DMPBMPixelFormat.Rgba4444: pixelDataFormat = PixelDataFormat.FormatRgba4444; break;

            case DMPBMPixelFormat.Rgba5551: pixelDataFormat = PixelDataFormat.FormatRgba5551; break;

            case DMPBMPixelFormat.Rgba8888: pixelDataFormat = PixelDataFormat.FormatRgba8888; break;

            case DMPBMPixelFormat.Indexed8: pixelDataFormat = PixelDataFormat.FormatIndexed8; break;

            default: throw new NotImplementedException(string.Format("DMPBM format 0x{0:X}", PixelFormat));
            }
            pixelDataFormat |= PixelDataFormat.PixelOrderingTiled3DS;

            imageBinary.Width            = (int)Width;
            imageBinary.Height           = (int)Height;
            imageBinary.InputPixelFormat = pixelDataFormat;
            imageBinary.InputEndianness  = Endian.LittleEndian;

            imageBinary.AddInputPixels(PixelData);

            if (PaletteData != null)
            {
                switch (PixelFormat)
                {
                // XBGR1555 ?
                case DMPBMPixelFormat.Indexed8:
                    imageBinary.InputPaletteFormat = (PixelDataFormat.Bpp16 | PixelDataFormat.ChannelsXbgr | PixelDataFormat.RedBits5 | PixelDataFormat.GreenBits5 | PixelDataFormat.BlueBits5 | PixelDataFormat.AlphaBits1);
                    break;

                default: throw new NotImplementedException(string.Format("DMPBM palette for format 0x{0:X}", PixelFormat));
                }

                imageBinary.AddInputPalette(PaletteData);
            }

            Bitmap bitmap = imageBinary.GetBitmap();

            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return(bitmap);
        }
Beispiel #17
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary
            {
                PhysicalWidth    = PhysicalWidth,
                PhysicalHeight   = PhysicalHeight,
                Width            = VirtualWidth,
                Height           = VirtualHeight,
                InputPixelFormat = pixelDataFormat,
                InputEndianness  = Endian.LittleEndian
            };

            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap(imageIndex, paletteIndex));
        }
Beispiel #18
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat, paletteDataFormat;

            if (Format == 0x8602)
            {
                pixelDataFormat   = PixelDataFormat.FormatIndexed4 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = PixelDataFormat.FormatAbgr4444;
            }
            else if (Format == 0x8803)
            {
                pixelDataFormat   = PixelDataFormat.FormatIndexed4 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = PixelDataFormat.FormatAbgr8888;
            }
            else if (Format == 0x8A02)
            {
                pixelDataFormat   = PixelDataFormat.FormatIndexed8 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = PixelDataFormat.FormatAbgr4444;
            }
            else if (Format == 0x8C03)
            {
                pixelDataFormat   = PixelDataFormat.FormatIndexed8 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = PixelDataFormat.FormatAbgr8888;
            }
            else if (Format == 0x800A)
            {
                pixelDataFormat   = PixelDataFormat.FormatDXT1Rgba_PSP;
                paletteDataFormat = PixelDataFormat.Undefined;
            }
            else
            {
                throw new NotImplementedException();
            }

            imageBinary.Width              = Width;
            imageBinary.Height             = Height;
            imageBinary.InputPaletteFormat = paletteDataFormat;
            imageBinary.InputPixelFormat   = pixelDataFormat;
            imageBinary.InputEndianness    = Endian.LittleEndian;

            imageBinary.AddInputPalette(PaletteData);
            imageBinary.AddInputPixels(PixelData);

            return(imageBinary.GetBitmap());
        }
Beispiel #19
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            MagicNumber   = Encoding.ASCII.GetString(reader.ReadBytes(4), 0, 4);
            Unknown0x04   = reader.ReadUInt32();
            Constant3553  = reader.ReadUInt32();
            Width         = reader.ReadUInt32();
            Height        = reader.ReadUInt32();
            DataType      = (PicaDataType)reader.ReadUInt32();
            PixelFormat   = (PicaPixelFormat)reader.ReadUInt32();
            NumImageBytes = reader.ReadUInt32();

            /* Disclaimer: Hacky as hell! I almost want to hope that Atlus someday leaves their internal STEX creator tool inside one of these games, maybe that'll help with figuring this out <.< */

            /* ...now at offset 0x20, assume here's the pointer to image data */
            ImageOffset = reader.ReadUInt32();

            /* ...now assume said pointer is 0x80 */
            if (ImageOffset == 0x80)
            {
                /* Read "well-formed" STEX (but really, who knows how the header's supposed to be) */
                Unknown0x24 = reader.ReadUInt32();
                Name        = Encoding.ASCII.GetString(reader.ReadBytes(0x58), 0, 0x58).TrimEnd('\0');

                /* ...but as image datasize is also unreliable, do some additional sanity checking on that, too! */
                reader.BaseStream.Seek(ImageOffset, SeekOrigin.Begin);
                PixelData = reader.ReadBytes((int)(NumImageBytes > reader.BaseStream.Length ? reader.BaseStream.Length - ImageOffset : NumImageBytes));
            }
            else /* ...otherwise... */
            {
                /* Seek back, then just assume image data starts right here at 0x20, and that the image is as many bytes long as are left in the file */
                reader.BaseStream.Seek(-4, SeekOrigin.Current);

                ImageOffset = (uint)reader.BaseStream.Position;
                Unknown0x24 = uint.MaxValue;
                Name        = string.Empty;

                PixelData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
            }

            /* Initialize ImageBinary */
            imageBinary                  = new ImageBinary();
            imageBinary.Width            = (int)Width;
            imageBinary.Height           = (int)Height;
            imageBinary.InputPixelFormat = N3DS.GetPixelDataFormat(DataType, PixelFormat);
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(PixelData);
        }
Beispiel #20
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            CTXBTexture texture = Textures[imageIndex];

            PicaPixelFormat pixelFormat = texture.PixelFormat;
            PicaDataType    dataType    = ((pixelFormat == PicaPixelFormat.ETC1RGB8NativeDMP || pixelFormat == PicaPixelFormat.ETC1AlphaRGB8A4NativeDMP) ? PicaDataType.UnsignedByte : texture.DataType);

            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width            = texture.Width;
            imageBinary.Height           = texture.Height;
            imageBinary.InputPixelFormat = N3DS.GetPixelDataFormat(dataType, pixelFormat);
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(pixelData[imageIndex]);

            return(imageBinary.GetBitmap(0, 0));
        }
Beispiel #21
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
#if DEBUG
            // DEBUG: if Scarlet format isn't set, return empty image
            if (pixelDataFormat == PixelDataFormat.Undefined)
            {
                return(new Bitmap(mipmapLevels[imageIndex].Width, mipmapLevels[imageIndex].Height));
            }
#endif
            ImageBinary imageBinary = new ImageBinary();
            imageBinary.Width            = mipmapLevels[imageIndex].Width;
            imageBinary.Height           = mipmapLevels[imageIndex].Height;
            imageBinary.InputPixelFormat = pixelDataFormat;
            imageBinary.InputEndianness  = Endian.LittleEndian; // ????
            imageBinary.AddInputPixels(mipmapLevels[imageIndex].PixelData);

            return(imageBinary.GetBitmap(0, 0));
        }
Beispiel #22
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            imageBinary                  = new ImageBinary();
            imageBinary.Width            = mipmapData[imageIndex].Width;
            imageBinary.Height           = mipmapData[imageIndex].Height;
            imageBinary.InputPixelFormat = N3DS.GetPixelDataFormat(header.PixelFormat);
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(mipmapData[imageIndex].PixelData);

            // Don't pass in original imageIndex and paletteIndex; Scarlet can't handle multiple widths/heights per ImageBinary, so we have to recreate it every time

            // TODO: uh, verify the whole Y flipping business here...

            //return imageBinary.GetBitmap(0, 0);
            Bitmap bitmap = imageBinary.GetBitmap(0, 0);

            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return(bitmap);
        }
Beispiel #23
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            ImageWidth    = reader.ReadUInt16();
            ImageHeight   = reader.ReadUInt16();
            ColorCount    = reader.ReadUInt16();
            Unknown0x06   = reader.ReadUInt16();
            PaletteWidth  = reader.ReadUInt16();
            PaletteHeight = reader.ReadUInt16();
            SwizzleFlag   = reader.ReadUInt16();
            Unknown0x0E   = reader.ReadUInt16();

            PaletteData = new byte[PaletteHeight][];
            for (int py = 0; py < PaletteHeight; py++)
            {
                PaletteData[py] = new byte[ColorCount * 4];
                for (int px = 0; px < PaletteWidth; px++)
                {
                    Buffer.BlockCopy(reader.ReadBytes(4), 0, PaletteData[py], px * 4, 4);
                }
            }

            PixelData = reader.ReadBytes((ImageWidth * ImageHeight) / (ColorCount == 256 ? 1 : 2));

            imageBinary                    = new ImageBinary();
            imageBinary.Width              = ImageWidth;
            imageBinary.Height             = ImageHeight;
            imageBinary.InputPaletteFormat = PixelDataFormat.FormatAbgr8888;
            imageBinary.InputPixelFormat   = (ColorCount == 256 ? PixelDataFormat.FormatIndexed8 : PixelDataFormat.FormatIndexed4);
            if (SwizzleFlag != 0)
            {
                imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledPSP;
            }
            imageBinary.InputEndianness = Endian.LittleEndian;

            foreach (byte[] palette in PaletteData)
            {
                imageBinary.AddInputPalette(palette);
            }
            imageBinary.AddInputPixels(PixelData);
        }
Beispiel #24
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            Tag         = Encoding.ASCII.GetString(reader.ReadBytes(6));
            Width       = reader.ReadUInt16();
            Height      = reader.ReadUInt16();
            Unknown0x0A = reader.ReadByte();
            Unknown0x0B = reader.ReadByte();

            PaletteData = reader.ReadBytes(256 * 4);
            PixelData   = reader.ReadBytes(Width * Height);

            /* Initialize ImageBinary */
            imageBinary                    = new ImageBinary();
            imageBinary.Width              = Width;
            imageBinary.Height             = Height;
            imageBinary.InputPaletteFormat = PixelDataFormat.FormatAbgr8888;
            imageBinary.InputPixelFormat   = PixelDataFormat.FormatIndexed8;
            imageBinary.InputEndianness    = Endian.LittleEndian;

            imageBinary.AddInputPalette(PaletteData);
            imageBinary.AddInputPixels(PixelData);
        }
Beispiel #25
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            Unknown0x00   = reader.ReadUInt16();
            ID            = reader.ReadUInt16();
            FileSize      = reader.ReadUInt32();
            MagicNumber   = Encoding.ASCII.GetString(reader.ReadBytes(4), 0, 4);
            Unknown0x0C   = reader.ReadUInt32();
            Unknown0x10   = reader.ReadByte();
            PaletteFormat = (PS2PixelFormat)reader.ReadByte();
            Width         = reader.ReadUInt16();
            Height        = reader.ReadUInt16();
            PixelFormat   = (PS2PixelFormat)reader.ReadByte();
            MipmapCount   = reader.ReadByte();
            MipmapKValue  = reader.ReadByte();
            MipmapLValue  = reader.ReadByte();
            TextureWrap   = (PS2WrapMode)reader.ReadUInt16();
            TextureID     = reader.ReadUInt32();
            CLUTID        = reader.ReadUInt32();
            Comment       = Encoding.ASCII.GetString(reader.ReadBytes(0x1C), 0, 0x1C);

            if (PS2.IsFormatIndexed(PixelFormat))
            {
                PaletteData = PS2.ReadPaletteData(reader, PixelFormat, PaletteFormat);
            }

            PixelData = reader.ReadBytes(CalculatePixelDataSize(Width, Height, PixelFormat));

            imageBinary                    = new ImageBinary();
            imageBinary.Width              = (int)Width;
            imageBinary.Height             = (int)Height;
            imageBinary.InputPaletteFormat = PS2.GetPixelDataFormat(PaletteFormat);
            imageBinary.InputPixelFormat   = PS2.GetPixelDataFormat(PixelFormat);
            imageBinary.InputEndianness    = Endian.LittleEndian;

            imageBinary.AddInputPalette(PaletteData);
            imageBinary.AddInputPixels(PixelData);
        }
Beispiel #26
0
        protected override void OnOpen(EndianBinaryReader reader)
        {
            MagicNumber       = Encoding.ASCII.GetString(reader.ReadBytes(3));
            PixelFormat       = reader.ReadByte();
            FileSize          = reader.ReadUInt32();
            ImageDataOffset   = reader.ReadUInt32();
            Unknown0x0C       = reader.ReadUInt32();
            Unknown0x10       = reader.ReadUInt32();
            Unknown0x14       = reader.ReadUInt32();
            Unknown0x18       = reader.ReadUInt32();
            Unknown0x1C       = reader.ReadUInt32();
            FileName          = Encoding.ASCII.GetString(reader.ReadBytes(0x20)).TrimEnd('\0');
            Unknown0x40       = reader.ReadUInt32();
            Width             = reader.ReadUInt32();
            Height            = reader.ReadUInt32();
            BitsPerPixel      = reader.ReadUInt32();
            Unknown0x50       = reader.ReadUInt16();
            Unknown0x52       = reader.ReadUInt16();
            PaletteDataSize   = reader.ReadUInt32();
            ImageDataSize     = reader.ReadUInt32();
            Unknown0x5C       = reader.ReadUInt32();
            Unknown0x60       = reader.ReadUInt32();
            CompressionFourCC = Encoding.ASCII.GetString(reader.ReadBytes(4)).TrimEnd('\0');
            Unknown0x68       = reader.ReadUInt32();
            Unknown0x6C       = reader.ReadUInt32();
            Unknown0x70       = reader.ReadUInt32();
            Unknown0x74       = reader.ReadUInt32();
            Unknown0x78       = reader.ReadByte();
            Unknown0x79       = reader.ReadByte();
            Unknown0x7A       = reader.ReadUInt16();
            Unknown0x7C       = reader.ReadUInt16();
            Unknown0x7E       = reader.ReadUInt16();

            if (reader.BaseStream.Position != 0x80)
            {
                throw new Exception("TID stream position mismatch");
            }

            PaletteData = reader.ReadBytes((int)PaletteDataSize);

            reader.BaseStream.Seek(ImageDataOffset, SeekOrigin.Begin);
            PixelData = reader.ReadBytes((int)ImageDataSize);

            TidFormatChannelOrder    pixelChannelOrder = ((TidFormatChannelOrder)PixelFormat & TidFormatChannelOrder.Argb);
            TidFormatCompressionFlag pixelCompression  = ((TidFormatCompressionFlag)PixelFormat & TidFormatCompressionFlag.Compressed);

            bool pixelUnknownBit0 = (((TidFormatUnknownBit0)PixelFormat & TidFormatUnknownBit0.Set) == TidFormatUnknownBit0.Set);
            bool pixelUnknownBit3 = (((TidFormatUnknownBit3)PixelFormat & TidFormatUnknownBit3.Set) == TidFormatUnknownBit3.Set);
            bool pixelUnknownBit4 = (((TidFormatUnknownBit4)PixelFormat & TidFormatUnknownBit4.Set) == TidFormatUnknownBit4.Set);   // TODO: vertical flip according to some docs, verify
            bool pixelUnknownBit5 = (((TidFormatUnknownBit5)PixelFormat & TidFormatUnknownBit5.Set) == TidFormatUnknownBit5.Set);
            bool pixelUnknownBit6 = (((TidFormatUnknownBit6)PixelFormat & TidFormatUnknownBit6.Set) == TidFormatUnknownBit6.Set);
            bool pixelUnknownBit7 = (((TidFormatUnknownBit7)PixelFormat & TidFormatUnknownBit7.Set) == TidFormatUnknownBit7.Set);

            PixelDataFormat pixelFormat, paletteFormat = PixelDataFormat.Undefined;

            if (pixelCompression == TidFormatCompressionFlag.Compressed)
            {
                switch (CompressionFourCC)
                {
                case "DXT1": pixelFormat = PixelDataFormat.FormatDXT1Rgba; break;

                case "DXT3": pixelFormat = PixelDataFormat.FormatDXT3; break;

                case "DXT5": pixelFormat = PixelDataFormat.FormatDXT5; break;

                default: throw new Exception(string.Format("Unimplemented TID compression format '{0}'", CompressionFourCC));
                }
            }
            else if (pixelCompression == TidFormatCompressionFlag.NotCompressed)
            {
                if (PaletteDataSize != 0)
                {
                    if (pixelChannelOrder == TidFormatChannelOrder.Rgba)
                    {
                        paletteFormat = PixelDataFormat.FormatRgba8888;
                    }
                    else if (pixelChannelOrder == TidFormatChannelOrder.Argb)
                    {
                        paletteFormat = PixelDataFormat.FormatArgb8888;
                    }
                    else
                    {
                        throw new Exception("Invalid TID channel order; should not be reached?!");
                    }

                    if (BitsPerPixel == 8)
                    {
                        pixelFormat = PixelDataFormat.FormatIndexed8;
                    }
                    else
                    {
                        throw new Exception("Invalid or unsupported TID bits per pixel in indexed mode");
                    }
                }
                else
                {
                    if (BitsPerPixel == 32)
                    {
                        if (pixelChannelOrder == TidFormatChannelOrder.Rgba)
                        {
                            pixelFormat = PixelDataFormat.FormatRgba8888;
                        }
                        else if (pixelChannelOrder == TidFormatChannelOrder.Argb)
                        {
                            pixelFormat = PixelDataFormat.FormatArgb8888;
                        }
                        else
                        {
                            throw new Exception("Invalid TID channel order; should not be reached?!");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid or unsupported TID bits per pixel in non-indexed mode");
                    }
                }
            }
            else
            {
                throw new Exception("Invalid TID compression flag; should not be reached?!");
            }

            // TODO: verify if [Compressed == Swizzled] is correct, or if swizzling depends on other factors

            if (pixelCompression == TidFormatCompressionFlag.Compressed)
            {
                pixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
            }

            imageBinary                    = new ImageBinary();
            imageBinary.Width              = (int)Width;
            imageBinary.Height             = (int)Height;
            imageBinary.InputPixelFormat   = pixelFormat;
            imageBinary.InputPaletteFormat = paletteFormat;
            imageBinary.InputEndianness    = Endian.BigEndian;

            imageBinary.AddInputPixels(PixelData);
            imageBinary.AddInputPalette(PaletteData);
        }
Beispiel #27
0
        private Bitmap CreateBitmap(int infoIdx, int forcePaletteIdx = -1)
        {
            SceGxtTextureInfo info = TextureInfos[infoIdx];

            ImageBinary imageBinary = new ImageBinary();

            imageBinary.Width            = info.GetWidth();
            imageBinary.Height           = info.GetHeight();
            imageBinary.InputPixelFormat = PSVita.GetPixelDataFormat(info.GetTextureFormat());
            imageBinary.InputEndianness  = Endian.LittleEndian;
            imageBinary.AddInputPixels(PixelData[infoIdx]);

            // TODO: verify all this crap, GXT conversion wrt image [dimension/format/type] is fragile as all hell

            SceGxmTextureBaseFormat textureBaseFormat = info.GetTextureBaseFormat();
            SceGxmTextureType       textureType       = info.GetTextureType();

            if (textureType == SceGxmTextureType.Linear &&
                textureBaseFormat != SceGxmTextureBaseFormat.UBC1 && textureBaseFormat != SceGxmTextureBaseFormat.UBC2 && textureBaseFormat != SceGxmTextureBaseFormat.UBC3 &&
                textureBaseFormat != SceGxmTextureBaseFormat.PVRT2BPP && textureBaseFormat != SceGxmTextureBaseFormat.PVRT4BPP &&
                textureBaseFormat != SceGxmTextureBaseFormat.PVRTII2BPP && textureBaseFormat != SceGxmTextureBaseFormat.PVRTII4BPP)
            {
                imageBinary.PhysicalWidth  = (int)(((info.DataSize / imageBinary.Height) * 8) / PSVita.GetBitsPerPixel(textureBaseFormat));
                imageBinary.PhysicalHeight = info.GetHeight();
            }
            else
            {
                imageBinary.PhysicalWidth  = info.GetWidthRounded();
                imageBinary.PhysicalHeight = info.GetHeightRounded();
            }

            if (textureBaseFormat != SceGxmTextureBaseFormat.PVRT2BPP && textureBaseFormat != SceGxmTextureBaseFormat.PVRT4BPP)
            {
                switch (textureType)
                {
                case SceGxmTextureType.Linear:
                    // Nothing to be done!
                    break;

                case SceGxmTextureType.Tiled:
                    // TODO: verify me!
                    imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingTiled3DS;
                    break;

                case SceGxmTextureType.Swizzled:
                case SceGxmTextureType.Cube:
                    // TODO: is cube really the same as swizzled? seems that way from CS' *env* files...
                    imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
                    break;

                case (SceGxmTextureType)0xA0000000:
                    // TODO: this is odd and needs investigation, found ex. in Odin Sphere, Puyo Puyo Tetris, ...
                    imageBinary.InputPixelFormat |= PixelDataFormat.PixelOrderingSwizzledVita;
                    break;
                }
            }

            if (textureBaseFormat == SceGxmTextureBaseFormat.P4 || textureBaseFormat == SceGxmTextureBaseFormat.P8)
            {
                imageBinary.InputPaletteFormat = PSVita.GetPaletteFormat(info.GetTextureFormat());

                if (textureBaseFormat == SceGxmTextureBaseFormat.P4)
                {
                    foreach (byte[] paletteData in P4Palettes)
                    {
                        imageBinary.AddInputPalette(paletteData);
                    }
                }
                else if (textureBaseFormat == SceGxmTextureBaseFormat.P8)
                {
                    foreach (byte[] paletteData in P8Palettes)
                    {
                        imageBinary.AddInputPalette(paletteData);
                    }
                }
            }

            return(imageBinary.GetBitmap(0, forcePaletteIdx != -1 ? forcePaletteIdx : info.PaletteIndex));
        }
Beispiel #28
0
        public static byte[] Decompress(EndianBinaryReader reader, int width, int height, PixelDataFormat inputFormat, long readLength)
        {
            byte[] outPixels = new byte[readLength * 8];

            PixelOrderingDelegate pixelOrderingFunc = ImageBinary.GetPixelOrderingFunction(inputFormat & PixelDataFormat.MaskPixelOrdering);

            DXTxRGTCBlockDecoderDelegate blockDecoder;
            DXTxRGTCBlockLayout          blockLayout;
            DXTxRGTCSignedness           signedness;

            PixelDataFormat specialFormat = (inputFormat & PixelDataFormat.MaskSpecial);

            switch (specialFormat)
            {
            case PixelDataFormat.SpecialFormatDXT1: blockDecoder = DecodeDXT1Block; blockLayout = DXTxRGTCBlockLayout.Normal; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatDXT3: blockDecoder = DecodeDXT3Block; blockLayout = DXTxRGTCBlockLayout.Normal; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatDXT5: blockDecoder = DecodeDXT5Block; blockLayout = DXTxRGTCBlockLayout.Normal; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatRGTC1: blockDecoder = DecodeRGTC1Block; blockLayout = DXTxRGTCBlockLayout.Normal; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatRGTC1_Signed: blockDecoder = DecodeRGTC1Block; blockLayout = DXTxRGTCBlockLayout.PSP; signedness = DXTxRGTCSignedness.Signed; break;

            case PixelDataFormat.SpecialFormatRGTC2: blockDecoder = DecodeRGTC2Block; blockLayout = DXTxRGTCBlockLayout.Normal; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatRGTC2_Signed: blockDecoder = DecodeRGTC2Block; blockLayout = DXTxRGTCBlockLayout.PSP; signedness = DXTxRGTCSignedness.Signed; break;

            case PixelDataFormat.SpecialFormatDXT1_PSP: blockDecoder = DecodeDXT1Block; blockLayout = DXTxRGTCBlockLayout.PSP; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatDXT3_PSP: blockDecoder = DecodeDXT3Block; blockLayout = DXTxRGTCBlockLayout.PSP; signedness = DXTxRGTCSignedness.Unsigned; break;

            case PixelDataFormat.SpecialFormatDXT5_PSP: blockDecoder = DecodeDXT5Block; blockLayout = DXTxRGTCBlockLayout.PSP; signedness = DXTxRGTCSignedness.Unsigned; break;

            default:
                throw new Exception("Trying to decode DXT/RGTC with format set to non-DXT/RGTC");
            }

            for (int y = 0; y < height; y += 4)
            {
                for (int x = 0; x < width; x += 4)
                {
                    byte[] decompressedBlock = blockDecoder(reader, inputFormat, blockLayout, signedness);

                    int rx, ry;
                    pixelOrderingFunc(x / 4, y / 4, width / 4, height / 4, inputFormat, out rx, out ry);
                    rx *= 4; ry *= 4;

                    for (int py = 0; py < 4; py++)
                    {
                        for (int px = 0; px < 4; px++)
                        {
                            int ix = (rx + px);
                            int iy = (ry + py);

                            if (ix >= width || iy >= height)
                            {
                                continue;
                            }

                            for (int c = 0; c < 4; c++)
                            {
                                outPixels[(((iy * width) + ix) * 4) + c] = decompressedBlock[(((py * 4) + px) * 4) + c];
                            }
                        }
                    }
                }
            }

            return(outPixels);
        }
Beispiel #29
0
        public static void Decode(this TEXFile file, Format format, ExtendedBinaryReader reader)
        {
            ImageBinary image;
            var         endian = file.UseBigEndian ? Endian.BigEndian : Endian.LittleEndian;

            if ((format & Format.DXT1) != 0)
            {
                image = new ImageBinary(file.SheetWidth, file.SheetHeight, PixelDataFormat.FormatDXT1Rgba,
                                        endian, PixelDataFormat.FormatAbgr8888, Endian.LittleEndian, file.SheetData);
                file.SheetData = image.GetOutputPixelData(0);
            }
            else if ((format & Format.DXT5) != 0 || (format & Format.Large) != 0)
            {
                image = new ImageBinary(file.SheetWidth, file.SheetHeight, PixelDataFormat.FormatDXT5,
                                        endian, PixelDataFormat.FormatAbgr8888, Endian.LittleEndian, file.SheetData);
                file.SheetData = image.GetOutputPixelData(0);
            }
            else if ((format & Format.Luminance8) != 0)
            {
                image = new ImageBinary(file.SheetWidth, file.SheetHeight, PixelDataFormat.FormatLuminance8,
                                        endian, PixelDataFormat.FormatAbgr8888, Endian.LittleEndian, file.SheetData);
                file.SheetData = image.GetOutputPixelData(0);
            }
            else if ((format & Format.Luminance4) != 0)
            {
                image = new ImageBinary(file.SheetWidth, file.SheetHeight, PixelDataFormat.FormatLuminance4,
                                        endian, PixelDataFormat.FormatAbgr8888, Endian.LittleEndian, file.SheetData);
                file.SheetData = image.GetOutputPixelData(0);
            }
            else if ((format & Format.Unknown) != 0)
            {
                throw new InvalidTextureFormatException((int)format);
            }
            else if ((format & Format.Raster) != 0)
            {
                // Read Colour Palette
                reader.JumpBehind(256 * 4);
                var colorpalette = reader.ReadBytes(256 * 4);

                var indies = file.SheetData;
                file.SheetData = new byte[file.SheetWidth * file.SheetHeight * 4];
                for (int i = 0; i < file.SheetWidth * file.SheetHeight; ++i)
                {
                    file.SheetData[i * 4 + 0] = colorpalette[indies[i] * 4 + 0];
                    file.SheetData[i * 4 + 1] = colorpalette[indies[i] * 4 + 1];
                    file.SheetData[i * 4 + 2] = colorpalette[indies[i] * 4 + 2];
                    file.SheetData[i * 4 + 3] = colorpalette[indies[i] * 4 + 3];
                }
            }
            else if ((format & Format.PNG) != 0)
            {
                using (var endStream = new MemoryStream())
                    using (var stream = new MemoryStream(file.SheetData))
                    {
                        var imagePNG = new Bitmap(Image.FromStream(stream));
                        var bitmap   = imagePNG.LockBits(new Rectangle(0, 0, file.SheetWidth, file.SheetHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        file.SheetData = new byte[file.SheetWidth * file.SheetHeight * 4];
                        Marshal.Copy(bitmap.Scan0, file.SheetData, 0, file.SheetWidth * file.SheetHeight * 4);
                        // Flip Red and Blue channels as this converter does not support ARGB
                        if (file.UseBigEndian)
                        {
                            ImageTools.FlipColors(file.SheetData);
                        }
                        imagePNG.UnlockBits(bitmap);
                        imagePNG.Dispose();
                    }
            }
            else if ((format & Format.BGRA) != 0)
            {
                return;
            }
            else
            {
                throw new InvalidTextureFormatException((int)format);
            }
        }
Beispiel #30
0
        protected override Bitmap OnGetBitmap(int imageIndex, int paletteIndex)
        {
            ImageBinary imageBinary = new ImageBinary();

            PixelDataFormat pixelDataFormat, paletteDataFormat;

            switch (ImageType)
            {
            // Non-indexed image
            case GbixImageType.Linear:
            case GbixImageType.LinearMipmaps:
                if (!dataTypeFormatMap.ContainsKey(DataType))
                {
                    throw new NotImplementedException(string.Format("GBIX: unimplemented pixel data type {0} (0x{1:X2})", DataType, (byte)DataType));
                }

                pixelDataFormat = dataTypeFormatMap[DataType];
                if (!isCompressed)
                {
                    pixelDataFormat |= PixelDataFormat.PixelOrderingSwizzledPSP;
                }
                paletteDataFormat = PixelDataFormat.Undefined;
                break;

            // Indexed image (4-bit)
            case GbixImageType.Indexed4:
            case GbixImageType.Indexed4_Alt:
                if (!dataTypeFormatMap.ContainsKey(DataType))
                {
                    throw new NotImplementedException(string.Format("GBIX: unimplemented palette data type {0} (0x{1:X2})", DataType, (byte)DataType));
                }

                pixelDataFormat   = PixelDataFormat.FormatIndexed4 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = dataTypeFormatMap[DataType];
                break;

            // Indexed image (8-bit)
            case GbixImageType.Indexed8:
            case GbixImageType.Indexed8_Alt:
                if (!dataTypeFormatMap.ContainsKey(DataType))
                {
                    throw new NotImplementedException(string.Format("GBIX: unimplemented palette data type {0} (0x{1:X2})", DataType, (byte)DataType));
                }

                pixelDataFormat   = PixelDataFormat.FormatIndexed8 | PixelDataFormat.PixelOrderingSwizzledPSP;
                paletteDataFormat = dataTypeFormatMap[DataType];
                break;

            default:
                throw new NotImplementedException(string.Format("GBIX: unimplemented image type {0} (0x{1:X2})", ImageType, (byte)ImageType));
            }

            imageBinary.Width              = mipmapData[imageIndex].Width;
            imageBinary.Height             = mipmapData[imageIndex].Height;
            imageBinary.InputPaletteFormat = paletteDataFormat;
            imageBinary.InputPixelFormat   = pixelDataFormat;
            imageBinary.InputEndianness    = Endian.LittleEndian;

            imageBinary.AddInputPalette(PaletteData);
            imageBinary.AddInputPixels(mipmapData[imageIndex].PixelData);

            return(imageBinary.GetBitmap(0, 0));
        }