Changing the gamma in pictures, revisited

I’ve written an article a few years back about changing the gamma of images in maxscript. Since then I’ve learned a lot and want to revisit this topic.

Improvement

Previously I’d adjust the global gamma-setting just before loading an image. Switching the gamma back after the image had been loaded would essentially convert the gamma of the image. However, as Johan Boekhoven pointed out, this is quite risky. what if the script crashes in between? The gamma settings would be messed with.

An alternative is to paste the bitmap from disk into a new bitmap which has the correct gamma settings. This is what I’ve done and use in my scripts for quite a while now to convert rendered images or to place images in rollouts.

Here’s the method I’m using.

function fn_loadBannerImage imagePath =
(
    /*<FUNCTION>
    Description:
        Loads an image from disk into a bitmap. You can use this bitmap in an imgtag control
        for instance. Uses proper gamma settings
    Arguments:
        <string> imagePath: the path to the image.
    Return:
        <bitmap> a bitmap
    </FUNCTION>*/
    
    --get the gamma setting
    local myGamma = 2.2
    if IDisplayGamma.colorCorrectionMode == #gamma do 
    (
        case of
        (
            ((maxVersion())[1] <= 14000): myGamma = fileInGamma --max 2012 reacts differently than max 2013 and up
            default: myGamma = fileInGamma/displayGamma --this calculation results in the correct gamma for the banner in max 2013 and up
        )
    )
    
    --work the bitmaps
    local bmpDisk = openbitmap imagePath --open the bitmap from disk
    local bmpCorrectGamma = bitmap bmpDisk.width bmpDisk.height gamma:myGamma --make a new bitmap with the same size but with the prper gamma settings
    pasteBitmap bmpDisk bmpCorrectGamma [0,0] [0,0] --paste the bitmap from disk into the gamma-bitmap. this essentially adjusts the gamma
    bmpCorrectGamma            
)


1 comment

Leave a Reply

* Will not be published