2
#include "ArwDecoder.h"
4
RawSpeed - RAW file decoder.
6
Copyright (C) 2009 Klaus Post
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Lesser General Public
10
License as published by the Free Software Foundation; either
11
version 2 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
http://www.klauspost.com
27
ArwDecoder::ArwDecoder(TiffIFD *rootIFD, FileMap* file) :
28
RawDecoder(file), mRootIFD(rootIFD) {
32
ArwDecoder::~ArwDecoder(void) {
38
RawImage ArwDecoder::decodeRawInternal() {
39
vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(STRIPOFFSETS);
42
ThrowRDE("ARW Decoder: No image data found");
44
TiffIFD* raw = data[0];
45
int compression = raw->getEntry(COMPRESSION)->getInt();
46
if (32767 != compression)
47
ThrowRDE("ARW Decoder: Unsupported compression");
49
TiffEntry *offsets = raw->getEntry(STRIPOFFSETS);
50
TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS);
52
if (offsets->count != 1) {
53
ThrowRDE("ARW Decoder: Multiple Strips found: %u", offsets->count);
55
if (counts->count != offsets->count) {
56
ThrowRDE("ARW Decoder: Byte count number does not match strip size: count:%u, strips:%u ", counts->count, offsets->count);
58
uint32 width = raw->getEntry(IMAGEWIDTH)->getInt();
59
uint32 height = raw->getEntry(IMAGELENGTH)->getInt();
60
uint32 bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getInt();
62
// Sony E-550 marks compressed 8bpp ARW with 12 bit per pixel
63
// this makes the compression detect it as a ARW v1.
64
// This camera has however another MAKER entry, so we MAY be able
65
// to detect it this way in the future.
66
data = mRootIFD->getIFDsWithTag(MAKE);
67
string make = data[0]->getEntry(MAKE)->getString();
68
if (!make.compare("SONY"))
71
bool arw1 = counts->getInt() * 8 != width * height * bitPerPixel;
75
mRaw->dim = iPoint2D(width, height);
78
const ushort16* c = raw->getEntry(SONY_CURVE)->getShortArray();
79
uint32 sony_curve[] = { 0, 0, 0, 0, 0, 4095 };
81
for (uint32 i = 0; i < 4; i++)
82
sony_curve[i+1] = (c[i] >> 2) & 0xfff;
84
for (uint32 i = 0; i < 0x4001; i++)
87
for (uint32 i = 0; i < 5; i++)
88
for (uint32 j = sony_curve[i] + 1; j <= sony_curve[i+1]; j++)
89
curve[j] = curve[j-1] + (1 << i);
91
uint32 c2 = counts->getInt();
92
uint32 off = offsets->getInt();
94
if (!mFile->isValid(off))
95
ThrowRDE("Sony ARW decoder: Data offset after EOF, file probably truncated");
97
if (!mFile->isValid(off + c2))
98
c2 = mFile->getSize() - off;
101
ByteStream input(mFile->getData(off), c2);
105
DecodeARW(input, width, height);
107
DecodeARW2(input, width, height, bitPerPixel);
108
} catch (IOException &e) {
109
mRaw->setError(e.what());
110
// Let's ignore it, it may have delivered somewhat useful data.
116
void ArwDecoder::DecodeARW(ByteStream &input, uint32 w, uint32 h) {
117
BitPumpMSB bits(&input);
118
uchar8* data = mRaw->getData();
119
ushort16* dest = (ushort16*) & data[0];
120
uint32 pitch = mRaw->pitch / sizeof(ushort16);
122
for (uint32 x = w; x--;)
123
for (uint32 y = 0; y < h + 1; y += 2) {
127
uint32 len = 4 - bits.getBitsNoFill(2);
128
if (len == 3 && bits.getBitNoFill()) len = 0;
130
while (len < 17 && !bits.getBitNoFill()) len++;
131
int diff = bits.getBits(len);
132
if ((diff & (1 << (len - 1))) == 0)
133
diff -= (1 << len) - 1;
135
_ASSERTE(!(sum >> 12));
136
if (y < h) dest[x+y*pitch] = sum;
140
void ArwDecoder::DecodeARW2(ByteStream &input, uint32 w, uint32 h, uint32 bpp) {
144
this->startThreads();
149
uchar8* data = mRaw->getData();
150
uint32 pitch = mRaw->pitch;
151
const uchar8 *in = input.getData();
153
if (input.getRemainSize() < (w * 3 / 2))
154
ThrowRDE("Sony Decoder: Image data section too small, file probably truncated");
156
if (input.getRemainSize() < (w*h*3 / 2))
157
h = input.getRemainSize() / (w * 3 / 2) - 1;
159
for (uint32 y = 0; y < h; y++) {
160
ushort16* dest = (ushort16*) & data[y*pitch];
161
for (uint32 x = 0 ; x < w; x += 2) {
164
// Shift up to match compressed precision
165
dest[x] = (g1 | ((g2 & 0xf) << 8)) << 2;
167
dest[x+1] = ((g2 >> 4) | (g3 << 4)) << 2;
172
ThrowRDE("Unsupported bit depth");
175
void ArwDecoder::checkSupportInternal(CameraMetaData *meta) {
176
vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(MODEL);
178
ThrowRDE("ARW Support check: Model name found");
179
string make = data[0]->getEntry(MAKE)->getString();
180
string model = data[0]->getEntry(MODEL)->getString();
181
this->checkCameraSupported(meta, make, model, "");
184
void ArwDecoder::decodeMetaDataInternal(CameraMetaData *meta) {
188
mRaw->cfa.setCFA(CFA_RED, CFA_GREEN, CFA_GREEN2, CFA_BLUE);
189
vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(MODEL);
192
ThrowRDE("ARW Meta Decoder: Model name found");
193
if (!data[0]->hasEntry(MAKE))
194
ThrowRDE("ARW Decoder: Make name not found");
196
string make = data[0]->getEntry(MAKE)->getString();
197
string model = data[0]->getEntry(MODEL)->getString();
199
if (mRootIFD->hasEntryRecursive(ISOSPEEDRATINGS))
200
iso = mRootIFD->getEntryRecursive(ISOSPEEDRATINGS)->getInt();
202
setMetaData(meta, make, model, "", iso);
205
/* Since ARW2 compressed images have predictable offsets, we decode them threaded */
207
void ArwDecoder::decodeThreaded(RawDecoderThread * t) {
208
uchar8* data = mRaw->getData();
209
uint32 pitch = mRaw->pitch;
210
uint32 w = mRaw->dim.x;
212
BitPumpPlain bits(in);
213
for (uint32 y = t->start_y; y < t->end_y; y++) {
214
ushort16* dest = (ushort16*) & data[y*pitch];
216
bits.setAbsoluteOffset((w*8*y) >> 3);
218
// Process 32 pixels (16x2) per loop.
219
for (uint32 x = 0; x < w - 30;) {
221
int _max = bits.getBits(11);
222
int _min = bits.getBits(11);
223
int _imax = bits.getBits(4);
224
int _imin = bits.getBits(4);
226
for (sh = 0; sh < 4 && 0x80 << sh <= _max - _min; sh++);
227
for (int i = 0; i < 16; i++) {
229
if (i == _imax) p = _max;
230
else if (i == _imin) p = _min;
232
p = (bits.getBits(7) << sh) + _min;
236
dest[x+i*2] = curve[p << 1];
238
x += x & 1 ? 31 : 1; // Skip to next 32 pixels
243
} // namespace RawSpeed