SDL图像比例

时间:2011-12-27 14:03:15

标签: c++ c sdl

我正在使用sdl库,但它支持缩放/调整大小表面,所以我下载了

SDL_image 1.2& SDL_gfx库。我的功能/代码有效,但图像显示为坏/低

质量。

假设我的图像是100X100,如果我缩小到95X95或者扩展到110X110

质量看起来非常低,但是如果我将其保留在100X100,这与它出现的尺寸相同

质量好。如果按比例缩小图像,大多数图像质量都很好,但是......它的剂量

我的代码是:

int drawImage(SDL_Surface* display, const char * filename, int x, int y, int xx, int yy , const double newwidth, const double newheight, int transparent = NULL)
{



    SDL_Surface *image;
    SDL_Surface *temp;
    temp = IMG_Load(filename); if (temp == NULL) { printf("Unable to load image: %s\n", SDL_GetError()); return 1; }
    image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);

        // Zoom function uses doubles for rates of scaling, rather than
    // exact size values. This is how we get around that:
    double zoomx = newwidth  / (float)image->w;
    double zoomy = newheight / (float)image->h;

    // This function assumes no smoothing, so that any colorkeys wont bleed.
    SDL_Surface* sized = zoomSurface( image, zoomx, zoomy, SMOOTHING_OFF );



    // If the original had an alpha color key, give it to the new one.
    if( image->flags & SDL_SRCCOLORKEY )
    {
        // Acquire the original Key
        Uint32 colorkey = image->format->colorkey;

        // Set to the new image
        SDL_SetColorKey( sized, SDL_SRCCOLORKEY, colorkey );
    }



    // The original picture is no longer needed.
    SDL_FreeSurface( image );

    // Set it instead to the new image.
    image =  sized;



    SDL_Rect src, dest;
    src.x = xx; src.y = yy; src.w = image->w; src.h = image->h; // size 
    dest.x =  x; dest.y = y; dest.w = image->w; dest.h = image->h;

    if(transparent == true )
     {

         //Set the color as transparent
         SDL_SetColorKey(image,SDL_SRCCOLORKEY|SDL_RLEACCEL,SDL_MapRGB(image->format,0x0,0x0,0x0)); 

     }

    else {

    }


    SDL_BlitSurface(image, &src, display, &dest);

    return true;
}

drawImage(display, "Image.png", 50, 100, NULL, NULL, 100, 100,true);

1 个答案:

答案 0 :(得分:2)

在不允许平滑的情况下缩放的图像将具有伪影。如果你从SVG开始并以你想要的比例渲染它,你可能会有更好的运气。 Here's an SVG -> SDL surface图书馆。