from buffered image to a shader

hi,
i’m looking for the fastest way to get a BufferedImage from type TYPE_INT_RGB to a texture, to process the pixel data in a shader, and back to the BufferedImage.
At the moment i convert the integer values with the help of a temporary Color object to seperate float (R, G, B) values. I pass these float values over a FloatBuffer to the texture, process them with the shader and write them back to the BufferedImage. of course this is very slow.

code for the conversion from the BufferedImage to the FloatBuffer


for ( int h = 0; h < img.getHeight (); h++ ) {
			for ( int w = 0; w < img.getWidth (); w++ ) {
				Color temp = new Color ( img.getRGB ( w, h ) );
				data.put ( temp.getRed () );
				data.put ( temp.getGreen () );
				data.put ( temp.getBlue () );
			}
		}

perhaps someone knows a faster solution…


BufferedImage img; // created above somewhere

int data[] = (int[])img.getData().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);

hmm…yes, but i’ll have to convert the pixel information from int to float to be able to put them into the FloatBuffer for the texture. and therefor i’ll also have to use a loop over the array…?!?
i could use an IntBuffer for the texture data but i had problems processing integer inputs in the shader. i dont’t know wether i did something wrong in the shader code, or it is not possible to work on texture information coming from integer values in glsl…

Are you sure you need a floating-point texture?

If so, yes, you have to iterate over the arrays.buffers and convert between te primitives yourself.

curious…i try using integers now.
i pass the data from an IntBuffer to the texture.
if i use this simple shader:


uniform samplerRECT BaseImage;

void main(void) {
	vec3 tmp = texRECT(BaseImage,gl_TexCoord[0].st);
   	gl_FragColor = vec4(tmp, 1);
}

…my input data = my output data

example:
in: 0
in: 2
in: 4
in: 6
in: 8
in: 10
in: 12
in: 14
in: 16

result: 0
result: 2
result: 4
result: 6
result: 8
result: 10
result: 12
result: 14
result: 16

…but if i try to operate on the data, for example:


void main(void) {
	vec3 tmp = texRECT(BaseImage,gl_TexCoord[0].st);
	tmp += 1;
   	gl_FragColor = vec4(tmp, 1);
}

the results are:

result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496
result: 2147482496

…using ivec3 also doens’t help.

…hmmm


tmp *= 0.5;

…works:

result: 0
result: 1
result: 2
result: 3
result: 4
result: 5
result: 6
result: 7
result: 8

why not :


tmp += 1;

???

quick idea: try 1.0 ?

no, already tried that, doesn’t work… :frowning:

There isn’t a vec3+int/float operator for vec3 because vec3 is a vector maybe?

[quote]The mathematical operators are supported with the following combination of types:

vec3 = vec3 + vec3
vec3 = vec3 - vec3
float = vec3 * vec3 # dot product
vec3 = float * vec3
vec3 = vec3 * float
vec3 = vec3 / float
vec3 = vec3 % float # each component
vec3 = vec3 % vec3 # component wise
vec3 = -vec3
float = vec3[ i ] # get or set element
[/quote]
so if you want to add 1 to each of them try, vec3[0]+=1; vec3[1]+=1; vec3[2]+=1;

if i try this (tmp[0] += 1;), i get these results:

result: 2147482496
result: 2
result: 4
result: 2147482496
result: 8
result: 10
result: 2147482496
result: 14
result: 16

so, doesn’t work , too :frowning:

Ahhh, then try tmp += vec3(1);

Also with the tmp[n], try tmp[n] = tmp[n] + 1.0 because it should in theory work

…unfortunately the same results.
i think it has something to do with the input data as integer. i have no problems manipulating pixels coming from floats.

Iterating over an array converting ints-to-floats-to-ints isn’t that slow… Just try it, and see if it floats your boat.

It depends on how often you’re doing it. I found it slowed down my texture mapping routine big time, and in fact cost more time than all of the calculations in between. Bear in mind it’s for HLSL so he’s in a place where you probably don’t want to be making such a costly conversion each pixel. However the best thing you can do is try it out.

As for the adding of the vectors, try it by itself and figure that one out. If anything try tmp += vec3(1.0, 1.0, 1.0). If that doesn’t then check there’s not something else wrong because vector addition should be included.