os/graphics/graphicscomposition/openwfcompositionengine/common/src/owfutils.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicscomposition/openwfcompositionengine/common/src/owfutils.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* Copyright (c) 2009 The Khronos Group Inc.
     1.5 + *
     1.6 + * Permission is hereby granted, free of charge, to any person obtaining a
     1.7 + * copy of this software and/or associated documentation files (the
     1.8 + * "Materials"), to deal in the Materials without restriction, including
     1.9 + * without limitation the rights to use, copy, modify, merge, publish,
    1.10 + * distribute, sublicense, and/or sell copies of the Materials, and to
    1.11 + * permit persons to whom the Materials are furnished to do so, subject to
    1.12 + * the following conditions:
    1.13 + *
    1.14 + * The above copyright notice and this permission notice shall be included
    1.15 + * in all copies or substantial portions of the Materials.
    1.16 + *
    1.17 + * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.18 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.19 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.20 + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.21 + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.22 + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.23 + * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
    1.24 + */
    1.25 +
    1.26 + #include "owftypes.h"
    1.27 +
    1.28 +#ifdef __cplusplus
    1.29 +extern "C" {
    1.30 +#endif
    1.31 +
    1.32 +OWFint min(OWFint aLhs, OWFint aRhs)
    1.33 +{
    1.34 +    if (aLhs < aRhs) {
    1.35 +        return aLhs;
    1.36 +    }
    1.37 +    return aRhs;
    1.38 +}
    1.39 +
    1.40 +void OWF_Rect_Set(OWF_RECTANGLE* rect,
    1.41 +                  OWFint left,
    1.42 +                  OWFint top,
    1.43 +                  OWFint width,
    1.44 +                  OWFint height)
    1.45 +{
    1.46 +    if (!rect) {
    1.47 +        return;
    1.48 +    }
    1.49 +
    1.50 +    rect->x = left;
    1.51 +    rect->y = top;
    1.52 +    rect->width = width;
    1.53 +    rect->height = height;
    1.54 +}
    1.55 +
    1.56 +OWFboolean OWF_Rect_Clip(OWF_RECTANGLE* clipped,
    1.57 +                         OWF_RECTANGLE* rect,
    1.58 +                         OWF_RECTANGLE* bounds)
    1.59 +{
    1.60 +    OWFint dw, dh, dx0, dy0, dx1, dy1; /* destination image coordinates */
    1.61 +    OWFint bl, bt, br, bb;
    1.62 +
    1.63 +    bl = bounds->x;
    1.64 +    bt = bounds->y;
    1.65 +    br = bl + bounds->width;
    1.66 +    bb = bt + bounds->height;
    1.67 +
    1.68 +    dw = rect->width;
    1.69 +    dh = rect->height;
    1.70 +    dx0 = rect->x;
    1.71 +    dy0 = rect->y;
    1.72 +
    1.73 +    /* Crop x coords to target image width */
    1.74 +    if (dx0 < bl) {
    1.75 +        dw = dw - (bl - dx0);
    1.76 +        dx0 = bl;
    1.77 +    }
    1.78 +
    1.79 +    /* check if destination rectangle is outside target image */
    1.80 +    if (dx0 >= br) {
    1.81 +        return OWF_FALSE;
    1.82 +    }
    1.83 +
    1.84 +    /* Crop y coord to target image height */
    1.85 +    if (dy0 < bt) {
    1.86 +        dh = dh - (bt - dy0);
    1.87 +        dy0 = bt;
    1.88 +    }
    1.89 +
    1.90 +    /* check if destination rectangle is outside target image */
    1.91 +    if (dy0 >= bb) {
    1.92 +        return OWF_FALSE;
    1.93 +    }
    1.94 +
    1.95 +    /* clamp right edge */
    1.96 +    dx1 = dx0 + dw;
    1.97 +    if (dx1 >= br) {
    1.98 +        dx1 = br;
    1.99 +    } else if (dx1 < bl) {
   1.100 +        return OWF_FALSE;
   1.101 +    }
   1.102 +
   1.103 +    /* clamp bottom edge */
   1.104 +    dy1 = dy0 + dh;
   1.105 +    if (dy1 >= bb) {
   1.106 +        dy1 = bb;
   1.107 +    } else if (dy1 < bt) {
   1.108 +        return OWF_FALSE;
   1.109 +    }
   1.110 +
   1.111 +    clipped->x = dx0;
   1.112 +    clipped->y = dy0;
   1.113 +    clipped->width = dx1 - dx0;
   1.114 +    clipped->height = dy1 - dy0;
   1.115 +
   1.116 +    return OWF_TRUE;
   1.117 +}
   1.118 +
   1.119 +#ifdef __cplusplus
   1.120 +}
   1.121 +#endif