gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
gl2png.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "GmshConfig.h"
7 #include "gl2png.h"
8 
9 #if !defined(HAVE_LIBPNG)
10 
11 void create_png(FILE *file, PixelBuffer *buffer, int quality)
12 {
13  Msg::Error("This version of Gmsh was compiled without PNG support");
14 }
15 
16 #else
17 
18 #include <png.h>
19 
20 #ifndef png_jmpbuf
21 #define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
22 #endif
23 
24 void create_png(FILE *file, PixelBuffer *buffer, int quality)
25 {
26  if((buffer->getFormat() != GL_RGB && buffer->getFormat() != GL_RGBA) ||
27  buffer->getType() != GL_UNSIGNED_BYTE) {
28  Msg::Error("PNG only implemented for GL_RGB/GL_RGBA and GL_UNSIGNED_BYTE");
29  return;
30  }
31 
32  png_structp png_ptr =
33  png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
34 
35  if(png_ptr == nullptr) {
36  Msg::Error("Could not create PNG write struct");
37  return;
38  }
39 
40  png_infop info_ptr = png_create_info_struct(png_ptr);
41 
42  if(info_ptr == nullptr) {
43  png_destroy_write_struct(&png_ptr, nullptr);
44  Msg::Error("Could not create PNG info struct");
45  return;
46  }
47 
48  if(setjmp(png_jmpbuf(png_ptr))) {
49  png_destroy_write_struct(&png_ptr, &info_ptr);
50  Msg::Error("Could not setjmp in PNG");
51  return;
52  }
53 
54  png_init_io(png_ptr, file);
55 
56  int height = buffer->getHeight();
57  int width = buffer->getWidth();
58  int numcomp = buffer->getNumComp();
59 
60  // png_set_compression_level(png_ptr, 5);
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);
65  time_t now;
66  time(&now);
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);
76 
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);
81  }
82  png_write_end(png_ptr, info_ptr);
83  png_destroy_write_struct(&png_ptr, &info_ptr);
84 }
85 
86 #endif
gl2png.h
PixelBuffer::getHeight
int getHeight()
Definition: PixelBuffer.h:71
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
PixelBuffer::getFormat
GLenum getFormat()
Definition: PixelBuffer.h:74
PixelBuffer::getType
GLenum getType()
Definition: PixelBuffer.h:75
create_png
void create_png(FILE *file, PixelBuffer *buffer, int quality)
Definition: gl2png.cpp:11
PixelBuffer::getPixels
void * getPixels()
Definition: PixelBuffer.h:76
PixelBuffer::getWidth
int getWidth()
Definition: PixelBuffer.h:70
PixelBuffer::getNumComp
int getNumComp()
Definition: PixelBuffer.h:72
PixelBuffer
Definition: PixelBuffer.h:32