From cac63cd3908d27c9e2d30293fb6e2bf357c36bae Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Thu, 11 Oct 2012 22:01:26 +0100 Subject: [PATCH] Add support for colour JPEG images. I used the luminance match algorithm for converting RGB colour images to grayscale as it gives the best visual results (but at the cost of speed). --- pic.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/pic.c b/pic.c index 447840154..d11dd4d9b 100644 --- a/pic.c +++ b/pic.c @@ -102,6 +102,25 @@ uint8_t *readJPEG(const char *fname, int *width, int *height, int *components) return (uint8_t *)image_buffer; } +/* Uses luminance match for approximating the human perception of colour, + * as per http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale + * L = 0.299*Red + 0.587*Green + 0.114*Blue */ +static uint8_t *rgbToGrayscale(uint8_t *image, int width, int height) +{ + int x, y; + uint8_t *buf = malloc(width*height+1); + + if (!buf) return NULL; + + for (x = 0; ximage = raw_image; + else if (components == 3) { + uint8_t *gray_image = rgbToGrayscale(raw_image, width, height); + if (!gray_image) { + free(raw_image); + return luaL_error(L, "Cannot convert to grayscale"); + } else { + free(raw_image); + doc->image = gray_image; + } + } else return luaL_error(L, "Unsupported image format"); - doc->image = image; doc->width = width; doc->height = height; doc->components = components; - //printf("openDocument(%s) decoded image: %dx%dx%d\n", filename, width, height, components); return 1; } @@ -132,6 +160,7 @@ static int openPage(lua_State *L) { PicPage *page = (PicPage*) lua_newuserdata(L, sizeof(PicPage)); luaL_getmetatable(L, "picpage"); lua_setmetatable(L, -2); + page->width = doc->width; page->height = doc->height; page->doc = doc; @@ -158,6 +187,7 @@ static int closeDocument(lua_State *L) { return 0; } +/* uses very simple nearest neighbour scaling */ static void scaleImage(uint8_t *result, uint8_t *image, int width, int height, int new_width, int new_height) { int x, y;