SDL框架无法显示小于20x20像素的BMP文件?

发布于 2024-11-04 08:24:29 字数 4830 浏览 3 评论 0原文

有谁知道是否有分钟。 SDL 框架中 BMP 文件的大小限制如何显示?
我问你这个问题是因为我尝试过但无法正确显示小于 20x20 像素的 BMP 文件。(我正在使用 SDL for C++)。

代码:

    bool successCondition = false;  
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    atexit(SDL_Quit);

    SDL_Surface *screen;

    screen = SDL_SetVideoMode(640, 480, 24, SDL_DOUBLEBUF);
    if (screen == NULL) {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    // nueva imagen
    Image* imgSmile = new Image("Images/smile.bmp");

    SDL_Rect src, dest;
    src.x = 0;
    src.y = 0;
    src.w = imgSmile->getWidth();
    src.h = imgSmile->getHeight();
    dest.x = 50;
    dest.y = 20;
    dest.w = imgSmile->getWidth();
    dest.h = imgSmile->getHeight();
    SDL_BlitSurface(imgSmile->getSDLSurface(), &src, screen, &dest);
    SDL_Flip(screen);

    // nueva imagen
    Image* imgSmileRe = new Image("Images/smile.bmp");


    // Achico la imagen
    imgSmileRe->resize(10, 10);

    // Pego la imagen en la pantalla
    src.x = 0;
    src.y = 0;
    src.w = imgSmileRe->getWidth();
    src.h = imgSmileRe->getHeight();
    dest.x = 100;
    dest.y = 20;
    dest.w = imgSmileRe->getWidth();
    dest.h = imgSmileRe->getHeight();
    SDL_BlitSurface(imgSmileRe->getSDLSurface(), &src, screen, &dest);
    SDL_Flip(screen);


    //esperar para cerrar
    SDL_Event e;
    bool running = true;

    while(running) 
    {
        while(SDL_PollEvent(&e)) 
        {
            switch(e.type)
            {
                case SDL_QUIT:
                    running = false;
                    SDL_Quit();
                    break;
            }
        }
    }

    // libero memoria
    free(imgSmile);
    free(imgSmileRe);

    successCondition = true;

    return successCondition;

}  

以及调整大小的代码:

void Image::resize(int newWidth, int newHeight)
{
    int widthSrc    = this->getWidth();
    int heightSrc   = this->getHeight();

    // Empty new image
    Image* temp = new Image(newWidth, newHeight);

    int posXDst = 0;
    int posYDst = 0;

    for(int posYSrc = 0; posYSrc < heightSrc ; posYSrc++){


        for(int posXSrc = 0; posXSrc < widthSrc; posXSrc++){

            posXDst = (posXSrc * newWidth) / widthSrc;
            posYDst = (posYSrc * newHeight) / heightSrc;

            Uint32 pixelImg = this->getPixel(posXSrc, posYSrc);


            if( posXSrc > 0 && posYSrc > 0 && newWidth > widthSrc && newHeight > heightSrc ){

                int fromY = (((posYSrc-1) * newHeight) / heightSrc);
                int fromX = (((posXSrc-1) * newWidth) / widthSrc);

                // interpolacion interna
                for (int y = fromY; y <= posYDst; y++){
                    for (int x = fromX; x <= posXDst; x++){

                        // pixel y posicion superior izquierdo
                        Uint32 pixelSI = this->getPixel(posXSrc-1, posYSrc-1);
                        int xSI = ((posXSrc-1) * newWidth) / widthSrc;
                        int ySI = ((posYSrc-1) * newHeight) / heightSrc;
                        // pixel y posicion superior derecho
                        Uint32 pixelSD = this->getPixel(posXSrc, posYSrc-1);
                        int xSD = (posXSrc * newWidth) / widthSrc;
                        int ySD = ((posYSrc-1) * newHeight) / heightSrc;
                        // pixel y posicion inferior izquierdo
                        Uint32 pixelII = this->getPixel(posXSrc-1, posYSrc);
                        int xII = ((posXSrc-1) * newWidth) / widthSrc;
                        int yII = (posYSrc * newHeight) / heightSrc;

                        // obtengo el pixel interpolado
                        Uint32 interpolatedPixel = this->getInterpolatedPixel( pixelSI, xSI, ySI, 
                                                                            pixelSD, xSD, ySD, 
                                                                            pixelII, xII, yII, 
                                                                            pixelImg, posXDst, posYDst, 
                                                                            x, y, temp->getFormat());

                        // coloco el pixel en la imagen destino
                        temp->putPixel( interpolatedPixel, x, y );
                    }
                }
            }

            // Pongo el pixel en las nuevas coordenadas
            temp->putPixel( pixelImg, posXDst, posYDst);
        }
    }

    this->copy(*temp);
    free(temp);
} 

抱歉西班牙评论。 我使用的是 Windows XP/7,该图像是 24 位 BMP 文件中的笑脸,大小约为 20x20 像素。

Does anybody know if there is a min. size limit for BMP files in SDL framework to show them?
I'm asking you this because I tried but can not show correctly BMP files that are less than 20x20 pixels.(I'm using SDL for C++).

Code:

    bool successCondition = false;  
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    atexit(SDL_Quit);

    SDL_Surface *screen;

    screen = SDL_SetVideoMode(640, 480, 24, SDL_DOUBLEBUF);
    if (screen == NULL) {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    // nueva imagen
    Image* imgSmile = new Image("Images/smile.bmp");

    SDL_Rect src, dest;
    src.x = 0;
    src.y = 0;
    src.w = imgSmile->getWidth();
    src.h = imgSmile->getHeight();
    dest.x = 50;
    dest.y = 20;
    dest.w = imgSmile->getWidth();
    dest.h = imgSmile->getHeight();
    SDL_BlitSurface(imgSmile->getSDLSurface(), &src, screen, &dest);
    SDL_Flip(screen);

    // nueva imagen
    Image* imgSmileRe = new Image("Images/smile.bmp");


    // Achico la imagen
    imgSmileRe->resize(10, 10);

    // Pego la imagen en la pantalla
    src.x = 0;
    src.y = 0;
    src.w = imgSmileRe->getWidth();
    src.h = imgSmileRe->getHeight();
    dest.x = 100;
    dest.y = 20;
    dest.w = imgSmileRe->getWidth();
    dest.h = imgSmileRe->getHeight();
    SDL_BlitSurface(imgSmileRe->getSDLSurface(), &src, screen, &dest);
    SDL_Flip(screen);


    //esperar para cerrar
    SDL_Event e;
    bool running = true;

    while(running) 
    {
        while(SDL_PollEvent(&e)) 
        {
            switch(e.type)
            {
                case SDL_QUIT:
                    running = false;
                    SDL_Quit();
                    break;
            }
        }
    }

    // libero memoria
    free(imgSmile);
    free(imgSmileRe);

    successCondition = true;

    return successCondition;

}  

And the code for resize:

void Image::resize(int newWidth, int newHeight)
{
    int widthSrc    = this->getWidth();
    int heightSrc   = this->getHeight();

    // Empty new image
    Image* temp = new Image(newWidth, newHeight);

    int posXDst = 0;
    int posYDst = 0;

    for(int posYSrc = 0; posYSrc < heightSrc ; posYSrc++){


        for(int posXSrc = 0; posXSrc < widthSrc; posXSrc++){

            posXDst = (posXSrc * newWidth) / widthSrc;
            posYDst = (posYSrc * newHeight) / heightSrc;

            Uint32 pixelImg = this->getPixel(posXSrc, posYSrc);


            if( posXSrc > 0 && posYSrc > 0 && newWidth > widthSrc && newHeight > heightSrc ){

                int fromY = (((posYSrc-1) * newHeight) / heightSrc);
                int fromX = (((posXSrc-1) * newWidth) / widthSrc);

                // interpolacion interna
                for (int y = fromY; y <= posYDst; y++){
                    for (int x = fromX; x <= posXDst; x++){

                        // pixel y posicion superior izquierdo
                        Uint32 pixelSI = this->getPixel(posXSrc-1, posYSrc-1);
                        int xSI = ((posXSrc-1) * newWidth) / widthSrc;
                        int ySI = ((posYSrc-1) * newHeight) / heightSrc;
                        // pixel y posicion superior derecho
                        Uint32 pixelSD = this->getPixel(posXSrc, posYSrc-1);
                        int xSD = (posXSrc * newWidth) / widthSrc;
                        int ySD = ((posYSrc-1) * newHeight) / heightSrc;
                        // pixel y posicion inferior izquierdo
                        Uint32 pixelII = this->getPixel(posXSrc-1, posYSrc);
                        int xII = ((posXSrc-1) * newWidth) / widthSrc;
                        int yII = (posYSrc * newHeight) / heightSrc;

                        // obtengo el pixel interpolado
                        Uint32 interpolatedPixel = this->getInterpolatedPixel( pixelSI, xSI, ySI, 
                                                                            pixelSD, xSD, ySD, 
                                                                            pixelII, xII, yII, 
                                                                            pixelImg, posXDst, posYDst, 
                                                                            x, y, temp->getFormat());

                        // coloco el pixel en la imagen destino
                        temp->putPixel( interpolatedPixel, x, y );
                    }
                }
            }

            // Pongo el pixel en las nuevas coordenadas
            temp->putPixel( pixelImg, posXDst, posYDst);
        }
    }

    this->copy(*temp);
    free(temp);
} 

Sorry for the spanish comments.
I'm using windows xp/ 7 the image is a smiley face in 24bits BMP file aprox 20x20 pixels.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2024-11-11 08:24:30

理论上,BMP 大小没有限制(就 SDL 而言)。

Theoreticaly, there is no limit for the BMP size (as far as SDL is concerned).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文