From 0804bb7e067e66d4b05a6c45f9736b1e20505b96 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Fri, 28 Sep 2012 11:19:34 +0100 Subject: Add error diffusion to palette based rendering. Only used for bitmap and scaled bitmap plots. Doesn't do serpentine path, since that would need changes to the common bitmap rendering code. --- src/palette.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/palette.c') diff --git a/src/palette.c b/src/palette.c index eba95cd..d600001 100644 --- a/src/palette.c +++ b/src/palette.c @@ -13,12 +13,13 @@ #include #include #include +#include #include "palette.h" /** Create an empty palette object. */ -bool nsfb_palette_new(struct nsfb_palette_s **palette) +bool nsfb_palette_new(struct nsfb_palette_s **palette, int width) { *palette = malloc(sizeof(struct nsfb_palette_s)); if (*palette == NULL) { @@ -28,14 +29,41 @@ bool nsfb_palette_new(struct nsfb_palette_s **palette) (*palette)->type = NSFB_PALETTE_EMPTY; (*palette)->last = 0; + (*palette)->dither = false; + (*palette)->dither_ctx.data_len = width * 3; + (*palette)->dither_ctx.data = malloc(width * 3 * sizeof(int)); + if ((*palette)->dither_ctx.data == NULL) { + nsfb_palette_free(*palette); + return false; + } + return true; } /** Free a palette object. */ void nsfb_palette_free(struct nsfb_palette_s *palette) { - if (palette != NULL) + if (palette != NULL) { + if (palette->dither_ctx.data != NULL) { + free(palette->dither_ctx.data); + } free(palette); + } +} + +/** Init error diffusion for a plot. */ +void nsfb_palette_dither_init(struct nsfb_palette_s *palette, int width) +{ + palette->dither = true; + memset(palette->dither_ctx.data, 0, palette->dither_ctx.data_len); + palette->dither_ctx.width = width * 3; + palette->dither_ctx.current = 0; +} + +/** Finalise error diffusion after a plot. */ +void nsfb_palette_dither_fini(struct nsfb_palette_s *palette) +{ + palette->dither = false; } /** Generate libnsfb 8bpp default palette. */ -- cgit v1.2.1