Path: blob/master/modules/imgcodecs/src/grfmt_hdr.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "grfmt_hdr.hpp"44#include "rgbe.hpp"4546#ifdef HAVE_IMGCODEC_HDR4748namespace cv49{5051HdrDecoder::HdrDecoder()52{53m_signature = "#?RGBE";54m_signature_alt = "#?RADIANCE";55file = NULL;56m_type = CV_32FC3;57}5859HdrDecoder::~HdrDecoder()60{61}6263size_t HdrDecoder::signatureLength() const64{65return m_signature.size() > m_signature_alt.size() ?66m_signature.size() : m_signature_alt.size();67}6869bool HdrDecoder::readHeader()70{71file = fopen(m_filename.c_str(), "rb");72if(!file) {73return false;74}75RGBE_ReadHeader(file, &m_width, &m_height, NULL);76if(m_width <= 0 || m_height <= 0) {77fclose(file);78file = NULL;79return false;80}81return true;82}8384bool HdrDecoder::readData(Mat& _img)85{86Mat img(m_height, m_width, CV_32FC3);87if(!file) {88if(!readHeader()) {89return false;90}91}92RGBE_ReadPixels_RLE(file, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);93fclose(file); file = NULL;9495if(_img.depth() == img.depth()) {96img.convertTo(_img, _img.type());97} else {98img.convertTo(_img, _img.type(), 255);99}100return true;101}102103bool HdrDecoder::checkSignature( const String& signature ) const104{105if (signature.size() >= m_signature.size() &&1060 == memcmp(signature.c_str(), m_signature.c_str(), m_signature.size())107)108return true;109if (signature.size() >= m_signature_alt.size() &&1100 == memcmp(signature.c_str(), m_signature_alt.c_str(), m_signature_alt.size())111)112return true;113return false;114}115116ImageDecoder HdrDecoder::newDecoder() const117{118return makePtr<HdrDecoder>();119}120121HdrEncoder::HdrEncoder()122{123m_description = "Radiance HDR (*.hdr;*.pic)";124}125126HdrEncoder::~HdrEncoder()127{128}129130bool HdrEncoder::write( const Mat& input_img, const std::vector<int>& params )131{132Mat img;133CV_Assert(input_img.channels() == 3 || input_img.channels() == 1);134if(input_img.channels() == 1) {135std::vector<Mat> splitted(3, input_img);136merge(splitted, img);137} else {138input_img.copyTo(img);139}140if(img.depth() != CV_32F) {141img.convertTo(img, CV_32FC3, 1/255.0f);142}143CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE);144FILE *fout = fopen(m_filename.c_str(), "wb");145if(!fout) {146return false;147}148149RGBE_WriteHeader(fout, img.cols, img.rows, NULL);150if(params.empty() || params[0] == HDR_RLE) {151RGBE_WritePixels_RLE(fout, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);152} else {153RGBE_WritePixels(fout, const_cast<float*>(img.ptr<float>()), img.cols * img.rows);154}155156fclose(fout);157return true;158}159160ImageEncoder HdrEncoder::newEncoder() const161{162return makePtr<HdrEncoder>();163}164165bool HdrEncoder::isFormatSupported( int depth ) const {166return depth != CV_64F;167}168169}170171#endif // HAVE_IMGCODEC_HDR172173174