Openholo  v4.1
Open Source Digital Holographic Library
examLightField.cpp
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 #include <iostream>
47 #include <string>
48 #include <filesystem>
49 #include <ctime>
50 
51 #include "ophLightField.h"
52 
53 namespace fs = std::experimental::filesystem;
54 
55 int main() {
56  // Create ophLF instance
57  ophLF* Hologram = new ophLF();
58 
59  std::string proj_dir = fs::current_path().generic_string();
60  std::string conf_file = proj_dir + "/../dataset/Dice/LightField/Dice_LightField.xml";
61 
62  // Read Config Parameters for Light Field CGH
63  if (!Hologram->readConfig(conf_file.c_str())) {
64  std::cerr << "Fail to load Config file : " << conf_file << std::endl;
65  exit(1);
66  }
67 
68  // Load the Light Field source image files
69  std::string lf_path = proj_dir + "/../dataset/Dice/LightField";
70  if (Hologram->loadLF(lf_path.c_str(), "bmp") < 0) {
71  std::cerr << "Fail to load Light Field files : " << lf_path << std::endl;
72  exit(1);
73  }
74 
75  // Generate the hologram
76  Hologram->generateHologram();
77 
78  // Save to ohc(Openholo complex field file format)
79  std::string res_path = "./result/";
80  if (!fs::exists(res_path)) {
81  fs::create_directories(res_path);
82  }
83 
84  //
85  time_t cur_time = time(nullptr);
86 
87  struct tm local;
88 #ifdef _MSC_VER
89  localtime_s(&local, &cur_time);
90 #else
91  localtime_r(&cur_time, &local);
92 #endif
93 
94  char buff[32];
95 #ifdef _MSC_VER
96  sprintf_s(buff, "%4d%02d%02d-%02d%02d%02d",
97  local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
98  local.tm_hour, local.tm_min, local.tm_sec);
99 #else
100  sprintf(buff, "%4d%02d%02d-%02d%02d%02d",
101  local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
102  local.tm_hour, local.tm_min, local.tm_sec);
103 #endif
104 
105  std::string ohc_file = res_path + "lf_sample_" + std::string(buff) + ".ohc";
106  if (!Hologram->saveAsOhc(ohc_file.c_str())) {
107  std::cerr << "Fail to save OHC file : " << ohc_file << std::endl;
108  exit(1);
109  }
110 
111  // Encode the hologram
112  Hologram->encoding(Hologram->ENCODE_PHASE);
113 
114  // Normalize the encoded hologram to generate image file
115  Hologram->normalize();
116 
117  // Save the encoded hologram image
118  ivec2 m_vecEncodeSize = Hologram->getEncodeSize(); // Get encoded hologram size
119  std::string bmp_file = res_path + "lf_sample_" + std::string(buff) + ".bmp";
120  if (!Hologram->save(bmp_file.c_str(), 8, nullptr, m_vecEncodeSize[_X], m_vecEncodeSize[_Y])) {
121  std::cerr << "Fail to save BMP file : " << bmp_file << std::endl;
122  exit(1);
123  }
124 
125  // Release memory used to Generate Light Field
126  Hologram->release();
127 
128  return 0;
129 }
bool readConfig(const char *fname)
Light Field based CGH configuration file load.
ivec2 & getEncodeSize(void)
Function for getting encode size.
Definition: ophGen.h:363
#define _Y
Definition: define.h:96
Openholo Light Field based CGH generation.
Definition: ophLightField.h:94
void generateHologram()
Hologram generation.
void normalize(void)
Normalization function to save as image file after hologram creation.
Definition: ophGen.cpp:677
void encoding(unsigned int ENCODE_FLAG)
Encoding Functions.
Definition: ophGen.cpp:839
bool save(const char *fname, uint8_t bitsperpixel=8, uchar *src=nullptr, uint px=0, uint py=0)
Function for saving image files.
Definition: ophGen.cpp:712
int main()
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
Definition: Openholo.cpp:252
unsigned long release(void)
Call release() when reference is finished.
Definition: Base.h:88
#define _X
Definition: define.h:92
int loadLF(const char *directory, const char *exten)
Light Field images load.