ます’s Blog - どうでもいい記事100選

どうでもいい記事100選

魚眼レンズ風

まさか自分でもできるとは思わなかった魚眼レンズちっくに画像を加工。

  private Bitmap effectFisheye( Bitmap bitmap ){

    if( bitmap == null ){
      bitmap = BitmapFactory.decodeResource( getResources( ), R.drawable.original ).copy( Bitmap.Config.ARGB_8888, true );
    }

    if( bitmap == null ){
      return bitmap;
    }

    if( bitmap.isMutable( ) != true ){
      bitmap = bitmap.copy( Bitmap.Config.ARGB_8888, true );
    }

    int height   = bitmap.getHeight( );
    int width    = bitmap.getWidth( );
    int[] pixelsSrc  = new int[( width * height )];
    int[] pixelsDest = new int[( width * height )];
    bitmap.getPixels( pixelsSrc, 0, width, 0, 0, width, height );

    int indexMax = ( width * ( height -1 ) + ( width - 1 ) );
    int weight   = 40;
    double radius;

    if( height < width ){
      radius = width / 2;
    } else {
      radius = height / 2;
    }

    for( int YY = 0; YY < height; ++YY ){
      for( int XX = 0; XX < width; ++XX ){

        double rp = Math.sqrt( (double)( Math.pow( (double)( weight ),          2 ) +
                                         Math.pow( (double)( XX - width  / 2 ), 2 ) +
                                         Math.pow( (double)( YY - height / 2 ), 2 ) ) );

       int XXX = (int)( ( rp * ( XX - width  / 2 ) ) / radius + width  / 2 );
       int YYY = (int)( ( rp * ( YY - height / 2 ) ) / radius + height / 2 );

       int srcIndex1  = ( width * YYY + XXX );
       int srcIndex2  = srcIndex1 + 1;
       int srcIndex3  = srcIndex1 + 2;

       int destIndex1 = ( width * YY + XX );
       int destIndex2 = destIndex1 + 1;
       int destIndex3 = destIndex1 + 2;

       if( 0 <= XXX && XXX < width && 0 <= YYY && YYY < height ){
         
         if( srcIndex1 <= indexMax && destIndex1 <= indexMax ){
           pixelsDest[destIndex1] = pixelsSrc[srcIndex1];
         } else if( destIndex1 <= indexMax ){
           pixelsDest[destIndex1] = Color.rgb( 0, 0, 0 );
         }
         
         if( srcIndex2 <= indexMax && destIndex2 <= indexMax ){
           pixelsDest[destIndex2] = pixelsSrc[srcIndex2];
         } else if( destIndex2 <= indexMax ){
           pixelsDest[destIndex2] = Color.rgb( 0, 0, 0 );
         }

         if( srcIndex3 <= indexMax && destIndex3 <= indexMax ){
           pixelsDest[destIndex2] = pixelsSrc[srcIndex2];
         } else if( destIndex3 <= indexMax ){
           pixelsDest[destIndex3] = Color.rgb( 0, 0, 0 );
         }
       } else if( destIndex1 <= indexMax ){
         pixelsDest[destIndex1] = Color.rgb( 0, 0, 0 );
       }
      }
    }

    bitmap.setPixels( pixelsDest, 0, width, 0, 0, width, height );

    return bitmap;
  }

オリジナル:
f:id:masugata:20110405125239:image:medium
魚眼レンズ風:
f:id:masugata:20110412162040:image:medium


変換式があるのですが、数学ができない自分にとっては理解不能。。。_| ̄|○