Commit Graph

185 Commits

Author SHA1 Message Date
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
kris
83b047b73f Whoops, fix a major bug with the iterated image fitting: we don't want
to mutate our source image!

Fix another bug introduced in the previous commit: convert from linear
rgb before quantizing //gs RGB palette since //gs RGB values are in
Rec.601 colour space.

Switch to double for colour_squared_distance and related variables,
not sure if it matters though.

When iterating palette clustering, reject the new palettes if they
would increase the total image error.  This prevents accepting changes
that are local improvements to one palette but which would introduce
more net errors elsewhere when this palette is reused.

This now seems to give monotonic improvements in image quality so no need
to write out intermediate images any more.
2021-11-16 15:44:04 +00:00
kris
8694ab364e Perform conversions in linear RGB space 2021-11-16 12:38:53 +00:00
kris
7ad560247b Clean up 2021-11-16 12:24:43 +00:00
kris
10c829906b Checkpoint
- Repeatedly refit palettes since k-means is only a local
  optimization.  This can produce incremental improvements in image
  quality but may also overfit, especially on complex images.
- use pygame to render incremental images
- Fix off-by-one in palette striping
- When fitting palettes, first cluster a 16-colour palette for the
  entire image and use this to initialize the centroids for individual
  palettes.  This improves quality when fitting images with large
  blocks of colour, since they will otherwise be fit separately and
  may have slight differences.  With a global initializer these will
  tend to be the same.  This also improves performance.
2021-11-16 11:21:53 +00:00
kris
b363d60754 Checkpoint
- switch to pyclustering for kmedians
- allow choosing the same palette as previous line, with a multiplicative penalty to distance in case it's much better
- iterate kmedians multiple times and choose the best, since it's only a local optimum
2021-11-15 09:19:44 +00:00
kris
643e50349e Optimize more 2021-11-13 17:29:13 +00:00
kris
0596aefe0b Use pyclustering for kmedians instead of hand-rolled
Optimize cython code
2021-11-13 17:18:34 +00:00
kris
52af982159 k-means should be using median with L1 norm, otherwise it may not converge
Also optimize a tiny bit
2021-11-13 16:10:33 +00:00
kris
5cab854269 Fit palettes from overlapping line ranges, and map line to palette
when dithering with two limitations:

- cannot choose the same palette as the previous line (this avoids banding)
- must be within +/- 1 of the "base" palette for the line number

This gives pretty good results!
2021-11-11 16:10:03 +00:00
kris
ee2229d0ea * Modify Floyd-Steinberg dithering to diffuse less error in the y
direction.  Otherwise, errors can accumulate in an RGB channel if
  there are no palette colours with an extremal value, and then when
  we introduce a new palette the error all suddenly discharges in a
  spurious horizontal line.  This now gives quite good results!

* Switch to using L1-norm for k-means, per suggestion of Lucas
  Scharenbroich: "A k-medians effectively uses an L1 distance metric
  instead of L2 for k-means.  Using a squared distance metric causes
  the fit to "fall off" too quickly and allows too many of the k
  centroids to cluster around areas of high density, which results in
  many similar colors being selected.  A linear cost function forces
  the centroids to spread out since the error influence has a broader
  range."
2021-11-11 11:10:22 +00:00
kris
8c34d87216 WIP - interleave 3 successive palettes for each contiguous row range.
Avoids the banding but not clear if it's overall better

Also implement my own k-means clustering which is able to keep some
centroids fixed, e.g. to be able to retain some fixed palette entries
while swapping out others.  I was hoping this would improve colour
blending across neighbouring palettes but it's also not clear if it
does.
2021-11-10 18:30:39 +00:00
kris
322123522c Assign scan lines randomly to palettes and cluster independently. This doesn't give good results either, since
neighbouring lines end up getting similar but not identical colours, which still results in horizontal striping.
2021-11-10 00:34:17 +00:00
kris
fb52815412 Experiment with striping 16 palettes contiguously across line ranges.
As expected it has clear banding.  A better approach (though still not optimal)
might be to assign lines to palettes randomly.
2021-11-09 22:42:27 +00:00
kris
80885aabf9 Working SHR version. Still just uses a single palette 2021-11-09 22:26:34 +00:00
kris
21058084e2 Tidy 2021-11-09 16:14:37 +00:00
kris
01b19a4a06 Use 4-bit RGB values instead of 8-bit 2021-11-09 15:35:44 +00:00
kris
a92c9cd7b5 Work in CAM16-UCS colour space and cythonize 2021-11-09 15:13:07 +00:00
kris
173c283369 First implementation of using k-means clustering in RGB space to dither a 320x200 SHR image. 2021-11-09 11:23:25 +00:00
kris
0630db2bf1 More relnotes 2021-11-04 14:55:23 +00:00
kris
f097945b2e Add version history 2021-11-04 14:46:53 +00:00
kris
aebb21263e Update docs 2021-11-04 14:35:14 +00:00