Search in sources :

Example 21 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class OGGLoader method load.

public Object load(AssetInfo info) throws IOException {
    if (!(info.getKey() instanceof AudioKey)) {
        throw new IllegalArgumentException("Audio assets must be loaded using an AudioKey");
    }
    AudioKey key = (AudioKey) info.getKey();
    boolean readStream = key.isStream();
    boolean streamCache = key.useStreamCache();
    InputStream in = null;
    try {
        in = info.openStream();
        AudioData data = load(in, readStream, streamCache);
        if (readStream && !streamCache) {
            // we still need the stream in this case ..
            in = null;
        }
        return data;
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : AudioData(com.jme3.audio.AudioData) InputStream(java.io.InputStream) AudioKey(com.jme3.audio.AudioKey)

Example 22 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class AndroidBufferImageLoader method load.

public Object load(AssetInfo assetInfo) throws IOException {
    Bitmap bitmap = null;
    Image.Format format;
    InputStream in = null;
    int bpp;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inTempStorage = tempData;
    options.inScaled = false;
    options.inDither = false;
    options.inInputShareable = true;
    options.inPurgeable = true;
    options.inSampleSize = 1;
    try {
        in = assetInfo.openStream();
        bitmap = BitmapFactory.decodeStream(in, null, options);
        if (bitmap == null) {
            throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    switch(bitmap.getConfig()) {
        case ALPHA_8:
            format = Image.Format.Alpha8;
            bpp = 1;
            break;
        case ARGB_8888:
            format = Image.Format.RGBA8;
            bpp = 4;
            break;
        case RGB_565:
            format = Image.Format.RGB565;
            bpp = 2;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized Android bitmap format: " + bitmap.getConfig());
    }
    TextureKey texKey = (TextureKey) assetInfo.getKey();
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer(bitmap.getWidth() * bitmap.getHeight() * bpp);
    if (format == Image.Format.RGBA8) {
        int[] pixelData = new int[width * height];
        bitmap.getPixels(pixelData, 0, width, 0, 0, width, height);
        if (texKey.isFlipY()) {
            int[] sln = new int[width];
            int y2;
            for (int y1 = 0; y1 < height / 2; y1++) {
                y2 = height - y1 - 1;
                convertARGBtoABGR(pixelData, y1 * width, sln, 0, width);
                convertARGBtoABGR(pixelData, y2 * width, pixelData, y1 * width, width);
                System.arraycopy(sln, 0, pixelData, y2 * width, width);
            }
        } else {
            convertARGBtoABGR(pixelData, 0, pixelData, 0, pixelData.length);
        }
        data.asIntBuffer().put(pixelData);
    } else {
        if (texKey.isFlipY()) {
            // 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);
            }
        }
        bitmap.copyPixelsToBuffer(data);
    }
    data.flip();
    bitmap.recycle();
    Image image = new Image(format, width, height, data, ColorSpace.sRGB);
    return image;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) TextureKey(com.jme3.asset.TextureKey) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BitmapFactory(android.graphics.BitmapFactory)

Example 23 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class AndroidNativeImageLoader method load.

public Image load(AssetInfo info) throws IOException {
    boolean flip = ((TextureKey) info.getKey()).isFlipY();
    InputStream in = null;
    try {
        in = info.openStream();
        return load(info.openStream(), flip, tmpArray);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) InputStream(java.io.InputStream)

Example 24 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class BlenderLoader method setup.

/**
     * This method sets up the loader.
     * @param assetInfo
     *            the asset info
     * @throws BlenderFileException
     *             an exception is throw when something wrong happens with blender file
     */
protected BlenderContext setup(AssetInfo assetInfo) throws BlenderFileException {
    // registering loaders
    ModelKey modelKey = (ModelKey) assetInfo.getKey();
    BlenderKey blenderKey;
    if (modelKey instanceof BlenderKey) {
        blenderKey = (BlenderKey) modelKey;
    } else {
        blenderKey = new BlenderKey(modelKey.getName());
    }
    // opening stream
    BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream());
    // reading blocks
    List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
    FileBlockHeader fileBlock;
    BlenderContext blenderContext = new BlenderContext();
    blenderContext.setBlenderVersion(inputStream.getVersionNumber());
    blenderContext.setAssetManager(assetInfo.getManager());
    blenderContext.setInputStream(inputStream);
    blenderContext.setBlenderKey(blenderKey);
    // creating helpers
    blenderContext.putHelper(AnimationHelper.class, new AnimationHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LandscapeHelper.class, new LandscapeHelper(inputStream.getVersionNumber(), blenderContext));
    // reading the blocks (dna block is automatically saved in the blender context when found)
    FileBlockHeader sceneFileBlock = null;
    do {
        fileBlock = new FileBlockHeader(inputStream, blenderContext);
        if (!fileBlock.isDnaBlock()) {
            blocks.add(fileBlock);
            // save the scene's file block
            if (fileBlock.getCode() == BlockCode.BLOCK_SC00) {
                sceneFileBlock = fileBlock;
            }
        }
    } while (!fileBlock.isLastBlock());
    if (sceneFileBlock != null) {
        blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
    }
    // adding locator for linked content
    assetInfo.getManager().registerLocator(assetInfo.getKey().getName(), LinkedContentLocator.class);
    return blenderContext;
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ModelKey(com.jme3.asset.ModelKey) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) ArrayList(java.util.ArrayList) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LandscapeHelper(com.jme3.scene.plugins.blender.landscape.LandscapeHelper) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) BlenderKey(com.jme3.asset.BlenderKey) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) ParticlesHelper(com.jme3.scene.plugins.blender.particles.ParticlesHelper) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper)

Example 25 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class AndroidAudioLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AndroidAudioData result = new AndroidAudioData();
    result.setAssetKey(assetInfo.getKey());
    return result;
}
Also used : AndroidAudioData(com.jme3.audio.android.AndroidAudioData)

Aggregations

InputStream (java.io.InputStream)17 TextureKey (com.jme3.asset.TextureKey)12 IOException (java.io.IOException)9 Image (com.jme3.texture.Image)7 AssetInfo (com.jme3.asset.AssetInfo)6 Texture (com.jme3.texture.Texture)6 AssetLoadException (com.jme3.asset.AssetLoadException)5 Test (org.junit.Test)5 AssetKey (com.jme3.asset.AssetKey)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)4 ModelKey (com.jme3.asset.ModelKey)4 InputStreamReader (java.io.InputStreamReader)4 BlenderKey (com.jme3.asset.BlenderKey)3 AudioStream (com.jme3.audio.AudioStream)3 MaterialDef (com.jme3.material.MaterialDef)3 DataInputStream (java.io.DataInputStream)3 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 AssetManager (com.jme3.asset.AssetManager)2 AndroidAssetInfo (com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo)2 AudioBuffer (com.jme3.audio.AudioBuffer)2