git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9187 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
06f5ad5bce
commit
b12285b8a4
@ -1,93 +1,101 @@ |
|||||||
package com.jme3.asset; |
package com.jme3.asset; |
||||||
|
|
||||||
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
||||||
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
||||||
import android.graphics.Matrix; |
import android.graphics.Matrix; |
||||||
import com.jme3.texture.Image; |
import com.jme3.texture.Image; |
||||||
import com.jme3.texture.Image.Format; |
import com.jme3.texture.Image.Format; |
||||||
import java.io.IOException; |
import java.io.IOException; |
||||||
import java.io.InputStream; |
import java.io.InputStream; |
||||||
|
|
||||||
public class AndroidImageInfo { |
/** |
||||||
|
* <code>AndroidImageInfo</code> is set in a jME3 image via the {@link Image#setEfficientData(java.lang.Object)} |
||||||
private AssetInfo assetInfo; |
* method to retrieve a {@link Bitmap} when it is needed by the renderer. |
||||||
private Bitmap bitmap; |
* User code may extend <code>AndroidImageInfo</code> and provide their own implementation of the |
||||||
private Format format; |
* {@link AndroidImageInfo#loadBitmap()} method to acquire a bitmap by their own means. |
||||||
|
* |
||||||
public AndroidImageInfo(AssetInfo assetInfo) { |
* @author Kirill Vainer |
||||||
this.assetInfo = assetInfo; |
*/ |
||||||
} |
public class AndroidImageInfo { |
||||||
|
|
||||||
public Bitmap getBitmap(){ |
protected AssetInfo assetInfo; |
||||||
if (bitmap == null || bitmap.isRecycled()){ |
protected Bitmap bitmap; |
||||||
try { |
protected Format format; |
||||||
loadBitmap(); |
|
||||||
} catch (IOException ex) { |
public AndroidImageInfo(AssetInfo assetInfo) { |
||||||
// If called first inside AssetManager, the error will propagate
|
this.assetInfo = assetInfo; |
||||||
// correctly. Assuming that if the first calls succeeds
|
} |
||||||
// then subsequent calls will as well.
|
|
||||||
throw new AssetLoadException("Failed to load image " + assetInfo.getKey(), ex); |
public Bitmap getBitmap(){ |
||||||
} |
if (bitmap == null || bitmap.isRecycled()){ |
||||||
} |
try { |
||||||
return bitmap; |
loadBitmap(); |
||||||
} |
} catch (IOException ex) { |
||||||
|
// If called first inside AssetManager, the error will propagate
|
||||||
|
// correctly. Assuming that if the first calls succeeds
|
||||||
|
// then subsequent calls will as well.
|
||||||
public Format getFormat(){ |
throw new AssetLoadException("Failed to load image " + assetInfo.getKey(), ex); |
||||||
return format; |
} |
||||||
} |
} |
||||||
|
return bitmap; |
||||||
/** |
} |
||||||
* Loads the bitmap directly from the asset info, possibly updating |
|
||||||
* or creating the image object. |
|
||||||
*/ |
|
||||||
private void loadBitmap() throws IOException{ |
public Format getFormat(){ |
||||||
InputStream in = null; |
return format; |
||||||
try { |
} |
||||||
in = assetInfo.openStream(); |
|
||||||
bitmap = BitmapFactory.decodeStream(in); |
/** |
||||||
if (bitmap == null) { |
* Loads the bitmap directly from the asset info, possibly updating |
||||||
throw new IOException("Failed to load image: " + assetInfo.getKey().getName()); |
* or creating the image object. |
||||||
} |
*/ |
||||||
} finally { |
protected void loadBitmap() throws IOException{ |
||||||
if (in != null) { |
InputStream in = null; |
||||||
in.close(); |
try { |
||||||
} |
in = assetInfo.openStream(); |
||||||
} |
bitmap = BitmapFactory.decodeStream(in); |
||||||
|
if (bitmap == null) { |
||||||
switch (bitmap.getConfig()) { |
throw new IOException("Failed to load image: " + assetInfo.getKey().getName()); |
||||||
case ALPHA_8: |
} |
||||||
format = Image.Format.Alpha8; |
} finally { |
||||||
break; |
if (in != null) { |
||||||
case ARGB_4444: |
in.close(); |
||||||
format = Image.Format.ARGB4444; |
} |
||||||
break; |
} |
||||||
case ARGB_8888: |
|
||||||
format = Image.Format.RGBA8; |
switch (bitmap.getConfig()) { |
||||||
break; |
case ALPHA_8: |
||||||
case RGB_565: |
format = Image.Format.Alpha8; |
||||||
format = Image.Format.RGB565; |
break; |
||||||
break; |
case ARGB_4444: |
||||||
default: |
format = Image.Format.ARGB4444; |
||||||
// This should still work as long
|
break; |
||||||
// as renderer doesn't check format
|
case ARGB_8888: |
||||||
// but just loads bitmap directly.
|
format = Image.Format.RGBA8; |
||||||
format = null; |
break; |
||||||
} |
case RGB_565: |
||||||
|
format = Image.Format.RGB565; |
||||||
TextureKey texKey = (TextureKey) assetInfo.getKey(); |
break; |
||||||
if (texKey.isFlipY()) { |
default: |
||||||
// Flip the image, then delete the old one.
|
// This should still work as long
|
||||||
Matrix flipMat = new Matrix(); |
// as renderer doesn't check format
|
||||||
flipMat.preScale(1.0f, -1.0f); |
// but just loads bitmap directly.
|
||||||
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false); |
format = null; |
||||||
bitmap.recycle(); |
} |
||||||
bitmap = newBitmap; |
|
||||||
|
TextureKey texKey = (TextureKey) assetInfo.getKey(); |
||||||
if (bitmap == null) { |
if (texKey.isFlipY()) { |
||||||
throw new IOException("Failed to flip image: " + texKey); |
// Flip the image, then delete the old one.
|
||||||
} |
Matrix flipMat = new Matrix(); |
||||||
} |
flipMat.preScale(1.0f, -1.0f); |
||||||
} |
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false); |
||||||
} |
bitmap.recycle(); |
||||||
|
bitmap = newBitmap; |
||||||
|
|
||||||
|
if (bitmap == null) { |
||||||
|
throw new IOException("Failed to flip image: " + texKey); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue