histogram.rs revision 89e35a551ee4b0bfcb3ede10da3b027dba1da7ef
1/*
2 * Copyright (C) 2013 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 "ip.rsh"
18
19rs_allocation gSrc;
20rs_allocation gDest;
21rs_allocation gSums;
22rs_allocation gSum;
23
24int gWidth;
25int gHeight;
26int gStep;
27int gSteps;
28
29void RS_KERNEL pass1(int in, uint x, uint y) {
30    for (int i=0; i < (256); i++) {
31        rsSetElementAt_int(gSums, 0, i, y);
32    }
33
34    for (int i = 0; i < gStep; i++) {
35        int py = y*gStep + i;
36        if (py >= gHeight) return;
37
38        for (int px=0; px < gWidth; px++) {
39            uchar4 c = rsGetElementAt_uchar4(gSrc, px, py);
40            int lum = (77 * c.r + 150 * c.g + 29 * c.b) >> 8;
41
42            int old = rsGetElementAt_int(gSums, lum, y);
43            rsSetElementAt_int(gSums, old+1, lum, y);
44        }
45    }
46}
47
48int RS_KERNEL pass2(uint x) {
49    int sum = 0;
50    for (int i=0; i < gSteps; i++) {
51        sum += rsGetElementAt_int(gSums, x, i);
52    }
53    return sum;
54}
55
56void rescale() {
57    int maxv = 0;
58
59    for (int i=0; i < 256; i++) {
60        maxv = max(maxv, rsGetElementAt_int(gSum, i));
61    }
62    float overMax = (1.f / maxv) * gHeight;
63
64    for (int i=0; i < 256; i++) {
65        int t = rsGetElementAt_int(gSum, i);
66        t = gHeight - (overMax * rsGetElementAt_int(gSum, i));
67        t = max(0, t);
68        rsSetElementAt_int(gSum, t, i);
69    }
70}
71
72static const uchar4 gClear = {0, 0, 0, 0xff};
73
74uchar4 RS_KERNEL clear() {
75    return gClear;
76}
77
78uchar4 RS_KERNEL draw(uint x, uint y) {
79    int l = rsGetElementAt_int(gSum, x >> 2);
80    if (y > l) {
81        return 0xff;
82    }
83    return gClear;
84}
85