1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
Brightness(SDL_Surface* src, int lum) {
int x,y;
int tmp;
Uint8 r, g, b;
Uint32 p;
SDL_Surface* dst = NULL;
dst = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 8, 0, 0, 0, 0);
SDL_Color* clrs[src->format->palette->ncolors];
SDL_LockSurface(src);
for (y = 0; y < src->h; y++) {
for (x = 0; x < src->w; x++) {
p = getPixel8(src, x, y);
tmp = r + lum;
if (tmp < 0)
r = 0;
else if (tmp > 255)
r = 255;
else r = tmp;
tmp = g + lum;
if (tmp < 0)
g = 0;
else if (tmp > 255)
g = 255;
else g = tmp;
tmp = b + lum;
if (tmp < 0)
b = 0;
else if (tmp > 255)
b = 255;
else b = tmp;
p = SDL_MapRGB(src->format, r, g, b);
putPixel8(dst, x, y, p);
}
}
SDL_UnlockSurface(src);
if (dst == NULL) {
fprintf(stderr, "Unable to create a destination surface in function : Brightness");
} else if (dst == src) {
fprintf(stderr, "Unable to apply an effect in function : Brightness");
}
//dst=src;
SDL_Palette *pal;
pal->ncolors = src->format->palette->ncolors;
pal->colors = clrs;
SDL_SetColors(dst, pal->colors, 0, pal->ncolors);
return dst;
}
putPixel8(SDL_Surface *surface, int x, int y, Uint32 pixel) {
if (surface->format->BytesPerPixel != 1) {
fprintf(stderr, "Fatal error: require 8 bits per pixel!\n");
exit (1);
}
*(Uint8 *)(surface->pixels + y * surface->pitch + x * 1) = pixel;
}
getPixel8(SDL_Surface *surface, int x, int y) {
if (surface->format->BytesPerPixel != 1) {
fprintf(stderr, "Fatal error: require 8 bits per pixel!\n");
exit (1);
}
return *((Uint8 *)surface->pixels + y * surface->pitch + x * 1);
} |
Partager