View Single Post
Old 04-20-2007, 04:54 PM   #19
Mostly dormant
stoffe's Avatar
Status: Administrator
Join Date: Apr 2002
Posts: 5,850
Helpful!  10 year veteran!  Notable contributor 
Originally Posted by tk102
sub reverse_bytes {
	my $in=shift;
	return unpack('V',pack('C4',reverse (unpack('C4',pack('V',$in)))));
It is indeed a reversal of bytes (0xAABBCCDD becomes 0xDDCCBBAA for example).
That reminds me why I'm not too fond of Perl either. I haven't done anything with Perl for over 6 years, so I didn't understand much of that code. (I prefer high-level "almost english" languages like Delphi, C#, PHP, so it feels like I'm in way over my head with this.)

The unpack() function takes a string and splits it by token into an array, like explode() in PHP? While pack() takes an array and spits out a token-separated string from it?

What does your reverse_bytes() function receive as parameter, a token-separated string (using "V" as separator) containing the three values a texel-struct holds?

If so you convert the string to an array, immediately explode the array into a string again (this time using "C4" as token separator instead), reverse-sort the string, turn it into an array again (wouldn't "C4" be "4C" when you reverse the string?), and immediately turns the array back into a string (this time separated with "V" again) which you return? Wouldn't that return an array of 3 values?

Huh? I don't follow what this accomplishes. It obviously works, but I still don't understand what it does.

(As you don't have the source: The C++ code takes a struct as function parameter containing two 16 bit unsigned ints and one 32 bit unsigned int value... does something with those three values which I have absolutely no idea what, and then spits out a single 32 bit unsigned integer as result.)

* * *

Another question ... this is called in a loop that loops up to the Y-size of the bitmap while writing the TGA (i is the incrementing value in the loop):
fwrite(file->pixels+pitch*(ysz-i-1), 4, pitch, fp)

file->pixels is an array of 32 bit unsigned integers (the RGBA data), pitch is the X-dimension of the image (in pixels), ysz is the Y-dimension of the image

What exactly is it writing? file->pixels is an array, but it doesn't supply any array index in that function call... Does the file->pixels+pitch*(ysz-i-1) add pitch*(ysz-i-1) to the memory pointer of the pixels array and then write the four bytes found in that location? Why do this instead of using an array index?

Weird lower-level languages I haven't used for years confuse me. Use of pointers always confuse me.
stoffe is offline   you may: quote & reply,