Commit Graph

206 Commits

Author SHA1 Message Date
kris ae89682dab Split out common utility functions into a shared module 2021-11-26 12:26:46 +00:00
kris 0dc2c0a7a0 Disable bounds checking and wraparound by default 2021-11-26 12:12:55 +00:00
kris 4221c00701 Split dither into dither_dhr and dither_shr 2021-11-26 12:08:48 +00:00
kris 1075ff0136 Tidy a bit and remove support for tunable parameters that are no longer needed 2021-11-26 10:36:39 +00:00
kris cf63a35797 Cython tweaks to remove some unnecessary C code 2021-11-26 09:54:42 +00:00
kris 25e6ed7b88 Preserve palette order when deduplicating entries
Also make sure we're not mutating _global_palettes, though this should
currently be harmless.
2021-11-25 21:57:27 +00:00
kris 61b4cbb184 Tweak k-means convergence criterion to return once the total centroid position error stops decreasing. 2021-11-25 21:33:12 +00:00
kris fc35387360 - Fill any palettes that have fewer than 16 unique entries after
clustering, using the most frequent pixel colours that are not yet
  in the palette

- Reassign any palettes that are duplicated after clustering
2021-11-25 13:14:22 +00:00
kris ad50ed103d Improvements to image quality:
- Preprocess the source image by dithering with the full 12-bit //gs
  colour palette, ignoring SHR palette restrictions (i.e. each pixel
  chosen independently from 4096 colours)

- Using this as the ground truth allows much better handling of
  e.g. solid colours, which were being dithered inconsistently with
  the previous approach

- Also when fitting an SHR palette, fix any colours that comprise more
  than 10% of source pixels.  This also encourages more uniformity in
  regions of solid colour.
2021-11-25 11:46:42 +00:00
kris 870c008827 Parametrize quantization error decay and minimum value. The latter
helps with images where there are large solid colour fields that
sometimes cause uneven dithering because of colours that cannot be
matched with the //gs palette, but it's not a viable solution in
general since it reduces overall quality (sometimes substantially,
e.g. in case of vertical colour gradients)
2021-11-25 09:09:40 +00:00
kris 8b5c3dc6c1 Fix bool flags 2021-11-24 16:03:55 +00:00
kris 9a77af37aa Add a --show-final-score to output the final image quality score.
This is useful when used as part of an image repository build
pipeline, to avoid replacing existing images if the new score is
higher.

Hide intermediate output behind --verbose
2021-11-24 15:49:56 +00:00
kris 0036ee9522 Add default values to help 2021-11-24 15:44:37 +00:00
kris 8d3ab4f50e Add the ability to disable saving preview images. Also rename --gamma_correct to --gamma-correct for consistency 2021-11-24 15:41:32 +00:00
kris 8175dcb052 Add --fixed-colours to control how many colours will be kept identical
across all 16 SHR palettes.
2021-11-24 15:27:34 +00:00
kris 5fefd0b0bb Don't initialize pygame if --no-show-output 2021-11-24 15:24:58 +00:00
kris e77e7abd43 Rename 2021-11-24 15:24:45 +00:00
kris d645cc5964 Tidy 2021-11-24 15:21:50 +00:00
kris c36de2b76b When initializing centroids for fitting the SHR palettes, only use the
reserved colours from the global palette, and pick unique random
points from the samples for the rest.  This encourages a larger range
of colours in the resulting images and may improve quality.

Iterate a max number of times without improvement in the outer loop as
well.

Save intermediate preview outputs.
2021-11-24 14:57:24 +00:00
kris 3b8767782b Each run seems to converge fairly quickly but there is a lot of variation across runs. Run in a loop and keep the running best. 2021-11-24 11:47:39 +00:00
kris de8a303de2 Initial attempt at fitting palettes to arbitrary lines instead of line ranges.
Works OK but isn't converging as well as I hoped.
2021-11-24 10:41:25 +00:00
kris 50c71d3a35 Whitespace 2021-11-24 09:19:35 +00:00
kris 04fd4f7427 Move reassigning palettes back to after fitting, otherwise it does the
wrong thing the first time.

Fix an off by one when splitting palette ranges
2021-11-24 09:18:59 +00:00
kris 62f23ff910 Don't mutate initial_centroids 2021-11-24 09:10:03 +00:00
kris 7179d009e1 Refactor
Reassign palettes before computing new ones instead of after
2021-11-23 15:09:12 +00:00
kris e488955c23 Reorder 2021-11-23 14:58:46 +00:00
kris 0b985a66b9 Reorder and tidy 2021-11-23 14:58:09 +00:00
kris c78f731cd7 Refactor 2021-11-23 14:55:45 +00:00
kris 0323b80e68 Refactor 2021-11-23 14:51:04 +00:00
kris 6988b19b43 Tidy 2021-11-23 14:00:57 +00:00
kris 1ce5c25764 Fix a bug where _fit_global_palette would crash if there were fewer
than 16 global colours computed.
2021-11-23 13:59:48 +00:00
kris 6e52680cf1 Dynamically tune the line ranges used to fit the 16 SHR palettes:
- start with an equal split
- with each iteration, pick a palette and adjust its line ranges by a small random amount
- if the proposed palette is accepted, continue to apply the same delta
- if not, revert the adjustment and pick a different one

In addition, often there will be palettes that are entirely unused by
the image.  For such palettes:

- find the palette with the largest line range.  If > 20, then
  subdivide this range and assign half each to both palettes
- if not, then pick a random line range for the unused palette

This helps to refine and explore more of the parameter space.
2021-11-23 13:01:50 +00:00
kris 189b4655ad Since fixing the bug in the previous commit there is no longer a need
to limit to neighbouring palettes (which was unaware of the dynamic
line splits anyway)
2021-11-23 12:49:37 +00:00
kris be55fb859d - Fix a serious bug in best_palette_for_line which was not actually computing the palette with lowest per-row error, rather the lowest per-pixel error!
- Tidy a bit
2021-11-23 12:46:36 +00:00
kris b78c42e287 Fix rounding 2021-11-18 22:35:15 +00:00
kris b1d3488182 Actually use equal-sized palette splits. With the previous version
the first and last were smaller.
2021-11-18 22:27:19 +00:00
kris 9e46ca48a0 Refactor to extract palette splits in preparation for tuning them dynamically 2021-11-18 22:08:09 +00:00
kris cfc150ed13 Remove some dead code 2021-11-18 22:03:18 +00:00
kris c608f6b961 Optimize calling _convert_cam16ucs_to_rgb12_iigs since it has
significant overhead
2021-11-18 21:50:39 +00:00
kris 3159a09c27 Uncomment 2021-11-18 20:33:21 +00:00
kris 7609297f0d Optimize a bit 2021-11-18 17:34:27 +00:00
kris d7969f50ba Remove cython checks and obsolete TODO 2021-11-18 17:24:12 +00:00
kris e53c085a91 Remove debugging prints 2021-11-17 22:55:47 +00:00
kris ed2082344a Working version! Quantize the k-means centroids in 12-bit //gs RGB
space but continue to use CAM16-UCS for distances and updating
centroid positions, before mapping back to the nearest legal 12-bit
RGB position.

Needs some more work to deal with the fact that now that there are
discrete distances (but no fixed minimum) between allowed centroid
positions, the previous notion of convergence doesn't apply.  Actually
the centroids can oscillate between positions.

There is room for optimization but this is already reasonably
performant, and the image quality is much higher \o/
2021-11-17 22:49:06 +00:00
kris 0009ce8913 - allow reserving a number of colours which are to be shared across
all palettes.  This will be useful for Total Replay which does an
  animation effect when displaying the image (first set palettes, then
  transition in pixels)

- this requires us to go back to computing k-means ourself instead of
  using sklearn, since it can't keep some centroids fixed

- try to be more careful about //gs RGB values, which are in the
  Rec.601 colour space.  This isn't quite right yet - the issue seems
  to be that since we dither in linear RGB space but quantize in the
  nonlinear space, small differences may lead to a +/- 1 in the 4-bit
  //gs RGB value, which is quite noticeable.  Instead we need to be
  clustering and/or dithering with awareness of the quantized palette
  space.
2021-11-17 17:09:42 +00:00
kris f2f07ddc04 Refactor and add comments 2021-11-16 23:45:11 +00:00
kris bb70eea7b0 Cleanup 2021-11-16 21:07:13 +00:00
kris 613a36909c Suppress pygame message at startup
Keep iterating until N iterations without quality improvement
2021-11-16 17:23:31 +00:00
kris 5111696d5c Compute number of unique colours. This does not seem to strongly
depend on the width of the palette sampling.

Note the potential issue that since we are clustering in CAM space but
then quantizing a (much coarser) 4-bit RGB value we could end up
picking multiple centroids that will be represented by the same RGB
value.  This doesn't seem to be a major issue though (e.g. 3-4 lost
colours per typical image)
2021-11-16 16:57:44 +00:00
kris 91e4fd7cba Add comment 2021-11-16 15:50:19 +00:00