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::() |
71 | { |
72 | file = fopen(filename: m_filename.c_str(), modes: "rb" ); |
73 | if(!file) { |
74 | return false; |
75 | } |
76 | RGBE_ReadHeader(fp: file, width: &m_width, height: &m_height, NULL); |
77 | if(m_width <= 0 || m_height <= 0) { |
78 | fclose(stream: 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(fp: file, data: const_cast<float*>(img.ptr<float>()), scanline_width: img.cols, num_scanlines: img.rows); |
94 | fclose(stream: file); file = NULL; |
95 | |
96 | if(_img.depth() == img.depth()) { |
97 | img.convertTo(m: _img, rtype: _img.type()); |
98 | } else { |
99 | img.convertTo(m: _img, rtype: _img.type(), alpha: 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(s1: signature.c_str(), s2: m_signature.c_str(), n: m_signature.size()) |
108 | ) |
109 | return true; |
110 | if (signature.size() >= m_signature_alt.size() && |
111 | 0 == memcmp(s1: signature.c_str(), s2: m_signature_alt.c_str(), n: 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(mv: splitted, dst: img); |
138 | } else { |
139 | input_img.copyTo(m: img); |
140 | } |
141 | if(img.depth() != CV_32F) { |
142 | img.convertTo(m: img, CV_32FC3, alpha: 1/255.0f); |
143 | } |
144 | |
145 | int compression = IMWRITE_HDR_COMPRESSION_RLE; |
146 | for (size_t i = 0; i + 1 < params.size(); i += 2) |
147 | { |
148 | switch (params[i]) |
149 | { |
150 | case IMWRITE_HDR_COMPRESSION: |
151 | compression = params[i + 1]; |
152 | break; |
153 | default: |
154 | break; |
155 | } |
156 | } |
157 | CV_Check(compression, compression == IMWRITE_HDR_COMPRESSION_NONE || compression == IMWRITE_HDR_COMPRESSION_RLE, "" ); |
158 | |
159 | FILE *fout = fopen(filename: m_filename.c_str(), modes: "wb" ); |
160 | if(!fout) { |
161 | return false; |
162 | } |
163 | |
164 | RGBE_WriteHeader(fp: fout, width: img.cols, height: img.rows, NULL); |
165 | if (compression == IMWRITE_HDR_COMPRESSION_RLE) { |
166 | RGBE_WritePixels_RLE(fp: fout, data: const_cast<float*>(img.ptr<float>()), scanline_width: img.cols, num_scanlines: img.rows); |
167 | } else { |
168 | RGBE_WritePixels(fp: fout, data: const_cast<float*>(img.ptr<float>()), numpixels: img.cols * img.rows); |
169 | } |
170 | |
171 | fclose(stream: fout); |
172 | return true; |
173 | } |
174 | |
175 | ImageEncoder HdrEncoder::newEncoder() const |
176 | { |
177 | return makePtr<HdrEncoder>(); |
178 | } |
179 | |
180 | bool HdrEncoder::isFormatSupported( int depth ) const { |
181 | return depth != CV_64F; |
182 | } |
183 | |
184 | } |
185 | |
186 | #endif // HAVE_IMGCODEC_HDR |
187 | |