aboutsummaryrefslogtreecommitdiff
path: root/src/readpng.c
diff options
context:
space:
mode:
authortaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-02-05 21:12:57 +0000
committertaw27 <taw27@bf6ca9ba-c028-0410-8290-897cf20841d1>2007-02-05 21:12:57 +0000
commit05b5d261682b9136fb46476a64eab6980b0dba64 (patch)
treed7faa450b69cf2104ffff7fc89a95914284d23af /src/readpng.c
Initial import
git-svn-id: svn://cook.msm.cam.ac.uk:745/diff-tomo/dtr@1 bf6ca9ba-c028-0410-8290-897cf20841d1
Diffstat (limited to 'src/readpng.c')
-rw-r--r--src/readpng.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/readpng.c b/src/readpng.c
new file mode 100644
index 0000000..bb5a60b
--- /dev/null
+++ b/src/readpng.c
@@ -0,0 +1,146 @@
+/*
+ * readpng.c
+ *
+ * Read images
+ *
+ * (c) 2007 Thomas White <taw27@cam.ac.uk>
+ * dtr - Diffraction Tomography Reconstruction
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <png.h>
+#include <math.h>
+
+#include "control.h"
+#include "reflections.h"
+#include "itrans.h"
+
+int readpng_read(const char *filename, double tilt, ControlContext *ctx) {
+
+ FILE *fh;
+ png_bytep header;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_infop end_info;
+ unsigned int width;
+ unsigned int height;
+ unsigned int bit_depth;
+ unsigned int channels;
+ png_bytep *row_pointers;
+ unsigned int x;
+ unsigned int y;
+ unsigned int xorig;
+ unsigned int yorig;
+ int16_t *image;
+
+ /* Open file */
+ fh = fopen(filename, "rb");
+ if ( !fh ) {
+ printf("RI: Couldn't open file '%s'\n", filename);
+ return -1;
+ }
+
+ /* Check it's actually a PNG file */
+ header = malloc(8);
+ fread(header, 1, 8, fh);
+ if ( png_sig_cmp(header, 0, 8)) {
+ printf("RI: Not a PNG file.\n");
+ free(header);
+ fclose(fh);
+ return -1;
+ }
+ free(header);
+
+ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if ( !png_ptr ) {
+ printf("RI: Couldn't create PNG read structure.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if ( !info_ptr ) {
+ png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
+ printf("RI: Couldn't create PNG info structure.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ end_info = png_create_info_struct(png_ptr);
+ if ( !end_info ) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
+ printf("RI: Couldn't create PNG end info structure.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ if ( setjmp(png_jmpbuf(png_ptr)) ) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ fclose(fh);
+ printf("RI: PNG read failed.\n");
+ return -1;
+ }
+
+ png_init_io(png_ptr, fh);
+ png_set_sig_bytes(png_ptr, 8);
+
+ /* Read! */
+ png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_ALPHA, NULL);
+
+ width = png_get_image_width(png_ptr, info_ptr);
+ height = png_get_image_height(png_ptr, info_ptr);
+ bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+ channels = png_get_channels(png_ptr, info_ptr);
+// printf("RI: width=%i, height=%i, depth=%i, channels=%i\n", width, height, bit_depth, channels);
+ if ( (bit_depth != 16) && (bit_depth != 8) ) {
+ printf("RI: Whoops! Can't handle images with other than 8 or 16 bpp yet...\n");
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ fclose(fh);
+ return -1;
+ }
+
+ /* Get image data */
+ row_pointers = png_get_rows(png_ptr, info_ptr);
+
+ xorig = 320;
+ yorig = 320; /* For now */
+ image = malloc(height * width * sizeof(int16_t));
+ ctx->width = width; ctx->height = height;
+ ctx->fmode = FORMULATION_CLEN;
+
+ for ( y=0; y<height; y++ ) {
+ for ( x=0; x<width; x++ ) {
+
+ int val = 0;
+
+ if ( (x-xorig)*(x-xorig) + (y-yorig)*(y-yorig) < ctx->max_d*ctx->max_d ) {
+
+ if ( bit_depth == 16 ) {
+ val = row_pointers[y][channels*x] << 8;
+ val += row_pointers[y][(channels*x)+1];
+ }
+ if ( bit_depth == 8 ) {
+ val = row_pointers[y][channels*x];
+ }
+
+ image[x + width*y] = val;
+
+ }
+ }
+ }
+
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ fclose(fh);
+
+ itrans_process_image(image, ctx, tilt);
+
+ return 0;
+
+}