Skip to content

Create a function to round image into an ellipse #2456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.facebook.imagepipeline.filter;

import android.graphics.Bitmap;
import com.facebook.common.internal.Preconditions;
import com.facebook.imageutils.BitmapUtil;

/**
* Filter for rounding to ellipse.
*/
public final class InPlaceEllipseRoundFilter {

private InPlaceEllipseRoundFilter() {}

/**
* An implementation for rounding a given bitmap to an ellipse shape.
*
* @param bitmap The input {@link Bitmap}
*/
public static void roundEllipseBitmapInPlace(Bitmap bitmap) {
Preconditions.checkNotNull(bitmap);
final int w = bitmap.getWidth();
final int h = bitmap.getHeight();

final int[] transparentColor = new int[w];

final int[] pixels = new int[w*h];
bitmap.getPixels(pixels, 0, w, 0, 0, w, h);

for (int y = -1*h/2; y < h/2; y++) {
int x = -1*w/2;
while ( ((float) 4*x*x)/(w*w) + ((float) 4*y*y)/(h*h) > 1 ) {
x++;
}

int pixelsToHide = Math.max(0, x + w/2 - 1);

System.arraycopy(transparentColor, 0, pixels, (y + h/2)*w, pixelsToHide);

System.arraycopy(transparentColor, 0, pixels, (y + h/2)*w + w - pixelsToHide, pixelsToHide);
}

bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.imagepipeline.postprocessors;

import android.graphics.Bitmap;
import com.facebook.cache.common.CacheKey;
import com.facebook.cache.common.SimpleCacheKey;
import com.facebook.imagepipeline.filter.InPlaceEllipseRoundFilter;
import com.facebook.imagepipeline.request.BasePostprocessor;
import javax.annotation.Nullable;

/** Postprocessor that rounds a given image as a ellipse using non-native code. */
public class EllipsePostprocessor extends BasePostprocessor {

private @Nullable CacheKey mCacheKey;

public EllipsePostprocessor() {;}

@Override
public void process(Bitmap bitmap) {
InPlaceEllipseRoundFilter.roundEllipseBitmapInPlace(bitmap);
}

@Nullable
@Override
public CacheKey getPostprocessorCacheKey() {
if (mCacheKey == null) {
mCacheKey = new SimpleCacheKey("InPlaceEllipseRoundFilter");
}
return mCacheKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.facebook.fresco.samples.showcase.postprocessor.SlowGreyScalePostprocessor;
import com.facebook.fresco.samples.showcase.postprocessor.WatermarkPostprocessor;
import com.facebook.imagepipeline.postprocessors.BlurPostProcessor;
import com.facebook.imagepipeline.postprocessors.EllipsePostprocessor;
import com.facebook.imagepipeline.postprocessors.IterativeBoxBlurPostProcessor;
import com.facebook.imagepipeline.postprocessors.RoundAsCirclePostprocessor;
import com.facebook.imagepipeline.postprocessors.RoundPostprocessor;
Expand Down Expand Up @@ -212,6 +213,10 @@ this, new RoundAsCirclePostprocessor(true))),
R.string.imagepipeline_postprocessor_set_rounded_corners,
new BenchmarkPostprocessorForDuplicatedBitmapInPlace(
this, new RoundedCornersPostprocessor())),
new Entry(
R.string.imagepipeline_postprocessor_set_ellipse,
new BenchmarkPostprocessorForDuplicatedBitmapInPlace(
this, new EllipsePostprocessor())),
new Entry(
R.string.imagepipeline_postprocessor_set_round_as_circle,
new BenchmarkPostprocessorForDuplicatedBitmap(this, new RoundPostprocessor())));
Expand Down
1 change: 1 addition & 0 deletions samples/showcase/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<string name="imagepipeline_postprocessor_set_round_as_circle">Round As Circle Post-Processor</string>
<string name="imagepipeline_postprocessor_set_round_as_aa_circle">Antialiased Circle Post-Processor</string>
<string name="imagepipeline_postprocessor_set_rounded_corners">Rounded Corners Post-Processor</string>
<string name="imagepipeline_postprocessor_set_ellipse">Ellipse Post-Processor</string>
<string name="imagepipeline_postprocessor_refresh">Refresh</string>

<string name="imagepipeline_prefetch_title">Prefetch</string>
Expand Down