Home | History | Annotate | Download | only in image
      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 
     19 rs_allocation gSrc;
     20 rs_allocation gDest;
     21 rs_allocation gSums;
     22 rs_allocation gSum;
     23 
     24 int gWidth;
     25 int gHeight;
     26 int gStep;
     27 int gSteps;
     28 
     29 void RS_KERNEL pass1(uint x) {
     30     for (int i=0; i < (256); i++) {
     31         rsSetElementAt_int(gSums, 0, i, x);
     32     }
     33 
     34     for (int i = 0; i < gStep; i++) {
     35         int py = x * 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, x);
     43             rsSetElementAt_int(gSums, old+1, lum, x);
     44         }
     45     }
     46 }
     47 
     48 int 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 
     56 void 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 
     72 static const uchar4 gClear = {0, 0, 0, 0xff};
     73 
     74 uchar4 RS_KERNEL clear() {
     75     return gClear;
     76 }
     77 
     78 uchar4 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