Search in sources :

Example 26 with Config

use of android.graphics.Bitmap.Config 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)

Example 27 with Config

use of android.graphics.Bitmap.Config in project AndEngine by nicolasgramlich.

the class BitmapTextureAtlas method writeTextureToHardware.

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
@Override
protected void writeTextureToHardware(final GL10 pGL) {
    final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig();
    final int glFormat = this.mPixelFormat.getGLFormat();
    final int glType = this.mPixelFormat.getGLType();
    final boolean preMultipyAlpha = this.mTextureOptions.mPreMultipyAlpha;
    final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
    final int textureSourceCount = textureSources.size();
    for (int j = 0; j < textureSourceCount; j++) {
        final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(j);
        if (bitmapTextureAtlasSource != null) {
            final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
            try {
                if (bitmap == null) {
                    throw new IllegalArgumentException(bitmapTextureAtlasSource.getClass().getSimpleName() + ": " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
                }
                if (preMultipyAlpha) {
                    GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTexturePositionX(), bitmapTextureAtlasSource.getTexturePositionY(), bitmap, glFormat, glType);
                } else {
                    GLHelper.glTexSubImage2D(pGL, GL10.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTexturePositionX(), bitmapTextureAtlasSource.getTexturePositionY(), bitmap, this.mPixelFormat);
                }
                bitmap.recycle();
            } catch (final IllegalArgumentException iae) {
                // TODO Load some static checkerboard or so to visualize that loading the texture has failed.
                // private Buffer createImage(final int width, final int height) {
                // final int stride = 3 * width;
                // final ByteBuffer image = ByteBuffer.allocateDirect(height * stride)
                // .order(ByteOrder.nativeOrder());
                // 
                // // Fill with a pretty "munching squares" pattern:
                // for (int t = 0; t < height; t++) {
                // final byte red = (byte) (255 - 2 * t);
                // final byte green = (byte) (2 * t);
                // final byte blue = 0;
                // for (int x = 0; x < width; x++) {
                // final int y = x ^ t;
                // image.position(stride * y + x * 3);
                // image.put(red);
                // image.put(green);
                // image.put(blue);
                // }
                // }
                // image.position(0);
                // return image;
                // }
                Debug.e("Error loading: " + bitmapTextureAtlasSource.toString(), iae);
                if (this.getTextureStateListener() != null) {
                    this.getTextureStateListener().onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, iae);
                } else {
                    throw iae;
                }
            }
        }
    }
}
Also used : IBitmapTextureAtlasSource(org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource) Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config)

Example 28 with Config

use of android.graphics.Bitmap.Config in project android_packages_apps_Gallery2 by LineageOS.

the class UploadedTexture method uploadToCanvas.

private void uploadToCanvas(GLCanvas canvas) {
    Bitmap bitmap = getBitmap();
    if (bitmap != null && !bitmap.isRecycled()) {
        try {
            int bWidth = bitmap.getWidth();
            int bHeight = bitmap.getHeight();
            int width = bWidth + mBorder * 2;
            int height = bHeight + mBorder * 2;
            int texWidth = getTextureWidth();
            int texHeight = getTextureHeight();
            Utils.assertTrue(bWidth <= texWidth && bHeight <= texHeight);
            // Null pointer check here is to avoid monkey test failure.
            if (canvas.getGLId() != null) {
                // Upload the bitmap to a new texture.
                mId = canvas.getGLId().generateTexture();
            }
            canvas.setTextureParameters(this);
            if (bWidth == texWidth && bHeight == texHeight) {
                canvas.initializeTexture(this, bitmap);
            } else {
                int format = GLUtils.getInternalFormat(bitmap);
                int type = GLUtils.getType(bitmap);
                Config config = bitmap.getConfig();
                canvas.initializeTextureSize(this, format, type);
                canvas.texSubImage2D(this, mBorder, mBorder, bitmap, format, type);
                if (mBorder > 0) {
                    // Left border
                    Bitmap line = getBorderLine(true, config, texHeight);
                    canvas.texSubImage2D(this, 0, 0, line, format, type);
                    // Top border
                    line = getBorderLine(false, config, texWidth);
                    canvas.texSubImage2D(this, 0, 0, line, format, type);
                }
                // Right border
                if (mBorder + bWidth < texWidth) {
                    Bitmap line = getBorderLine(true, config, texHeight);
                    canvas.texSubImage2D(this, mBorder + bWidth, 0, line, format, type);
                }
                // Bottom border
                if (mBorder + bHeight < texHeight) {
                    Bitmap line = getBorderLine(false, config, texWidth);
                    canvas.texSubImage2D(this, 0, mBorder + bHeight, line, format, type);
                }
            }
        } finally {
            freeBitmap();
        }
        // Update texture state.
        setAssociatedCanvas(canvas);
        mState = STATE_LOADED;
        mContentValid = true;
    } else {
        mState = STATE_ERROR;
        if (bitmap == null) {
            throw new RuntimeException("Texture load fail, no bitmap");
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config)

Example 29 with Config

use of android.graphics.Bitmap.Config in project glide by bumptech.

the class TransformationUtils method rotateImageExif.

/**
 * Rotate and/or flip the image to match the given exif orientation.
 *
 * @param pool            A pool that may or may not contain an image of the necessary
 *                        dimensions.
 * @param inBitmap        The bitmap to rotate/flip.
 * @param exifOrientation the exif orientation [1-8].
 * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary.
 */
public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation) {
    if (!isExifOrientationRequired(exifOrientation)) {
        return inBitmap;
    }
    final Matrix matrix = new Matrix();
    initializeMatrixForRotation(exifOrientation, matrix);
    // From Bitmap.createBitmap.
    final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight());
    matrix.mapRect(newRect);
    final int newWidth = Math.round(newRect.width());
    final int newHeight = Math.round(newRect.height());
    Bitmap.Config config = getNonNullConfig(inBitmap);
    Bitmap result = pool.get(newWidth, newHeight, config);
    matrix.postTranslate(-newRect.left, -newRect.top);
    applyMatrix(inBitmap, result, matrix);
    return result;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Paint(android.graphics.Paint) Config(android.graphics.Bitmap.Config)

Example 30 with Config

use of android.graphics.Bitmap.Config in project glide by bumptech.

the class TransformationUtils method fitCenter.

/**
 * An expensive operation to resize the given Bitmap down so that it fits within the given
 * dimensions maintain the original proportions.
 *
 * @param pool   The BitmapPool obtain a bitmap from.
 * @param inBitmap  The Bitmap to shrink.
 * @param width  The width in pixels the final image will fit within.
 * @param height The height in pixels the final image will fit within.
 * @return A new Bitmap shrunk to fit within the given dimensions, or toFit if toFit's width or
 * height matches the given dimensions and toFit fits within the given dimensions
 */
public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height) {
    if (inBitmap.getWidth() == width && inBitmap.getHeight() == height) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "requested target size matches input, returning input");
        }
        return inBitmap;
    }
    final float widthPercentage = width / (float) inBitmap.getWidth();
    final float heightPercentage = height / (float) inBitmap.getHeight();
    final float minPercentage = Math.min(widthPercentage, heightPercentage);
    // Round here in case we've decoded exactly the image we want, but take the floor below to
    // avoid a line of garbage or blank pixels in images.
    int targetWidth = Math.round(minPercentage * inBitmap.getWidth());
    int targetHeight = Math.round(minPercentage * inBitmap.getHeight());
    if (inBitmap.getWidth() == targetWidth && inBitmap.getHeight() == targetHeight) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "adjusted target size matches input, returning input");
        }
        return inBitmap;
    }
    // Take the floor of the target width/height, not round. If the matrix
    // passed into drawBitmap rounds differently, we want to slightly
    // overdraw, not underdraw, to avoid artifacts from bitmap reuse.
    targetWidth = (int) (minPercentage * inBitmap.getWidth());
    targetHeight = (int) (minPercentage * inBitmap.getHeight());
    Bitmap.Config config = getNonNullConfig(inBitmap);
    Bitmap toReuse = pool.get(targetWidth, targetHeight, config);
    // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
    TransformationUtils.setAlpha(inBitmap, toReuse);
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "request: " + width + "x" + height);
        Log.v(TAG, "toFit:   " + inBitmap.getWidth() + "x" + inBitmap.getHeight());
        Log.v(TAG, "toReuse: " + toReuse.getWidth() + "x" + toReuse.getHeight());
        Log.v(TAG, "minPct:   " + minPercentage);
    }
    Matrix matrix = new Matrix();
    matrix.setScale(minPercentage, minPercentage);
    applyMatrix(inBitmap, toReuse, matrix);
    return toReuse;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Paint(android.graphics.Paint) Config(android.graphics.Bitmap.Config)

Aggregations

Config (android.graphics.Bitmap.Config)34 Bitmap (android.graphics.Bitmap)31 Paint (android.graphics.Paint)9 Canvas (android.graphics.Canvas)6 Matrix (android.graphics.Matrix)4 TargetApi (android.annotation.TargetApi)2 BitmapFactory (android.graphics.BitmapFactory)2 Rect (android.graphics.Rect)2 RectF (android.graphics.RectF)2 Nullable (android.support.annotation.Nullable)2 NullBitmapException (org.andengine.util.exception.NullBitmapException)2 SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 PaintFlagsDrawFilter (android.graphics.PaintFlagsDrawFilter)1 Point (android.graphics.Point)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1