diff options
author | Benjamin Herrenschmidt <benh@kernel.crashing.org> | 2007-04-03 14:31:39 +1000 |
---|---|---|
committer | Benjamin Herrenschmidt <benh@kernel.crashing.org> | 2007-04-03 14:31:39 +1000 |
commit | 73b1b6aa2d81d1e0df90bf490321dee2a3e4deb6 (patch) | |
tree | 5af7e02eee213ae620d8c9096a7ec9f2de0f5653 | |
parent | 61dacbd33a231e584bf400c195969d2357feaefc (diff) |
twin_jpeg: add a query function to get image details from file
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-rw-r--r-- | libtwin/twin_jpeg.c | 48 | ||||
-rw-r--r-- | libtwin/twin_jpeg.h | 18 |
2 files changed, 66 insertions, 0 deletions
diff --git a/libtwin/twin_jpeg.c b/libtwin/twin_jpeg.c index 273dbf7..4ddc274 100644 --- a/libtwin/twin_jpeg.c +++ b/libtwin/twin_jpeg.c @@ -167,3 +167,51 @@ twin_pixmap_t *twin_jpeg_to_pixmap(const char *filepath, twin_format_t fmt) return pix; } + +twin_bool_t twin_jpeg_query(const char *filepath, + twin_coord_t *out_width, + twin_coord_t *out_height, + int *out_components, + twin_jpeg_cspace_t *out_colorspace) +{ + struct jpeg_decompress_struct cinfo; + struct twin_jpeg_err_mgr jerr; + FILE *infile; + + /* Open file first, as example */ + infile = fopen(filepath, "rb"); + if (infile == NULL) { + fprintf(stderr, "can't open %s\n", filepath); + return TWIN_FALSE; + } + + /* Error handling crap */ + memset(&cinfo, 0, sizeof(cinfo)); + cinfo.err = jpeg_std_error(&jerr.mgr); + jerr.mgr.error_exit = twin_jpeg_error_exit; + if (setjmp(jerr.jbuf)) { + jpeg_destroy_decompress(&cinfo); + fclose(infile); + return TWIN_FALSE; + } + + /* Init libjpeg, hook it up to file and read header */ + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo, infile); + (void)jpeg_read_header(&cinfo, TRUE); + + /* Gather infos */ + if (out_width) + *out_width = cinfo.image_width; + if (out_height) + *out_height = cinfo.image_height; + if (out_components) + *out_components = cinfo.num_components; + if (out_colorspace) + *out_colorspace = (twin_jpeg_cspace_t)cinfo.jpeg_color_space; + + jpeg_destroy_decompress(&cinfo); + fclose(infile); + + return TWIN_TRUE; +} diff --git a/libtwin/twin_jpeg.h b/libtwin/twin_jpeg.h index 828e361..1af84db 100644 --- a/libtwin/twin_jpeg.h +++ b/libtwin/twin_jpeg.h @@ -23,6 +23,24 @@ #include <libtwin/twin.h> +/* This matches the libjpeg colorspace definitions so you don't have + * to use libjpeg headers in your application + */ +typedef enum { + TWIN_JCS_UNKNOWN, /* error/unspecified */ + TWIN_JCS_GRAYSCALE, /* monochrome */ + TWIN_JCS_RGB, /* red/green/blue */ + TWIN_JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */ + TWIN_JCS_CMYK, /* C/M/Y/K */ + TWIN_JCS_YCCK /* Y/Cb/Cr/K */ +} twin_jpeg_cspace_t; + +twin_bool_t twin_jpeg_query(const char *filepath, + twin_coord_t *out_width, + twin_coord_t *out_height, + int *out_components, + twin_jpeg_cspace_t *out_colorspace); + twin_pixmap_t *twin_jpeg_to_pixmap(const char *filepath, twin_format_t fmt); #endif /* _TWIN_JPEG_H_ */ |