File: cfapattern.cpp

package info (click to toggle)
libopenraw 0.1.2-0.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,484 kB
  • sloc: cpp: 13,907; sh: 4,090; ansic: 1,922; xml: 769; makefile: 676; perl: 42
file content (202 lines) | stat: -rw-r--r-- 4,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * libopenraw - cfapattern.cpp
 *
 * Copyright (C) 2012 Hubert Figuière
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <stddef.h>

#include <array>

#include <boost/static_assert.hpp>

#include <libopenraw/consts.h>

#include "cfapattern.hpp"

namespace OpenRaw {

namespace Internals {

/** alias the colours. */
static const uint8_t RED = OR_PATTERN_COLOUR_RED;
static const uint8_t GREEN = OR_PATTERN_COLOUR_GREEN;
static const uint8_t BLUE = OR_PATTERN_COLOUR_BLUE;

static const uint8_t RGGB_PATTERN[] = { RED, GREEN, GREEN, BLUE };
static const uint8_t GBRG_PATTERN[] = { GREEN, BLUE, RED, GREEN };
static const uint8_t BGGR_PATTERN[] = { BLUE, GREEN, GREEN, RED };
static const uint8_t GRBG_PATTERN[] = { GREEN, RED, BLUE, GREEN };

class Cfa2x2RgbPattern
  : public CfaPattern
{
public:
  Cfa2x2RgbPattern(::or_cfa_pattern pattern)
    : CfaPattern(pattern, 2, 2)
    {
      switch(pattern) {
      case OR_CFA_PATTERN_RGGB:
        setPatternPattern(RGGB_PATTERN, 4);
        break;
      case OR_CFA_PATTERN_GBRG:
        setPatternPattern(GBRG_PATTERN, 4);
        break;
      case OR_CFA_PATTERN_BGGR:
        setPatternPattern(BGGR_PATTERN, 4);
        break;
      case OR_CFA_PATTERN_GRBG:
        setPatternPattern(GRBG_PATTERN, 4);
        break;

      default:
        break;
      }
    }

};

}

const CfaPattern*
CfaPattern::twoByTwoPattern(::or_cfa_pattern pattern)
{
  static std::array<CfaPattern*, _OR_CFA_PATTERN_INVALID> s_patterns
    = { { NULL, NULL, NULL, NULL, NULL, NULL } };
  // this should be updated if we change the enum
  BOOST_STATIC_ASSERT(_OR_CFA_PATTERN_INVALID == 6);

  if((pattern == OR_CFA_PATTERN_NON_RGB22) ||
     (pattern >= _OR_CFA_PATTERN_INVALID)) {
    return NULL;
  }

  CfaPattern* pat = s_patterns[pattern];
  if(!pat) {
    pat = new Internals::Cfa2x2RgbPattern(pattern);
    s_patterns[pattern] = pat;
  }

  return pat;
}


class CfaPattern::Private
{
public:
  friend class Internals::Cfa2x2RgbPattern;

  Private()
    : x(0), y(0), n_colours(0)
    , pattern_type(OR_CFA_PATTERN_NONE)
    , pattern(NULL)
    {}

  uint16_t x;
  uint16_t y;
  uint16_t n_colours;
  ::or_cfa_pattern pattern_type;
  const uint8_t* pattern;
};

CfaPattern::CfaPattern()
  : d(new CfaPattern::Private)
{
}

CfaPattern::CfaPattern(::or_cfa_pattern pattern,
                       uint16_t width, uint16_t height)
  : d(new CfaPattern::Private)
{
  setSize(width, height);
  setPatternType(pattern);
}

CfaPattern::~CfaPattern()
{
  delete d;
}

void CfaPattern::setSize(uint16_t x, uint16_t y)
{
  d->x = x;
  d->y = y;
  if(x != 2 || y != 2) {
    d->pattern_type = OR_CFA_PATTERN_NON_RGB22;
  }
  else if(!is2by2Rgb()) {
    d->pattern_type = OR_CFA_PATTERN_NONE;
  }
}

bool CfaPattern::is2by2Rgb() const
{
  return (d->pattern_type != OR_CFA_PATTERN_NONE)
    && (d->pattern_type != OR_CFA_PATTERN_NON_RGB22);
}

void
CfaPattern::setPatternPattern(const uint8_t* pattern, uint16_t count)
{
  if(count != d->x * d->y) {
    d->pattern = NULL;
    // TODO deal with the error
    return;
  }
  d->pattern = pattern;
}

const uint8_t*
CfaPattern::patternPattern(uint16_t& count) const
{
  if(d->pattern) {
    count = d->x * d->y;
    return d->pattern;
  }
  
  count = 0;
  return NULL;
}

void CfaPattern::setPatternType(::or_cfa_pattern pattern)
{
  d->pattern_type = pattern;
  if(is2by2Rgb()) {
    setSize(2, 2);
    d->n_colours = 3;
  }
}

::or_cfa_pattern
CfaPattern::patternType() const
{
  return d->pattern_type;
}

}
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0))
  tab-width:2
  c-basic-offset:2
  indent-tabs-mode:nil
  fill-column:80
  End:
*/