From 189da15810deabd739d7c11c6e95fea55739fe60 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 1 Aug 2020 15:13:49 +0200 Subject: Initial import from archive --- src/png-file.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/png-file.c (limited to 'src/png-file.c') diff --git a/src/png-file.c b/src/png-file.c new file mode 100644 index 0000000..09925a7 --- /dev/null +++ b/src/png-file.c @@ -0,0 +1,125 @@ +/* + * png-file.c + * + * PNG output + * + * (c) 2006-2007 Thomas White + * + * synth2d - Two-Dimensional Crystallographic Fourier Synthesis + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#include "data.h" +#include "colwheel.h" +#include "renderer.h" + +int png_write_real(const char *filename, fftw_complex *out, double brightness, double gamma_angle, int width, int height, int nx, int ny) { + + FILE *fh; + png_structp png_ptr; + png_infop info_ptr; + png_bytep *row_pointers; + int xn, yn; + int width_n, height_n; + ComplexArray cxar; + + fh = fopen(filename, "wb"); + if (!fh) { + fprintf(stderr, "Couldn't open output file.\n"); + return 1; + } + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if ( !png_ptr ) { + fprintf(stderr, "Couldn't create PNG write structure.\n"); + fclose(fh); + return 1; + } + info_ptr = png_create_info_struct(png_ptr); + if ( !info_ptr ) { + png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + fprintf(stderr, "Couldn't create PNG info structure.\n"); + fclose(fh); + return 1; + } + if ( setjmp(png_jmpbuf(png_ptr)) ) { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fh); + fprintf(stderr, "PNG write failed.\n"); + return 1; + } + png_init_io(png_ptr, fh); + + width_n = (int)renderer_width(width, height, gamma_angle, nx, ny); + height_n = (int)renderer_height(width, height, gamma_angle, nx, ny); + cxar = renderer_draw(out, width, height, gamma_angle, nx, ny); + + png_set_IHDR(png_ptr, info_ptr, width_n, height_n, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + /* Write the image data */ + row_pointers = malloc(height_n*sizeof(png_bytep *)); + + for ( yn=0; yn 1 ) am = 1; + + row_pointers[yn][3*xn] = (png_byte)255*colwheel_red(am, ph); + row_pointers[yn][3*xn+1] = (png_byte)255*colwheel_green(am, ph); + row_pointers[yn][3*xn+2] = (png_byte)255*colwheel_blue(am, ph); + + } + } + + for ( yn=0; yn