From 42d3ebebbaa4abeb06a1f7fe1e29e2982b2699db Mon Sep 17 00:00:00 2001 From: Latif Khalifa Date: Tue, 16 Mar 2010 18:43:49 +0000 Subject: [PATCH] LIBOMV-785: Imaging.OpenJPEG.DecodeToImage adds alpha channel to images that do not have alpha component Patch by Sheet Spotter git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@3282 52acb1d6-8a22-11de-b505-999d5b087335 --- OpenMetaverse/Imaging/ManagedImage.cs | 28 +++++++++++++++++++++- OpenMetaverse/Imaging/TGALoader.cs | 34 +++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/OpenMetaverse/Imaging/ManagedImage.cs b/OpenMetaverse/Imaging/ManagedImage.cs index a6211b14..b3c6f315 100644 --- a/OpenMetaverse/Imaging/ManagedImage.cs +++ b/OpenMetaverse/Imaging/ManagedImage.cs @@ -187,7 +187,33 @@ namespace OpenMetaverse.Imaging bitmap.UnlockBits(bd); } - else + else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb) + { + Channels = ImageChannels.Color; + Red = new byte[pixelCount]; + Green = new byte[pixelCount]; + Blue = new byte[pixelCount]; + + System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), + System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); + + unsafe + { + byte* pixel = (byte*)bd.Scan0; + + for (int i = 0; i < pixelCount; i++) + { + // GDI+ gives us BGR and we need to turn that in to RGB + Blue[i] = *(pixel++); + Green[i] = *(pixel++); + Red[i] = *(pixel++); + pixel++; // Skip over the empty byte where the Alpha info would normally be + } + } + + bitmap.UnlockBits(bd); + } + else { throw new NotSupportedException("Unrecognized pixel format: " + bitmap.PixelFormat.ToString()); } diff --git a/OpenMetaverse/Imaging/TGALoader.cs b/OpenMetaverse/Imaging/TGALoader.cs index d50176d2..a3e7e15e 100644 --- a/OpenMetaverse/Imaging/TGALoader.cs +++ b/OpenMetaverse/Imaging/TGALoader.cs @@ -454,12 +454,36 @@ namespace OpenMetaverse.Imaging 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.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); + } - 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: