Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2019
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. // ReadFromFloat16.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
  2. //
  3.  
  4. #include <string>
  5. #include <iostream>
  6. #include <filesystem>
  7. #include <fstream>
  8. #include <vector>
  9. #include <stdint.h>
  10.  
  11. #include <opencv2/core.hpp>
  12. #include <opencv2/highgui.hpp>
  13.  
  14. #if __cplusplus < 201703L // If the version of C++ is less than 17
  15.     // It was still in the experimental:: namespace
  16. namespace fs = std::experimental::filesystem;
  17. #else
  18. namespace fs = std::filesystem;
  19. #endif
  20.  
  21. // My own 16 bit implementation
  22. float fromInt16(uint16_t input)
  23. {
  24.     bool sign = (input >> 15) & 0x1;
  25.     float mantisse = 1.0;
  26.  
  27.     // Mantisse are 10 bit
  28.     for (int count = 0; count < 10; ++count)
  29.     {
  30.         int singleFlag = ((input >> (14 - count)) & 0x1);
  31.         mantisse += 1.0 / (std::pow(2.0, count + 1)) * ((input >> (14 - count)) & 0x1);
  32.     }
  33.     int16_t exponent = input & 0x1F;
  34.     exponent -= 15;
  35.  
  36.     // if exponent == 16, and mantisse > 0 -> NaN
  37.     // if exponent == 16, and mantisse == 0 -> infinity (not handled)
  38.     if (exponent == 16)
  39.         return std::nan("");
  40.  
  41.     float result = mantisse * pow(2.0f, exponent);
  42.     if (sign)
  43.         result = -result;
  44.  
  45.     int test = 0;
  46.     return result;
  47. }
  48.  
  49. std::vector<uint16_t> readFile(std::string filename)
  50. {
  51.     // open the file:
  52.     std::ifstream file(filename, std::ios::binary);
  53.  
  54.     // Stop eating new lines in binary mode!!!
  55.     file.unsetf(std::ios::skipws);
  56.  
  57.     // get its size:
  58.     std::streampos fileSize;
  59.  
  60.     file.seekg(0, std::ios::end);
  61.     fileSize = file.tellg();
  62.     file.seekg(0, std::ios::beg);
  63.  
  64.     // read the data:
  65.     std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(file), {});
  66.  
  67.     file.close();
  68.  
  69.     std::vector<uint16_t> vec;
  70.     vec.resize(buffer.size() / 2);
  71.  
  72.     for (int index = 0; index < vec.size(); ++index)
  73.     {
  74.         vec[index] = static_cast<uint16_t>(buffer[index * 2 + 0]) * 256 + buffer[index * 2 + 1];
  75.     }
  76.  
  77.     return vec;
  78. }
  79.  
  80. int main(int argc, void** argv)
  81. {
  82.     if (argc < 2)
  83.     {
  84.         return -1;
  85.     }
  86.  
  87.     std::string path = std::string(reinterpret_cast<char*>(argv[1]));
  88.     std::vector<std::string> allFiles;
  89.  
  90.     for (const auto & entry : fs::directory_iterator(path))
  91.     {
  92.         allFiles.push_back(entry.path().u8string());
  93.     }
  94.  
  95.     int counter = 0;
  96.  
  97.     for (auto file : allFiles)
  98.     {
  99.         std::vector<uint16_t> buffer = readFile(file);
  100.         std::vector<float> fImageValues;
  101.        
  102.         for (auto value : buffer)
  103.         {
  104.             float tempVal = fromInt16(value);
  105.             fImageValues.push_back(tempVal);
  106.         }
  107.  
  108.         float maxValue = fromInt16(0x7FFE);
  109.         float minValue = fromInt16(0xFFFE);
  110.        
  111.         char* cvData = new char[fImageValues.size()];
  112.  
  113.         for (int index = 0; index < fImageValues.size(); ++index)
  114.         {
  115.             cvData[index] = static_cast<char>((fImageValues[index] - minValue) * 255 / (maxValue - minValue));
  116.         }
  117.  
  118.         cv::Mat img(1024, 1024, CV_8UC4, cvData);
  119.         cv::imshow("Test data", img);
  120.         cv::imwrite("test.jpg", img);
  121.         cv::waitKey(50);
  122.         delete[] cvData;
  123.         printf("Image %d done\n", ++counter);
  124.     }
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement