static int test1(void) { sixel_dither_t *dither = NULL; int nret = EXIT_FAILURE; #if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif dither = sixel_dither_create(0); #if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS # pragma GCC diagnostic pop #endif if (dither == NULL) { goto error; } sixel_dither_ref(dither); sixel_dither_unref(dither); nret = EXIT_SUCCESS; error: sixel_dither_unref(dither); return nret; }
static SIXELSTATUS output_sixel(unsigned char *pixbuf, int width, int height, int ncolors, int pixelformat) { sixel_output_t *context; sixel_dither_t *dither; SIXELSTATUS status; #if USE_OSMESA pixelformat = SIXEL_PIXELFORMAT_RGBA8888; #endif context = sixel_output_create(sixel_write, stdout); dither = sixel_dither_create(ncolors); status = sixel_dither_initialize(dither, pixbuf, width, height, pixelformat, SIXEL_LARGE_AUTO, SIXEL_REP_AUTO, SIXEL_QUALITY_AUTO); if (SIXEL_FAILED(status)) return status; status = sixel_encode(pixbuf, width, height, pixelformat, dither, context); if (SIXEL_FAILED(status)) return status; sixel_output_unref(context); sixel_dither_unref(dither); return status; }
static int test2(void) { sixel_dither_t *dither = NULL; int colors; int nret = EXIT_FAILURE; #if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif dither = sixel_dither_create(INT_MAX); #if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS # pragma GCC diagnostic pop #endif if (dither == NULL) { goto error; } sixel_dither_set_body_only(dither, 1); colors = sixel_dither_get_num_of_histogram_colors(dither); if (colors != -1) { goto error; } nret = EXIT_SUCCESS; error: sixel_dither_unref(dither); return nret; }
SIXELAPI unsigned char * sixel_dither_apply_palette( sixel_dither_t /* in */ *dither, unsigned char /* in */ *pixels, int /* in */ width, int /* in */ height) { SIXELSTATUS status = SIXEL_FALSE; int bufsize; unsigned char *dest = NULL; int ncolors; unsigned char *normalized_pixels = NULL; unsigned char *input_pixels; if (dither == NULL) { sixel_helper_set_additional_message( "sixel_dither_apply_palette: dither is null."); status = SIXEL_BAD_ARGUMENT; goto end; } sixel_dither_ref(dither); bufsize = width * height * sizeof(unsigned char); dest = (unsigned char *)sixel_allocator_malloc(dither->allocator, bufsize); if (dest == NULL) { sixel_helper_set_additional_message( "sixel_dither_new: sixel_allocator_malloc() failed."); status = SIXEL_BAD_ALLOCATION; goto end; } /* if quality_mode is full, do not use palette caching */ if (dither->quality_mode == SIXEL_QUALITY_FULL) { dither->optimized = 0; } if (dither->cachetable == NULL && dither->optimized) { if (dither->palette != pal_mono_dark && dither->palette != pal_mono_light) { dither->cachetable = (unsigned short *)sixel_allocator_calloc(dither->allocator, 1 << 3 * 5, sizeof(unsigned short)); if (dither->cachetable == NULL) { sixel_helper_set_additional_message( "sixel_dither_new: sixel_allocator_calloc() failed."); status = SIXEL_BAD_ALLOCATION; goto end; } } } if (dither->pixelformat != SIXEL_PIXELFORMAT_RGB888) { /* normalize pixelformat */ normalized_pixels = (unsigned char *)sixel_allocator_malloc(dither->allocator, width * height * 3); if (normalized_pixels == NULL) { sixel_helper_set_additional_message( "sixel_dither_new: sixel_allocator_malloc() failed."); status = SIXEL_BAD_ALLOCATION; goto end; } status = sixel_helper_normalize_pixelformat(normalized_pixels, &dither->pixelformat, pixels, dither->pixelformat, width, height); if (SIXEL_FAILED(status)) { goto end; } input_pixels = normalized_pixels; } else { input_pixels = pixels; } status = sixel_quant_apply_palette(dest, input_pixels, width, height, 3, dither->palette, dither->ncolors, dither->method_for_diffuse, dither->optimized, dither->optimize_palette, dither->complexion, dither->cachetable, &ncolors, dither->allocator); if (SIXEL_FAILED(status)) { free(dest); dest = NULL; goto end; } dither->ncolors = ncolors; end: free(normalized_pixels); sixel_dither_unref(dither); return dest; }
SIXELAPI SIXELSTATUS sixel_dither_initialize( sixel_dither_t /* in */ *dither, unsigned char /* in */ *data, int /* in */ width, int /* in */ height, int /* in */ pixelformat, int /* in */ method_for_largest, int /* in */ method_for_rep, int /* in */ quality_mode) { unsigned char *buf = NULL; unsigned char *normalized_pixels = NULL; unsigned char *input_pixels; SIXELSTATUS status = SIXEL_FALSE; if (dither == NULL) { sixel_helper_set_additional_message( "sixel_dither_new: dither is null."); status = SIXEL_BAD_ARGUMENT; goto end; } sixel_dither_ref(dither); sixel_dither_set_pixelformat(dither, pixelformat); if (pixelformat != SIXEL_PIXELFORMAT_RGB888) { /* normalize pixelformat */ normalized_pixels = (unsigned char *)sixel_allocator_malloc(dither->allocator, width * height * 3); if (normalized_pixels == NULL) { sixel_helper_set_additional_message( "sixel_dither_initialize: sixel_allocator_malloc() failed."); status = SIXEL_BAD_ALLOCATION; goto end; } status = sixel_helper_normalize_pixelformat( normalized_pixels, &pixelformat, data, pixelformat, width, height); if (SIXEL_FAILED(status)) { goto end; } input_pixels = normalized_pixels; } else { input_pixels = data; } sixel_dither_set_method_for_largest(dither, method_for_largest); sixel_dither_set_method_for_rep(dither, method_for_rep); sixel_dither_set_quality_mode(dither, quality_mode); status = sixel_quant_make_palette(&buf, input_pixels, width * height * 3, SIXEL_PIXELFORMAT_RGB888, dither->reqcolors, &dither->ncolors, &dither->origcolors, dither->method_for_largest, dither->method_for_rep, dither->quality_mode, dither->allocator); if (SIXEL_FAILED(status)) { goto end; } memcpy(dither->palette, buf, dither->ncolors * 3); dither->optimized = 1; if (dither->origcolors <= dither->ncolors) { dither->method_for_diffuse = SIXEL_DIFFUSE_NONE; } sixel_quant_free_palette(buf, dither->allocator); status = SIXEL_OK; end: free(normalized_pixels); sixel_dither_unref(dither); return status; }