libopenraw
rawcontainer.cpp
1 /* -*- Mode: C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*- */
2 /*
3  * libopenraw - rawcontainer.cpp
4  *
5  * Copyright (C) 2006-2017 Hubert Figuière
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 <fcntl.h>
23 #include <memory>
24 
25 #include <libopenraw/debug.h>
26 
27 #include "trace.hpp"
28 #include "endianutils.hpp"
29 #include "rawcontainer.hpp"
30 
31 using namespace Debug;
32 
33 namespace OpenRaw {
34 namespace Internals {
35 
36 
37 RawContainer::RawContainer(const IO::Stream::Ptr &_file, off_t _offset)
38  : m_file(_file),
39  m_offset(_offset),
40  m_endian(ENDIAN_NULL)
41 {
42  m_file->open();
43  m_file->seek(_offset, SEEK_SET);
44 }
45 
46 
48 {
49  m_file->close();
50 }
51 
52 bool RawContainer::skip(off_t offset)
53 {
54  m_file->seek(offset, SEEK_CUR);
55  return true;
56 }
57 
59 RawContainer::readInt8(const IO::Stream::Ptr &f)
60 {
61  unsigned char buf;
62  int s = f->read(&buf, 1);
63  if (s != 1) {
64  return Option<int8_t>();
65  }
66  return Option<int8_t>(buf);
67 }
68 
70 RawContainer::readUInt8(const IO::Stream::Ptr &f)
71 {
72  unsigned char buf;
73  int s = f->read(&buf, 1);
74  if (s != 1) {
75  return Option<uint8_t>();
76  }
77  return Option<uint8_t>(buf);
78 }
79 
81 RawContainer::readInt16(const IO::Stream::Ptr &f)
82 {
83  if (m_endian == ENDIAN_NULL) {
84 
85  LOGERR("null endian\n");
86 
87  return Option<int16_t>();
88  }
89  unsigned char buf[2];
90  int s = f->read(buf, 2);
91  if (s != 2) {
92  return Option<int16_t>();
93  }
94  if (m_endian == ENDIAN_LITTLE) {
95  return Option<int16_t>(EL16(buf));
96  } else {
97  return Option<int16_t>(BE16(buf));
98  }
99 }
100 
101 
105 size_t
106 RawContainer::readUInt16Array(const IO::Stream::Ptr &f, std::vector<uint16_t> & v, size_t count)
107 {
108  if (m_endian == ENDIAN_NULL) {
109  LOGERR("null endian\n");
110  return 0;
111  }
112 
113  if (v.size() < count) {
114  v.resize(count, 0);
115  }
116  uint8_t buf[2];
117  size_t num_read = 0;
118  for (size_t i = 0; i < count; i++) {
119  int s = f->read(buf, 2);
120  uint16_t val;
121  if (s != 2) {
122  return num_read;
123  }
124  if (m_endian == ENDIAN_LITTLE) {
125  val = EL16(buf);
126  } else {
127  val = BE16(buf);
128  }
129  v[i] = val;
130  num_read++;
131  }
132 
133  return num_read;
134 }
135 
136 
138 RawContainer::readInt32(const IO::Stream::Ptr &f)
139 {
140  if (m_endian == ENDIAN_NULL) {
141  LOGERR("null endian\n");
142  return Option<int32_t>();
143  }
144  unsigned char buf[4];
145  int s = f->read(buf, 4);
146  if (s != 4) {
147  LOGERR("read %d bytes\n", s);
148  return Option<int32_t>();
149  }
150 
151  if (m_endian == ENDIAN_LITTLE) {
152  return Option<int32_t>(EL32(buf));
153  } else {
154  return Option<int32_t>(BE32(buf));
155  }
156 }
157 
158 
160 RawContainer::readUInt16(const IO::Stream::Ptr &f)
161 {
162  if (m_endian == ENDIAN_NULL) {
163 
164  LOGERR("null endian\n");
165 
166  return Option<uint16_t>();
167  }
168  unsigned char buf[2];
169  int s = f->read(buf, 2);
170  if (s != 2) {
171  return Option<uint16_t>();
172  }
173  if (m_endian == ENDIAN_LITTLE) {
174  return Option<uint16_t>(EL16(buf));
175  } else {
176  return Option<uint16_t>(BE16(buf));
177  }
178 }
179 
180 
182 RawContainer::readUInt32(const IO::Stream::Ptr &f)
183 {
184  if (m_endian == ENDIAN_NULL) {
185  LOGERR("null endian\n");
186 
187  return Option<uint32_t>();
188  }
189  unsigned char buf[4];
190  int s = f->read(buf, 4);
191  if (s != 4) {
192  return Option<uint32_t>();
193  }
194 
195  if (m_endian == ENDIAN_LITTLE) {
196  return Option<uint32_t>(EL32(buf));
197  } else {
198  return Option<uint32_t>(BE32(buf));
199  }
200 }
201 
202 size_t
203 RawContainer::fetchData(void *buf, off_t _offset,
204  size_t buf_size)
205 {
206  size_t s = 0;
207  m_file->seek(_offset, SEEK_SET);
208  s = m_file->read(buf, buf_size);
209  return s;
210 }
211 
212 off_t
214 {
215  return m_file->filesize();
216 }
217 
218 }
219 }
220 /*
221  Local Variables:
222  mode:c++
223  c-file-style:"stroustrup"
224  c-file-offsets:((innamespace . 0))
225  tab-width:2
226  c-basic-offset:2
227  indent-tabs-mode:nil
228  fill-column:80
229  End:
230 */
size_t fetchData(void *buf, off_t offset, size_t buf_size)
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
Option< int16_t > readInt16(const IO::Stream::Ptr &f)
Option< uint32_t > readUInt32(const IO::Stream::Ptr &f)
Definition: trace.cpp:30
size_t readUInt16Array(const IO::Stream::Ptr &f, std::vector< uint16_t > &v, size_t count)
Option< uint16_t > readUInt16(const IO::Stream::Ptr &f)
Option< int32_t > readInt32(const IO::Stream::Ptr &f)