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