public static Image ExtractImage(byte[] bytes) { int size = IOHelper.GetInt(bytes, 8); byte[] rawPixels = new byte[size]; Array.Copy(bytes, 12, rawPixels, 0, rawPixels.Length); DDSHeader ddsHdr = getDDSHeader(rawPixels); if (ddsHdr == null) { return(null); } fixDDSHeader(rawPixels, ddsHdr); int w = GetWidth(rawPixels); int h = GetHeight(rawPixels); if (w > 96 || w > 96) { return(null); } int[] decodedPixels = read(rawPixels, 0); if (decodedPixels == null) { return(null); } Bitmap bitmap1 = bitmapFromBytes(GetWidth(rawPixels), GetHeight(rawPixels), decodedPixels); return(bitmap1); }
/* * private static byte[] convert(int[] data) { * byte[] result = new byte[data.Length * 4]; * for (int i = 0; i < data.Length; i++) { * byte[] bytes = BitConverter.GetBytes(data[i]); * if (!BitConverter.IsLittleEndian) { * //Array.Reverse(bytes); * } * * result[i * 4 + 0] = (byte)bytes[0]; * result[i * 4 + 1] = (byte)bytes[1]; * result[i * 4 + 2] = (byte)bytes[2]; * result[i * 4 + 3] = (byte)bytes[3]; * } * * return result; * }*/ #region Fix private static DDSHeader GetDDSHeader(byte[] bytes) { var header = new DDSHeader(); var offset = 0; if (bytes.Length < 4) { return(null); } Array.Copy(bytes, offset, header.version, 0, 4); //header.version = GDReader.getBytes4(bytes, offset); header.size = IOHelper.GetInt(bytes, offset += 4); offset += 4; if (header.size != 124) { //logger.Warn("ERR_UNSUPPORTED_HD_SIZE"); return(null); } header.flags = IOHelper.GetInt(bytes, offset); header.height = IOHelper.GetInt(bytes, offset += 4); header.width = IOHelper.GetInt(bytes, offset += 4); header.linearSize = IOHelper.GetInt(bytes, offset += 4); header.depth = IOHelper.GetInt(bytes, offset += 4); header.num_mipmap = IOHelper.GetInt(bytes, offset += 4); offset += 4; for (var i = 0; i < header.reserved1.Length; ++i) { header.reserved1[i] = IOHelper.GetInt(bytes, offset); offset += 4; } header.pixelFormat.size = IOHelper.GetInt(bytes, offset); offset += 4; if (header.pixelFormat.size != 32) { throw new Exception("ERR_UNSUPPORTED_PIX_SIZE"); } header.pixelFormat.flags = IOHelper.GetInt(bytes, offset); header.pixelFormat.fourCC = IOHelper.GetInt(bytes, offset += 4); header.pixelFormat.rgbBitCount = IOHelper.GetInt(bytes, offset += 4); header.pixelFormat.rBitMask = IOHelper.GetInt(bytes, offset += 4); header.pixelFormat.gBitMask = IOHelper.GetInt(bytes, offset += 4); header.pixelFormat.bBitMask = IOHelper.GetInt(bytes, offset += 4); header.pixelFormat.aBitMask = IOHelper.GetInt(bytes, offset += 4); header.caps = IOHelper.GetInt(bytes, offset += 4); header.caps2 = IOHelper.GetInt(bytes, offset += 4); header.caps3 = IOHelper.GetInt(bytes, offset += 4); header.caps4 = IOHelper.GetInt(bytes, offset += 4); header.reserved2 = IOHelper.GetInt(bytes, offset + 4); return(header); }
private static void FixDDSHeader(byte[] bytes, DDSHeader header) { if (header.version[3] == 82) { bytes[3] = 32; } var flags = header.flags | 1 | 2 | 4 | 4096; var caps = header.caps | 4096; if (header.num_mipmap > 1) { header.flags |= 131072; header.caps = header.caps | 8 | 4194304; } if ((header.caps2 & 512) != 0) { caps |= 8; } if (header.depth > 1) { flags |= 8388608; caps |= 8; } if ((header.pixelFormat.flags & 4) != 0 && header.linearSize != 0) { flags |= 524288; } if (((header.pixelFormat.flags & 64) == 64 || (header.pixelFormat.flags & 512) == 512 || (header.pixelFormat.flags & 131072) == 131072 || (header.pixelFormat.flags & 2) == 2) && header.linearSize != 0) { flags |= 8; } //Array.Copy(flags, 0, bytes, 8, flags.) IOHelper.SetInt(bytes, 8, flags); IOHelper.SetInt(bytes, 80, header.pixelFormat.flags); IOHelper.SetInt(bytes, 92, 16711680); IOHelper.SetInt(bytes, 96, 65280); IOHelper.SetInt(bytes, 100, 255); IOHelper.SetInt(bytes, 104, 255); IOHelper.SetInt(bytes, 108, caps); }