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