Openholo  v4.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 _WIN64
52 #ifdef GEN_EXPORT
53 #define GEN_DLL __declspec(dllexport)
54 #else
55 #define GEN_DLL __declspec(dllimport)
56 #endif
57 #else
58 #ifdef GEN_EXPORT
59 #define GEN_DLL __attribute__((visibility("default")))
60 #else
61 #define GEN_DLL
62 #endif
63 #endif
64 
65 struct OphPointCloudConfig;
66 struct OphPointCloudData;
67 struct OphDepthMapConfig;
68 struct OphMeshData;
69 struct OphWRPConfig;
70 
76 class GEN_DLL ophGen : public Openholo
77 {
78 public:
79  enum PRECISION {
80  SINGLE,
81  DOUBLE,
82  };
83 
84  enum ENCODE_FLAG {
85  ENCODE_PHASE,
86  ENCODE_AMPLITUDE,
87  ENCODE_REAL,
88  ENCODE_IMAGINARY,
89  ENCODE_SIMPLENI,
90  ENCODE_BURCKHARDT,
91  ENCODE_TWOPHASE,
92  ENCODE_SSB,
93  ENCODE_OFFSSB,
94  ENCODE_SIMPLEBINARY,
95  ENCODE_EDBINARY
96  };
97 
98 public:
102  explicit ophGen(void);
103 
104 protected:
108  virtual ~ophGen(void) = 0;
109 
110 public:
117  Real** getEncodedBuffer(void) { return m_lpEncoded; }
124  uchar** getNormalizedBuffer(void) { return m_lpNormalized; }
125 
129  void initialize(void);
130 
139  int loadPointCloud(const char* pc_file, OphPointCloudData *pc_data_);
140 
148  bool readConfig(const char* fname);
149 
158  void RS_Diffraction(Point src, Complex<Real> *dst, Real lambda, Real distance, Real amplitude);
159 
168  void Fresnel_Diffraction(Point src, Complex<Real> *dst, Real lambda, Real distance, Real amplitude);
169 
178  void Fresnel_FFT(Complex<Real> *src, Complex<Real> *dst, Real lambda, Real waveRatio, Real distance);
179 
180 
189  void AngularSpectrumMethod(Complex<Real>* src, Complex<Real>* dst, Real lambda, Real distance);
190 
198  void conv_fft2(Complex<Real>* src1, Complex<Real>* src2, Complex<Real>* dst, ivec2 size);
199 
200 
204  void normalize(void);
205 
217  bool save(const char* fname, uint8_t bitsperpixel = 8, uchar* src = nullptr, uint px = 0, uint py = 0);
218 
223  void* load(const char* fname);
224 
232  virtual bool loadAsOhc(const char *fname);
233 
234 protected:
245  bool save(const char* fname, uint8_t bitsperpixel, uint px, uint py, uint fnum, uchar* args ...);
246 
251  void resetBuffer();
252 
253 
254 public:
259  void setPrecision(uint precision) { m_precision = precision; }
260  uint getPrecision() { return m_precision; }
261 
262  void setEncodeMethod(unsigned int ENCODE_FLAG) { ENCODE_METHOD = ENCODE_FLAG; }
263 
277  //void encoding(unsigned int ENCODE_FLAG, Complex<Real>* holo = nullptr);
278  void encoding(unsigned int ENCODE_FLAG);
279  void encoding(unsigned int ENCODE_FLAG, Complex<Real>* holo, Real* encoded);
280  //template<typename T>
281  //void encoding(unsigned int ENCODE_FLAG, Complex<T>* holo = nullptr, T* encoded = nullptr);
282 
283  void encoding();
284  /*
285  * @brief Encoding Functions
286  * @details
287  * ENCODE_SSB : Single Side Band Encoding@n
288  * ENCODE_OFFSSB : Off-axis + Single Side Band Encoding@n
289  * @param[in] ENCODE_FLAG encoding method.
290  * @param[in] SSB_PASSBAND shift direction.
291  * @param[in] holo buffer to encode.
292  * @overload
293  */
294  virtual void encoding(unsigned int ENCODE_FLAG, unsigned int SSB_PASSBAND, Complex<Real>* holo = nullptr, Real* encoded = nullptr);
295  enum SSB_PASSBAND { SSB_LEFT, SSB_RIGHT, SSB_TOP, SSB_BOTTOM };
296 
314  void encoding(unsigned int BIN_ENCODE_FLAG, unsigned int ENCODE_FLAG, Real threshold, Complex<Real>* holo = nullptr, Real* encoded = nullptr);
315 
316 public:
317 
318  bool Shift(Real x, Real y);
325  void waveCarry(Real carryingAngleX, Real carryingAngleY, Real distance);
326 
327  void waveCarry(Complex<Real>* src, Complex<Real>* dst, Real wavelength, int carryIdxX, int carryIdxY);
328 protected:
330  ivec2 m_vecEncodeSize;
332  int ENCODE_METHOD;
334  int SSB_PASSBAND;
336  Real m_elapsedTime;
338  Real** m_lpEncoded;
340  uchar** m_lpNormalized;
341 
342 private:
344  uint m_nOldChannel;
345  uint m_precision;
346 
347 protected:
348  Real m_dFieldLength;
349  int m_nStream;
350  unsigned int m_mode;
351 
352 public:
353  void transVW(int nVertex, Vertex *dst, Vertex *src);
354  void transVW(int nVertex, Real *dst, Real *src);
355  int getStream() { return m_nStream; }
356  Real getFieldLength() { return m_dFieldLength; }
363  ivec2& getEncodeSize(void) { return m_vecEncodeSize; }
364 
369  void setResolution(ivec2 resolution);
370 
376  Real getElapsedTime() { return m_elapsedTime; };
377 
378 protected:
385  template <typename T>
386  void RealPart(Complex<T>* holo, T* encoded, const int size);
387  template <typename T>
388  void ImaginaryPart(Complex<T>* holo, T* encoded, const int size);
389  template <typename T>
390  void Phase(Complex<T>* holo, T* encoded, const int size);
391  template <typename T>
392  void Amplitude(Complex<T>* holo, T* encoded, const int size);
393  template <typename T>
394  void TwoPhase(Complex<T>* holo, T* encoded, const int size);
395  template <typename T>
396  void Burckhardt(Complex<T>* holo, T* encoded, const int size);
397  template <typename T>
398  void SimpleNI(Complex<T>* holo, T* encoded, const int size);
399 
407  void singleSideBand(Complex<Real>* holo, Real* encoded, const ivec2 holosize, int passband);
408 
417  void freqShift(Complex<Real>* holo, Complex<Real>* encoded, const ivec2 holosize, int shift_x, int shift_y);
418 
419 
420 public:
421  enum ED_WType { FLOYD_STEINBERG, SINGLE_RIGHT, SINGLE_DOWN, ITERATIVE_DESIGN };
422  bool saveRefImages(char* fnameW, char* fnameWC, char* fnameAS, char* fnameSSB, char* fnameHP, char* fnameFreq, char* fnameReal, char* fnameBin, char* fnameReconBin, char* fnameReconErr, char* fnameReconNo);
423 
424 protected:
426  Complex<Real>* AS;
427  Complex<Real>* normalized;
428  Complex<Real>* fftTemp;
429  Real* weight;
430  Complex<Real>* weightC;
431  Complex<Real>* freqW;
432  Real* realEnc;
433  Real* binary;
434  Real* maskSSB;
435  Real* maskHP;
436  bool m_bRandomPhase;
437 
438  bool binaryErrorDiffusion(Complex<Real>* holo, Real* encoded, const ivec2 holosize, const int type, Real threshold);
439  bool getWeightED(const ivec2 holosize, const int type, ivec2* pNw);
440  bool shiftW(ivec2 holosize);
441  void binarization(Complex<Real>* src, Real* dst, const int size, int ENCODE_FLAG, Real threshold);
442  void CorrectionChromaticAberration(uchar* src, uchar* dst, int width, int height, int ch);
443 
444  //public:
445  //bool carrierWaveMultiplexingEncoding(char* dirName, uint ENCODE_METHOD, Real cenFxIdx, Real cenFyIdx, Real stepFx, Real stepFy, int nFx, int nFy);
446  //bool carrierWaveMultiplexingEncoding(char* dirName, uint ENCODE_METHOD, uint PASSBAND, Real cenFxIdx, Real cenFyIdx, Real stepFx, Real stepFy, int nFx, int nFy);
447 
448  //protected:
449 
450 
451 
452 public:
460  void fresnelPropagation(OphConfig context, Complex<Real>* in, Complex<Real>* out, Real distance);
468  void fresnelPropagation(Complex<Real>* in, Complex<Real>* out, Real distance, uint channel);
469 protected:
477  void encodeSideBand(bool bCPU, ivec2 sig_location);
489  void encodeSideBand_CPU(int cropx1, int cropx2, int cropy1, int cropy2, ivec2 sig_location);
490 
502  void encodeSideBand_GPU(int cropx1, int cropx2, int cropy1, int cropy2, ivec2 sig_location);
503 
511  void getShiftPhaseValue(Complex<Real>& shift_phase_val, int idx, ivec2 sig_location);
512 
520  void GetRandomPhaseValue(Complex<Real>& rand_phase_val, bool rand_phase);
521 
522  void ScaleChange(Real *src, Real *dst, int nSize, Real scaleX, Real scaleY, Real scaleZ);
523  void GetMaxMin(Real *src, int len, Real& max, Real& min);
524 
525 public:
529  void SetRandomPhase(bool bRandomPhase) { m_bRandomPhase = bRandomPhase; }
530 
537  bool GetRandomPhase() { return m_bRandomPhase; }
538  void SetMode(unsigned int mode) { m_mode = mode; }
539  unsigned int GetMode() { return m_mode; }
540 
541 
542 
543 protected:
544 
548  virtual void ophFree(void);
549 };
550 
557  vec3 scale;
559  Real distance;
561  int8_t* filter_shape_flag;
563  vec2 filter_width;
565  Real focal_length_lens_in;
567  Real focal_length_lens_out;
569  Real focal_length_lens_eye_piece;
571  vec2 tilt_angle;
572 
574  : scale(0.0, 0.0, 0.0), distance(0.0), filter_shape_flag(0), focal_length_lens_in(0.0), focal_length_lens_out(0.0), focal_length_lens_eye_piece(0.0), tilt_angle(0.0, 0.0)
575  {}
576 };
577 
582 struct GEN_DLL OphPointCloudData {
584  ulonglong n_points;
586  int n_colors;
588  Vertex* vertices;
589 
590  OphPointCloudData() : n_points(0), n_colors(0), vertices(nullptr) {}
591 };
592 
612 struct GEN_DLL OphDepthMapConfig {
614  Real fieldLength;
616  Real near_depthmap;
618  Real far_depthmap;
624  uint num_of_depth;
626  vector<int> render_depth;
628  bool change_depth_quantization;
630  uint default_depth_quantization;
632  uint num_of_depth_quantization;
634  bool random_phase;
635 
636  OphDepthMapConfig() :fieldLength(0.0), near_depthmap(0.0), far_depthmap(0.0), num_of_depth(0), change_depth_quantization(false)
637  , default_depth_quantization(0), num_of_depth_quantization(0), random_phase(false) {}
638 };
639 
644 struct GEN_DLL OphMeshData {
646  ulonglong n_faces = 0;
647  Face* faces;
648  OphMeshData() : faces(nullptr) { n_faces = 0; }
649 };
650 
655 struct GEN_DLL OphWRPConfig {
657  Real fieldLength;
659  vec3 scale;
661  int num_wrp;
663  Real wrp_location;
665  Real propagation_distance;
666 
668  : fieldLength(0.0), scale(0.0, 0.0, 0.0), num_wrp(0), wrp_location(0.0), propagation_distance(0.0)
669  { }
670 
671  OphWRPConfig(Real _fieldLength, vec3 _scale, int _num_wrp, Real _wrp_location, Real _propagation_distance)
672  : fieldLength(_fieldLength), scale(_scale), num_wrp(_num_wrp), wrp_location(_wrp_location), propagation_distance(_propagation_distance)
673  { }
674 };
675 
679 struct GEN_DLL OphIFTAConfig {
681  Real near_depthmap;
683  Real far_depthmap;
685  int num_of_depth;
686 
687  int num_of_iteration;
688 
689  OphIFTAConfig() :near_depthmap(0), far_depthmap(0), num_of_depth(0), num_of_iteration(0) {}
690  OphIFTAConfig(Real _near_depthmap, Real _far_depthmap, int _num_of_depth, int _num_of_iteration)
691  :near_depthmap(_near_depthmap), far_depthmap(_far_depthmap), num_of_depth(_num_of_depth), num_of_iteration(_num_of_iteration) {}
692 };
693 
694 
695 #endif // !__ophGen_h
696 
ENCODE_FLAG
Definition: ophGen.h:84
Abstract class.
Definition: Openholo.h:145
OphWRPConfig(Real _fieldLength, vec3 _scale, int _num_wrp, Real _wrp_location, Real _propagation_distance)
Definition: ophGen.h:671
#define GEN_DLL
Definition: ophGen.h:61
OphIFTAConfig(Real _near_depthmap, Real _far_depthmap, int _num_of_depth, int _num_of_iteration)
Definition: ophGen.h:690
unsigned char uchar
Definition: typedef.h:64
Configuration for WRP.
Definition: ophGen.h:655
uchar ** getNormalizedBuffer(void)
Function for getting the normalized(0~255) complex field buffer.
Definition: ophGen.h:124
ivec2 & getEncodeSize(void)
Function for getting encode size.
Definition: ophGen.h:363
Definition: struct.h:86
OphWRPConfig()
Definition: ophGen.h:667
unsigned int GetMode()
Definition: ophGen.h:539
structure for 2-dimensional integer vector and its arithmetic.
Definition: ivec.h:66
bool GetRandomPhase()
Function for getting the random phase.
Definition: ophGen.h:537
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:538
PRECISION
Definition: ophGen.h:79
Real getFieldLength()
Definition: ophGen.h:356
void setEncodeMethod(unsigned int ENCODE_FLAG)
Definition: ophGen.h:262
OphMeshData()
Definition: ophGen.h:648
unsigned long long ulonglong
Definition: typedef.h:67
Real ** getEncodedBuffer(void)
Function for getting the encoded complex field buffer.
Definition: ophGen.h:117
OphIFTAConfig()
Definition: ophGen.h:689
ED_WType
Definition: ophGen.h:421
uint getPrecision()
Definition: ophGen.h:260
structure for 2-dimensional Real type vector and its arithmetic.
Definition: vec.h:66
Definition: struct.h:102
Real getElapsedTime()
Function for getting elapsed time.
Definition: ophGen.h:376
Data for triangular mesh.
Definition: ophGen.h:644
structure for 3-dimensional Real type vector and its arithmetic.
Definition: vec.h:466
SSB_PASSBAND
Passband in Single-side band encoding.
Definition: ophGen.h:295
int getStream()
Definition: ophGen.h:355
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Definition: Openholo.cpp:280
Definition: struct.h:115
Configuration for Point Cloud.
Definition: ophGen.h:555
Data for Point Cloud.
Definition: ophGen.h:582
float Real
Definition: typedef.h:55
void SetRandomPhase(bool bRandomPhase)
Function for setting the random phase.
Definition: ophGen.h:529
void setPrecision(uint precision)
Function for setting precision.
Definition: ophGen.h:259
unsigned int uint
Definition: typedef.h:62
virtual void ophFree(void)
Pure virtual function for override in child classes.
Definition: Openholo.cpp:805
Definition: ophGen.h:76
Configuration for Depth Map.
Definition: ophGen.h:612