Skip to content

Commit

Permalink
feat(cli/quantize): preserve transparency
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
dbohdan committed Jul 8, 2021
1 parent 6a86d8e commit 211d930
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ HiColor is a program for converting images to 15- and 16-bit RGB color, the colo

I wrote this program because I wanted to create images with the characteristic high-color look, and nothing seemed to support high color. It implements its own simple [file format](format.md) and converts between this format and PNG. It can also convert normal PNG to normal 32-bit PNG with only high color color values. (This simulates a roundtrip through HiColor without creating a temporary file.) To reduce the quantization error (the difference between the original and the high-color pixel), HiColor uses the period-appropriate [Bayer ordered dithering](https://bisqwit.iki.fi/story/howto/dither/jy/#StandardOrderedDitheringAlgorithm) algorithm. Dithering can be disabled with a command line flag. HiColor files have the extension `.hic` or `.hi5` for 15-bit and `.hi6` for 16-bit.

Quantized images compress better when their originals, so HiColor may serve as a less-lossy alternative to the 256-color [pngquant](https://pngquant.org/). However, unlike pngquant, it does not preserve transparency.
Quantized images compress better when their originals, so HiColor may serve as a less-lossy alternative to the 256-color [pngquant](https://pngquant.org/). Quantizing a PNG file to PNG preserves transparency (but does not quantize the alpha channel). Conversion to and from the HiColor format does not preserve transparency.

The program is written in C with minimal dependencies and builds as a static binary by default. It works at least on Linux and Windows 98 Second Edition, 2000 Service Pack 4, XP, and 7.

Expand Down
4 changes: 4 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ bool png_quantize(
}

cp_image_t quant_png_img = rgb_to_cp(meta, rgb_img);
/* Restore the alpha channel. */
for (uint32_t i = 0; i < (uint32_t) png_img.w * (uint32_t) png_img.h; i++) {
quant_png_img.pix[i].a = png_img.pix[i].a;
}
if (!cp_save_png(dest, &quant_png_img)) {
fprintf(stderr, "can't save PNG\n");
goto clean_up_quant_image;
Expand Down
2 changes: 1 addition & 1 deletion hicolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdio.h>

#define HICOLOR_BAYER_SIZE 8
#define HICOLOR_LIBRARY_VERSION 101
#define HICOLOR_LIBRARY_VERSION 200

/* Types. */

Expand Down
5 changes: 5 additions & 0 deletions tests/hicolor.test
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ tcltest::test data-integrity-1.1 {roundtrip} -constraints gm -body {
exec gm compare -metric rmse photo.png temp.png
} -match glob -result {*Total: 0.01*}

tcltest::test data-integrity-2.1 {alpha roundtrip} -constraints gm -body {
hicolor quantize alpha.png alpha-q.png
exec gm compare -metric rmse alpha.png alpha-q.png
} -match regexp -result {Total: 0.0+ }


incr failed [expr {$tcltest::numTests(Failed) > 0}]
tcltest::cleanupTests
Expand Down

0 comments on commit 211d930

Please sign in to comment.