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#pragma version(1)
16#pragma rs java_package_name(com.android.rs.image)
17
18uint32_t gMaxIteration = 500;
19uint32_t gDimX = 1024;
20uint32_t gDimY = 1024;
21
22float lowerBoundX = -2.f;
23float lowerBoundY = -2.f;
24float scaleFactor = 4.f;
25
26uchar4 __attribute__((kernel)) root(uint32_t x, uint32_t y) {
27  float2 p;
28  p.x = lowerBoundX + ((float)x / gDimX) * scaleFactor;
29  p.y = lowerBoundY + ((float)y / gDimY) * scaleFactor;
30
31  float2 t = 0;
32  float2 t2 = t * t;
33  int iter = 0;
34  while((t2.x + t2.y < 4.f) && (iter < gMaxIteration)) {
35    float xtemp = t2.x - t2.y + p.x;
36    t.y = 2 * t.x * t.y + p.y;
37    t.x = xtemp;
38    iter++;
39    t2 = t * t;
40  }
41
42  if(iter >= gMaxIteration) {
43    // write a non-transparent black pixel
44    return (uchar4){0, 0, 0, 0xff};
45  } else {
46    float mi3 = gMaxIteration / 3.f;
47    if (iter <= (gMaxIteration / 3))
48      return (uchar4){0xff * (iter / mi3), 0, 0, 0xff};
49    else if (iter <= (((gMaxIteration / 3) * 2)))
50      return (uchar4){0xff - (0xff * ((iter - mi3) / mi3)),
51                      (0xff * ((iter - mi3) / mi3)), 0, 0xff};
52    else
53      return (uchar4){0, 0xff - (0xff * ((iter - (mi3 * 2)) / mi3)),
54                      (0xff * ((iter - (mi3 * 2)) / mi3)), 0xff};
55  }
56}
57