Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/src/grfmt_hdr.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "grfmt_hdr.hpp"
45
#include "rgbe.hpp"
46
47
#ifdef HAVE_IMGCODEC_HDR
48
49
namespace cv
50
{
51
52
HdrDecoder::HdrDecoder()
53
{
54
m_signature = "#?RGBE";
55
m_signature_alt = "#?RADIANCE";
56
file = NULL;
57
m_type = CV_32FC3;
58
}
59
60
HdrDecoder::~HdrDecoder()
61
{
62
}
63
64
size_t HdrDecoder::signatureLength() const
65
{
66
return m_signature.size() > m_signature_alt.size() ?
67
m_signature.size() : m_signature_alt.size();
68
}
69
70
bool HdrDecoder::readHeader()
71
{
72
file = fopen(m_filename.c_str(), "rb");
73
if(!file) {
74
return false;
75
}
76
RGBE_ReadHeader(file, &m_width, &m_height, NULL);
77
if(m_width <= 0 || m_height <= 0) {
78
fclose(file);
79
file = NULL;
80
return false;
81
}
82
return true;
83
}
84
85
bool HdrDecoder::readData(Mat& _img)
86
{
87
Mat img(m_height, m_width, CV_32FC3);
88
if(!file) {
89
if(!readHeader()) {
90
return false;
91
}
92
}
93
RGBE_ReadPixels_RLE(file, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);
94
fclose(file); file = NULL;
95
96
if(_img.depth() == img.depth()) {
97
img.convertTo(_img, _img.type());
98
} else {
99
img.convertTo(_img, _img.type(), 255);
100
}
101
return true;
102
}
103
104
bool HdrDecoder::checkSignature( const String& signature ) const
105
{
106
if (signature.size() >= m_signature.size() &&
107
0 == memcmp(signature.c_str(), m_signature.c_str(), m_signature.size())
108
)
109
return true;
110
if (signature.size() >= m_signature_alt.size() &&
111
0 == memcmp(signature.c_str(), m_signature_alt.c_str(), m_signature_alt.size())
112
)
113
return true;
114
return false;
115
}
116
117
ImageDecoder HdrDecoder::newDecoder() const
118
{
119
return makePtr<HdrDecoder>();
120
}
121
122
HdrEncoder::HdrEncoder()
123
{
124
m_description = "Radiance HDR (*.hdr;*.pic)";
125
}
126
127
HdrEncoder::~HdrEncoder()
128
{
129
}
130
131
bool HdrEncoder::write( const Mat& input_img, const std::vector<int>& params )
132
{
133
Mat img;
134
CV_Assert(input_img.channels() == 3 || input_img.channels() == 1);
135
if(input_img.channels() == 1) {
136
std::vector<Mat> splitted(3, input_img);
137
merge(splitted, img);
138
} else {
139
input_img.copyTo(img);
140
}
141
if(img.depth() != CV_32F) {
142
img.convertTo(img, CV_32FC3, 1/255.0f);
143
}
144
CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE);
145
FILE *fout = fopen(m_filename.c_str(), "wb");
146
if(!fout) {
147
return false;
148
}
149
150
RGBE_WriteHeader(fout, img.cols, img.rows, NULL);
151
if(params.empty() || params[0] == HDR_RLE) {
152
RGBE_WritePixels_RLE(fout, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);
153
} else {
154
RGBE_WritePixels(fout, const_cast<float*>(img.ptr<float>()), img.cols * img.rows);
155
}
156
157
fclose(fout);
158
return true;
159
}
160
161
ImageEncoder HdrEncoder::newEncoder() const
162
{
163
return makePtr<HdrEncoder>();
164
}
165
166
bool HdrEncoder::isFormatSupported( int depth ) const {
167
return depth != CV_64F;
168
}
169
170
}
171
172
#endif // HAVE_IMGCODEC_HDR
173
174