|
Mastering
3D Studio MAX R3 |
Bitmap I/O
Bitmaps have a .filename property that allows us to specify
the filename associated with them. You can save bitmaps by defining the
.filename property and using SAVE. For instance,
b = render()
b.filename = “save_example.tga”
save b
will render with the default values and save the rendered image to the
file specified.
You can read bitmaps using two commands: OPENBITMAP and SELECTBITMAP().
The first command opens a specified bitmap and loads it into memory. The
command b = selectbitmap()displays a dialog box (Figure
16.9) that allows the user to select a bitmap file.
You cannot directly manipulate or save bitmaps that were read from the
disk; you must first make a copy of a bitmap using the COPY command. Suppose
you wanted to save the bitmap loaded previously. Youd need to do
it this way:
c = copy b
c.filename = b.filename
save c
You can use DISPLAY to show a bitmap on screen and UNDISPLAY to close
it when displayed.
FIGURE
16.9 Select Bitmap dialog box
After using bitmap files that were loaded or saved, its very important
to close them. This will remove them from memory, which usually helps
a lot, because many bitmaps take large amounts of RAM. To do so, just
use close bitmap, where bitmap is the bitmap
variable. This command will close this bitmap and free the allocated memory.
|
|
| TIP When working
with bitmaps, MAXScript usually requires a lot of memory, and part
of this memory will not be reclaimed automatically. To reclaim this
memory you can use gc(), which will invoke the garbage
collector and get rid of all the garbage you might have in memory.
|
Pixel Manipulation
You can manipulate the bitmap pixel color. It can be useful to read this
information and process it, to adjust any bitmap parameters, or attach
any information to it.
Two commands will be used to perform these operations: GETPIXELS and
SETPIXELS. Both of them work the same way. GETPIXELS returns an array
of colors, starting at a given coordinate with a specified length. SETPIXELS
writes an array of colors starting at the specified coordinate. Usually,
both of these are used in loops, to automate the process.
For instance, lets create a script that reads a bitmap and embeds
it as a watermark in the rendered image. Open add_watermark.ms
from the CD, or start a new script and type all the commands in LISTING
16.4.
LISTING 16.4:The Add Watermark script (add_watermark.ms)
rollout add_watermark “Parameters”
(
local img, big, big2, logo, base, side, bmp
button sel_bmp “Select Logo Bitmap” width:150
button sel_img “Select Rendered Image” width:150 enabled:false
button config “Configure Renderer” width:150
button go “Render” width:150 enabled:false
spinner bottom “Distance from Base: “ type:#integer range:[0,30,10] \
fieldwidth:35
spinner right “Distance from Right: “ type:#integer range:[0,30,10] \
fieldwidth:35
on sel_bmp pressed do
(
bmp = undefined
bmp = selectbitmap()
if bmp == undefined then sel_img.enabled = go.enabled = false
else sel_img.enabled = go.enabled = true
)
on config pressed do max render scene
fn add_it big2 logo base side =
(
if big != undefined do undisplay big
big = copy big2
bh = big.height
bw = big.width
lh = logo.height
lw = logo.width
if (lh + base) > bh and (lw + side) > bw then
print “Logo too large for image”
else
(
start_left = bw - side - lw
start_top = bh - base - lh
for i in 0 to lh do
(
tmp_color = getpixels logo [0,i] lw
setpixels big [start_left,start_top + i] tmp_color
)
display big
)
)
on sel_img pressed do
(
img = selectbitmap()
if img != undefined then add_it img bmp bottom.value right.value
)
on go pressed do
(
img = render vfb:off
if img != undefined then add_it img bmp bottom.value right.value
)
on bottom changed val do add_it img bmp bottom.value right.value
on right changed val do add_it img bmp bottom.value right.value
)
try (closerolloutfloater watermark_floater) catch()
watermark_floater = newrolloutfloater “Add Watermark” 220 215
addrollout add_watermark watermark_floater
You can create a Macro Script or a utility from this script. When this
script is evaluated, a floater appears and allows the user to select a
logo bitmap (Figure 16.10). The user will have two choices, to render
a scene or to select a rendered scene.
FIGURE
16.10 The Add Watermark floater
Configure Render will allow the user to adjust the render parameters,
because otherwise the render will use default values.
Right after rendering or selecting an image, the logo will be pasted
in the image, using GETPIXEL and SETPIXEL. Notice you used loop commands
to paste each row of the logo into the bitmap.
© 2000, Frol (selection,
edition, publication)
|