1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 
19 #include "RenderScriptToolkit.h"
20 #include "TaskProcessor.h"
21 #include "Utils.h"
22 
23 #define LOG_TAG "renderscript.toolkit.Lut"
24 
25 namespace renderscript {
26 
27 class LutTask : public Task {
28     const uchar4* mIn;
29     uchar4* mOut;
30     const uchar* mRedTable;
31     const uchar* mGreenTable;
32     const uchar* mBlueTable;
33     const uchar* mAlphaTable;
34 
35     // Process a 2D tile of the overall work. threadIndex identifies which thread does the work.
36     void processData(int threadIndex, size_t startX, size_t startY, size_t endX,
37                      size_t endY) override;
38 
39    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)40     LutTask(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY, const uint8_t* red,
41             const uint8_t* green, const uint8_t* blue, const uint8_t* alpha,
42             const Restriction* restriction)
43         : Task{sizeX, sizeY, 4, true, restriction},
44           mIn{reinterpret_cast<const uchar4*>(input)},
45           mOut{reinterpret_cast<uchar4*>(output)},
46           mRedTable{red},
47           mGreenTable{green},
48           mBlueTable{blue},
49           mAlphaTable{alpha} {}
50 };
51 
processData(int,size_t startX,size_t startY,size_t endX,size_t endY)52 void LutTask::processData(int /* threadIndex */, size_t startX, size_t startY, size_t endX,
53                           size_t endY) {
54     for (size_t y = startY; y < endY; y++) {
55         size_t offset = mSizeX * y + startX;
56         const uchar4* in = mIn + offset;
57         uchar4* out = mOut + offset;
58         for (size_t x = startX; x < endX; x++) {
59             auto v = *in;
60             *out = uchar4{mRedTable[v.x], mGreenTable[v.y], mBlueTable[v.z], mAlphaTable[v.w]};
61             in++;
62             out++;
63         }
64     }
65 }
66 
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)67 void RenderScriptToolkit::lut(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY,
68                               const uint8_t* red, const uint8_t* green, const uint8_t* blue,
69                               const uint8_t* alpha, const Restriction* restriction) {
70 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
71     if (!validRestriction(LOG_TAG, sizeX, sizeY, restriction)) {
72         return;
73     }
74 #endif
75 
76     LutTask task(input, output, sizeX, sizeY, red, green, blue, alpha, restriction);
77     processor->doTask(&task);
78 }
79 
80 }  // namespace renderscript
81