openvg/openvgrefimplementation/sfopenvg/sfopenvg/riUtils.cpp
branchNewGraphicsArchitecture
changeset 64 5c983aa672ea
equal deleted inserted replaced
61:4ac0d9f65e95 64:5c983aa672ea
       
     1 /*------------------------------------------------------------------------
       
     2  *
       
     3  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4  *
       
     5  * Permission is hereby granted, free of charge, to any person obtaining a
       
     6  * copy of this software and /or associated documentation files
       
     7  * (the "Materials "), to deal in the Materials without restriction,
       
     8  * including without limitation the rights to use, copy, modify, merge,
       
     9  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
    10  * and to permit persons to whom the Materials are furnished to do so,
       
    11  * subject to the following conditions: 
       
    12  *
       
    13  * The above copyright notice and this permission notice shall be included 
       
    14  * in all copies or substantial portions of the Materials. 
       
    15  *
       
    16  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    22  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    23  *
       
    24  *//*-------------------------------------------------------------------*/
       
    25 
       
    26 #ifndef __RIUTILS_H_
       
    27 #   include "riUtils.h"
       
    28 #endif
       
    29 
       
    30 #include <string.h>
       
    31 
       
    32 namespace OpenVGRI
       
    33 {
       
    34 
       
    35 /**
       
    36  * \brief   Sets mem areas to byte(s) in c.
       
    37  * \param   dst     Destination pointer.
       
    38  * \param   c       Data to set into dst.
       
    39  * \param   nElements   Amount of elements to set.
       
    40  * \param   nBytesPerElement    Amount of bytes for each element.
       
    41  * \note    This is moslty an image-settings support function. It is assumed that several
       
    42  *          bytes / elements can be set at once, especially in 3-byte case.
       
    43  */
       
    44 void riMemSet32(void* dst, RIuint32 c, size_t nElements, size_t nBytesPerElement)
       
    45 {
       
    46     // \todo This function should be called from a function that handles npot element sizes.
       
    47     // \todo Investigate the status of (open) std::fill implementations. Some of that code 
       
    48     // did not _seem_ to bundle sets or use SSE, etc.
       
    49     // \todo Use SSE instructions on Intel? 
       
    50     
       
    51     RI_ASSERT(dst);
       
    52     RI_ASSERT(nElements);
       
    53 
       
    54     switch(nBytesPerElement)
       
    55     {
       
    56     case 4:
       
    57     {
       
    58         RIuint32* ptr = (RIuint32*)dst;
       
    59         do {
       
    60             *ptr++ = c;
       
    61         } while(--nElements);
       
    62         break;
       
    63     }
       
    64     case 3:
       
    65     {
       
    66         // \todo Endianness.
       
    67         RIuint8* ptr = (RIuint8*)dst;
       
    68         RIuint8 b[3];
       
    69         b[0] = c & 0xff;
       
    70         b[1] = (c >> 8)&0xff;
       
    71         b[2] = (c >> 16)&0xff;
       
    72         do {
       
    73             *ptr++ = b[0];
       
    74             *ptr++ = b[1];
       
    75             *ptr++ = b[2];
       
    76         } while(--nElements);
       
    77         break;
       
    78     }
       
    79     case 2:
       
    80     {
       
    81         size_t dws = nElements / 2; 
       
    82         if (dws)
       
    83         {
       
    84             RIuint32* ptr32 = (RIuint32*)dst;
       
    85             dst = (void*)(((RIuint8*)dst + dws * 4));
       
    86             RIuint32 dw = c | (c<<16);
       
    87             do {
       
    88                 *ptr32++ = dw;
       
    89             } while(--dws);
       
    90             nElements &= 0x01;
       
    91         }
       
    92         if (nElements)
       
    93         {
       
    94             RIuint16 *ptr16 = (RIuint16*)dst;
       
    95             const RIuint16 w = (RIuint16)c;
       
    96             do {
       
    97                 *ptr16++ = w;
       
    98             } while(--nElements);
       
    99         }
       
   100     }
       
   101     case 1:
       
   102     {
       
   103         memset(dst, c, nElements);
       
   104         break;
       
   105     }
       
   106     default:
       
   107         RI_ASSERT(false);
       
   108     }
       
   109 
       
   110 }
       
   111 
       
   112 }
       
   113