hostsupport/hostopenvg/src/riUtils.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
parent 24 a3f46bb01be2
child 69 3f914c77c2e9
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and /or associated documentation files
       
     5  * (the "Materials "), to deal in the Materials without restriction,
       
     6  * including without limitation the rights to use, copy, modify, merge,
       
     7  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
     8  * and to permit persons to whom the Materials are furnished to do so,
       
     9  * subject to 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 CLAIM,
       
    18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    20  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  */
       
    22 
       
    23 #ifndef __RIUTILS_H_
       
    24 #   include "riUtils.h"
       
    25 #endif
       
    26 
       
    27 #include <string.h>
       
    28 
       
    29 namespace OpenVGRI
       
    30 {
       
    31 
       
    32 /**
       
    33  * \brief   Sets mem areas to byte(s) in c.
       
    34  * \param   dst     Destination pointer.
       
    35  * \param   c       Data to set into dst.
       
    36  * \param   nElements   Amount of elements to set.
       
    37  * \param   nBytesPerElement    Amount of bytes for each element.
       
    38  * \note    This is moslty an image-settings support function. It is assumed that several
       
    39  *          bytes / elements can be set at once, especially in 3-byte case.
       
    40  */
       
    41 void riMemSet32(void* dst, RIuint32 c, size_t nElements, size_t nBytesPerElement)
       
    42 {
       
    43     // \todo This function should be called from a function that handles npot element sizes.
       
    44     // \todo Investigate the status of (open) std::fill implementations. Some of that code 
       
    45     // did not _seem_ to bundle sets or use SSE, etc.
       
    46     // \todo Use SSE instructions on Intel? 
       
    47     
       
    48     RI_ASSERT(dst);
       
    49     RI_ASSERT(nElements);
       
    50 
       
    51     switch(nBytesPerElement)
       
    52     {
       
    53     case 4:
       
    54     {
       
    55         RIuint32* ptr = (RIuint32*)dst;
       
    56         do {
       
    57             *ptr++ = c;
       
    58         } while(--nElements);
       
    59         break;
       
    60     }
       
    61     case 3:
       
    62     {
       
    63         // \todo Endianness.
       
    64         RIuint8* ptr = (RIuint8*)dst;
       
    65         RIuint8 b[3];
       
    66         b[0] = c & 0xff;
       
    67         b[1] = (c >> 8)&0xff;
       
    68         b[2] = (c >> 16)&0xff;
       
    69         do {
       
    70             *ptr++ = b[0];
       
    71             *ptr++ = b[1];
       
    72             *ptr++ = b[2];
       
    73         } while(--nElements);
       
    74         break;
       
    75     }
       
    76     case 2:
       
    77     {
       
    78         size_t dws = nElements / 2; 
       
    79         if (dws)
       
    80         {
       
    81             RIuint32* ptr32 = (RIuint32*)dst;
       
    82             dst = (void*)(((RIuint8*)dst + dws * 4));
       
    83             RIuint32 dw = c | (c<<16);
       
    84             do {
       
    85                 *ptr32++ = dw;
       
    86             } while(--dws);
       
    87             nElements &= 0x01;
       
    88         }
       
    89         if (nElements)
       
    90         {
       
    91             RIuint16 *ptr16 = (RIuint16*)dst;
       
    92             const RIuint16 w = (RIuint16)c;
       
    93             do {
       
    94                 *ptr16++ = w;
       
    95             } while(--nElements);
       
    96         }
       
    97         break;
       
    98     }
       
    99     default:
       
   100     {
       
   101         RI_ASSERT(nBytesPerElement == 1);
       
   102         memset(dst, c, nElements);
       
   103         break;
       
   104     }
       
   105     }
       
   106 
       
   107 }
       
   108 
       
   109 /**
       
   110  * \brief   Insert bits into an array of 32-bit integers.
       
   111  * \param   hashSize    Size of array in bytes.
       
   112  * \param   bits        Actual bits to insert.
       
   113  * \param   bitSize     Amount of bits to insert (max 32).
       
   114  * \param   bitPtr      Pointer to first bit to insert [0 .. (hashSize*8)-1]
       
   115  */
       
   116 int riInsertBits32(RIuint32* hash, size_t hashSize, RIuint32 bits, RIuint32 bitSize, int bitPtr)
       
   117 {
       
   118     RI_ASSERT(bitSize > 0 && bitSize <= 32);
       
   119     RI_ASSERT(bits < (1u<<bitSize));
       
   120     RI_ASSERT((bitPtr + bitSize) < (hashSize * 32));
       
   121 
       
   122     int idw = bitPtr >> 5;
       
   123     int ib = bitPtr - (idw << 5);
       
   124 
       
   125     if ((ib + bitSize) > 32)
       
   126     {
       
   127         int rb = ((ib + bitSize) - 32) + 1;
       
   128         RI_ASSERT(rb > 0);
       
   129         hash[idw] |= (bits << ib);
       
   130         hash[idw] |= (bits >> rb);
       
   131     } 
       
   132     else
       
   133     {
       
   134         int new_ib = (ib + bitSize) & 0x1f;
       
   135         RI_ASSERT((ib + bitSize == 32) ? new_ib == 0 : true);
       
   136         hash[idw] |= (bits << ib);
       
   137     }
       
   138     return bitPtr + bitSize;
       
   139 }
       
   140 
       
   141 /**
       
   142  * \brief   Convert an image format to (internal) index.
       
   143  */
       
   144 int riVGImageFormatToIndex(VGImageFormat format)
       
   145 {
       
   146     switch(format)
       
   147     {
       
   148     /* RGB{A,X} channel ordering */
       
   149     case VG_sRGBX_8888:
       
   150         return 0;
       
   151     case VG_sRGBA_8888:
       
   152         return 1;
       
   153     case VG_sRGBA_8888_PRE:
       
   154         return 2;
       
   155     case VG_sRGB_565:
       
   156         return 3;
       
   157     case VG_sRGBA_5551:
       
   158         return 4;
       
   159     case VG_sRGBA_4444:
       
   160         return 5;
       
   161     case VG_sL_8:
       
   162         return 6;
       
   163     case VG_lRGBX_8888:
       
   164         return 7;
       
   165     case VG_lRGBA_8888:
       
   166         return 8;
       
   167     case VG_lRGBA_8888_PRE:
       
   168         return 9;
       
   169     case VG_lL_8:
       
   170         return 10;
       
   171     case VG_A_8:
       
   172         return 11;
       
   173     case VG_BW_1:
       
   174         return 12;
       
   175     case VG_A_1:
       
   176         return 13;
       
   177     case VG_A_4:
       
   178         return 14;
       
   179 
       
   180     /* {A,X}RGB channel ordering */
       
   181     case VG_sXRGB_8888:
       
   182         return 15;
       
   183     case VG_sARGB_8888:
       
   184         return 16;
       
   185     case VG_sARGB_8888_PRE:
       
   186         return 17;
       
   187     case VG_sARGB_1555:
       
   188         return 18;
       
   189     case VG_sARGB_4444:
       
   190         return 19;
       
   191     case VG_lXRGB_8888:
       
   192         return 20;
       
   193     case VG_lARGB_8888:
       
   194         return 21;
       
   195     case VG_lARGB_8888_PRE:
       
   196         return 22;
       
   197 
       
   198     /* BGR{A,X} channel ordering */
       
   199     case VG_sBGRX_8888:
       
   200         return 23;
       
   201     case VG_sBGRA_8888:
       
   202         return 24;
       
   203     case VG_sBGRA_8888_PRE:
       
   204         return 25;
       
   205     case VG_sBGR_565:
       
   206         return 26;
       
   207     case VG_sBGRA_5551:
       
   208         return 27;
       
   209     case VG_sBGRA_4444:
       
   210         return 28;
       
   211     case VG_lBGRX_8888:
       
   212         return 29;
       
   213     case VG_lBGRA_8888:
       
   214         return 30;
       
   215     case VG_lBGRA_8888_PRE:
       
   216         return 31;
       
   217 
       
   218     /* {A,X}BGR channel ordering */
       
   219     case VG_sXBGR_8888:
       
   220         return 32;
       
   221     case VG_sABGR_8888:
       
   222         return 33;
       
   223     case VG_sABGR_8888_PRE:
       
   224         return 34;
       
   225     case VG_sABGR_1555:
       
   226         return 35;
       
   227     case VG_sABGR_4444:
       
   228         return 36;
       
   229     case VG_lXBGR_8888:
       
   230         return 37;
       
   231     case VG_lABGR_8888:
       
   232         return 38;
       
   233     default:
       
   234         RI_ASSERT(format == VG_lABGR_8888_PRE);
       
   235         return 39;
       
   236     }
       
   237 }
       
   238 
       
   239 }
       
   240