Search in sources :

Example 6 with Config

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

the class BitmapTextureAtlas method writeTextureToHardware.

// ===========================================================
// Methods
// ===========================================================
@Override
protected void writeTextureToHardware(final GLState pGLState) {
    final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
    final int glInternalFormat = pixelFormat.getGLInternalFormat();
    final int glFormat = pixelFormat.getGLFormat();
    final int glType = pixelFormat.getGLType();
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);
    final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
    /* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
    final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;
    final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
    final int textureSourceCount = textureSources.size();
    final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
    for (int i = 0; i < textureSourceCount; i++) {
        final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
        try {
            final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
            if (bitmap == null) {
                throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
            }
            final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
            if (!useDefaultAlignment) {
                /* Adjust unpack alignment. */
                GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
            }
            if (preMultipyAlpha) {
                GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
            } else {
                pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
            }
            if (!useDefaultAlignment) {
                /* Restore default unpack alignment. */
                GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
            }
            bitmap.recycle();
            if (textureStateListener != null) {
                textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
            }
        } catch (final NullBitmapException e) {
            if (textureStateListener != null) {
                textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
            } else {
                throw e;
            }
        }
    }
}
Also used : IBitmapTextureAtlasSource(org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource) Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config) NullBitmapException(org.andengine.util.exception.NullBitmapException) PixelFormat(org.andengine.opengl.texture.PixelFormat)

Example 7 with Config

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

the class BitmapTexture method writeTextureToHardware.

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
    final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig();
    final Bitmap bitmap = this.onGetBitmap(bitmapConfig);
    if (bitmap == null) {
        throw new NullBitmapException("Caused by: '" + this.toString() + "'.");
    }
    final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (this.mPixelFormat == PixelFormat.RGBA_8888);
    if (!useDefaultAlignment) {
        /* Adjust unpack alignment. */
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    }
    final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
    if (preMultipyAlpha) {
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    } else {
        pGLState.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0, this.mPixelFormat);
    }
    if (!useDefaultAlignment) {
        /* Restore default unpack alignment. */
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
    }
    bitmap.recycle();
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config) NullBitmapException(org.andengine.util.exception.NullBitmapException)

Example 8 with Config

use of android.graphics.Bitmap.Config in project wechat by motianhuo.

the class ImageUtils method drawableToBitmap.

/**
	 * Drawable转Bitmap
	 * 
	 * @param drawable
	 * @return
	 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Config(android.graphics.Bitmap.Config)

Example 9 with Config

use of android.graphics.Bitmap.Config in project android_frameworks_base by DirtyUnicorns.

the class UploadedTexture method uploadToCanvas.

private void uploadToCanvas(GLCanvas canvas) {
    Bitmap bitmap = getBitmap();
    if (bitmap != null) {
        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();
            Assert.assertTrue(bWidth <= texWidth && bHeight <= texHeight);
            // 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;
        throw new RuntimeException("Texture load fail, no bitmap");
    }
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config)

Example 10 with Config

use of android.graphics.Bitmap.Config in project android_frameworks_base by ResurrectionRemix.

the class UploadedTexture method uploadToCanvas.

private void uploadToCanvas(GLCanvas canvas) {
    Bitmap bitmap = getBitmap();
    if (bitmap != null) {
        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();
            Assert.assertTrue(bWidth <= texWidth && bHeight <= texHeight);
            // 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;
        throw new RuntimeException("Texture load fail, no bitmap");
    }
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config)

Aggregations

Config (android.graphics.Bitmap.Config)11 Bitmap (android.graphics.Bitmap)10 Canvas (android.graphics.Canvas)3 Paint (android.graphics.Paint)2 NullBitmapException (org.andengine.util.exception.NullBitmapException)2 SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 PaintFlagsDrawFilter (android.graphics.PaintFlagsDrawFilter)1 Rect (android.graphics.Rect)1 PixelFormat (org.andengine.opengl.texture.PixelFormat)1 IBitmapTextureAtlasSource (org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource)1