Commit Graph

100 Commits

Author SHA1 Message Date
KrisKennaway 3aa29f2d2c
Add support for hi-res conversions (#11)
Hi-Res is essentially a more constrained version of Double Hi-Res, in which only about half of the 560 horizontal screen pixels can be independently addressed.

In particular an 8 bit byte in screen memory controls 14 or 15 screen pixels.  Bits 0-7 are doubled, and bit 8 shifts these 14 dots to the right if enabled.  In this case bit 7 of the previous byte is repeated a third time.

This means that we have to optimize all 8 bits at once and move forward in increments of 14 screen pixels.

There's also a timing difference that results in a phase shift of the NTSC colour signal, which means the mappings from dot patterns to effective colours are rotated.

Error diffusion seems to give best results if we only distribute about 2/3 of the quantization error according to the dither pattern.
2023-02-03 00:40:32 +00:00
kris 0560409717 Fix --lookahead parsing 2023-01-31 21:28:40 +00:00
kris 39e8eac8ed Add support for DHGR mono conversions 2023-01-21 17:30:27 +00:00
kris 3196369b7d Tidy a bit and add a --save-intermediate flag 2022-07-18 10:00:19 +01:00
kris 99aa394196 Tweak comment 2022-07-16 22:00:14 +01:00
kris a2b67ba882 Require a subcommand 2021-11-26 13:36:29 +00:00
kris 4d5dea2c41 Restore dhr conversion support 2021-11-26 13:15:57 +00:00
kris 0a964b377a Move SHR conversion out into convert_shr in preparation for re-enabling dhr support 2021-11-26 12:35:45 +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 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 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 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 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 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 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