Openholo  v2.1
Open Source Digital Holographic Library
ophSig.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 __ophSig_h
48 #define __ophSig_h
49 
50 #include "tinyxml2.h"
51 #include "Openholo.h"
52 #include "sys.h"
53 
54 
55 
56 #ifdef SIG_EXPORT
57 #define SIG_DLL __declspec(dllexport)
58 #else
59 #define SIG_DLL __declspec(dllimport)
60 #endif
61 
62 struct SIG_DLL ophSigConfig {
63  int cols;
64  int rows;
65  Real_t width;
66  Real_t height;
67  Real_t NA;
68  Real_t z;
69  int wavelength_num;
70  Real wavelength[3];
71 };
72 
76 
93 
98 
151 
156 
256 
261 
331 
336 
415 
420 
447 
454 class SIG_DLL ophSig : public Openholo
455 {
456 protected:
457 
458  virtual ~ophSig(void) = default;
459 
460  virtual void ophFree(void);
461  bool is_CPU;
462  ophSigConfig _cfgSig;
463  OphComplexField* ComplexH;
464  fftw_plan bwd_plan, fwd_plan;
465 
466  //float _width;
467  //float _height;
468 
469  //float _NA;
470  //float _z;
471 
472  int _wavelength_num;
473 
474  /*float _angleX;
475  float _angleY;
476  float _redRate;*/
477  Real_t _radius;
478  Real_t* _foc;
479 
480 
481 
489  bool sigConvertOffaxis_CPU(Real angleX, Real angleY);
497  bool sigConvertOffaxis_GPU(Real angleX, Real angleY);
505  bool sigConvertHPO_CPU(Real depth, Real_t redRate);
513  bool sigConvertHPO_GPU(Real depth, Real_t redRate);
514 
524  bool sigConvertCAC_CPU(double red, double green, double blue);
533  bool sigConvertCAC_GPU(double red, double green, double blue);
539  double sigGetParamAT_CPU();
545  double sigGetParamAT_GPU();
555  double sigGetParamSF_CPU(float zMax, float zMin, int sampN, float th);
565  double sigGetParamSF_GPU(float zMax, float zMin, int sampN, float th);
566 
572  bool propagationHolo_CPU(float depth);
578  bool propagationHolo_GPU(float depth);
579 
580  bool Color_propagationHolo_GPU(float depth);
581 public:
585  explicit ophSig(void);
592  bool load(const char *real, const char *imag);
599  bool save(const char *real, const char *imag);
600  bool save(const char *real);
601 
607  bool loadAsOhc(const char *fname);
613  bool saveAsOhc(const char *fname);
621  template<typename T>
622  void linInterp(vector<T> &X, matrix<Complex<T>> &src, vector<T> &Xq, matrix<Complex<T>> &dst);
630  template<typename T>
631  vector<T> linspace(T first, T last, int len) {
632  vector<Real> result(len);
633  Real step = (last - first) / (len - 1);
634  for (int i = 0; i < len; i++) { result[i] = first + i * step; }
635  return result;
636  }
642  template<typename T>
643  void absMat(matrix<Complex<T>>& src, matrix<T>& dst) {
644  if (src.size != dst.size) {
645  dst.resize(src.size[_X], src.size[_Y]);
646  }
647  for (int i = 0; i < src.size[_X]; i++)
648  {
649  for (int j = 0; j < src.size[_Y]; j++)
650  {
651  dst.mat[i][j] = sqrt(src.mat[i][j]._Val[_RE] * src.mat[i][j]._Val[_RE] + src.mat[i][j]._Val[_IM] * src.mat[i][j]._Val[_IM]);
652  }
653  }
654  }
660  template<typename T>
661  void absMat(matrix<T>& src, matrix<T>& dst) {
662  if (src.size != dst.size) {
663  dst.resize(src.size[_X], src.size[_Y]);
664  }
665  for (int i = 0; i < src.size[_X]; i++)
666  {
667  for (int j = 0; j < src.size[_Y]; j++)
668  {
669  dst.mat[i][j] = abs(src.mat[i][j]);
670  }
671  }
672  }
678  template<typename T>
679  void angleMat(matrix<Complex<T>>& src, matrix<T>& dst) {
680  if (src.size != dst.size) {
681  dst.resize(src.size[_X], src.size[_Y]);
682  }
683  for (int i = 0; i < src.size[_X]; i++)
684  {
685  for (int j = 0; j < src.size[_Y]; j++)
686  {
687  angle(src(i, j), dst(i, j));
688  }
689  }
690  }
696  template<typename T>
697  void conjMat(matrix<Complex<T>>& src, matrix<Complex<T>>& dst) {
698  if (src.size != dst.size) {
699  dst.resize(src.size[_X], src.size[_Y]);
700  }
701  for (int i = 0; i < src.size[_X]; i++)
702  {
703  for (int j = 0; j < src.size[_Y]; j++)
704  {
705  dst(i, j) = src(i, j).conj();
706 
707  }
708  }
709  }
715  template<typename T>
716  void expMat(matrix<Complex<T>>& src, matrix<Complex<T>>& dst) {
717  if (src.size != dst.size) {
718  dst.resize(src.size[_X], src.size[_Y]);
719  }
720  for (int i = 0; i < src.size[_X]; i++)
721  {
722  for (int j = 0; j < src.size[_Y]; j++)
723  {
724  dst.mat[i][j]._Val[_RE] = exp(src.mat[i][j]._Val[_RE]) * cos(src.mat[i][j]._Val[_IM]);
725  dst.mat[i][j]._Val[_IM] = exp(src.mat[i][j]._Val[_RE]) * sin(src.mat[i][j]._Val[_IM]);
726  }
727  }
728  }
734  template<typename T>
735  void expMat(matrix<T>& src, matrix<T>& dst) {
736  if (src.size != dst.size) {
737  dst.resize(src.size[_X], src.size[_Y]);
738  }
739  for (int i = 0; i < src.size[_X]; i++)
740  {
741  for (int j = 0; j < src.size[_Y]; j++)
742  {
743  dst.mat[i][j] = exp(src.mat[i][j]);
744  }
745  }
746  }
752  template<typename T>
753  void meanOfMat(matrix<T>& src, T &dst) {
754  dst = 0;
755  for (int i = 0; i < src.size[_X]; i++)
756  {
757  for (int j = 0; j < src.size[_Y]; j++)
758  {
759  dst += src(i, j);
760  }
761  }
762  dst = dst / (src.size[_X] * src.size[_Y]);
763  }
769  Real maxOfMat(matrix<Real>& src) {
770  Real max = MIN_REAL;
771  for (int i = 0; i < src.size[_X]; i++)
772  {
773  for (int j = 0; j < src.size[_Y]; j++)
774  {
775  if (src(i, j) > max) max = src(i, j);
776  }
777  }
778  return max;
779  }
785  Complex<Real> maxOfMat(matrix<Complex<Real>>& src) {
786  Real max = MIN_REAL;
787  for (int i = 0; i < src.size[_X]; i++)
788  {
789  for (int j = 0; j < src.size[_Y]; j++)
790  {
791  if (src(i, j)._Val[_RE] > max) max = src(i, j)._Val[_RE];
792  //if (src(i, j)._Val[_IM] > max) max = src(i, j)._Val[_IM];
793  }
794  }
795  return max;
796  }
801  //template<typename T>
802  Real minOfMat(matrix<Real>& src) {
803  Real min = MAX_REAL;
804  for (int i = 0; i < src.size[_X]; i++)
805  {
806  for (int j = 0; j < src.size[_Y]; j++)
807  {
808  if (src(i, j) < min) min = src(i, j);
809  }
810  }
811  return min;
812  }
817  Complex<Real> minOfMat(matrix<Complex<Real>>& src) {
818  Real min = MAX_REAL;
819  for (int i = 0; i < src.size[_X]; i++)
820  {
821  for (int j = 0; j < src.size[_Y]; j++)
822  {
823  if (src(i, j)._Val[_RE] < min) min = src(i, j)._Val[_RE];
824  }
825  }
826  return min;
827  }
828 
834  void ophSig::fftShift(matrix<Complex<Real>> &src, matrix<Complex<Real>> &dst)
835  {
836  if (src.size != dst.size) {
837  dst.resize(src.size[_X], src.size[_Y]);
838  }
839  int xshift = src.size[_X] / 2;
840  int yshift = src.size[_Y] / 2;
841  for (int i = 0; i < src.size[_X]; i++)
842  {
843  int ii = (i + xshift) % src.size[_X];
844  for (int j = 0; j < src.size[_Y]; j++)
845  {
846  int jj = (j + yshift) % src.size[_Y];
847  dst.mat[ii][jj]._Val[_RE] = src.mat[i][j].real();
848  dst.mat[ii][jj]._Val[_IM] = src.mat[i][j].imag();
849  }
850  }
851  }
852 
860  template<typename T>
861  void fft1(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
869  template<typename T>
870  void fft2(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
876  bool readConfig(const char* fname);
877  void Parameter_Set(int nx, int ny, double width, double height , double NA );
878  void wavelength_Set(double wavelength);
879  void focal_length_Set(double red , double green, double blue, double rad);
880  void Data_output(uchar *data, int pos ,int bitpixel);
881  void Wavenumber_output(int &wavenumber);
882 
890  bool sigConvertOffaxis(Real angleX, Real angleY);
891  bool cvtOffaxis_CPU(Real angleX, Real angleY);
892  void cvtOffaxis_GPU(Real angleX, Real angleY);
893 
894 
902  bool sigConvertHPO(Real depth, Real_t redRate);
903 
904 
913  bool sigConvertCAC(double red, double green, double blue);
914 
920  bool propagationHolo(float depth);
927  OphComplexField propagationHolo(OphComplexField complexH, float depth);
928 
934  double sigGetParamAT();
935 
945  double sigGetParamSF(float zMax, float zMin, int sampN, float th);
946 
951  void setMode(bool is_CPU);
959  void cField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
960 
968  void ColorField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
969 
973 
979 
988  bool getComplexHFromPSDH(const char* fname0, const char* fname90, const char* fname180, const char* fname270);
989 
993 
999 
1010  bool getComplexHFrom3ArbStepPSDH(const char* f0, const char* f1, const char* f2, const char* fOI, const char* fRI, int nIter);
1011 
1012 };
1013 
1014 #endif // !__ophSig_h
1015 
Complex< Real > maxOfMat(matrix< Complex< Real >> &src)
Function for extracts maximum of matrix , where matrix is complex number.
Definition: ophSig.h:785
oph::matrix< Complex< Real > > OphComplexField
Definition: mat.h:421
void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
Definition: function.h:113
Abstract class.
Definition: Openholo.h:88
void angleMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for extracts Complex phase value.
Definition: ophSig.h:679
#define _IM
Definition: complex.h:57
#define OPH_ESTIMATE
Definition: define.h:76
void meanOfMat(matrix< T > &src, T &dst)
Function for extracts mean of matrix.
Definition: ophSig.h:753
unsigned char uchar
Definition: typedef.h:64
void conjMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for extracts Complex conjugate value.
Definition: ophSig.h:697
#define _Y
Definition: define.h:84
#define MIN_REAL
Definition: define.h:135
#define _RE
Definition: complex.h:54
double Real_t
Definition: typedef.h:56
Complex< Real > minOfMat(matrix< Complex< Real >> &src)
Function for extracts minimum of matrix , where matrix is complex number.
Definition: ophSig.h:817
#define SIG_DLL
Definition: ophSig.h:59
void expMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for extracts exponent e(x), where x is complex number.
Definition: ophSig.h:716
Definition: ophSig.h:454
void fft2(ivec2 n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 2-dimension operations inside Openholo.
#define MAX_REAL
Definition: define.h:136
void absMat(matrix< T > &src, matrix< T > &dst)
Function for extracts real absolute value.
Definition: ophSig.h:661
void fftShift(matrix< Complex< Real >> &src, matrix< Complex< Real >> &dst)
Shift zero-frequency component to center of spectrum.
Definition: ophSig.h:834
void absMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for extracts Complex absolute value.
Definition: ophSig.h:643
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Definition: Openholo.cpp:321
Real minOfMat(matrix< Real > &src)
Function for extracts minimum of matrix , where matrix is real number.
Definition: ophSig.h:802
void expMat(matrix< T > &src, matrix< T > &dst)
Function for extracts exponent e(x), where x is real number.
Definition: ophSig.h:735
void angle(const std::vector< Complex< T >> &src, std::vector< T > &dst)
Definition: function.h:159
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
Definition: Openholo.cpp:300
Real maxOfMat(matrix< Real > &src)
Function for extracts maximum of matrix , where matrix is real number.
Definition: ophSig.h:769
float Real
Definition: typedef.h:55
#define _X
Definition: define.h:80
#define OPH_FORWARD
Definition: define.h:66
void fft1(int n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 1-dimension operations inside Openholo.
Definition: Openholo.cpp:513
vector< T > linspace(T first, T last, int len)
Generate linearly spaced vector.
Definition: ophSig.h:631
unsigned int uint
Definition: typedef.h:62
virtual void ophFree(void)
Pure virtual function for override in child classes.
Definition: Openholo.cpp:845