Refactor palette sorting computation.

This will ease the integration of new methods.

Change-Id: Icec3eddaa4ab9030d28ccfe579eb6dc13ded2f02
This commit is contained in:
Vincent Rabaud
2023-07-26 09:16:07 +02:00
parent af7fbfd2d9
commit 1432ebbadb
3 changed files with 66 additions and 44 deletions

View File

@@ -182,8 +182,8 @@ static int PaletteHasNonMonotonousDeltas(const uint32_t* const palette,
return (sign_found & (sign_found << 1)) != 0; // two consequent signs.
}
void PaletteSortMinimizeDeltas(const uint32_t* const palette_sorted,
int num_colors, uint32_t* const palette) {
static void PaletteSortMinimizeDeltas(const uint32_t* const palette_sorted,
int num_colors, uint32_t* const palette) {
uint32_t predict = 0x00000000;
int i, k;
memcpy(palette, palette_sorted, num_colors * sizeof(*palette));
@@ -293,9 +293,10 @@ struct Sum {
uint32_t sum;
};
int PaletteSortModifiedZeng(const WebPPicture* const pic,
const uint32_t* const palette_in,
uint32_t num_colors, uint32_t* const palette) {
static int PaletteSortModifiedZeng(const WebPPicture* const pic,
const uint32_t* const palette_in,
uint32_t num_colors,
uint32_t* const palette) {
uint32_t i, j, ind;
uint8_t remapping[MAX_PALETTE_SIZE];
uint32_t* cooccurrence;
@@ -375,3 +376,27 @@ int PaletteSortModifiedZeng(const WebPPicture* const pic,
}
return 1;
}
// -----------------------------------------------------------------------------
int PaletteSort(PaletteSorting method, const struct WebPPicture* const pic,
const uint32_t* const palette_sorted, uint32_t num_colors,
uint32_t* const palette) {
switch (method) {
case kSortedDefault:
// Nothing to do, we have already sorted the palette.
memcpy(palette, palette_sorted, num_colors * sizeof(*palette));
return 1;
case kMinimizeDelta:
PaletteSortMinimizeDeltas(palette_sorted, num_colors, palette);
return 1;
case kModifiedZeng:
return PaletteSortModifiedZeng(pic, palette_sorted, num_colors, palette);
case kUnusedPalette:
case kPaletteSortingNum:
break;
}
assert(0);
return 0;
}

View File

@@ -21,9 +21,15 @@ struct WebPPicture;
// The different ways a palette can be sorted.
typedef enum PaletteSorting {
kSortedDefault = 0,
// Sorts by minimizing L1 deltas between consecutive colors, giving more
// weight to RGB colors.
kMinimizeDelta = 1,
// Implements the modified Zeng method from "A Survey on Palette Reordering
// Methods for Improving the Compression of Color-Indexed Images" by Armando
// J. Pinho and Antonio J. R. Neves.
kModifiedZeng = 2,
kUnusedPalette = 3
kUnusedPalette = 3,
kPaletteSortingNum = 4
} PaletteSorting;
// Returns the index of 'color' in the sorted palette 'sorted' of size
@@ -44,19 +50,11 @@ void PrepareMapToPalette(const uint32_t palette[], uint32_t num_colors,
int GetColorPalette(const struct WebPPicture* const pic,
uint32_t* const palette);
// Sorts the color sorted palette in 'palette_sorted'/'num_colors' by
// minimizing deltas between consecutive colors and stores it in 'palette'.
void PaletteSortMinimizeDeltas(const uint32_t* const palette_sorted,
int num_colors, uint32_t* const palette);
// Implements the modified Zeng method from "A Survey on Palette Reordering
// Methods for Improving the Compression of Color-Indexed Images" by Armando J.
// Pinho and Antonio J. R. Neves. The palette defined by 'palette_in' and
// 'num_colors' is sorted using information from 'pic' and output in
// 'palette'.
// Returns 0 on memory allocation error.
int PaletteSortModifiedZeng(const struct WebPPicture* const pic,
const uint32_t* const palette_in,
uint32_t num_colors, uint32_t* const palette);
// Sorts the palette according to the criterion defined by 'method'.
// 'palette_sorted' is the input palette sorted lexicographically, as done in
// PrepareMapToPalette. Returns 0 on memory allocation error.
int PaletteSort(PaletteSorting method, const struct WebPPicture* pic,
const uint32_t* palette_sorted, uint32_t num_colors,
uint32_t* palette);
#endif // WEBP_UTILS_PALETTE_H_