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); }
public static byte[] Convert(Bitmap image, PixelDataFormat destinationFormat) { image.ThrowIfNull("image"); if (destinationFormat == PixelDataFormat.Link) { throw new ArgumentException("Code tried to convert an image to link format which doesn't make sense."); } int bytesPerPixel = FrameInfo.GetBytesPerPixel(destinationFormat); byte[] convertedPixelBytes = new byte[image.Width * image.Height * bytesPerPixel]; switch (destinationFormat) { case PixelDataFormat.OneFiveFiveFive: FillBufferOneFiveFiveFiveLe(image, convertedPixelBytes); break; case PixelDataFormat.FourFourFourFour: FillBufferFourFourFourFourLe(image, convertedPixelBytes); break; case PixelDataFormat.EightEightEightEight: FillBufferEightEightEightEightLe(image, convertedPixelBytes); break; default: throw new NotImplementedException("Oops, missed a pixel format to convert to: {0}".F(destinationFormat)); } return convertedPixelBytes; //BitmapData bits = inputImage.LockBits(new Rectangle(0, 0, inputImage.Width, inputImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555); }
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()); }
public static byte[] Convert(Bitmap image, PixelDataFormat destinationFormat) { image.ThrowIfNull("image"); if (destinationFormat == PixelDataFormat.Link) { throw new ArgumentException("Code tried to convert an image to link format which doesn't make sense."); } int bytesPerPixel = FrameInfo.GetBytesPerPixel(destinationFormat); byte[] convertedPixelBytes = new byte[image.Width * image.Height * bytesPerPixel]; switch (destinationFormat) { case PixelDataFormat.OneFiveFiveFive: FillBufferOneFiveFiveFiveLe(image, convertedPixelBytes); break; case PixelDataFormat.FourFourFourFour: FillBufferFourFourFourFourLe(image, convertedPixelBytes); break; case PixelDataFormat.EightEightEightEight: FillBufferEightEightEightEightLe(image, convertedPixelBytes); break; default: throw new NotImplementedException("Oops, missed a pixel format to convert to: {0}".F(destinationFormat)); } return(convertedPixelBytes); //BitmapData bits = inputImage.LockBits(new Rectangle(0, 0, inputImage.Width, inputImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555); }
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()); }
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); } }
private static byte[] DecodeDXT5Block(EndianBinaryReader reader, PixelDataFormat inputFormat, DXTxRGTCBlockLayout blockLayout, DXTxRGTCSignedness signedness) { DXT5Block inBlock = new DXT5Block(reader, blockLayout); byte[] outData = DecodeColorBlock(inBlock.Color, false, false); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { byte code = inBlock.Bits[(y * 4) + x]; int destOffset = (((y * 4) + x) * 4) + 3; if (inBlock.Alpha0 > inBlock.Alpha1) { switch (code) { case 0x00: outData[destOffset] = inBlock.Alpha0; break; case 0x01: outData[destOffset] = inBlock.Alpha1; break; case 0x02: outData[destOffset] = (byte)((6 * inBlock.Alpha0 + 1 * inBlock.Alpha1) / 7); break; case 0x03: outData[destOffset] = (byte)((5 * inBlock.Alpha0 + 2 * inBlock.Alpha1) / 7); break; case 0x04: outData[destOffset] = (byte)((4 * inBlock.Alpha0 + 3 * inBlock.Alpha1) / 7); break; case 0x05: outData[destOffset] = (byte)((3 * inBlock.Alpha0 + 4 * inBlock.Alpha1) / 7); break; case 0x06: outData[destOffset] = (byte)((2 * inBlock.Alpha0 + 5 * inBlock.Alpha1) / 7); break; case 0x07: outData[destOffset] = (byte)((1 * inBlock.Alpha0 + 6 * inBlock.Alpha1) / 7); break; } } else { switch (code) { case 0x00: outData[destOffset] = inBlock.Alpha0; break; case 0x01: outData[destOffset] = inBlock.Alpha1; break; case 0x02: outData[destOffset] = (byte)((4 * inBlock.Alpha0 + 1 * inBlock.Alpha1) / 5); break; case 0x03: outData[destOffset] = (byte)((3 * inBlock.Alpha0 + 2 * inBlock.Alpha1) / 5); break; case 0x04: outData[destOffset] = (byte)((2 * inBlock.Alpha0 + 3 * inBlock.Alpha1) / 5); break; case 0x05: outData[destOffset] = (byte)((1 * inBlock.Alpha0 + 4 * inBlock.Alpha1) / 5); break; case 0x06: outData[destOffset] = 0x00; break; case 0x07: outData[destOffset] = 0xFF; break; } } } } return(outData); }
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()); }
protected override void OnOpen(EndianBinaryReader reader) { MagicNumber = Encoding.ASCII.GetString(reader.ReadBytes(4)); Data = new uint[3]; for (int i = 0; i < Data.Length; i++) { Data[i] = reader.ReadUInt32(); } Constant = (Data[0] & 0x00000FFF); Unknown1 = ((Data[0] >> 12) & 0xFFF); Unknown2 = ((Data[0] >> 28) & 0xF); MipmapCount = (Data[1] & 0x3F); Width = ((Data[1] >> 6) & 0x1FFF); Height = ((Data[1] >> 19) & 0x1FFF); Format = ((Data[2] >> 8) & 0xFF); int pixelDataStart = 0x10; // TODO: remove this hacky crap ~(<_>)~ if (Unknown1 != 0x40) { pixelDataStart += (int)(Constant > 0xA4 ? (4 * MipmapCount) : 0); } else { if (MipmapCount == 2) { pixelDataStart += 0x6C; } else if (MipmapCount == 3) { pixelDataStart += 0xB4; } } reader.BaseStream.Seek(pixelDataStart, SeekOrigin.Begin); PixelData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); switch (Format) { case 0x03: pixelDataFormat = PixelDataFormat.FormatRgba8888 | PixelDataFormat.PixelOrderingTiled3DS; break; case 0x07: pixelDataFormat = PixelDataFormat.FormatLuminanceAlpha88 | PixelDataFormat.PixelOrderingTiled3DS; break; case 0x0B: pixelDataFormat = PixelDataFormat.FormatETC1_3DS; break; case 0x0C: pixelDataFormat = PixelDataFormat.FormatETC1A4_3DS; break; case 0x0E: pixelDataFormat = PixelDataFormat.FormatLuminance4 | PixelDataFormat.PixelOrderingTiled3DS; break; case 0x10: pixelDataFormat = PixelDataFormat.FormatLuminanceAlpha44 | PixelDataFormat.PixelOrderingTiled3DS; break; case 0x11: pixelDataFormat = PixelDataFormat.FormatRgb888 | PixelDataFormat.PixelOrderingTiled3DS; break; default: throw new NotImplementedException(string.Format("Capcom TEX format 0x{0:X}", Format)); } }
public DisplayMode(PixelDataFormat format, int width, int height, int refreshRate, IntPtr driverData) { this.Format = format; this.Width = width; this.Height = height; this.RefreshRate = refreshRate; this.DriverData = driverData; }
/// <summary> /// Creates a new PixelBufferDescriptor referencing an image in main memory. /// </summary> /// <param name="linearImage">LinearImage to use for data.</param> /// <param name="format">Format of the image pixels.</param> /// <param name="type">Type of the image pixels.</param> public PixelBufferDescriptor( LinearImage linearImage, PixelDataFormat format, PixelDataType type) { LinearImage = linearImage; Stride = 0; Format = format; Type = type; Alignment = 1; }
/// <summary> /// Creates a new PixelBufferDescriptor referencing an image in main memory. /// </summary> /// <param name="buffer">Buffer containing the image.</param> /// <param name="format">Format of the image pixels.</param> /// <param name="type">Type of the image pixels.</param> public PixelBufferDescriptor( byte[] buffer, PixelDataFormat format, PixelDataType type) { Buffer = buffer; Stride = 0; Format = format; Type = type; Alignment = 1; }
public void ReadPixels( int x, int y, int width, int height, PixelDataFormat format, uint[] pixels) { if (pixels == null || pixels.Length > HeapData.ReadPixelsuivMaxSize) { throw new ArgumentException("Array has to many elements."); } Marshal.Copy(pixels.Select(i => (int)i).ToArray(), 0, HeapData.ptrReadPixelsuiv, pixels.Length); ReadPixels(x, y, width, height, format, PixelDataType.UNSIGNED_INT, HeapData.ptrReadPixelsuiv); }
public void ReadPixels( int x, int y, int width, int height, PixelDataFormat format, float[] pixels) { if (pixels == null || pixels.Length > HeapData.ReadPixelsfvMaxSize) { throw new ArgumentException("Array has to many elements."); } Marshal.Copy(pixels, 0, HeapData.ptrReadPixelsfv, pixels.Length); ReadPixels(x, y, width, height, format, PixelDataType.FLOAT, HeapData.ptrReadPixelsfv); }
public void DrawPixels( int width, int height, PixelDataFormat format, sbyte[] pixels) { if (pixels == null || pixels.Length > HeapData.DrawPixelssbvMaxSize) { throw new ArgumentException("Array has to many elements."); } Marshal.Copy(pixels.Select(x => (byte)x).ToArray(), 0, HeapData.ptrDrawPixelssbv, pixels.Length); DrawPixels(width, height, format, PixelDataType.SIGNED_BYTE, HeapData.ptrDrawPixelssbv); }
public void DrawPixels( int width, int height, PixelDataFormat format, int[] pixels) { if (pixels == null || pixels.Length > HeapData.DrawPixelsivMaxSize) { throw new ArgumentException("Array has to many elements."); } Marshal.Copy(pixels, 0, HeapData.ptrDrawPixelsiv, pixels.Length); DrawPixels(width, height, format, PixelDataType.INT, HeapData.ptrDrawPixelsiv); }
public void DrawPixels( int width, int height, PixelDataFormat format, ushort[] pixels) { if (pixels == null || pixels.Length > HeapData.DrawPixelsusvMaxSize) { throw new ArgumentException("Array has to many elements."); } Marshal.Copy(pixels.Select(x => (short)x).ToArray(), 0, HeapData.ptrDrawPixelsusv, pixels.Length); DrawPixels(width, height, format, PixelDataType.UNSIGNED_SHORT, HeapData.ptrDrawPixelsusv); }
public FrameInfo(bool isCompressed, int compressedLength, PixelDataFormat pixelFormat, int width, int height, int locationX, int locationY, int maxWidth, int maxHeight) { IsCompressed = isCompressed; CompressedLength = compressedLength; PixelFormat = pixelFormat; Width = width; Height = height; LocationX = locationX; LocationY = locationY; MaxWidth = maxWidth; MaxHeight = maxHeight; }
public FrameInfo(bool isCompressed, int compressedLength, PixelDataFormat pixelFormat, int width, int height, int locationX, int locationY, int maxWidth, int maxHeight) { IsCompressed = isCompressed; CompressedLength = compressedLength; PixelFormat = pixelFormat; Width = width; Height = height; LocationX = locationX; LocationY = locationY; MaxWidth = maxWidth; MaxHeight = maxHeight; }
/// <summary> /// Creates a new PixelBufferDescriptor referencing an image in main memory. /// </summary> /// <param name="linearImage">LinearImage to use for data.</param> /// <param name="format">Format of the image pixels.</param> /// <param name="type">Type of the image pixels.</param> /// <param name="alignment">Alignment in bytes of pixel rows.</param> /// <param name="left">Left coordinate in pixels.</param> /// <param name="top">Top coordinate in pixels.</param> /// <param name="stride">Stride of a row in pixels.</param> public PixelBufferDescriptor( LinearImage linearImage, PixelDataFormat format, PixelDataType type, int alignment, int left, int top, int stride) { LinearImage = linearImage; Format = format; Type = type; Left = left; Top = top; Stride = stride; Alignment = alignment; }
/// <summary> /// Creates a new PixelBufferDescriptor referencing an image in main memory. /// </summary> /// <param name="buffer">Buffer containing the image.</param> /// <param name="format">Format of the image pixels.</param> /// <param name="type">Type of the image pixels.</param> /// <param name="alignment">Alignment in bytes of pixel rows.</param> /// <param name="left">Left coordinate in pixels.</param> /// <param name="top">Top coordinate in pixels.</param> /// <param name="stride">Stride of a row in pixels.</param> public PixelBufferDescriptor( byte[] buffer, PixelDataFormat format, PixelDataType type, int alignment, int left, int top, int stride) { Buffer = buffer; Format = format; Type = type; Left = left; Top = top; Stride = stride; Alignment = alignment; }
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); }
public void TexImage2D( GetTarget target, int level, PixelDataFormat internalformat, int width, int height, int border, PixelDataFormat format, byte[] pixels) { if (pixels == null || pixels.Length > HeapData.TexImage2DbvMaxSize) { throw new ArgumentException("Array has to many elements."); } //IntPtr ptr = Marshal.AllocHGlobal(sizeof(byte) * 2048); Marshal.Copy(pixels, 0, HeapData.ptrTexImage2Dbv, pixels.Length); texImage2D(target, level, internalformat, width, height, border, format, IndexesDataType.UNSIGNED_BYTE, HeapData.ptrTexImage2Dbv); }
private static byte[] DecodeDXT3Block(EndianBinaryReader reader, PixelDataFormat inputFormat, DXTxRGTCBlockLayout blockLayout, DXTxRGTCSignedness signedness) { DXT3Block inBlock = new DXT3Block(reader, blockLayout); byte[] outData = DecodeColorBlock(inBlock.Color, false, false); ulong alpha = inBlock.Alpha; for (int i = 0; i < outData.Length; i += 4) { outData[i + 3] = (byte)(((alpha & 0xF) << 4) | (alpha & 0xF)); alpha >>= 4; } return(outData); }
public static int GetBytesPerPixel(PixelDataFormat pixelFormat) { switch (pixelFormat) { case PixelDataFormat.EightEightEightEight: return(4); case PixelDataFormat.FourFourFourFour: return(2); case PixelDataFormat.OneFiveFiveFive: return(2); case PixelDataFormat.Link: return(0); default: throw new Exception("Invalid pixel format {0}.".F(pixelFormat)); } }
private static byte[] DecodeRGTC1Block(EndianBinaryReader reader, PixelDataFormat inputFormat, DXTxRGTCBlockLayout blockLayout, DXTxRGTCSignedness signedness) { RGTCBlock inBlock = new RGTCBlock(reader, blockLayout); byte[] outData = new byte[(4 * 4) * 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { int destOffset = (((y * 4) + x) * 4); outData[destOffset + 0] = DecodeRGTCValue(inBlock, x, y, signedness); outData[destOffset + 1] = 0x00; outData[destOffset + 2] = 0x00; outData[destOffset + 3] = 0xFF; } } return(outData); }
public g1tTexture(EndianBinaryReader br) { header = br.ReadBytes(8); mipmap = header[0]; format = header[1]; wh = header[2]; unk4 = header[4]; unk5 = header[5]; unk6 = header[6]; unk7 = header[7]; if (br.Endianness == Endian.LittleEndian) { mipmap = (int)IOMath.Swap4byte((byte)mipmap); format = (int)IOMath.Swap4byte((byte)format); wh = (int)IOMath.Swap4byte((byte)wh); unk4 = (int)IOMath.Swap4byte((byte)unk4); unk5 = (int)IOMath.Swap4byte((byte)unk5); unk6 = (int)IOMath.Swap4byte((byte)unk6); unk7 = (int)IOMath.Swap4byte((byte)unk7); } MipmapCount = (int)mipmap; switch (format) { case 0x21: // DXT5 swizzled from PSVITA 影牢(Deception) is 0x21 textureFormat = PixelDataFormat.FormatDXT5; BitPerPixel = 8; break; default: throw new Exception("unknown g1t texture format"); } Width = (1 << (wh >> 4)); Height = (1 << (wh & 0xf)); if (unk7 == 1) { br.ReadBytes(0xc); } position = (int)br.BaseStream.Position; Data = br.ReadBytes(BitPerPixel / 8 * Width * Height); }
/// <summary> /// /// </summary> /// <param name="imgPath"></param> /// <param name="frameIndex"></param> /// <param name="pngFilePath"></param> /// <exception cref="DFOToolbox.DFOToolboxException">Something went wrong while editing. Message is suitable for UI display.</exception> /// <exception cref="System.Exception">Other errors resulting from incorrect usage of this function, such as passing null arguments or trying to edit a frame while no file is open.</exception> public void EditFrame(string imgPath, int frameIndex, string pngFilePath) { // validate that we have a file open for editing, that it has the img, that it has the frame index, that the file exists imgPath.ThrowIfNull("imgPath"); pngFilePath.ThrowIfNull("pngFileName"); if (!_editor.IsOpen) { throw new InvalidOperationException("Cannot edit a frame because no file is currently open."); } NpkPath npkPath = imgPath; if (!_editor.Frames.ContainsKey(npkPath)) { throw new KeyNotFoundException("There is no img with path {0}.".F(imgPath)); } if (frameIndex >= _editor.Frames[npkPath].Count) { throw new ArgumentOutOfRangeException("{0} does not have a frame with index {1}.".F(imgPath, frameIndex)); } FrameInfo uneditedFrameMetadata = _editor.Frames[npkPath][frameIndex]; // Use same pixel format as original. // If it's a link frame, we're turning it into a non-link frame, so use 8888 as that will preserve the colors of the new image. PixelDataFormat pixelFormatToUse = uneditedFrameMetadata.PixelFormat; if (pixelFormatToUse == PixelDataFormat.Link) { pixelFormatToUse = PixelDataFormat.EightEightEightEight; } if (!File.Exists(pngFilePath)) { throw new DFOToolboxException("{0} does not exist.".F(pngFilePath)); } byte[] newImageBytesInCorrectFormat; int newImageWidth; int newImageHeight; // Load image pixels into memory // TODO: catch exceptions using (System.Drawing.Bitmap inputImage = new System.Drawing.Bitmap(pngFilePath)) { // need to get pixels into format used by image newImageBytesInCorrectFormat = PixelConversion.Convert(inputImage, pixelFormatToUse); newImageWidth = inputImage.Width; newImageHeight = inputImage.Height; } using (MemoryStream newImageBytesInCorrectFormatStream = new MemoryStream(newImageBytesInCorrectFormat)) { FrameInfo newFrameMetadata = new FrameInfo( isCompressed: uneditedFrameMetadata.IsCompressed, compressedLength: -1, // not used pixelFormat: pixelFormatToUse, width: newImageWidth, height: newImageHeight, locationX: uneditedFrameMetadata.LocationX, locationY: uneditedFrameMetadata.LocationY, maxWidth: uneditedFrameMetadata.MaxWidth, maxHeight: uneditedFrameMetadata.MaxHeight ); _editor.EditFrame(npkPath, frameIndex, newFrameMetadata, newImageBytesInCorrectFormatStream); } // XXX: This code assumes the imgPath passed in is currently selected RefreshFrameList(); FrameList.MoveCurrentToPosition(frameIndex); }
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); }
public static int GetBytesPerPixel(PixelDataFormat pixelFormat) { switch(pixelFormat) { case PixelDataFormat.EightEightEightEight: return 4; case PixelDataFormat.FourFourFourFour: return 2; case PixelDataFormat.OneFiveFiveFive: return 2; case PixelDataFormat.Link: return 0; default: throw new Exception("Invalid pixel format {0}.".F(pixelFormat)); } }
private static byte[] DecodeDXT1Block(EndianBinaryReader reader, PixelDataFormat inputFormat, DXTxRGTCBlockLayout blockLayout, DXTxRGTCSignedness signedness) { DXT1Block inBlock = new DXT1Block(reader, blockLayout); return(DecodeColorBlock(inBlock, (inputFormat & PixelDataFormat.MaskChannels) != PixelDataFormat.ChannelsRgb, true)); }
private static extern void DrawPixels( int width, int height, PixelDataFormat format, PixelDataType type, IntPtr pixels);
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); }