ViktorMansfield

The simplest dng writing

May 23rd, 2021 (edited)
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #ifndef _DNGCREATOR_HPP
  2. #define _DNGCREATOR_HPP
  3.  
  4. #include <opencv2/opencv.hpp>
  5.  
  6. #include "dng_camera_profile.h"
  7. #include "dng_color_space.h"
  8. #include "dng_date_time.h"
  9. #include "dng_exceptions.h"
  10. #include "dng_file_stream.h"
  11. #include "dng_globals.h"
  12. #include "dng_host.h"
  13. #include "dng_ifd.h"
  14. #include "dng_image_writer.h"
  15. #include "dng_info.h"
  16. #include "dng_linearization_info.h"
  17. #include "dng_mosaic_info.h"
  18. #include "dng_negative.h"
  19. #include "dng_preview.h"
  20. #include "dng_render.h"
  21. #include "dng_simple_image.h"
  22. #include "dng_tag_codes.h"
  23. #include "dng_tag_types.h"
  24. #include "dng_tag_values.h"
  25. #include "dng_xmp.h"
  26. #include "dng_xmp_sdk.h"
  27.  
  28. class DngCreator {
  29. public:
  30.     DngCreator();
  31.     ~DngCreator();
  32.     void BuildDngImage(cv::Mat image);
  33.     void setCameraProfile();
  34.     void setMosaicInfo();
  35.     void setXmp();
  36.     void WriteDng(const char* filename);
  37. private:
  38.     dng_host host_;
  39.     AutoPtr<dng_negative> negative_;
  40.     ushort *rawBuffer;
  41.     AutoPtr<dng_xmp> negXmp;
  42. };
  43.  
  44. cv::Mat GetMosaic(cv::Mat source);
  45.  
  46. #endif  /* _DNGCREATOR_H */
  47.  
  48.  
  49.  
  50.  
  51.  
  52. //IMPLEMENTATION PART
  53. //IT IS COPIED FROM CPP FILE
  54.  
  55. using namespace cv;
  56. using namespace std;
  57.  
  58. Mat GetMosaic(Mat source) {
  59.     //Converting 8 bit images to 16 bit
  60.     Mat mosaic(source.size(),CV_16UC1);
  61.     Mat img;
  62.     source.convertTo(img,CV_16UC3,257);
  63.  
  64.     using depth = uint16_t;
  65.     using vec_depth = Vec3w;
  66.     for (int row(0); row < img.rows; row++) {
  67.         for (int col(0); col < img.cols; col++) {
  68.             if( (row == 0 || row % 2 == 0) && (col == 0 || col % 2 == 0)){ //red color
  69.                 mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[2];
  70.             } else if(row % 2 == 1 && col % 2 == 1) { //blue color
  71.                 mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[0];
  72.             } else {
  73.                 mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[1];
  74.             }
  75.         }
  76.     }
  77.     return mosaic;
  78. }
  79.  
  80. DngCreator::DngCreator() {
  81.     dng_xmp_sdk::InitializeSDK();
  82.     host_.SetSaveDNGVersion(dngVersion_SaveDefault);
  83.     host_.SetSaveLinearDNG(false);
  84.     host_.SetKeepOriginalFile(true);
  85.  
  86.  
  87.     negative_.Reset(host_.Make_dng_negative ());
  88. }
  89.  
  90. DngCreator::~DngCreator() {
  91.     dng_xmp_sdk::TerminateSDK();
  92. }
  93.  
  94. void DngCreator::setCameraProfile() {
  95.     AutoPtr<dng_camera_profile> profile(new dng_camera_profile);
  96.  
  97.     dng_string profile_name;
  98.     profile_name.Append("Syntetic profile");
  99.  
  100.     profile->SetName(profile_name.Get());
  101.     negative_->AddProfile(profile);
  102. }
  103.  
  104.  
  105. void DngCreator::setMosaicInfo() {
  106.     negative_->SetFourColorBayer();
  107.  
  108.     //RGGB pattern
  109.    
  110.     ColorKeyCode color0 = ColorKeyCode::colorKeyRed;
  111.     ColorKeyCode color1 = ColorKeyCode::colorKeyGreen;
  112.     ColorKeyCode color2  = ColorKeyCode::colorKeyBlue;
  113.  
  114.     negative_->SetColorKeys(color0,color1,color2);
  115.  
  116.     negative_->SetBayerMosaic(1);
  117. }
  118.  
  119. void DngCreator::BuildDngImage(Mat mosaic) {
  120.  
  121.     using depth = ushort;
  122.     int raw_height = mosaic.rows;
  123.     int raw_width  = mosaic.cols;
  124.  
  125.     depth *rawBuffer = (depth*)malloc(raw_width * raw_height * sizeof(depth));
  126.  
  127.     for (int row = 0; row < raw_height; row++) {
  128.         for (int col = 0; col < raw_width; col++) {
  129.             int idx = row * raw_width + col;
  130.             rawBuffer[idx] = mosaic.at<depth>(row,col);
  131.         }  
  132.     }
  133.  
  134.     uint32 inputPlanes = 1;
  135.     uint32 outputPlanes = 1;
  136.  
  137.     dng_rect bounds = dng_rect(raw_height, raw_width);
  138.     dng_simple_image *image = new dng_simple_image(bounds, outputPlanes,ttShort, host_.Allocator());
  139.  
  140.     dng_pixel_buffer buffer;  
  141.     image->GetPixelBuffer(buffer);
  142.  
  143.     depth* imageBuffer = (depth*)buffer.fData;
  144.  
  145.     memcpy(imageBuffer, rawBuffer, raw_height * raw_width * outputPlanes * sizeof(depth));
  146.  
  147.     AutoPtr<dng_image> castImage(dynamic_cast<dng_image*>(image));
  148.     negative_->SetStage1Image(castImage);
  149. }
  150.  
  151. void DngCreator::setXmp() {
  152.     negXmp.Reset(new dng_xmp(host_.Allocator()));
  153.     negative_->ResetXMP(negXmp.Release());
  154. }
  155.  
  156. void DngCreator::WriteDng(const char *filename) {
  157.     dng_file_stream ostream(filename, true);
  158.  
  159.     dng_image_writer writer;
  160.  
  161.     writer.WriteDNG(host_,
  162.                     ostream,
  163.                     *negative_.Get(),
  164.                     nullptr,
  165.                     dngVersion_Current,
  166.                     true);
  167. }
Add Comment
Please, Sign In to add comment