Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ReadFromFloat16.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
- //
- #include <string>
- #include <iostream>
- #include <filesystem>
- #include <fstream>
- #include <vector>
- #include <stdint.h>
- #include <opencv2/core.hpp>
- #include <opencv2/highgui.hpp>
- #if __cplusplus < 201703L // If the version of C++ is less than 17
- // It was still in the experimental:: namespace
- namespace fs = std::experimental::filesystem;
- #else
- namespace fs = std::filesystem;
- #endif
- // My own 16 bit implementation
- float fromInt16(uint16_t input)
- {
- bool sign = (input >> 15) & 0x1;
- float mantisse = 1.0;
- // Mantisse are 10 bit
- for (int count = 0; count < 10; ++count)
- {
- int singleFlag = ((input >> (14 - count)) & 0x1);
- mantisse += 1.0 / (std::pow(2.0, count + 1)) * ((input >> (14 - count)) & 0x1);
- }
- int16_t exponent = input & 0x1F;
- exponent -= 15;
- // if exponent == 16, and mantisse > 0 -> NaN
- // if exponent == 16, and mantisse == 0 -> infinity (not handled)
- if (exponent == 16)
- return std::nan("");
- float result = mantisse * pow(2.0f, exponent);
- if (sign)
- result = -result;
- int test = 0;
- return result;
- }
- std::vector<uint16_t> readFile(std::string filename)
- {
- // open the file:
- std::ifstream file(filename, std::ios::binary);
- // Stop eating new lines in binary mode!!!
- file.unsetf(std::ios::skipws);
- // get its size:
- std::streampos fileSize;
- file.seekg(0, std::ios::end);
- fileSize = file.tellg();
- file.seekg(0, std::ios::beg);
- // read the data:
- std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(file), {});
- file.close();
- std::vector<uint16_t> vec;
- vec.resize(buffer.size() / 2);
- for (int index = 0; index < vec.size(); ++index)
- {
- vec[index] = static_cast<uint16_t>(buffer[index * 2 + 0]) * 256 + buffer[index * 2 + 1];
- }
- return vec;
- }
- int main(int argc, void** argv)
- {
- if (argc < 2)
- {
- return -1;
- }
- std::string path = std::string(reinterpret_cast<char*>(argv[1]));
- std::vector<std::string> allFiles;
- for (const auto & entry : fs::directory_iterator(path))
- {
- allFiles.push_back(entry.path().u8string());
- }
- int counter = 0;
- for (auto file : allFiles)
- {
- std::vector<uint16_t> buffer = readFile(file);
- std::vector<float> fImageValues;
- for (auto value : buffer)
- {
- float tempVal = fromInt16(value);
- fImageValues.push_back(tempVal);
- }
- float maxValue = fromInt16(0x7FFE);
- float minValue = fromInt16(0xFFFE);
- char* cvData = new char[fImageValues.size()];
- for (int index = 0; index < fImageValues.size(); ++index)
- {
- cvData[index] = static_cast<char>((fImageValues[index] - minValue) * 255 / (maxValue - minValue));
- }
- cv::Mat img(1024, 1024, CV_8UC4, cvData);
- cv::imshow("Test data", img);
- cv::imwrite("test.jpg", img);
- cv::waitKey(50);
- delete[] cvData;
- printf("Image %d done\n", ++counter);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement