CMS 3D CMS Logo

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="")
 
 FWTEveViewer (const FWTEveViewer &)=delete
 
const FWTEveVieweroperator= (const FWTEveViewer &)=delete
 
FWTGLViewerSpawnFWTGLViewer ()
 
 ~FWTEveViewer () override
 

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

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 = nullptr
 
bool m_thr_exit = false
 
int m_ww
 

Detailed Description

Description: [one line class summary]

Usage: <usage>

Definition at line 36 of file FWTEveViewer.h.

Constructor & Destructor Documentation

◆ FWTEveViewer() [1/2]

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

Definition at line 38 of file FWTEveViewer.cc.

38 : TEveViewer(n, t), m_fwGlViewer(nullptr) {}
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:65

◆ ~FWTEveViewer()

FWTEveViewer::~FWTEveViewer ( )
override

Definition at line 45 of file FWTEveViewer.cc.

References m_cnd, m_moo, m_thr, and m_thr_exit.

45  {
46  if (m_thr)
47  m_thr->detach();
48 
49  {
50  std::unique_lock<std::mutex> lk(m_moo);
51 
52  m_thr_exit = true;
53  m_cnd.notify_one();
54  }
55 
56  delete m_thr;
57 }
std::mutex m_moo
Definition: FWTEveViewer.h:74
std::condition_variable m_cnd
Definition: FWTEveViewer.h:75
std::thread * m_thr
Definition: FWTEveViewer.h:72

◆ FWTEveViewer() [2/2]

FWTEveViewer::FWTEveViewer ( const FWTEveViewer )
delete

Member Function Documentation

◆ CaptureAndSaveImage()

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

Definition at line 125 of file FWTEveViewer.cc.

References geometryDiff::file, ALPAKA_ACCELERATOR_NAMESPACE::caPixelDoublets::hh, m_cnd, m_fwGlViewer, m_hh, m_imgBuffer, m_moo, m_name, m_prom, m_thr, m_ww, FWTGLViewer::MakeFbo(), FWTGLViewer::MakeFboHeight(), spawn_image_thread(), and ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder::ww.

125  {
126  static const TString eh("FWTEveViewer::CaptureAndSaveImage");
127 
128  TGLFBO* fbo = nullptr;
129  if (height == -1)
130  fbo = m_fwGlViewer->MakeFbo();
131  else
132  fbo = m_fwGlViewer->MakeFboHeight(height);
133 
134  if (fbo == nullptr) {
135  ::Error(eh, "Returned FBO is 0.");
136  m_prom = std::promise<int>();
137  m_prom.set_value(-1);
138  return m_prom.get_future();
139  }
140 
141  int ww, hh;
142  if (fbo->GetIsRescaled()) {
143  ww = TMath::Nint(fbo->GetW() * fbo->GetWScale());
144  hh = TMath::Nint(fbo->GetH() * fbo->GetHScale());
145  } else {
146  ww = fbo->GetW();
147  hh = fbo->GetH();
148  }
149 
150  fbo->SetAsReadBuffer();
151 
152  size_t bufsize = 3 * ww * hh;
153  if (bufsize != m_imgBuffer.size()) {
154  m_imgBuffer.resize(bufsize);
155  }
156 
157  glPixelStorei(GL_PACK_ALIGNMENT, 1);
158  glReadPixels(0, 0, ww, hh, GL_RGB, GL_UNSIGNED_BYTE, &m_imgBuffer[0]);
159 
160  if (m_thr == nullptr)
162 
163  {
164  std::unique_lock<std::mutex> lk(m_moo);
165 
166  m_prom = std::promise<int>();
167  m_name = file;
168  m_ww = ww;
169  m_hh = hh;
170 
171  m_cnd.notify_one();
172  }
173 
174  return m_prom.get_future();
175 }
edm::ErrorSummaryEntry Error
std::mutex m_moo
Definition: FWTEveViewer.h:74
TString m_name
Definition: FWTEveViewer.h:69
TGLFBO * MakeFboHeight(Int_t height, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:116
std::condition_variable m_cnd
Definition: FWTEveViewer.h:75
std::thread * m_thr
Definition: FWTEveViewer.h:72
std::vector< unsigned char > m_imgBuffer
Definition: FWTEveViewer.h:67
std::promise< int > m_prom
Definition: FWTEveViewer.h:73
ALPAKA_FN_ACC ALPAKA_FN_INLINE void uint32_t const uint32_t CACellT< TrackerTraits > uint32_t CellNeighborsVector< TrackerTraits > CellTracksVector< TrackerTraits > HitsConstView< TrackerTraits > hh
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:65
TGLFBO * MakeFbo()
Definition: FWTGLViewer.cc:97
void spawn_image_thread()
Definition: FWTEveViewer.cc:77

◆ fwGlViewer()

FWTGLViewer* FWTEveViewer::fwGlViewer ( )
inline

Definition at line 50 of file FWTEveViewer.h.

References m_fwGlViewer.

Referenced by FWEveView::fwViewerGL().

50 { return m_fwGlViewer; }
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:65

◆ operator=()

const FWTEveViewer& FWTEveViewer::operator= ( const FWTEveViewer )
delete

◆ SaveJpg()

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

Definition at line 270 of file FWTEveViewer.cc.

References geometryDiff::file, personalPlayback::fp, ALPAKA_ACCELERATOR_NAMESPACE::caPixelDoublets::hh, mps_fire::i, dqmiolumiharvest::j, mysort::rows, TRUE, ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder::ww, and geometryCSVtoXML::xx.

Referenced by spawn_image_thread().

270  {
271  struct jpeg_compress_struct JpegInfo;
272  struct jpeg_error_mgr Error;
273 
274  JpegInfo.err = jpeg_std_error(&Error);
275 
276  // Now we can initialize the JPEG compression object.
277  jpeg_create_compress(&JpegInfo);
278 
279  FILE* fp = fopen(file, "w");
280  jpeg_stdio_dest(&JpegInfo, fp);
281 
282  JpegInfo.image_width = ww;
283  JpegInfo.image_height = hh;
284  JpegInfo.input_components = 3;
285  JpegInfo.in_color_space = JCS_RGB;
286 
287  jpeg_set_defaults(&JpegInfo);
288 
289  JpegInfo.write_JFIF_header = TRUE;
290 
291  // Set the quality output
292  // const int quality = 98;
293  // jpeg_set_quality(&JpegInfo, quality, true); // bool force_baseline ????
294 
295  jpeg_start_compress(&JpegInfo, TRUE);
296 
297  std::vector<UChar_t*> rows(hh);
298  {
299  int j = hh - 1;
300  for (int i = 0; i < hh; i++, j--) {
301  rows[i] = xx + j * ww * 3;
302  }
303  }
304 
305  jpeg_write_scanlines(&JpegInfo, &rows[0], hh);
306 
307  // Step 6: Finish compression
308  jpeg_finish_compress(&JpegInfo);
309 
310  // Step 7: release JPEG compression object
311 
312  // This is an important step since it will release a good deal of memory.
313  jpeg_destroy_compress(&JpegInfo);
314 
315  return true;
316 }
edm::ErrorSummaryEntry Error
#define TRUE
Definition: scimark2.h:12
ALPAKA_FN_ACC ALPAKA_FN_INLINE void uint32_t const uint32_t CACellT< TrackerTraits > uint32_t CellNeighborsVector< TrackerTraits > CellTracksVector< TrackerTraits > HitsConstView< TrackerTraits > hh
rows
Definition: mysort.py:12

◆ SavePng()

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

Definition at line 185 of file FWTEveViewer.cc.

References geometryDiff::file, personalPlayback::fp, ALPAKA_ACCELERATOR_NAMESPACE::caPixelDoublets::hh, mps_fire::i, dqmiolumiharvest::j, mysort::rows, ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder::ww, and geometryCSVtoXML::xx.

Referenced by spawn_image_thread().

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

◆ spawn_image_thread()

void FWTEveViewer::spawn_image_thread ( )
private

Definition at line 77 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().

77  {
78  std::unique_lock<std::mutex> lko(m_moo);
79 
80  m_thr = new std::thread([=]() {
81  {
82  std::unique_lock<std::mutex> lk(m_moo);
83  m_cnd.notify_one();
84  }
85  while (true) {
86  {
87  std::unique_lock<std::mutex> lk(m_moo);
88  m_cnd.wait(lk);
89 
90  if (m_thr_exit) {
91  return;
92  }
93  }
94  if (m_name.EndsWith(".jpg")) {
96  } else {
98  }
99 
100  m_prom.set_value(0);
101  }
102  });
103 
104  m_cnd.wait(lko);
105 }
std::mutex m_moo
Definition: FWTEveViewer.h:74
TString m_name
Definition: FWTEveViewer.h:69
std::condition_variable m_cnd
Definition: FWTEveViewer.h:75
std::thread * m_thr
Definition: FWTEveViewer.h:72
std::vector< unsigned char > m_imgBuffer
Definition: FWTEveViewer.h:67
std::promise< int > m_prom
Definition: FWTEveViewer.h:73
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)

◆ SpawnFWTGLViewer()

FWTGLViewer * FWTEveViewer::SpawnFWTGLViewer ( )

Definition at line 109 of file FWTEveViewer.cc.

References m_fwGlViewer.

Referenced by FWEveView::FWEveView().

109  {
110  TGCompositeFrame* cf = GetGUICompositeFrame();
111 
112  m_fwGlViewer = new FWTGLViewer(cf);
113  SetGLViewer(m_fwGlViewer, m_fwGlViewer->GetFrame());
114 
115  cf->AddFrame(fGLViewerFrame, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY));
116 
117  fGLViewerFrame->MapWindow();
118 
119  if (fEveFrame == nullptr)
120  PreUndock();
121 
122  return m_fwGlViewer;
123 }
FWTGLViewer * m_fwGlViewer
Definition: FWTEveViewer.h:65

Member Data Documentation

◆ m_cnd

std::condition_variable FWTEveViewer::m_cnd
private

Definition at line 75 of file FWTEveViewer.h.

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

◆ m_fwGlViewer

FWTGLViewer* FWTEveViewer::m_fwGlViewer
private

Definition at line 65 of file FWTEveViewer.h.

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

◆ m_hh

int FWTEveViewer::m_hh
private

Definition at line 70 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

◆ m_imgBuffer

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

Definition at line 67 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

◆ m_moo

std::mutex FWTEveViewer::m_moo
private

Definition at line 74 of file FWTEveViewer.h.

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

◆ m_name

TString FWTEveViewer::m_name
private

Definition at line 69 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

◆ m_prom

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

Definition at line 73 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().

◆ m_thr

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

Definition at line 72 of file FWTEveViewer.h.

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

◆ m_thr_exit

bool FWTEveViewer::m_thr_exit = false
private

Definition at line 71 of file FWTEveViewer.h.

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

◆ m_ww

int FWTEveViewer::m_ww
private

Definition at line 70 of file FWTEveViewer.h.

Referenced by CaptureAndSaveImage(), and spawn_image_thread().