Skip to content

Commit da23152

Browse files
committed
pass DitherType as arg
1 parent bc16f92 commit da23152

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Qualetize.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
#include "Tiles.h"
77
/**************************************/
88

9+
//! Dither modes available
10+
#define DITHER_NONE ( 0) //! No dither
11+
#define DITHER_ORDERED(n) ( n) //! Ordered dithering (Kernel size: (2^n) x (2^n))
12+
#define DITHER_FLOYDSTEINBERG (-1) //! Floyd-Steinberg (diffusion)
13+
14+
//! Dither settings
15+
//! NOTE: Ordered dithering gives consistent tiled results, but Floyd-Steinberg can look nicer.
16+
//! Recommend dither level of 0.5 for ordered, and 1.0 for Floyd-Steinberg.
17+
//! NOTE: DITHER_NO_ALPHA disables dithering on the alpha channel.
18+
#define DITHER_LEVEL 0.5f
19+
#define DITHER_NO_ALPHA
20+
21+
/**************************************/
22+
923
//! Handle conversion of image, return RMS error
1024
//! NOTE:
1125
//! * With ReplaceImage != 0, {Image->ColMap,Image->PxIdx} (or
@@ -18,6 +32,8 @@ struct BGRAf_t Qualetize(
1832
int MaxTilePals,
1933
int MaxPalSize,
2034
int PalUnused,
35+
const struct BGRA8_t *BitRange,
36+
int DitherType,
2137
int ReplaceImage
2238
);
2339

tilequant.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#define TILE_H 8
1313
#define PALUNUSED 1 //! These will be filled with {0,0,0,0} but still be used for matching
1414
#define BITRANGE (const struct BGRA8_t){0x1F,0x1F,0x1F,0x01} //! None of these may be 0
15+
16+
//! Default dither mode
17+
#define DITHER_TYPE DITHER_ORDERED(3)
18+
1519
/**************************************/
1620

1721
//! When not zero, the PSNR for each channel will be displayed
@@ -47,7 +51,7 @@ int main(int argc, const char *argv[]) {
4751

4852
//! Perform processing
4953
//! NOTE: PxData and Palette will be assigned to image; do NOT destroy
50-
struct TilesData_t *TilesData = TilesData_FromBitmap(&Image, TILE_W, TILE_H, &BITRANGE);
54+
struct TilesData_t *TilesData = TilesData_FromBitmap(&Image, TILE_W, TILE_H);
5155
uint8_t *PxData = malloc(Image.Width * Image.Height * sizeof(uint8_t));
5256
struct BGRAf_t *Palette = calloc(BMP_PALETTE_COLOURS, sizeof(struct BGRAf_t));
5357
if(!TilesData || !PxData || !Palette) {
@@ -58,7 +62,9 @@ int main(int argc, const char *argv[]) {
5862
BmpCtx_Destroy(&Image);
5963
return -1;
6064
}
61-
struct BGRAf_t RMSE = Qualetize(&Image, TilesData, PxData, Palette, MaxTilePals, MaxPalSize, PALUNUSED, 1);
65+
struct BGRAf_t RMSE = Qualetize(
66+
&Image, TilesData, PxData, Palette, MaxTilePals, MaxPalSize, PALUNUSED, &BITRANGE, DITHER_TYPE, 1
67+
);
6268
free(TilesData);
6369

6470
//! Output PSNR

tilequantDLL.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! NOTE: DstPal must have enough space to accomodate:
2222
//! (struct BGRAf_t)[nPalettes * nColoursPerPalette]
2323
//! TilePalIdx = NULL or int32_t[(Width*Height) / (TileW*TileH)]
24+
//! DitherMode = Dither mode to use: 0 = DITHER_NONE, -1 = DITHER_FLOYDSTEINBERG, n = DITHER_ORDERED(n)
2425
//! OutputPaletteIs24bitRGB outputs RGB (byte order: {RR, GG, BB})
2526
//! colours without an alpha channel; the default is to output to
2627
//! BGRA (byte order: {BB, GG, RR, AA}).
@@ -41,7 +42,8 @@ DECLSPEC int QualetizeFromRawImage(
4142
int TileW,
4243
int TileH,
4344
int32_t *TilePalIdx,
44-
const uint8_t BitRange[4]
45+
const uint8_t BitRange[4],
46+
int DitherMode
4547
) {
4648
//! Create image context
4749
//! NOTE: 'const' violations in image data, but not modified so this is safe
@@ -55,9 +57,11 @@ DECLSPEC int QualetizeFromRawImage(
5557
//! Do processing
5658
//! NOTE: Do NOT allow image replacing, or things will go
5759
//! very wrong when Qualetize() tries to free the pointers.
58-
struct TilesData_t *TilesData = TilesData_FromBitmap(&Ctx, TileW, TileH, (const struct BGRA8_t*)BitRange);
60+
struct TilesData_t *TilesData = TilesData_FromBitmap(&Ctx, TileW, TileH);
5961
if(!TilesData) return 0;
60-
(void)Qualetize(&Ctx, TilesData, DstPxIdx, (struct BGRAf_t*)DstPal, nPalettes, nColoursPerPalette, nUnusedColoursPerPalette, 0);
62+
(void)Qualetize(
63+
&Ctx, TilesData, DstPxIdx, (struct BGRAf_t*)DstPal, nPalettes, nColoursPerPalette, nUnusedColoursPerPalette, (const struct BGRA8_t*)BitRange, DitherMode, 0
64+
);
6165

6266
//! Store tile palette indices
6367
if(TilePalIdx) {

0 commit comments

Comments
 (0)