aboutsummaryrefslogtreecommitdiff
path: root/engine/src/blender/com/jme3/scene/plugins/blender/textures/TexturePixel.java
blob: 37c0122cc4f73b578f046a7e2671a36951816b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.jme3.scene.plugins.blender.textures;

import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image.Format;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The class that stores the pixel values of a texture.
 * 
 * @author Marcin Roguski (Kaelthas)
 */
public class TexturePixel implements Cloneable {
	private static final Logger	LOGGER	= Logger.getLogger(TexturePixel.class.getName());

	/** The pixel data. */
	public float				intensity, red, green, blue, alpha;

	/**
	 * Copies the values from the given pixel.
	 * 
	 * @param pixel
	 *            the pixel that we read from
	 */
	public void fromPixel(TexturePixel pixel) {
		this.intensity = pixel.intensity;
		this.red = pixel.red;
		this.green = pixel.green;
		this.blue = pixel.blue;
		this.alpha = pixel.alpha;
	}

	/**
	 * Copies the values from the given color.
	 * 
	 * @param colorRGBA
	 *            the color that we read from
	 */
	public void fromColor(ColorRGBA colorRGBA) {
		this.red = colorRGBA.r;
		this.green = colorRGBA.g;
		this.blue = colorRGBA.b;
		this.alpha = colorRGBA.a;
	}

	/**
	 * Copies the values from the given values.
	 * 
	 * @param a
	 *            the alpha value
	 * @param r
	 *            the red value
	 * @param g
	 *            the green value
	 * @param b
	 *            the blue value
	 */
	public void fromARGB8(float a, float r, float g, float b) {
		this.alpha = a;
		this.red = r;
		this.green = g;
		this.blue = b;
	}

	/**
	 * Copies the values from the given integer that stores the ARGB8 data.
	 * 
	 * @param argb8
	 *            the data stored in an integer
	 */
	public void fromARGB8(int argb8) {
		byte pixelValue = (byte) ((argb8 & 0xFF000000) >> 24);
		this.alpha = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
		pixelValue = (byte) ((argb8 & 0xFF0000) >> 16);
		this.red = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
		pixelValue = (byte) ((argb8 & 0xFF00) >> 8);
		this.green = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
		pixelValue = (byte) (argb8 & 0xFF);
		this.blue = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
	}

	/**
	 * Copies the data from the given image.
	 * 
	 * @param imageFormat
	 *            the image format
	 * @param data
	 *            the image data
	 * @param pixelIndex
	 *            the index of the required pixel
	 */
	public void fromImage(Format imageFormat, ByteBuffer data, int pixelIndex) {
		int firstByteIndex;
		byte pixelValue;
		switch (imageFormat) {
			case ABGR8:
				firstByteIndex = pixelIndex << 2;
				pixelValue = data.get(firstByteIndex);
				this.alpha = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 1);
				this.blue = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 2);
				this.green = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 3);
				this.red = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				break;
			case RGBA8:
				firstByteIndex = pixelIndex << 2;
				pixelValue = data.get(firstByteIndex);
				this.red = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 1);
				this.green = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 2);
				this.blue = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 3);
				this.alpha = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				break;
			case BGR8:
				firstByteIndex = pixelIndex * 3;
				pixelValue = data.get(firstByteIndex);
				this.blue = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 1);
				this.green = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 2);
				this.red = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				this.alpha = 1.0f;
				break;
			case RGB8:
				firstByteIndex = pixelIndex * 3;
				pixelValue = data.get(firstByteIndex);
				this.red = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 1);
				this.green = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				pixelValue = data.get(firstByteIndex + 2);
				this.blue = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				this.alpha = 1.0f;
				break;
			case Luminance8:
				pixelValue = data.get(pixelIndex);
				this.intensity = pixelValue >= 0 ? pixelValue / 255.0f : 1.0f - (~pixelValue) / 255.0f;
				break;
			default:
				LOGGER.log(Level.FINEST, "Unknown type of texture: {0}. Black pixel used!", imageFormat);
				this.intensity = this.blue = this.red = this.green = this.alpha = 0.0f;
		}
	}

	/**
	 * Stores RGBA values in the given array.
	 * 
	 * @param result
	 *            the array to store values
	 */
	public void toRGBA(float[] result) {
		result[0] = this.red;
		result[1] = this.green;
		result[2] = this.blue;
		result[3] = this.alpha;
	}

	/**
	 * Stores the data in the given table.
	 * 
	 * @param result
	 *            the result table
	 */
	public void toRGBA8(byte[] result) {
		result[0] = (byte) (this.red * 255.0f);
		result[1] = (byte) (this.green * 255.0f);
		result[2] = (byte) (this.blue * 255.0f);
		result[3] = (byte) (this.alpha * 255.0f);
	}

	/**
	 * Stores the pixel values in the integer.
	 * 
	 * @return the integer that stores the pixel values
	 */
	public int toARGB8() {
		int result = 0;
		int b = (int) (this.alpha * 255.0f);
		result |= b << 24;
		b = (int) (this.red * 255.0f);
		result |= b << 16;
		b = (int) (this.green * 255.0f);
		result |= b << 8;
		b = (int) (this.blue * 255.0f);
		result |= b;
		return result;
	}

	/**
	 * Merges two pixels (adds the values of each color).
	 * 
	 * @param pixel
	 *            the pixel we merge with
	 */
	public void merge(TexturePixel pixel) {
		float oneMinusAlpha = 1 - pixel.alpha;
		this.red = oneMinusAlpha * this.red + pixel.alpha * pixel.red;
		this.green = oneMinusAlpha * this.green + pixel.alpha * pixel.green;
		this.blue = oneMinusAlpha * this.blue + pixel.alpha * pixel.blue;
		// alpha should be always 1.0f as a result
	}

	/**
	 * This method negates the colors.
	 */
	public void negate() {
		this.red = 1.0f - this.red;
		this.green = 1.0f - this.green;
		this.blue = 1.0f - this.blue;
		this.alpha = 1.0f - this.alpha;
	}

	/**
	 * This method clears the pixel values.
	 */
	public void clear() {
		this.intensity = this.blue = this.red = this.green = this.alpha = 0.0f;
	}

	/**
	 * This method adds the calues of the given pixel to the current pixel.
	 * 
	 * @param pixel
	 *            the pixel we add
	 */
	public void add(TexturePixel pixel) {
		this.red += pixel.red;
		this.green += pixel.green;
		this.blue += pixel.blue;
		this.alpha += pixel.alpha;
		this.intensity += pixel.intensity;
	}

	/**
	 * This method multiplies the values of the given pixel by the given value.
	 * 
	 * @param value
	 *            multiplication factor
	 */
	public void mult(float value) {
		this.red *= value;
		this.green *= value;
		this.blue *= value;
		this.alpha *= value;
		this.intensity *= value;
	}

	/**
	 * This method divides the values of the given pixel by the given value.
	 * ATTENTION! Beware of the zero value. This will cause you NaN's in the
	 * pixel values.
	 * 
	 * @param value
	 *            division factor
	 */
	public void divide(float value) {
		this.red /= value;
		this.green /= value;
		this.blue /= value;
		this.alpha /= value;
		this.intensity /= value;
	}

	/**
	 * This method clamps the pixel values to the given borders.
	 * 
	 * @param min
	 *            the minimum value
	 * @param max
	 *            the maximum value
	 */
	public void clamp(float min, float max) {
		this.red = FastMath.clamp(this.red, min, max);
		this.green = FastMath.clamp(this.green, min, max);
		this.blue = FastMath.clamp(this.blue, min, max);
		this.alpha = FastMath.clamp(this.alpha, min, max);
		this.intensity = FastMath.clamp(this.intensity, min, max);
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	@Override
	public String toString() {
		return "[" + red + ", " + green + ", " + blue + ", " + alpha + " {" + intensity + "}]";
	}
}