libopenraw
rawdata.cpp
1 /*
2  * libopenraw - rawdata.cpp
3  *
4  * Copyright (C) 2007 Hubert Figuiere
5  * Copyright (C) 2008 Novell, Inc.
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <assert.h>
23 
24 #include <libopenraw++/rawdata.h>
25 #include <libopenraw++/rawfile.h>
26 
27 namespace OpenRaw {
28 
30 public:
31  RawData *self;
32  uint16_t min, max;
33  CfaPattern cfa_pattern;
34  uint32_t compression;
35  uint8_t *pos;
36  size_t offset;
37  size_t row_offset;
38  uint8_t slice;
39  uint32_t sliceWidth;
40  uint32_t sliceOffset;
42  std::vector<uint16_t> slices;
44  Private(RawData *_self)
45  : self(_self),
46  min(0), max(0),
47  cfa_pattern(OR_CFA_PATTERN_NONE),
48  compression(0),
49  pos(NULL), offset(0),
50  row_offset(0),
51  slice(0), sliceWidth(0),
52  sliceOffset(0), slices()
53  {
54  }
55  void advance(size_t s);
56  void nextSlice();
57  void nextRow();
58 private:
59  Private(const Private &);
60  Private & operator=(const Private &);
61 };
62 
63 
64 RawData *
65 RawData::getAndExtractRawData(const char* filename, uint32_t options,
66  or_error & err)
67 {
68  err = OR_ERROR_NONE;
69  RawData *rawdata = NULL;
70 
71  RawFile *file = RawFile::newRawFile(filename);
72  if (file) {
73  rawdata = new RawData();
74  err = file->getRawData(*rawdata, options);
75  delete file;
76  }
77  else {
78  err = OR_ERROR_CANT_OPEN; // file error
79  }
80  return rawdata;
81 }
82 
83 
84 RawData::RawData()
85  : BitmapData(),
86  d(new RawData::Private(this))
87 {
88 
89 }
90 
91 
92 RawData::~RawData()
93 {
94  delete d;
95 }
96 
97 uint16_t RawData::min()
98 {
99  return d->min;
100 }
101 
102 uint16_t RawData::max()
103 {
104  return d->max;
105 }
106 
107 void RawData::setMin(uint16_t m)
108 {
109  d->min = m;
110 }
111 
112 void RawData::setMax(uint16_t m)
113 {
114  d->max = m;
115 }
116 
117 void RawData::swap(RawData & with)
118 {
119  BitmapData::swap(with);
120  std::swap(this->d, with.d);
121 }
122 
123 void * RawData::allocData(const size_t s)
124 {
125  void * p = BitmapData::allocData(s);
126  d->pos = (uint8_t*)p;
127  d->offset = 0;
128  return p;
129 }
130 
131 
132 void RawData::setDimensions(uint32_t _x, uint32_t _y)
133 {
135  if(d->slices.size()) {
136  d->sliceWidth = d->slices[0];
137  }
138  else {
139  d->sliceWidth = _x;
140  }
141 }
142 
143 void RawData::setSlices(const std::vector<uint16_t> & slices)
144 {
145  d->slices = slices;
146  if(slices.size()) {
147  d->sliceWidth = slices[0];
148  }
149  else {
150  d->sliceWidth = x();
151  }
152 }
153 
154 void RawData::setCfaPattern(or_cfa_pattern t)
155 {
156  d->cfa_pattern = t;
157 }
158 
159 or_cfa_pattern RawData::cfaPattern()
160 {
161  return d->cfa_pattern;
162 }
163 
164 void RawData::setCompression(uint32_t t)
165 {
166  d->compression = t;
167 }
168 
169 uint32_t RawData::compression()
170 {
171  return d->compression;
172 }
173 
174 #if 0
175 RawData &RawData::append(uint8_t c)
176 {
177  assert(d->pos);
178  assert(d->offset < d->data_size);
179  *(d->pos) = c;
180  advance(sizeof(c));
181  return *this;
182 }
183 #endif
184 
186 {
187  assert(d->pos);
188  assert(d->offset < size());
189  *(d->pos) = c & 0xff;
190  *(d->pos + 1) = (c >> 8) & 0xff;
191  d->advance(sizeof(c));
192  return *this;
193 }
194 
195 
197 {
198  d->nextRow();
199 }
200 
201 
202 void RawData::Private::nextRow()
203 {
204  uint32_t w = self->x() * 2;
205  uint32_t row = offset / w;
206  row++;
207  if(row == self->y())
208  {
209  // on the last
210  nextSlice();
211  row = 0;
212  }
213  offset = row * w + sliceOffset * 2;
214  pos = (uint8_t*)(self->data()) + offset;
215  row_offset = offset;
216 }
217 
218 void RawData::Private::nextSlice()
219 {
220  if(slices.size()) {
221  sliceOffset += slices[slice];
222  slice++;
223  if(slices.size() > slice) {
224  sliceWidth = slices[slice];
225  }
226  else {
227  sliceWidth = 0;
228  }
229  }
230 }
231 
232 void RawData::Private::advance(size_t s)
233 {
234  if(offset + s - row_offset >= sliceWidth * 2) {
235  nextRow();
236  }
237  else {
238  pos += s;
239  offset += s;
240  }
241 }
242 
243 }
244 /*
245  Local Variables:
246  mode:c++
247  c-file-style:"stroustrup"
248  c-file-offsets:((innamespace . 0))
249  indent-tabs-mode:nil
250  fill-column:80
251  End:
252 */
253 
Private(RawData *_self)
Definition: rawdata.cpp:44
virtual void setDimensions(uint32_t x, uint32_t y)
Definition: rawdata.cpp:132
::or_error getRawData(RawData &rawdata, uint32_t options)
Definition: rawfile.cpp:372
RawData & append(uint16_t c)
Definition: rawdata.cpp:185
void swap(RawData &with)
Definition: rawdata.cpp:117
virtual void setDimensions(uint32_t x, uint32_t y)
Definition: bitmapdata.cpp:148
void swap(BitmapData &with)
Definition: bitmapdata.cpp:82
size_t size() const
Definition: bitmapdata.cpp:122
static RawFile * newRawFile(const char *_filename, Type _typeHint=OR_RAWFILE_TYPE_UNKNOWN)
Definition: rawfile.cpp:135