From dbc59934a7504126f5e98cd185da513b46ea6ce5 Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Sun, 16 Sep 2012 00:54:51 +0000 Subject: [PATCH] * Added new image codec that should be faster for byte component image formats like RGBA8 git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9740 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../texture/image/ByteOffsetImageCodec.java | 54 +++++++++++++++++++ .../com/jme3/texture/image/ImageCodec.java | 54 ++++++++++++------- 2 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 engine/src/core/com/jme3/texture/image/ByteOffsetImageCodec.java diff --git a/engine/src/core/com/jme3/texture/image/ByteOffsetImageCodec.java b/engine/src/core/com/jme3/texture/image/ByteOffsetImageCodec.java new file mode 100644 index 000000000..116f902c8 --- /dev/null +++ b/engine/src/core/com/jme3/texture/image/ByteOffsetImageCodec.java @@ -0,0 +1,54 @@ +package com.jme3.texture.image; + +import java.nio.ByteBuffer; + +public class ByteOffsetImageCodec extends ImageCodec { + + private int redPos, greenPos, bluePos, alphaPos; + + public ByteOffsetImageCodec(int bpp, int flags, int alphaPos, int redPos, int greenPos, int bluePos) { + super(bpp, flags, alphaPos != -1 ? 255 : 0, + redPos != -1 ? 255 : 0, + greenPos != -1 ? 255 : 0, + bluePos != -1 ? 255 : 0); + this.alphaPos = alphaPos; + this.redPos = redPos; + this.greenPos = greenPos; + this.bluePos = bluePos; + } + + @Override + public void readComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { + int i = (y * width + x) * bpp; + if (alphaPos != -1) { + components[0] = buf.get(i + alphaPos) & 0xff; + } + if (redPos != -1) { + components[1] = buf.get(i + redPos) & 0xff; + } + if (greenPos != -1) { + components[2] = buf.get(i + greenPos) & 0xff; + } + if (bluePos != -1) { + components[3] = buf.get(i + bluePos) & 0xff; + } + } + + @Override + public void writeComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { + int i = (y * width + x) * bpp; + if (alphaPos != -1) { + buf.put(i + alphaPos, (byte) components[0]); + } + if (redPos != -1) { + buf.put(i + redPos, (byte) components[1]); + } + if (greenPos != -1) { + buf.put(i + greenPos, (byte) components[2]); + } + if (bluePos != -1) { + buf.put(i + bluePos, (byte) components[3]); + } + } + +} diff --git a/engine/src/core/com/jme3/texture/image/ImageCodec.java b/engine/src/core/com/jme3/texture/image/ImageCodec.java index 5a3d3ca31..f99b96416 100644 --- a/engine/src/core/com/jme3/texture/image/ImageCodec.java +++ b/engine/src/core/com/jme3/texture/image/ImageCodec.java @@ -23,15 +23,20 @@ abstract class ImageCodec { static { // == ALPHA == - params.put(Format.Alpha8, new BitMaskImageCodec(1, 0, 8, 0, 0, 0, - 0, 0, 0, 0)); +// params.put(Format.Alpha8, new BitMaskImageCodec(1, 0, 8, 0, 0, 0, +// 0, 0, 0, 0)); + + params.put(Format.Alpha8, new ByteOffsetImageCodec(1, 0, 0, -1, -1, -1)); params.put(Format.Alpha16, new BitMaskImageCodec(2, 0, 16, 0, 0, 0, 0, 0, 0, 0)); // == LUMINANCE == - params.put(Format.Luminance8, new BitMaskImageCodec(1, FLAG_GRAY, 0, 8, 0, 0, - 0, 0, 0, 0)); +// params.put(Format.Luminance8, new BitMaskImageCodec(1, FLAG_GRAY, 0, 8, 0, 0, +// 0, 0, 0, 0)); + + params.put(Format.Luminance8, new ByteOffsetImageCodec(1, FLAG_GRAY, -1, 0, -1, -1)); + params.put(Format.Luminance16, new BitMaskImageCodec(2, FLAG_GRAY, 0, 16, 0, 0, 0, 0, 0, 0)); params.put(Format.Luminance16F, new BitMaskImageCodec(2, FLAG_GRAY | FLAG_F16, 0, 16, 0, 0, @@ -43,9 +48,11 @@ abstract class ImageCodec { // ?? // == LUMINANCA ALPHA == - params.put(Format.Luminance8Alpha8, new BitMaskImageCodec(2, FLAG_GRAY, - 8, 8, 0, 0, - 8, 0, 0, 0)); +// params.put(Format.Luminance8Alpha8, new BitMaskImageCodec(2, FLAG_GRAY, +// 8, 8, 0, 0, +// 8, 0, 0, 0)); + + params.put(Format.Luminance8Alpha8, new ByteOffsetImageCodec(2, FLAG_GRAY, 1, 0, -1, -1)); params.put(Format.Luminance16Alpha16, new BitMaskImageCodec(4, FLAG_GRAY, 16, 16, 0, 0, @@ -56,17 +63,21 @@ abstract class ImageCodec { 16, 0, 0, 0)); // == RGB == - params.put(Format.BGR8, new BitMaskImageCodec(3, 0, - 0, 8, 8, 8, - 0, 16, 8, 0)); +// params.put(Format.BGR8, new BitMaskImageCodec(3, 0, +// 0, 8, 8, 8, +// 0, 16, 8, 0)); +// + params.put(Format.BGR8, new ByteOffsetImageCodec(3, 0, -1, 2, 1, 0)); params.put(Format.RGB565, new BitMaskImageCodec(2, 0, 0, 5, 6, 5, 0, 11, 5, 0)); +// +// params.put(Format.RGB8, new BitMaskImageCodec(3, 0, +// 0, 8, 8, 8, +// 0, 0, 8, 16)); - params.put(Format.RGB8, new BitMaskImageCodec(3, 0, - 0, 8, 8, 8, - 0, 0, 8, 16)); + params.put(Format.RGB8, new ByteOffsetImageCodec(3, 0, -1, 0, 1, 2)); params.put(Format.RGB16, new ByteAlignedImageCodec(6, 0, 0, 2, 2, 2, @@ -83,11 +94,12 @@ abstract class ImageCodec { params.put(Format.RGB16F_to_RGB111110F, rgb16f); params.put(Format.RGB16F_to_RGB9E5, rgb16f); - // == RGBA == - params.put(Format.ABGR8, new BitMaskImageCodec(4, 0, - 0, 8, 8, 8, - 0, 24, 16, 8)); +// params.put(Format.ABGR8, new BitMaskImageCodec(4, 0, +// 0, 8, 8, 8, +// 0, 24, 16, 8)); + + params.put(Format.ABGR8, new ByteOffsetImageCodec(4, 0, 0, 3, 2, 1)); params.put(Format.ARGB4444, new BitMaskImageCodec(2, 0, 4, 4, 4, 4, @@ -98,14 +110,16 @@ abstract class ImageCodec { 0, 11, 6, 1)); ((BitMaskImageCodec)params.get(Format.RGB5A1)).be = true; - params.put(Format.RGBA8, new ByteAlignedImageCodec(4, 0, - 0, 1, 1, 1, - 0, 0, 1, 2)); +// params.put(Format.RGBA8, new ByteAlignedImageCodec(4, 0, +// 0, 1, 1, 1, +// 0, 0, 1, 2)); //new BitMaskImageCodec(4, 0, // 8, 8, 8, 8, // 24, 0, 8, 16)); + params.put(Format.RGBA8, new ByteOffsetImageCodec(4, 0, 3, 0, 1, 2)); + params.put(Format.RGBA16, new ByteAlignedImageCodec(8, 0, 2, 2, 2, 2, 6, 0, 2, 4));