Home | History | Annotate | Download | only in renderer
      1 //
      2 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // loadimage.h: Defines image loading functions
      8 
      9 #ifndef LIBGLESV2_RENDERER_LOADIMAGE_H_
     10 #define LIBGLESV2_RENDERER_LOADIMAGE_H_
     11 
     12 #include "common/mathutil.h"
     13 
     14 namespace rx
     15 {
     16 
     17 template <typename T>
     18 inline static T *offsetDataPointer(void *data, int y, int z, int rowPitch, int depthPitch)
     19 {
     20     return reinterpret_cast<T*>(reinterpret_cast<unsigned char*>(data) + (y * rowPitch) + (z * depthPitch));
     21 }
     22 
     23 template <typename T>
     24 inline static const T *offsetDataPointer(const void *data, int y, int z, int rowPitch, int depthPitch)
     25 {
     26     return reinterpret_cast<const T*>(reinterpret_cast<const unsigned char*>(data) + (y * rowPitch) + (z * depthPitch));
     27 }
     28 
     29 void loadAlphaDataToBGRA(int width, int height, int depth,
     30                          const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     31                          void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     32 
     33 void loadAlphaDataToNative(int width, int height, int depth,
     34                            const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     35                            void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     36 
     37 void loadAlphaDataToBGRASSE2(int width, int height, int depth,
     38                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     39                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     40 
     41 void loadAlphaFloatDataToRGBA(int width, int height, int depth,
     42                               const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     43                               void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     44 
     45 void loadAlphaHalfFloatDataToRGBA(int width, int height, int depth,
     46                                   const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     47                                   void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     48 
     49 void loadLuminanceDataToNative(int width, int height, int depth,
     50                                const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     51                                void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     52 
     53 void loadLuminanceDataToBGRA(int width, int height, int depth,
     54                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     55                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     56 
     57 void loadLuminanceFloatDataToRGBA(int width, int height, int depth,
     58                                   const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     59                                   void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     60 
     61 void loadLuminanceFloatDataToRGB(int width, int height, int depth,
     62                                  const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     63                                  void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     64 
     65 void loadLuminanceHalfFloatDataToRGBA(int width, int height, int depth,
     66                                       const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     67                                       void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     68 
     69 void loadLuminanceAlphaDataToNative(int width, int height, int depth,
     70                                     const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     71                                     void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     72 
     73 void loadLuminanceAlphaDataToBGRA(int width, int height, int depth,
     74                                   const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     75                                   void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     76 
     77 void loadLuminanceAlphaFloatDataToRGBA(int width, int height, int depth,
     78                                        const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     79                                        void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     80 
     81 void loadLuminanceAlphaHalfFloatDataToRGBA(int width, int height, int depth,
     82                                            const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     83                                            void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     84 
     85 void loadRGBUByteDataToBGRX(int width, int height, int depth,
     86                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     87                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     88 
     89 void loadRGUByteDataToBGRX(int width, int height, int depth,
     90                            const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     91                            void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     92 
     93 void loadRUByteDataToBGRX(int width, int height, int depth,
     94                           const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     95                           void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
     96 
     97 void loadRGBUByteDataToRGBA(int width, int height, int depth,
     98                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
     99                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    100 
    101 void loadRGBSByteDataToRGBA(int width, int height, int depth,
    102                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    103                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    104 
    105 void loadRGB565DataToBGRA(int width, int height, int depth,
    106                           const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    107                           void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    108 
    109 void loadRGB565DataToRGBA(int width, int height, int depth,
    110                           const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    111                           void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    112 
    113 void loadRGBFloatDataToRGBA(int width, int height, int depth,
    114                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    115                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    116 
    117 void loadRGBFloatDataToNative(int width, int height, int depth,
    118                               const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    119                               void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    120 
    121 void loadRGBHalfFloatDataToRGBA(int width, int height, int depth,
    122                                 const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    123                                 void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    124 
    125 void loadRGBAUByteDataToBGRASSE2(int width, int height, int depth,
    126                                  const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    127                                  void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    128 
    129 void loadRGBAUByteDataToBGRA(int width, int height, int depth,
    130                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    131                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    132 
    133 void loadRGBAUByteDataToNative(int width, int height, int depth,
    134                                const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    135                                void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    136 
    137 void loadRGBA4444DataToBGRA(int width, int height, int depth,
    138                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    139                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    140 
    141 void loadRGBA4444DataToRGBA(int width, int height, int depth,
    142                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    143                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    144 
    145 void loadRGBA5551DataToBGRA(int width, int height, int depth,
    146                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    147                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    148 
    149 void loadRGBA5551DataToRGBA(int width, int height, int depth,
    150                             const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    151                             void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    152 
    153 void loadRGBAFloatDataToRGBA(int width, int height, int depth,
    154                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    155                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    156 
    157 void loadRGBAHalfFloatDataToRGBA(int width, int height, int depth,
    158                                  const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    159                                  void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    160 
    161 void loadBGRADataToBGRA(int width, int height, int depth,
    162                         const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    163                         void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    164 
    165 void loadRGBA2101010ToNative(int width, int height, int depth,
    166                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    167                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    168 
    169 void loadRGBA2101010ToRGBA(int width, int height, int depth,
    170                            const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    171                            void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    172 
    173 void loadRGBHalfFloatDataTo999E5(int width, int height, int depth,
    174                                  const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    175                                  void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    176 
    177 void loadRGBFloatDataTo999E5(int width, int height, int depth,
    178                              const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    179                              void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    180 
    181 void loadRGBHalfFloatDataTo111110Float(int width, int height, int depth,
    182                                        const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    183                                        void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    184 
    185 void loadRGBFloatDataTo111110Float(int width, int height, int depth,
    186                                    const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    187                                    void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    188 
    189 void loadG8R24DataToR24G8(int width, int height, int depth,
    190                         const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    191                         void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    192 
    193 template <typename type, unsigned int componentCount>
    194 void loadToNative(int width, int height, int depth,
    195                   const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    196                   void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch)
    197 {
    198     const unsigned int rowSize = width * sizeof(type) * componentCount;
    199     const unsigned int layerSize = rowSize * height;
    200     const unsigned int imageSize = layerSize * depth;
    201 
    202     if (layerSize == inputDepthPitch && layerSize == outputDepthPitch)
    203     {
    204         ASSERT(rowSize == inputRowPitch && rowSize == outputRowPitch);
    205         memcpy(output, input, imageSize);
    206     }
    207     else if (rowSize == inputRowPitch && rowSize == outputRowPitch)
    208     {
    209         for (int z = 0; z < depth; z++)
    210         {
    211             const type *source = offsetDataPointer<type>(input, 0, z, inputRowPitch, inputDepthPitch);
    212             type *dest = offsetDataPointer<type>(output, 0, z, outputRowPitch, outputDepthPitch);
    213 
    214             memcpy(dest, source, layerSize);
    215         }
    216     }
    217     else
    218     {
    219         for (int z = 0; z < depth; z++)
    220         {
    221             for (int y = 0; y < height; y++)
    222             {
    223                 const type *source = offsetDataPointer<type>(input, y, z, inputRowPitch, inputDepthPitch);
    224                 type *dest = offsetDataPointer<type>(output, y, z, outputRowPitch, outputDepthPitch);
    225                 memcpy(dest, source, width * sizeof(type) * componentCount);
    226             }
    227         }
    228     }
    229 }
    230 
    231 template <typename type, unsigned int fourthComponentBits>
    232 void loadToNative3To4(int width, int height, int depth,
    233                       const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    234                       void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch)
    235 {
    236     const type *source = NULL;
    237     type *dest = NULL;
    238 
    239     const unsigned int fourthBits = fourthComponentBits;
    240     const type fourthValue = *reinterpret_cast<const type*>(&fourthBits);
    241 
    242     for (int z = 0; z < depth; z++)
    243     {
    244         for (int y = 0; y < height; y++)
    245         {
    246             source = offsetDataPointer<type>(input, y, z, inputRowPitch, inputDepthPitch);
    247             dest = offsetDataPointer<type>(output, y, z, outputRowPitch, outputDepthPitch);
    248 
    249             for (int x = 0; x < width; x++)
    250             {
    251                 dest[x * 4 + 0] = source[x * 3 + 0];
    252                 dest[x * 4 + 1] = source[x * 3 + 1];
    253                 dest[x * 4 + 2] = source[x * 3 + 2];
    254                 dest[x * 4 + 3] = fourthValue;
    255             }
    256         }
    257     }
    258 }
    259 
    260 template <unsigned int componentCount>
    261 void loadFloatDataToHalfFloat(int width, int height, int depth,
    262                               const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    263                               void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch)
    264 {
    265     const float *source = NULL;
    266     unsigned short *dest = NULL;
    267 
    268     const int elementWidth = componentCount * width;
    269 
    270     for (int z = 0; z < depth; z++)
    271     {
    272         for (int y = 0; y < height; y++)
    273         {
    274             source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
    275             dest = offsetDataPointer<unsigned short>(output, y, z, outputRowPitch, outputDepthPitch);
    276 
    277             for (int x = 0; x < elementWidth; x++)
    278             {
    279                 dest[x] = gl::float32ToFloat16(source[x]);
    280             }
    281         }
    282     }
    283 }
    284 
    285 void loadFloatRGBDataToHalfFloatRGBA(int width, int height, int depth,
    286                                      const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    287                                      void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    288 
    289 template <unsigned int blockWidth, unsigned int blockHeight, unsigned int blockSize>
    290 void loadCompressedBlockDataToNative(int width, int height, int depth,
    291                                      const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    292                                      void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch)
    293 {
    294     int columns = (width + (blockWidth - 1)) / blockWidth;
    295     int rows = (height + (blockHeight - 1)) / blockHeight;
    296 
    297     for (int z = 0; z < depth; ++z)
    298     {
    299         for (int y = 0; y < rows; ++y)
    300         {
    301             void *source = (void*)((char*)input + y * inputRowPitch + z * inputDepthPitch);
    302             void *dest = (void*)((char*)output + y * outputRowPitch + z * outputDepthPitch);
    303 
    304             memcpy(dest, source, columns * blockSize);
    305         }
    306     }
    307 }
    308 
    309 void loadUintDataToUshort(int width, int height, int depth,
    310                           const void *input, unsigned int inputRowPitch, unsigned int inputDepthPitch,
    311                           void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch);
    312 
    313 template <typename type, unsigned int firstBits, unsigned int secondBits, unsigned int thirdBits, unsigned int fourthBits>
    314 void initialize4ComponentData(int width, int height, int depth,
    315                               void *output, unsigned int outputRowPitch, unsigned int outputDepthPitch)
    316 {
    317     unsigned int writeBits[4] = { firstBits, secondBits, thirdBits, fourthBits };
    318     type writeValues[4] = { *reinterpret_cast<const type*>(&writeBits[0]),
    319                             *reinterpret_cast<const type*>(&writeBits[1]),
    320                             *reinterpret_cast<const type*>(&writeBits[2]),
    321                             *reinterpret_cast<const type*>(&writeBits[3]) };
    322 
    323     for (int z = 0; z < depth; z++)
    324     {
    325         for (int y = 0; y < height; y++)
    326         {
    327             type* destRow = offsetDataPointer<type>(output, y, z, outputRowPitch, outputDepthPitch);
    328 
    329             for (int x = 0; x < width; x++)
    330             {
    331                 type* destPixel = destRow + x * 4;
    332 
    333                 // This could potentially be optimized by generating an entire row of initialization
    334                 // data and copying row by row instead of pixel by pixel.
    335                 memcpy(destPixel, writeValues, sizeof(type) * 4);
    336             }
    337         }
    338     }
    339 }
    340 
    341 }
    342 
    343 #endif // LIBGLESV2_RENDERER_LOADIMAGE_H_
    344