public static Texture LoadTgaTexture( GraphicsDevice graphicsDevice, ResourceFactory resourceFactory, Stream stream, Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm) { using var bitmap = new TgaImage(stream, true).GetBitmap(); var width = (uint)bitmap.Width; var height = (uint)bitmap.Height; var sampledTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Sampled)); var stagingTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Staging)); var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var byteCount = bitmapData.Stride * bitmap.Height; var pixelData = new byte[byteCount]; var pointer = bitmapData.Scan0; Marshal.Copy(pointer, pixelData, 0, byteCount); graphicsDevice.UpdateTexture(stagingTexture, pixelData, 0, 0, 0, width, height, 1, 0, 0); CreateCommandList(graphicsDevice, resourceFactory, stagingTexture, sampledTexture); return(sampledTexture); }
/// <summary> /// ファイルオープン時の処理。 /// </summary> /// <param name="message">ファイルオープンメッセージ。</param> private void OnOpenFileDialog(OpenFileDialogMessage message) { if (!message.DialogResult.HasValue || !message.DialogResult.Value) { return; } try { using (var fs = new FileStream(message.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var reader = new BinaryReader(fs)) { var tga = new TgaImage(reader); ImageSource = tga.GetBitmap(); } OpenedFile = message.FileName; } catch (Exception ex) { MessengerInstance.Send <DialogMessage>( new DialogMessage(string.Format("ファイルのオープンに失敗しました\r\n{0}", ex), (r) => { }) { Caption = "ファイルオープンエラー" }); } }
/// <summary> /// Loads image data from disk into a byte array. /// </summary> public void LoadImageFromDisk(string filePath) { // TGA is a special format, so we need to handle is separately from bmp/jpg/png/etc. if (filePath.EndsWith(".tga")) { using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { BinaryReader reader = new BinaryReader(strm); TgaImage tga = new TgaImage(reader); m_rgbaImageData = tga.ImageBytes; Width = tga.Header.Width; Height = tga.Header.Height; } return; } Bitmap bmp = new Bitmap(filePath); byte[] data = new byte[bmp.Width * bmp.Height * 4]; BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); Marshal.Copy(bmpData.Scan0, data, 0, data.Length); bmp.UnlockBits(bmpData); m_rgbaImageData = data; Width = (ushort)bmp.Width; Height = (ushort)bmp.Height; }
static void Main(string[] args) { using (var fs = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read)) using (var reader = new BinaryReader(fs)) { var tga = new TgaImage(reader); Console.WriteLine("{0}", tga); } }
public bool IsIndexedPalette(TgaImage tgaImage) { switch (tgaImage.DataTypeCode) { case TgaDataType.RleIndexed: case TgaDataType.HuffmanIndexed: case TgaDataType.UncompressedIndexed: case TgaDataType.HuffmanQuadtreeIndexed: return(true); default: return(false); } }
private Bitmap LoadImage(FileInfo fileInfo) { if (!fileInfo.Exists) { throw new($"Image loading: {fileInfo.FullName} does not exists"); } if (!imageCache.TryGetImage(fileInfo, out var image)) { image = TgaImage.FromFile(fileInfo.FullName).ToBitmap(); imageCache.Set(fileInfo, image); } return(image); }
public static Bitmap OpenTGA(string path) { using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path))) { //return DmitryBrant.ImageFormats.TgaReader.Load(ms); //Surface surface = Surface.LoadFromStream(ms, true); //return getBitmapFromSurface(surface); using (BinaryReader br = new BinaryReader(ms)) { TgaImage tga = new TgaImage(br); BitmapSource bmpSource = tga.GetBitmap(); return(getBitmapFromBitmapSource(bmpSource)); } } }
static void Main(string[] args) { string[] files = Directory.GetFiles(".", "*.bmp"); BmpImage inImage; TgaImage outImage; foreach (string file in files) { Console.WriteLine("Converting {0}",file); inImage = new BmpImage(file); outImage = TgaImage.FromBMP(inImage); outImage.Save(file + ".TGA"); } Console.WriteLine("Done!"); }
public static BitmapSource OpenImage(FileInfo fileInfo) { switch (fileInfo.Extension) { case ".tga": var targa = new TgaImage(fileInfo.FullName); return(targa.BitmapSourceImage); case ".jpg": case ".jpeg": case ".png": case ".tiff": case ".gif": case ".bmp": var bitmapImage = new BitmapImage(new Uri(fileInfo.FullName, UriKind.Relative)); return(new FormatConvertedBitmap(bitmapImage, PixelFormats.Bgra32, bitmapImage.Palette, .5)); default: throw new NotSupportedException($"File extension {fileInfo.Extension} not supported"); } }
public void TestGetBitmap() { var testcase = new[] { new { Input = "TestData/UBW8.tga", Expected = "TestData/grayscale.png", UseAlphaForcefully = false }, new { Input = "TestData/UCM8.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC16.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC24.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC32.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CBW8.tga", Expected = "TestData/grayscale.png", UseAlphaForcefully = false }, new { Input = "TestData/CCM8.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC16.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC24.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC32.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/rgb32rle.tga", Expected = "TestData/rgb32rle.png", UseAlphaForcefully = true }, }; testcase.ToList().ForEach((tc) => { using (var fs = new FileStream(tc.Input, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var r = new BinaryReader(fs)) { var expectedImage = new BitmapImage(new Uri(tc.Expected, UriKind.Relative)); var tga = new TgaImage(r, tc.UseAlphaForcefully); var actualImage = tga.GetBitmap(); var expectedConvertedImage = new FormatConvertedBitmap(expectedImage, PixelFormats.Bgra32, null, 0.0); var bytesPerPixel = (expectedConvertedImage.Format.BitsPerPixel + 7) / 8; var stride = expectedConvertedImage.PixelWidth * bytesPerPixel; var expectedImageBytes = new byte[stride * expectedImage.PixelHeight]; expectedConvertedImage.CopyPixels(expectedImageBytes, stride, 0); var actualConvertedImage = new FormatConvertedBitmap(actualImage, PixelFormats.Bgra32, null, 0.0); var actualImageBytes = new byte[stride * tga.Header.Height]; actualConvertedImage.CopyPixels(actualImageBytes, stride, 0); CollectionAssert.AreEqual(expectedImageBytes, actualImageBytes, string.Format("expected:{0}, actual:{1}", tc.Expected, tc.Input)); } }); }
static int Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage:"); Console.WriteLine(" tgareader <filename.tga>"); return(-1); } if (!File.Exists(args[0])) { Console.WriteLine("Usage:"); Console.WriteLine(" tgareader <filename.tga>"); Console.WriteLine(); Console.WriteLine($"\"{args[0]}\" not found."); return(-1); } var tgaImage = new TgaImage(args[0]); tgaImage.Validate(); Console.WriteLine($"TGA Info: {args[0]}"); Console.WriteLine(); Console.WriteLine($" ColorMapLength: {tgaImage.Header.ColorMapSpecification.ColorMapLength}"); Console.WriteLine($" ColorMapEntrySize: {tgaImage.Header.ColorMapSpecification.ColorMapEntrySize}"); Console.WriteLine($" XOrigin: {tgaImage.Header.ImageSpecification.XOrigin}"); Console.WriteLine($" YOrigin: {tgaImage.Header.ImageSpecification.YOrigin}"); Console.WriteLine($" ImageWidth: {tgaImage.Header.ImageSpecification.ImageWidth}"); Console.WriteLine($" ImageHeight: {tgaImage.Header.ImageSpecification.ImageHeight}"); Console.WriteLine(); Console.WriteLine(" Palette:"); for (var i = 0; i < tgaImage.Header.ColorMapData.TgaColors.Length; i++) { var color = tgaImage.Header.ColorMapData.TgaColors[i]; Console.WriteLine($" {i:D3} - {color.Red:D3}, {color.Green:D3}, {color.Blue:D3}"); } return(0); }
public void TestGetTgaBitmap(string inputImagePath, string expectedImagePath, bool forceAlpha) { using (var fileStream = File.OpenRead(inputImagePath)) { var expectedImage = new Bitmap(expectedImagePath); var actualImage = new TgaImage(fileStream, forceAlpha).GetBitmap(); Assert.AreEqual(expectedImage.Width, actualImage.Width); Assert.AreEqual(expectedImage.Height, actualImage.Height); for (var y = 0; y < expectedImage.Height; y++) { for (var x = 0; x < expectedImage.Width; x++) { Assert.AreEqual(expectedImage.GetPixel(x, y), actualImage.GetPixel(x, y)); } } expectedImage.Dispose(); actualImage.Dispose(); } }
public RawBitmap Decode(TgaImage tgaImage) { if (tgaImage.Interleave != TgaInterleave.None) { throw new RhythmCodexException("Only non-interleaved images are supported for now."); } if (IsIndexedPalette(tgaImage)) { return(DecodeIndexed(tgaImage).ToRawBitmap()); } switch (tgaImage.DataTypeCode) { case TgaDataType.UncompressedRgb: { var count = tgaImage.Width * tgaImage.Height; var pixels = new int[count]; var inputIndex = 0; var outputIndex = tgaImage.OriginType == TgaOriginType.UpperLeft ? 0 : tgaImage.Width * (tgaImage.Height - 1); var scanIncrement = tgaImage.OriginType == TgaOriginType.UpperLeft ? 0 : tgaImage.Width * -2; var data = tgaImage.ImageData; switch (tgaImage.BitsPerPixel) { case 24: { for (var y = 0; y < tgaImage.Height; y++) { for (var x = 0; x < tgaImage.Width; x++) { pixels[outputIndex++] = ~0x00FFFFFF | data[inputIndex] | (data[inputIndex + 1] << 8) | (data[inputIndex + 2] << 16); inputIndex += 3; } outputIndex += scanIncrement; } return(new RawBitmap { Width = tgaImage.Width, Height = tgaImage.Height, Data = pixels }); } case 32: { for (var y = 0; y < tgaImage.Height; y++) { for (var x = 0; x < tgaImage.Width; x++) { pixels[outputIndex++] = data[inputIndex] | (data[inputIndex + 1] << 8) | (data[inputIndex + 2] << 16) | (data[inputIndex + 3] << 24); inputIndex += 4; } outputIndex += scanIncrement; } return(new RawBitmap { Width = tgaImage.Width, Height = tgaImage.Height, Data = pixels }); } default: { throw new RhythmCodexException($"Bit depth is not supported: {tgaImage.BitsPerPixel}"); } } } default: { throw new RhythmCodexException($"This TGA image type is not supported: {tgaImage.DataTypeCode}"); } } }
public PaletteBitmap DecodeIndexed(TgaImage tgaImage) { if (!IsIndexedPalette(tgaImage)) { throw new RhythmCodexException( $"{nameof(DecodeIndexed)} can only be used with images that contain a palette."); } var palette = new int[tgaImage.ColorMapLength]; var paletteSize = tgaImage.ColorMapLength * tgaImage.ColorMapBitsPerEntry / 8; var paltteData = tgaImage.ImageData.AsSpan(0, paletteSize); switch (tgaImage.ColorMapBitsPerEntry) { case 16: { for (var i = 0; i < tgaImage.ColorMapLength; i++) { var sourceIndex = i << 1; var entry = paltteData[sourceIndex] | (paltteData[sourceIndex + 1] << 8); var blue = (entry & 0x1F) << 3; var green = (entry >> 2) & 0xF8; var red = (entry >> 7) & 0xF8; var alpha = (entry >> 15) * 0xFF; palette[i] = blue | (green << 8) | (red << 16) | (alpha << 24); } break; } case 24: { var inputIndex = 0; for (var i = 0; i < tgaImage.ColorMapLength; i++) { palette[i] = ~0x00FFFFFF | paltteData[inputIndex++] | (paltteData[inputIndex++] << 8) | (paltteData[inputIndex++] << 16); } break; } case 32: { var inputIndex = 0; for (var i = 0; i < tgaImage.ColorMapLength; i++) { palette[i] = paltteData[inputIndex++] | (paltteData[inputIndex++] << 8) | (paltteData[inputIndex++] << 16) | (paltteData[inputIndex++] << 24); } break; } default: { throw new RhythmCodexException("Only palettes with color index sizes 2-4 are supported."); } } switch (tgaImage.DataTypeCode) { case TgaDataType.UncompressedIndexed: { if (tgaImage.BitsPerPixel != 8) { throw new RhythmCodexException("Only 8bpp is supported for indexed images for now."); } var count = tgaImage.Width * tgaImage.Height; var pixels = new int[count]; var inputIndex = 0; var outputIndex = tgaImage.OriginType == TgaOriginType.UpperLeft ? 0 : tgaImage.Width * (tgaImage.Height - 1); var scanIncrement = tgaImage.OriginType == TgaOriginType.UpperLeft ? 0 : tgaImage.Width * -2; var data = tgaImage.ImageData.AsSpan(paletteSize); for (var y = 0; y < tgaImage.Height; y++) { for (var x = 0; x < tgaImage.Width; x++) { pixels[outputIndex++] = data[inputIndex++]; } outputIndex += scanIncrement; } return(new PaletteBitmap { Width = tgaImage.Width, Height = tgaImage.Height, Data = pixels, Palette = palette }); } default: { throw new RhythmCodexException($"This TGA image type is not supported: {tgaImage.DataTypeCode}"); } } }
// NOTE: The following implementation duplicates with ImageFile. // We need to consolidate and remove one of them. object IExtractIcon.ExtractIcon(ExtractIconType type, Size desiredSize) { if (type == ExtractIconType.Thumbnail) { // Do we support this extension? string ext = Path.GetExtension( entry.FullName).ToLowerInvariant(); if (ext == ".mif") { using (Stream stream = entry.Open()) { QQGame.MifImage mif = new QQGame.MifImage(stream); mif.Name = entry.FullName; return mif; } } else if (ext == ".bmp" || ext == ".png") { using (Stream stream = entry.Open()) using (Bitmap bmp = new Bitmap(stream)) { // Make a copy of the bitmap, because MSDN says "You must // keep the stream open for the lifetime of the Bitmap." return bmp.Clone(); } } else if (ext == ".tga") { using (Stream stream = entry.Open()) using (TgaImage tga = new TgaImage(stream)) { // Make a copy of the bitmap so that we can dispose // the TgaImage object. return tga.Image.Clone(); } } } return null; }