2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
17 /*---------------------------------------------------------------------------*/
19 void image_swab(SDL_Surface *src)
21 int i, j, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
25 unsigned char *s = (unsigned char *) src->pixels;
28 /* Iterate over each pixel of the image. */
30 for (i = 0; i < src->h; i++)
31 for (j = 0; j < src->w; j++)
33 int k = (i * src->w + j) * b;
35 /* Swap the red and blue channels of each. */
42 SDL_UnlockSurface(src);
45 void image_white(SDL_Surface *src)
47 int i, j, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
51 unsigned char *s = (unsigned char *) src->pixels;
53 /* Iterate over each pixel of the image. */
55 for (i = 0; i < src->h; i++)
56 for (j = 0; j < src->w; j++)
58 int k = (i * src->w + j) * b;
60 /* Whiten the RGB channels without touching any Alpha. */
67 SDL_UnlockSurface(src);
70 SDL_Surface *image_scale(SDL_Surface *src, int n)
74 int k, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
76 SDL_Surface *dst = SDL_CreateRGBSurface(SDL_SWSURFACE,
79 src->format->BitsPerPixel,
89 unsigned char *s = (unsigned char *) src->pixels;
90 unsigned char *d = (unsigned char *) dst->pixels;
92 /* Iterate each component of each destination pixel. */
94 for (di = 0; di < src->h / n; di++)
95 for (dj = 0; dj < src->w / n; dj++)
96 for (k = 0; k < b; k++)
100 /* Average the NxN source pixel block for each. */
102 for (si = di * n; si < (di + 1) * n; si++)
103 for (sj = dj * n; sj < (dj + 1) * n; sj++)
104 c += s[(si * src->w + sj) * b + k];
106 d[(di * dst->w + dj) * b + k] =
107 (unsigned char) (c / (n * n));
110 SDL_UnlockSurface(dst);
111 SDL_UnlockSurface(src);
117 /*---------------------------------------------------------------------------*/