Openholo  v2.1
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 
56 #include "ImgCodecOhc.h"
57 #include <vector>
58 
59 using namespace oph;
60 
62 {
63  bool bUseDP; // use double precision
64  ivec2 pixel_number; //< SLM_PIXEL_NUMBER_X & SLM_PIXEL_NUMBER_Y
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 
74 {
75  bool bRotation;
77  int nFlip;
78 };
79 
80 
81 
88 class OPH_DLL Openholo : public Base{
89 
90 public:
94  explicit Openholo(void);
95 
96 protected:
101  virtual ~Openholo(void) = 0;
102 
103 protected:
112  bool checkExtension(const char* fname, const char* ext);
113 
114 public:
126  virtual bool saveAsImg(const char* fname, uint8_t bitsperpixel, uchar* src, int width, int height);
127 
135  virtual uchar* loadAsImg(const char* fname);
136 
144  virtual bool saveAsOhc(const char *fname);
145 
146 
154  virtual bool loadAsOhc(const char *fname);
155 
156 
163  inline Complex<Real>** getComplexField(void) { return complex_H; }
164  inline Complex<float>** getComplexfField(void) { return complexf_H; }
165 
166 
173  OphConfig& getContext(void) { return context_; }
174 
180  ImageConfig& getImageConfig() { return imgCfg; }
181 
186  inline void setPixelNumber(ivec2 n) { context_.pixel_number[_X] = n[_X]; context_.pixel_number[_Y] = n[_Y]; }
187  inline void setPixelNumber(int width, int height) { context_.pixel_number[_X] = width; context_.pixel_number[_Y] = height; }
188 
193  inline void setPixelPitch(vec2 p) { context_.pixel_pitch[_X] = p[_X]; context_.pixel_pitch[_Y] = p[_Y]; }
194  inline void setPixelPitch(Real pitchX, Real pitchY) { context_.pixel_pitch[_X] = pitchX; context_.pixel_pitch[_Y] = pitchY; }
195 
201  inline void setWaveLength(Real w, const uint idx = 0) { context_.wave_length[idx] = w; }
202 
203  void setWaveNum(int nNum);
204 
205  void setImageMerge(bool bMerge) { imgCfg.bMergeImage = bMerge; }
206  void setImageRotate(bool rotate) { imgCfg.bRotation = rotate; }
207  void setImageFlip(int flip) { imgCfg.nFlip = flip; }
208  bool getImageRotate() { return imgCfg.bRotation; }
209 
210  void SetMaxThreadNum(int num);
211  int GetMaxThreadNum();
212 
213  bool mergeColor(int idx, int width, int height, uchar *src, uchar *dst);
214  bool separateColor(int idx, int width, int height, uchar *src, uchar *dst);
215 
216 protected:
225  bool loadAsImgUpSideDown(const char* fname, uchar* dst);
226 
237  bool getImgSize(int& w, int& h, int& bytesperpixel, const char* fname);
238 
248  void imgScaleBilinear(uchar* src, uchar* dst, int w, int h, int neww, int newh, int channels = 1);
249 
258  void convertToFormatGray8(uchar* src, uchar* dst, int w, int h, int bytesperpixel);
259 
267  void fft1(int n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
268 
276  void fft2(ivec2 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
284  void fft3(ivec3 n, Complex<Real>* in, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
285 
290  void fftExecute(Complex<Real>* out, bool bReverse = false);
291  void fftFree(void);
292  void fftInit2D(ivec2 size, int sign, unsigned int flag);
302  void fft2(Complex<Real>* src, Complex<Real>* dst, int nx, int ny, int type, bool bNormalized = false);
303 
311  void fftShift(int nx, int ny, Complex<Real>* input, Complex<Real>* output);
312  void fftShift(int nx, int ny, Complex<Real>* input, fftw_complex* output);
313  void fftShift(int nx, int ny, fftw_complex* input, Complex<Real>* output);
314 
315 protected:
319  virtual void ophFree(void);
320 
321 private:
325  fftw_plan plan_fwd, plan_bwd;
326  fftw_complex *fft_in, *fft_out;
327  int pnx, pny, pnz;
328  int fft_sign;
329 
333  int m_precision;
334 
335 protected:
338 
339  Complex<Real>** complex_H;
340  Complex<float>** complexf_H;
341 protected:
347 
348 protected:
352  inline void setPixelNumberOHC(const ivec2 pixel_number)
353  { OHC_encoder->setNumOfPixel(pixel_number); }
354 
355  inline void setPixelPitchOHC(const vec2 pixel_pitch)
356  { OHC_encoder->setPixelPitch(pixel_pitch); }
357 
358  inline void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
359  { OHC_encoder->setWavelength(wavelength, wavelength_unit); }
360 
361  inline void setWaveLengthNumOHC(const uint wavelength_num)
362  { OHC_encoder->setNumOfWavlen(wavelength_num); }
363 
364  inline void setColorTypeOHC(const ColorType color_type)
365  { OHC_encoder->setColorType(color_type); }
366 
367  inline void setColorArrangeOHC(const ColorArran color_arrange)
368  { OHC_encoder->setColorArrange(color_arrange); }
369 
370  inline void setWaveLengthUnitOHC(const LenUnit length_unit)
371  { OHC_encoder->setUnitOfWavlen(length_unit); }
372 
373  inline void setFieldEncodingOHC(const FldStore field_store, const FldCodeType field_code_type)
374  { OHC_encoder->setFieldEncoding(field_store, field_code_type); }
375 
376  inline void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
377  { OHC_encoder->setPhaseEncoding(phase_code, phase_code_range); }
381  inline void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField& complex_field)
382  { OHC_encoder->addWavelengthNComplexFieldData(wavelength, complex_field); }
383 
384  inline void addWaveLengthOHC(const Real wavelength)
385  { OHC_encoder->addWavelength(wavelength); }
386 
387  inline void addComplexFieldDataOHC(const OphComplexField& complex_field)
388  { OHC_encoder->addComplexFieldData(complex_field); }
389 
390  inline void getPixelNumberOHC(ivec2& pixel_number)
391  { pixel_number = OHC_decoder->getNumOfPixel(); }
392 
393  inline void getPixelPitchOHC(vec2& pixel_pitch)
394  { pixel_pitch = OHC_decoder->getPixelPitch(); }
395 
396  inline void getWavelengthOHC(vector<Real>& wavelength)
397  { OHC_decoder->getWavelength(wavelength); }
398 
399  inline void getWaveLengthNumOHC(uint& wavelength_num)
400  { wavelength_num = OHC_decoder->getNumOfWavlen(); }
401 
402  inline void getColorTypeOHC(ColorType& color_type)
403  { color_type = OHC_decoder->getColorType(); }
404 
405  inline void getColorArrangeOHC(ColorArran& color_arrange)
406  { color_arrange = OHC_decoder->getColorArrange(); }
407 
408  inline void getWaveLengthUnitOHC(LenUnit& length_unit)
409  { length_unit = OHC_decoder->getUnitOfWavlen(); }
410 
411  inline void getComplexFieldDataOHC(Complex<Real>** cmplx, uint wavelen_idx)
412  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
413 
414  inline void getComplexFieldDataOHC(OphComplexField& cmplx, uint wavelen_idx)
415  { OHC_decoder->getComplexFieldData(cmplx, wavelen_idx); }
416 };
417 
418 #endif // !__Openholo_h
void setWaveLength(Real w, const uint idx=0)
Function for setting the wave length.
Definition: Openholo.h:201
oph::matrix< Complex< Real > > OphComplexField
Definition: mat.h:421
OphConfig & getContext(void)
Function for getting the current context.
Definition: Openholo.h:173
void setImageRotate(bool rotate)
Definition: Openholo.h:206
void setColorArrangeOHC(const ColorArran color_arrange)
Definition: Openholo.h:367
int nFlip
Definition: Openholo.h:77
void setPhaseEncodingOHC(const BPhaseCode phase_code, const vec2 phase_code_range)
Definition: Openholo.h:376
Abstract class.
Definition: Openholo.h:88
Real k
Definition: Openholo.h:67
Definition: Base.h:55
void getPixelPitchOHC(vec2 &pixel_pitch)
Definition: Openholo.h:393
vec3 shift
Definition: Openholo.h:66
#define OPH_ESTIMATE
Definition: define.h:76
void setColorArrange(const ColorArran _clrArrange)
ColorArran getColorArrange()
void setPixelNumberOHC(const ivec2 pixel_number)
getter/setter for OHC file read and write
Definition: Openholo.h:352
unsigned char uchar
Definition: typedef.h:64
void getColorArrangeOHC(ColorArran &color_arrange)
Definition: Openholo.h:405
void setPixelPitchOHC(const vec2 pixel_pitch)
Definition: Openholo.h:355
vec2 ss
Definition: Openholo.h:68
#define _Y
Definition: define.h:84
bool bRotation
Definition: Openholo.h:75
void getComplexFieldDataOHC(Complex< Real > **cmplx, uint wavelen_idx)
Definition: Openholo.h:411
void setColorTypeOHC(const ColorType color_type)
Definition: Openholo.h:364
void getWaveLengthUnitOHC(LenUnit &length_unit)
Definition: Openholo.h:408
void getPixelNumberOHC(ivec2 &pixel_number)
Definition: Openholo.h:390
ImageConfig imgCfg
Definition: Openholo.h:337
void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
Definition: Openholo.h:358
void setPixelNumber(int width, int height)
Definition: Openholo.h:187
void addWaveLengthNComplexFieldDataOHC(const Real wavelength, const OphComplexField &complex_field)
Function to add ComplexField when adding wavelength data.
Definition: Openholo.h:381
vec2 pixel_pitch
Definition: Openholo.h:65
void setWaveLengthUnitOHC(const LenUnit length_unit)
Definition: Openholo.h:370
void getWavelengthOHC(vector< Real > &wavelength)
Definition: Openholo.h:396
void setFieldEncoding(const FldStore _fldStore, const FldCodeType _fldCodeType)
void getWaveLengthNumOHC(uint &wavelength_num)
Definition: Openholo.h:399
void getComplexFieldData(OphComplexField &cmplx_field, uint wavelen_idx)
Definition: ImgCodecOhc.h:81
ColorType getColorType()
void setImageMerge(bool bMerge)
Definition: Openholo.h:205
bool getImageRotate()
Definition: Openholo.h:208
void addWaveLengthOHC(const Real wavelength)
Definition: Openholo.h:384
void addComplexFieldData(const OphComplexField &data)
void setNumOfWavlen(const uint n_wavlens)
void setWaveLengthNumOHC(const uint wavelength_num)
Definition: Openholo.h:361
void addComplexFieldDataOHC(const OphComplexField &complex_field)
Definition: Openholo.h:387
void setPhaseEncoding(const BPhaseCode _bPhaseCode, const double _phaseCodeMin, const double _phaseCodeMax)
void addWavelength(const Real wavlen)
ImgDecoderOhc * OHC_decoder
Definition: Openholo.h:346
Complex< float > ** getComplexfField(void)
Definition: Openholo.h:164
uint waveNum
Definition: Openholo.h:69
void setPixelPitch(Real pitchX, Real pitchY)
Definition: Openholo.h:194
ivec2 pixel_number
Definition: Openholo.h:64
#define OPH_DLL
Definition: Base.h:52
LenUnit getUnitOfWavlen()
bool bMergeImage
Definition: Openholo.h:76
void setPixelPitch(vec2 p)
Function for setting the output pixel pitch.
Definition: Openholo.h:193
void setColorType(const ColorType _clrType)
Complex< float > ** complexf_H
Definition: Openholo.h:340
bool bUseDP
Definition: Openholo.h:63
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:373
Complex< Real > ** complex_H
Definition: Openholo.h:339
void setImageFlip(int flip)
Definition: Openholo.h:207
void setWavelength(const Real _wavlen, const LenUnit _unit=LenUnit::m)
void setNumOfPixel(const uint _pxNumX, const uint _pxNumY)
float Real
Definition: typedef.h:55
Complex< Real > ** getComplexField(void)
Function for getting the complex field.
Definition: Openholo.h:163
ImageConfig & getImageConfig()
Function for getting the image config.
Definition: Openholo.h:180
void setPixelNumber(ivec2 n)
Function for setting the output resolution.
Definition: Openholo.h:186
void getColorTypeOHC(ColorType &color_type)
Definition: Openholo.h:402
void setUnitOfWavlen(const LenUnit unit)
#define _X
Definition: define.h:80
ImgEncoderOhc * OHC_encoder
OHC file format Variables for read and write.
Definition: Openholo.h:345
OphConfig context_
Definition: Openholo.h:336
Definition: Bitmap.h:49
void getComplexFieldDataOHC(OphComplexField &cmplx, uint wavelen_idx)
Definition: Openholo.h:414
#define OPH_FORWARD
Definition: define.h:66
Real * wave_length
Definition: Openholo.h:70
void getWavelength(std::vector< double_t > &wavlen_array)
unsigned int uint
Definition: typedef.h:62
void addWavelengthNComplexFieldData(const Real wavlen, const OphComplexField &data)