Skip to content

Commit 1fb65e0

Browse files
committed
Change --lossy algorithm to use current colorspace.
* The loss score is scaled to errors in the current colorspace by normalizing to the distance between rgb(0,0,0) and rgb(0,0,LOSS). * Reset accumulated error at the start of each line (don't want to carry error into a different region of the image). * The previous difference algorithm would accumulate differences in the difference score. The new algorithm does not. Rather, the error term is used only to eliminate candidates. A color is a valid candidate iff it is both close enough to the real pixel, and close enough to the real pixel modified by accumulated error. * Rather than choosing the longest sequence with minimum error, compute a score that considers both length and error. The results look reasonable, but the argument to `--lossy` has different effects. For `--gamma=1`, this commit results in better compression than the former commit (1.644M v. 2.202M), on the falling-beecat gif from pull request #16, at the cost of modest posterization (long runs of the same pixel, rather than dithering). For `--gamma=srgb`, this commit compresses worse than the old version, but with fewer artifacts, and `--lossy=40` still looks ok.
1 parent 99f4845 commit 1fb65e0

File tree

5 files changed

+225
-151
lines changed

5 files changed

+225
-151
lines changed

src/gifsicle.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -899,18 +899,14 @@ set_new_fixed_colormap(const char *name)
899899
static void
900900
do_colormap_change(Gif_Stream *gfs)
901901
{
902-
if (active_output_data.colormap_fixed || active_output_data.colormap_size > 0)
903-
kc_set_gamma(active_output_data.colormap_gamma_type,
904-
active_output_data.colormap_gamma);
905-
906902
if (active_output_data.colormap_fixed)
907903
colormap_stream(gfs, active_output_data.colormap_fixed,
908904
&active_output_data);
909905

910906
if (active_output_data.colormap_size > 0) {
911907
kchist kch;
912908
Gif_Colormap* (*adapt_func)(kchist*, Gt_OutputData*);
913-
Gif_Colormap *new_cm;
909+
Gif_Colormap* new_cm;
914910

915911
/* set up the histogram */
916912
{
@@ -1025,6 +1021,8 @@ merge_and_write_frames(const char *outfile, int f1, int f2)
10251021
w = active_output_data.resize_width;
10261022
h = active_output_data.resize_height;
10271023
}
1024+
kc_set_gamma(active_output_data.colormap_gamma_type,
1025+
active_output_data.colormap_gamma);
10281026
if (active_output_data.scaling != GT_SCALING_NONE)
10291027
resize_stream(out, w, h, active_output_data.resize_flags,
10301028
active_output_data.scale_method,

src/gifsicle.h

+6
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ enum {
280280
int set_dither_type(Gt_OutputData* od, const char* name);
281281
void colormap_stream(Gif_Stream*, Gif_Colormap*, Gt_OutputData*);
282282

283+
struct kchist;
284+
Gif_Colormap* colormap_blend_diversity(struct kchist* kch, Gt_OutputData* od);
285+
Gif_Colormap* colormap_flat_diversity(struct kchist* kch, Gt_OutputData* od);
286+
Gif_Colormap* colormap_median_cut(struct kchist* kch, Gt_OutputData* od);
287+
288+
283289
/*****
284290
* parsing stuff
285291
**/

0 commit comments

Comments
 (0)