aboutsummaryrefslogtreecommitdiff
path: root/renderscript-toolkit/src/main/cpp/Lut.cpp
blob: f064d290a6ad2d8c1d18769e23a6a5205d5a19ac (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
/*
 * Copyright (C) 2012 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 <cstdint>

#include "RenderScriptToolkit.h"
#include "TaskProcessor.h"
#include "Utils.h"

#define LOG_TAG "renderscript.toolkit.Lut"

namespace renderscript {

class LutTask : public Task {
    const uchar4* mIn;
    uchar4* mOut;
    const uchar* mRedTable;
    const uchar* mGreenTable;
    const uchar* mBlueTable;
    const uchar* mAlphaTable;

    // Process a 2D tile of the overall work. threadIndex identifies which thread does the work.
    void processData(int threadIndex, size_t startX, size_t startY, size_t endX,
                     size_t endY) override;

   public:
    LutTask(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY, const uint8_t* red,
            const uint8_t* green, const uint8_t* blue, const uint8_t* alpha,
            const Restriction* restriction)
        : Task{sizeX, sizeY, 4, true, restriction},
          mIn{reinterpret_cast<const uchar4*>(input)},
          mOut{reinterpret_cast<uchar4*>(output)},
          mRedTable{red},
          mGreenTable{green},
          mBlueTable{blue},
          mAlphaTable{alpha} {}
};

void LutTask::processData(int /* threadIndex */, size_t startX, size_t startY, size_t endX,
                          size_t endY) {
    for (size_t y = startY; y < endY; y++) {
        size_t offset = mSizeX * y + startX;
        const uchar4* in = mIn + offset;
        uchar4* out = mOut + offset;
        for (size_t x = startX; x < endX; x++) {
            auto v = *in;
            *out = uchar4{mRedTable[v.x], mGreenTable[v.y], mBlueTable[v.z], mAlphaTable[v.w]};
            in++;
            out++;
        }
    }
}

void RenderScriptToolkit::lut(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY,
                              const uint8_t* red, const uint8_t* green, const uint8_t* blue,
                              const uint8_t* alpha, const Restriction* restriction) {
#ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
    if (!validRestriction(LOG_TAG, sizeX, sizeY, restriction)) {
        return;
    }
#endif

    LutTask task(input, output, sizeX, sizeY, red, green, blue, alpha, restriction);
    processor->doTask(&task);
}

}  // namespace renderscript