Main repository of MikuMikuStudio
Revision | 15b496efa67533af071eb868adf0698cb126333f (tree) |
---|---|
Time | 2013-07-07 06:35:10 |
Author | kobayasi <kobayasi@pscn...> |
Commiter | kobayasi |
merge from jme10694
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - * Copyright (c) 2009-2010 jMonkeyEngine | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
@@ -29,12 +29,11 @@ | ||
29 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | 31 | */ |
32 | - | |
33 | 32 | package com.jme3.texture; |
34 | 33 | |
34 | +import com.jme3.export.InputCapsule; | |
35 | 35 | import com.jme3.export.JmeExporter; |
36 | 36 | import com.jme3.export.JmeImporter; |
37 | -import com.jme3.export.InputCapsule; | |
38 | 37 | import com.jme3.export.OutputCapsule; |
39 | 38 | import java.io.IOException; |
40 | 39 |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - * Copyright (c) 2009-2010 jMonkeyEngine | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
@@ -31,9 +31,9 @@ | ||
31 | 31 | */ |
32 | 32 | package com.jme3.texture; |
33 | 33 | |
34 | +import com.jme3.export.InputCapsule; | |
34 | 35 | import com.jme3.export.JmeExporter; |
35 | 36 | import com.jme3.export.JmeImporter; |
36 | -import com.jme3.export.InputCapsule; | |
37 | 37 | import com.jme3.export.OutputCapsule; |
38 | 38 | import java.io.IOException; |
39 | 39 |
@@ -0,0 +1,146 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture; | |
33 | + | |
34 | +import com.jme3.texture.Image.Format; | |
35 | +import java.util.List; | |
36 | +import java.util.logging.Level; | |
37 | +import java.util.logging.Logger; | |
38 | + | |
39 | +/** | |
40 | + * This class implements a Texture array | |
41 | + * warning, this feature is only supported on opengl 3.0 version. | |
42 | + * To check if a hardware supports TextureArray check : | |
43 | + * renderManager.getRenderer().getCaps().contains(Caps.TextureArray) | |
44 | + * @author phate666 | |
45 | + */ | |
46 | +public class TextureArray extends Texture { | |
47 | + | |
48 | + private WrapMode wrapS = WrapMode.EdgeClamp; | |
49 | + private WrapMode wrapT = WrapMode.EdgeClamp; | |
50 | + | |
51 | + /** | |
52 | + * Construct a TextureArray | |
53 | + * warning, this feature is only supported on opengl 3.0 version. | |
54 | + * To check if a hardware supports TextureArray check : | |
55 | + * renderManager.getRenderer().getCaps().contains(Caps.TextureArray) | |
56 | + */ | |
57 | + public TextureArray() { | |
58 | + super(); | |
59 | + } | |
60 | + | |
61 | + /** | |
62 | + * Construct a TextureArray from the given list of images. | |
63 | + * To check if a hardware supports TextureArray check : | |
64 | + * renderManager.getRenderer().getCaps().contains(Caps.TextureArray) | |
65 | + * @param images | |
66 | + */ | |
67 | + public TextureArray(List<Image> images) { | |
68 | + super(); | |
69 | + | |
70 | + int width = images.get(0).getWidth(); | |
71 | + int height = images.get(0).getHeight(); | |
72 | + Format format = images.get(0).getFormat(); | |
73 | + Image arrayImage = new Image(format, width, height, null); | |
74 | + | |
75 | + for (Image img : images) { | |
76 | + if (img.getHeight() != height || img.getWidth() != width) { | |
77 | + throw new IllegalArgumentException("Images in texture array must have same dimensions"); | |
78 | + } | |
79 | + if (img.getFormat() != format) { | |
80 | + throw new IllegalArgumentException("Images in texture array must have same format"); | |
81 | + } | |
82 | + | |
83 | + arrayImage.addData(img.getData(0)); | |
84 | + } | |
85 | + setImage(arrayImage); | |
86 | + } | |
87 | + | |
88 | + @Override | |
89 | + public Texture createSimpleClone() { | |
90 | + TextureArray clone = new TextureArray(); | |
91 | + createSimpleClone(clone); | |
92 | + return clone; | |
93 | + } | |
94 | + | |
95 | + @Override | |
96 | + public Texture createSimpleClone(Texture rVal) { | |
97 | + rVal.setWrap(WrapAxis.S, wrapS); | |
98 | + rVal.setWrap(WrapAxis.T, wrapT); | |
99 | + return super.createSimpleClone(rVal); | |
100 | + } | |
101 | + | |
102 | + @Override | |
103 | + public Type getType() { | |
104 | + return Type.TwoDimensionalArray; | |
105 | + } | |
106 | + | |
107 | + @Override | |
108 | + public WrapMode getWrap(WrapAxis axis) { | |
109 | + switch (axis) { | |
110 | + case S: | |
111 | + return wrapS; | |
112 | + case T: | |
113 | + return wrapT; | |
114 | + default: | |
115 | + throw new IllegalArgumentException("invalid WrapAxis: " + axis); | |
116 | + } | |
117 | + } | |
118 | + | |
119 | + @Override | |
120 | + public void setWrap(WrapAxis axis, WrapMode mode) { | |
121 | + if (mode == null) { | |
122 | + throw new IllegalArgumentException("mode can not be null."); | |
123 | + } else if (axis == null) { | |
124 | + throw new IllegalArgumentException("axis can not be null."); | |
125 | + } | |
126 | + switch (axis) { | |
127 | + case S: | |
128 | + this.wrapS = mode; | |
129 | + break; | |
130 | + case T: | |
131 | + this.wrapT = mode; | |
132 | + break; | |
133 | + default: | |
134 | + throw new IllegalArgumentException("Not applicable for 2D textures"); | |
135 | + } | |
136 | + } | |
137 | + | |
138 | + @Override | |
139 | + public void setWrap(WrapMode mode) { | |
140 | + if (mode == null) { | |
141 | + throw new IllegalArgumentException("mode can not be null."); | |
142 | + } | |
143 | + this.wrapS = mode; | |
144 | + this.wrapT = mode; | |
145 | + } | |
146 | +} | |
\ No newline at end of file |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - * Copyright (c) 2009-2010 jMonkeyEngine | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
@@ -29,14 +29,15 @@ | ||
29 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | 31 | */ |
32 | - | |
33 | 32 | package com.jme3.texture; |
34 | 33 | |
34 | +import com.jme3.export.InputCapsule; | |
35 | 35 | import com.jme3.export.JmeExporter; |
36 | 36 | import com.jme3.export.JmeImporter; |
37 | -import com.jme3.export.InputCapsule; | |
38 | 37 | import com.jme3.export.OutputCapsule; |
39 | 38 | import java.io.IOException; |
39 | +import java.nio.ByteBuffer; | |
40 | +import java.util.ArrayList; | |
40 | 41 | |
41 | 42 | /** |
42 | 43 | * Describes a cubemap texture. |
@@ -63,9 +64,10 @@ public class TextureCubeMap extends Texture { | ||
63 | 64 | * Face of the Cubemap as described by its directional offset from the |
64 | 65 | * origin. |
65 | 66 | */ |
66 | -// public enum Face { | |
67 | -// PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ; | |
68 | -// } | |
67 | + public enum Face { | |
68 | + | |
69 | + PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ; | |
70 | + } | |
69 | 71 | |
70 | 72 | public TextureCubeMap(){ |
71 | 73 | super(); |
@@ -75,6 +77,20 @@ public class TextureCubeMap extends Texture { | ||
75 | 77 | super(); |
76 | 78 | setImage(img); |
77 | 79 | } |
80 | + | |
81 | + public TextureCubeMap(int width, int height, Image.Format format){ | |
82 | + this(createEmptyLayeredImage(width, height, 6, format)); | |
83 | + } | |
84 | + | |
85 | + private static Image createEmptyLayeredImage(int width, int height, | |
86 | + int layerCount, Image.Format format) { | |
87 | + ArrayList<ByteBuffer> layers = new ArrayList<ByteBuffer>(); | |
88 | + for(int i = 0; i < layerCount; i++) { | |
89 | + layers.add(null); | |
90 | + } | |
91 | + Image image = new Image(format, width, height, 0, layers); | |
92 | + return image; | |
93 | + } | |
78 | 94 | |
79 | 95 | public Texture createSimpleClone() { |
80 | 96 | return createSimpleClone(new TextureCubeMap()); |
@@ -0,0 +1,80 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture; | |
33 | + | |
34 | +import com.jme3.asset.AssetKey; | |
35 | +import com.jme3.asset.AssetProcessor; | |
36 | +import com.jme3.asset.TextureKey; | |
37 | +import java.nio.ByteBuffer; | |
38 | + | |
39 | +public class TextureProcessor implements AssetProcessor { | |
40 | + | |
41 | + public Object postProcess(AssetKey key, Object obj) { | |
42 | + TextureKey texKey = (TextureKey) key; | |
43 | + Image img = (Image) obj; | |
44 | + if (img == null) { | |
45 | + return null; | |
46 | + } | |
47 | + | |
48 | + Texture tex; | |
49 | + if (texKey.isAsCube()) { | |
50 | + if (texKey.isFlipY()) { | |
51 | + // also flip -y and +y image in cubemap | |
52 | + ByteBuffer pos_y = img.getData(2); | |
53 | + img.setData(2, img.getData(3)); | |
54 | + img.setData(3, pos_y); | |
55 | + } | |
56 | + tex = new TextureCubeMap(); | |
57 | + } else if (texKey.isAsTexture3D()) { | |
58 | + tex = new Texture3D(); | |
59 | + } else { | |
60 | + tex = new Texture2D(); | |
61 | + } | |
62 | + | |
63 | + // enable mipmaps if image has them | |
64 | + // or generate them if requested by user | |
65 | + if (img.hasMipmaps() || texKey.isGenerateMips()) { | |
66 | + tex.setMinFilter(Texture.MinFilter.Trilinear); | |
67 | + } | |
68 | + | |
69 | + tex.setAnisotropicFilter(texKey.getAnisotropy()); | |
70 | + tex.setName(texKey.getName()); | |
71 | + tex.setImage(img); | |
72 | + return tex; | |
73 | + } | |
74 | + | |
75 | + public Object createClone(Object obj) { | |
76 | + Texture tex = (Texture) obj; | |
77 | + return tex.clone(); | |
78 | + } | |
79 | + | |
80 | +} |
@@ -0,0 +1,118 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import java.nio.ByteBuffer; | |
35 | + | |
36 | +class BitMaskImageCodec extends ImageCodec { | |
37 | + | |
38 | + // Shifts | |
39 | + final int as, rs, gs, bs; | |
40 | + boolean be = false; | |
41 | + | |
42 | + public BitMaskImageCodec(int bpp, int flags, int ac, int rc, int gc, int bc, int as, int rs, int gs, int bs) { | |
43 | + super(bpp, flags, | |
44 | + (int) (((long) 1 << ac) - 1), | |
45 | + (int) (((long) 1 << rc) - 1), | |
46 | + (int) (((long) 1 << gc) - 1), | |
47 | + (int) (((long) 1 << bc) - 1)); | |
48 | + | |
49 | + if (bpp > 4) { | |
50 | + throw new UnsupportedOperationException("Use ByteAlignedImageCodec for codecs with pixel sizes larger than 4 bytes"); | |
51 | + } | |
52 | + | |
53 | + this.as = as; | |
54 | + this.rs = rs; | |
55 | + this.gs = gs; | |
56 | + this.bs = bs; | |
57 | + } | |
58 | + | |
59 | + private static int readPixelRaw(ByteBuffer buf, int idx, int bpp) { | |
60 | + //idx += bpp; | |
61 | + //int original = buf.get(--idx) & 0xff; | |
62 | + //while ((--bpp) > 0) { | |
63 | + // original = (original << 8) | (buf.get(--idx) & 0xff); | |
64 | + //} | |
65 | + //return original; | |
66 | + //return buf.getInt(idx) & (0xFFFFFFFF >>> (32 - bpp)); | |
67 | + int pixel = 0; | |
68 | + buf.position(idx); | |
69 | + for (int i = 0; i < bpp; i++) { | |
70 | + pixel = pixel | (buf.get() & 0xff) << (i * 8); | |
71 | + } | |
72 | + return pixel; | |
73 | + } | |
74 | + | |
75 | + private void writePixelRaw(ByteBuffer buf, int idx, int pixel, int bpp){ | |
76 | +// buf.position(idx); | |
77 | +// if (!be){ | |
78 | + // This works: | |
79 | +// while ((--bpp) >= 0){ | |
80 | +// byte bt = (byte) ((pixel >> (bpp * 8)) & 0xff); | |
81 | +// buf.put(idx + bpp, bt); | |
82 | +// } | |
83 | + // == | |
84 | +// } else { | |
85 | +// for (int i = bpp - 1; i >= 0; i--) { | |
86 | +// byte bt = (byte) ((pixel >> (i * 8)) & 0xff); | |
87 | +// buf.put(idx + i, bt); | |
88 | +// } | |
89 | +// } | |
90 | + | |
91 | + buf.position(idx); | |
92 | + for (int i = 0; i < bpp; i++) { | |
93 | + buf.put( (byte)((pixel >> (8 * i)) & 0xff) ); | |
94 | + } | |
95 | + } | |
96 | + | |
97 | + @Override | |
98 | + public void readComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
99 | + int inputPixel = readPixelRaw(buf, (x + y * width) * bpp, bpp); | |
100 | + components[0] = (inputPixel >> as) & maxAlpha; | |
101 | + components[1] = (inputPixel >> rs) & maxRed; | |
102 | + components[2] = (inputPixel >> gs) & maxGreen; | |
103 | + components[3] = (inputPixel >> bs) & maxBlue; | |
104 | + } | |
105 | + | |
106 | + public void writeComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
107 | + // Shift components then mask them | |
108 | + // Map all components into a single bitspace | |
109 | + int outputPixel = ((components[0] & maxAlpha) << as) | |
110 | + | ((components[1] & maxRed) << rs) | |
111 | + | ((components[2] & maxGreen) << gs) | |
112 | + | ((components[3] & maxBlue) << bs); | |
113 | + | |
114 | + // Find index in image where to write pixel. | |
115 | + // Write the resultant bitspace into the pixel. | |
116 | + writePixelRaw(buf, (x + y * width) * bpp, outputPixel, bpp); | |
117 | + } | |
118 | +} |
@@ -0,0 +1,127 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import java.nio.ByteBuffer; | |
35 | + | |
36 | +class ByteAlignedImageCodec extends ImageCodec { | |
37 | + | |
38 | + private final int ap, az, rp, rz, gp, gz, bp, bz; | |
39 | + boolean be; | |
40 | + | |
41 | + public ByteAlignedImageCodec(int bpp, int flags, int az, int rz, int gz, int bz, int ap, int rp, int gp, int bp) { | |
42 | + // Cast to long to compute max vals, since some components could be as high as 32 bits. | |
43 | + super(bpp, flags, | |
44 | + (int)(((long)1 << (az << 3)) - 1), | |
45 | + (int)(((long)1 << (rz << 3)) - 1), | |
46 | + (int)(((long)1 << (gz << 3)) - 1), | |
47 | + (int)(((long)1 << (bz << 3)) - 1)); | |
48 | + | |
49 | + this.ap = ap; | |
50 | + this.az = az; | |
51 | + this.rp = rp; | |
52 | + this.rz = rz; | |
53 | + | |
54 | + this.gp = gp; | |
55 | + this.gz = gz; | |
56 | + this.bp = bp; | |
57 | + this.bz = bz; | |
58 | + } | |
59 | + | |
60 | + private static void readPixelRaw(ByteBuffer buf, int idx, int bpp, byte[] result) { | |
61 | + buf.position(idx); | |
62 | + buf.get(result, 0, bpp); | |
63 | + } | |
64 | + | |
65 | + private static void writePixelRaw(ByteBuffer buf, int idx, byte[] pixel, int bpp) { | |
66 | +// try { | |
67 | + buf.position(idx); | |
68 | + buf.put(pixel, 0, bpp); | |
69 | +// } catch (IndexOutOfBoundsException ex) { | |
70 | +// System.out.println("!"); | |
71 | +// } | |
72 | + } | |
73 | + | |
74 | + private static int readComponent(byte[] encoded, int position, int size) { | |
75 | +// int component = encoded[position] & 0xff; | |
76 | +// while ((--size) > 0){ | |
77 | +// component = (component << 8) | (encoded[++position] & 0xff); | |
78 | +// } | |
79 | +// return component; | |
80 | + try { | |
81 | + int component = 0; | |
82 | + for (int i = size - 1; i >= 0; i--) { | |
83 | + component = (component << 8) | (encoded[position + i] & 0xff); | |
84 | + } | |
85 | + return component; | |
86 | +// position += size - 1; | |
87 | +// | |
88 | +// while ((--size) >= 0) { | |
89 | +// component = (component << 8) | (encoded[position--] & 0xff); | |
90 | +// } | |
91 | +// return component; | |
92 | + } catch (ArrayIndexOutOfBoundsException ex){ | |
93 | + ex.printStackTrace(); | |
94 | + return 0; | |
95 | + } | |
96 | + } | |
97 | + | |
98 | + private void writeComponent(int component, int position, int size, byte[] result) { | |
99 | +// if (!be) { | |
100 | +// while ((--size) >= 0){ | |
101 | +// byte bt = (byte) ((component >> (size * 8)) & 0xff); | |
102 | +// result[position++] = bt; | |
103 | +// } | |
104 | +// } else { | |
105 | + for (int i = 0; i < size; i++) { | |
106 | + byte bt = (byte) ((component >> (i * 8)) & 0xff); | |
107 | + result[position++] = bt; | |
108 | + } | |
109 | +// } | |
110 | + } | |
111 | + | |
112 | + public void readComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
113 | + readPixelRaw(buf, (x + y * width) * bpp, bpp, tmp); | |
114 | + components[0] = readComponent(tmp, ap, az); | |
115 | + components[1] = readComponent(tmp, rp, rz); | |
116 | + components[2] = readComponent(tmp, gp, gz); | |
117 | + components[3] = readComponent(tmp, bp, bz); | |
118 | + } | |
119 | + | |
120 | + public void writeComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
121 | + writeComponent(components[0], ap, az, tmp); | |
122 | + writeComponent(components[1], rp, rz, tmp); | |
123 | + writeComponent(components[2], gp, gz, tmp); | |
124 | + writeComponent(components[3], bp, bz, tmp); | |
125 | + writePixelRaw(buf, (x + y * width) * bpp, tmp, bpp); | |
126 | + } | |
127 | +} |
@@ -0,0 +1,89 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import java.nio.ByteBuffer; | |
35 | + | |
36 | +public class ByteOffsetImageCodec extends ImageCodec { | |
37 | + | |
38 | + private int redPos, greenPos, bluePos, alphaPos; | |
39 | + | |
40 | + public ByteOffsetImageCodec(int bpp, int flags, int alphaPos, int redPos, int greenPos, int bluePos) { | |
41 | + super(bpp, flags, alphaPos != -1 ? 255 : 0, | |
42 | + redPos != -1 ? 255 : 0, | |
43 | + greenPos != -1 ? 255 : 0, | |
44 | + bluePos != -1 ? 255 : 0); | |
45 | + this.alphaPos = alphaPos; | |
46 | + this.redPos = redPos; | |
47 | + this.greenPos = greenPos; | |
48 | + this.bluePos = bluePos; | |
49 | + } | |
50 | + | |
51 | + @Override | |
52 | + public void readComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
53 | + int i = (y * width + x) * bpp; | |
54 | + buf.position(i); | |
55 | + buf.get(tmp, 0, bpp); | |
56 | + if (alphaPos != -1) { | |
57 | + components[0] = tmp[alphaPos] & 0xff; | |
58 | + } | |
59 | + if (redPos != -1) { | |
60 | + components[1] = tmp[redPos] & 0xff; | |
61 | + } | |
62 | + if (greenPos != -1) { | |
63 | + components[2] = tmp[greenPos] & 0xff; | |
64 | + } | |
65 | + if (bluePos != -1) { | |
66 | + components[3] = tmp[bluePos] & 0xff; | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + public void writeComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp) { | |
72 | + int i = (y * width + x) * bpp; | |
73 | + if (alphaPos != -1) { | |
74 | + tmp[alphaPos] = (byte) components[0]; | |
75 | + } | |
76 | + if (redPos != -1) { | |
77 | + tmp[redPos] = (byte) components[1]; | |
78 | + } | |
79 | + if (greenPos != -1) { | |
80 | + tmp[greenPos] = (byte) components[2]; | |
81 | + } | |
82 | + if (bluePos != -1) { | |
83 | + tmp[bluePos] = (byte) components[3]; | |
84 | + } | |
85 | + buf.position(i); | |
86 | + buf.put(tmp, 0, bpp); | |
87 | + } | |
88 | + | |
89 | +} |
@@ -0,0 +1,169 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import com.jme3.math.ColorRGBA; | |
35 | +import com.jme3.math.FastMath; | |
36 | +import com.jme3.texture.Image; | |
37 | +import java.nio.ByteBuffer; | |
38 | + | |
39 | +public class DefaultImageRaster extends ImageRaster { | |
40 | + | |
41 | + private final int[] components = new int[4]; | |
42 | + private ByteBuffer buffer; | |
43 | + private final Image image; | |
44 | + private final ImageCodec codec; | |
45 | + private final int width; | |
46 | + private final int height; | |
47 | + private final byte[] temp; | |
48 | + private int slice; | |
49 | + | |
50 | + private void rangeCheck(int x, int y) { | |
51 | + if (x < 0 || y < 0 || x >= width || y >= height) { | |
52 | + throw new IllegalArgumentException("x and y must be inside the image dimensions"); | |
53 | + } | |
54 | + } | |
55 | + | |
56 | + public DefaultImageRaster(Image image, int slice) { | |
57 | + this.image = image; | |
58 | + this.slice = slice; | |
59 | + this.buffer = image.getData(slice); | |
60 | + this.codec = ImageCodec.lookup(image.getFormat()); | |
61 | + this.width = image.getWidth(); | |
62 | + this.height = image.getHeight(); | |
63 | + if (codec instanceof ByteAlignedImageCodec || codec instanceof ByteOffsetImageCodec) { | |
64 | + this.temp = new byte[codec.bpp]; | |
65 | + } else { | |
66 | + this.temp = null; | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + public int getWidth() { | |
72 | + return width; | |
73 | + } | |
74 | + | |
75 | + @Override | |
76 | + public int getHeight() { | |
77 | + return height; | |
78 | + } | |
79 | + | |
80 | + @Override | |
81 | + public void setPixel(int x, int y, ColorRGBA color) { | |
82 | + rangeCheck(x, y); | |
83 | + | |
84 | + // Check flags for grayscale | |
85 | + if (codec.isGray) { | |
86 | + float gray = color.r * 0.27f + color.g * 0.67f + color.b * 0.06f; | |
87 | + color = new ColorRGBA(gray, gray, gray, color.a); | |
88 | + } | |
89 | + | |
90 | + switch (codec.type) { | |
91 | + case ImageCodec.FLAG_F16: | |
92 | + components[0] = (int) FastMath.convertFloatToHalf(color.a); | |
93 | + components[1] = (int) FastMath.convertFloatToHalf(color.r); | |
94 | + components[2] = (int) FastMath.convertFloatToHalf(color.g); | |
95 | + components[3] = (int) FastMath.convertFloatToHalf(color.b); | |
96 | + break; | |
97 | + case ImageCodec.FLAG_F32: | |
98 | + components[0] = (int) Float.floatToIntBits(color.a); | |
99 | + components[1] = (int) Float.floatToIntBits(color.r); | |
100 | + components[2] = (int) Float.floatToIntBits(color.g); | |
101 | + components[3] = (int) Float.floatToIntBits(color.b); | |
102 | + break; | |
103 | + case 0: | |
104 | + // Convert color to bits by multiplying by size | |
105 | + components[0] = Math.min( (int) (color.a * codec.maxAlpha + 0.5f), codec.maxAlpha); | |
106 | + components[1] = Math.min( (int) (color.r * codec.maxRed + 0.5f), codec.maxRed); | |
107 | + components[2] = Math.min( (int) (color.g * codec.maxGreen + 0.5f), codec.maxGreen); | |
108 | + components[3] = Math.min( (int) (color.b * codec.maxBlue + 0.5f), codec.maxBlue); | |
109 | + break; | |
110 | + } | |
111 | + codec.writeComponents(getBuffer(), x, y, width, components, temp); | |
112 | + image.setUpdateNeeded(); | |
113 | + } | |
114 | + | |
115 | + private ByteBuffer getBuffer(){ | |
116 | + if(buffer == null){ | |
117 | + this.buffer = image.getData(slice); | |
118 | + } | |
119 | + return buffer; | |
120 | + } | |
121 | + | |
122 | + @Override | |
123 | + public ColorRGBA getPixel(int x, int y, ColorRGBA store) { | |
124 | + rangeCheck(x, y); | |
125 | + | |
126 | + codec.readComponents(getBuffer(), x, y, width, components, temp); | |
127 | + if (store == null) { | |
128 | + store = new ColorRGBA(); | |
129 | + } | |
130 | + switch (codec.type) { | |
131 | + case ImageCodec.FLAG_F16: | |
132 | + store.set(FastMath.convertHalfToFloat((short)components[1]), | |
133 | + FastMath.convertHalfToFloat((short)components[2]), | |
134 | + FastMath.convertHalfToFloat((short)components[3]), | |
135 | + FastMath.convertHalfToFloat((short)components[0])); | |
136 | + break; | |
137 | + case ImageCodec.FLAG_F32: | |
138 | + store.set(Float.intBitsToFloat((int)components[1]), | |
139 | + Float.intBitsToFloat((int)components[2]), | |
140 | + Float.intBitsToFloat((int)components[3]), | |
141 | + Float.intBitsToFloat((int)components[0])); | |
142 | + break; | |
143 | + case 0: | |
144 | + // Convert to float and divide by bitsize to get into range 0.0 - 1.0. | |
145 | + store.set((float)components[1] / codec.maxRed, | |
146 | + (float)components[2] / codec.maxGreen, | |
147 | + (float)components[3] / codec.maxBlue, | |
148 | + (float)components[0] / codec.maxAlpha); | |
149 | + break; | |
150 | + } | |
151 | + if (codec.isGray) { | |
152 | + store.g = store.b = store.r; | |
153 | + } else { | |
154 | + if (codec.maxRed == 0) { | |
155 | + store.r = 1; | |
156 | + } | |
157 | + if (codec.maxGreen == 0) { | |
158 | + store.g = 1; | |
159 | + } | |
160 | + if (codec.maxBlue == 0) { | |
161 | + store.b = 1; | |
162 | + } | |
163 | + if (codec.maxAlpha == 0) { | |
164 | + store.a = 1; | |
165 | + } | |
166 | + } | |
167 | + return store; | |
168 | + } | |
169 | +} |
@@ -0,0 +1,187 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import com.jme3.texture.Image; | |
35 | +import com.jme3.texture.Image.Format; | |
36 | +import java.nio.ByteBuffer; | |
37 | +import java.util.EnumMap; | |
38 | + | |
39 | +abstract class ImageCodec { | |
40 | + | |
41 | + public static final int FLAG_F16 = 1, FLAG_F32 = 2, FLAG_GRAY = 4; //, FLAG_ALPHAONLY = 8, FLAG_SHAREDEXP = 16; | |
42 | + private static final EnumMap<Image.Format, ImageCodec> params = new EnumMap<Image.Format, ImageCodec>(Image.Format.class); | |
43 | + | |
44 | + protected final int bpp, type, maxAlpha, maxRed, maxGreen, maxBlue; | |
45 | + protected final boolean isGray; | |
46 | + | |
47 | + public ImageCodec(int bpp, int flags, int maxAlpha, int maxRed, int maxGreen, int maxBlue) { | |
48 | + this.bpp = bpp; | |
49 | + this.isGray = (flags & FLAG_GRAY) != 0; | |
50 | + this.type = flags & ~FLAG_GRAY; | |
51 | + this.maxAlpha = maxAlpha; | |
52 | + this.maxRed = maxRed; | |
53 | + this.maxGreen = maxGreen; | |
54 | + this.maxBlue = maxBlue; | |
55 | + } | |
56 | + | |
57 | + static { | |
58 | + // == ALPHA == | |
59 | +// params.put(Format.Alpha8, new BitMaskImageCodec(1, 0, 8, 0, 0, 0, | |
60 | +// 0, 0, 0, 0)); | |
61 | + | |
62 | + params.put(Format.Alpha8, new ByteOffsetImageCodec(1, 0, 0, -1, -1, -1)); | |
63 | + | |
64 | + params.put(Format.Alpha16, new BitMaskImageCodec(2, 0, 16, 0, 0, 0, | |
65 | + 0, 0, 0, 0)); | |
66 | + | |
67 | + // == LUMINANCE == | |
68 | +// params.put(Format.Luminance8, new BitMaskImageCodec(1, FLAG_GRAY, 0, 8, 0, 0, | |
69 | +// 0, 0, 0, 0)); | |
70 | + | |
71 | + params.put(Format.Luminance8, new ByteOffsetImageCodec(1, FLAG_GRAY, -1, 0, -1, -1)); | |
72 | + | |
73 | + params.put(Format.Luminance16, new BitMaskImageCodec(2, FLAG_GRAY, 0, 16, 0, 0, | |
74 | + 0, 0, 0, 0)); | |
75 | + params.put(Format.Luminance16F, new BitMaskImageCodec(2, FLAG_GRAY | FLAG_F16, 0, 16, 0, 0, | |
76 | + 0, 0, 0, 0)); | |
77 | + params.put(Format.Luminance32F, new BitMaskImageCodec(4, FLAG_GRAY | FLAG_F32, 0, 32, 0, 0, | |
78 | + 0, 0, 0, 0)); | |
79 | + | |
80 | + // == INTENSITY == | |
81 | + // ?? | |
82 | + | |
83 | + // == LUMINANCA ALPHA == | |
84 | +// params.put(Format.Luminance8Alpha8, new BitMaskImageCodec(2, FLAG_GRAY, | |
85 | +// 8, 8, 0, 0, | |
86 | +// 8, 0, 0, 0)); | |
87 | + | |
88 | + params.put(Format.Luminance8Alpha8, new ByteOffsetImageCodec(2, FLAG_GRAY, 1, 0, -1, -1)); | |
89 | + | |
90 | + params.put(Format.Luminance16Alpha16, new BitMaskImageCodec(4, FLAG_GRAY, | |
91 | + 16, 16, 0, 0, | |
92 | + 16, 0, 0, 0)); | |
93 | + | |
94 | + params.put(Format.Luminance16FAlpha16F, new BitMaskImageCodec(4, FLAG_GRAY | FLAG_F16, | |
95 | + 16, 16, 0, 0, | |
96 | + 16, 0, 0, 0)); | |
97 | + | |
98 | + // == RGB == | |
99 | +// params.put(Format.BGR8, new BitMaskImageCodec(3, 0, | |
100 | +// 0, 8, 8, 8, | |
101 | +// 0, 16, 8, 0)); | |
102 | +// | |
103 | + params.put(Format.BGR8, new ByteOffsetImageCodec(3, 0, -1, 2, 1, 0)); | |
104 | + | |
105 | + params.put(Format.RGB565, new BitMaskImageCodec(2, 0, | |
106 | + 0, 5, 6, 5, | |
107 | + 0, 11, 5, 0)); | |
108 | +// | |
109 | +// params.put(Format.RGB8, new BitMaskImageCodec(3, 0, | |
110 | +// 0, 8, 8, 8, | |
111 | +// 0, 0, 8, 16)); | |
112 | + | |
113 | + params.put(Format.RGB8, new ByteOffsetImageCodec(3, 0, -1, 0, 1, 2)); | |
114 | + | |
115 | + params.put(Format.RGB16, new ByteAlignedImageCodec(6, 0, | |
116 | + 0, 2, 2, 2, | |
117 | + 0, 0, 2, 4)); | |
118 | + | |
119 | + params.put(Format.RGB32F, new ByteAlignedImageCodec(12, FLAG_F32, | |
120 | + 0, 4, 4, 4, | |
121 | + 0, 0, 4, 8)); | |
122 | + | |
123 | + ByteAlignedImageCodec rgb16f = new ByteAlignedImageCodec(6, FLAG_F16, | |
124 | + 0, 2, 2, 2, | |
125 | + 0, 0, 2, 4); | |
126 | + params.put(Format.RGB16F, rgb16f); | |
127 | + params.put(Format.RGB16F_to_RGB111110F, rgb16f); | |
128 | + params.put(Format.RGB16F_to_RGB9E5, rgb16f); | |
129 | + | |
130 | + // == RGBA == | |
131 | +// params.put(Format.ABGR8, new BitMaskImageCodec(4, 0, | |
132 | +// 0, 8, 8, 8, | |
133 | +// 0, 24, 16, 8)); | |
134 | + | |
135 | + params.put(Format.ABGR8, new ByteOffsetImageCodec(4, 0, 0, 3, 2, 1)); | |
136 | + | |
137 | + params.put(Format.ARGB4444, new BitMaskImageCodec(2, 0, | |
138 | + 4, 4, 4, 4, | |
139 | + 12, 0, 4, 8)); | |
140 | + | |
141 | + params.put(Format.RGB5A1, new BitMaskImageCodec(2, 0, | |
142 | + 1, 5, 5, 5, | |
143 | + 0, 11, 6, 1)); | |
144 | + ((BitMaskImageCodec)params.get(Format.RGB5A1)).be = true; | |
145 | + | |
146 | +// params.put(Format.RGBA8, new ByteAlignedImageCodec(4, 0, | |
147 | +// 0, 1, 1, 1, | |
148 | +// 0, 0, 1, 2)); | |
149 | + | |
150 | + //new BitMaskImageCodec(4, 0, | |
151 | + // 8, 8, 8, 8, | |
152 | + // 24, 0, 8, 16)); | |
153 | + | |
154 | + params.put(Format.RGBA8, new ByteOffsetImageCodec(4, 0, 3, 0, 1, 2)); | |
155 | + | |
156 | + params.put(Format.RGBA16, new ByteAlignedImageCodec(8, 0, | |
157 | + 2, 2, 2, 2, | |
158 | + 6, 0, 2, 4)); | |
159 | + | |
160 | + params.put(Format.RGBA16F, new ByteAlignedImageCodec(8, FLAG_F16, | |
161 | + 2, 2, 2, 2, | |
162 | + 6, 0, 2, 4)); | |
163 | + | |
164 | + params.put(Format.RGBA32F, new ByteAlignedImageCodec(16, FLAG_F32, | |
165 | + 4, 4, 4, 4, | |
166 | + 12, 0, 4, 8)); | |
167 | + } | |
168 | + | |
169 | + public abstract void readComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp); | |
170 | + | |
171 | + public abstract void writeComponents(ByteBuffer buf, int x, int y, int width, int[] components, byte[] tmp); | |
172 | + | |
173 | + /** | |
174 | + * Looks up the format in the codec registry. | |
175 | + * The codec will be able to decode the given format. | |
176 | + * | |
177 | + * @param format The format to lookup. | |
178 | + * @return The codec capable of decoding it, or null if not found. | |
179 | + */ | |
180 | + public static ImageCodec lookup(Format format) { | |
181 | + ImageCodec codec = params.get(format); | |
182 | + if (codec == null) { | |
183 | + throw new UnsupportedOperationException("The format " + format + " is not supported"); | |
184 | + } | |
185 | + return codec; | |
186 | + } | |
187 | +} |
@@ -0,0 +1,185 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2012 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.texture.image; | |
33 | + | |
34 | +import com.jme3.math.ColorRGBA; | |
35 | +import com.jme3.system.JmeSystem; | |
36 | +import com.jme3.texture.Image; | |
37 | + | |
38 | +/** | |
39 | + * Utility class for reading and writing from jME3 {@link Image images}. | |
40 | + * <br> | |
41 | + * Allows directly manipulating pixels of the image by writing and | |
42 | + * reading {@link ColorRGBA colors} at any coordinate, without | |
43 | + * regard to the underlying {@link Image.Format format} of the image. | |
44 | + * NOTE: compressed and depth formats are <strong>not supported</strong>. | |
45 | + * Special RGB formats like RGB111110F and RGB9E5 are not supported | |
46 | + * at the moment, but may be added later on. For now | |
47 | + * use RGB16F_to_RGB111110F and RGB16F_to_RGB9E5 to handle | |
48 | + * the conversion on the GPU. | |
49 | + * <p> | |
50 | + * If direct manipulations are done to the image, such as replacing | |
51 | + * the image data, or changing the width, height, or format, then | |
52 | + * all current instances of <code>ImageReadWrite</code> become invalid, and | |
53 | + * new instances must be created in order to properly access | |
54 | + * the image data. | |
55 | + * | |
56 | + * Usage example:<br> | |
57 | + * <code> | |
58 | + * Image myImage = ... | |
59 | + * ImageRaster raster = ImageRaster.create(myImage); | |
60 | + * raster.setPixel(1, 5, ColorRGBA.Green); | |
61 | + * System.out.println( raster.getPixel(1, 5) ); // Will print [0.0, 1.0, 0.0, 1.0]. | |
62 | + * </code> | |
63 | + * | |
64 | + * @author Kirill Vainer | |
65 | + */ | |
66 | +public abstract class ImageRaster { | |
67 | + | |
68 | + /** | |
69 | + * Create new image reader / writer. | |
70 | + * | |
71 | + * @param image The image to read / write to. | |
72 | + * @param slice Which slice to use. Only applies to 3D images, 2D image | |
73 | + * arrays or cubemaps. | |
74 | + */ | |
75 | + public static ImageRaster create(Image image, int slices) { | |
76 | + // TODO: need implement JmeSystem::createImageRaster | |
77 | + return new DefaultImageRaster(image, slices); //JmeSystem.createImageRaster(image, slices); | |
78 | + } | |
79 | + | |
80 | + /** | |
81 | + * Create new image reader / writer for 2D images. | |
82 | + * | |
83 | + * @param image The image to read / write to. | |
84 | + */ | |
85 | + public static ImageRaster create(Image image) { | |
86 | + if (image.getData().size() > 1) { | |
87 | + throw new IllegalStateException("Use constructor that takes slices argument to read from multislice image"); | |
88 | + } | |
89 | + // TODO: need implement JmeSystem::createImageRaster | |
90 | + return new DefaultImageRaster(image, 0);//JmeSystem.createImageRaster(image, 0); | |
91 | + } | |
92 | + | |
93 | + public ImageRaster() { | |
94 | + } | |
95 | + | |
96 | + /** | |
97 | + * Returns the pixel width of the underlying image. | |
98 | + * | |
99 | + * @return the pixel width of the underlying image. | |
100 | + */ | |
101 | + public abstract int getWidth(); | |
102 | + | |
103 | + /** | |
104 | + * Returns the pixel height of the underlying image. | |
105 | + * | |
106 | + * @return the pixel height of the underlying image. | |
107 | + */ | |
108 | + public abstract int getHeight(); | |
109 | + | |
110 | + /** | |
111 | + * Sets the pixel at the given coordinate to the given color. | |
112 | + * <p> | |
113 | + * For all integer based formats (those not ending in "F"), the | |
114 | + * color is first clamped to 0.0 - 1.0 before converting it to | |
115 | + * an integer to avoid overflow. For floating point based formats, | |
116 | + * components larger than 1.0 can be represented, but components | |
117 | + * lower than 0.0 are still not allowed (as all formats are unsigned). | |
118 | + * <p> | |
119 | + * If the underlying format is grayscale (e.g. one of the luminance formats, | |
120 | + * such as {@link Image.Format#Luminance8}) then a color to grayscale | |
121 | + * conversion is done first, before writing the result into the image. | |
122 | + * <p> | |
123 | + * If the image does not have some of the components in the color (such | |
124 | + * as alpha, or any of the color components), then these components | |
125 | + * will be ignored. The only exception to this is luminance formats | |
126 | + * for which the color is converted to luminance first (see above). | |
127 | + * <p> | |
128 | + * After writing the color, the image shall be marked as requiring an | |
129 | + * update. The next time it is used for rendering, all pixel changes | |
130 | + * will be reflected when the image is rendered. | |
131 | + * | |
132 | + * @param x The x coordinate, from 0 to width - 1. | |
133 | + * @param y The y coordinate, from 0 to height - 1. | |
134 | + * @param color The color to write. | |
135 | + * @throws IllegalArgumentException If x or y are outside the image dimensions. | |
136 | + */ | |
137 | + public abstract void setPixel(int x, int y, ColorRGBA color); | |
138 | + | |
139 | + /** | |
140 | + * Retrieve the color at the given coordinate. | |
141 | + * <p> | |
142 | + * Any components that are not defined in the image format | |
143 | + * will be set to 1.0 in the returned color. For example, | |
144 | + * reading from an {@link Image.Format#Alpha8} format will | |
145 | + * return a ColorRGBA with the R, G, and B components set to 1.0, and | |
146 | + * the A component set to the alpha in the image. | |
147 | + * <p> | |
148 | + * For grayscale or luminance formats, the luminance value is replicated | |
149 | + * in the R, G, and B components. | |
150 | + * <p> | |
151 | + * Integer formats are converted to the range 0.0 - 1.0, based | |
152 | + * on the maximum possible integer value that can be represented | |
153 | + * by the number of bits the component has. | |
154 | + * For example, the {@link Image.Format#RGB5A1} format can | |
155 | + * contain the integer values 0 - 31, a conversion to floating point | |
156 | + * is done by diving the integer value by 31 (done with floating point | |
157 | + * precision). | |
158 | + * | |
159 | + * @param x The x coordinate, from 0 to width - 1. | |
160 | + * @param y The y coordinate, from 0 to height - 1. | |
161 | + * @param store Storage location for the read color, if <code>null</code>, | |
162 | + * then a new ColorRGBA is created and returned with the read color. | |
163 | + * @return The store parameter, if it is null, then a new ColorRGBA | |
164 | + * with the read color. | |
165 | + * @throws IllegalArgumentException If x or y are outside the image dimensions. | |
166 | + */ | |
167 | + public abstract ColorRGBA getPixel(int x, int y, ColorRGBA store); | |
168 | + | |
169 | + /** | |
170 | + * Retrieve the color at the given coordinate. | |
171 | + * <p> | |
172 | + * Convenience method that does not take a store argument. Equivalent | |
173 | + * to calling getPixel(x, y, null). | |
174 | + * See {@link #getPixel(int, int, com.jme3.math.ColorRGBA) } for | |
175 | + * more information. | |
176 | + * | |
177 | + * @param x The x coordinate, from 0 to width - 1. | |
178 | + * @param y The y coordinate, from 0 to height - 1. | |
179 | + * @return A new ColorRGBA with the read color. | |
180 | + * @throws IllegalArgumentException If x or y are outside the image dimensions | |
181 | + */ | |
182 | + public ColorRGBA getPixel(int x, int y) { | |
183 | + return getPixel(x, y, null); | |
184 | + } | |
185 | +} |