PDA

View Full Version : What datatype is rgb() giving me?



joeloud
03-13-2010, 08:04 AM
I'm trying to write a script that looks at each pixel of an image and writes the value of the green channel to a text file (it's for a broader script I'm working on, which works except, seemingly, for this part).

I have working code that writes all three values to a text file, but I'm trying to isolate just one value. (By the way, luma() does not seem to actually work. I only get nil from it.) It doesn't necessarily need to be the green channel, that's arbitrary, but I thought I was getting a vector from the rgb() method. I figured out that it's not actually giving me a vector, and I guessed that it must be a string. It's not a string either. I tried all the different isNum(), isInt() methods, and I keep getting zeros.

I can make it a vector with asVec(), but then the next question is, how do I get just one value out of it as a floating point number? I thought it worked like arrays, like "vector[2]" to get just the second value, but that doesn't seem to work either. I can turn it into a string and then parse it, though I doubt this is the most elegant solution.

Below is the initial code, before much of this experimentation of trying to figure out what I'm getting out of rgb(). It asks where you want to save, looks at the image (it's a controlled experiment, so nothing fancy there), creates an array of values containing all the color information (the image is just a 10x10 gray scale test image to keep it simple), and then writes the values to the text file. (The file stuff is borrowed from another script--this is really just so I can see what values I'm actually getting with rgb(), and I ended up more confused than I was before).

(BTW, As I was preparing this post, I thought maybe another option would be to make all these images in alpha channels, since the alpha() method appears to give you just one simple number to work with, but I haven't tried that yet. I'd prefer to know how this works before going that route)


reqbegin("Save Out Image Value Array");
reqsize(250,60);
arrayFile = ctlfilename("Save Array", "", 30, 0);
return if !reqpost();
fileName = getvalue(arrayFile);
reqend();

image = Image(2) || error("No images loaded!");
iW = image.width;
iH = image.height;
iT = iW*iH;

content = "";

for(x = 1 ; x <= iW ; x++)
{
for(y = 1 ; y <= iH ; y++)
{
rgbV[x,y] = image.rgb(x,y);
}
}

for(x = 1 ; x <= iW ; x++)
{
for(y = 1 ; y <= iH ; y++)
{
content = string(content, rgbV[x,y].asStr(), "\t");
}

content = string(content, "\n");
}

if(strright(fileName,4) != ".txt")
fileName = string(fileName, ".txt");
filedelete(fileName);
fout = File(fileName,"a");
fout.writeln(content);

evenflcw
03-13-2010, 05:25 PM
It could only be one of two data types. An lscript vector or an array. If it's not the first it's the second. :) So try accessing using regular array indexing (brackets).

ie.
myrgb = image.rgb(x,y);
r = myrgb[1];
g = myrgb[2];
b = myrgb[3];

That should work! (just checked, and it does indeed seem to be floating point notation).

You can save yourself alot of questions by inserting debug(); anywhere in your code. Use F10 to step through, F11 to step in.

PS. The debugger will reveal an bug in your rgbV array handling. At first assignment lscript will make a bad best guess about what kind of data structure you want. And a different guess subsequent times. If you know the size of an array before hand, initialize the whole thing to nil! It will make you script alot faster as all the space needed is allocated only once instead of reallocated each time the array grows. This is not documented but proven fact (I've done alot of research on Lscript arrays).

ie.
rgbV[image.width, image.height] = nil;

joeloud
03-13-2010, 07:39 PM
Thanks for all that info, evenflcw. This will be a big help in the future. Thanks!

The thing is, though, as I think about it, I did a size() test on the result I was getting from rgb(), and it gave me a 1, which I presumed meant that it wasn't an array. Weird, I'll have another look at it with your tips in mind.

Regarding the other question, though, if it was a vector, how do I read just one value of the vector? "vectorName[2]" doesn't seem to work (unless I somehow wasn't working with a vector, though I thought I was).

evenflcw
03-13-2010, 07:57 PM
Individual values in Lscript vectors (group of 3 numbers;<x,y,z>) are accessed as data members ie. vec.x, vec.y, vec.z. You can also use h/p/b or r/g/b, atleast for vectors you've created yourselfs. Vectors in object agents might not be as flexible. I remember being irritated concerning this some time ago, but don't remember what exactly. But it had to do with not being able to use hpb instead of xyz or somesuch, to make the code more readable. But might have been an older lscript version perhaps.