os/graphics/graphicscomposition/openwfcompositionengine/common/src/owfutils.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* Copyright (c) 2009 The Khronos Group Inc.
sl@0
     2
 *
sl@0
     3
 * Permission is hereby granted, free of charge, to any person obtaining a
sl@0
     4
 * copy of this software and/or associated documentation files (the
sl@0
     5
 * "Materials"), to deal in the Materials without restriction, including
sl@0
     6
 * without limitation the rights to use, copy, modify, merge, publish,
sl@0
     7
 * distribute, sublicense, and/or sell copies of the Materials, and to
sl@0
     8
 * permit persons to whom the Materials are furnished to do so, subject to
sl@0
     9
 * the following conditions:
sl@0
    10
 *
sl@0
    11
 * The above copyright notice and this permission notice shall be included
sl@0
    12
 * in all copies or substantial portions of the Materials.
sl@0
    13
 *
sl@0
    14
 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
sl@0
    15
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
sl@0
    16
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
sl@0
    17
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
sl@0
    18
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
sl@0
    19
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
sl@0
    20
 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
sl@0
    21
 */
sl@0
    22
sl@0
    23
 #include "owftypes.h"
sl@0
    24
sl@0
    25
#ifdef __cplusplus
sl@0
    26
extern "C" {
sl@0
    27
#endif
sl@0
    28
sl@0
    29
OWFint min(OWFint aLhs, OWFint aRhs)
sl@0
    30
{
sl@0
    31
    if (aLhs < aRhs) {
sl@0
    32
        return aLhs;
sl@0
    33
    }
sl@0
    34
    return aRhs;
sl@0
    35
}
sl@0
    36
sl@0
    37
void OWF_Rect_Set(OWF_RECTANGLE* rect,
sl@0
    38
                  OWFint left,
sl@0
    39
                  OWFint top,
sl@0
    40
                  OWFint width,
sl@0
    41
                  OWFint height)
sl@0
    42
{
sl@0
    43
    if (!rect) {
sl@0
    44
        return;
sl@0
    45
    }
sl@0
    46
sl@0
    47
    rect->x = left;
sl@0
    48
    rect->y = top;
sl@0
    49
    rect->width = width;
sl@0
    50
    rect->height = height;
sl@0
    51
}
sl@0
    52
sl@0
    53
OWFboolean OWF_Rect_Clip(OWF_RECTANGLE* clipped,
sl@0
    54
                         OWF_RECTANGLE* rect,
sl@0
    55
                         OWF_RECTANGLE* bounds)
sl@0
    56
{
sl@0
    57
    OWFint dw, dh, dx0, dy0, dx1, dy1; /* destination image coordinates */
sl@0
    58
    OWFint bl, bt, br, bb;
sl@0
    59
sl@0
    60
    bl = bounds->x;
sl@0
    61
    bt = bounds->y;
sl@0
    62
    br = bl + bounds->width;
sl@0
    63
    bb = bt + bounds->height;
sl@0
    64
sl@0
    65
    dw = rect->width;
sl@0
    66
    dh = rect->height;
sl@0
    67
    dx0 = rect->x;
sl@0
    68
    dy0 = rect->y;
sl@0
    69
sl@0
    70
    /* Crop x coords to target image width */
sl@0
    71
    if (dx0 < bl) {
sl@0
    72
        dw = dw - (bl - dx0);
sl@0
    73
        dx0 = bl;
sl@0
    74
    }
sl@0
    75
sl@0
    76
    /* check if destination rectangle is outside target image */
sl@0
    77
    if (dx0 >= br) {
sl@0
    78
        return OWF_FALSE;
sl@0
    79
    }
sl@0
    80
sl@0
    81
    /* Crop y coord to target image height */
sl@0
    82
    if (dy0 < bt) {
sl@0
    83
        dh = dh - (bt - dy0);
sl@0
    84
        dy0 = bt;
sl@0
    85
    }
sl@0
    86
sl@0
    87
    /* check if destination rectangle is outside target image */
sl@0
    88
    if (dy0 >= bb) {
sl@0
    89
        return OWF_FALSE;
sl@0
    90
    }
sl@0
    91
sl@0
    92
    /* clamp right edge */
sl@0
    93
    dx1 = dx0 + dw;
sl@0
    94
    if (dx1 >= br) {
sl@0
    95
        dx1 = br;
sl@0
    96
    } else if (dx1 < bl) {
sl@0
    97
        return OWF_FALSE;
sl@0
    98
    }
sl@0
    99
sl@0
   100
    /* clamp bottom edge */
sl@0
   101
    dy1 = dy0 + dh;
sl@0
   102
    if (dy1 >= bb) {
sl@0
   103
        dy1 = bb;
sl@0
   104
    } else if (dy1 < bt) {
sl@0
   105
        return OWF_FALSE;
sl@0
   106
    }
sl@0
   107
sl@0
   108
    clipped->x = dx0;
sl@0
   109
    clipped->y = dy0;
sl@0
   110
    clipped->width = dx1 - dx0;
sl@0
   111
    clipped->height = dy1 - dy0;
sl@0
   112
sl@0
   113
    return OWF_TRUE;
sl@0
   114
}
sl@0
   115
sl@0
   116
#ifdef __cplusplus
sl@0
   117
}
sl@0
   118
#endif