protected static Bitmap GetBitmapFromDDS_DXT3(SubStream data, int offset, int width, int height, bool IsDXT2, out bool hasalpha) { Bitmap bmp = new Bitmap(width, height, IsDXT2 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[width * height * 4]; hasalpha = false; for (int y = 0; y < height; y += 4) { for (int x = 0; x < width; x += 4) { ulong alpharaw = data.GetUInt64(offset); byte[,] alphadata = new byte[4, 4]; for (int i = 0; i < 16; i++) { alphadata[i % 4, i / 4] = (byte)(((alpharaw >> (i * 4)) & 0x0F) * 0x11); } Color[,] colourdata = GetDXT1ColourBlock(data, offset + 8, true); for (int v = 0; v < 4; v++) { for (int u = 0; u < 4; u++) { if (x + u < width && y + v < height) { int i = ((y + v) * width + (x + u)) * 4; bmpraw[i] = colourdata[u, v].B; bmpraw[i + 1] = colourdata[u, v].G; bmpraw[i + 2] = colourdata[u, v].R; bmpraw[i + 3] = alphadata[u, v]; hasalpha |= alphadata[u, v] != 255; } } } offset += 16; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, IsDXT2 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); for (int y = 0; y < height; y++) { Marshal.Copy(bmpraw, y * width * 4, bmpdata.Scan0 + y * bmpdata.Stride, width * 4); } bmp.UnlockBits(bmpdata); return(bmp); }
protected static Bitmap GetBitmapFromDDS_RAW(SubStream Data, int Offset, int Width, int Height, int Pitch, int RGBBitCount, uint RedMask, uint GreenMask, uint BlueMask, uint AlphaMask, out bool hasalpha, out bool isargb32) { uint RGBBitMask = (uint)((1L << RGBBitCount) - 1); int RedShift = MostSignificantBitPosition(RedMask) - 7; int GreenShift = MostSignificantBitPosition(GreenMask) - 7; int BlueShift = MostSignificantBitPosition(BlueMask) - 7; int AlphaShift = MostSignificantBitPosition(AlphaMask) - 7; int RedBits = RedMask == 0 ? 0 : MostSignificantBitPosition(RedMask) - LeastSignificantBitPosition(RedMask) + 1; int GreenBits = GreenMask == 0 ? 0 : MostSignificantBitPosition(GreenMask) - LeastSignificantBitPosition(GreenMask) + 1; int BlueBits = BlueMask == 0 ? 0 : MostSignificantBitPosition(BlueMask) - LeastSignificantBitPosition(BlueMask) + 1; int AlphaBits = AlphaMask == 0 ? 0 : MostSignificantBitPosition(AlphaMask) - LeastSignificantBitPosition(AlphaMask) + 1; hasalpha = false; isargb32 = RedBits <= 8 && GreenBits <= 8 && BlueBits <= 8 && AlphaBits <= 8; Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[Width * Height * 4]; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { int w = (4 - ((RGBBitCount + 7) / 8)); int bitpos = y * Pitch * 8 + x * RGBBitCount; int i = ((bitpos + RGBBitCount - 1) / 8 - 7); int b = (bitpos - i * 8); int o = (y * Width + x) * 4; uint v = (uint)((Data.GetUInt64(i + 128) >> b) & RGBBitMask); bmpraw[o + 0] = GetMaskShiftVal(v, BlueMask, BlueShift, 0x00); bmpraw[o + 1] = GetMaskShiftVal(v, GreenMask, GreenShift, 0x00); bmpraw[o + 2] = GetMaskShiftVal(v, RedMask, RedShift, 0x00); bmpraw[o + 3] = GetMaskShiftVal(v, AlphaMask, AlphaShift, 0xFF); hasalpha = bmpraw[o + 3] != 255; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); for (int y = 0; y < Height; y++) { Marshal.Copy(bmpraw, y * Width * 4, bmpdata.Scan0 + y * bmpdata.Stride, Width * 4); } bmp.UnlockBits(bmpdata); return(bmp); }
protected static Bitmap GetBitmapFromDDS_RAW(SubStream Data, int Offset, int Width, int Height, int Pitch, int RGBBitCount, uint RedMask, uint GreenMask, uint BlueMask, uint AlphaMask, out bool hasalpha, out bool isargb32) { uint RGBBitMask = (uint)((1L << RGBBitCount) - 1); int RedShift = MostSignificantBitPosition(RedMask) - 7; int GreenShift = MostSignificantBitPosition(GreenMask) - 7; int BlueShift = MostSignificantBitPosition(BlueMask) - 7; int AlphaShift = MostSignificantBitPosition(AlphaMask) - 7; int RedBits = RedMask == 0 ? 0 : MostSignificantBitPosition(RedMask) - LeastSignificantBitPosition(RedMask) + 1; int GreenBits = GreenMask == 0 ? 0 : MostSignificantBitPosition(GreenMask) - LeastSignificantBitPosition(GreenMask) + 1; int BlueBits = BlueMask == 0 ? 0 : MostSignificantBitPosition(BlueMask) - LeastSignificantBitPosition(BlueMask) + 1; int AlphaBits = AlphaMask == 0 ? 0 : MostSignificantBitPosition(AlphaMask) - LeastSignificantBitPosition(AlphaMask) + 1; hasalpha = false; isargb32 = RedBits <= 8 && GreenBits <= 8 && BlueBits <= 8 && AlphaBits <= 8; Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[Width * Height * 4]; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { int w = (4 - ((RGBBitCount + 7) / 8)); int bitpos = y * Pitch * 8 + x * RGBBitCount; int i = ((bitpos + RGBBitCount - 1) / 8 - 7); int b = (bitpos - i * 8); int o = (y * Width + x) * 4; uint v = (uint)((Data.GetUInt64(i + 128) >> b) & RGBBitMask); bmpraw[o + 0] = GetMaskShiftVal(v, BlueMask, BlueShift, 0x00); bmpraw[o + 1] = GetMaskShiftVal(v, GreenMask, GreenShift, 0x00); bmpraw[o + 2] = GetMaskShiftVal(v, RedMask, RedShift, 0x00); bmpraw[o + 3] = GetMaskShiftVal(v, AlphaMask, AlphaShift, 0xFF); hasalpha = bmpraw[o + 3] != 255; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); for (int y = 0; y < Height; y++) { Marshal.Copy(bmpraw, y * Width * 4, bmpdata.Scan0 + y * bmpdata.Stride, Width * 4); } bmp.UnlockBits(bmpdata); return bmp; }
protected static Bitmap GetBitmapFromDDS_DXT5(SubStream data, int offset, int width, int height, bool IsDXT4, out bool hasalpha) { Bitmap bmp = new Bitmap(width, height, IsDXT4 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[width * height * 4]; hasalpha = false; for (int y = 0; y < height; y += 4) { for (int x = 0; x < width; x += 4) { byte[] a = new byte[8]; a[0] = data.GetByte(offset); a[1] = data.GetByte(offset + 1); if (a[0] > a[1]) { a[2] = (byte)(((int)a[0] * 6 + a[1] * 1 + 3) / 7); a[3] = (byte)(((int)a[0] * 5 + a[1] * 2 + 3) / 7); a[4] = (byte)(((int)a[0] * 4 + a[1] * 3 + 3) / 7); a[5] = (byte)(((int)a[0] * 3 + a[1] * 4 + 3) / 7); a[6] = (byte)(((int)a[0] * 2 + a[1] * 5 + 3) / 7); a[7] = (byte)(((int)a[0] * 1 + a[1] * 6 + 3) / 7); } else { a[2] = (byte)(((int)a[0] * 4 + a[1] * 1 + 2) / 5); a[3] = (byte)(((int)a[0] * 3 + a[1] * 2 + 2) / 5); a[4] = (byte)(((int)a[0] * 2 + a[1] * 3 + 2) / 5); a[5] = (byte)(((int)a[0] * 1 + a[1] * 4 + 2) / 5); a[6] = 0x00; a[7] = 0xFF; } ulong alphasel = data.GetUInt64(offset) >> 16; byte[,] alphadata = new byte[4, 4]; for (int i = 0; i < 16; i++) { alphadata[i % 4, i / 4] = a[(alphasel >> (i * 3)) & 7]; } Color[,] colourdata = GetDXT1ColourBlock(data, offset + 8, true); for (int v = 0; v < 4; v++) { for (int u = 0; u < 4; u++) { if (x + u < width && y + v < height) { int i = ((y + v) * width + (x + u)) * 4; bmpraw[i] = colourdata[u, v].B; bmpraw[i + 1] = colourdata[u, v].G; bmpraw[i + 2] = colourdata[u, v].R; bmpraw[i + 3] = alphadata[u, v]; hasalpha |= alphadata[u, v] != 255; } } } offset += 16; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, IsDXT4 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); for (int y = 0; y < height; y++) { Marshal.Copy(bmpraw, y * width * 4, bmpdata.Scan0 + y * bmpdata.Stride, width * 4); } bmp.UnlockBits(bmpdata); return bmp; }
protected static Bitmap GetBitmapFromDDS_DXT3(SubStream data, int offset, int width, int height, bool IsDXT2, out bool hasalpha) { Bitmap bmp = new Bitmap(width, height, IsDXT2 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[width * height * 4]; hasalpha = false; for (int y = 0; y < height; y += 4) { for (int x = 0; x < width; x += 4) { ulong alpharaw = data.GetUInt64(offset); byte[,] alphadata = new byte[4, 4]; for (int i = 0; i < 16; i++) { alphadata[i % 4, i / 4] = (byte)(((alpharaw >> (i * 4)) & 0x0F) * 0x11); } Color[,] colourdata = GetDXT1ColourBlock(data, offset + 8, true); for (int v = 0; v < 4; v++) { for (int u = 0; u < 4; u++) { if (x + u < width && y + v < height) { int i = ((y + v) * width + (x + u)) * 4; bmpraw[i] = colourdata[u, v].B; bmpraw[i + 1] = colourdata[u, v].G; bmpraw[i + 2] = colourdata[u, v].R; bmpraw[i + 3] = alphadata[u, v]; hasalpha |= alphadata[u, v] != 255; } } } offset += 16; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, IsDXT2 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); for (int y = 0; y < height; y++) { Marshal.Copy(bmpraw, y * width * 4, bmpdata.Scan0 + y * bmpdata.Stride, width * 4); } bmp.UnlockBits(bmpdata); return bmp; }
protected static Bitmap GetBitmapFromDDS_DXT5(SubStream data, int offset, int width, int height, bool IsDXT4, out bool hasalpha) { Bitmap bmp = new Bitmap(width, height, IsDXT4 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); byte[] bmpraw = new byte[width * height * 4]; hasalpha = false; for (int y = 0; y < height; y += 4) { for (int x = 0; x < width; x += 4) { byte[] a = new byte[8]; a[0] = data.GetByte(offset); a[1] = data.GetByte(offset + 1); if (a[0] > a[1]) { a[2] = (byte)(((int)a[0] * 6 + a[1] * 1 + 3) / 7); a[3] = (byte)(((int)a[0] * 5 + a[1] * 2 + 3) / 7); a[4] = (byte)(((int)a[0] * 4 + a[1] * 3 + 3) / 7); a[5] = (byte)(((int)a[0] * 3 + a[1] * 4 + 3) / 7); a[6] = (byte)(((int)a[0] * 2 + a[1] * 5 + 3) / 7); a[7] = (byte)(((int)a[0] * 1 + a[1] * 6 + 3) / 7); } else { a[2] = (byte)(((int)a[0] * 4 + a[1] * 1 + 2) / 5); a[3] = (byte)(((int)a[0] * 3 + a[1] * 2 + 2) / 5); a[4] = (byte)(((int)a[0] * 2 + a[1] * 3 + 2) / 5); a[5] = (byte)(((int)a[0] * 1 + a[1] * 4 + 2) / 5); a[6] = 0x00; a[7] = 0xFF; } ulong alphasel = data.GetUInt64(offset) >> 16; byte[,] alphadata = new byte[4, 4]; for (int i = 0; i < 16; i++) { alphadata[i % 4, i / 4] = a[(alphasel >> (i * 3)) & 7]; } Color[,] colourdata = GetDXT1ColourBlock(data, offset + 8, true); for (int v = 0; v < 4; v++) { for (int u = 0; u < 4; u++) { if (x + u < width && y + v < height) { int i = ((y + v) * width + (x + u)) * 4; bmpraw[i] = colourdata[u, v].B; bmpraw[i + 1] = colourdata[u, v].G; bmpraw[i + 2] = colourdata[u, v].R; bmpraw[i + 3] = alphadata[u, v]; hasalpha |= alphadata[u, v] != 255; } } } offset += 16; } } BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, IsDXT4 ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb); for (int y = 0; y < height; y++) { Marshal.Copy(bmpraw, y * width * 4, bmpdata.Scan0 + y * bmpdata.Stride, width * 4); } bmp.UnlockBits(bmpdata); return(bmp); }