48 #define NOMINMAX // using std::numeric_limits<DataType>::max(), min() of <limits> instead of <minwindef.h> 55 #define FHeader this->Header->fileHeader 56 #define FldInfo this->Header->fieldInfo 57 #define WavLeng this->Header->wavlenTable 67 this->releaseOHCheader();
68 this->releaseFldData();
69 this->releaseCodeBuffer();
73 this->initOHCheader();
74 this->setFileName(_fname);
78 this->initOHCheader();
79 this->setFileName(_fname);
80 this->setOHCheader(_Header);
84 if (this->Header !=
nullptr) {
86 this->Header =
nullptr;
99 if (this->Header !=
nullptr) {
101 this->Header =
nullptr;
110 if (this->Header ==
nullptr)
111 LOG(
"OHC CODEC : No Header Data.");
113 _Header = *(this->Header);
117 if (this->Header ==
nullptr)
118 LOG(
"OHC CODEC : No Header Data.");
120 _FieldInfo = this->Header->fieldInfo;
121 _wavlenTable = this->Header->wavlenTable;
134 for (
uint i = 0; i < field_cmplx.size(); i++)
136 (*cmplx_field)[i].
resize(Header->fieldInfo.pxNumX, Header->fieldInfo.pxNumY);
138 (*cmplx_field)[i] = field_cmplx[i];
144 if (*cmplx_field ==
nullptr)
147 for (
uint i = 0; i < field_cmplx.size(); i++)
152 if (this->Header !=
nullptr) {
154 this->Header =
nullptr;
161 delete[] this->buf_f32;
162 this->buf_f32 =
nullptr;
165 delete[] this->buf_f64;
166 this->buf_f64 =
nullptr;
171 for (
size_t i = 0; i < field_cmplx.size(); ++i) {
172 this->field_cmplx[i].release();
174 this->field_cmplx.clear();
196 this->releaseOHCheader();
197 this->releaseFldData();
198 this->releaseCodeBuffer();
204 for (
size_t i = 0; i < field_ampli.size(); ++i) {
205 this->field_ampli[i].release();
207 this->field_ampli.clear();
209 for (
size_t i = 0; i < field_phase.size(); ++i) {
210 this->field_phase[i].release();
212 this->field_phase.clear();
214 this->bLoadFile =
false;
218 if ((this->Header ==
nullptr) || !this->bLoadFile) {
219 LOG(
"OHC CODEC Error : No loaded data.");
220 return ivec2(-1, -1);
227 if ((this->Header ==
nullptr) || !this->bLoadFile) {
228 LOG(
"OHC CODEC Error : No loaded data.");
229 return vec2(-1., -1.);
236 if ((this->Header ==
nullptr) || !this->bLoadFile) {
237 LOG(
"OHC CODEC Error : No loaded data.");
245 if ((this->Header ==
nullptr) || !this->bLoadFile) {
246 LOG(
"OHC CODEC Error : No loaded data.");
254 if ((this->Header ==
nullptr) || !this->bLoadFile) {
255 LOG(
"OHC CODEC Error : No loaded data.");
263 if ((this->Header ==
nullptr) || !this->bLoadFile) {
264 LOG(
"OHC CODEC Error : No loaded data.");
272 if ((this->Header ==
nullptr) || !this->bLoadFile) {
273 LOG(
"OHC CODEC Error : No loaded data.");
281 if ((this->Header ==
nullptr) || !this->bLoadFile) {
282 LOG(
"OHC CODEC Error : No loaded data.");
290 if ((this->Header ==
nullptr) || !this->bLoadFile) {
291 LOG(
"OHC CODEC Error : No loaded data.");
299 if ((this->Header ==
nullptr) || !this->bLoadFile) {
300 LOG(
"OHC CODEC Error : No loaded data.");
304 linkFilePath_array = this->linkFilePath;
308 this->File.open(this->fname, std::ios::in | std::ios::binary);
310 bool isOpen = File.is_open();
311 if (this->File.is_open()) {
312 if (this->Header ==
nullptr)
317 File.read((
char *)&
FHeader.fileSignature,
sizeof(
FHeader.fileSignature));
323 File.seekg(ios::beg);
325 printf(
"Reading Openholo Complex Field File...\n%s\n", fname.c_str());
326 printf(
"OHC File was made on OpenHolo version v%x.%x...\n",
FHeader.fileVersionMajor,
FHeader.fileVersionMinor);
332 LOG(
"Error : No Field Data");
339 double_t waveLength = 0.0;
340 File.read((
char *)&waveLength,
sizeof(waveLength));
346 switch (
FldInfo.cmplxFldType) {
349 ok = decodeFieldData();
352 LOG(
"Error : Compressed Image Format Decoding is Not Yet supported...");
357 LOG(
"Error : Invalid Decoding Complex Field Data Type...");
405 this->bLoadFile =
true;
410 LOG(
"Error : Failed loading OHC file...");
417 if (field_cmplx.empty() !=
true)
return;
419 uint x = Header->fieldInfo.pxNumX;
420 uint y = Header->fieldInfo.pxNumY;
421 uint n_wav = Header->fieldInfo.wavlenNum;
423 for (
uint l = 0; l < n_wav; l++)
425 for (
uint i = 0; i < x; i++)
427 for (
uint j = 0; j < y; j++)
429 if (field_ampli.empty() !=
true) field_cmplx[l][i][j][
_RE] = field_ampli[l][i][j];
430 if (field_phase.empty() !=
true) field_cmplx[l][i][j][
_IM] = field_phase[l][i][j];
438 int n_wavlens =
FldInfo.wavlenNum;
441 int n_pixels = cols * rows;
442 ulonglong n_fields = n_pixels * n_wavlens;
451 for (
int w = 0;
w < n_wavlens; ++
w) {
454 this->field_cmplx.push_back(data_field);
460 for (
int w = 0;
w < n_wavlens; ++
w) {
463 this->field_ampli.push_back(data_field);
464 this->field_phase.push_back(data_field);
470 for (
int w = 0;
w < n_wavlens; ++
w) {
473 this->field_ampli.push_back(data_field);
479 for (
int w = 0;
w < n_wavlens; ++
w) {
482 this->field_phase.push_back(data_field);
487 LOG(
"Error : Invalid Complex Field Encoding Type...\n");
494 this->buf_f32 =
new float[n_fields * n_cmplxChnl];
495 for (
ulonglong i = 0; i < n_fields * n_cmplxChnl; i++)
496 this->File.read((
char*)&this->buf_f32[i],
sizeof(
float));
499 this->buf_f64 =
new double[n_fields * n_cmplxChnl];
500 for (
ulonglong i = 0; i < n_fields * n_cmplxChnl; i++)
501 this->File.read((
char*)&this->buf_f64[i],
sizeof(
double));
504 for (
int x = 0; x < cols; ++x) {
505 for (
int y = 0; y < rows; ++y) {
506 int idx = x * rows + y;
508 for (
int clrChnl = 0; clrChnl < n_wavlens; ++clrChnl) {
509 ulonglong idx_sqtlChnl = n_wavlens * idx + clrChnl;
515 this->field_cmplx[clrChnl][x][y][
_RE] = *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields);
516 this->field_cmplx[clrChnl][x][y][
_IM] = *(this->buf_f32 + idx_sqtlChnl + 1 * n_fields);
519 this->field_cmplx[clrChnl][x][y][
_RE] = *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields);
520 this->field_cmplx[clrChnl][x][y][
_IM] = *(this->buf_f64 + idx_sqtlChnl + 1 * n_fields);
526 this->field_ampli[clrChnl][x][y] = *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields);
527 this->field_phase[clrChnl][x][y] = *(this->buf_f32 + idx_sqtlChnl + 1 * n_fields);
530 this->field_ampli[clrChnl][x][y] = *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields);
531 this->field_phase[clrChnl][x][y] = *(this->buf_f64 + idx_sqtlChnl + 1 * n_fields);
537 this->field_ampli[clrChnl][x][y] = *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields);
539 this->field_ampli[clrChnl][x][y] = *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields);
544 this->field_phase[clrChnl][x][y] = *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields);
546 this->field_phase[clrChnl][x][y] = *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields);
552 ulonglong idx_eachChnl = idx + clrChnl * n_pixels;
557 this->field_cmplx[clrChnl][x][y][
_RE] = *(this->buf_f32 + idx_eachChnl + 0 * n_fields);
558 this->field_cmplx[clrChnl][x][y][
_IM] = *(this->buf_f32 + idx_eachChnl + 1 * n_fields);
561 this->field_cmplx[clrChnl][x][y][
_RE] = *(this->buf_f64 + idx_eachChnl + 0 * n_fields);
562 this->field_cmplx[clrChnl][x][y][
_IM] = *(this->buf_f64 + idx_eachChnl + 1 * n_fields);
568 this->field_ampli[clrChnl][x][y] = *(this->buf_f32 + idx_eachChnl + 0 * n_fields);
569 this->field_phase[clrChnl][x][y] = *(this->buf_f32 + idx_eachChnl + 1 * n_fields);
572 this->field_ampli[clrChnl][x][y] = *(this->buf_f64 + idx_eachChnl + 0 * n_fields);
573 this->field_phase[clrChnl][x][y] = *(this->buf_f64 + idx_eachChnl + 1 * n_fields);
579 this->field_ampli[clrChnl][x][y] = *(this->buf_f32 + idx_eachChnl + 0 * n_fields);
581 this->field_ampli[clrChnl][x][y] = *(this->buf_f64 + idx_eachChnl + 0 * n_fields);
586 this->field_phase[clrChnl][x][y] = *(this->buf_f32 + idx_eachChnl + 0 * n_fields);
588 this->field_phase[clrChnl][x][y] = *(this->buf_f64 + idx_eachChnl + 0 * n_fields);
600 LOG(
"Error : Link Image File Decoding is Not Yet supported...\n");
604 LOG(
"Error : Invalid Field Data Store Type...\n");
837 this->releaseOHCheader();
838 this->releaseFldData();
839 this->releaseCodeBuffer();
843 if (this->Header !=
nullptr) {
845 this->Header =
nullptr;
872 if (std::is_same<double, Real>::value)
874 else if (std::is_same<float, Real>::value)
879 if (this->Header ==
nullptr) {
880 LOG(
"OHC CODEC Error : No header data.");
890 if (this->Header ==
nullptr) {
891 LOG(
"OHC CODEC Error : No header data.");
901 if (this->Header ==
nullptr) {
902 LOG(
"OHC CODEC Error : No header data.");
913 if (this->Header ==
nullptr) {
914 LOG(
"OHC CODEC Error : No header data.");
925 if (this->Header ==
nullptr) {
926 LOG(
"OHC CODEC Error : No header data.");
935 if (this->Header ==
nullptr) {
936 LOG(
"OHC CODEC Error : No header data.");
945 if (this->Header ==
nullptr) {
946 LOG(
"OHC CODEC Error : No header data.");
950 FldInfo.clrArrange = _clrArrange;
955 if (this->Header ==
nullptr) {
956 LOG(
"OHC CODEC Error : No header data.");
966 if (this->Header ==
nullptr) {
967 LOG(
"OHC CODEC Error : No header data.");
972 FldInfo.fldCodeType = _fldCodeType;
978 if (this->Header ==
nullptr) {
979 LOG(
"OHC CODEC Error : No header data.");
983 FldInfo.bPhaseCode = _bPhaseCode;
984 FldInfo.phaseCodeMin = _phaseCodeMin;
985 FldInfo.phaseCodeMax = _phaseCodeMax;
991 if (this->Header ==
nullptr) {
992 LOG(
"OHC CODEC Error : No header data.");
996 FldInfo.bPhaseCode = _bPhaseCode;
997 FldInfo.phaseCodeMin = _phaseCodeRange[0];
998 FldInfo.phaseCodeMax = _phaseCodeRange[1];
1013 this->addWavelength(_wavlen);
1014 this->setUnitOfWavlen(_unit);
1018 this->addWavelength(wavlen);
1019 this->addComplexFieldData(data);
1023 this->field_cmplx.push_back(data);
1028 if (data ==
nullptr) {
1029 LOG(
"not found Complex data");
1033 ivec2 buffer_size =
ivec2(this->Header->fieldInfo.pxNumX, this->Header->fieldInfo.pxNumY);
1038 this->field_cmplx.push_back(complexField);
1050 this->setNumOfWavlen((uint32_t)
WavLeng.size());
1059 this->File.open(this->fname, std::ios::out | std::ios::trunc | std::ios::binary);
1065 LOG(
"Saving...%s...", fname.c_str());
1068 if (this->File.is_open()) {
1069 if (this->Header ==
nullptr) {
1071 this->initOHCheader();
1075 uint64_t dataSize = 0;
1076 switch (
FldInfo.cmplxFldType) {
1079 dataSize = encodeFieldData();
1082 LOG(
"Error : Compressed Image Format Encoding is Not Yet supported...");
1088 LOG(
"Error : Invalid Encoding Complex Field Data Type...");
1140 uint64_t wavlenTableSize =
FldInfo.wavlenNum *
sizeof(double_t);
1142 if (dataSize == 0) {
1143 LOG(
"Error : No Field Data");
1167 double_t waveLength =
WavLeng[n];
1168 File.write((
char*)&waveLength,
sizeof(double_t));
1175 size_t dataTypeSize =
sizeof(float);
1176 ulonglong maxIdx = dataSize / dataTypeSize;
1178 File.write((
char *)&buf_f32[i], dataTypeSize);
1182 size_t dataTypeSize =
sizeof(double);
1183 ulonglong maxIdx = dataSize / dataTypeSize;
1185 File.write((
char *)&buf_f64[i], dataTypeSize);
1194 auto during = ((std::chrono::duration<Real>)(end - start)).count();
1196 LOG(
"%.5lfsec...done\n", during);
1200 LOG(
"Error : Failed saving OHC file...");
1208 int n_wavlens =
FldInfo.wavlenNum;
1211 int n_pixels = cols * rows;
1212 ulonglong n_fields = n_pixels * n_wavlens;
1214 int n_cmplxChnl = 0;
1222 dataSizeBytes =
sizeof(float) * n_fields * n_cmplxChnl;
1223 this->buf_f32 =
new float[n_fields * n_cmplxChnl];
1224 std::memset(this->buf_f32, 0, dataSizeBytes);
1227 dataSizeBytes =
sizeof(double) * n_fields * n_cmplxChnl;
1228 this->buf_f64 =
new double[n_fields * n_cmplxChnl];
1229 std::memset(this->buf_f64, 0, dataSizeBytes);
1232 for (
int x = 0; x < cols; ++x) {
1233 for (
int y = 0; y < rows; ++y) {
1234 int idx = x * rows + y;
1236 for (
int clrChnl = 0; clrChnl < n_wavlens; ++clrChnl) {
1237 ulonglong idx_sqtlChnl = n_wavlens * idx + clrChnl;
1240 switch (
FldInfo.fldCodeType) {
1244 *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y][
_RE];
1245 *(this->buf_f32 + idx_sqtlChnl + 1 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y][
_IM];
1249 *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y][
_RE];
1250 *(this->buf_f64 + idx_sqtlChnl + 1 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y][
_IM];
1257 *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].mag();
1258 *(this->buf_f32 + idx_sqtlChnl + 1 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].angle();
1262 *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].mag();
1263 *(this->buf_f64 + idx_sqtlChnl + 1 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].angle();
1270 *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].mag();
1274 *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].mag();
1281 *(this->buf_f32 + idx_sqtlChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].angle();
1285 *(this->buf_f64 + idx_sqtlChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].angle();
1292 ulonglong idx_eachChnl = idx + clrChnl * n_pixels;
1294 switch (
FldInfo.fldCodeType) {
1298 *(this->buf_f32 + idx_eachChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y][
_RE];
1299 *(this->buf_f32 + idx_eachChnl + 1 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y][
_IM];
1303 *(this->buf_f64 + idx_eachChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y][
_RE];
1304 *(this->buf_f64 + idx_eachChnl + 1 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y][
_IM];
1311 *(this->buf_f32 + idx_eachChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].mag();
1312 *(this->buf_f32 + idx_eachChnl + 1 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].angle();
1316 *(this->buf_f64 + idx_eachChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].mag();
1317 *(this->buf_f64 + idx_eachChnl + 1 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].angle();
1324 *(this->buf_f32 + idx_eachChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].mag();
1328 *(this->buf_f64 + idx_eachChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].mag();
1335 *(this->buf_f32 + idx_eachChnl + 0 * n_fields) = (
float)this->field_cmplx[clrChnl][x][y].angle();
1339 *(this->buf_f64 + idx_eachChnl + 0 * n_fields) = (
double)this->field_cmplx[clrChnl][x][y].angle();
1348 return dataSizeBytes;
1351 LOG(
"Error : Link Image File Encoding is Not Yet supported...");
1352 return dataSizeBytes;
1355 LOG(
"Error : Invalid Field Data Store Type...");
uint64_t encodeFieldData()
virtual void releaseFldData()
void setColorArrange(const ColorArran _clrArrange)
oph::ColorArran getColorArrange()
virtual void initOHCheader()
structure for 2-dimensional integer vector and its arithmetic.
matrix< T > & resize(int x, int y)
void getLinkFilePath(std::vector< std::string > &linkFilePath_array)
void fieldToComplex(void)
void setFieldEncoding(const FldStore _fldStore, const FldCodeType _fldCodeType)
void getComplexFieldData(OphComplexField &cmplx_field, uint wavelen_idx)
unsigned long long ulonglong
oph::ColorType getColorType()
bool setOHCheader(const oph::ohcHeader &_Header)
virtual void releaseFldData()
void addComplexFieldData(const OphComplexField &data)
void setNumOfWavlen(const uint n_wavlens)
void getFieldInfo(oph::ohcFieldInfoHeader &_FieldInfo, std::vector< double_t > &_wavlenTable)
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.
matrix< T > & zeros(void)
void Field2Buffer(matrix< T > &src, T **dst)
bool setFileName(const std::string &_fname)
#define _OPH_LIB_VERSION_MINOR_
oph::LenUnit getUnitOfWavlen()
void setColorType(const ColorType _clrType)
void setPixelPitch(const double _pxPitchX, const double _pxPitchY, const LenUnit unit=LenUnit::m)
oph::LenUnit getPixelPitchUnit()
void setWavelength(const Real _wavlen, const LenUnit _unit=LenUnit::m)
void setNumOfPixel(const uint _pxNumX, const uint _pxNumY)
void setUnitOfWavlen(const LenUnit unit)
void getOHCheader(oph::ohcHeader &_Header)
void Buffer2Field(const T *src, matrix< T > &dst, const ivec2 buffer_size)
oph::CompresType getCompressedFormatType()
void getWavelength(std::vector< double_t > &wavlen_array)
#define _OPH_LIB_VERSION_MAJOR_
void addWavelengthNComplexFieldData(const Real wavlen, const OphComplexField &data)