How to adjust gamma?

I need to copy source image to a new surface and modify the new image (lighten
it or darken)
So I write a code:---------------
SDL_Surface *newImage;
Uint32 r, g, b;

newImage = SDL_CreateRGBSurface(SDL_SWSURFACE,
oldImage->w, oldImage->h, oldimage->format->BitsPerPixel,
oldimage->format->Rmask, oldimage->format->Gmask,
oldimage->format->Bmask, oldimage->format->Amask);

for(x=0; xw; x++) for(y=0; yh; y++) {
getPixel(oldImage, x, y, &r, &g, &b);
adjastBrightness(&r, &g, &b);
setPixel(newImage, x, y, &r, &g, &b);

is there any other, more easy way to do it?