diff --git a/GreenshotPlugin/Core/BitmapBuffer.cs b/GreenshotPlugin/Core/BitmapBuffer.cs index e8948b144..ea5d755be 100644 --- a/GreenshotPlugin/Core/BitmapBuffer.cs +++ b/GreenshotPlugin/Core/BitmapBuffer.cs @@ -33,6 +33,8 @@ namespace GreenshotPlugin.Core { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BitmapBuffer)); private bool clone; private Bitmap bitmap; + // Used for indexed images + private Color[] colorEntries; public static Color BackgroundBlendColor { get; @@ -164,6 +166,9 @@ namespace GreenshotPlugin.Core { this.bitmap = sourceImage as Bitmap; this.rect = sourceRect; } + if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) { + colorEntries = bitmap.Palette.Entries; + } } /** @@ -297,21 +302,33 @@ namespace GreenshotPlugin.Core { /// /// X coordinate /// Y Coordinate - /// index + /// color index public byte GetColorIndexAt(int x, int y) { int offset = x*bytesPerPixel+y*stride; return pointer[offset]; } + /// + /// Get the color for an 8-bit image + /// + /// + /// + /// Color from the palette + public Color GetColor(int x, int y) { + int offset = x * bytesPerPixel + y * stride; + byte colorIndex = pointer[offset]; + return colorEntries[colorIndex]; + } + /// /// Set the color index, for 8BPP, at location x,y /// /// X coordinate /// Y Coordinate /// Color index to set - public void SetColorIndexAt(int x, int y, byte color) { + public void SetColorIndexAt(int x, int y, byte colorIndex) { int offset = x * bytesPerPixel + y * stride; - pointer[offset] = color; + pointer[offset] = colorIndex; } /// @@ -369,6 +386,7 @@ namespace GreenshotPlugin.Core { int blue = pointer[bIndex + offset]; if (a < 255) { + // As the request is to get without alpha, we blend. int rem = 255 - a; red = (red * a + BackgroundBlendColor.R * rem) / 255; green = (green * a + BackgroundBlendColor.G * rem) / 255; diff --git a/GreenshotPlugin/Core/QuantizerHelper.cs b/GreenshotPlugin/Core/QuantizerHelper.cs index 8cf79665e..acb9ee10c 100644 --- a/GreenshotPlugin/Core/QuantizerHelper.cs +++ b/GreenshotPlugin/Core/QuantizerHelper.cs @@ -149,13 +149,19 @@ namespace GreenshotPlugin.Core { // Use a bitmap to store the initial match, which is just as good as an array and saves us 2x the storage resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format8bppIndexed); resultBitmap.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + bool is8Bit = sourceBitmap.PixelFormat == PixelFormat.Format8bppIndexed; using (BitmapBuffer bbbSrc = new BitmapBuffer(sourceBitmap, false)) { bbbSrc.Lock(); using (BitmapBuffer bbbDest = new BitmapBuffer(resultBitmap, false)) { bbbDest.Lock(); for (int y = 0; y < bbbSrc.Height; y++) { for (int x = 0; x < bbbSrc.Width; x++) { - Color color = bbbSrc.GetColorAtWithoutAlpha(x, y); + Color color; + if (is8Bit) { + color = bbbSrc.GetColor(x, y); + } else { + color = bbbSrc.GetColorAtWithoutAlpha(x, y); + } // To count the colors int index = color.ToArgb() & 0x00ffffff; // Check if we already have this color @@ -203,9 +209,15 @@ namespace GreenshotPlugin.Core { using (BitmapBuffer bbbSrc = new BitmapBuffer(sourceBitmap, false)) { bbbSrc.Lock(); byte index; + bool is8Bit = sourceBitmap.PixelFormat == PixelFormat.Format8bppIndexed; for (int y = 0; y < bbbSrc.Height; y++) { for (int x = 0; x < bbbSrc.Width; x++) { - Color color = bbbSrc.GetColorAt(x, y); + Color color; + if (is8Bit) { + color = bbbSrc.GetColor(x, y); + } else { + color = bbbSrc.GetColorAt(x, y); + } if (lookup.ContainsKey(color)) { index = lookup[color]; } else { @@ -240,9 +252,12 @@ namespace GreenshotPlugin.Core { /// Get the image /// public Bitmap GetQuantizedImage(int allowedColorCount) { - if (colorCount < 256) { + if (allowedColorCount > 256) { + throw new ArgumentOutOfRangeException("Quantizing muss be done to get less than 256 colors"); + } + if (colorCount < allowedColorCount) { // Simple logic to reduce to 8 bit - LOG.Info("Using simple copy, no quantizing needed!"); + LOG.Info("Colors in the image are already less as whished for, using simple copy to indexed image, no quantizing needed!"); return SimpleReindex(); } // preprocess the colors @@ -315,9 +330,18 @@ namespace GreenshotPlugin.Core { bbbSrc.Lock(); Dictionary lookup = new Dictionary(); byte bestMatch; + bool is8Bit = sourceBitmap.PixelFormat == PixelFormat.Format8bppIndexed; for (int y = 0; y < bbbSrc.Height; y++) { for (int x = 0; x < bbbSrc.Width; x++) { - Color color = bbbSrc.GetColorAtWithoutAlpha(x, y); + Color color; + if (is8Bit) { + color = bbbSrc.GetColor(x, y); + } else { + color = bbbSrc.GetColorAtWithoutAlpha(x, y); + } + + // No need to a strip the alpha as before + // Check if we already matched the color if (!lookup.ContainsKey(color)) { // If not we need to find the best match