Home | History | Annotate | Download | only in image
      1 // Copyright (C) 2011 The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include "ip.rsh"
     16 
     17 uint32_t gMaxIteration = 500;
     18 uint32_t gDimX = 1024;
     19 uint32_t gDimY = 1024;
     20 
     21 float lowerBoundX = -2.f;
     22 float lowerBoundY = -2.f;
     23 float scaleFactor = 4.f;
     24 
     25 uchar4 RS_KERNEL root(uint32_t x, uint32_t y) {
     26   float2 p;
     27   p.x = lowerBoundX + ((float)x / gDimX) * scaleFactor;
     28   p.y = lowerBoundY + ((float)y / gDimY) * scaleFactor;
     29 
     30   float2 t = 0;
     31   float2 t2 = t * t;
     32   int iter = 0;
     33   while((t2.x + t2.y < 4.f) && (iter < gMaxIteration)) {
     34     float xtemp = t2.x - t2.y + p.x;
     35     t.y = 2 * t.x * t.y + p.y;
     36     t.x = xtemp;
     37     iter++;
     38     t2 = t * t;
     39   }
     40 
     41   if(iter >= gMaxIteration) {
     42     // write a non-transparent black pixel
     43     return (uchar4){0, 0, 0, 0xff};
     44   } else {
     45     float mi3 = gMaxIteration / 3.f;
     46     if (iter <= (gMaxIteration / 3))
     47       return (uchar4){0xff * (iter / mi3), 0, 0, 0xff};
     48     else if (iter <= (((gMaxIteration / 3) * 2)))
     49       return (uchar4){0xff - (0xff * ((iter - mi3) / mi3)),
     50                       (0xff * ((iter - mi3) / mi3)), 0, 0xff};
     51     else
     52       return (uchar4){0, 0xff - (0xff * ((iter - (mi3 * 2)) / mi3)),
     53                       (0xff * ((iter - (mi3 * 2)) / mi3)), 0xff};
     54   }
     55 }
     56 
     57 uchar4 RS_KERNEL rootD(uint32_t x, uint32_t y) {
     58   double2 p;
     59   p.x = lowerBoundX + ((float)x / gDimX) * scaleFactor;
     60   p.y = lowerBoundY + ((float)y / gDimY) * scaleFactor;
     61 
     62   double2 t = 0;
     63   double2 t2 = t * t;
     64   int iter = 0;
     65   while((t2.x + t2.y < 4.f) && (iter < gMaxIteration)) {
     66     double xtemp = t2.x - t2.y + p.x;
     67     t.y = 2 * t.x * t.y + p.y;
     68     t.x = xtemp;
     69     iter++;
     70     t2 = t * t;
     71   }
     72 
     73   if(iter >= gMaxIteration) {
     74     // write a non-transparent black pixel
     75     return (uchar4){0, 0, 0, 0xff};
     76   } else {
     77     double mi3 = gMaxIteration / 3.f;
     78     if (iter <= (gMaxIteration / 3))
     79       return (uchar4){0xff * (iter / mi3), 0, 0, 0xff};
     80     else if (iter <= (((gMaxIteration / 3) * 2)))
     81       return (uchar4){0xff - (0xff * ((iter - mi3) / mi3)),
     82                       (0xff * ((iter - mi3) / mi3)), 0, 0xff};
     83     else
     84       return (uchar4){0, 0xff - (0xff * ((iter - (mi3 * 2)) / mi3)),
     85                       (0xff * ((iter - (mi3 * 2)) / mi3)), 0xff};
     86   }
     87 }
     88 
     89