PDA

View Full Version : Save PFM files from MaxScript


48design
02-21-2012, 06:10 PM
Hi!

I tried the following to create a PFM (portable float map) file:


(

local out_name = getSaveFileName types: "Portable Floatmap (*.pfm) |*.pfm|" filename:"c:\\"
local out_file = createfile out_name

local img = bitmap 10 10 hdr:true
for x=0 to img.width-1 do
(
for y=0 to img.height-1 do
(
setPixels img [x,y] #([1.5,2.3,3.3333,1])
)
)
display img

local header_magic = "PF"
local header_comment ="# pfm created with MaxScript by 48design"
local header_width = img.width
local header_height = img.height
local header_byteOrder = "-1.0000"

format "%\n%\n% %\n%\n" header_magic header_comment header_width header_height header_byteOrder to:out_file
close out_file
free out_file

f = fopen out_name "ab"

for x=0 to img.width-1 do
(
for y=0 to img.height-1 do
(
px = getPixels img [x,y] 1

-- write bytes
fseek f -1 #seek_cur
WriteFloat f (px[1].r/255 as float)
fseek f -1 #seek_cur
WriteFloat f (px[1].g/255 as float)
fseek f -1 #seek_cur
WriteFloat f (px[1].b/255 as float)
)
)

fflush f
fclose f

--format "[%,%,%]" (px[1].r/255) (px[1].g/255) (px[1].b/255)

)


This didn't work. Opening in Photohop gives different results than I thought it should...

I'm not one of those hardcore programmers but after reading those two webpages I thought it can't be that hard, even for MaxScript. Wrong?
http://netpbm.sourceforge.net/doc/pfm.html
http://people.cs.kuleuven.be/~ares.lagae/libpfm/index.html (http://people.cs.kuleuven.be/%7Eares.lagae/libpfm/index.html)

wallworm
02-22-2012, 05:17 AM
I'm still neck deep in the Brush UVW dilemma or else I'd try to lend a brain cell for this one. Please let me know if/when you get this one banged out.

48design
02-22-2012, 04:23 PM
For those interested two PFM files: one saved with Photoshop and one made by this script.
They differ from each other.

wallworm
02-22-2012, 04:49 PM
I know this is jumping agead... but it would be useful for the Source exporter if the function you're building has a parameter that, if set, automatically flips the image vertically when being built... because for some reason the Source PFM > VTF compiler needs the image flipped that way.

48design
02-23-2012, 07:32 AM
I think this would be no problem because the pixel rows would just need to be written from right to left but as you can see the PFM files are different and therefore the PFM-problem isn't fixed yet. :scream:

Neuro69
02-24-2012, 09:38 AM
Some small differences in "newline" chars seem to mess it up. This works for me:


(

local out_name = getSaveFileName types: "Portable Floatmap (*.pfm) |*.pfm|" filename:"c:\\"
f=fopen out_name "wb"

local img = bitmap 10 10 hdr:true
for x=0 to img.width-1 do
(
for y=0 to img.height-1 do
(
setPixels img [x,y] #([1.5,2.3,3.3333,1])
)
)
display img

WriteString f "PF"
fseek f -1 #seek_cur
WriteByte f 10
WriteString f "# pfm created with MaxScript by 48design"
fseek f -1 #seek_cur
WriteByte f 10
WriteString f (img.width as string)
fseek f -1 #seek_cur
WriteByte f 10
WriteString f (img.height as string)
fseek f -1 #seek_cur
WriteByte f 10
WriteString f "-1.0000"
fseek f -1 #seek_cur
WriteByte f 10

for x=0 to img.width-1 do
(
for y=0 to img.height-1 do
(
px = getPixels img [x,y] 1

-- write bytes
WriteFloat f (px[1].r/255 as float)
WriteFloat f (px[1].g/255 as float)
WriteFloat f (px[1].b/255 as float)
)
)

fflush f
fclose f
)

48design
02-24-2012, 04:55 PM
Wow! Thanks, I modded it a bit and now can flip the bitmap as well. ;)
Thank you Neuro69!
:beer:

wallworm
02-25-2012, 07:40 AM
Thanks a million Fabian! I've added a note in the changelog (http://dev.wallworm.com/topic/33/wwmt_changelog.html) and in the credits (http://dev.wallworm.com/topic/29/credits.html)! I've also added a credit in the packaged readme of wwmt.

Neuro69
02-27-2012, 09:36 AM
The joys of hex! (get a hex editor) Glad you liked it :-)

48design
02-27-2012, 10:50 AM
Thanks! :)

CGTalk Moderation
02-27-2012, 10:50 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.