File: olympusdecompressor.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 (195 lines) | stat: -rw-r--r-- 6,287 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
/*
 * libopenraw - olympusdecompressor.cpp
 *
 * Copyright (C) 2011-2016 Hubert Figuiere
 * Olympus Decompression copied from RawSpeed
 * Copyright (C) 2009 Klaus Post
 *
 * 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 <stdlib.h>
#include <string.h>

#include <algorithm>

#include "rawdata.hpp"
#include "olympusdecompressor.hpp"
#include "bititerator.hpp"

namespace OpenRaw {
namespace Internals {

static void decompressOlympus(const uint8_t* buffer, size_t size, uint8_t* data,
                              uint32_t w, uint32_t h);

// decompression ported from RawSpeed.
static void decompressOlympus(const uint8_t* buffer, size_t size, uint8_t* data,
                              uint32_t w, uint32_t h)
{
    int nbits, sign, low, high, i, wo0, n, nw0, wo1, nw1;
    int acarry0[3], acarry1[3], pred, diff;

    int pitch = w * 2; //(((w * 2/*bpp*/) + 15) / 16) * 16; // TODO make that
                       //part of the outer datas

    /* Build a table to quickly look up "high" value */
    char bittable[4096];
    for (i = 0; i < 4096; i++) {
        int b = i;
        for (high = 0; high < 12; high++) {
            if ((b >> (11 - high)) & 1) {
                break;
            }
        }
        bittable[i] = high;
    }
    wo0 = nw0 = wo1 = nw1 = 0;
    buffer += 7;

    BitIterator bits(buffer, size - 7);

    for (uint32_t y = 0; y < h; y++) {
        memset(acarry0, 0, sizeof acarry0);
        memset(acarry1, 0, sizeof acarry1);
        uint16_t* dest = (uint16_t*)&data[y * pitch];
        for (uint32_t x = 0; x < w; x++) {
            //			bits.checkPos();
            //			bits.fill();
            i = 2 * (acarry0[2] < 3);
            for (nbits = 2 + i; (uint16_t)acarry0[0] >> (nbits + i); nbits++) {
            }

            uint32_t b = bits.peek(15);
            sign = (b >> 14) * -1;
            low = (b >> 12) & 3;
            high = bittable[b & 4095];
            // Skip bits used above.
            bits.skip(std::min(12 + 3, high + 1 + 3));

            if (high == 12) {
                high = bits.get(16 - nbits) >> 1;
            }

            acarry0[0] = (high << nbits) | bits.get(nbits);
            diff = (acarry0[0] ^ sign) + acarry0[1];
            acarry0[1] = (diff * 3 + acarry0[1]) >> 5;
            acarry0[2] = acarry0[0] > 16 ? 0 : acarry0[2] + 1;

            if (y < 2 || x < 2) {
                if (y < 2 && x < 2) {
                    pred = 0;
                } else if (y < 2) {
                    pred = wo0;
                } else {
                    pred = dest[-pitch + ((int)x)];
                    nw0 = pred;
                }
                dest[x] = pred + ((diff << 2) | low);
                // Set predictor
                wo0 = dest[x];
            } else {
                n = dest[-pitch + ((int)x)];
                if (((wo0 < nw0) & (nw0 < n)) | ((n < nw0) & (nw0 < wo0))) {
                    if (abs(wo0 - nw0) > 32 || abs(n - nw0) > 32) {
                        pred = wo0 + n - nw0;
                    } else {
                        pred = (wo0 + n) >> 1;
                    }
                } else {
                    pred = abs(wo0 - nw0) > abs(n - nw0) ? wo0 : n;
                }

                dest[x] = pred + ((diff << 2) | low);
                // Set predictors
                wo0 = dest[x];
                nw0 = n;
            }
            //      _ASSERTE(0 == dest[x] >> 12) ;

            // ODD PIXELS
            x += 1;
            //			bits.checkPos();
            //			bits.fill();
            i = 2 * (acarry1[2] < 3);
            for (nbits = 2 + i; (uint16_t)acarry1[0] >> (nbits + i); nbits++) {
            }
            b = bits.peek(15);
            sign = (b >> 14) * -1;
            low = (b >> 12) & 3;
            high = bittable[b & 4095];
            // Skip bits used above.
            bits.skip(std::min(12 + 3, high + 1 + 3));

            if (high == 12) {
                high = bits.get(16 - nbits) >> 1;
            }

            acarry1[0] = (high << nbits) | bits.get(nbits);
            diff = (acarry1[0] ^ sign) + acarry1[1];
            acarry1[1] = (diff * 3 + acarry1[1]) >> 5;
            acarry1[2] = acarry1[0] > 16 ? 0 : acarry1[2] + 1;

            if (y < 2 || x < 2) {
                if (y < 2 && x < 2) {
                    pred = 0;
                } else if (y < 2) {
                    pred = wo1;
                } else {
                    pred = dest[-pitch + ((int)x)];
                    nw1 = pred;
                }
                dest[x] = pred + ((diff << 2) | low);
                // Set predictor
                wo1 = dest[x];
            } else {
                n = dest[-pitch + ((int)x)];
                if (((wo1 < nw1) & (nw1 < n)) | ((n < nw1) & (nw1 < wo1))) {
                    if (abs(wo1 - nw1) > 32 || abs(n - nw1) > 32) {
                        pred = wo1 + n - nw1;
                    } else {
                        pred = (wo1 + n) >> 1;
                    }
                } else {
                    pred = abs(wo1 - nw1) > abs(n - nw1) ? wo1 : n;
                }

                dest[x] = pred + ((diff << 2) | low);

                // Set predictors
                wo1 = dest[x];
                nw1 = n;
            }
            //      _ASSERTE(0 == dest[x] >> 12) ;
        }
    }
}

RawDataPtr OlympusDecompressor::decompress()
{
    RawDataPtr output(new RawData);

    output->allocData(m_w * m_h * 2);
    decompressOlympus(m_buffer, m_size, (uint8_t*)output->data(), m_w, m_h);

    // hardcoded 12bits values
    output->setBpc(12);
    output->setWhiteLevel((1 << 12) - 1);

    return output;
}

}
}