Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _DNGCREATOR_HPP
- #define _DNGCREATOR_HPP
- #include <opencv2/opencv.hpp>
- #include "dng_camera_profile.h"
- #include "dng_color_space.h"
- #include "dng_date_time.h"
- #include "dng_exceptions.h"
- #include "dng_file_stream.h"
- #include "dng_globals.h"
- #include "dng_host.h"
- #include "dng_ifd.h"
- #include "dng_image_writer.h"
- #include "dng_info.h"
- #include "dng_linearization_info.h"
- #include "dng_mosaic_info.h"
- #include "dng_negative.h"
- #include "dng_preview.h"
- #include "dng_render.h"
- #include "dng_simple_image.h"
- #include "dng_tag_codes.h"
- #include "dng_tag_types.h"
- #include "dng_tag_values.h"
- #include "dng_xmp.h"
- #include "dng_xmp_sdk.h"
- class DngCreator {
- public:
- DngCreator();
- ~DngCreator();
- void BuildDngImage(cv::Mat image);
- void setCameraProfile();
- void setMosaicInfo();
- void setXmp();
- void WriteDng(const char* filename);
- private:
- dng_host host_;
- AutoPtr<dng_negative> negative_;
- ushort *rawBuffer;
- AutoPtr<dng_xmp> negXmp;
- };
- cv::Mat GetMosaic(cv::Mat source);
- #endif /* _DNGCREATOR_H */
- //IMPLEMENTATION PART
- //IT IS COPIED FROM CPP FILE
- using namespace cv;
- using namespace std;
- Mat GetMosaic(Mat source) {
- //Converting 8 bit images to 16 bit
- Mat mosaic(source.size(),CV_16UC1);
- Mat img;
- source.convertTo(img,CV_16UC3,257);
- using depth = uint16_t;
- using vec_depth = Vec3w;
- for (int row(0); row < img.rows; row++) {
- for (int col(0); col < img.cols; col++) {
- if( (row == 0 || row % 2 == 0) && (col == 0 || col % 2 == 0)){ //red color
- mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[2];
- } else if(row % 2 == 1 && col % 2 == 1) { //blue color
- mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[0];
- } else {
- mosaic.at<depth>(row,col) = img.at<vec_depth>(row, col)[1];
- }
- }
- }
- return mosaic;
- }
- DngCreator::DngCreator() {
- dng_xmp_sdk::InitializeSDK();
- host_.SetSaveDNGVersion(dngVersion_SaveDefault);
- host_.SetSaveLinearDNG(false);
- host_.SetKeepOriginalFile(true);
- negative_.Reset(host_.Make_dng_negative ());
- }
- DngCreator::~DngCreator() {
- dng_xmp_sdk::TerminateSDK();
- }
- void DngCreator::setCameraProfile() {
- AutoPtr<dng_camera_profile> profile(new dng_camera_profile);
- dng_string profile_name;
- profile_name.Append("Syntetic profile");
- profile->SetName(profile_name.Get());
- negative_->AddProfile(profile);
- }
- void DngCreator::setMosaicInfo() {
- negative_->SetFourColorBayer();
- //RGGB pattern
- ColorKeyCode color0 = ColorKeyCode::colorKeyRed;
- ColorKeyCode color1 = ColorKeyCode::colorKeyGreen;
- ColorKeyCode color2 = ColorKeyCode::colorKeyBlue;
- negative_->SetColorKeys(color0,color1,color2);
- negative_->SetBayerMosaic(1);
- }
- void DngCreator::BuildDngImage(Mat mosaic) {
- using depth = ushort;
- int raw_height = mosaic.rows;
- int raw_width = mosaic.cols;
- depth *rawBuffer = (depth*)malloc(raw_width * raw_height * sizeof(depth));
- for (int row = 0; row < raw_height; row++) {
- for (int col = 0; col < raw_width; col++) {
- int idx = row * raw_width + col;
- rawBuffer[idx] = mosaic.at<depth>(row,col);
- }
- }
- uint32 inputPlanes = 1;
- uint32 outputPlanes = 1;
- dng_rect bounds = dng_rect(raw_height, raw_width);
- dng_simple_image *image = new dng_simple_image(bounds, outputPlanes,ttShort, host_.Allocator());
- dng_pixel_buffer buffer;
- image->GetPixelBuffer(buffer);
- depth* imageBuffer = (depth*)buffer.fData;
- memcpy(imageBuffer, rawBuffer, raw_height * raw_width * outputPlanes * sizeof(depth));
- AutoPtr<dng_image> castImage(dynamic_cast<dng_image*>(image));
- negative_->SetStage1Image(castImage);
- }
- void DngCreator::setXmp() {
- negXmp.Reset(new dng_xmp(host_.Allocator()));
- negative_->ResetXMP(negXmp.Release());
- }
- void DngCreator::WriteDng(const char *filename) {
- dng_file_stream ostream(filename, true);
- dng_image_writer writer;
- writer.WriteDNG(host_,
- ostream,
- *negative_.Get(),
- nullptr,
- dngVersion_Current,
- true);
- }
Add Comment
Please, Sign In to add comment