6 #include "GmshConfig.h"
9 #if !defined(HAVE_LIBPNG)
13 Msg::Error(
"This version of Gmsh was compiled without PNG support");
21 #define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
27 buffer->
getType() != GL_UNSIGNED_BYTE) {
28 Msg::Error(
"PNG only implemented for GL_RGB/GL_RGBA and GL_UNSIGNED_BYTE");
33 png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
35 if(png_ptr ==
nullptr) {
36 Msg::Error(
"Could not create PNG write struct");
40 png_infop info_ptr = png_create_info_struct(png_ptr);
42 if(info_ptr ==
nullptr) {
43 png_destroy_write_struct(&png_ptr,
nullptr);
44 Msg::Error(
"Could not create PNG info struct");
48 if(setjmp(png_jmpbuf(png_ptr))) {
49 png_destroy_write_struct(&png_ptr, &info_ptr);
54 png_init_io(png_ptr, file);
61 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
62 (numcomp == 3) ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
63 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
64 PNG_FILTER_TYPE_BASE);
67 png_text text_ptr[10];
68 text_ptr[0].key = (
char *)
"Creator";
69 text_ptr[0].text = (
char *)
"Gmsh";
70 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
71 text_ptr[1].key = (
char *)
"Date";
72 text_ptr[1].text = ctime(&now);
73 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
74 png_set_text(png_ptr, info_ptr, text_ptr, 2);
75 png_write_info(png_ptr, info_ptr);
77 unsigned char *pixels = (
unsigned char *)buffer->
getPixels();
78 for(
int row = height - 1; row >= 0; row--) {
79 unsigned char *row_pointer = &pixels[row * width * numcomp];
80 png_write_row(png_ptr, (png_bytep)row_pointer);
82 png_write_end(png_ptr, info_ptr);
83 png_destroy_write_struct(&png_ptr, &info_ptr);