Advertisement
Guest User

main.cpp

a guest
Nov 21st, 2019
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <cstdio>
  2. #include <ctime>
  3. #include <libraw/libraw.h>
  4. #include <opencv2/opencv.hpp>
  5. #include <pybind11/pybind11.h>
  6.  
  7. cv::Mat convert_cr3(std::string filename) {
  8.     int ret; // Variable to consume the error-codes
  9.     clock_t x, y, start; // Variables for the profiler
  10.     LibRaw RawProcessor; // Variable for the raw-data
  11.     x = start = clock();
  12.  
  13.     // Open file - With error recognition
  14.     printf("Opening file %s\n", filename.c_str());
  15.     if ((ret = RawProcessor.open_file(filename.c_str())) != LIBRAW_SUCCESS) {
  16.         printf("\033[0;31mCannot open_file %s: %s\033[0m\n", filename.c_str(), libraw_strerror(ret));
  17.         exit(EXIT_FAILURE);
  18.     }
  19.     y = clock();
  20.     printf("\033[0;32mElapsed time: %f\033[0m\n\n", double(y-x)/CLOCKS_PER_SEC);
  21.  
  22.     // Unpack file - With error recognition
  23.     printf("Unpacking image data\n");
  24.     if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS) {
  25.         printf("\033[0;31mCannot unpack %s: %s\033[0m\n", filename.c_str(), libraw_strerror(ret));
  26.         exit(EXIT_FAILURE);
  27.     }
  28.     x = clock();
  29.     printf("\033[0;32mElapsed time: %f\033[0m\n\n", double(x-y)/CLOCKS_PER_SEC);
  30.  
  31.     // Post process file - With error recognition
  32.     printf("Postprocessing of image data\n");
  33.     if ((ret = RawProcessor.dcraw_process()) != LIBRAW_SUCCESS) {
  34.         printf("\033[0;31mCannot do postprocessing on %s: %s\033[0m\n", filename.c_str(), libraw_strerror(ret));
  35.         exit(EXIT_FAILURE);
  36.     }
  37.     y = clock();
  38.     printf("\033[0;32mElapsed time: %f\033[0m\n\n", double(y-x)/CLOCKS_PER_SEC);
  39.  
  40.     // Make OpenCV-Matrix out of LibRaw-format
  41.     printf("Convert to cv::Mat\n");
  42.     libraw_processed_image_t *img = RawProcessor.dcraw_make_mem_image();
  43.     cv::Mat image = cv::Mat(img->height, img->width, CV_8UC3, img->data);
  44.     cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
  45.     if ( !image.data ) {
  46.         printf("\033[0;31mNo image data!\033[0m\n");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.     x = clock();
  50.     printf("\033[0;32mElapsed time: %f\033[0m\n\n", double(x-y)/CLOCKS_PER_SEC);
  51.     printf("\033[0;32mTotal elapsed time: %f\033[0m\n\n", double(x-start)/CLOCKS_PER_SEC);
  52.  
  53.     return image;
  54. }
  55.  
  56. PYBIND11_MODULE(cr3_to_python, m) {
  57.     m.doc() = "pybind11-plugin to convert an existing .cr3-image to an RGB-array"; // optional module docstring
  58.     m.def("convert_cr3", &convert_cr3, "Convert a .cr3-image to RGB-array");
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement