public DdsTexture(Image image, int mipMapCount, ImgPixelFormat pixelFormat, bool unkBool1) { MipMapCount = mipMapCount; PixelFormat = pixelFormat; Size = image.Size; ChangeImageProbably(image, mipMapCount, pixelFormat, unkBool1); }
public void ChangeImageProbably(Image img, int mipMaps, ImgPixelFormat imgpixelFormat1, bool bool0) { var memoryStream = new MemoryStream(); var binaryWriter = new BinaryWriter(memoryStream); mipMaps = ClampMipMaps(MipMapCount, img.Width, img.Height); //Generate mipmaps for (var i = 0; i < mipMaps; i++) { var bitmap = KeyGenerator.ScaleImageFixedRatio(img, Math.Max(1, img.Width >> i), Math.Max(1, img.Height >> i)); if (imgpixelFormat1 == ImgPixelFormat.Bgra32) { var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); var array = new byte[bitmapData.Stride * bitmap.Height]; Marshal.Copy(bitmapData.Scan0, array, 0, array.Length); bitmap.UnlockBits(bitmapData); binaryWriter.Write(array); } else { ZzTextureClass.smethod_16(bitmap, binaryWriter, imgpixelFormat1, bool0); } } MipMapCount = mipMaps; PixelFormat = imgpixelFormat1; Data = memoryStream.ToArray(); }
public void ReplaceTexture(int index, Image img, ImgPixelFormat format) { var texMetaData = TextureList[index]; texMetaData.Height = (short)img.Height; texMetaData.Width = (short)img.Width; texMetaData.Data = new DdsTexture(img, texMetaData.MipMapCount, format).ToByteArray(); }
private void ImgList_SelectedIndexChanged(object sender, EventArgs e) { var index = _imgList.SelectedIndex; if (index >= 0) { var texture = _currentTexFile[index]; _currentTexturePixelFormat = texture.PixelFormat; _bppTxt.Text = string.Concat(texture.Bpp); switch (texture.PixelFormat) { case ImgPixelFormat.Dxt1: _formatTxt.Text = "DXT1"; break; case ImgPixelFormat.Dxt3: _formatTxt.Text = "DXT3"; break; case ImgPixelFormat.Dxt5: _formatTxt.Text = "DXT5"; break; default: _formatTxt.Text = "A8R8G8B8"; break; } _mipMapTxt.Text = string.Concat(texture.MipMapCount); _widthTxt.Text = string.Concat(texture.Size.Width); _heightTxt.Text = string.Concat(texture.Size.Height); var image = texture.GetImage(); _size0 = image.Size; if (image.Width > _imagePreviewBox.Width || image.Height > _imagePreviewBox.Height) { image = KeyGenerator.ScaleImageFixedRatio(image, _imagePreviewBox.Size); } _imagePreviewBox.Image = image; _imageInfoBox.Enabled = true; var metadata = _currentTexFile.TextureList[index]; CurrentImgFile = metadata; } else { _imageInfoBox.Enabled = false; } }
public static void smethod_16(Bitmap bitmap0, BinaryWriter binaryWriter0, ImgPixelFormat imgpixelFormat0, bool bool1) { var height = bitmap0.Height; var width = bitmap0.Width; if (!_bool0) { smethod_0(); } for (var i = 0; i < height; i += 4) { for (var j = 0; j < width; j += 4) { var array = new Color[16]; var num = 0; for (var k = 0; k < 4; k++) { for (var l = 0; l < 4; l++) { if (j + l < width && i + k < height) { array[num++] = bitmap0.GetPixel(j + l, i + k); } } } if (imgpixelFormat0 == ImgPixelFormat.Dxt3) { binaryWriter0.Write(smethod_14(array)); } else if (imgpixelFormat0 == ImgPixelFormat.Dxt5) { binaryWriter0.Write(smethod_15(array)); } smethod_12(array, bool1).method_0(binaryWriter0); } } }
private void Load(Stream stream, bool leaveOpen) { var binaryReader = new BinaryReader(stream); if (new string(binaryReader.ReadChars(4)) != "DDS ") { throw new FileLoadException("Invalid DDS file"); } DdsHeader header; header.size = binaryReader.ReadUInt32(); header.Flags = binaryReader.ReadUInt32(); header.Height = binaryReader.ReadUInt32(); header.Width = binaryReader.ReadUInt32(); header.PitchOrLinearSize = binaryReader.ReadUInt32(); header.Depth = binaryReader.ReadUInt32(); header.mipMapCount = binaryReader.ReadUInt32(); binaryReader.ReadBytes(44); header.pixelFormat.size = binaryReader.ReadUInt32(); header.pixelFormat.Flags = binaryReader.ReadUInt32(); header.pixelFormat.FourCc = binaryReader.ReadUInt32(); header.pixelFormat.RgbBitCount = binaryReader.ReadUInt32(); header.pixelFormat.RedBitMask = binaryReader.ReadUInt32(); header.pixelFormat.GreenBitMask = binaryReader.ReadUInt32(); header.pixelFormat.BlueBitMask = binaryReader.ReadUInt32(); header.pixelFormat.AlphaBitMask = binaryReader.ReadUInt32(); header.Unk1 = binaryReader.ReadUInt32(); header.Unk2 = binaryReader.ReadUInt32(); binaryReader.ReadBytes(12); if (header.size == 124u) { if (header.pixelFormat.size == 32u) { if ((header.Flags & 131072u) != 0u) { MipMapCount = (int)header.mipMapCount; } else { MipMapCount = 1; } Size = new Size((int)header.Width, (int)header.Height); if ((header.pixelFormat.Flags & 4u) != 0u) { var uint_ = header.pixelFormat.FourCc; if (uint_ > 116u) { if (uint_ <= 844388420u) { if (uint_ == 827611204u) { PixelFormat = ImgPixelFormat.Dxt1; Bpp = 8; goto IL_3BE; } if (uint_ != 844388420u) { goto IL_2D5; } } else if (uint_ != 861165636u) { if (uint_ != 877942852u && uint_ != 894720068u) { goto IL_2D5; } PixelFormat = ImgPixelFormat.Dxt5; Bpp = 16; goto IL_3BE; } PixelFormat = ImgPixelFormat.Dxt3; Bpp = 16; goto IL_3BE; } if (uint_ <= 28u) { switch (uint_) { case 20u: PixelFormat = ImgPixelFormat.Bgr24; Bpp = 3; goto IL_3BE; case 21u: PixelFormat = ImgPixelFormat.Bgra32; Bpp = 4; goto IL_3BE; case 22u: break; case 23u: PixelFormat = ImgPixelFormat.Bgr16; Bpp = 2; goto IL_3BE; default: if (uint_ == 28u) { PixelFormat = ImgPixelFormat.Alpha8; Bpp = 1; goto IL_3BE; } break; } } else { if (uint_ == 50u) { PixelFormat = ImgPixelFormat.Luminance8; Bpp = 1; goto IL_3BE; } if (uint_ == 113u) { PixelFormat = ImgPixelFormat.Rgba64Float; Bpp = 8; goto IL_3BE; } if (uint_ == 116u) { PixelFormat = ImgPixelFormat.Rgba128Float; Bpp = 16; goto IL_3BE; } } IL_2D5: PixelFormat = ImgPixelFormat.Bgra32; Bpp = 4; } else { if (header.pixelFormat.RgbBitCount == 8u && header.pixelFormat.AlphaBitMask == 255u) { PixelFormat = ImgPixelFormat.Alpha8; Bpp = 1; } if (header.pixelFormat.RgbBitCount == 32u && header.pixelFormat.RedBitMask == 16711680u && header.pixelFormat.GreenBitMask == 65280u && header.pixelFormat.BlueBitMask == 255u && header.pixelFormat.AlphaBitMask == 4278190080u) { PixelFormat = ImgPixelFormat.Bgra32; Bpp = 4; } if (Bpp == 0) { throw new FileLoadException("Invalid Texture Format"); } } IL_3BE: var array = new byte[header.Width * header.Height * (uint)Bpp]; stream.Read(array, 0, array.Length); Data = array; if (leaveOpen) { return; } stream.Close(); stream.Dispose(); return; } } throw new FileLoadException("Unable to read DDS file: wrong format"); }
public DdsTexture(Image image0, int int2, ImgPixelFormat imgpixelFormat1) : this(image0, int2, imgpixelFormat1, true) { }
public static void smethod_17(BinaryReader binaryReader0, ImageRelatedClass class2190, ImgPixelFormat imgpixelFormat0) { var array = new Tom[16]; var num = class2190.method_1(); var num2 = class2190.method_0(); for (var i = 0; i < num; i += 4) { for (var j = 0; j < num2; j += 4) { if (imgpixelFormat0 == ImgPixelFormat.Dxt3) { var array2 = new ushort[4]; for (var k = 0; k < 4; k++) { for (var l = 0; l < 4; l++) { if (l == 0) { array2[k] = binaryReader0.ReadUInt16(); } array[k * 4 + l].Float0 = (array2[k] & 15) / 15f; var expr74Cp0 = array2; var expr74Cp1 = k; expr74Cp0[expr74Cp1] = (ushort)(expr74Cp0[expr74Cp1] >> 4); } } } else if (imgpixelFormat0 == ImgPixelFormat.Dxt5) { var array3 = binaryReader0.ReadBytes(2); var array4 = binaryReader0.ReadBytes(6); var array5 = new float[8]; array5[0] = array3[0] / 255f; array5[1] = array3[1] / 255f; var num3 = 4; if (array3[0] > array3[1]) { num3 = 6; } else { array5[6] = 0f; array5[7] = 1f; } var num4 = 1f / (num3 + 1); for (var m = 0; m < num3; m++) { var num5 = (num3 - m) * num4; var num6 = (m + 1) * num4; array5[m + 2] = num5 * array3[0] + num6 * array3[1]; } for (var n = 0; n < 16; n++) { //Console.WriteLine("Broken"); var div = (n * 3) / 8; var rem = (n * 3) % 8; var b = (byte)((array4[div] >> rem) & 7); if (rem > 5) { var b2 = (byte)((array4[div + 1] << (8 - rem)) & 0xFF); b |= (byte)(b2 & 7); } array[n].Float0 = array5[b]; } } var @struct = Jerry.smethod_0(binaryReader0); var array6 = new Tom[4]; array6[0].Float1 = ((@struct.Ushort0 & 63488) >> 11) / 31f; array6[0].Float2 = ((@struct.Ushort0 & 2016) >> 5) / 63f; array6[0].Float3 = (@struct.Ushort0 & 31) / 31f; array6[1].Float1 = ((@struct.Ushort1 & 63488) >> 11) / 31f; array6[1].Float2 = ((@struct.Ushort1 & 2016) >> 5) / 63f; array6[1].Float3 = (@struct.Ushort1 & 31) / 31f; if (imgpixelFormat0 == ImgPixelFormat.Dxt1 && @struct.Ushort0 <= @struct.Ushort1) { array6[2] = Tom.smethod_1(Tom.smethod_2(array6[0], array6[1]), 0.5f); array6[3] = default(Tom); } else { array6[2] = Tom.smethod_1(Tom.smethod_2(Tom.smethod_0(2f, array6[0]), array6[1]), 0.333333343f); array6[3] = Tom.smethod_1(Tom.smethod_2(array6[0], Tom.smethod_0(2f, array6[1])), 0.333333343f); } for (var num9 = 0; num9 < 4; num9++) { for (var num10 = 0; num10 < 4; num10++) { if (j + num10 < num2 && i + num9 < num) { var point = new Point(j + num10, i + num9); var num11 = @struct.Uint0 & 3u; if (imgpixelFormat0 == ImgPixelFormat.Dxt1) { class2190.method_6(point, Color.FromArgb((byte)(array6[(int)((UIntPtr)num11)].Float0 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float1 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float2 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float3 * 255f))); } else { class2190.method_6(point, Color.FromArgb((byte)(array[num9 * 4 + num10].Float0 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float1 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float2 * 255f), (byte)(array6[(int)((UIntPtr)num11)].Float3 * 255f))); } } @struct.Uint0 >>= 2; } } } } }