-
Notifications
You must be signed in to change notification settings - Fork 131
/
hist.rs
416 lines (349 loc) · 14.5 KB
/
hist.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::error::*;
use crate::image::Image;
use crate::pal::MAX_COLORS;
use crate::pal::PalIndex;
use crate::pal::ARGBF;
use crate::pal::{f_pixel, gamma_lut, RGBA};
use crate::quant::QuantizationResult;
use crate::rows::temp_buf;
use crate::rows::DynamicRows;
use crate::Attributes;
use fallible_collections::FallibleVec;
use rgb::ComponentSlice;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::fmt;
use std::hash::Hash;
use std::os::raw::c_uint;
/// Number of pixels in a given color for [`Histogram::add_colors()`]
///
/// Used for building a histogram manually. Otherwise see [`Histogram::add_image()`]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct HistogramEntry {
/// The color
pub color: RGBA,
/// Importance of the color (e.g. number of occurrences)
pub count: c_uint,
}
/// Generate one shared palette for multiple images
///
/// If you're converting one image at a time, see [`Attributes::new_image`] instead
pub struct Histogram {
gamma: Option<f64>,
fixed_colors: FixedColorsSet,
/// maps RGBA as u32 to (boosted) count
hashmap: HashMap<u32, (u32, RGBA), RgbaHasher>,
/// how many pixels were counted
total_area: usize,
posterize_bits: u8,
max_histogram_entries: u32,
}
pub(crate) type FixedColorsSet = HashSet<HashColor, RgbaHasher>;
#[derive(Clone)]
pub(crate) struct HistItem {
pub color: f_pixel,
pub adjusted_weight: f32,
pub perceptual_weight: f32,
pub mc_color_weight: f32,
pub tmp: HistSortTmp,
}
impl HistItem {
// Safety: just an int, and it's been initialized when constructing the object
#[inline(always)]
pub fn mc_sort_value(&self) -> u32 {
unsafe { self.tmp.mc_sort_value }
}
}
impl fmt::Debug for HistItem {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HistItem")
.field("color", &self.color)
.field("adjusted_weight", &self.adjusted_weight)
.field("perceptual_weight", &self.perceptual_weight)
.field("color_weight", &self.mc_color_weight)
.finish()
}
}
#[derive(Clone, Copy)]
pub union HistSortTmp {
pub mc_sort_value: u32,
pub likely_palette_index: PalIndex,
}
impl Histogram {
/// Creates histogram object that will be used to collect color statistics from multiple images.
///
/// All options should be set on `attr` before the histogram object is created. Options changed later may not have effect.
#[inline]
#[must_use]
pub fn new(attr: &Attributes) -> Self {
Self {
posterize_bits: attr.posterize_bits(),
max_histogram_entries: attr.max_histogram_entries,
fixed_colors: HashSet::with_hasher(RgbaHasher(0)),
hashmap: HashMap::with_hasher(RgbaHasher(0)),
gamma: None,
total_area: 0,
}
}
/// "Learns" colors from the image, which will be later used to generate the palette.
///
/// Fixed colors added to the image are also added to the histogram. If the total number of fixed colors exceeds 256,
/// this function will fail with `LIQ_BUFFER_TOO_SMALL`.
#[inline(never)]
pub fn add_image(&mut self, attr: &Attributes, image: &mut Image) -> Result<(), Error> {
let width = image.width();
let height = image.height();
if image.importance_map.is_none() && attr.use_contrast_maps {
image.contrast_maps()?;
}
self.gamma = Some(image.gamma());
for c in image.fixed_colors.iter().copied() {
self.fixed_colors.insert(HashColor(c));
}
if attr.progress(attr.progress_stage1 as f32 * 0.40) {
return Err(Aborted); // bow can free the RGBA source if copy has been made in f_pixels
}
let posterize_bits = attr.posterize_bits();
let surface_area = height * width;
let estimated_colors = (surface_area / (posterize_bits as usize + if surface_area > 512 * 512 { 7 } else { 5 })).min(250_000);
self.reserve(estimated_colors);
let importance_map = image.importance_map.as_ref().map(|m| m.as_slice());
self.add_pixel_rows(&mut image.px, importance_map, posterize_bits)?;
image.free_histogram_inputs();
Ok(())
}
/// Alternative to `add_image()`. Intead of counting colors in an image, it directly takes an array of colors and their counts.
///
/// This function is only useful if you already have a histogram of the image from another source.
#[inline(never)]
pub fn add_colors(&mut self, entries: &[HistogramEntry], gamma: f64) -> Result<(), Error> {
if entries.is_empty() || entries.len() > 1 << 24 {
return Err(ValueOutOfRange);
}
if !(0. ..1.).contains(&gamma) {
return Err(ValueOutOfRange);
}
self.gamma = Some(if gamma > 0. { gamma } else { 0.45455 });
self.reserve(entries.len());
self.total_area += entries.len();
for e in entries {
self.add_color(e.color, e.count.try_into().unwrap_or(u16::MAX));
}
Ok(())
}
/// Add a color guaranteed to be in the final palette
pub fn add_fixed_color(&mut self, color: RGBA, gamma: f64) -> Result<(), Error> {
let lut = gamma_lut(if gamma > 0. { gamma } else { 0.45455 });
let px = f_pixel::from_rgba(&lut, RGBA{r: color.r, g: color.g, b: color.b, a: color.a,});
if self.fixed_colors.len() >= MAX_COLORS {
return Err(Unsupported);
}
self.fixed_colors.insert(HashColor(px));
Ok(())
}
/// Generate palette for all images/colors added to the histogram.
///
/// Palette generated using this function won't be improved during remapping.
/// If you're generating palette for only one image, it's better not to use the `Histogram`.
#[inline]
pub fn quantize(&mut self, attr: &Attributes) -> Result<QuantizationResult, Error> {
self.quantize_internal(attr, true)
}
#[inline(never)]
pub(crate) fn quantize_internal(&mut self, attr: &Attributes, freeze_result_colors: bool) -> Result<QuantizationResult, Error> {
if self.hashmap.is_empty() && self.fixed_colors.is_empty() {
return Err(Unsupported);
}
if attr.progress(0.) { return Err(Aborted); }
if attr.progress(attr.progress_stage1 as f32 * 0.89) {
return Err(Aborted);
}
let gamma = self.gamma.unwrap_or(0.45455);
let (_, target_mse, _) = attr.target_mse(self.hashmap.len());
let hist = self.finalize_builder(gamma, target_mse).map_err(|_| OutOfMemory)?;
attr.verbose_print(format!(" made histogram...{} colors found", hist.items.len()));
QuantizationResult::new(attr, hist, freeze_result_colors, &self.fixed_colors, gamma)
}
#[inline(always)]
fn add_color(&mut self, rgba: RGBA, boost: u16) {
let px_int = if rgba.a != 0 {
self.posterize_mask() & unsafe { RGBAInt { rgba }.int }
} else { 0 };
self.hashmap.entry(px_int)
.and_modify(move |e| e.0 += boost as u32)
.or_insert((boost as u32, rgba));
}
fn reserve(&mut self, entries: usize) {
let new_entries = entries.saturating_sub(self.hashmap.len() / 3); // assume some will be dupes, if called multiple times
self.hashmap.reserve(new_entries);
}
#[inline(always)]
fn posterize_mask(&self) -> u32 {
let channel_mask = 255 << self.posterize_bits;
u32::from_ne_bytes([channel_mask, channel_mask, channel_mask, channel_mask])
}
/// optionallys et
fn init_posterize_bits(&mut self, posterize_bits: u8) {
if self.posterize_bits >= posterize_bits {
return;
}
self.posterize_bits = posterize_bits;
let new_posterize_mask = self.posterize_mask();
let new_size = (self.hashmap.len()/3).max(self.hashmap.capacity()/5);
let old_hashmap = std::mem::replace(&mut self.hashmap, HashMap::with_capacity_and_hasher(new_size, RgbaHasher(0)));
self.hashmap.extend(old_hashmap.into_iter().map(move |(k, v)| {
(k & new_posterize_mask, v)
}));
}
pub(crate) fn add_pixel_rows(&mut self, image: &mut DynamicRows<'_, '_>, importance_map: Option<&[u8]>, posterize_bits: u8) -> Result<(), Error> {
let width = image.width as usize;
let height = image.height as usize;
self.total_area += width * height;
let mut importance_map = importance_map.unwrap_or(&[]).chunks_exact(width).fuse();
let image_iter = image.rgba_rows_iter()?;
let mut temp_row = temp_buf(width)?;
for row in 0..height {
let pixels_row = &image_iter.row_rgba(&mut temp_row, row)[..width];
let importance_map = importance_map.next().map(move |m| &m[..width]);
for (col, px) in pixels_row.iter().copied().enumerate() {
self.add_color(px, importance_map.map(move |map| map[col]).unwrap_or(255) as u16);
}
}
self.init_posterize_bits(posterize_bits);
if self.hashmap.len() > self.max_histogram_entries as usize && self.posterize_bits < 3 {
self.init_posterize_bits(self.posterize_bits + 1);
}
Ok(())
}
pub(crate) fn finalize_builder(&mut self, gamma: f64, target_mse: f64) -> Result<HistogramInternal, Error> {
debug_assert!(gamma > 0.);
let mut counts = [0; LIQ_MAXCLUSTER];
let mut temp: Vec<_> = FallibleVec::try_with_capacity(self.hashmap.len())?;
// Limit perceptual weight to 1/10th of the image surface area to prevent
// a single color from dominating all others.
let max_perceptual_weight = 0.1 * self.total_area as f32;
let max_fixed_color_difference = (target_mse / 2.).max(2. / 256. / 256.) as f32;
let lut = gamma_lut(gamma);
let total_perceptual_weight = self.hashmap.values().map(|&(boost, color)| {
if boost == 0 && !temp.is_empty() {
return 0.;
}
let cluster_index = (((color.r >> 7) << 3) | ((color.g >> 7) << 2) | ((color.b >> 7) << 1) | (color.a >> 7)) as u8;
let weight = (boost as f32 / 170.).min(max_perceptual_weight);
if weight == 0. {
return 0.;
}
let color = f_pixel::from_rgba(&lut, color);
// fixed colors are always included in the palette, so it would be wasteful to duplicate them in palette from histogram
// FIXME: removes fixed colors from histogram (could be done better by marking them as max importance instead)
for HashColor(fixed) in &self.fixed_colors {
if color.diff(fixed) < max_fixed_color_difference {
return 0.;
}
}
counts[cluster_index as usize] += 1;
temp.push(TempHistItem {
color, cluster_index, weight,
});
weight as f64
}).sum::<f64>();
let mut clusters = [Cluster { begin: 0, end: 0 }; LIQ_MAXCLUSTER];
let mut next_begin = 0;
for (cluster, count) in clusters.iter_mut().zip(counts) {
cluster.begin = next_begin;
cluster.end = next_begin;
next_begin += count;
}
let mut items: Vec<_> = FallibleVec::try_with_capacity(temp.len())?;
items.resize(temp.len(), HistItem {
color: if cfg!(debug_assertions) { f_pixel( ARGBF { r:f32::NAN, g:f32::NAN, b:f32::NAN, a:f32::NAN } ) } else { f_pixel::default() },
adjusted_weight: if cfg!(debug_assertions) { f32::NAN } else { 0. },
perceptual_weight: if cfg!(debug_assertions) { f32::NAN } else { 0. },
mc_color_weight: if cfg!(debug_assertions) { f32::NAN } else { 0. },
tmp: HistSortTmp { mc_sort_value: 0 },
});
let mut items = items.into_boxed_slice();
for temp_item in temp {
let cluster = &mut clusters[temp_item.cluster_index as usize];
let next_index = cluster.end as usize;
cluster.end += 1;
items[next_index].color = temp_item.color;
items[next_index].perceptual_weight = temp_item.weight;
items[next_index].adjusted_weight = temp_item.weight;
}
Ok(HistogramInternal {
items,
clusters,
total_perceptual_weight,
})
}
}
#[derive(Copy, Clone)]
struct TempHistItem {
color: f_pixel, // FIXME: RGBA would be more efficient?
weight: f32,
cluster_index: u8,
}
union RGBAInt {
rgba: RGBA,
int: u32,
}
/// Clusters form initial boxes for quantization, to ensure extreme colors are better represented
pub const LIQ_MAXCLUSTER: usize = 16;
pub(crate) struct HistogramInternal {
pub items: Box<[HistItem]>,
pub total_perceptual_weight: f64,
pub clusters: [Cluster; LIQ_MAXCLUSTER],
}
// Pre-grouped colors
#[derive(Copy, Clone, Debug)]
pub(crate) struct Cluster {
pub begin: u32,
pub end: u32,
}
// Simple deterministic hasher for the color hashmap
impl std::hash::BuildHasher for RgbaHasher {
type Hasher = Self;
#[inline(always)]
fn build_hasher(&self) -> Self {
Self(0)
}
}
pub(crate) struct RgbaHasher(pub u32);
impl std::hash::Hasher for RgbaHasher {
// magic constant from fxhash. For a single 32-bit key that's all it needs!
#[inline(always)]
fn finish(&self) -> u64 { (self.0 as u64).wrapping_mul(0x517cc1b727220a95) }
#[inline(always)]
fn write_u32(&mut self, i: u32) { self.0 = i; }
fn write(&mut self, _bytes: &[u8]) { unimplemented!() }
fn write_u8(&mut self, _i: u8) { unimplemented!() }
fn write_u16(&mut self, _i: u16) { unimplemented!() }
fn write_u64(&mut self, _i: u64) { unimplemented!() }
fn write_u128(&mut self, _i: u128) { unimplemented!() }
fn write_usize(&mut self, _i: usize) { unimplemented!() }
fn write_i8(&mut self, _i: i8) { unimplemented!() }
fn write_i16(&mut self, _i: i16) { unimplemented!() }
fn write_i32(&mut self, _i: i32) { unimplemented!() }
fn write_i64(&mut self, _i: i64) { unimplemented!() }
fn write_i128(&mut self, _i: i128) { unimplemented!() }
fn write_isize(&mut self, _i: isize) { unimplemented!() }
}
/// libstd's HashSet is afraid of NaN
#[repr(transparent)]
#[derive(PartialEq, Debug)]
pub(crate) struct HashColor(pub f_pixel);
#[allow(clippy::derive_hash_xor_eq)]
impl Hash for HashColor {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
for c in self.0.as_slice() {
u32::from_ne_bytes(c.to_ne_bytes()).hash(state);
}
}
}
impl Eq for HashColor {
fn assert_receiver_is_total_eq(&self) {}
}