Added alpha-blending to the Quantizer, otherwise the colors would be counted wrong and look like sh*t.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2509 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-02-27 17:17:31 +00:00
parent e5ee2c3711
commit 565bf262db
2 changed files with 35 additions and 6 deletions

View file

@ -138,6 +138,17 @@ namespace GreenshotPlugin.Core {
bool Contains(int x, int y); bool Contains(int x, int y);
} }
/// <summary>
/// This interface is implemented when there is a alpha-blending possibility
/// </summary>
public interface IFastBitmapWithBlend : IFastBitmap {
Color BackgroundBlendColor {
get;
set;
}
Color GetBlendedColorAt(int x, int y);
}
/// <summary> /// <summary>
/// The base class for the fast bitmap implementation /// The base class for the fast bitmap implementation
/// </summary> /// </summary>
@ -659,7 +670,7 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// This is the implementation of the IFastBitmap for 32 bit images with Alpha /// This is the implementation of the IFastBitmap for 32 bit images with Alpha
/// </summary> /// </summary>
public unsafe class Fast32ARGBBitmap : FastBitmap { public unsafe class Fast32ARGBBitmap : FastBitmap, IFastBitmapWithBlend {
public override bool hasAlphaChannel { public override bool hasAlphaChannel {
get { get {
return true; return true;

View file

@ -155,12 +155,18 @@ 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 // Use a bitmap to store the initial match, which is just as good as an array and saves us 2x the storage
using (IFastBitmap sourceFastBitmap = FastBitmap.Create(sourceBitmap)) { using (IFastBitmap sourceFastBitmap = FastBitmap.Create(sourceBitmap)) {
IFastBitmapWithBlend sourceFastBitmapWithBlend = sourceFastBitmap as IFastBitmapWithBlend;
sourceFastBitmap.Lock(); sourceFastBitmap.Lock();
using (FastChunkyBitmap destinationFastBitmap = FastBitmap.CreateEmpty(sourceBitmap.Size, PixelFormat.Format8bppIndexed, Color.White) as FastChunkyBitmap) { using (FastChunkyBitmap destinationFastBitmap = FastBitmap.CreateEmpty(sourceBitmap.Size, PixelFormat.Format8bppIndexed, Color.White) as FastChunkyBitmap) {
destinationFastBitmap.Lock(); destinationFastBitmap.Lock();
for (int y = 0; y < sourceFastBitmap.Height; y++) { for (int y = 0; y < sourceFastBitmap.Height; y++) {
for (int x = 0; x < sourceFastBitmap.Width; x++) { for (int x = 0; x < sourceFastBitmap.Width; x++) {
Color color = sourceFastBitmap.GetColorAt(x, y); Color color;
if (sourceFastBitmapWithBlend == null) {
color = sourceFastBitmap.GetColorAt(x, y);
} else {
color = sourceFastBitmapWithBlend.GetBlendedColorAt(x, y);
}
// To count the colors // To count the colors
int index = color.ToArgb() & 0x00ffffff; int index = color.ToArgb() & 0x00ffffff;
// Check if we already have this color // Check if we already have this color
@ -207,11 +213,18 @@ namespace GreenshotPlugin.Core {
using (FastChunkyBitmap bbbDest = FastBitmap.Create(resultBitmap) as FastChunkyBitmap) { using (FastChunkyBitmap bbbDest = FastBitmap.Create(resultBitmap) as FastChunkyBitmap) {
bbbDest.Lock(); bbbDest.Lock();
using (IFastBitmap bbbSrc = FastBitmap.Create(sourceBitmap)) { using (IFastBitmap bbbSrc = FastBitmap.Create(sourceBitmap)) {
IFastBitmapWithBlend bbbSrcBlend = bbbSrc as IFastBitmapWithBlend;
bbbSrc.Lock(); bbbSrc.Lock();
byte index; byte index;
for (int y = 0; y < bbbSrc.Height; y++) { for (int y = 0; y < bbbSrc.Height; y++) {
for (int x = 0; x < bbbSrc.Width; x++) { for (int x = 0; x < bbbSrc.Width; x++) {
Color color = bbbSrc.GetColorAt(x, y); Color color;
if (bbbSrcBlend != null) {
color = bbbSrcBlend.GetBlendedColorAt(x, y);
} else {
color = bbbSrc.GetColorAt(x, y);
}
if (lookup.ContainsKey(color)) { if (lookup.ContainsKey(color)) {
index = lookup[color]; index = lookup[color];
} else { } else {
@ -322,14 +335,19 @@ namespace GreenshotPlugin.Core {
using (FastChunkyBitmap bbbDest = FastBitmap.Create(resultBitmap) as FastChunkyBitmap) { using (FastChunkyBitmap bbbDest = FastBitmap.Create(resultBitmap) as FastChunkyBitmap) {
bbbDest.Lock(); bbbDest.Lock();
using (IFastBitmap bbbSrc = FastBitmap.Create(sourceBitmap)) { using (IFastBitmap bbbSrc = FastBitmap.Create(sourceBitmap)) {
IFastBitmapWithBlend bbbSrcBlend = bbbSrc as IFastBitmapWithBlend;
bbbSrc.Lock(); bbbSrc.Lock();
Dictionary<Color, byte> lookup = new Dictionary<Color, byte>(); Dictionary<Color, byte> lookup = new Dictionary<Color, byte>();
byte bestMatch; byte bestMatch;
for (int y = 0; y < bbbSrc.Height; y++) { for (int y = 0; y < bbbSrc.Height; y++) {
for (int x = 0; x < bbbSrc.Width; x++) { for (int x = 0; x < bbbSrc.Width; x++) {
// Consider WithoutAlpha Color color;
Color color = bbbSrc.GetColorAt(x, y); if (bbbSrcBlend != null) {
// No need to a strip the alpha as before // WithoutAlpha, this makes it possible to ignore the alpha
color = bbbSrcBlend.GetBlendedColorAt(x, y);
} else {
color = bbbSrc.GetColorAt(x, y);
}
// Check if we already matched the color // Check if we already matched the color
if (!lookup.ContainsKey(color)) { if (!lookup.ContainsKey(color)) {