Added a helper function for generating pointer offsets and changed type names to include size for clarity.

TRAC #22705

Signed-off-by: Jamie Madill
Signed-off-by: Shannon Woods
Author: Geoff Lang

git-svn-id: https://angleproject.googlecode.com/svn/branches/es3proto@2160 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/renderer/Image.cpp b/src/libGLESv2/renderer/Image.cpp
index b5e768c..27f15b0 100644
--- a/src/libGLESv2/renderer/Image.cpp
+++ b/src/libGLESv2/renderer/Image.cpp
@@ -23,19 +23,31 @@
     mActualFormat = GL_NONE;
 }
 
+template <typename T>
+inline static T *offsetDataPointer(void *data, int y, int z, int rowPitch, int depthPitch)
+{
+    return reinterpret_cast<T*>(reinterpret_cast<unsigned char*>(data) + (y * rowPitch) + (z * depthPitch));
+}
+
+template <typename T>
+inline static const T *offsetDataPointer(const void *data, int y, int z, int rowPitch, int depthPitch)
+{
+    return reinterpret_cast<const T*>(reinterpret_cast<const unsigned char*>(data) + (y * rowPitch) + (z * depthPitch));
+}
+
 void Image::loadAlphaDataToBGRA(GLsizei width, GLsizei height, GLsizei depth,
                                 int inputRowPitch, int inputDepthPitch, const void *input,
                                 size_t outputRowPitch, size_t outputDepthPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
-    
+
     for (int z = 0; z < depth; z++)
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = 0;
@@ -58,8 +70,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             memcpy(dest, source, width);
         }
     }
@@ -76,8 +88,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = 0;
@@ -100,8 +112,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned short>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = 0;
@@ -125,9 +137,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
-
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             if (!native)   // BGRA8 destination format
             {
                 for (int x = 0; x < width; x++)
@@ -157,8 +168,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x];
@@ -181,8 +192,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[3 * x + 0] = source[x];
@@ -204,8 +215,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned short>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x];
@@ -229,8 +240,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
 
             if (!native)   // BGRA8 destination format
             {
@@ -261,8 +272,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[2*x+0];
@@ -285,8 +296,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned short>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[2*x+0];
@@ -309,8 +320,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x * 3 + 2];
@@ -333,8 +344,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x * 3 + 0];
@@ -357,8 +368,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -382,8 +393,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -407,8 +418,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x * 3 + 0];
@@ -431,8 +442,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             memcpy(dest, source, width * 12);
         }
     }
@@ -449,8 +460,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned short>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 dest[4 * x + 0] = source[x * 3 + 0];
@@ -473,8 +484,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned int>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned int>(output, y, z, outputRowPitch, outputDepthPitch);
 
             for (int x = 0; x < width; x++)
             {
@@ -496,8 +507,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<unsigned int>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned int>(output, y, z, outputRowPitch, outputDepthPitch);
 
             memcpy(dest, source, width * 4);
         }
@@ -515,8 +526,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -540,8 +551,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -565,8 +576,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -590,8 +601,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned short>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             for (int x = 0; x < width; x++)
             {
                 unsigned short rgba = source[x];
@@ -615,8 +626,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch);
-            dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch);
+            source = offsetDataPointer<float>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<float>(output, y, z, outputRowPitch, outputDepthPitch);
             memcpy(dest, source, width * 16);
         }
     }
@@ -633,8 +644,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             memcpy(dest, source, width * 8);
         }
     }
@@ -651,8 +662,8 @@
     {
         for (int y = 0; y < height; y++)
         {
-            source = static_cast<const unsigned char*>(input) + y * inputRowPitch + z * inputDepthPitch;
-            dest = static_cast<unsigned char*>(output) + y * outputRowPitch + z * outputDepthPitch;
+            source = offsetDataPointer<unsigned char>(input, y, z, inputRowPitch, inputDepthPitch);
+            dest = offsetDataPointer<unsigned char>(output, y, z, outputRowPitch, outputDepthPitch);
             memcpy(dest, source, width*4);
         }
     }