| 1 | // Copyright 2023 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Utilities for palette analysis. |
| 11 | // |
| 12 | // Author: Vincent Rabaud ([email protected]) |
| 13 | |
| 14 | #ifndef WEBP_UTILS_PALETTE_H_ |
| 15 | #define WEBP_UTILS_PALETTE_H_ |
| 16 | |
| 17 | #include "src/webp/types.h" |
| 18 | |
| 19 | struct WebPPicture; |
| 20 | |
| 21 | // The different ways a palette can be sorted. |
| 22 | typedef enum PaletteSorting { |
| 23 | kSortedDefault = 0, |
| 24 | // Sorts by minimizing L1 deltas between consecutive colors, giving more |
| 25 | // weight to RGB colors. |
| 26 | kMinimizeDelta = 1, |
| 27 | // Implements the modified Zeng method from "A Survey on Palette Reordering |
| 28 | // Methods for Improving the Compression of Color-Indexed Images" by Armando |
| 29 | // J. Pinho and Antonio J. R. Neves. |
| 30 | kModifiedZeng = 2, |
| 31 | kUnusedPalette = 3, |
| 32 | kPaletteSortingNum = 4 |
| 33 | } PaletteSorting; |
| 34 | |
| 35 | // Returns the index of 'color' in the sorted palette 'sorted' of size |
| 36 | // 'num_colors'. |
| 37 | int SearchColorNoIdx(const uint32_t sorted[], uint32_t color, int num_colors); |
| 38 | |
| 39 | // Sort palette in increasing order and prepare an inverse mapping array. |
| 40 | void PrepareMapToPalette(const uint32_t palette[], uint32_t num_colors, |
| 41 | uint32_t sorted[], uint32_t idx_map[]); |
| 42 | |
| 43 | // Returns count of unique colors in 'pic', assuming pic->use_argb is true. |
| 44 | // If the unique color count is more than MAX_PALETTE_SIZE, returns |
| 45 | // MAX_PALETTE_SIZE+1. |
| 46 | // If 'palette' is not NULL and the number of unique colors is less than or |
| 47 | // equal to MAX_PALETTE_SIZE, also outputs the actual unique colors into |
| 48 | // 'palette' in a sorted order. Note: 'palette' is assumed to be an array |
| 49 | // already allocated with at least MAX_PALETTE_SIZE elements. |
| 50 | int GetColorPalette(const struct WebPPicture* const pic, |
| 51 | uint32_t* const palette); |
| 52 | |
| 53 | // Sorts the palette according to the criterion defined by 'method'. |
| 54 | // 'palette_sorted' is the input palette sorted lexicographically, as done in |
| 55 | // PrepareMapToPalette. Returns 0 on memory allocation error. |
| 56 | int PaletteSort(PaletteSorting method, const struct WebPPicture* const pic, |
| 57 | const uint32_t* const palette_sorted, uint32_t num_colors, |
| 58 | uint32_t* const palette); |
| 59 | |
| 60 | #endif // WEBP_UTILS_PALETTE_H_ |