public static System.Drawing.Size GetTGASize(string filename) { System.IO.FileStream f = System.IO.File.OpenRead(filename); System.IO.BinaryReader br = new System.IO.BinaryReader(f); tgaHeader header = new tgaHeader(); header.Read(br); br.Close(); return new System.Drawing.Size(header.ImageSpec.Width, header.ImageSpec.Height); }
public static System.Drawing.Size GetTGASize(string filename) { System.IO.FileStream f = System.IO.File.OpenRead(filename); System.IO.BinaryReader br = new System.IO.BinaryReader(f); tgaHeader header = new tgaHeader(); header.Read(br); br.Close(); return(new System.Drawing.Size(header.ImageSpec.Width, header.ImageSpec.Height)); }
public static unsafe ManagedImage LoadTGAImage(System.IO.Stream source, bool mask) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.AlphaBits > 8) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) throw new ArgumentException("Image too large."); byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4]; System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData(); fixed (byte* pdecoded = &decoded[0]) { bd.Width = header.ImageSpec.Width; bd.Height = header.ImageSpec.Height; bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; bd.Stride = header.ImageSpec.Width * 4; bd.Scan0 = (IntPtr)pdecoded; switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) decodeSpecial16(bd, header, br); else decodeStandard16(bd, header, br); break; case 24: if (header.ImageSpec.AlphaBits > 0) decodeSpecial24(bd, header, br); else decodeStandard24(bd, header, br); break; case 32: decodeStandard32(bd, header, br); break; default: return null; } } int n = header.ImageSpec.Width * header.ImageSpec.Height; ManagedImage image; if (mask && header.ImageSpec.AlphaBits == 0 && header.ImageSpec.PixelDepth == 8) { image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height, ManagedImage.ImageChannels.Alpha); int p = 3; for (int i = 0; i < n; i++) { image.Alpha[i] = decoded[p]; p += 4; } } else { image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height, ManagedImage.ImageChannels.Color | ManagedImage.ImageChannels.Alpha); int p = 0; for (int i = 0; i < n; i++) { image.Blue[i] = decoded[p++]; image.Green[i] = decoded[p++]; image.Red[i] = decoded[p++]; image.Alpha[i] = decoded[p++]; } } br.Close(); return image; }
public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.AlphaBits > 8) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) throw new ArgumentException("Image too large."); System.Drawing.Bitmap b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height); System.Drawing.Imaging.BitmapData bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) decodeSpecial16(bd, header, br); else decodeStandard16(bd, header, br); break; case 24: if (header.ImageSpec.AlphaBits > 0) decodeSpecial24(bd, header, br); else decodeStandard24(bd, header, br); break; case 32: decodeStandard32(bd, header, br); break; default: b.UnlockBits(bd); b.Dispose(); return null; } b.UnlockBits(bd); br.Close(); return b; }
public static unsafe byte[] LoadTGARaw(System.IO.Stream source) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.AlphaBits > 8) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) throw new ArgumentException("Image too large."); byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4]; System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData(); fixed (byte* pdecoded = &decoded[0]) { bd.Width = header.ImageSpec.Width; bd.Height = header.ImageSpec.Height; bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; bd.Stride = header.ImageSpec.Width * 4; bd.Scan0 = (IntPtr)pdecoded; switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) decodeSpecial16(bd, header, br); else decodeStandard16(bd, header, br); break; case 24: if (header.ImageSpec.AlphaBits > 0) decodeSpecial24(bd, header, br); else decodeStandard24(bd, header, br); break; case 32: decodeStandard32(bd, header, br); break; default: return null; } } // swap red and blue channels (TGA is BGRA) byte tmp; for (int i = 0; i < decoded.Length; i += 4) { tmp = decoded[i]; decoded[i] = decoded[i + 2]; decoded[i + 2] = tmp; } br.Close(); return decoded; }
public static unsafe ManagedImage LoadTGAImage(System.IO.Stream source, bool mask) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.AlphaBits > 8) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) { throw new ArgumentException("Image too large."); } byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4]; System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData(); fixed(byte *pdecoded = &decoded[0]) { bd.Width = header.ImageSpec.Width; bd.Height = header.ImageSpec.Height; bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; bd.Stride = header.ImageSpec.Width * 4; bd.Scan0 = (IntPtr)pdecoded; switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial16(bd, header, br); } else { decodeStandard16(bd, header, br); } break; case 24: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial24(bd, header, br); } else { decodeStandard24(bd, header, br); } break; case 32: decodeStandard32(bd, header, br); break; default: return(null); } } int n = header.ImageSpec.Width * header.ImageSpec.Height; ManagedImage image; if (mask && header.ImageSpec.AlphaBits == 0 && header.ImageSpec.PixelDepth == 8) { image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height, ManagedImage.ImageChannels.Alpha); int p = 3; for (int i = 0; i < n; i++) { image.Alpha[i] = decoded[p]; p += 4; } } else { image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height, ManagedImage.ImageChannels.Color | ManagedImage.ImageChannels.Alpha); int p = 0; for (int i = 0; i < n; i++) { image.Blue[i] = decoded[p++]; image.Green[i] = decoded[p++]; image.Red[i] = decoded[p++]; image.Alpha[i] = decoded[p++]; } } br.Close(); return(image); }
public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.AlphaBits > 8) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) { throw new ArgumentException("Image too large."); } System.Drawing.Bitmap b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height); System.Drawing.Imaging.BitmapData bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial16(bd, header, br); } else { decodeStandard16(bd, header, br); } break; case 24: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial24(bd, header, br); } else { decodeStandard24(bd, header, br); } break; case 32: decodeStandard32(bd, header, br); break; default: b.UnlockBits(bd); b.Dispose(); return(null); } b.UnlockBits(bd); br.Close(); return(b); }
public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); using (System.IO.BinaryReader br = new System.IO.BinaryReader(ms)) { tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.AlphaBits > 8) { throw new ArgumentException("Not a supported tga file."); } if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) { throw new ArgumentException("Image too large."); } System.Drawing.Bitmap b; System.Drawing.Imaging.BitmapData bd; // Create a bitmap for the image. // Only include an alpha layer when the image requires one. if (header.ImageSpec.AlphaBits > 0 || header.ImageSpec.PixelDepth == 8 || // Assume 8 bit images are alpha only header.ImageSpec.PixelDepth == 32) // Assume 32 bit images are ARGB { // Image needs an alpha layer b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); } else { // Image does not need an alpha layer, so do not include one. b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); } switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial16(bd, header, br); } else { decodeStandard16(bd, header, br); } break; case 24: if (header.ImageSpec.AlphaBits > 0) { decodeSpecial24(bd, header, br); } else { decodeStandard24(bd, header, br); } break; case 32: decodeStandard32(bd, header, br); break; default: b.UnlockBits(bd); b.Dispose(); return(null); } b.UnlockBits(bd); return(b); } }
public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source) { byte[] buffer = new byte[source.Length]; source.Read(buffer, 0, buffer.Length); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); using (System.IO.BinaryReader br = new System.IO.BinaryReader(ms)) { tgaHeader header = new tgaHeader(); header.Read(br); if (header.ImageSpec.PixelDepth != 8 && header.ImageSpec.PixelDepth != 16 && header.ImageSpec.PixelDepth != 24 && header.ImageSpec.PixelDepth != 32) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.AlphaBits > 8) throw new ArgumentException("Not a supported tga file."); if (header.ImageSpec.Width > 4096 || header.ImageSpec.Height > 4096) throw new ArgumentException("Image too large."); System.Drawing.Bitmap b; System.Drawing.Imaging.BitmapData bd; // Create a bitmap for the image. // Only include an alpha layer when the image requires one. if (header.ImageSpec.AlphaBits > 0 || header.ImageSpec.PixelDepth == 8 || // Assume 8 bit images are alpha only header.ImageSpec.PixelDepth == 32) // Assume 32 bit images are ARGB { // Image needs an alpha layer b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); } else { // Image does not need an alpha layer, so do not include one. b = new System.Drawing.Bitmap( header.ImageSpec.Width, header.ImageSpec.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); } switch (header.ImageSpec.PixelDepth) { case 8: decodeStandard8(bd, header, br); break; case 16: if (header.ImageSpec.AlphaBits > 0) decodeSpecial16(bd, header, br); else decodeStandard16(bd, header, br); break; case 24: if (header.ImageSpec.AlphaBits > 0) decodeSpecial24(bd, header, br); else decodeStandard24(bd, header, br); break; case 32: decodeStandard32(bd, header, br); break; default: b.UnlockBits(bd); b.Dispose(); return null; } b.UnlockBits(bd); return b; } }