Переглянути джерело

scaleBuffer() no longer limiting at 256x256

Thomas Buck 10 роки тому
джерело
коміт
74c6e6c54d
5 змінених файлів з 42 додано та 39 видалено
  1. 1
    0
      ChangeLog.md
  2. 2
    2
      TODO.md
  3. 1
    1
      include/utils/pixel.h
  4. 8
    6
      src/TextureManager.cpp
  5. 30
    30
      src/utils/pixel.cpp

+ 1
- 0
ChangeLog.md Переглянути файл

@@ -10,6 +10,7 @@
10 10
     * Added pngCheck() and pcxCheck() methods
11 11
     * Added ColorMode and bpp info to pcx/png API
12 12
     * Modified tga to match png/pcx API
13
+    * scaleBuffer() no longer stops at 256x256
13 14
 
14 15
     [ 20140621 ]
15 16
     * Created StaticMesh class replacing model_mesh_t stuff

+ 2
- 2
TODO.md Переглянути файл

@@ -8,8 +8,8 @@
8 8
 * Don't use C-Style code, try to replace with C++ lib
9 9
     * Use std::strings
10 10
     * Rewrite Console and use operator << to write to the console?
11
-* SDL_TTF 2.0.12+ can do line breaks, use it: http://stackoverflow.com/questions/17847818/how-to-do-line-breaks-and-line-wrapping-with-sdl-ttf/18418688#18418688
12
-* Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even reusing the Mesh class for the model_meshes...
11
+* SDL_TTF 2.0.12+ [can do line breaks](http://stackoverflow.com/questions/17847818/how-to-do-line-breaks-and-line-wrapping-with-sdl-ttf/18418688#18418688), use it
12
+* Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even combining Mesh and StaticMesh...
13 13
 
14 14
 ## Changes
15 15
 

+ 1
- 1
include/utils/pixel.h Переглянути файл

@@ -12,7 +12,7 @@ void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h);
12 12
 void bgra2rgba32(unsigned char *image, unsigned int w, unsigned int h);
13 13
 void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h);
14 14
 
15
-unsigned char *scaleBuffer(unsigned char *image, int width, int height, int components);
15
+unsigned char *scaleBuffer(unsigned char *image, unsigned int *w, unsigned int *h, unsigned int bpp);
16 16
 
17 17
 #endif
18 18
 

+ 8
- 6
src/TextureManager.cpp Переглянути файл

@@ -289,6 +289,11 @@ int TextureManager::loadPCX(const char *filename) {
289 289
     int id = -1;
290 290
     int error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
291 291
     if (error == 0) {
292
+        unsigned char *image2 = scaleBuffer(image, &w, &h, bpp);
293
+        if (image2) {
294
+            delete [] image;
295
+            image = image2;
296
+        }
292 297
         id = loadBuffer(image, w, h, c, bpp);
293 298
         delete [] image;
294 299
     }
@@ -313,20 +318,17 @@ int TextureManager::loadTGA(const char *filename) {
313 318
     } else if (!tgaCheck(f)) {
314 319
         tgaLoad(f, &image, &w, &h, &type);
315 320
 
316
-        type += 2;
317
-
318
-        image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
321
+        image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
319 322
 
320 323
         if (image2) {
321 324
             delete [] image;
322 325
             image = image2;
323
-            w = h = 256;
324 326
         }
325 327
 
326 328
         if (image) {
327 329
             id = loadBuffer(image, w, h,
328
-                    (type == 4) ? RGBA : RGB,
329
-                    (type == 4) ? 32 : 24);
330
+                    (type == 2) ? RGBA : RGB,
331
+                    (type == 2) ? 32 : 24);
330 332
 
331 333
             delete [] image;
332 334
         }

+ 30
- 30
src/utils/pixel.cpp Переглянути файл

@@ -50,40 +50,40 @@ void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h) {
50 50
 }
51 51
 
52 52
 #define NEXT_POWER(x) do {        \
53
-    int i;                        \
53
+    unsigned int i;               \
54 54
     for (i = 1; i < (x); i *= 2); \
55
-    x = i;                        \
55
+    (x) = i;                      \
56 56
 } while (false);
57 57
 
58 58
 // This code based off on gluScaleImage()
59
-unsigned char *scaleBuffer(unsigned char *image, int width, int height, int components) {
59
+unsigned char *scaleBuffer(unsigned char *image, unsigned int *w, unsigned int *h, unsigned int bpp) {
60
+    unsigned int width = *w;
61
+    unsigned int height = *h;
60 62
     assert(image != NULL);
61 63
     assert(width > 0);
62 64
     assert(height > 0);
63
-    assert((components == 3) || (components == 4));
65
+    assert((bpp % 8) == 0);
64 66
 
65
-    int original_height = height;
66
-    int original_width = width;
67
+    unsigned int components = bpp / 8;
68
+    unsigned int original_height = height;
69
+    unsigned int original_width = width;
67 70
 
68 71
     NEXT_POWER(height);
69 72
     NEXT_POWER(width);
70 73
 
71
-    if (height > 256)
72
-        height = 256;
73
-
74
-    if (width > 256)
75
-        width = 256;
76
-
77 74
     // Check to see if scaling is needed
78 75
     if (height == original_height && width == original_width)
79 76
         return NULL;
80 77
 
78
+    *w = width;
79
+    *h = height;
80
+
81 81
     unsigned char *timage = new unsigned char[height * width * components];
82 82
     float *tempin = new float[original_width * original_height * components];
83 83
     float *tempout = new float[width * height * components];
84 84
 
85 85
     // Copy user data to float format.
86
-    for (int i = 0; i < original_height * original_width * components; ++i) {
86
+    for (unsigned int i = 0; i < original_height * original_width * components; ++i) {
87 87
         tempin[i] = (float)image[i];
88 88
     }
89 89
 
@@ -103,9 +103,9 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
103 103
     }
104 104
 
105 105
     if (sx < 1.0 && sy < 1.0) { // Magnify both width and height: use weighted sample of 4 pixels
106
-        for (int i = 0; i < height; ++i) {
107
-            int i0 = (int)(i * sy);
108
-            int i1 = i0 + 1;
106
+        for (unsigned int i = 0; i < height; ++i) {
107
+            unsigned int i0 = (unsigned int)(i * sy);
108
+            unsigned int i1 = i0 + 1;
109 109
 
110 110
             if (i1 >= original_height) {
111 111
                 i1 = original_height - 1;
@@ -113,9 +113,9 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
113 113
 
114 114
             float alpha = i * sy - i0;
115 115
 
116
-            for (int j = 0; j < width; ++j) {
117
-                int j0 = (int) (j * sx);
118
-                int j1 = j0 + 1;
116
+            for (unsigned int j = 0; j < width; ++j) {
117
+                unsigned int j0 = (unsigned int) (j * sx);
118
+                unsigned int j1 = j0 + 1;
119 119
 
120 120
                 if (j1 >= original_width) {
121 121
                     j1 = original_width - 1;
@@ -131,7 +131,7 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
131 131
 
132 132
                 float *dst = tempout + (i * width + j) * components;
133 133
 
134
-                for (int k = 0; k < components; ++k) {
134
+                for (unsigned int k = 0; k < components; ++k) {
135 135
                     float s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
136 136
                     float s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
137 137
                     *dst++ = s1 * (1.0f - alpha) + s2 * alpha;
@@ -139,17 +139,17 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
139 139
             }
140 140
         }
141 141
     } else { // Shrink width and/or height: use an unweighted box filter
142
-        for (int i = 0; i < height; ++i) {
143
-            int i0 = (int) (i * sy);
144
-            int i1 = i0 + 1;
142
+        for (unsigned int i = 0; i < height; ++i) {
143
+            unsigned int i0 = (unsigned int) (i * sy);
144
+            unsigned int i1 = i0 + 1;
145 145
 
146 146
             if (i1 >= original_height) {
147 147
                 i1 = original_height - 1;
148 148
             }
149 149
 
150
-            for (int j = 0; j < width; ++j) {
151
-                int j0 = (int) (j * sx);
152
-                int j1 = j0 + 1;
150
+            for (unsigned int j = 0; j < width; ++j) {
151
+                unsigned int j0 = (unsigned int) (j * sx);
152
+                unsigned int j1 = j0 + 1;
153 153
 
154 154
                 if (j1 >= original_width) {
155 155
                     j1 = original_width - 1;
@@ -158,11 +158,11 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
158 158
                 float *dst = tempout + (i * width + j) * components;
159 159
 
160 160
                 // Compute average of pixels in the rectangle (i0,j0)-(i1,j1)
161
-                for (int k = 0; k < components; ++k) {
161
+                for (unsigned int k = 0; k < components; ++k) {
162 162
                     float sum = 0.0;
163 163
 
164
-                    for (int ii = i0; ii <= i1; ++ii) {
165
-                        for (int jj = j0; jj <= j1; ++jj) {
164
+                    for (unsigned int ii = i0; ii <= i1; ++ii) {
165
+                        for (unsigned int jj = j0; jj <= j1; ++jj) {
166 166
                             sum += *(tempin + (ii * original_width + jj)
167 167
                                     * components + k);
168 168
                         }
@@ -176,7 +176,7 @@ unsigned char *scaleBuffer(unsigned char *image, int width, int height, int comp
176 176
     }
177 177
 
178 178
     // Copy to our results.
179
-    for (int i = 0; i < height * width * components; ++i) {
179
+    for (unsigned int i = 0; i < height * width * components; ++i) {
180 180
         timage[i] = (unsigned char)tempout[i];
181 181
     }
182 182
 

Завантаження…
Відмінити
Зберегти