Search in sources :

Example 1 with RobolectricBufferedImage

use of org.robolectric.shadows.ImageUtil.RobolectricBufferedImage in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeStream.

@Implementation
protected static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
    byte[] ninePatchChunk = null;
    if (is instanceof AssetInputStream) {
        ShadowAssetInputStream sais = Shadow.extract(is);
        if (sais.isNinePatch()) {
            ninePatchChunk = new byte[0];
        }
        if (sais.getDelegate() != null) {
            is = sais.getDelegate();
        }
    }
    try {
        if (is != null) {
            is.reset();
        }
    } catch (IOException e) {
    // ignore
    }
    boolean isNamedStream = is instanceof NamedStream;
    String name = isNamedStream ? is.toString().replace("stream for ", "") : null;
    RobolectricBufferedImage image = isNamedStream ? null : getImageFromStream(is);
    if (!allowInvalidImageData && image == null) {
        if (opts != null) {
            opts.outWidth = -1;
            opts.outHeight = -1;
        }
        return null;
    }
    Bitmap bitmap = create(name, outPadding, opts, null, image);
    ReflectionHelpers.callInstanceMethod(bitmap, "setNinePatchChunk", ClassParameter.from(byte[].class, ninePatchChunk));
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.createdFromStream = is;
    if (image != null && opts != null) {
        opts.outMimeType = image.getMimeType();
    }
    return bitmap;
}
Also used : AssetInputStream(android.content.res.AssetManager.AssetInputStream) Bitmap(android.graphics.Bitmap) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) IOException(java.io.IOException) NamedStream(org.robolectric.util.NamedStream) Implementation(org.robolectric.annotation.Implementation)

Example 2 with RobolectricBufferedImage

use of org.robolectric.shadows.ImageUtil.RobolectricBufferedImage in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeByteArray.

@Implementation
protected static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
    String desc = new String(data, UTF_8);
    if (offset != 0 || length != data.length) {
        desc += " bytes " + offset + ".." + length;
    }
    ByteArrayInputStream is = new ByteArrayInputStream(data, offset, length);
    RobolectricBufferedImage image = getImageFromStream(is);
    if (!allowInvalidImageData && image == null) {
        if (opts != null) {
            opts.outWidth = -1;
            opts.outHeight = -1;
        }
        return null;
    }
    Bitmap bitmap = create(desc, opts, image);
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.createdFromBytes = data;
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) Implementation(org.robolectric.annotation.Implementation)

Example 3 with RobolectricBufferedImage

use of org.robolectric.shadows.ImageUtil.RobolectricBufferedImage in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeResource.

@Implementation
protected static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options options) {
    if (id == 0) {
        return null;
    }
    final TypedValue value = new TypedValue();
    InputStream is = res.openRawResource(id, value);
    String resourceName = res.getResourceName(id);
    RobolectricBufferedImage image = getImageFromStream(resourceName, is);
    if (!allowInvalidImageData && image == null) {
        if (options != null) {
            options.outWidth = -1;
            options.outHeight = -1;
        }
        return null;
    }
    Bitmap bitmap = create("resource:" + resourceName, options, image);
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.createdFromResId = id;
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AssetInputStream(android.content.res.AssetManager.AssetInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TypedValue(android.util.TypedValue) Implementation(org.robolectric.annotation.Implementation)

Example 4 with RobolectricBufferedImage

use of org.robolectric.shadows.ImageUtil.RobolectricBufferedImage in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeFile.

@SuppressWarnings("Var")
@Implementation
protected static Bitmap decodeFile(String pathName, BitmapFactory.Options options) {
    // If a real file is used, attempt to get the image size from that file.
    RobolectricBufferedImage image = null;
    if (pathName != null && new File(pathName).exists()) {
        try (FileInputStream fileInputStream = new FileInputStream(pathName);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            image = getImageFromStream(pathName, bufferedInputStream);
        } catch (IOException e) {
            Logger.warn("Error getting size of bitmap file", e);
        }
    }
    if (!allowInvalidImageData && image == null) {
        if (options != null) {
            options.outWidth = -1;
            options.outHeight = -1;
        }
        return null;
    }
    Bitmap bitmap = create("file:" + pathName, options, image);
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.createdFromPath = pathName;
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Implementation(org.robolectric.annotation.Implementation)

Example 5 with RobolectricBufferedImage

use of org.robolectric.shadows.ImageUtil.RobolectricBufferedImage in project robolectric by robolectric.

the class ShadowBitmapFactory method create.

private static Bitmap create(final String name, final Rect outPadding, final BitmapFactory.Options options, final Point widthAndHeightOverride, final RobolectricBufferedImage image) {
    Bitmap bitmap = Shadow.newInstanceOf(Bitmap.class);
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.appendDescription(name == null ? "Bitmap" : "Bitmap for " + name);
    Bitmap.Config config;
    if (options != null && options.inPreferredConfig != null) {
        config = options.inPreferredConfig;
    } else {
        config = Bitmap.Config.ARGB_8888;
    }
    shadowBitmap.setConfig(config);
    String optionsString = stringify(options);
    if (!optionsString.isEmpty()) {
        shadowBitmap.appendDescription(" with options ");
        shadowBitmap.appendDescription(optionsString);
    }
    Point p = new Point(selectWidthAndHeight(name, widthAndHeightOverride, image));
    if (options != null && options.inSampleSize > 1) {
        p.x = p.x / options.inSampleSize;
        p.y = p.y / options.inSampleSize;
        p.x = p.x == 0 ? 1 : p.x;
        p.y = p.y == 0 ? 1 : p.y;
    }
    shadowBitmap.setWidth(p.x);
    shadowBitmap.setHeight(p.y);
    if (image != null) {
        BufferedImage bufferedImage = new BufferedImage(p.x, p.y, BufferedImage.TYPE_INT_ARGB);
        // Copy the image as TYPE_INT_ARGB for fast comparison (sameAs).
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(image.getBufferedImage(), 0, 0, null);
        g.dispose();
        shadowBitmap.setBufferedImage(bufferedImage);
    } else {
        shadowBitmap.setPixelsInternal(new int[p.x * p.y], 0, 0, 0, 0, p.x, p.y);
    }
    if (options != null) {
        options.outWidth = p.x;
        options.outHeight = p.y;
        shadowBitmap.setMutable(options.inMutable);
    }
    if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.KITKAT) {
        ReflectionHelpers.callStaticMethod(BitmapFactory.class, "setDensityFromOptions", ClassParameter.from(Bitmap.class, bitmap), ClassParameter.from(BitmapFactory.Options.class, options));
    } else {
        bitmap = ReflectionHelpers.callStaticMethod(BitmapFactory.class, "finishDecode", ClassParameter.from(Bitmap.class, bitmap), ClassParameter.from(Rect.class, outPadding), ClassParameter.from(BitmapFactory.Options.class, options));
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Point(android.graphics.Point) BitmapFactory(android.graphics.BitmapFactory) Config(android.graphics.Bitmap.Config) BufferedImage(java.awt.image.BufferedImage) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Bitmap (android.graphics.Bitmap)6 RobolectricBufferedImage (org.robolectric.shadows.ImageUtil.RobolectricBufferedImage)6 Implementation (org.robolectric.annotation.Implementation)5 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 AssetInputStream (android.content.res.AssetManager.AssetInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Config (android.graphics.Bitmap.Config)1 BitmapFactory (android.graphics.BitmapFactory)1 Point (android.graphics.Point)1 TypedValue (android.util.TypedValue)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 NamedStream (org.robolectric.util.NamedStream)1