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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations