Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2012 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 "filters.h"
     18 #include <math.h>
     19 
     20 #ifdef __cplusplus
     21 extern "C" {
     22 #endif
     23 
     24 
     25 #define PI_F 3.141592653589f
     26 
     27 class ImageRGBA {
     28  public:
     29   ImageRGBA(unsigned char* image, int width, int height)
     30    : image_(image), width_(width), height_(height) {
     31     width_step_ = width * 4;
     32   }
     33 
     34   int Width() const {
     35     return width_;
     36   }
     37 
     38   int Height() const {
     39     return height_;
     40   }
     41 
     42   // Pixel accessor.
     43   unsigned char* operator()(int x, int y) {
     44     return image_ + y * width_step_ + x * 4;
     45   }
     46   const unsigned char* operator()(int x, int y) const {
     47     return image_ + y * width_step_ + x * 4;
     48   }
     49 
     50  private:
     51   unsigned char* image_;
     52   int width_;
     53   int height_;
     54   int width_step_;
     55 };
     56 
     57 // Interpolate a pixel in a 3 channel image.
     58 inline void InterpolatePixel(const ImageRGBA &image, float x, float y,
     59                              unsigned char* dest) {
     60   // Get pointers and scale factors for the source pixels.
     61   float ax = x - floor(x);
     62   float ay = y - floor(y);
     63   float axn = 1.0f - ax;
     64   float ayn = 1.0f - ay;
     65   const unsigned char *p = image(x, y);
     66   const unsigned char *p2 = image(x, y + 1);
     67 
     68   // Interpolate each image color plane.
     69   dest[0] = static_cast<unsigned char>(axn * ayn * p[0] + ax * ayn * p[4] +
     70              ax * ay * p2[4] + axn * ay * p2[0] + 0.5f);
     71   p++;
     72   p2++;
     73 
     74   dest[1] = static_cast<unsigned char>(axn * ayn * p[0] + ax * ayn * p[4] +
     75              ax * ay * p2[4] + axn * ay * p2[0] + 0.5f);
     76   p++;
     77   p2++;
     78 
     79   dest[2] = static_cast<unsigned char>(axn * ayn * p[0] + ax * ayn * p[4] +
     80              ax * ay * p2[4] + axn * ay * p2[0] + 0.5f);
     81   p++;
     82   p2++;
     83   dest[3] = 0xFF;
     84 }
     85 
     86 // Wrap circular coordinates around the globe
     87 inline float wrap(float value, float dimension) {
     88   return value - (dimension * floor(value/dimension));
     89 }
     90 
     91 void StereographicProjection(float scale, float angle, unsigned char* input_image,
     92                              int input_width, int input_height,
     93                              unsigned char* output_image, int output_width,
     94                              int output_height) {
     95   ImageRGBA input(input_image, input_width, input_height);
     96   ImageRGBA output(output_image, output_width, output_height);
     97 
     98   const float image_scale = output_width * scale;
     99 
    100   for (int x = 0; x < output_width; x++) {
    101     // Center and scale x
    102     float xf = (x - output_width / 2.0f) / image_scale;
    103 
    104     for (int y = 0; y < output_height; y++) {
    105       // Center and scale y
    106       float yf = (y - output_height / 2.0f) / image_scale;
    107 
    108       // Convert to polar
    109       float r = hypotf(xf, yf);
    110       float theta = angle+atan2(yf, xf);
    111       if (theta>PI_F) theta-=2*PI_F;
    112 
    113       // Project onto plane
    114       float phi = 2 * atan(1 / r);
    115       // (theta stays the same)
    116 
    117       // Map to panorama image
    118       float px = (theta / (2 * PI_F)) * input_width;
    119       float py = (phi / PI_F) * input_height;
    120 
    121       // Wrap around the globe
    122       px = wrap(px, input_width);
    123       py = wrap(py, input_height);
    124 
    125       // Write the interpolated pixel
    126       InterpolatePixel(input, px, py, output(x, y));
    127     }
    128   }
    129 }
    130 
    131 
    132 void JNIFUNCF(ImageFilterTinyPlanet, nativeApplyFilter, jobject bitmap_in, jint width, jint height, jobject bitmap_out, jint output_size, jfloat scale,jfloat angle)
    133 {
    134     char* source = 0;
    135     char* destination = 0;
    136     AndroidBitmap_lockPixels(env, bitmap_in, (void**) &source);
    137     AndroidBitmap_lockPixels(env, bitmap_out, (void**) &destination);
    138     unsigned char * rgb_in = (unsigned char * )source;
    139     unsigned char * rgb_out = (unsigned char * )destination;
    140 
    141     StereographicProjection(scale,angle, rgb_in, width, height, rgb_out, output_size, output_size);
    142     AndroidBitmap_unlockPixels(env, bitmap_in);
    143     AndroidBitmap_unlockPixels(env, bitmap_out);
    144 }
    145 
    146 #ifdef __cplusplus
    147 }
    148 #endif
    149 
    150 
    151