[Closed] Save PFM files from MaxScript
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 "%
%
% %
%
" 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
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.
For those interested two PFM files: one saved with Photoshop and one made by this script.
They differ from each other.
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.
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.
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
)
Wow! Thanks, I modded it a bit and now can flip the bitmap as well.
Thank you Neuro69!