From 211d930c886a0796992a85d301c5ec9b7fc4a67c Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Thu, 8 Jul 2021 12:05:10 +0000 Subject: [PATCH] feat(cli/quantize): preserve transparency v0.2.0 --- README.md | 2 +- cli.c | 4 ++++ hicolor.h | 2 +- tests/hicolor.test | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6221925..2f20cbd 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cli.c b/cli.c index 3984304..866309f 100644 --- a/cli.c +++ b/cli.c @@ -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; diff --git a/hicolor.h b/hicolor.h index a2ecdbe..c4479a4 100644 --- a/hicolor.h +++ b/hicolor.h @@ -6,7 +6,7 @@ #include #define HICOLOR_BAYER_SIZE 8 -#define HICOLOR_LIBRARY_VERSION 101 +#define HICOLOR_LIBRARY_VERSION 200 /* Types. */ diff --git a/tests/hicolor.test b/tests/hicolor.test index edc958c..5d91698 100755 --- a/tests/hicolor.test +++ b/tests/hicolor.test @@ -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