CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
FWTEveViewer Class Reference

#include "FWTEveViewer.h"

Inheritance diagram for FWTEveViewer:

Public Member Functions

std::future< int > CaptureAndSaveImage (const TString &file, int height=-1)
 
FWTGLViewerfwGlViewer ()
 
 FWTEveViewer (const char *n="FWTEveViewer", const char *t="")
 
FWTGLViewerSpawnFWTGLViewer ()
 
virtual ~FWTEveViewer ()
 

Static Public Member Functions

static bool SaveJpg (const TString &file, UChar_t *xx, int ww, int hh)
 
static bool SavePng (const TString &file, UChar_t *xx, int ww, int hh)
 

Private Member Functions

 FWTEveViewer (const FWTEveViewer &)
 
const FWTEveVieweroperator= (const FWTEveViewer &)
 
void spawn_image_thread ()
 

Private Attributes

std::condition_variable m_cnd
 
FWTGLViewerm_fwGlViewer
 
int m_hh
 
std::vector< unsigned char > m_imgBuffer
 
std::mutex m_moo
 
TString m_name
 
std::promise< int > m_prom
 
std::thread * m_thr = 0
 
bool m_thr_exit = false
 
int m_ww
 

Detailed Description

Description: [one line class summary]

Usage: <usage>

Definition at line 37 of file FWTEveViewer.h.

Constructor & Destructor Documentation

FWTEveViewer::FWTEveViewer ( const char *  n = "FWTEveViewer",
const char *  t = "" 
)

Definition at line 41 of file FWTEveViewer.cc.

41  :
42  TEveViewer(n, t),
43  m_fwGlViewer(0)
44 {}
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:68
FWTEveViewer::~FWTEveViewer ( )
virtual

Definition at line 51 of file FWTEveViewer.cc.

References m_cnd, m_moo, m_thr, and m_thr_exit.

52 {
53  if (m_thr) m_thr->detach();
54 
55  {
56  std::unique_lock<std::mutex> lk(m_moo);
57 
58  m_thr_exit = true;
59  m_cnd.notify_one();
60  }
61 
62  delete m_thr;
63 }
std::mutex m_moo
Definition: FWTEveViewer.h:77
std::condition_variable m_cnd
Definition: FWTEveViewer.h:78
std::thread * m_thr
Definition: FWTEveViewer.h:75
FWTEveViewer::FWTEveViewer ( const FWTEveViewer )
private

Member Function Documentation

std::future< int > FWTEveViewer::CaptureAndSaveImage ( const TString &  file,
int  height = -1 
)

Definition at line 136 of file FWTEveViewer.cc.

References mergeVDriftHistosByStation::file, m_cnd, m_fwGlViewer, m_hh, m_imgBuffer, m_moo, m_name, m_prom, m_thr, m_ww, FWTGLViewer::MakeFbo(), FWTGLViewer::MakeFboHeight(), and spawn_image_thread().

137 {
138  static const TString eh("FWTEveViewer::CaptureAndSaveImage");
139 
140  TGLFBO *fbo = 0;
141  if (height == -1)
142  fbo = m_fwGlViewer->MakeFbo();
143  else
144  fbo = m_fwGlViewer->MakeFboHeight(height);
145 
146  if (fbo == 0)
147  {
148  ::Error(eh, "Returned FBO is 0.");
149  m_prom = std::promise<int>();
150  m_prom.set_value(-1);
151  return m_prom.get_future();
152  }
153 
154  int ww, hh;
155  if (fbo->fIsRescaled)
156  {
157  ww = TMath::Nint(fbo->fW * fbo->fWScale);
158  hh = TMath::Nint(fbo->fH * fbo->fHScale);
159  }
160  else
161  {
162  ww = fbo->fW;
163  hh = fbo->fH;
164  }
165 
166  fbo->SetAsReadBuffer();
167 
168  size_t bufsize = 3 * ww * hh;
169  if (bufsize != m_imgBuffer.size())
170  {
171  m_imgBuffer.resize(bufsize);
172  }
173 
174  glPixelStorei(GL_PACK_ALIGNMENT, 1);
175  glReadPixels(0, 0, ww, hh, GL_RGB, GL_UNSIGNED_BYTE, &m_imgBuffer[0]);
176 
177  if (m_thr == 0) spawn_image_thread();
178 
179  {
180  std::unique_lock<std::mutex> lk(m_moo);
181 
182  m_prom = std::promise<int>();
183  m_name = file;
184  m_ww = ww;
185  m_hh = hh;
186 
187  m_cnd.notify_one();
188  }
189 
190  return m_prom.get_future();
191 }
std::mutex m_moo
Definition: FWTEveViewer.h:77
TString m_name
Definition: FWTEveViewer.h:72
TGLFBO * MakeFboHeight(Int_t height, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:132
std::condition_variable m_cnd
Definition: FWTEveViewer.h:78
std::thread * m_thr
Definition: FWTEveViewer.h:75
std::vector< unsigned char > m_imgBuffer
Definition: FWTEveViewer.h:70
std::promise< int > m_prom
Definition: FWTEveViewer.h:76
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:68
TGLFBO * MakeFbo()
Definition: FWTGLViewer.cc:111
void spawn_image_thread()
Definition: FWTEveViewer.cc:83
FWTGLViewer* FWTEveViewer::fwGlViewer ( )
inline

Definition at line 53 of file FWTEveViewer.h.

References m_fwGlViewer.

Referenced by FWEveView::fwViewerGL().

53 { return m_fwGlViewer; }
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:68
const FWTEveViewer& FWTEveViewer::operator= ( const FWTEveViewer )
private
bool FWTEveViewer::SaveJpg ( const TString &  file,
UChar_t *  xx,
int  ww,
int  hh 
)
static

Definition at line 281 of file FWTEveViewer.cc.

References i, j, tablePrinter::rows, and TRUE.

Referenced by spawn_image_thread().

282 {
283  struct jpeg_compress_struct JpegInfo;
284  struct jpeg_error_mgr Error;
285 
286  JpegInfo.err = jpeg_std_error(&Error);
287 
288  // Now we can initialize the JPEG compression object.
289  jpeg_create_compress(&JpegInfo);
290 
291  FILE *fp = fopen(file, "w");
292  jpeg_stdio_dest(&JpegInfo, fp);
293 
294  JpegInfo.image_width = ww;
295  JpegInfo.image_height = hh;
296  JpegInfo.input_components = 3;
297  JpegInfo.in_color_space = JCS_RGB;
298 
299  jpeg_set_defaults(&JpegInfo);
300 
301  JpegInfo.write_JFIF_header = TRUE;
302 
303  // Set the quality output
304  // const int quality = 98;
305  // jpeg_set_quality(&JpegInfo, quality, true); // bool force_baseline ????
306 
307  jpeg_start_compress(&JpegInfo, TRUE);
308 
309  std::vector<UChar_t*> rows(hh);
310  {
311  int j = hh - 1;
312  for (int i = 0; i < hh; i++, j--) {
313  rows[i] = xx + j * ww * 3;
314  }
315  }
316 
317  jpeg_write_scanlines(&JpegInfo, &rows[0], hh);
318 
319  // Step 6: Finish compression
320  jpeg_finish_compress(&JpegInfo);
321 
322  // Step 7: release JPEG compression object
323 
324  // This is an important step since it will release a good deal of memory.
325  jpeg_destroy_compress(&JpegInfo);
326 
327  return true;
328 }
int i
Definition: DBlmapReader.cc:9
#define TRUE
Definition: scimark2.h:12
int j
Definition: DBlmapReader.cc:9
bool FWTEveViewer::SavePng ( const TString &  file,
UChar_t *  xx,
int  ww,
int  hh 
)
static

Definition at line 201 of file FWTEveViewer.cc.

References i, j, NULL, and tablePrinter::rows.

Referenced by spawn_image_thread().

202 {
203  png_structp png_ptr;
204  png_infop info_ptr;
205 
206  /* Create and initialize the png_struct with the desired error handler
207  * functions. If you want to use the default stderr and longjump method,
208  * you can supply NULL for the last three parameters. We also check that
209  * the library version is compatible with the one used at compile time,
210  * in case we are using dynamically linked libraries. REQUIRED.
211  */
212  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, 0, 0);
213  if (png_ptr == NULL) {
214  printf("Error creating png write struct\n");
215  return false;
216  }
217 
218  // Allocate/initialize the image information data. REQUIRED
219  info_ptr = png_create_info_struct(png_ptr);
220  if (info_ptr == NULL) {
221  printf("Error creating png info struct\n");
222  png_destroy_write_struct(&png_ptr, &info_ptr);
223  return false;
224  }
225 
226  /*// Set error handling. REQUIRED if you aren't supplying your own
227  // error handling functions in the png_create_write_struct() call.
228  if (setjmp(png_jmpbuf(png_ptr))) {
229  // If we get here, we had a problem reading the file
230  png_destroy_write_struct(&png_ptr, &info_ptr);
231  ilSetError(IL_LIB_PNG_ERROR);
232  return IL_FALSE;
233  }*/
234 
235  FILE *fp = fopen(file, "w");
236 
237  png_init_io(png_ptr, fp);
238 
239 
240  // Use PNG_INTERLACE_ADAM7 for interlacing
241  png_set_IHDR(png_ptr, info_ptr, ww, hh, 8, PNG_COLOR_TYPE_RGB,
242  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
243 
244  /* Optional gamma chunk is strongly suggested if you have any guess
245  * as to the correct gamma of the image.
246  */
247  // png_set_gAMA(png_ptr, info_ptr, gamma);
248 
249  // Optionally write comments into the image.
250  // png_text text;
251  // text.key = "Generated by";
252  // text.text = "Generated by cmsShow";
253  // text.compression = PNG_TEXT_COMPRESSION_NONE;
254  // png_set_text(png_ptr, info_ptr, &text, 1);
255 
256  // Write the file header information. REQUIRED.
257  png_write_info(png_ptr, info_ptr);
258 
259  std::vector<UChar_t*> rows(hh);
260  {
261  int j = hh - 1;
262  for (int i = 0; i < hh; i++, j--) {
263  rows[i] = xx + j * ww * 3;
264  }
265  }
266 
267  // Writes the image.
268  png_write_image(png_ptr, &rows[0]);
269 
270  // It is REQUIRED to call this to finish writing the rest of the file
271  png_write_end(png_ptr, info_ptr);
272 
273  // clean up after the write, and ifree any memory allocated
274  png_destroy_write_struct(&png_ptr, &info_ptr);
275 
276  fclose(fp);
277 
278  return true;
279 }
int i
Definition: DBlmapReader.cc:9
#define NULL
Definition: scimark2.h:8
int j
Definition: DBlmapReader.cc:9
void FWTEveViewer::spawn_image_thread ( )
private

Definition at line 83 of file FWTEveViewer.cc.

References m_cnd, m_hh, m_imgBuffer, m_moo, m_name, m_prom, m_thr, m_thr_exit, m_ww, SaveJpg(), and SavePng().

Referenced by CaptureAndSaveImage().

84 {
85  std::unique_lock<std::mutex> lko(m_moo);
86 
87  m_thr = new std::thread([=]() {
88  { std::unique_lock<std::mutex> lk(m_moo); m_cnd.notify_one(); }
89  while (true)
90  {
91  {
92  std::unique_lock<std::mutex> lk(m_moo);
93  m_cnd.wait(lk);
94 
95  if (m_thr_exit)
96  {
97  return;
98  }
99  }
100  if (m_name.EndsWith(".jpg"))
101  {
103  }
104  else
105  {
107  }
108 
109  m_prom.set_value(0);
110  }
111  });
112 
113  m_cnd.wait(lko);
114 }
std::mutex m_moo
Definition: FWTEveViewer.h:77
TString m_name
Definition: FWTEveViewer.h:72
std::condition_variable m_cnd
Definition: FWTEveViewer.h:78
std::thread * m_thr
Definition: FWTEveViewer.h:75
std::vector< unsigned char > m_imgBuffer
Definition: FWTEveViewer.h:70
std::promise< int > m_prom
Definition: FWTEveViewer.h:76
static bool SaveJpg(const TString &file, UChar_t *xx, int ww, int hh)
static bool SavePng(const TString &file, UChar_t *xx, int ww, int hh)
FWTGLViewer * FWTEveViewer::SpawnFWTGLViewer ( )

Definition at line 118 of file FWTEveViewer.cc.

References m_fwGlViewer.

Referenced by FWEveView::FWEveView().

119 {
120  TGCompositeFrame* cf = GetGUICompositeFrame();
121 
122  m_fwGlViewer = new FWTGLViewer(cf);
123  SetGLViewer(m_fwGlViewer, m_fwGlViewer->GetFrame());
124 
125  cf->AddFrame(fGLViewerFrame, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY));
126 
127  fGLViewerFrame->MapWindow();
128 
129  if (fEveFrame == 0)
130  PreUndock();
131 
132  return m_fwGlViewer;
133 }
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:68

Member Data Documentation

std::condition_variable FWTEveViewer::m_cnd
private

Definition at line 78 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), spawn_image_thread(), and ~FWTEveViewer().

FWTGLViewer* FWTEveViewer::m_fwGlViewer
private

Definition at line 68 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), fwGlViewer(), and SpawnFWTGLViewer().

int FWTEveViewer::m_hh
private

Definition at line 73 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

std::vector<unsigned char> FWTEveViewer::m_imgBuffer
private

Definition at line 70 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

std::mutex FWTEveViewer::m_moo
private

Definition at line 77 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), spawn_image_thread(), and ~FWTEveViewer().

TString FWTEveViewer::m_name
private

Definition at line 72 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

std::promise<int> FWTEveViewer::m_prom
private

Definition at line 76 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

std::thread* FWTEveViewer::m_thr = 0
private

Definition at line 75 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), spawn_image_thread(), and ~FWTEveViewer().

bool FWTEveViewer::m_thr_exit = false
private

Definition at line 74 of file FWTEveViewer.h.

Referenced by spawn_image_thread(), and ~FWTEveViewer().

int FWTEveViewer::m_ww
private

Definition at line 73 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().