aboutsummaryrefslogtreecommitdiffstats
path: root/src/jpeg.c
diff options
context:
space:
mode:
authorJohn Hawthorn <jhawthor@uvic.ca>2008-05-14 21:35:42 -0700
committerJohn Hawthorn <jhawthor@uvic.ca>2008-05-14 21:35:42 -0700
commit7a68eb750b334031b9c0cd29b6aff0d46734e23a (patch)
tree88ee50f6166506fce1dd5afe71a8f52cd7094523 /src/jpeg.c
downloadmirror-meh-7a68eb750b334031b9c0cd29b6aff0d46734e23a.tar.gz
mirror-meh-7a68eb750b334031b9c0cd29b6aff0d46734e23a.tar.bz2
mirror-meh-7a68eb750b334031b9c0cd29b6aff0d46734e23a.zip
initial
Diffstat (limited to 'src/jpeg.c')
-rw-r--r--src/jpeg.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/jpeg.c b/src/jpeg.c
new file mode 100644
index 0000000..6d3b40c
--- /dev/null
+++ b/src/jpeg.c
@@ -0,0 +1,54 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "jpeglib.h"
+
+#include "jpeg.h"
+
+unsigned char *loadjpeg(char *filename, int *width, int *height){
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+
+ FILE *infile;
+ JSAMPARRAY buffer;
+ int row_stride;
+ int i = 0;
+ int x, y;
+ unsigned char *retbuf;
+
+ if((infile = fopen(filename, "rb")) == NULL){
+ fprintf(stderr, "can't open %s\n", filename);
+ exit(1);
+ }
+
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_decompress(&cinfo);
+ jpeg_stdio_src(&cinfo, infile);
+ jpeg_read_header(&cinfo, TRUE);
+ jpeg_start_decompress(&cinfo);
+ *width = cinfo.output_width;
+ *height = cinfo.output_height;
+ retbuf = malloc(cinfo.output_components * (cinfo.output_width * cinfo.output_height));
+
+ if(cinfo.output_components != 3){
+ fprintf(stderr, "TODO: greyscale images are not supported\n");
+ exit(1);
+ }
+ row_stride = cinfo.output_width * cinfo.output_components;
+ buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
+ for(y = 0; y < cinfo.output_height; y++){
+ jpeg_read_scanlines(&cinfo, buffer, 1);
+ for(x = 0; x < row_stride; x++){
+ retbuf[i++] = buffer[0][x];
+ }
+ assert(i % *width == 0);
+ }
+ jpeg_finish_decompress(&cinfo);
+ jpeg_destroy_decompress(&cinfo);
+ fclose(infile);
+ return retbuf;
+}
+
+