libopenraw  0.3.7
mosaicinfo.cpp
1 /*
2  * libopenraw - mosaicinfo.cpp
3  *
4  * Copyright (C) 2012-2019 Hubert Figuière
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <stdint.h>
22 #include <stddef.h>
23 
24 #include <array>
25 
26 #include <boost/static_assert.hpp>
27 
28 #include <libopenraw/consts.h>
29 
30 #include "mosaicinfo.hpp"
31 
32 namespace OpenRaw {
33 
34 namespace Internals {
35 
41 static const uint8_t RED = OR_PATTERN_COLOUR_RED;
42 static const uint8_t GREEN = OR_PATTERN_COLOUR_GREEN;
43 static const uint8_t BLUE = OR_PATTERN_COLOUR_BLUE;
44 
45 static const uint8_t RGGB_PATTERN[] = { RED, GREEN, GREEN, BLUE };
46 static const uint8_t GBRG_PATTERN[] = { GREEN, BLUE, RED, GREEN };
47 static const uint8_t BGGR_PATTERN[] = { BLUE, GREEN, GREEN, RED };
48 static const uint8_t GRBG_PATTERN[] = { GREEN, RED, BLUE, GREEN };
49 
52  : public MosaicInfo
53 {
54 public:
57  : MosaicInfo(pattern, 2, 2)
58  {
59  switch(pattern) {
60  case OR_CFA_PATTERN_RGGB:
61  setPatternPattern(RGGB_PATTERN, 4);
62  break;
63  case OR_CFA_PATTERN_GBRG:
64  setPatternPattern(GBRG_PATTERN, 4);
65  break;
66  case OR_CFA_PATTERN_BGGR:
67  setPatternPattern(BGGR_PATTERN, 4);
68  break;
69  case OR_CFA_PATTERN_GRBG:
70  setPatternPattern(GRBG_PATTERN, 4);
71  break;
72 
73  default:
74  break;
75  }
76  }
77 
78 };
79 
81 }
82 
83 const MosaicInfo*
85 {
86  static std::array<MosaicInfo*, _OR_CFA_PATTERN_INVALID> s_patterns
87  = { { NULL, NULL, NULL, NULL, NULL, NULL } };
88  // this should be updated if we change the enum
89  BOOST_STATIC_ASSERT(_OR_CFA_PATTERN_INVALID == 6);
90 
91  if((pattern == OR_CFA_PATTERN_NON_RGB22) ||
92  (pattern >= _OR_CFA_PATTERN_INVALID)) {
93  return NULL;
94  }
95 
96  MosaicInfo* pat = s_patterns[pattern];
97  if(!pat) {
98  pat = new Internals::Cfa2x2RgbPattern(pattern);
99  s_patterns[pattern] = pat;
100  }
101 
102  return pat;
103 }
104 
105 
107 {
108 public:
109  friend class Internals::Cfa2x2RgbPattern;
110 
111  Private()
112  : x(0), y(0), n_colours(0)
113  , pattern_type(OR_CFA_PATTERN_NONE)
114  , pattern(NULL)
115  {}
116 
117  uint16_t x;
118  uint16_t y;
119  uint16_t n_colours;
120  ::or_cfa_pattern pattern_type;
121  const uint8_t* pattern;
122 };
123 
124 MosaicInfo::MosaicInfo()
125  : d(new MosaicInfo::Private)
126 {
127 }
128 
129 MosaicInfo::MosaicInfo(::or_cfa_pattern pattern,
130  uint16_t width, uint16_t height)
131  : d(new MosaicInfo::Private)
132 {
133  setSize(width, height);
134  setPatternType(pattern);
135 }
136 
137 MosaicInfo::~MosaicInfo()
138 {
139  delete d;
140 }
141 
142 void MosaicInfo::setSize(uint16_t x, uint16_t y)
143 {
144  d->x = x;
145  d->y = y;
146  if(x != 2 || y != 2) {
147  d->pattern_type = OR_CFA_PATTERN_NON_RGB22;
148  }
149  else if(!is2by2Rgb()) {
150  d->pattern_type = OR_CFA_PATTERN_NONE;
151  }
152 }
153 
154 void MosaicInfo::getSize(uint16_t &x, uint16_t &y) const
155 {
156  x = d->y;
157  y = d->y;
158 }
159 
161 {
162  return (d->pattern_type != OR_CFA_PATTERN_NONE)
163  && (d->pattern_type != OR_CFA_PATTERN_NON_RGB22);
164 }
165 
166 void
167 MosaicInfo::setPatternPattern(const uint8_t* pattern, uint16_t count)
168 {
169  if(count != d->x * d->y) {
170  d->pattern = NULL;
171  // TODO deal with the error
172  return;
173  }
174  d->pattern = pattern;
175 }
176 
177 const uint8_t*
178 MosaicInfo::patternPattern(uint16_t& count) const
179 {
180  if(d->pattern) {
181  count = d->x * d->y;
182  return d->pattern;
183  }
184 
185  count = 0;
186  return NULL;
187 }
188 
190 {
191  d->pattern_type = pattern;
192  if(is2by2Rgb()) {
193  setSize(2, 2);
194  d->n_colours = 3;
195  }
196 }
197 
200 {
201  return d->pattern_type;
202 }
203 
204 }
205 /*
206  Local Variables:
207  mode:c++
208  c-file-style:"stroustrup"
209  c-file-offsets:((innamespace . 0))
210  tab-width:2
211  c-basic-offset:2
212  indent-tabs-mode:nil
213  fill-column:80
214  End:
215 */
The MosaicInfo type for 2x2 bayer mosaic patterns.
Definition: mosaicinfo.cpp:53
Cfa2x2RgbPattern(::or_cfa_pattern pattern)
Constructor for a specific 2x2 pattern.
Definition: mosaicinfo.cpp:56
Info on the mosaic for the Colour Filter Array.
Definition: mosaicinfo.hpp:33
void setSize(uint16_t x, uint16_t y)
Set the pattern size.
Definition: mosaicinfo.cpp:142
void setPatternType(::or_cfa_pattern pattern)
Set the pattern type.
Definition: mosaicinfo.cpp:189
::or_cfa_pattern patternType() const
Definition: mosaicinfo.cpp:199
Private * d
Private data.
Definition: mosaicinfo.hpp:87
void getSize(uint16_t &x, uint16_t &y) const
Get the pattern size.
Definition: mosaicinfo.cpp:154
void setPatternPattern(const uint8_t *pattern, uint16_t count)
Set the pattern pattern.
Definition: mosaicinfo.cpp:167
static const MosaicInfo * twoByTwoPattern(::or_cfa_pattern)
Factory to return a singleton instance of the right pattern.
Definition: mosaicinfo.cpp:84
const uint8_t * patternPattern(uint16_t &count) const
Return the pattern's pattern.
Definition: mosaicinfo.cpp:178
bool is2by2Rgb() const
Return if the pattern is 2x2 RGB.
Definition: mosaicinfo.cpp:160
or_cfa_pattern
CFA pattern types.
Definition: consts.h:94
@ OR_CFA_PATTERN_NON_RGB22
Definition: consts.h:96
@ OR_CFA_PATTERN_NONE
Definition: consts.h:95
@ OR_PATTERN_COLOUR_RED
Definition: consts.h:106
@ OR_PATTERN_COLOUR_GREEN
Definition: consts.h:107
Global namespace for libopenraw.
Definition: arwfile.cpp:29