Skip to content

Commit

Permalink
added color quantization and floyd steinberg dithering
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsiomb committed Apr 18, 2022
1 parent b5bc24d commit 855a235
Show file tree
Hide file tree
Showing 3 changed files with 508 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/conv.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
libimago - a multi-format image file input/output library.
Copyright (C) 2010-2021 John Tsiombikas <nuclear@member.fsf.org>
Copyright (C) 2010-2022 John Tsiombikas <nuclear@member.fsf.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
Expand Down Expand Up @@ -97,9 +97,7 @@ int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
}

if(tofmt == IMG_FMT_IDX8) {
/* TODO */
fprintf(stderr, "imago: conversions to indexed not implemented yet\n");
return 0;
return img_quantize(img, 256, 0);
}

img_init(&nimg);
Expand Down
15 changes: 15 additions & 0 deletions src/imago2.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ enum img_fmt {
NUM_IMG_FMT
};

enum img_dither {
IMG_DITHER_NONE,
IMG_DITHER_ORDERED,
IMG_DITHER_FLOYD_STEINBERG
};

struct img_pixmap {
void *pixels;
int width, height;
Expand Down Expand Up @@ -141,6 +147,15 @@ int img_write(struct img_pixmap *img, struct img_io *io);
/* Converts an image to the specified pixel format */
int img_convert(struct img_pixmap *img, enum img_fmt tofmt);

/* Quantize an image to a have at most certain maximum number of colors,
* converting it to IMG_FMT_IDX8 in the process.
* The number of colors must be at most 256.
* The last argument defines the dithering algorithm to be used.
*
* C++: the dither argument is optional and defaults to IMG_DITHER_NONE
*/
int img_quantize(struct img_pixmap *img, int maxcol, IMG_OPTARG(enum img_dither dither, IMG_DITHER_NONE));

/* Flip the image vertically or horizontally */
void img_vflip(struct img_pixmap *img);
void img_hflip(struct img_pixmap *img);
Expand Down

0 comments on commit 855a235

Please sign in to comment.