Browse Source

PNG reader flips output

Thomas Buck 10 years ago
parent
commit
b1c743394d
1 changed files with 22 additions and 11 deletions
  1. 22
    11
      src/utils/png.cpp

+ 22
- 11
src/utils/png.cpp View File

@@ -104,27 +104,38 @@ int pngLoad(const char *filename, unsigned char **image, unsigned int *width, un
104 104
     delete [] row_pointers;
105 105
     fclose(fp);
106 106
 
107
-    if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
108
-        *image = image_data;
109
-    } else {
110
-        *image = new unsigned char[*width * *height * 4];
107
+    // Fix alpha
108
+    if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
109
+        unsigned char *tmpimage = image_data;
110
+        image_data = new unsigned char[*width * *height * 4];
111 111
         if (color_type == PNG_COLOR_TYPE_RGB) {
112 112
             for (unsigned int i = 0; i < (*width * *height); i++) {
113
-                *image[i * 4] = image_data[i * 3];
114
-                *image[(i * 4) + 1] = image_data[(i * 3) + 1];
115
-                *image[(i * 4) + 2] = image_data[(i * 3) + 2];
116
-                *image[(i * 4) + 3] = 255;
113
+                image_data[i * 4] = tmpimage[i * 3];
114
+                image_data[(i * 4) + 1] = tmpimage[(i * 3) + 1];
115
+                image_data[(i * 4) + 2] = tmpimage[(i * 3) + 2];
116
+                image_data[(i * 4) + 3] = 255;
117 117
             }
118 118
         } else {
119 119
             pngPrint("%s: Unknown libpng color type %d.", filename, color_type);
120 120
             delete [] image_data;
121
-            delete [] *image;
122
-            *image = NULL;
121
+            delete [] tmpimage;
123 122
             return -8;
124 123
         }
125
-        delete [] image_data;
124
+        delete [] tmpimage;
126 125
     }
127 126
 
127
+    // Flip
128
+    *image = new unsigned char[*width * *height * 4];
129
+    for (unsigned int y = 0; y < (*height); y++) {
130
+        for (unsigned int x = 0; x < (*width); x++) {
131
+            (*image)[((y * *width) + x) * 4] = image_data[(((*height - y - 1) * *width) + x) * 4];
132
+            (*image)[(((y * *width) + x) * 4) + 1] = image_data[((((*height - y - 1) * *width) + x) * 4) + 1];
133
+            (*image)[(((y * *width) + x) * 4) + 2] = image_data[((((*height - y - 1) * *width) + x) * 4) + 2];
134
+            (*image)[(((y * *width) + x) * 4) + 3] = image_data[((((*height - y - 1) * *width) + x) * 4) + 3];
135
+        }
136
+    }
137
+
138
+    delete [] image_data;
128 139
     return 0;
129 140
 }
130 141
 

Loading…
Cancel
Save