Fix for bug 1444, the WuQuantizer doesn't work if the colors are already less than 256 so we just create a 8bpp image from the source.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2451 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-02-01 16:18:48 +00:00
parent e30939093a
commit 2730ac5540
4 changed files with 57 additions and 14 deletions

View file

@ -533,7 +533,7 @@ namespace Greenshot.Drawing {
/// <param name="memento">The memento implementing the undo</param>
public void MakeUndoable(IMemento memento, bool allowMerge) {
if (inUndoRedo) {
throw new InvalidOperationException("Involking do within an undo/redo action.");
throw new InvalidOperationException("Invoking do within an undo/redo action.");
}
if (memento != null) {
bool allowPush = true;

View file

@ -1199,12 +1199,12 @@ namespace GreenshotPlugin.Core {
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <param name="horizontalResolution"></param>
/// <param name="verticalResolution"></param>
/// <returns></returns>
/// <returns>Bitmap</returns>
public static Bitmap CreateEmpty(int width, int height, PixelFormat format, Color backgroundColor, float horizontalResolution, float verticalResolution) {
// Create a new "clean" image
Bitmap newImage = new Bitmap(width, height, format);
newImage.SetResolution(horizontalResolution, verticalResolution);
if (format != PixelFormat.Format8bppIndexed) {
using (Graphics graphics = Graphics.FromImage(newImage)) {
// Make sure the background color is what we want (transparent or white, depending on the pixel format)
if (!Color.Empty.Equals(backgroundColor)) {
@ -1215,6 +1215,7 @@ namespace GreenshotPlugin.Core {
graphics.Clear(Color.White);
}
}
}
return newImage;
}

View file

@ -264,8 +264,8 @@ namespace GreenshotPlugin.Core {
LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
if (outputSettings.ReduceColors || colorCount < 256) {
try {
LOG.Info("Reducing colors on bitmap to 255.");
tmpImage = quantizer.GetQuantizedImage(255);
LOG.Info("Reducing colors on bitmap to 256.");
tmpImage = quantizer.GetQuantizedImage(256);
if (disposeImage) {
imageToSave.Dispose();
}

View file

@ -184,10 +184,52 @@ namespace GreenshotPlugin.Core {
return colorCount;
}
/// <summary>
/// Reindex the 24/32 BPP (A)RGB image to a 8BPP
/// </summary>
/// <returns>Bitmap</returns>
public Bitmap SimpleReindex() {
List<Color> colors = new List<Color>();
Dictionary<Color, byte> lookup = new Dictionary<Color, byte>();
using (BitmapBuffer bbbDest = new BitmapBuffer(resultBitmap, false)) {
bbbDest.Lock();
using (BitmapBuffer bbbSrc = new BitmapBuffer(sourceBitmap, false)) {
bbbSrc.Lock();
byte index;
for (int y = 0; y < bbbSrc.Height; y++) {
for (int x = 0; x < bbbSrc.Width; x++) {
Color color = bbbSrc.GetColorAt(x, y);
if (lookup.ContainsKey(color)) {
index = lookup[color];
} else {
colors.Add(color);
index = (byte)(colors.Count - 1);
lookup.Add(color, index);
}
bbbDest.SetColorIndexAt(x, y, index);
}
}
}
}
// generates palette
ColorPalette imagePalette = resultBitmap.Palette;
for (Int32 paletteIndex = 0; paletteIndex < colorCount; paletteIndex++) {
imagePalette.Entries[paletteIndex] = colors[paletteIndex];
}
resultBitmap.Palette = imagePalette;
return resultBitmap;
}
/// <summary>
/// Get the image
/// </summary>
public Bitmap GetQuantizedImage(int allowedColorCount) {
if (colorCount < 256) {
// Simple logic to reduce to 8 bit
LOG.Info("Using simple copy, no quantizing needed!");
return SimpleReindex();
}
// preprocess the colors
CalculateMoments();
LOG.Info("Calculated the moments...");
@ -302,9 +344,9 @@ namespace GreenshotPlugin.Core {
}
}
ColorPalette imagePalette = resultBitmap.Palette;
// generates palette
ColorPalette imagePalette = resultBitmap.Palette;
for (Int32 paletteIndex = 0; paletteIndex < allowedColorCount; paletteIndex++) {
if (sums[paletteIndex] > 0) {
reds[paletteIndex] /= sums[paletteIndex];