engine/source/gamelib/bitmap.c
// Clipbitmap: cuts off transparent edges to optimize a bitmap.
cuts off transparent edges to optimize a bitmap.
cuts off transparent edges to optimize a bitmap .
void clipbitmap(s_bitmap *bitmap, int *clip_left, int *clip_right, int *clip_top, int *clip_bottom)
{
    int x, y;
    int clip, clear;
    int xsize = bitmap->width;
    int ysize = bitmap->height;
    int fullwidth = bitmap->width;
    int top_clipmove = 0;
    int left_clipmove = 0;
    int bottom_clipmove = 0;
    int right_clipmove = 0;
    clip = 0;
    for(y = 0; y < ysize; y++)
    {
        clear = 1;
        for(x = 0; x < xsize && clear; x++)
        {
            if(bitmap->data[y * xsize + x] != TRANS_INDEX)
            {
                clear = 0;
            }
        }
        if(clear)
        {
            ++clip;
        }
        else
        {
            break;
        }
    }
    if(clip)
    {
        ysize -= clip;
        if(ysize < 1)
        {
            if(clip_left)
            {
                *clip_left = 0;
            }
            if(clip_right)
            {
                *clip_right = 0;
            }
            if(clip_top)
            {
                *clip_top = 0;
            }
            if(clip_bottom)
            {
                *clip_bottom = 0;
            }
            bitmap->clipped_width = 0;
            bitmap->clipped_height = 0;
            return;
        }
        bitmap->clipped_y_offset = clip;
        top_clipmove = clip;
    }
    clip = 0;
    for(y = bitmap->height - 1; y >= top_clipmove; y--)
    {
        clear = 1;
        for(x = 0; x < xsize && clear; x++)
        {
            if(bitmap->data[y * xsize + x] != TRANS_INDEX)
            {
                clear = 0;
            }
        }
        if(clear)
        {
            ++clip;
        }
        else
        {
            break;
        }
    }
    ysize -= clip;
    bitmap->clipped_height = ysize;
    bottom_clipmove = clip;
    clip = 2000000000;
    for(y = top_clipmove; y < ysize + top_clipmove; y++)
    {
        clear = 0;
        for(x = 0; x < xsize; x++)
        {
            if(bitmap->data[y * xsize + x] != TRANS_INDEX)
            {
                break;
            }
            ++clear;
        }
        if(clear < clip)
        {
            clip = clear;
        }
    }
    if(clip)
    {
        xsize -= clip;
        bitmap->clipped_width = xsize;
        bitmap->clipped_x_offset = clip;
        left_clipmove = clip;
    }
    clip = 2000000000;
    for(y = top_clipmove; y < ysize + top_clipmove; y++)
    {
        clear = 0;
        for(x = fullwidth - 1; x >= left_clipmove; x--)
        {
            if(bitmap->data[y * fullwidth + x] != TRANS_INDEX)
            {
                break;
            }
            ++clear;
        }
        if(clear < clip)
        {
            clip = clear;
        }
    }
    if(clip)
    {
        xsize -= clip;
        bitmap->clipped_width = xsize;
        right_clipmove = clip;
    }
    if(clip_left)
    {
        *clip_left = left_clipmove;
    }
    if(clip_right)
    {
        *clip_right = right_clipmove;
    }
    if(clip_top)
    {
        *clip_top = top_clipmove;
    }
    if(clip_bottom)
    {
        *clip_bottom = bottom_clipmove;
    }
}
void clipbitmap ( s_bitmap * bitmap , int * clip_left , int * clip_right , int * clip_top , int * clip_bottom ) { int x , y ; int clip , clear ; int xsize = bitmap -> width ; int ysize = bitmap -> height ; int fullwidth = bitmap -> width ; int top_clipmove = 0 ; int left_clipmove = 0 ; int bottom_clipmove = 0 ; int right_clipmove = 0 ; clip = 0 ; for ( y = 0 ; y < ysize ; y ++ ) { clear = 1 ; for ( x = 0 ; x < xsize && clear ; x ++ ) { if ( bitmap -> data [ y * xsize + x ] != TRANS_INDEX ) { clear = 0 ; } } if ( clear ) { ++ clip ; } else { break ; } } if ( clip ) { ysize -= clip ; if ( ysize < 1 ) { if ( clip_left ) { * clip_left = 0 ; } if ( clip_right ) { * clip_right = 0 ; } if ( clip_top ) { * clip_top = 0 ; } if ( clip_bottom ) { * clip_bottom = 0 ; } bitmap -> clipped_width = 0 ; bitmap -> clipped_height = 0 ; return ; } bitmap -> clipped_y_offset = clip ; top_clipmove = clip ; } clip = 0 ; for ( y = bitmap -> height - 1 ; y >= top_clipmove ; y -- ) { clear = 1 ; for ( x = 0 ; x < xsize && clear ; x ++ ) { if ( bitmap -> data [ y * xsize + x ] != TRANS_INDEX ) { clear = 0 ; } } if ( clear ) { ++ clip ; } else { break ; } } ysize -= clip ; bitmap -> clipped_height = ysize ; bottom_clipmove = clip ; clip = 2000000000 ; for ( y = top_clipmove ; y < ysize + top_clipmove ; y ++ ) { clear = 0 ; for ( x = 0 ; x < xsize ; x ++ ) { if ( bitmap -> data [ y * xsize + x ] != TRANS_INDEX ) { break ; } ++ clear ; } if ( clear < clip ) { clip = clear ; } } if ( clip ) { xsize -= clip ; bitmap -> clipped_width = xsize ; bitmap -> clipped_x_offset = clip ; left_clipmove = clip ; } clip = 2000000000 ; for ( y = top_clipmove ; y < ysize + top_clipmove ; y ++ ) { clear = 0 ; for ( x = fullwidth - 1 ; x >= left_clipmove ; x -- ) { if ( bitmap -> data [ y * fullwidth + x ] != TRANS_INDEX ) { break ; } ++ clear ; } if ( clear < clip ) { clip = clear ; } } if ( clip ) { xsize -= clip ; bitmap -> clipped_width = xsize ; right_clipmove = clip ; } if ( clip_left ) { * clip_left = left_clipmove ; } if ( clip_right ) { * clip_right = right_clipmove ; } if ( clip_top ) { * clip_top = top_clipmove ; } if ( clip_bottom ) { * clip_bottom = bottom_clipmove ; } }
Clipbitmap: cuts off transparent edges to optimize a bitmap.
Clipbitmap : cuts off transparent edges to optimize a bitmap .
// Determine size of empty top // "Cut off" empty top // If nothing is left of the bitmap, return... // Determine size of empty bottom // "Cut off" empty bottom // Determine size of empty left side // "Cut off" empty pixels on the left side // Determine size of empty right side // "Cut off" empty pixels on the right side