-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite ColorQuantizer based on KGySoft.Drawing code (for saving imag…
…es in GIF format)
- Loading branch information
Showing
10 changed files
with
572 additions
and
729 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "ColorQuantizer.h" | ||
|
||
#include "OctreeColorQuantizer.h" | ||
|
||
using namespace Gdiplus; | ||
|
||
std::unique_ptr<Bitmap> ColorQuantizer::getQuantized(Bitmap* sourceBitmap, Gdiplus::Color backgroundColor, UINT nMaxColors /* = 256 */) | ||
{ | ||
BYTE alphaThreshold = 0; | ||
UINT width = sourceBitmap->GetWidth(); | ||
UINT height = sourceBitmap->GetHeight(); | ||
Rect rc(0, 0, width, height); | ||
std::unique_ptr<Bitmap> destBitmap; | ||
BitmapData dataSource; | ||
|
||
if (sourceBitmap->LockBits(&rc, ImageLockModeRead, PixelFormat32bppARGB, &dataSource) == Ok) { | ||
BYTE* pScan0Source = (BYTE*)dataSource.Scan0; | ||
UINT strideSource; | ||
|
||
if (dataSource.Stride > 0) { | ||
strideSource = dataSource.Stride; | ||
} else { | ||
pScan0Source += height * dataSource.Stride; | ||
strideSource = -dataSource.Stride; | ||
} | ||
|
||
BYTE* pRowSource = pScan0Source; | ||
OctreeColorQuantizer quantizer(nMaxColors, 8, &dataSource); | ||
destBitmap = std::make_unique<Bitmap>(width, height, PixelFormat8bppIndexed); | ||
|
||
for (UINT y = 0; y < height; y++) { | ||
BYTE* pPixelSource = pRowSource; | ||
|
||
for (UINT x = 0; x < width; x++) { | ||
BYTE b = *pPixelSource++; | ||
BYTE g = *pPixelSource++; | ||
BYTE r = *pPixelSource++; | ||
BYTE a = *pPixelSource++; | ||
Gdiplus::Color c(a, r, g, b); | ||
if (c.GetA() != 255) { | ||
c = c.GetA() < alphaThreshold ? Color() : BlendWithBackgroundSrgb(c, backgroundColor); | ||
} | ||
|
||
quantizer.addColor(c); | ||
} | ||
|
||
pRowSource += strideSource; | ||
} | ||
|
||
auto pallete = quantizer.generatePalette(); | ||
MyPalette mypal(pallete.get(), backgroundColor, alphaThreshold); | ||
|
||
BitmapData dataDest; | ||
destBitmap->SetPalette(pallete.get()); | ||
if (destBitmap->LockBits(&rc, ImageLockModeWrite, PixelFormat8bppIndexed, &dataDest) == Ok) { | ||
BYTE* pRowSource = pScan0Source; | ||
|
||
BYTE* pRowDest = (BYTE*)dataDest.Scan0; | ||
UINT strideDest; | ||
|
||
if (dataDest.Stride > 0) { | ||
strideDest = dataDest.Stride; | ||
} else { | ||
pRowDest += height * dataDest.Stride; | ||
strideDest = -dataDest.Stride; | ||
} | ||
|
||
for (UINT y = 0; y < height; y++) { | ||
BYTE* pPixelSource = pRowSource; | ||
BYTE* pPixelDest = pRowDest; | ||
|
||
for (UINT x = 0; x < width; x++) { | ||
BYTE b = *pPixelSource++; | ||
BYTE g = *pPixelSource++; | ||
BYTE r = *pPixelSource++; | ||
BYTE a = *pPixelSource++; | ||
|
||
BYTE index = (BYTE)mypal.getNearestColorIndex(Gdiplus::Color(a, r, g, b)); | ||
|
||
*pPixelDest++ = index; | ||
} | ||
|
||
pRowSource += strideSource; | ||
pRowDest += strideDest; | ||
} | ||
destBitmap->UnlockBits(&dataDest); | ||
} | ||
sourceBitmap->UnlockBits(&dataSource); | ||
} | ||
return destBitmap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
#pragma once | ||
#include <memory> | ||
|
||
#include <windows.h> | ||
#include "3rdpart/GdiplusH.h" | ||
|
||
class ColorQuantizer { | ||
public: | ||
std::unique_ptr<Gdiplus::Bitmap> getQuantized(Gdiplus::Bitmap* pSource, Gdiplus::Color backgroundColor, UINT nMaxColors = 256); | ||
}; |
Oops, something went wrong.