Wednesday, December 26, 2012

Pseudo HDR effect using Pixilang


Here is my version of Pseudo HDR filter (tone mapping) for Pixilang. You can use it freely in any Pixilang applications.

/*
    hdr_simulation.pixi

    Copyright (c) 2012, Alexander Zolotov
    http://www.warmplace.ru

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to 
    deal in the Software without restriction, including without limitation the 
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is 
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in 
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
*/

scr = get_screen()
xsize = get_xsize( scr )
ysize = get_ysize( scr )
clear()

//$src - source image (container of PIXEL type)
//$filter_radius (in percent)
fn apply_hdr_effect( $src, $filter_radius, $intensity )
{
    $src_xsize = get_xsize( $src )
    $src_ysize = get_ysize( $src )

    //Make grayscale copy:
    $img2 = clone( $src )
    $c = new( $src_xsize, $src_ysize, FLOAT32 )
    split_rgb( 0, $src, -1, $c ) //Get red channel
    split_rgb( 1, $img2, $c, $c, $c )

    //Blur grayscale image:
    $scr = get_screen()
    set_screen( $img2 )
    $size = ( $src_xsize * $filter_radius ) / 100
    effector( EFF_HBLUR, $size )
    effector( EFF_VBLUR, $size )
    set_screen( $scr )

    //Invert it:
    op_cn( OP_XOR, $img2, WHITE )

    //Do normalization:
    split_rgb( 0, $img2, $c )
    op_cc( OP_MUL, $c, $c ) //Add contrast
    op_cc( OP_MUL, $c, $c ) //Add contrast
    if $intensity != 1
    {
op_cn( OP_MUL, $c, $intensity )
    }
    op_cn( OP_ADD, $c, 1 ) //Add base level
    $r = clone( $c ) 
    $g = clone( $c ) 
    $b = clone( $c )
    split_rgb( 0, $src, $r, $g, $b )
    op_cc( OP_MUL, $r, $c )
    op_cc( OP_MUL, $g, $c )
    op_cc( OP_MUL, $b, $c )
    split_rgb( 1, $src, $r, $g, $b )
    remove( $r )
    remove( $g )
    remove( $b )
    
    remove( $c )
    remove( $img2 )
}

//Load image:
img = load( "images/dark.jpg" )
set_flags( img, CFLAG_INTERP )
img_xsize = get_xsize( img )
img_ysize = get_ysize( img )
s = 1
if img_xsize > xsize || img_ysize >= ysize
{
    s1 = xsize / img_xsize
    s2 = ysize / img_ysize
    if s1 < s2 { s = s1 } else { s = s2 }
}
pixi( img, 0, 0, WHITE, s, s )
frame( 500 )

apply_hdr_effect( img, 3, 2 )
set_flags( img, CFLAG_INTERP )
save( img, "hdr.jpg", FORMAT_JPEG, 95 )

start_timer( 0 )
while 1
{
    t = get_timer( 0 )
    transp( t / 64 )
    pixi( img, 0, 0, WHITE, s, s )
    frame()
    while get_event() { if EVT[ EVT_TYPE ] == EVT_QUIT { halt } }
}

The main function is apply_hdr_effect( $img, $radius, $intensity ) will apply Pseudo HDR effect to the selected image.
Parameters: $img - container with the image; $radius - blur-filter radius; $intensity - effect intensity.

Now let's see how it works.

Before:
 After:

Before:
 After:

Before:
 After:

Before:
 After:

You can always get the latest version of this program from the Pixilang distribution. Folder - examples/graphics. File - hdr_simulation.pixi

No comments:

Post a Comment