Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 64481195 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Rename ImageDecoder.setResize

Bug: 76448408
Bug: 73537624
Test: Ib40d65c68a6c709b6456f2145ad8a5557a941494

setResize is two verbs, and "resize" implies we're changing the size of
an existing object. In truth, the method specifies the desired size. So
rename setResize(int, int) to setTargetSize, which clearly specifies the
behavior.

Rename setResize(int sampleSize) to setSampleSize.

Hide getSampledSize, which looks too similar to the newly named
setSampleSize. In addition, b/76448408 suggests hiding it. It isn't
really necessary anyway, since a client can just call setSampleSize - no
need to query and call setTargetSize manually.

Since there is no way for a client to know that a RAW image couldn't be
decoded to the desired size (could previously be done with
getSampledSize), make setSampleSize do the extra scaling. This is a
better API anyway.

Change-Id: I84c29fdc6bdfb999a7f712fdc069304ae9676ba6
parent ff23ffa8
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -13635,7 +13635,6 @@ package android.graphics {
    method public android.graphics.ImageDecoder.OnPartialImageListener getOnPartialImageListener();
    method public android.graphics.PostProcessor getPostProcessor();
    method public boolean getRequireUnpremultiplied();
    method public android.util.Size getSampledSize(int);
    method public android.graphics.ImageDecoder setAllocator(int);
    method public android.graphics.ImageDecoder setConserveMemory(boolean);
    method public android.graphics.ImageDecoder setCrop(android.graphics.Rect);
@@ -13644,8 +13643,8 @@ package android.graphics {
    method public android.graphics.ImageDecoder setOnPartialImageListener(android.graphics.ImageDecoder.OnPartialImageListener);
    method public android.graphics.ImageDecoder setPostProcessor(android.graphics.PostProcessor);
    method public android.graphics.ImageDecoder setRequireUnpremultiplied(boolean);
    method public android.graphics.ImageDecoder setResize(int, int);
    method public android.graphics.ImageDecoder setResize(int);
    method public android.graphics.ImageDecoder setSampleSize(int);
    method public android.graphics.ImageDecoder setTargetSize(int, int);
    field public static final int ALLOCATOR_DEFAULT = 0; // 0x0
    field public static final int ALLOCATOR_HARDWARE = 3; // 0x3
    field public static final int ALLOCATOR_SHARED_MEMORY = 2; // 0x2
+2 −0
Original line number Diff line number Diff line
@@ -184,6 +184,8 @@ package android.graphics {
  public final class ImageDecoder implements java.lang.AutoCloseable {
    method public deprecated boolean getAsAlphaMask();
    method public deprecated android.graphics.ImageDecoder setAsAlphaMask(boolean);
    method public deprecated android.graphics.ImageDecoder setResize(int, int);
    method public deprecated android.graphics.ImageDecoder setResize(int);
    field public static final deprecated int ERROR_SOURCE_ERROR = 3; // 0x3
    field public static final deprecated int ERROR_SOURCE_EXCEPTION = 1; // 0x1
    field public static final deprecated int ERROR_SOURCE_INCOMPLETE = 2; // 0x2
+74 −11
Original line number Diff line number Diff line
@@ -766,7 +766,7 @@ public final class ImageDecoder implements AutoCloseable {
     *
     *  <p>This takes an input that functions like
     *  {@link BitmapFactory.Options#inSampleSize}. It returns a width and
     *  height that can be acheived by sampling the encoded image. Other widths
     *  height that can be achieved by sampling the encoded image. Other widths
     *  and heights may be supported, but will require an additional (internal)
     *  scaling step. Such internal scaling is *not* supported with
     *  {@link #setRequireUnpremultiplied} set to {@code true}.</p>
@@ -774,6 +774,8 @@ public final class ImageDecoder implements AutoCloseable {
     *  @param sampleSize Sampling rate of the encoded image.
     *  @return {@link android.util.Size} of the width and height after
     *      sampling.
     *
     *  @hide
     */
    @NonNull
    public Size getSampledSize(int sampleSize) {
@@ -789,14 +791,28 @@ public final class ImageDecoder implements AutoCloseable {
    }

    // Modifiers
    /** @removed
     * @deprecated Renamed to {@link #setTargetSize}.
     */
    @java.lang.Deprecated
    public ImageDecoder setResize(int width, int height) {
        return this.setTargetSize(width, height);
    }

    /**
     *  Resize the output to have the following size.
     *  Specify the size of the output {@link Drawable} or {@link Bitmap}.
     *
     *  <p>By default, the output size will match the size of the encoded
     *  image, which can be retrieved from the {@link ImageInfo} in
     *  {@link OnHeaderDecodedListener#onHeaderDecoded}.</p>
     *
     *  <p>Only the last call to this or {@link #setSampleSize} is respected.</p>
     *
     *  @param width must be greater than 0.
     *  @param height must be greater than 0.
     *  @return this object for chaining.
     */
    public ImageDecoder setResize(int width, int height) {
    public ImageDecoder setTargetSize(int width, int height) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("Dimensions must be positive! "
                    + "provided (" + width + ", " + height + ")");
@@ -807,18 +823,65 @@ public final class ImageDecoder implements AutoCloseable {
        return this;
    }

    /** @removed
     * @deprecated Renamed to {@link #setSampleSize}.
     */
    @java.lang.Deprecated
    public ImageDecoder setResize(int sampleSize) {
        return this.setSampleSize(sampleSize);
    }

    private int getTargetDimension(int original, int sampleSize, int computed) {
        // Sampling will never result in a smaller size than 1.
        if (sampleSize >= original) {
            return 1;
        }

        // Use integer divide to find the desired size. If that is what
        // getSampledSize computed, that is the size to use.
        int target = original / sampleSize;
        if (computed == target) {
            return computed;
        }

        // If sampleSize does not divide evenly into original, the decoder
        // may round in either direction. It just needs to get a result that
        // is close.
        int reverse = computed * sampleSize;
        if (Math.abs(reverse - original) < sampleSize) {
            // This is the size that can be decoded most efficiently.
            return computed;
        }

        // The decoder could not get close (e.g. it is a DNG image).
        return target;
    }

    /**
     *  Resize based on a sample size.
     *  Set the target size with a sampleSize.
     *
     *  <p>By default, the output size will match the size of the encoded
     *  image, which can be retrieved from the {@link ImageInfo} in
     *  {@link OnHeaderDecodedListener#onHeaderDecoded}.</p>
     *
     *  <p>This has the same effect as passing the result of
     *  {@link #getSampledSize} to {@link #setResize(int, int)}.</p>
     *  <p>Requests the decoder to subsample the original image, returning a
     *  smaller image to save memory. The sample size is the number of pixels
     *  in either dimension that correspond to a single pixel in the output.
     *  For example, sampleSize == 4 returns an image that is 1/4 the
     *  width/height of the original, and 1/16 the number of pixels.</p>
     *
     *  <p>Must be greater than or equal to 1.</p>
     *
     *  <p>Only the last call to this or {@link #setTargetSize} is respected.</p>
     *
     *  @param sampleSize Sampling rate of the encoded image.
     *  @return this object for chaining.
     */
    public ImageDecoder setResize(int sampleSize) {
    public ImageDecoder setSampleSize(int sampleSize) {
        Size size = this.getSampledSize(sampleSize);
        return this.setResize(size.getWidth(), size.getHeight());
        int targetWidth = getTargetDimension(mWidth, sampleSize, size.getWidth());
        int targetHeight = getTargetDimension(mHeight, sampleSize, size.getHeight());
        return this.setTargetSize(targetWidth, targetHeight);
    }

    private boolean requestedResize() {
@@ -972,8 +1035,8 @@ public final class ImageDecoder implements AutoCloseable {
     *  Crop the output to {@code subset} of the (possibly) scaled image.
     *
     *  <p>{@code subset} must be contained within the size set by
     *  {@link #setResize} or the bounds of the image if setResize was not
     *  called. Otherwise an {@link IllegalStateException} will be thrown by
     *  {@link #setTargetSize} or the bounds of the image if setTargetSize was
     *  not called. Otherwise an {@link IllegalStateException} will be thrown by
     *  {@link #decodeDrawable}/{@link #decodeBitmap}.</p>
     *
     *  <p>NOT intended as a replacement for
@@ -1353,7 +1416,7 @@ public final class ImageDecoder implements AutoCloseable {
        float scale = (float) dstDensity / srcDensity;
        int scaledWidth = (int) (decoder.mWidth * scale + 0.5f);
        int scaledHeight = (int) (decoder.mHeight * scale + 0.5f);
        decoder.setResize(scaledWidth, scaledHeight);
        decoder.setTargetSize(scaledWidth, scaledHeight);
        return dstDensity;
    }