Java Code Examples for com.jme3.texture.Image#Format

The following examples show how to use com.jme3.texture.Image#Format . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KTXLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * returns the JME image format from gl formats and types.
 * @param glFormat
 * @param glInternalFormat
 * @param glType
 * @return 
 */
private Image.Format getImageFormat(int glFormat, int glInternalFormat, int glType) {
    EnumSet<Caps> caps = EnumSet.allOf(Caps.class);
    GLImageFormat[][] formats = GLImageFormats.getFormatsForCaps(caps);
    for (GLImageFormat[] format : formats) {
        for (int j = 0; j < format.length; j++) {
            GLImageFormat glImgFormat = format[j];
            if (glImgFormat != null) {
                if (glImgFormat.format == glFormat && glImgFormat.dataType == glType) {
                    if (glFormat == glInternalFormat || glImgFormat.internalFormat == glInternalFormat) {
                        return Image.Format.values()[j];
                    }
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: GLImageFormats.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void formatSrgb(GLImageFormat[][] formatToGL, Image.Format format, 
                               int glInternalFormat, 
                               int glFormat, 
                               int glDataType)
{
    formatToGL[1][format.ordinal()] = new GLImageFormat(glInternalFormat, glFormat, glDataType);
}
 
Example 3
Source File: GLImageFormats.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void formatSrgbSwiz(GLImageFormat[][] formatToGL, Image.Format format, 
                                   int glInternalFormat, 
                                   int glFormat, 
                                   int glDataType)
{
    formatToGL[1][format.ordinal()] = new GLImageFormat(glInternalFormat, glFormat, glDataType, false, true);
}
 
Example 4
Source File: GLImageFormats.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void formatCompSrgb(GLImageFormat[][] formatToGL, Image.Format format, 
                                   int glCompressedFormat,
                                   int glFormat, 
                                   int glDataType)
{
    formatToGL[1][format.ordinal()] = new GLImageFormat(glCompressedFormat, glFormat, glDataType, true);
}
 
Example 5
Source File: AndroidBufferImageLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
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;
}
 
Example 6
Source File: GLImageFormats.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void formatSwiz(GLImageFormat[][] formatToGL, Image.Format format, 
                               int glInternalFormat, 
                               int glFormat, 
                               int glDataType){
    formatToGL[0][format.ordinal()] = new GLImageFormat(glInternalFormat, glFormat, glDataType, false, true);
}
 
Example 7
Source File: GLImageFormats.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void formatComp(GLImageFormat[][] formatToGL, Image.Format format, 
                               int glCompressedFormat,
                               int glFormat, 
                               int glDataType){
    formatToGL[0][format.ordinal()] = new GLImageFormat(glCompressedFormat, glFormat, glDataType, true);
}
 
Example 8
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void uploadTexture(Image image,
                          int target,
                          int index,
                          boolean linearizeSrgb) {

    boolean getSrgbFormat = image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    Image.Format jmeFormat = image.getFormat();
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);

    ByteBuffer data = null;
    int sliceCount = 1;
    
    if (index >= 0) {
        data = image.getData(index);
    }
    
    if (image.getData() != null && image.getData().size() > 0) {
        sliceCount = image.getData().size();
    }

    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    
    int[] mipSizes = image.getMipMapSizes();
    int pos = 0;
    // TODO: Remove unnecessary allocation
    if (mipSizes == null) {
        if (data != null) {
            mipSizes = new int[]{data.capacity()};
        } else {
            mipSizes = new int[]{width * height * jmeFormat.getBitsPerPixel() / 8};
        }
    }

    int samples = image.getMultiSamples();
    
    // For OGL3 core: setup texture swizzle.
    if (oglFormat.swizzleRequired) {
        setupTextureSwizzle(target, jmeFormat);
    }

    for (int i = 0; i < mipSizes.length; i++) {
        int mipWidth = Math.max(1, width >> i);
        int mipHeight = Math.max(1, height >> i);
        int mipDepth = Math.max(1, depth >> i);

        if (data != null) {
            data.position(pos);
            data.limit(pos + mipSizes[i]);
        }

        uploadTextureLevel(oglFormat, target, i, index, sliceCount, mipWidth, mipHeight, mipDepth, samples, data);

        pos += mipSizes[i];
    }
}
 
Example 9
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @deprecated Use uploadSubTexture(int target,  Image src, int index,int targetX, int targetY,int srcX,int srcY,  int areaWidth,int areaHeight, boolean linearizeSrgb) 
 */
@Deprecated
public void uploadSubTexture(Image image, int target, int index, int x, int y, boolean linearizeSrgb) {
    if (target != GL.GL_TEXTURE_2D || image.getDepth() > 1) {
        throw new UnsupportedOperationException("Updating non-2D texture is not supported");
    }
    
    if (image.getMipMapSizes() != null) {
        throw new UnsupportedOperationException("Updating mip-mapped images is not supported");
    }
    
    if (image.getMultiSamples() > 1) {
        throw new UnsupportedOperationException("Updating multisampled images is not supported");
    }
    
    Image.Format jmeFormat = image.getFormat();
    
    if (jmeFormat.isCompressed()) {
        throw new UnsupportedOperationException("Updating compressed images is not supported");
    } else if (jmeFormat.isDepthFormat()) {
        throw new UnsupportedOperationException("Updating depth images is not supported");
    }
    
    boolean getSrgbFormat = image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);
    
    ByteBuffer data = null;
    
    if (index >= 0) {
        data = image.getData(index);
    }
    
    if (data == null) {
        throw new IndexOutOfBoundsException("The image index " + index + " is not valid for the given image");
    }

    data.position(0);
    data.limit(data.capacity());
    
    gl.glTexSubImage2D(target, 0, x, y, image.getWidth(), image.getHeight(), 
                       oglFormat.format, oglFormat.dataType, data);
}
 
Example 10
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void uploadSubTexture(int target, Image src, int index, int targetX, int targetY, int areaX, int areaY, int areaWidth, int areaHeight, boolean linearizeSrgb) {
    if (target != GL.GL_TEXTURE_2D || src.getDepth() > 1) {
        throw new UnsupportedOperationException("Updating non-2D texture is not supported");
    }

    if (src.getMipMapSizes() != null) {
        throw new UnsupportedOperationException("Updating mip-mappped images is not supported");
    }

    if (src.getMultiSamples() > 1) {
        throw new UnsupportedOperationException("Updating multisampled images is not supported");
    }

    Image.Format jmeFormat = src.getFormat();

    if (jmeFormat.isCompressed()) {
        throw new UnsupportedOperationException("Updating compressed images is not supported");
    } else if (jmeFormat.isDepthFormat()) {
        throw new UnsupportedOperationException("Updating depth images is not supported");
    }

    boolean getSrgbFormat = src.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);

    ByteBuffer data = src.getData(index);

    if (data == null) {
        throw new IndexOutOfBoundsException("The image index " + index + " is not valid for the given image");
    }

    int Bpp = src.getFormat().getBitsPerPixel() / 8;

    int srcWidth = src.getWidth();
    int cpos = data.position();
    int skip = areaX;
    skip += areaY * srcWidth;
    skip *= Bpp;

    data.position(skip);

    boolean needsStride = srcWidth != areaWidth;

    if (needsStride && (!supportUnpackRowLength)) { // doesn't support stride, copy row by row (slower).
        for (int i = 0; i < areaHeight; i++) {
            data.position(skip + (srcWidth * Bpp * i));
            gl.glTexSubImage2D(target, 0, targetX, targetY + i, areaWidth, 1, oglFormat.format, oglFormat.dataType, data);
        }
    } else {
        if (needsStride)
            gl2.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH, srcWidth);
        gl.glTexSubImage2D(target, 0, targetX, targetY, areaWidth, areaHeight, oglFormat.format, oglFormat.dataType, data);
        if (needsStride)
            gl2.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH, 0);
    }
    data.position(cpos);

}
 
Example 11
Source File: GLRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void readFrameBufferWithFormat(FrameBuffer fb, ByteBuffer byteBuf, Image.Format format) {
    GLImageFormat glFormat = texUtil.getImageFormatWithError(format, false);
    readFrameBufferWithGLFormat(fb, byteBuf, glFormat.format, glFormat.dataType);
}
 
Example 12
Source File: NullRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void readFrameBufferWithFormat(FrameBuffer fb, ByteBuffer byteBuf, Image.Format format) {        
}
 
Example 13
Source File: KTXWriter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * get the glformat from JME image Format
 * @param format
 * @return 
 */
private GLImageFormat getGlFormat(Image.Format format) {
    EnumSet<Caps> caps = EnumSet.allOf(Caps.class);
    GLImageFormat[][] formats = GLImageFormats.getFormatsForCaps(caps);
    return formats[0][format.ordinal()];
}
 
Example 14
Source File: KTXLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Create an image with given parameters
 * @param nbSlices
 * @param byteBuffersSize
 * @param imgFormat
 * @param pixelWidth
 * @param pixelHeight
 * @param depth
 * @return 
 */
private Image createImage(int nbSlices, int byteBuffersSize, Image.Format imgFormat, int pixelWidth, int pixelHeight, int depth) {
    ArrayList<ByteBuffer> imageData = new ArrayList<ByteBuffer>(nbSlices);
    for (int i = 0; i < nbSlices; i++) {
        imageData.add(BufferUtils.createByteBuffer(byteBuffersSize));
    }
    Image image = new Image(imgFormat, pixelWidth, pixelHeight, depth, imageData, ColorSpace.sRGB);
    return image;
}
 
Example 15
Source File: Renderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Reads the pixels currently stored in the specified framebuffer
 * into the given ByteBuffer object. 
 * Only color pixels are transferred, with the given format. 
 * The given byte buffer should have at least
 * fb.getWidth() * fb.getHeight() * 4 bytes remaining.
 * 
 * @param fb The framebuffer to read from
 * @param byteBuf The bytebuffer to transfer color data to
 * @param format the image format to use when reading the frameBuffer.
 */
public void readFrameBufferWithFormat(FrameBuffer fb, ByteBuffer byteBuf, Image.Format format);