* Problem: Slow. Solution? Speed.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9742 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Sha..rd 12 years ago
parent 47563c2cf1
commit 105d27fddf
  1. 83
      engine/src/core/com/jme3/texture/image/DefaultImageRaster.java
  2. 8
      engine/src/core/com/jme3/texture/image/ImageCodec.java

@ -36,64 +36,69 @@ public class DefaultImageRaster extends ImageRaster {
@Override
public void setPixel(int x, int y, ColorRGBA color) {
rangeCheck(x, y);
// rangeCheck(x, y);
// Check flags for grayscale
if ((codec.flags & ImageCodec.FLAG_GRAY) != 0) {
if (codec.isGray) {
float gray = color.r * 0.27f + color.g * 0.67f + color.b * 0.06f;
color = new ColorRGBA(gray, gray, gray, color.a);
}
if ((codec.flags & ImageCodec.FLAG_F16) != 0) {
components[0] = (int) FastMath.convertFloatToHalf(color.a);
components[1] = (int) FastMath.convertFloatToHalf(color.r);
components[2] = (int) FastMath.convertFloatToHalf(color.g);
components[3] = (int) FastMath.convertFloatToHalf(color.b);
} else if ((codec.flags & ImageCodec.FLAG_F32) != 0) {
components[0] = (int) Float.floatToIntBits(color.a);
components[1] = (int) Float.floatToIntBits(color.r);
components[2] = (int) Float.floatToIntBits(color.g);
components[3] = (int) Float.floatToIntBits(color.b);
} else {
// Convert color to bits by multiplying by size
components[0] = Math.min( (int) (color.a * codec.maxAlpha + 0.5f), codec.maxAlpha);
components[1] = Math.min( (int) (color.r * codec.maxRed + 0.5f), codec.maxRed);
components[2] = Math.min( (int) (color.g * codec.maxGreen + 0.5f), codec.maxGreen);
components[3] = Math.min( (int) (color.b * codec.maxBlue + 0.5f), codec.maxBlue);
switch (codec.type) {
case ImageCodec.FLAG_F16:
components[0] = (int) FastMath.convertFloatToHalf(color.a);
components[1] = (int) FastMath.convertFloatToHalf(color.r);
components[2] = (int) FastMath.convertFloatToHalf(color.g);
components[3] = (int) FastMath.convertFloatToHalf(color.b);
break;
case ImageCodec.FLAG_F32:
components[0] = (int) Float.floatToIntBits(color.a);
components[1] = (int) Float.floatToIntBits(color.r);
components[2] = (int) Float.floatToIntBits(color.g);
components[3] = (int) Float.floatToIntBits(color.b);
break;
case 0:
// Convert color to bits by multiplying by size
components[0] = Math.min( (int) (color.a * codec.maxAlpha + 0.5f), codec.maxAlpha);
components[1] = Math.min( (int) (color.r * codec.maxRed + 0.5f), codec.maxRed);
components[2] = Math.min( (int) (color.g * codec.maxGreen + 0.5f), codec.maxGreen);
components[3] = Math.min( (int) (color.b * codec.maxBlue + 0.5f), codec.maxBlue);
break;
}
codec.writeComponents(buffer, x, y, width, components, temp);
image.setUpdateNeeded();
}
@Override
public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
rangeCheck(x, y);
// rangeCheck(x, y);
codec.readComponents(buffer, x, y, width, components, temp);
if (store == null) {
store = new ColorRGBA();
}
if ((codec.flags & ImageCodec.FLAG_F16) != 0) {
store.set(FastMath.convertHalfToFloat((short)components[1]),
FastMath.convertHalfToFloat((short)components[2]),
FastMath.convertHalfToFloat((short)components[3]),
FastMath.convertHalfToFloat((short)components[0]));
} else if ((codec.flags & ImageCodec.FLAG_F32) != 0) {
store.set(Float.intBitsToFloat((int)components[1]),
Float.intBitsToFloat((int)components[2]),
Float.intBitsToFloat((int)components[3]),
Float.intBitsToFloat((int)components[0]));
} else {
// Convert to float and divide by bitsize to get into range 0.0 - 1.0.
store.set((float)components[1] / codec.maxRed,
(float)components[2] / codec.maxGreen,
(float)components[3] / codec.maxBlue,
(float)components[0] / codec.maxAlpha);
switch (codec.type) {
case ImageCodec.FLAG_F16:
store.set(FastMath.convertHalfToFloat((short)components[1]),
FastMath.convertHalfToFloat((short)components[2]),
FastMath.convertHalfToFloat((short)components[3]),
FastMath.convertHalfToFloat((short)components[0]));
break;
case ImageCodec.FLAG_F32:
store.set(Float.intBitsToFloat((int)components[1]),
Float.intBitsToFloat((int)components[2]),
Float.intBitsToFloat((int)components[3]),
Float.intBitsToFloat((int)components[0]));
break;
case 0:
// Convert to float and divide by bitsize to get into range 0.0 - 1.0.
store.set((float)components[1] / codec.maxRed,
(float)components[2] / codec.maxGreen,
(float)components[3] / codec.maxBlue,
(float)components[0] / codec.maxAlpha);
break;
}
if ((codec.flags & ImageCodec.FLAG_GRAY) != 0) {
if (codec.isGray) {
store.g = store.b = store.r;
} else {
if (codec.maxRed == 0) {

@ -7,14 +7,16 @@ import java.util.EnumMap;
abstract class ImageCodec {
public static final int FLAG_F16 = 1, FLAG_F32 = 2, FLAG_GRAY = 4, FLAG_ALPHAONLY = 8, FLAG_SHAREDEXP = 16;
public static final int FLAG_F16 = 1, FLAG_F32 = 2, FLAG_GRAY = 4; //, FLAG_ALPHAONLY = 8, FLAG_SHAREDEXP = 16;
private static final EnumMap<Image.Format, ImageCodec> params = new EnumMap<Image.Format, ImageCodec>(Image.Format.class);
protected final int bpp, flags, maxAlpha, maxRed, maxGreen, maxBlue;
protected final int bpp, type, maxAlpha, maxRed, maxGreen, maxBlue;
protected final boolean isGray;
public ImageCodec(int bpp, int flags, int maxAlpha, int maxRed, int maxGreen, int maxBlue) {
this.bpp = bpp;
this.flags = flags;
this.isGray = (flags & FLAG_GRAY) != 0;
this.type = flags & ~FLAG_GRAY;
this.maxAlpha = maxAlpha;
this.maxRed = maxRed;
this.maxGreen = maxGreen;

Loading…
Cancel
Save