From 02453dd699291cd6ef8f3e06ab24878cac9cc2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Tue, 22 Nov 2011 14:40:00 -0500 Subject: [PATCH] cairo-util: Dont use non-standard JCS_EXT_BGRX color space Everytime somebody has to write a channel swizzling loop, God kills a kitten... and a dolphin and a unicorn. --- clients/cairo-util.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/clients/cairo-util.c b/clients/cairo-util.c index 61a053b3..54a05b1a 100644 --- a/clients/cairo-util.c +++ b/clients/cairo-util.c @@ -289,13 +289,28 @@ rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) cairo_close_path(cr); } +static void +swizzle_row(JSAMPLE *row, JDIMENSION width) +{ + JSAMPLE *s; + uint32_t *d; + + s = row + (width - 1) * 3; + d = (uint32_t *) (row + (width - 1) * 4); + while (s >= row) { + *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0); + s -= 3; + d--; + } +} + cairo_surface_t * load_jpeg(const char *filename) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp; - int stride, i; + int stride, i, first; JSAMPLE *data, *rows[4]; cinfo.err = jpeg_std_error(&jerr); @@ -310,7 +325,7 @@ load_jpeg(const char *filename) jpeg_read_header(&cinfo, TRUE); - cinfo.out_color_space = JCS_EXT_BGRX; + cinfo.out_color_space = JCS_RGB; jpeg_start_decompress(&cinfo); stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, @@ -322,10 +337,13 @@ load_jpeg(const char *filename) } while (cinfo.output_scanline < cinfo.output_height) { + first = cinfo.output_scanline; for (i = 0; i < ARRAY_LENGTH(rows); i++) - rows[i] = data + (cinfo.output_scanline + i) * stride; + rows[i] = data + (first + i) * stride; jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows)); + for (i = 0; first + i < cinfo.output_scanline; i++) + swizzle_row(rows[i], cinfo.output_width); } jpeg_finish_decompress(&cinfo);