Openholo  v4.2
Open Source Digital Holographic Library
Openholo.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 
47 #ifndef __Openholo_h
48 #define __Openholo_h
49 
50 #include "Base.h"
51 #include "include.h"
52 #include "vec.h"
53 #include "ivec.h"
54 #include "fftw3.h"
55 #include "ImgCodecOhc.h"
56 
57 using namespace oph;
58 
60 {
63  int pnX;
64  int pnY;
65  int half_pnX;
66  int half_pnY;
67  long long int size;
68  long long int half_size;
69  long long int double_size;
70 
72  this->pnX = 0;
73  this->pnY = 0;
74  this->half_pnX = 0;
75  this->half_pnY = 0;
76  this->double_pnX = 01;
77  this->double_pnY = 01;
78  this->size = 0;
79  this->half_size = 0;
80  this->double_size = 0;
81  }
82 
83  ResolutionConfig(const ivec2& pixel_number) {
84  this->pnX = pixel_number[_X];
85  this->pnY = pixel_number[_Y];
86  this->half_pnX = pnX >> 1;
87  this->half_pnY = pnY >> 1;
88  this->double_pnX = pnX << 1;
89  this->double_pnY = pnY << 1;
90  this->size = pnX * pnY;
91  this->half_size = size >> 2;
92  this->double_size = size << 2;
93  }
94 };
95 
97 {
98  bool bUseDP; // use double precision
99  ivec2 pixel_number; // SLM_PIXEL_NUMBER_X & SLM_PIXEL_NUMBER_Y
100  ivec2 offset; // start offset
101  vec2 pixel_pitch; // SLM_PIXEL_PITCH_X & SLM_PIXEL_PITCH_Y
102  vec3 shift; // shift
103  Real k; // 2 * PI / lambda(wavelength)
104  vec2 ss; // pn * pp
105  uint waveNum; // wave num
106  Real* wave_length; // wave length
107 
108  //basic constructor
110  this->bUseDP = true;
111  this->pixel_number = ivec2();
112  this->offset = ivec2();
113  this->pixel_pitch = vec2();
114  this->shift = vec3();
115  this->k = 0;
116  this->ss = vec2();
117  this->waveNum = 0;
118  this->wave_length = 0;
119  }
120 };
121 
123 {
124  bool rotate; // rotat
125  bool merge; // merge
126  int flip; // flip
127 
128 
129  //basic constructor
131  this->rotate = false;
132  this->merge = false;
133  this->flip = 0;
134  }
135 };
136 
137 
138 
145 class OPH_DLL Openholo : public Base{
146 
147 public:
151  explicit Openholo(void);
152 
153 protected:
158  virtual ~Openholo(void) = 0;
159 
160 protected:
169  bool checkExtension(const char* fname, const char* ext);
170 
171 public:
183  virtual bool saveAsImg(const char* fname, uint8_t bitsperpixel, uchar* src, int width, int height);
184 
192  virtual uchar* loadAsImg(const char* fname);
193 
201  virtual bool saveAsOhc(const char *fname);
202 
203 
211  virtual bool loadAsOhc(const char *fname);
212 
213 
220  inline Complex<Real>** getComplexField(void) { return complex_H; }
221 
222 
229  OphConfig& getContext(void) { return context_; }
230 
236  ImageConfig& getImageConfig() { return imgCfg; }
237 
242  inline void setPixelNumber(ivec2 n) {
243  context_.pixel_number[_X] = n[_X]; context_.pixel_number[_Y] = n[_Y];
244  context_.ss = context_.pixel_number * context_.pixel_pitch;
245  }
246  inline void setPixelNumber(int width, int height) {
247  context_.pixel_number[_X] = width; context_.pixel_number[_Y] = height;
248  context_.ss = context_.pixel_number * context_.pixel_pitch;
249  }
250 
255  inline void setPixelPitch(vec2 p) {
256  context_.pixel_pitch[_X] = p[_X]; context_.pixel_pitch[_Y] = p[_Y];
257  context_.ss = context_.pixel_number * context_.pixel_pitch;
258  }
259  inline void setPixelPitch(Real pitchX, Real pitchY) {
260  context_.pixel_pitch[_X] = pitchX; context_.pixel_pitch[_Y] = pitchY;
261  context_.ss = context_.pixel_number * context_.pixel_pitch;
262  }
263 
269  inline void setWaveLength(Real w, const uint idx = 0) { context_.wave_length[idx] = w; }
270 
275  void setWaveNum(int num);
276 
281  inline void setOffset(ivec2 offset) { context_.offset[_X] = offset[_X]; context_.offset[_Y] = offset[_Y]; }
282 
292  void setImageMerge(bool merge) { imgCfg.merge = merge; }
293 
303  void setImageRotate(bool rotate) { imgCfg.rotate = rotate; }
304 
318  void setImageFlip(int flip) { imgCfg.flip = flip; }
319 
324  void setMaxThreadNum(int num);
325 
331  int getMaxThreadNum();
332 
344  bool mergeColor(int idx, int width, int height, uchar *src, uchar *dst);
345 
357  bool separateColor(int idx, int width, int height, uchar *src, uchar *dst);
358 
359 protected:
360 
369  bool loadAsImgUpSideDown(const char* fname, uchar* dst);
370 
381  bool getImgSize(int& w, int& h, int& bytesperpixel, const char* fname);
382 
392  void imgScaleBilinear(uchar* src, uchar* dst, int w, int h, int neww, int newh, int channels = 1);
393 
402  void convertToFormatGray8(uchar* src, uchar* dst, int w, int h, int bytesperpixel);
403 
411  void fft1(int n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
412 
420  void fft2(ivec2 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
428  void fft3(ivec3 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
429 
434  void fftExecute(Complex<Real>* out, bool bReverse = false);
435 
439  void fftFree(void);
440 
447  void fftInit2D(ivec2 size, int sign, unsigned int flag);
448 
458  void fft2(Complex<Real>* src, Complex<Real>* dst, int nx, int ny, int type, bool bNormalized = false);
459 
467  void fftShift(int nx, int ny, Complex<Real>* input, Complex<Real>* output);
468 
469 
470 protected:
474  virtual void ophFree(void);
475 
476 private:
480  fftw_plan plan_fwd, plan_bwd;
481  fftw_complex *fft_in, *fft_out;
482  int pnx, pny, pnz;
483  int fft_sign;
484 protected:
488 
490 protected:
496 
497 protected:
501  inline void setPixelNumberOHC(const ivec2 pixel_number)
502  { OHC_encoder->setNumOfPixel(pixel_number); }
503 
504  inline void setPixelPitchOHC(const vec2 pixel_pitch)
505  { OHC_encoder->setPixelPitch(pixel_pitch); }
506 
507  inline void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
508  { OHC_encoder->setWavelength(wavelength, wavelength_unit); }
509 
510  inline void setWaveLengthNumOHC(const uint wavelength_num)
511  { OHC_encoder->setNumOfWavlen(wavelength_num); }
512 
513  inline void setColorTypeOHC(const ColorType color_type)
514  { OHC_encoder->setColorType(color_type); }
515 
516  inline void setColorArrangeOHC(const ColorArran color_arrange)
517  { OHC_encoder->setColorArrange(color_arrange); }
518 
519  inline void setWaveLengthUnitOHC(const LenUnit length_unit)
520  { OHC_encoder->setUnitOfWavlen(length_unit); }
521 
522  inline void setFieldEncodingOHC(const FldStore field_store, const FldCodeType field_code_type)
523  { OHC_encoder->setFieldEncoding(field_store, field_code_type); }
524 
525  inline void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
526  { OHC_encoder->setPhaseEncoding(phase_code, phase_code_range); }
530  inline void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField& complex_field)
531  { OHC_encoder->addWavelengthNComplexFieldData(wavelength, complex_field); }
532 
533  inline void addWaveLengthOHC(const Real wavelength)
534  { OHC_encoder->addWavelength(wavelength); }
535 
536  inline void addComplexFieldDataOHC(const OphComplexField& complex_field)
537  { OHC_encoder->addComplexFieldData(complex_field); }
538 
539  inline void getPixelNumberOHC(ivec2& pixel_number)
540  { pixel_number = OHC_decoder->getNumOfPixel(); }
541 
542  inline void getPixelPitchOHC(vec2& pixel_pitch)
543  { pixel_pitch = OHC_decoder->getPixelPitch(); }
544 
545  inline void getWavelengthOHC(vector<Real>& wavelength)
546  { OHC_decoder->getWavelength(wavelength); }
547 
548  inline void getWaveLengthNumOHC(uint& wavelength_num)
549  { wavelength_num = OHC_decoder->getNumOfWavlen(); }
550 
551  inline void getColorTypeOHC(ColorType& color_type)
552  { color_type = OHC_decoder->getColorType(); }
553 
554  inline void getColorArrangeOHC(ColorArran& color_arrange)
555  { color_arrange = OHC_decoder->getColorArrange(); }
556 
557  inline void getWaveLengthUnitOHC(LenUnit& length_unit)
558  { length_unit = OHC_decoder->getUnitOfWavlen(); }
559 
560  inline void getComplexFieldDataOHC(Complex<Real>** cmplx, uint wavelen_idx)
561  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
562 
563  inline void getComplexFieldDataOHC(OphComplexField& cmplx, uint wavelen_idx)
564  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
565 };
566 
567 #endif // !__Openholo_h
void setWaveLength(Real w, const uint idx=0)
Function for setting the wave length.
Definition: Openholo.h:269
OphConfig & getContext(void)
Function for getting the current context.
Definition: Openholo.h:229
void setImageRotate(bool rotate)
Function for setting the image rotate(true or false)
Definition: Openholo.h:303
void setColorArrangeOHC(const ColorArran color_arrange)
Definition: Openholo.h:516
void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
Definition: Openholo.h:525
Abstract class.
Definition: Openholo.h:145
#define OPH_FORWARD
Definition: define.h:66
Real k
Definition: Openholo.h:103
w
Definition: Openholo.cpp:429
ResolutionConfig resCfg
Definition: Openholo.h:486
Definition: Base.h:63
OphConfig()
Definition: Openholo.h:109
void getPixelPitchOHC(vec2 &pixel_pitch)
Definition: Openholo.h:542
bytesperpixel
Definition: Openholo.cpp:431
vec3 shift
Definition: Openholo.h:102
void setColorArrange(const ColorArran _clrArrange)
oph::ColorArran getColorArrange()
void setPixelNumberOHC(const ivec2 pixel_number)
getter/setter for OHC file read and write
Definition: Openholo.h:501
unsigned char uchar
Definition: typedef.h:64
void getColorArrangeOHC(ColorArran &color_arrange)
Definition: Openholo.h:554
long long int double_size
Definition: Openholo.h:69
long long int size
Definition: Openholo.h:67
void setPixelPitchOHC(const vec2 pixel_pitch)
Definition: Openholo.h:504
vec2 ss
Definition: Openholo.h:104
long long int half_size
Definition: Openholo.h:68
float Real
Definition: typedef.h:55
structure for 2-dimensional integer vector and its arithmetic.
Definition: ivec.h:66
void getComplexFieldDataOHC(Complex< Real > **cmplx, uint wavelen_idx)
Definition: Openholo.h:560
void setColorTypeOHC(const ColorType color_type)
Definition: Openholo.h:513
h
Definition: Openholo.cpp:430
void getWaveLengthUnitOHC(LenUnit &length_unit)
Definition: Openholo.h:557
ivec2 offset
Definition: Openholo.h:100
void setOffset(ivec2 offset)
Function for setting the offset.
Definition: Openholo.h:281
bool rotate
Definition: Openholo.h:124
void getPixelNumberOHC(ivec2 &pixel_number)
Definition: Openholo.h:539
void setImageMerge(bool merge)
Function for setting the image merge(true or false)
Definition: Openholo.h:292
ImageConfig imgCfg
Definition: Openholo.h:487
void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
Definition: Openholo.h:507
void setPixelNumber(int width, int height)
Definition: Openholo.h:246
void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField &complex_field)
Function to add ComplexField when adding wavelength data.
Definition: Openholo.h:530
vec2 pixel_pitch
Definition: Openholo.h:101
void setWaveLengthUnitOHC(const LenUnit length_unit)
Definition: Openholo.h:519
void getWavelengthOHC(vector< Real > &wavelength)
Definition: Openholo.h:545
void setFieldEncoding(const FldStore _fldStore, const FldCodeType _fldCodeType)
void getWaveLengthNumOHC(uint &wavelength_num)
Definition: Openholo.h:548
void getComplexFieldData(OphComplexField &cmplx_field, uint wavelen_idx)
Definition: ImgCodecOhc.h:74
oph::ColorType getColorType()
#define _X
Definition: define.h:92
void addWaveLengthOHC(const Real wavelength)
Definition: Openholo.h:533
void addComplexFieldData(const OphComplexField &data)
void setNumOfWavlen(const uint n_wavlens)
void setWaveLengthNumOHC(const uint wavelength_num)
Definition: Openholo.h:510
void addComplexFieldDataOHC(const OphComplexField &complex_field)
Definition: Openholo.h:536
void setPhaseEncoding(const BPhaseCode _bPhaseCode, const double _phaseCodeMin, const double _phaseCodeMax)
void addWavelength(const Real wavlen)
structure for 2-dimensional Real type vector and its arithmetic.
Definition: vec.h:66
ImgDecoderOhc * OHC_decoder
Definition: Openholo.h:495
uint waveNum
Definition: Openholo.h:105
structure for 3-dimensional Real type vector and its arithmetic.
Definition: vec.h:466
void setPixelPitch(Real pitchX, Real pitchY)
Definition: Openholo.h:259
ResolutionConfig(const ivec2 &pixel_number)
Definition: Openholo.h:83
#define OPH_DLL
Definition: Base.h:59
ivec2 pixel_number
Definition: Openholo.h:99
oph::LenUnit getUnitOfWavlen()
void setPixelPitch(vec2 p)
Function for setting the output pixel pitch.
Definition: Openholo.h:255
void setColorType(const ColorType _clrType)
#define _Y
Definition: define.h:96
bool bUseDP
Definition: Openholo.h:98
void setPixelPitch(const double _pxPitchX, const double _pxPitchY, const LenUnit unit=LenUnit::m)
void setFieldEncodingOHC(const FldStore field_store, const FldCodeType field_code_type)
Definition: Openholo.h:522
structure for 3-dimensional integer vector and its arithmetic.
Definition: ivec.h:386
Complex< Real > ** complex_H
Definition: Openholo.h:489
void setImageFlip(int flip)
Function for setting the image flip.
Definition: Openholo.h:318
void setWavelength(const Real _wavlen, const LenUnit _unit=LenUnit::m)
void setNumOfPixel(const uint _pxNumX, const uint _pxNumY)
#define OPH_ESTIMATE
Definition: define.h:76
Complex< Real > ** getComplexField(void)
Function for getting the complex field.
Definition: Openholo.h:220
ImageConfig & getImageConfig()
Function for getting the image config.
Definition: Openholo.h:236
void setPixelNumber(ivec2 n)
Function for setting the output resolution.
Definition: Openholo.h:242
void getColorTypeOHC(ColorType &color_type)
Definition: Openholo.h:551
void setUnitOfWavlen(const LenUnit unit)
ImgEncoderOhc * OHC_encoder
OHC file format Variables for read and write.
Definition: Openholo.h:494
OphConfig context_
Definition: Openholo.h:485
Definition: Bitmap.h:49
void getComplexFieldDataOHC(OphComplexField &cmplx, uint wavelen_idx)
Definition: Openholo.h:563
Real * wave_length
Definition: Openholo.h:106
void getWavelength(std::vector< double_t > &wavlen_array)
unsigned int uint
Definition: typedef.h:62
bool merge
Definition: Openholo.h:125
void addWavelengthNComplexFieldData(const Real wavlen, const OphComplexField &data)