gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
gl2jpeg.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 "gl2jpeg.h"
8 #undef EXTERN
9 
10 #if !defined(HAVE_LIBJPEG)
11 
12 void create_jpeg(FILE *outfile, PixelBuffer *buffer, int quality, int smoothing)
13 {
14  Msg::Error("This version of Gmsh was compiled without JPEG support");
15 }
16 
17 #else
18 
19 extern "C" {
20 #include <jpeglib.h>
21 #include <jerror.h>
22 }
23 
24 static void my_output_message(j_common_ptr cinfo)
25 {
26  char buffer[JMSG_LENGTH_MAX];
27 
28  (*cinfo->err->format_message)(cinfo, buffer);
29 
30  Msg::Debug("%s", buffer);
31 }
32 
33 void create_jpeg(FILE *outfile, PixelBuffer *buffer, int quality, int smoothing)
34 {
35  if(buffer->getFormat() != GL_RGB || buffer->getType() != GL_UNSIGNED_BYTE) {
36  Msg::Error("JPEG only implemented for GL_RGB and GL_UNSIGNED_BYTE");
37  return;
38  }
39 
40  struct jpeg_compress_struct cinfo;
41  struct jpeg_error_mgr jerr;
42  cinfo.err = jpeg_std_error(&jerr);
43  cinfo.err->output_message = my_output_message;
44 
45  jpeg_create_compress(&cinfo);
46  jpeg_stdio_dest(&cinfo, outfile);
47  cinfo.image_width = buffer->getWidth();
48  cinfo.image_height = buffer->getHeight();
49  cinfo.input_components = 3;
50  cinfo.in_color_space = JCS_RGB;
51  jpeg_set_defaults(&cinfo);
52  jpeg_set_quality(&cinfo, quality, TRUE);
53  cinfo.optimize_coding = TRUE;
54  cinfo.smoothing_factor = smoothing;
55  jpeg_start_compress(&cinfo, TRUE);
56 
57  unsigned char *pixels = (unsigned char *)buffer->getPixels();
58  JSAMPROW row_pointer[1];
59  int row_stride = cinfo.image_width * cinfo.input_components;
60  int i = cinfo.image_height - 1;
61  while(i >= 0) {
62  row_pointer[0] = &pixels[i * row_stride];
63  (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
64  i--;
65  }
66  jpeg_finish_compress(&cinfo);
67  jpeg_destroy_compress(&cinfo);
68 }
69 
70 #endif
Msg::Debug
static void Debug(const char *fmt,...)
Definition: GmshMessage.cpp:752
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
gl2jpeg.h
PixelBuffer::getType
GLenum getType()
Definition: PixelBuffer.h:75
PixelBuffer::getPixels
void * getPixels()
Definition: PixelBuffer.h:76
TRUE
#define TRUE
Definition: gl2gif.cpp:554
PixelBuffer::getWidth
int getWidth()
Definition: PixelBuffer.h:70
create_jpeg
void create_jpeg(FILE *outfile, PixelBuffer *buffer, int quality, int smoothing)
Definition: gl2jpeg.cpp:12
PixelBuffer
Definition: PixelBuffer.h:32