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

どうでもいい記事100選

モザイク(ブロック)

基本的にはモザイク処理ですが、モザイク範囲の左上部分の明度を少し高くしてモザイク範囲の右下部分の明度を少し低くする事でブロック(縦横サイズを違うものにすればレンガ)調みたいな画像に加工できます。。。コードが長くてスミマセン。手抜きです。_| ̄|○

  private Bitmap effectPixelizationBlock( Bitmap bitmap, int blockFlg ){

    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[] pixels = new int[( width * height )];
    bitmap.getPixels( pixels, 0, width, 0, 0, width, height );

    int i, j, ii, jj;
    int sizeWidth  = 20;
    int sizeHeight = 10;
    int sizeMargin = 1;
    boolean flg    = false;

    if( blockFlg == 1 ){
      // 横にずらす
      for( i = 0; i < height; i += sizeHeight ){

        j = 0;

        if( flg == true ){
          j -= sizeWidth / 2;
          flg = false;
        } else {
          flg = true;
        }

        for( ; j < width; j += sizeWidth ){

          int r, rr, g, gg , b, bb, counts;
          r = rr = g = gg = b = bb = counts = 0;

          for( ii = i; ii < ( i + sizeHeight ); ++ii ){
            for( jj = j; jj < ( j + sizeWidth ); ++jj ){

              if( ii < 0 || height <= ii ||
                  jj < 0 || width  <= jj ){
                continue;
              }

              int bitmapColor = pixels[( jj + ii * width )];

              rr = Color.red( bitmapColor );
              gg = Color.green( bitmapColor );
              bb = Color.blue( bitmapColor );

              r += rr;
              g += gg;
              b += bb;

              counts++;
            }
          }

          rr = r / counts;
          gg = g / counts;
          bb = b / counts;

          // hsv[0] is Hue [0.0f .. 360.0f]
          // hsv[1] is Saturation [0.0f...1.0f]
          // hsv[2] is Value [0.0f...1.0.0f]
          float[] hsvHighlight = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvHighlight );
          hsvHighlight[2] += 0.15f;

          if( 1 < hsvHighlight[2] ){
            hsvHighlight[2] = 1.0f;
          }

          float[] hsvShadow = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvShadow );
          hsvShadow[2] -= 0.15f;

          if( hsvShadow[2] < 0 ){
            hsvShadow[2] = 0.0f;
          }

          int colorHighlight = Color.HSVToColor( hsvHighlight );
          int colorShadow    = Color.HSVToColor( hsvShadow );

          for( ii = i; ii < ( i + sizeHeight ); ++ii ){
            for( jj = j; jj < ( j + sizeWidth ); ++jj ){

              if( ii < 0 || height <= ii ||
                  jj < 0 || width  <= jj ){
                continue;
              }

              int iii = ii - i;
              int jjj = jj - j;

              if( ( iii < ( sizeHeight - sizeMargin ) && jjj < sizeMargin ) ||
                  ( iii < sizeMargin                  && jjj < ( sizeWidth - sizeMargin ) ) ){
                pixels[( jj + ii * width )] = colorHighlight;
              } else if( ( ( sizeHeight - sizeMargin ) <= iii && jjj < sizeWidth ) ||
                         ( 0 <= iii                           && ( sizeWidth - sizeMargin ) <= jjj ) ){
                pixels[( jj + ii * width )] = colorShadow;
              } else {
                pixels[( jj + ii * width )] = Color.rgb( rr, gg, bb );
              }
            }
          }
        }
      }    
    } else if( blockFlg == 2 ){
      // 縦にずらす
      for( i = 0; i < width; i += sizeWidth ){

        j = 0;

        if( flg == true ){
          j -= sizeHeight / 2;
          flg = false;
        } else {
          flg = true;
        }

        for( ; j < height; j += sizeHeight ){

          int r, rr, g, gg , b, bb, counts;
          r = rr = g = gg = b = bb = counts = 0;

          for( ii = i; ii < ( i + sizeWidth ); ++ii ){
            for( jj = j; jj < ( j + sizeHeight ); ++jj ){

              if( ii < 0 || width  <= ii ||
                  jj < 0 || height <= jj ){
                continue;
              }

              int bitmapColor = pixels[( ii + jj * width )];

              rr = Color.red( bitmapColor );
              gg = Color.green( bitmapColor );
              bb = Color.blue( bitmapColor );

              r += rr;
              g += gg;
              b += bb;

              counts++;
            }
          }

          rr = r / counts;
          gg = g / counts;
          bb = b / counts;

          // hsv[0] is Hue [0.0f .. 360.0f]
          // hsv[1] is Saturation [0.0f...1.0f]
          // hsv[2] is Value [0.0f...1.0.0f]
          float[] hsvHighlight = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvHighlight );
          hsvHighlight[2] += 0.15f;

          if( 1 < hsvHighlight[2] ){
            hsvHighlight[2] = 1.0f;
          }

          float[] hsvShadow = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvShadow );
          hsvShadow[2] -= 0.15f;

          if( hsvShadow[2] < 0 ){
            hsvShadow[2] = 0.0f;
          }

          int colorHighlight = Color.HSVToColor( hsvHighlight );
          int colorShadow    = Color.HSVToColor( hsvShadow );

          for( ii = i; ii < ( i + sizeWidth ); ++ii ){
            for( jj = j; jj < ( j + sizeHeight ); ++jj ){

              if( ii < 0 || width  <= ii ||
                  jj < 0 || height <= jj ){
                continue;
              }

              int iii = ii - i;
              int jjj = jj - j;

              if( ( iii < ( sizeWidth - sizeMargin ) && jjj < sizeMargin ) ||
                  ( iii < sizeMargin                 && jjj < ( sizeHeight - sizeMargin ) ) ){
                pixels[( ii + jj * width )] = colorHighlight;
              } else if( ( ( sizeWidth - sizeMargin ) <= iii && jjj < sizeHeight ) ||
                         ( 0 <= iii                          && ( sizeHeight - sizeMargin ) <= jjj ) ){
                pixels[( ii + jj * width )] = colorShadow;
              } else {
                pixels[( ii + jj * width )] = Color.rgb( rr, gg, bb );
              }
            }
          }
        }
      }
    } else {
      // ずらさない
      for( i = 0; i < width; i += sizeWidth ){
        for( j = 0; j < height; j += sizeHeight ){

          int r, rr, g, gg , b, bb, counts;
          r = rr = g = gg = b = bb = counts = 0;

          for( ii = i; ii < ( i + sizeWidth ); ++ii ){
            for( jj = j; jj < ( j + sizeHeight ); ++jj ){

              if( ii < 0 || width  <= ii ||
                  jj < 0 || height <= jj ){
                continue;
              }

              int bitmapColor = pixels[( ii + jj * width )];

              rr = Color.red( bitmapColor );
              gg = Color.green( bitmapColor );
              bb = Color.blue( bitmapColor );

              r += rr;
              g += gg;
              b += bb;

              counts++;
            }
          }

          rr = r / counts;
          gg = g / counts;
          bb = b / counts;

          // hsv[0] is Hue [0.0f .. 360.0f]
          // hsv[1] is Saturation [0.0f...1.0f]
          // hsv[2] is Value [0.0f...1.0.0f]
          float[] hsvHighlight = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvHighlight );
          hsvHighlight[2] += 0.15f;

          if( 1 < hsvHighlight[2] ){
            hsvHighlight[2] = 1.0f;
          }

          float[] hsvShadow = new float[3];
          Color.RGBToHSV( rr, gg, bb, hsvShadow );
          hsvShadow[2] -= 0.15f;

          if( hsvShadow[2] < 0 ){
            hsvShadow[2] = 0.0f;
          }

          int colorHighlight = Color.HSVToColor( hsvHighlight );
          int colorShadow    = Color.HSVToColor( hsvShadow );

          for( ii = i; ii < ( i + sizeWidth ); ++ii ){
            for( jj = j; jj < ( j + sizeHeight ); ++jj ){

              if( ii < 0 || width  <= ii ||
                  jj < 0 || height <= jj ){
                continue;
              }

              int iii = ii - i;
              int jjj = jj - j;

              if( ( iii < ( sizeWidth - sizeMargin ) && jjj < sizeMargin ) ||
                  ( iii < sizeMargin                 && jjj < ( sizeHeight - sizeMargin ) ) ){
                pixels[( ii + jj * width )] = colorHighlight;
              } else if( ( ( sizeWidth - sizeMargin ) <= iii && jjj < sizeHeight ) ||
                         ( 0 <= iii                          && ( sizeHeight - sizeMargin ) <= jjj ) ){
                pixels[( ii + jj * width )] = colorShadow;
              } else {
                pixels[( ii + jj * width )] = Color.rgb( rr, gg, bb );
              }
            }
          }
        }
      }
    }
  
    bitmap.setPixels( pixels, 0, width, 0, 0, width, height );

    return bitmap;
  }

オリジナル:
f:id:masugata:20110405125239:image:medium
モザイク(ブロック・縦ずれ)(違いは詳細で見てください):
f:id:masugata:20110407204647:image:medium
モザイク(ブロック・横ずれ)(違いは詳細で見てください):
f:id:masugata:20110407204634:image:medium
モザイク(ブロック・ずれ無)(違いは詳細で見てください):
f:id:masugata:20110407204701:image:medium