Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
// -*- C++ -*-
//
// Package:     Subsystem/Package
// Class  :     FWTEveViewer
//
// Implementation:
//     [Notes on implementation]
//
// Original Author:
//         Created:  Tue, 03 Feb 2015 21:46:04 GMT
//

// system include files

#include "png.h"
#include "jpeglib.h"

// user include files

#include "TMath.h"
#include "TGLIncludes.h"
#include "TGLFBO.h"

#include "Fireworks/Core/interface/FWTEveViewer.h"
#include "Fireworks/Core/interface/FWTGLViewer.h"

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
FWTEveViewer::FWTEveViewer(const char* n, const char* t) : TEveViewer(n, t), m_fwGlViewer(nullptr) {}

// FWTEveViewer::FWTEveViewer(const FWTEveViewer& rhs)
// {
//    // do actual copying here;
// }

FWTEveViewer::~FWTEveViewer() {
  if (m_thr)
    m_thr->detach();

  {
    std::unique_lock<std::mutex> lk(m_moo);

    m_thr_exit = true;
    m_cnd.notify_one();
  }

  delete m_thr;
}

//
// assignment operators
//
// const FWTEveViewer& FWTEveViewer::operator=(const FWTEveViewer& rhs)
// {
//   //An exception safe implementation is
//   FWTEveViewer temp(rhs);
//   swap(rhs);
//
//   return *this;
// }

//==============================================================================

//
// member functions
//

void FWTEveViewer::spawn_image_thread() {
  std::unique_lock<std::mutex> lko(m_moo);

  m_thr = new std::thread([this]() {
    {
      std::unique_lock<std::mutex> lk(m_moo);
      m_cnd.notify_one();
    }
    while (true) {
      {
        std::unique_lock<std::mutex> lk(m_moo);
        m_cnd.wait(lk);

        if (m_thr_exit) {
          return;
        }
      }
      if (m_name.EndsWith(".jpg")) {
        SaveJpg(m_name, &m_imgBuffer[0], m_ww, m_hh);
      } else {
        SavePng(m_name, &m_imgBuffer[0], m_ww, m_hh);
      }

      m_prom.set_value(0);
    }
  });

  m_cnd.wait(lko);
}

//------------------------------------------------------------------------------

FWTGLViewer* FWTEveViewer::SpawnFWTGLViewer() {
  TGCompositeFrame* cf = GetGUICompositeFrame();

  m_fwGlViewer = new FWTGLViewer(cf);
  SetGLViewer(m_fwGlViewer, m_fwGlViewer->GetFrame());

  cf->AddFrame(fGLViewerFrame, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY));

  fGLViewerFrame->MapWindow();

  if (fEveFrame == nullptr)
    PreUndock();

  return m_fwGlViewer;
}

std::future<int> FWTEveViewer::CaptureAndSaveImage(const TString& file, int height) {
  static const TString eh("FWTEveViewer::CaptureAndSaveImage");

  TGLFBO* fbo = nullptr;
  if (height == -1)
    fbo = m_fwGlViewer->MakeFbo();
  else
    fbo = m_fwGlViewer->MakeFboHeight(height);

  if (fbo == nullptr) {
    ::Error(eh, "Returned FBO is 0.");
    m_prom = std::promise<int>();
    m_prom.set_value(-1);
    return m_prom.get_future();
  }

  int ww, hh;
  if (fbo->GetIsRescaled()) {
    ww = TMath::Nint(fbo->GetW() * fbo->GetWScale());
    hh = TMath::Nint(fbo->GetH() * fbo->GetHScale());
  } else {
    ww = fbo->GetW();
    hh = fbo->GetH();
  }

  fbo->SetAsReadBuffer();

  size_t bufsize = 3 * ww * hh;
  if (bufsize != m_imgBuffer.size()) {
    m_imgBuffer.resize(bufsize);
  }

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glReadPixels(0, 0, ww, hh, GL_RGB, GL_UNSIGNED_BYTE, &m_imgBuffer[0]);

  if (m_thr == nullptr)
    spawn_image_thread();

  {
    std::unique_lock<std::mutex> lk(m_moo);

    m_prom = std::promise<int>();
    m_name = file;
    m_ww = ww;
    m_hh = hh;

    m_cnd.notify_one();
  }

  return m_prom.get_future();
}

//
// const member functions
//

//
// static member functions
//

bool FWTEveViewer::SavePng(const TString& file, UChar_t* xx, int ww, int hh) {
  png_structp png_ptr;
  png_infop info_ptr;

  /* Create and initialize the png_struct with the desired error handler
    * functions.  If you want to use the default stderr and longjump method,
    * you can supply NULL for the last three parameters.  We also check that
    * the library version is compatible with the one used at compile time,
    * in case we are using dynamically linked libraries.  REQUIRED.
    */
  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  if (png_ptr == nullptr) {
    printf("Error creating png write struct\n");
    return false;
  }

  // Allocate/initialize the image information data.      REQUIRED
  info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == nullptr) {
    printf("Error creating png info struct\n");
    png_destroy_write_struct(&png_ptr, &info_ptr);
    return false;
  }

  /*// Set error handling.  REQUIRED if you aren't supplying your own
   //      error handling functions in the png_create_write_struct() call.
   if (setjmp(png_jmpbuf(png_ptr))) {
   // If we get here, we had a problem reading the file
   png_destroy_write_struct(&png_ptr, &info_ptr);
   ilSetError(IL_LIB_PNG_ERROR);
   return IL_FALSE;
   }*/

  FILE* fp = fopen(file, "w");

  png_init_io(png_ptr, fp);

  // Use PNG_INTERLACE_ADAM7 for interlacing
  png_set_IHDR(png_ptr,
               info_ptr,
               ww,
               hh,
               8,
               PNG_COLOR_TYPE_RGB,
               PNG_INTERLACE_NONE,
               PNG_COMPRESSION_TYPE_BASE,
               PNG_FILTER_TYPE_BASE);

  /* Optional gamma chunk is strongly suggested if you have any guess
    * as to the correct gamma of the image.
    */
  // png_set_gAMA(png_ptr, info_ptr, gamma);

  // Optionally write comments into the image.
  // png_text text;
  // text.key  = "Generated by";
  // text.text = "Generated by cmsShow";
  // text.compression = PNG_TEXT_COMPRESSION_NONE;
  // png_set_text(png_ptr, info_ptr, &text, 1);

  // Write the file header information.  REQUIRED.
  png_write_info(png_ptr, info_ptr);

  std::vector<UChar_t*> rows(hh);
  {
    int j = hh - 1;
    for (int i = 0; i < hh; i++, j--) {
      rows[i] = xx + j * ww * 3;
    }
  }

  // Writes the image.
  png_write_image(png_ptr, &rows[0]);

  // It is REQUIRED to call this to finish writing the rest of the file
  png_write_end(png_ptr, info_ptr);

  // clean up after the write, and ifree any memory allocated
  png_destroy_write_struct(&png_ptr, &info_ptr);

  fclose(fp);

  return true;
}

bool FWTEveViewer::SaveJpg(const TString& file, UChar_t* xx, int ww, int hh) {
  struct jpeg_compress_struct JpegInfo;
  struct jpeg_error_mgr Error;

  JpegInfo.err = jpeg_std_error(&Error);

  // Now we can initialize the JPEG compression object.
  jpeg_create_compress(&JpegInfo);

  FILE* fp = fopen(file, "w");
  jpeg_stdio_dest(&JpegInfo, fp);

  JpegInfo.image_width = ww;
  JpegInfo.image_height = hh;
  JpegInfo.input_components = 3;
  JpegInfo.in_color_space = JCS_RGB;

  jpeg_set_defaults(&JpegInfo);

  JpegInfo.write_JFIF_header = TRUE;

  // Set the quality output
  // const int quality = 98;
  // jpeg_set_quality(&JpegInfo, quality, true); // bool force_baseline ????

  jpeg_start_compress(&JpegInfo, TRUE);

  std::vector<UChar_t*> rows(hh);
  {
    int j = hh - 1;
    for (int i = 0; i < hh; i++, j--) {
      rows[i] = xx + j * ww * 3;
    }
  }

  jpeg_write_scanlines(&JpegInfo, &rows[0], hh);

  // Step 6: Finish compression
  jpeg_finish_compress(&JpegInfo);

  // Step 7: release JPEG compression object

  // This is an important step since it will release a good deal of memory.
  jpeg_destroy_compress(&JpegInfo);

  return true;
}