Skip to content
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

Implemented CENTER_INSIDE support #727

Merged
merged 1 commit into from
Nov 24, 2015
Merged

Conversation

josemontiel
Copy link
Contributor

Fixes #591

final int verticalPadding = (height - inBitmap.getHeight()) / 2;

Bitmap.Config config = getSafeConfig(inBitmap);
Bitmap toReuse = pool.get(width, height, config);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pool may return null (at least in 3.0 it did)

@TWiStErRob TWiStErRob added this to the 4.0 milestone Nov 5, 2015
@TWiStErRob
Copy link
Collaborator

I have one bigger concern (other than my small line comments):
Applying .centerInside() to a load may produce an unnecessarily large Bitmap. Imagine one's loading a 16x16px icon into a 512x512px space. This means that Glide's centerInside will create a 512px Bitmap, but there will be 240px padding inside where there's nothing. This is a waste of memory considering the ImageView would handle this for us if it's ScaleType is set to centerInside. So I'm inclined to say that the automatic transformation for ImageViews should be a little different: return the original Bitmap if it's smaller than the requested size, and fitCenter if it's bigger. But, the padded version is still viable in situations like toBytes() or asBitmap() with custom targets because there it is better to have a Bitmap that has the requested size than implementing centerInside somehow every single time. The best solution would be to parameterize all centerInside calls with a boolean padSmaller argument, so the user can/has to decide which logic to use.

final int horizontalPadding = (width - inBitmap.getWidth()) / 2;
final int verticalPadding = (height - inBitmap.getHeight()) / 2;

Bitmap.Config config = getSafeConfig(inBitmap);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to upgrade to 8888 if there's a padding, otherwise we'll end up with a color that someone won't like (BLACK/WHITE are both wrong choices), it should be TRANSPARENT.

@josemontiel
Copy link
Contributor Author

Thanks for the feedback @TWiStErRob. Right on it!

@sjudd
Copy link
Collaborator

sjudd commented Nov 5, 2015

I think you can disregard padding entirely, it's hard for me to imagine a scenario where you want extra transparency baked into the Bitmap.

I'd just check if the width and height are greater than the target, and if so, fitCenter, and otherwise just return the original Bitmap.

Transformations never promise that the Bitmap they return will exactly match the requested width and height.

Thanks for putting this up!

@TWiStErRob
Copy link
Collaborator

@sjudd what if the requested transformation doesn't match scaleType?

ImageView.scaleType = fitCenter
.centerInside() on load

I would still expect a small image to be centered in a middle of a bigger space.

@sjudd
Copy link
Collaborator

sjudd commented Nov 5, 2015

Oh I wouldn't, I'd expect the Bitmap to remain at the smallest possible size (to minimize memory usage), but for the Bitmap to be stretched by the view when it's drawn to match the requested scale type.

None of our other transformations try to override a conflicting scale type set on a view, I don't think we need to do so here either.

@TWiStErRob
Copy link
Collaborator

@sjudd ah, you're right; the ImageView wins; @josemontiel sorry about that.

@josemontiel
Copy link
Contributor Author

@sjudd so just to be sure, it should check if the Bitmap's dimensions are larger than the target and call fitCenter, otherwise return the input as is?

@sjudd
Copy link
Collaborator

sjudd commented Nov 5, 2015

@josemontiel I believe so? I think that matches what ImageView does?

@TWiStErRob
Copy link
Collaborator

What do you both think about the Downsampler issue? NONE will prevent huge images from loading with centerInside.

@josemontiel
Copy link
Contributor Author

That definitely would be a problem, perhaps the NONE strategy could be overridden in the Downsampler when the source's dimensions are larger than the target's?

@TWiStErRob
Copy link
Collaborator

Hmm, there's already a DownsamplerStrategy.CENTER_INSIDE but it doesn't match what we need, let's see what @sjudd says about this, he knows that part better. I think that current CENTER_INSIDE strategy should be called FIT_INSIDE.

@josemontiel
Copy link
Contributor Author

Hmm interesting @TWiStErRob . It could be something like this:

private static class RealCenterInside extends DownsampleStrategy {
    @Override
    public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {

      if (sourceWidth > requestedWidth || sourceHeight > requestedHeight) {
        float widthPercentage = requestedWidth / (float) sourceWidth;
        float heightPercentage = requestedHeight / (float) sourceHeight;
        return Math.min(widthPercentage, heightPercentage);
      } else {
        return 1f;
      }
    }

    @Override
    public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight,
        int requestedWidth, int requestedHeight) {
      return SampleSizeRounding.QUALITY;
    }
}

@sjudd
Copy link
Collaborator

sjudd commented Nov 7, 2015

Yeah I think you're going to need a new downsample strategy that's equivalent to CENTER_INSIDE but that doesn't upscale.

The names here are pretty poor, sorry for that. Is there some standard naming convention for these transformations? Alternative suggestions welcome. I'm not totally sure that FIT_INSIDE is much clearer than CENTER_INSIDE, though I agree CENTER_INSIDE is poor. If we rename CENTER_INSIDE to FIT_INSIDE, what do we call the equivalent strategy that doesn't downsample?

@josemontiel That looks right, but I think you could simplify by just returning Math.min(1f, CENTER_INSIDE.getScaleFactor(...))? We want the same transformation, except that we don't want to upscale?

@TWiStErRob
Copy link
Collaborator

@sjudd I'm not aware of any standards here, I created #730 with my suggestions/questions.

@josemontiel I think you should just go with your "Real" naming and we'll rename it by fixing #730
or call it DOWNSCALE_ONLY as I proposed.

@josemontiel
Copy link
Contributor Author

@TWiStErRob I'll go with DownscaleOnly, makes sense.

@TWiStErRob
Copy link
Collaborator

gradlew :library:checkstyle failed because of long lines in DownsampleStrategy, use Format code in IDEA to wrap automatically. Can you please fix that and squash your commits into 1?

Implemented CENTER_INSIDE support

Implemented CENTER_INSIDE support

CENTER_INSIDE implemented

CENTER_INSIDE implemented: DownscaleOnly Downsample strategy

CENTER_INSIDE: Formatting fixed
@TWiStErRob
Copy link
Collaborator

@sjudd this looks ready.

@sjudd
Copy link
Collaborator

sjudd commented Nov 11, 2015

LGTM, I'll merge internally as soon as I can and push out.

I'll probably rename DOWNSCALE_ONLY per our brief discussion on your doc @TWiStErRob since this isn't the only strategy that doesn't upscale.

@sjudd
Copy link
Collaborator

sjudd commented Nov 24, 2015

Thanks!

sjudd added a commit that referenced this pull request Nov 24, 2015
Implemented CENTER_INSIDE support
@sjudd sjudd merged commit acc3fe3 into bumptech:master Nov 24, 2015
sjudd added a commit to sjudd/glide that referenced this pull request Dec 2, 2015
*** Reason for rollback ***

Broke Street View

*** Original change description ***

MOE automated commit.

Merge pull request bumptech#762 from vanniktech/master_clean_upload_script

Clean upload script up

-------------
Merge pull request bumptech#727 from josemontiel/master

Implemented CENTER_INSIDE support

-------------
Update Downsampler names.

-------------
Merge pull request bumptech#758 from mullender/glide-issue-738

fix for issue bumptech#738 Some images simply do not download (Black image) -…

-------------
Created by MOE: https://github.com/google/moe

***
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=108724201
sjudd added a commit to sjudd/glide that referenced this pull request Dec 2, 2015
*** Reason for rollback ***

Rollforward with compatibility for apps targeting API 21

*** Original change description ***

Automated g4 rollback of changelist 108709885.

*** Reason for rollback ***

Broke Street View

*** Original change description ***

MOE automated commit.

Merge pull request bumptech#762 from vanniktech/master_clean_upload_script

Clean upload script up

-------------
Merge pull request bumptech#727 from josemontiel/master

Implemented CENTER_INSIDE support

-------------
Update Downsampler names.

-------------
Merge pull request bumptech#758 from mullender/glide-issue-738

fix for issue bumptech#738 Some images simply do no...

***
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=108748541
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants