Openholo  v4.0
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 
59 
61 {
62  bool bUseDP; // use double precision
63  ivec2 pixel_number; // SLM_PIXEL_NUMBER_X & SLM_PIXEL_NUMBER_Y
64  ivec2 offset; // start offset
65  vec2 pixel_pitch; // SLM_PIXEL_PITCH_X & SLM_PIXEL_PITCH_Y
66  vec3 shift; // shift
67  Real k; // 2 * PI / lambda(wavelength)
68  vec2 ss; // pn * pp
69  uint waveNum; // wave num
70  Real* wave_length; // wave length
71 
72  //basic constructor
74  this->bUseDP = true;
75  this->pixel_number = ivec2();
76  this->offset = ivec2();
77  this->pixel_pitch = vec2();
78  this->shift = vec3();
79  this->k = 0;
80  this->ss = vec2();
81  this->waveNum = 0;
82  this->wave_length = 0;
83  }
84 };
85 
87 {
88  bool rotate; // rotat
89  bool merge; // merge
90  int flip; // flip
91 
92 
93  //basic constructor
95  this->rotate = false;
96  this->merge = false;
97  this->flip = 0;
98  }
99 };
100 
101 
102 
109 class OPH_DLL Openholo : public Base{
110 
111 public:
115  explicit Openholo(void);
116 
117 protected:
122  virtual ~Openholo(void) = 0;
123 
124 protected:
133  bool checkExtension(const char* fname, const char* ext);
134 
135 public:
147  virtual bool saveAsImg(const char* fname, uint8_t bitsperpixel, uchar* src, int width, int height);
148 
156  virtual uchar* loadAsImg(const char* fname);
157 
165  virtual bool saveAsOhc(const char *fname);
166 
167 
175  virtual bool loadAsOhc(const char *fname);
176 
177 
184  inline Complex<Real>** getComplexField(void) { return complex_H; }
185 
186 
193  OphConfig& getContext(void) { return context_; }
194 
200  ImageConfig& getImageConfig() { return imgCfg; }
201 
206  inline void setPixelNumber(ivec2 n) {
207  context_.pixel_number[_X] = n[_X]; context_.pixel_number[_Y] = n[_Y];
208  context_.ss = context_.pixel_number * context_.pixel_pitch;
209  }
210  inline void setPixelNumber(int width, int height) {
211  context_.pixel_number[_X] = width; context_.pixel_number[_Y] = height;
212  context_.ss = context_.pixel_number * context_.pixel_pitch;
213  }
214 
219  inline void setPixelPitch(vec2 p) {
220  context_.pixel_pitch[_X] = p[_X]; context_.pixel_pitch[_Y] = p[_Y];
221  context_.ss = context_.pixel_number * context_.pixel_pitch;
222  }
223  inline void setPixelPitch(Real pitchX, Real pitchY) {
224  context_.pixel_pitch[_X] = pitchX; context_.pixel_pitch[_Y] = pitchY;
225  context_.ss = context_.pixel_number * context_.pixel_pitch;
226  }
227 
233  inline void setWaveLength(Real w, const uint idx = 0) { context_.wave_length[idx] = w; }
234 
239  void setWaveNum(int num);
240 
245  inline void setOffset(ivec2 offset) { context_.offset[_X] = offset[_X]; context_.offset[_Y] = offset[_Y]; }
246 
256  void setImageMerge(bool merge) { imgCfg.merge = merge; }
257 
267  void setImageRotate(bool rotate) { imgCfg.rotate = rotate; }
268 
282  void setImageFlip(int flip) { imgCfg.flip = flip; }
283 
288  void setMaxThreadNum(int num);
289 
295  int getMaxThreadNum();
296 
308  bool mergeColor(int idx, int width, int height, uchar *src, uchar *dst);
309 
321  bool separateColor(int idx, int width, int height, uchar *src, uchar *dst);
322 
323 protected:
324 
333  bool loadAsImgUpSideDown(const char* fname, uchar* dst);
334 
345  bool getImgSize(int& w, int& h, int& bytesperpixel, const char* fname);
346 
356  void imgScaleBilinear(uchar* src, uchar* dst, int w, int h, int neww, int newh, int channels = 1);
357 
366  void convertToFormatGray8(uchar* src, uchar* dst, int w, int h, int bytesperpixel);
367 
375  void fft1(int n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
376 
384  void fft2(ivec2 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
392  void fft3(ivec3 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
393 
398  void fftExecute(Complex<Real>* out, bool bReverse = false);
399 
403  void fftFree(void);
404 
411  void fftInit2D(ivec2 size, int sign, unsigned int flag);
412 
422  void fft2(Complex<Real>* src, Complex<Real>* dst, int nx, int ny, int type, bool bNormalized = false);
423 
431  void fftShift(int nx, int ny, Complex<Real>* input, Complex<Real>* output);
432 
433 
434 protected:
438  virtual void ophFree(void);
439 
440 private:
444  fftw_plan plan_fwd, plan_bwd;
445  fftw_complex *fft_in, *fft_out;
446  int pnx, pny, pnz;
447  int fft_sign;
448 protected:
451 
453 protected:
459 
460 protected:
464  inline void setPixelNumberOHC(const ivec2 pixel_number)
465  { OHC_encoder->setNumOfPixel(pixel_number); }
466 
467  inline void setPixelPitchOHC(const vec2 pixel_pitch)
468  { OHC_encoder->setPixelPitch(pixel_pitch); }
469 
470  inline void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
471  { OHC_encoder->setWavelength(wavelength, wavelength_unit); }
472 
473  inline void setWaveLengthNumOHC(const uint wavelength_num)
474  { OHC_encoder->setNumOfWavlen(wavelength_num); }
475 
476  inline void setColorTypeOHC(const ColorType color_type)
477  { OHC_encoder->setColorType(color_type); }
478 
479  inline void setColorArrangeOHC(const ColorArran color_arrange)
480  { OHC_encoder->setColorArrange(color_arrange); }
481 
482  inline void setWaveLengthUnitOHC(const LenUnit length_unit)
483  { OHC_encoder->setUnitOfWavlen(length_unit); }
484 
485  inline void setFieldEncodingOHC(const FldStore field_store, const FldCodeType field_code_type)
486  { OHC_encoder->setFieldEncoding(field_store, field_code_type); }
487 
488  inline void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
489  { OHC_encoder->setPhaseEncoding(phase_code, phase_code_range); }
493  inline void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField& complex_field)
494  { OHC_encoder->addWavelengthNComplexFieldData(wavelength, complex_field); }
495 
496  inline void addWaveLengthOHC(const Real wavelength)
497  { OHC_encoder->addWavelength(wavelength); }
498 
499  inline void addComplexFieldDataOHC(const OphComplexField& complex_field)
500  { OHC_encoder->addComplexFieldData(complex_field); }
501 
502  inline void getPixelNumberOHC(ivec2& pixel_number)
503  { pixel_number = OHC_decoder->getNumOfPixel(); }
504 
505  inline void getPixelPitchOHC(vec2& pixel_pitch)
506  { pixel_pitch = OHC_decoder->getPixelPitch(); }
507 
508  inline void getWavelengthOHC(vector<Real>& wavelength)
509  { OHC_decoder->getWavelength(wavelength); }
510 
511  inline void getWaveLengthNumOHC(uint& wavelength_num)
512  { wavelength_num = OHC_decoder->getNumOfWavlen(); }
513 
514  inline void getColorTypeOHC(ColorType& color_type)
515  { color_type = OHC_decoder->getColorType(); }
516 
517  inline void getColorArrangeOHC(ColorArran& color_arrange)
518  { color_arrange = OHC_decoder->getColorArrange(); }
519 
520  inline void getWaveLengthUnitOHC(LenUnit& length_unit)
521  { length_unit = OHC_decoder->getUnitOfWavlen(); }
522 
523  inline void getComplexFieldDataOHC(Complex<Real>** cmplx, uint wavelen_idx)
524  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
525 
526  inline void getComplexFieldDataOHC(OphComplexField& cmplx, uint wavelen_idx)
527  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
528 };
529 
530 #endif // !__Openholo_h
bytesperpixel
Definition: Openholo.cpp:431
void setWaveLength(Real w, const uint idx=0)
Function for setting the wave length.
Definition: Openholo.h:233
OphConfig & getContext(void)
Function for getting the current context.
Definition: Openholo.h:193
void setImageRotate(bool rotate)
Function for setting the image rotate(true or false)
Definition: Openholo.h:267
void setColorArrangeOHC(const ColorArran color_arrange)
Definition: Openholo.h:479
void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
Definition: Openholo.h:488
Abstract class.
Definition: Openholo.h:109
Real k
Definition: Openholo.h:67
Definition: Base.h:63
OphConfig()
Definition: Openholo.h:73
void getPixelPitchOHC(vec2 &pixel_pitch)
Definition: Openholo.h:505
vec3 shift
Definition: Openholo.h:66
Real * wave_length
Definition: Openholo.h:70
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:464
unsigned char uchar
Definition: typedef.h:64
void getColorArrangeOHC(ColorArran &color_arrange)
Definition: Openholo.h:517
float Real
Definition: typedef.h:55
void setPixelPitchOHC(const vec2 pixel_pitch)
Definition: Openholo.h:467
vec2 ss
Definition: Openholo.h:68
#define OPH_ESTIMATE
Definition: define.h:76
structure for 2-dimensional integer vector and its arithmetic.
Definition: ivec.h:66
void getComplexFieldDataOHC(Complex< Real > **cmplx, uint wavelen_idx)
Definition: Openholo.h:523
void setColorTypeOHC(const ColorType color_type)
Definition: Openholo.h:476
void getWaveLengthUnitOHC(LenUnit &length_unit)
Definition: Openholo.h:520
ivec2 offset
Definition: Openholo.h:64
void setOffset(ivec2 offset)
Function for setting the offset.
Definition: Openholo.h:245
bool rotate
Definition: Openholo.h:88
void getPixelNumberOHC(ivec2 &pixel_number)
Definition: Openholo.h:502
void setImageMerge(bool merge)
Function for setting the image merge(true or false)
Definition: Openholo.h:256
#define _Y
Definition: define.h:96
ImageConfig imgCfg
Definition: Openholo.h:450
void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
Definition: Openholo.h:470
void setPixelNumber(int width, int height)
Definition: Openholo.h:210
void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField &complex_field)
Function to add ComplexField when adding wavelength data.
Definition: Openholo.h:493
ImgEncoderOhc * OHC_encoder
OHC file format Variables for read and write.
Definition: Openholo.h:457
vec2 pixel_pitch
Definition: Openholo.h:65
void setWaveLengthUnitOHC(const LenUnit length_unit)
Definition: Openholo.h:482
void getWavelengthOHC(vector< Real > &wavelength)
Definition: Openholo.h:508
void setFieldEncoding(const FldStore _fldStore, const FldCodeType _fldCodeType)
void getWaveLengthNumOHC(uint &wavelength_num)
Definition: Openholo.h:511
void getComplexFieldData(OphComplexField &cmplx_field, uint wavelen_idx)
Definition: ImgCodecOhc.h:74
ImgDecoderOhc * OHC_decoder
Definition: Openholo.h:458
oph::ColorType getColorType()
#define _X
Definition: define.h:92
#define OPH_DLL
Definition: Base.h:59
void addWaveLengthOHC(const Real wavelength)
Definition: Openholo.h:496
void addComplexFieldData(const OphComplexField &data)
void setNumOfWavlen(const uint n_wavlens)
void setWaveLengthNumOHC(const uint wavelength_num)
Definition: Openholo.h:473
void addComplexFieldDataOHC(const OphComplexField &complex_field)
Definition: Openholo.h:499
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
ImageConfig()
Definition: Openholo.h:94
int flip
Definition: Openholo.h:90
uint waveNum
Definition: Openholo.h:69
structure for 3-dimensional Real type vector and its arithmetic.
Definition: vec.h:466
void setPixelPitch(Real pitchX, Real pitchY)
Definition: Openholo.h:223
ivec2 pixel_number
Definition: Openholo.h:63
oph::LenUnit getUnitOfWavlen()
void setPixelPitch(vec2 p)
Function for setting the output pixel pitch.
Definition: Openholo.h:219
void setColorType(const ColorType _clrType)
bool bUseDP
Definition: Openholo.h:62
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:485
structure for 3-dimensional integer vector and its arithmetic.
Definition: ivec.h:386
void setImageFlip(int flip)
Function for setting the image flip.
Definition: Openholo.h:282
void setWavelength(const Real _wavlen, const LenUnit _unit=LenUnit::m)
void setNumOfPixel(const uint _pxNumX, const uint _pxNumY)
Complex< Real > ** getComplexField(void)
Function for getting the complex field.
Definition: Openholo.h:184
ImageConfig & getImageConfig()
Function for getting the image config.
Definition: Openholo.h:200
void setPixelNumber(ivec2 n)
Function for setting the output resolution.
Definition: Openholo.h:206
void getColorTypeOHC(ColorType &color_type)
Definition: Openholo.h:514
void setUnitOfWavlen(const LenUnit unit)
w
Definition: Openholo.cpp:429
OphConfig context_
Definition: Openholo.h:449
#define OPH_FORWARD
Definition: define.h:66
Complex< Real > ** complex_H
Definition: Openholo.h:452
Definition: Bitmap.h:49
void getComplexFieldDataOHC(OphComplexField &cmplx, uint wavelen_idx)
Definition: Openholo.h:526
void getWavelength(std::vector< double_t > &wavlen_array)
unsigned int uint
Definition: typedef.h:62
bool merge
Definition: Openholo.h:89
void addWavelengthNComplexFieldData(const Real wavlen, const OphComplexField &data)
h
Definition: Openholo.cpp:430