Openholo  v2.1
Open Source Digital Holographic Library
ophGen.h
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install, copy or use the software.
7 //
8 //
9 // License Agreement
10 // For Open Source Digital Holographic Library
11 //
12 // Openholo library is free software;
13 // you can redistribute it and/or modify it under the terms of the BSD 2-Clause license.
14 //
15 // Copyright (C) 2017-2024, Korea Electronics Technology Institute. All rights reserved.
16 // E-mail : contact.openholo@gmail.com
17 // Web : http://www.openholo.org
18 //
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
21 //
22 // 1. Redistribution's of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistribution's in binary form must reproduce the above copyright notice,
26 // this list of conditions and the following disclaimer in the documentation
27 // and/or other materials provided with the distribution.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the copyright holder or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 // This software contains opensource software released under GNU Generic Public License,
41 // NVDIA Software License Agreement, or CUDA supplement to Software License Agreement.
42 // Check whether software you use contains licensed software.
43 //
44 //M*/
45 
46 #ifndef __ophGen_h
47 #define __ophGen_h
48 
49 #include "Openholo.h"
50 
51 #ifdef GEN_EXPORT
52 #define GEN_DLL __declspec(dllexport)
53 #else
54 #define GEN_DLL __declspec(dllimport)
55 #endif
56 
57 struct OphPointCloudConfig;
58 struct OphPointCloudData;
59 struct OphDepthMapConfig;
60 struct OphMeshData;
61 struct OphWRPConfig;
62 
68 class GEN_DLL ophGen : public Openholo
69 {
70 public:
71  enum PRECISION {
72  SINGLE,
73  DOUBLE,
74  };
75 
76  enum ENCODE_FLAG {
77  ENCODE_PHASE,
78  ENCODE_AMPLITUDE,
79  ENCODE_REAL,
80  ENCODE_IMAGINEARY,
81  ENCODE_SIMPLENI,
82  ENCODE_BURCKHARDT,
83  ENCODE_TWOPHASE,
84  ENCODE_SSB,
85  ENCODE_OFFSSB,
86  ENCODE_SIMPLEBINARY,
87  ENCODE_EDBINARY
88  };
89 
90 public:
94  explicit ophGen(void);
95 
96 protected:
100  virtual ~ophGen(void) = 0;
101 
102 public:
109  inline Real** getEncodedBuffer(void) { return m_lpEncoded; }
116  inline uchar** getNormalizedBuffer(void) { return m_lpNormalized; }
117 
121  void initialize(void);
122 
131  int loadPointCloud(const char* pc_file, OphPointCloudData *pc_data_);
132 
140  bool readConfig(const char* fname);
141 
151  void AngularSpectrumMethod(Complex<Real>* input, Complex<Real>* output, Real distance, Real k, Real lambda);
152 
160  void conv_fft2(Complex<Real>* src1, Complex<Real>* src2, Complex<Real>* dst, ivec2 size);
161 
162 
166  void normalize(void);
167  void normalize(int ch);
168 
180  bool save(const char* fname, uint8_t bitsperpixel = 8, uchar* src = nullptr, uint px = 0, uint py = 0);
181 
186  void* load(const char* fname);
187 
195  virtual bool loadAsOhc(const char *fname);
196 
197 protected:
208  bool save(const char* fname, uint8_t bitsperpixel, uint px, uint py, uint fnum, uchar* args ...);
209 
214  void resetBuffer();
215 
216 
217 public:
222  void setPrecision(uint precision) { m_precision = precision; }
223  uint getPrecision() { return m_precision; }
224 
225  void setEncodeMethod(unsigned int ENCODE_FLAG) { ENCODE_METHOD = ENCODE_FLAG; }
226 
240  //void encoding(unsigned int ENCODE_FLAG, Complex<Real>* holo = nullptr);
241  void encoding(unsigned int ENCODE_FLAG);
242  void encoding(unsigned int ENCODE_FLAG, Complex<Real>* holo, Real* encoded);
243  //template<typename T>
244  //void encoding(unsigned int ENCODE_FLAG, Complex<T>* holo = nullptr, T* encoded = nullptr);
245 
246  void encoding();
247  /*
248  * @brief Encoding Functions
249  * @details
250  * ENCODE_SSB : Single Side Band Encoding@n
251  * ENCODE_OFFSSB : Off-axis + Single Side Band Encoding@n
252  * @param[in] ENCODE_FLAG encoding method.
253  * @param[in] SSB_PASSBAND shift direction.
254  * @param[in] holo buffer to encode.
255  * @overload
256  */
257  virtual void encoding(unsigned int ENCODE_FLAG, unsigned int SSB_PASSBAND, Complex<Real>* holo = nullptr, Real* encoded = nullptr);
258  enum SSB_PASSBAND { SSB_LEFT, SSB_RIGHT, SSB_TOP, SSB_BOTTOM };
259 
277  void encoding(unsigned int BIN_ENCODE_FLAG, unsigned int ENCODE_FLAG, Real threshold, Complex<Real>* holo = nullptr, Real* encoded = nullptr);
278 
279 public:
280 
281  bool Shift(Real x, Real y);
288  void waveCarry(Real carryingAngleX, Real carryingAngleY, Real distance);
289 
290  void waveCarry(Complex<Real>* src, Complex<Real>* dst, Real wavelength, int carryIdxX, int carryIdxY);
291 protected:
293  ivec2 m_vecEncodeSize;
295  int ENCODE_METHOD;
297  int SSB_PASSBAND;
299  Real m_elapsedTime;
301  Real** m_lpEncoded;
303  uchar** m_lpNormalized;
304 
305 private:
307  int m_nOldChannel;
308  uint m_precision;
309 
310 protected:
311  Real m_dFieldLength;
312  int m_nStream;
313 
315  Complex<Real>* src1FT;
316  Complex<Real>* src2FT;
317  Complex<Real>* dstFT;
318 public:
319  void transVW(int nSize, Real *dst, Real *src);
320  int getStream() { return m_nStream; }
321  Real getFieldLength() { return m_dFieldLength; }
328  ivec2& getEncodeSize(void) { return m_vecEncodeSize; }
329 
334  void setResolution(ivec2 resolution);
335 
341  Real getElapsedTime() { return m_elapsedTime; };
342 
343 protected:
350  template <typename T>
351  void RealPart(Complex<T>* holo, T* encoded, const int size);
352  template <typename T>
353  void ImaginearyPart(Complex<T>* holo, T* encoded, const int size);
354  template <typename T>
355  void Phase(Complex<T>* holo, T* encoded, const int size);
356  template <typename T>
357  void Amplitude(Complex<T>* holo, T* encoded, const int size);
358  template <typename T>
359  void TwoPhase(Complex<T>* holo, T* encoded, const int size);
360  template <typename T>
361  void Burckhardt(Complex<T>* holo, T* encoded, const int size);
362  template <typename T>
363  void SimpleNI(Complex<T>* holo, T* encoded, const int size);
364 
372  void singleSideBand(Complex<Real>* holo, Real* encoded, const ivec2 holosize, int passband);
373 
382  void freqShift(Complex<Real>* src, Complex<Real>* dst, const ivec2 holosize, int shift_x, int shift_y);
383 
384 
385 public:
386  enum ED_WType { FLOYD_STEINBERG, SINGLE_RIGHT, SINGLE_DOWN, ITERATIVE_DESIGN };
387  bool saveRefImages(char* fnameW, char* fnameWC, char* fnameAS, char* fnameSSB, char* fnameHP, char* fnameFreq, char* fnameReal, char* fnameBin, char* fnameReconBin, char* fnameReconErr, char* fnameReconNo);
388 
389 protected:
391  int ss;
392  Complex<Real>* AS;
393  Complex<Real>* normalized;
394  Complex<Real>* fftTemp;
395  Real* weight;
396  Complex<Real>* weightC;
397  Complex<Real>* freqW;
398  Real* realEnc;
399  Real* binary;
400  Real* maskSSB;
401  Real* maskHP;
402  unsigned int m_mode;
403  bool m_bRandomPhase;
404 
405  bool binaryErrorDiffusion(Complex<Real>* holo, Real* encoded, const ivec2 holosize, const int type, Real threshold);
406  bool getWeightED(const ivec2 holosize, const int type, ivec2* pNw);
407  bool shiftW(ivec2 holosize);
408  void binarization(Complex<Real>* src, Real* dst, const int size, int ENCODE_FLAG, Real threshold);
409  void CorrectionChromaticAberration(uchar* src, uchar* dst, int width, int height, int ch);
410 
411  //public:
412  //bool carrierWaveMultiplexingEncoding(char* dirName, uint ENCODE_METHOD, Real cenFxIdx, Real cenFyIdx, Real stepFx, Real stepFy, int nFx, int nFy);
413  //bool carrierWaveMultiplexingEncoding(char* dirName, uint ENCODE_METHOD, uint PASSBAND, Real cenFxIdx, Real cenFyIdx, Real stepFx, Real stepFy, int nFx, int nFy);
414 
415  //protected:
416 
417 
418 
419 public:
427  void fresnelPropagation(OphConfig context, Complex<Real>* in, Complex<Real>* out, Real distance);
435  void fresnelPropagation(Complex<Real>* in, Complex<Real>* out, Real distance, uint channel);
436 protected:
444  void encodeSideBand(bool bCPU, ivec2 sig_location);
456  void encodeSideBand_CPU(int cropx1, int cropx2, int cropy1, int cropy2, ivec2 sig_location);
457 
469  void encodeSideBand_GPU(int cropx1, int cropx2, int cropy1, int cropy2, ivec2 sig_location);
470 
478  void getShiftPhaseValue(Complex<Real>& shift_phase_val, int idx, ivec2 sig_location);
479 
487  void GetRandomPhaseValue(Complex<Real>& rand_phase_val, bool rand_phase);
488 
489  void ScaleChange(Real *src, Real *dst, int nSize, Real scaleX, Real scaleY, Real scaleZ);
490  void GetMaxMin(Real *src, int len, Real& max, Real& min);
491 
492 public:
493  void AngularSpectrumMethod(Complex<Real> *src, Complex<Real> *dst, Real lambda, Real distance);
494  void RS_Diffraction(vec3 src, Complex<Real> *dst, Real lambda, Real distance, Real amplitude);
495  void RS_Diffraction(vec3 src, Complex<Real> *dst, float lambda, float distance, float amplitude);
496  void RS_Diffraction(uchar *src, Complex<Real> *dst, Real lambda, Real distance);
497  void Fresnel_Diffraction(vec3 src, Complex<Real> *dst, Real lambda, Real distance, Real amplitude);
498  void Fresnel_FFT(Complex<Real> *src, Complex<Real> *dst, Real lambda, Real waveRatio, Real distance);
499 
500  bool readImage(const char* fname, bool bRGB);
501  void SetMode(unsigned int mode) { m_mode = mode; }
502  unsigned int GetMode() { return m_mode; }
503  void SetRandomPhase(bool bRandomPhase) { m_bRandomPhase = bRandomPhase; }
504  bool GetRandomPhase() { return m_bRandomPhase; }
505 
506  uchar* imgRGB;
507  uchar* imgDepth;
508  int m_width;
509  int m_height;
510  int m_bpp;
511 protected:
515  virtual void ophFree(void);
516 };
517 
524  vec3 scale;
526  Real distance;
528  int8_t* filter_shape_flag;
530  vec2 filter_width;
532  Real focal_length_lens_in;
534  Real focal_length_lens_out;
536  Real focal_length_lens_eye_piece;
538  vec2 tilt_angle;
539 
541  : scale(0, 0, 0), distance(0), filter_shape_flag(0), focal_length_lens_in(0), focal_length_lens_out(0), focal_length_lens_eye_piece(0), tilt_angle(0, 0)
542  {}
543 };
544 
549 struct GEN_DLL OphPointCloudData {
551  ulonglong n_points;
553  int n_colors;
555  Real *vertex;
557  Real *color;
559  Real *phase;
561  bool isPhaseParse;
562 
563  OphPointCloudData() :vertex(nullptr), color(nullptr), phase(nullptr) { n_points = 0; n_colors = 0; isPhaseParse = 0; }
564 };
565 
585 struct GEN_DLL OphDepthMapConfig {
587  Real fieldLength;
589  Real near_depthmap;
591  Real far_depthmap;
597  uint num_of_depth;
599  vector<int> render_depth;
601  bool change_depth_quantization;
603  uint default_depth_quantization;
605  uint num_of_depth_quantization;
607  bool random_phase;
608 
609  OphDepthMapConfig() :fieldLength(0), near_depthmap(0), far_depthmap(0), num_of_depth(0) {}
610 };
611 
616 struct GEN_DLL OphMeshData {
618  ulonglong n_faces = 0;
620  int color_channels;
622  uint* face_idx;
624  Real* vertex;
626  Real* color;
627 };
628 
633 struct GEN_DLL OphWRPConfig {
635  Real fieldLength;
637  vec3 scale;
639  int num_wrp;
641  Real wrp_location;
643  Real propagation_distance;
644 
645 };
646 
650 struct GEN_DLL OphIFTAConfig {
652  Real near_depthmap;
654  Real far_depthmap;
656  int num_of_depth;
657 
658  int num_of_iteration;
659 
660  OphIFTAConfig() :near_depthmap(0), far_depthmap(0), num_of_depth(0), num_of_iteration(0) {}
661 };
662 #endif // !__ophGen_h
663 
ENCODE_FLAG
Definition: ophGen.h:76
Abstract class.
Definition: Openholo.h:88
#define GEN_DLL
Definition: ophGen.h:54
unsigned char uchar
Definition: typedef.h:64
Configuration for WRP.
Definition: ophGen.h:633
uchar ** getNormalizedBuffer(void)
Function for getting the normalized(0~255) complex field buffer.
Definition: ophGen.h:116
ivec2 & getEncodeSize(void)
Function for getting encode size.
Definition: ophGen.h:328
unsigned int GetMode()
Definition: ophGen.h:502
bool GetRandomPhase()
Definition: ophGen.h:504
void normalize(const Complex< T > *src, Complex< T > *dst, const int &size)
Normalize all elements of Complex<T>* src from 0 to 1.
Definition: function.h:176
void SetMode(unsigned int mode)
Definition: ophGen.h:501
PRECISION
Definition: ophGen.h:71
Real getFieldLength()
Definition: ophGen.h:321
def k(wvl)
Definition: Depthmap.py:16
void setEncodeMethod(unsigned int ENCODE_FLAG)
Definition: ophGen.h:225
unsigned long long ulonglong
Definition: typedef.h:67
Real ** getEncodedBuffer(void)
Function for getting the encoded complex field buffer.
Definition: ophGen.h:109
OphIFTAConfig()
Definition: ophGen.h:660
ED_WType
Definition: ophGen.h:386
uint getPrecision()
Definition: ophGen.h:223
Real getElapsedTime()
Function for getting elapsed time.
Definition: ophGen.h:341
Data for triangular mesh.
Definition: ophGen.h:616
__managed__ float amplitude
Definition: ophPAS_GPU.cpp:28
SSB_PASSBAND
Passband in Single-side band encoding.
Definition: ophGen.h:258
int getStream()
Definition: ophGen.h:320
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Definition: Openholo.cpp:321
Configuration for Point Cloud.
Definition: ophGen.h:522
Data for Point Cloud.
Definition: ophGen.h:549
float Real
Definition: typedef.h:55
void SetRandomPhase(bool bRandomPhase)
Definition: ophGen.h:503
void setPrecision(uint precision)
Function for setting precision.
Definition: ophGen.h:222
unsigned int uint
Definition: typedef.h:62
virtual void ophFree(void)
Pure virtual function for override in child classes.
Definition: Openholo.cpp:845
Definition: ophGen.h:68
Configuration for Depth Map.
Definition: ophGen.h:585