Make a guess:

    {
        int bpp = surface->format->BytesPerPixel;
        
        SDL_LockSurface(surface);

        Uint8 *p = (Uint8 *) surface->pixels + surface->pitch * (y + top) + left;

        switch(surface->format->BytesPerPixel)
        {
            case 1:
                for(x = 0; x < surface->w; x++) {
                    *p = ((*p & (255 >> surface->format->Rloss)) * bpp) / surface->format->Rloss;
                    p++;
                }
                break;
            case 2:
                for(x = 0; x < surface->w; x++) {
                    *p = ((*p & (255 >> surface->format->Gloss)) * bpp) / surface->format->Gloss;
                    p++;
                    *p = ((*p & (255 >> surface->format->Bloss)) * bpp) / surface->format->Bloss;
                    p++;
                }
                break;
            case 3:
                for(x = 0; x < surface->w; x++) {
                    *p = ((*p & (255 >> surface->format->Aloss)) * bpp) / surface->format->Aloss;
                    p++;
                    *p = ((*p & (255 >> surface->format->Rloss)) * bpp) / surface->format->Rloss;
                    p++;
                    *p = ((*p & (255 >> surface->format->Gloss)) * bpp) / surface->format->Gloss;
                    p++;
                    *p = ((*p & (255 >> surface->format->Bloss)) * bpp) / surface->format->Bloss;
                    p++;
                    *p = ((*p & (255 >> surface->format->Aloss)) * bpp) / surface->format->Aloss;
                    p++;
                }
                break;
        }
        
        switch(bpp)
        {
            case 1:
                SDL_UnlockSurface(surface);
                break;
            case 2:
                *p = *p | (*p & (0x00ff << surface->format->Rloss));
                p++;
                *p = *p | (*p & (0x00ff << surface->format->Gloss));
                p++;
                *p = *p | (*p & (0x00ff << surface->format->Bloss));
                p++;
                *p = *p | (*p & (0x00ff << surface->format->Aloss));
                p++;
                break;
            case 3:
                *p = *p | (*p & (0xff00 << surface->format->Rloss));
                p++;
                *p = *p | (*p & (0xff00 << surface->format->Gloss));
                p++;
                *p = *p | (*p & (0xff00 << surface->format->Bloss));
                p++;
                *p = *p | (*p & (0xff00 << surface->format->Aloss));
                p++;
                break;
        }

        surface->pixels += surface->pitch;
        SDL_UnlockSurface(surface);
    }
    
    return 0;
}

int R_GetPixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    
    if(surface->format->Amask && !surface->format->Amask->w && !surface->format->Amask->h) {
        Uint8 *p = (Uint8 *) surface->pixels + surface->pitch * (y + y % surface->h) + x * bpp;
        for(int pixel_x = 0; pixel_x < surface->w; pixel_x++)
            *p++ = 255;
    } else {
        Uint8 *p = (Uint8 *) surface->pixels + surface->pitch * (y + y % surface->h) + x;
        for(int pixel_x = 0; pixel_x < surface->w; pixel_x++) {
            int byte_value = p[x >> 3];
            int value = (byte_value & (0x80 >> (x & 0x07))) != 0;
            *p++ = value ? 255 : 0;
        }
    }
    
    return 0;
}

void R_ResizeSurface(SDL_Surface *surface, int width, int height)
{
    SDL_Rect rect;
    
    rect.x = 0;
    rect.y = 0;
    rect