summaryrefslogtreecommitdiffstats
path: root/media/img_utils/src/DngUtils.cpp
blob: b21340381b9f3359f5c83a12980f06d2396d55c4 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <img_utils/DngUtils.h>

#include <inttypes.h>

#include <math.h>

namespace android {
namespace img_utils {

OpcodeListBuilder::OpcodeListBuilder() : mCount(0), mOpList(), mEndianOut(&mOpList, BIG) {
    if(mEndianOut.open() != OK) {
        ALOGE("%s: Open failed.", __FUNCTION__);
    }
}

OpcodeListBuilder::~OpcodeListBuilder() {
    if(mEndianOut.close() != OK) {
        ALOGE("%s: Close failed.", __FUNCTION__);
    }
}

size_t OpcodeListBuilder::getSize() const {
    return mOpList.getSize() + sizeof(mCount);
}

uint32_t OpcodeListBuilder::getCount() const {
    return mCount;
}

status_t OpcodeListBuilder::buildOpList(uint8_t* buf) const {
    uint32_t count = convertToBigEndian(mCount);
    memcpy(buf, &count, sizeof(count));
    memcpy(buf + sizeof(count), mOpList.getArray(), mOpList.getSize());
    return OK;
}

status_t OpcodeListBuilder::addGainMapsForMetadata(uint32_t lsmWidth,
                                                   uint32_t lsmHeight,
                                                   uint32_t activeAreaTop,
                                                   uint32_t activeAreaLeft,
                                                   uint32_t activeAreaBottom,
                                                   uint32_t activeAreaRight,
                                                   CfaLayout cfa,
                                                   const float* lensShadingMap) {
    uint32_t activeAreaWidth = activeAreaRight - activeAreaLeft;
    uint32_t activeAreaHeight = activeAreaBottom - activeAreaTop;
    double spacingV = 1.0 / lsmHeight;
    double spacingH = 1.0 / lsmWidth;

    float redMap[lsmWidth * lsmHeight];
    float greenEvenMap[lsmWidth * lsmHeight];
    float greenOddMap[lsmWidth * lsmHeight];
    float blueMap[lsmWidth * lsmHeight];

    size_t lsmMapSize = lsmWidth * lsmHeight * 4;

    // Split lens shading map channels into separate arrays
    size_t j = 0;
    for (size_t i = 0; i < lsmMapSize; i += 4, ++j) {
        redMap[j] = lensShadingMap[i + LSM_R_IND];
        greenEvenMap[j] = lensShadingMap[i + LSM_GE_IND];
        greenOddMap[j] = lensShadingMap[i + LSM_GO_IND];
        blueMap[j] = lensShadingMap[i + LSM_B_IND];
    }

    uint32_t redTop = 0;
    uint32_t redLeft = 0;
    uint32_t greenEvenTop = 0;
    uint32_t greenEvenLeft = 1;
    uint32_t greenOddTop = 1;
    uint32_t greenOddLeft = 0;
    uint32_t blueTop = 1;
    uint32_t blueLeft = 1;

    switch(cfa) {
        case CFA_RGGB:
            redTop = 0;
            redLeft = 0;
            greenEvenTop = 0;
            greenEvenLeft = 1;
            greenOddTop = 1;
            greenOddLeft = 0;
            blueTop = 1;
            blueLeft = 1;
            break;
        case CFA_GRBG:
            redTop = 0;
            redLeft = 1;
            greenEvenTop = 0;
            greenEvenLeft = 0;
            greenOddTop = 1;
            greenOddLeft = 1;
            blueTop = 1;
            blueLeft = 0;
            break;
        case CFA_GBRG:
            redTop = 1;
            redLeft = 0;
            greenEvenTop = 0;
            greenEvenLeft = 0;
            greenOddTop = 1;
            greenOddLeft = 1;
            blueTop = 0;
            blueLeft = 1;
            break;
        case CFA_BGGR:
            redTop = 1;
            redLeft = 1;
            greenEvenTop = 0;
            greenEvenLeft = 1;
            greenOddTop = 1;
            greenOddLeft = 0;
            blueTop = 0;
            blueLeft = 0;
            break;
        default:
            ALOGE("%s: Unknown CFA layout %d", __FUNCTION__, cfa);
            return BAD_VALUE;
    }

    status_t err = addGainMap(/*top*/redTop,
                              /*left*/redLeft,
                              /*bottom*/activeAreaHeight - 1,
                              /*right*/activeAreaWidth - 1,
                              /*plane*/0,
                              /*planes*/1,
                              /*rowPitch*/2,
                              /*colPitch*/2,
                              /*mapPointsV*/lsmHeight,
                              /*mapPointsH*/lsmWidth,
                              /*mapSpacingV*/spacingV,
                              /*mapSpacingH*/spacingH,
                              /*mapOriginV*/0,
                              /*mapOriginH*/0,
                              /*mapPlanes*/1,
                              /*mapGains*/redMap);
    if (err != OK) return err;

    err = addGainMap(/*top*/greenEvenTop,
                     /*left*/greenEvenLeft,
                     /*bottom*/activeAreaHeight - 1,
                     /*right*/activeAreaWidth - 1,
                     /*plane*/0,
                     /*planes*/1,
                     /*rowPitch*/2,
                     /*colPitch*/2,
                     /*mapPointsV*/lsmHeight,
                     /*mapPointsH*/lsmWidth,
                     /*mapSpacingV*/spacingV,
                     /*mapSpacingH*/spacingH,
                     /*mapOriginV*/0,
                     /*mapOriginH*/0,
                     /*mapPlanes*/1,
                     /*mapGains*/greenEvenMap);
    if (err != OK) return err;

    err = addGainMap(/*top*/greenOddTop,
                     /*left*/greenOddLeft,
                     /*bottom*/activeAreaHeight - 1,
                     /*right*/activeAreaWidth - 1,
                     /*plane*/0,
                     /*planes*/1,
                     /*rowPitch*/2,
                     /*colPitch*/2,
                     /*mapPointsV*/lsmHeight,
                     /*mapPointsH*/lsmWidth,
                     /*mapSpacingV*/spacingV,
                     /*mapSpacingH*/spacingH,
                     /*mapOriginV*/0,
                     /*mapOriginH*/0,
                     /*mapPlanes*/1,
                     /*mapGains*/greenOddMap);
    if (err != OK) return err;

    err = addGainMap(/*top*/blueTop,
                     /*left*/blueLeft,
                     /*bottom*/activeAreaHeight - 1,
                     /*right*/activeAreaWidth - 1,
                     /*plane*/0,
                     /*planes*/1,
                     /*rowPitch*/2,
                     /*colPitch*/2,
                     /*mapPointsV*/lsmHeight,
                     /*mapPointsH*/lsmWidth,
                     /*mapSpacingV*/spacingV,
                     /*mapSpacingH*/spacingH,
                     /*mapOriginV*/0,
                     /*mapOriginH*/0,
                     /*mapPlanes*/1,
                     /*mapGains*/blueMap);
    return err;
}

status_t OpcodeListBuilder::addGainMap(uint32_t top,
                                       uint32_t left,
                                       uint32_t bottom,
                                       uint32_t right,
                                       uint32_t plane,
                                       uint32_t planes,
                                       uint32_t rowPitch,
                                       uint32_t colPitch,
                                       uint32_t mapPointsV,
                                       uint32_t mapPointsH,
                                       double mapSpacingV,
                                       double mapSpacingH,
                                       double mapOriginV,
                                       double mapOriginH,
                                       uint32_t mapPlanes,
                                       const float* mapGains) {

    uint32_t opcodeId = GAIN_MAP_ID;

    status_t err = mEndianOut.write(&opcodeId, 0, 1);
    if (err != OK) return err;

    uint8_t version[] = {1, 3, 0, 0};
    err = mEndianOut.write(version, 0, NELEMS(version));
    if (err != OK) return err;

    // Allow this opcode to be skipped if not supported
    uint32_t flags = FLAG_OPTIONAL;

    err = mEndianOut.write(&flags, 0, 1);
    if (err != OK) return err;

    const uint32_t NUMBER_INT_ARGS = 11;
    const uint32_t NUMBER_DOUBLE_ARGS = 4;

    uint32_t totalSize = NUMBER_INT_ARGS * sizeof(uint32_t) + NUMBER_DOUBLE_ARGS * sizeof(double) +
            mapPointsV * mapPointsH * mapPlanes * sizeof(float);

    err = mEndianOut.write(&totalSize, 0, 1);
    if (err != OK) return err;

    // Batch writes as much as possible
    uint32_t settings1[] = { top,
                            left,
                            bottom,
                            right,
                            plane,
                            planes,
                            rowPitch,
                            colPitch,
                            mapPointsV,
                            mapPointsH };

    err = mEndianOut.write(settings1, 0, NELEMS(settings1));
    if (err != OK) return err;

    double settings2[] = { mapSpacingV,
                          mapSpacingH,
                          mapOriginV,
                          mapOriginH };

    err = mEndianOut.write(settings2, 0, NELEMS(settings2));
    if (err != OK) return err;

    err = mEndianOut.write(&mapPlanes, 0, 1);
    if (err != OK) return err;

    err = mEndianOut.write(mapGains, 0, mapPointsV * mapPointsH * mapPlanes);
    if (err != OK) return err;

    mCount++;

    return OK;
}

status_t OpcodeListBuilder::addWarpRectilinearForMetadata(const float* kCoeffs,
                                                          uint32_t activeArrayWidth,
                                                          uint32_t activeArrayHeight,
                                                          float opticalCenterX,
                                                          float opticalCenterY) {
    if (activeArrayWidth <= 1 || activeArrayHeight <= 1) {
        ALOGE("%s: Cannot add opcode for active array with dimensions w=%" PRIu32 ", h=%" PRIu32,
                __FUNCTION__, activeArrayWidth, activeArrayHeight);
        return BAD_VALUE;
    }

    double normalizedOCX = opticalCenterX / static_cast<double>(activeArrayWidth - 1);
    double normalizedOCY = opticalCenterY / static_cast<double>(activeArrayHeight - 1);

    normalizedOCX = CLAMP(normalizedOCX, 0, 1);
    normalizedOCY = CLAMP(normalizedOCY, 0, 1);

    // Conversion factors from Camera2 K factors to DNG spec. K factors:
    //
    //      Note: these are necessary because our unit system assumes a
    //      normalized max radius of sqrt(2), whereas the DNG spec's
    //      WarpRectilinear opcode assumes a normalized max radius of 1.
    //      Thus, each K coefficient must include the domain scaling
    //      factor (the DNG domain is scaled by sqrt(2) to emulate the
    //      domain used by the Camera2 specification).

    const double c_0 = sqrt(2);
    const double c_1 = 2 * sqrt(2);
    const double c_2 = 4 * sqrt(2);
    const double c_3 = 8 * sqrt(2);
    const double c_4 = 2;
    const double c_5 = 2;

    const double coeffs[] = { c_0 * kCoeffs[0],
                              c_1 * kCoeffs[1],
                              c_2 * kCoeffs[2],
                              c_3 * kCoeffs[3],
                              c_4 * kCoeffs[4],
                              c_5 * kCoeffs[5] };


    return addWarpRectilinear(/*numPlanes*/1,
                              /*opticalCenterX*/normalizedOCX,
                              /*opticalCenterY*/normalizedOCY,
                              coeffs);
}

status_t OpcodeListBuilder::addWarpRectilinear(uint32_t numPlanes,
                                               double opticalCenterX,
                                               double opticalCenterY,
                                               const double* kCoeffs) {

    uint32_t opcodeId = WARP_RECTILINEAR_ID;

    status_t err = mEndianOut.write(&opcodeId, 0, 1);
    if (err != OK) return err;

    uint8_t version[] = {1, 3, 0, 0};
    err = mEndianOut.write(version, 0, NELEMS(version));
    if (err != OK) return err;

    // Allow this opcode to be skipped if not supported
    uint32_t flags = FLAG_OPTIONAL;

    err = mEndianOut.write(&flags, 0, 1);
    if (err != OK) return err;

    const uint32_t NUMBER_CENTER_ARGS = 2;
    const uint32_t NUMBER_COEFFS = numPlanes * 6;
    uint32_t totalSize = (NUMBER_CENTER_ARGS + NUMBER_COEFFS) * sizeof(double) + sizeof(uint32_t);

    err = mEndianOut.write(&totalSize, 0, 1);
    if (err != OK) return err;

    err = mEndianOut.write(&numPlanes, 0, 1);
    if (err != OK) return err;

    err = mEndianOut.write(kCoeffs, 0, NUMBER_COEFFS);
    if (err != OK) return err;

    err = mEndianOut.write(&opticalCenterX, 0, 1);
    if (err != OK) return err;

    err = mEndianOut.write(&opticalCenterY, 0, 1);
    if (err != OK) return err;

    mCount++;

    return OK;
}

} /*namespace img_utils*/
} /*namespace android*/