1

我正在写一个 gif 解码器,这个图像是一个动画图像。当我写第一帧时,它显示得很好。当我显示第二帧时,它只显示更改的像素。其他像素自动变为黑色。不知道为什么?。

我的第一帧有完整的画面。第二帧再次只改变了像素,它包含了其余未改变的像素。

现在,当我绘制第二个缓冲区时,它也会重绘未更改的像素。并且未更改的像素被绘制为黑色(或者恰好在监视器中我看到这些未更改的像素不存在)。那是它必须绘制第二帧的时候。它绘制了更改的像素(这是正确的),但它也重新绘制了未更改的像素。而这个不变的像素被视为黑色(即没有颜色)。我觉得这是一个令人耳目一新的问题。或者它可能是别的东西。帮助表示赞赏。

必需:它应该重新绘制完整的图像。简而言之,这是我的功能的片段。

不幸的是,它清除了以前的显示 - linux framebuffer。我想停止清除 linux 帧缓冲区。

这是完整的文件。

/** This is using the Direct Fb calls here; and is tightly coupled with Linux Framebuffer **/
static int fbfd = 0;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static long int screensize = 0;
static char *fbp = 0;
static int x = 0, y = 0;
static long int location = 0;

/** This is a clone to linux Frame buffer, and will be called to dump on Framebuffer **/
char *local_display_mem;

/** local functions **/
static void SetBackground(FrameData *tempInfo);
static void SetPixel(char *fbp, unsigned int x, unsigned int y, Byte red, Byte green, Byte blue);


/** This is the entry function to initialize the display **/
void display_init()
{

    // Open the file for reading and writing
    fbfd = open("/dev/fb0", O_RDWR);
    if (fbfd == -1)
    {
        perror("cannot open framebuffer device");
        exit(1);
    }
    #ifdef DEBUG
    printf("The framebuffer device was opened successfully.\n");
    #endif

    /** Read the Screen Information **/
        if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1)
        {
          perror("Driver error-- reading fixed information");
          exit(1);
        }

    // Get variable screen information

    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1)
    {
        perror("Error reading variable information");
        exit(1);
    }

    #ifdef DEBUG
    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
    #endif

      // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);

    local_display_mem = (char*)malloc(screensize);

    if ((int)fbp == -1)
     {
        perror("Error: mmap failed\r\n");
        exit(1);
     }
    #ifdef DEBUG
    printf("The framebuffer device was mapped to memory successfully.\n");
    #endif

    printf("Shreyas..Display Initialized..\r\n");


   //munmap(fbp, screensize);
   //close(fbfd);

}

/** This function is called by gif_read to display the Image **/

void Display(FrameData *FrameInfo)
{

    short int ImageStartX = 0;
    short int ImageStartY = 0;
    int Index = 0;

    printf("\r\n INFO: Display Called.\r\n");

    while(1)
    {

        Index = 0;
        ImageStartX = (FrameInfo->frameScreenInfo.LeftPosition);
        ImageStartY = (FrameInfo->frameScreenInfo.TopPosition);


        while(ImageStartY < ((FrameInfo->frameScreenInfo.ImageHeight)+(FrameInfo->frameScreenInfo.TopPosition)))
        {

            while(ImageStartX < ((FrameInfo->frameScreenInfo.ImageWidth)+(FrameInfo->frameScreenInfo.LeftPosition)))
            {
                if(FrameInfo->frame[Index] != FrameInfo->transperencyindex)
                {
                  SetPixel(local_display_mem,ImageStartX,ImageStartY,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Red,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Green,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Blue);

                }


                Index++;
                ImageStartX++;
            }


            ImageStartY++;


            ImageStartX=(FrameInfo->frameScreenInfo.LeftPosition);
        }

        printf("INFO:..Dumping Framebuffer\r\n");

        memcpy(fbp,local_display_mem,screensize);

        /** Tune this multiplication to meet the right output on the display **/
        usleep((FrameInfo->InterFrameDelay)*100000);

        if( FrameInfo->DisposalMethod == 2)
        {
            printf("set the Background\r\n");
            SetBackground(FrameInfo);
        }
        FrameInfo = FrameInfo->Next;

     }


}

static void SetBackground(FrameData *tempInfo)
{

    unsigned int ImageStartX=0;
    unsigned int ImageStartY=0;


    ImageStartX=(tempInfo->frameScreenInfo.LeftPosition);
    ImageStartY=(tempInfo->frameScreenInfo.TopPosition);


    while(ImageStartY<(tempInfo->frameScreenInfo.ImageHeight))
    {

        while(ImageStartX<(tempInfo->frameScreenInfo.ImageWidth))
        {

           SetPixel(local_display_mem,ImageStartX,ImageStartY,255,255,255);

            ImageStartX++;
        }

        ImageStartX=(tempInfo->frameScreenInfo.LeftPosition);
        ImageStartY++;


    }



}

static void SetPixel(char *fbp_lc, unsigned int x, unsigned int y, Byte red, Byte green, Byte blue)
{
           //printf("Shreyas..set pixel called\r\n");
           location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

            if (vinfo.bits_per_pixel == 32)
            {
                *(fbp_lc + location) =     blue;        // Some blue
                *(fbp_lc + location + 1) = green;     // A little green
                *(fbp_lc + location + 2) = red;    // A lot of red
                *(fbp_lc + location + 3) = 0;      // No transparency
                //location += 4;
            }
            else
            { //assume 16bpp
                unsigned short int t = red<<11 | green << 5 | blue;
                *((unsigned short int*)(fbp_lc + location)) = t;
            }

             //printf("Shreyas..set pixel exit called\r\n");

}







/** This is windows version of display function, and it works correctly.
void Display(FrameData *FrameInfo)
{


    short int ImageStartX=0;
    short int ImageStartY=0;
    int Index=0;



    DisplayCntrl=GetDC(hWnd);

        printf("Shreyas.. Display Init is called\r\n");

    //display_init();


    while(1)
    {

        Index=0;
        ImageStartX=(FrameInfo->frameScreenInfo.LeftPosition);
        ImageStartY=(FrameInfo->frameScreenInfo.TopPosition);   

        while(ImageStartY<((FrameInfo->frameScreenInfo.ImageHeight)+(FrameInfo->frameScreenInfo.TopPosition)))
        {

            while(ImageStartX<((FrameInfo->frameScreenInfo.ImageWidth)+(FrameInfo->frameScreenInfo.LeftPosition)))
            {
                if(FrameInfo->frame[Index]!=FrameInfo->transperencyindex)
                    SetPixel(DisplayCntrl,ImageStartX,ImageStartY,RGB(((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Red,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Green,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Blue));

                Index++;
                ImageStartX++;
            }


            ImageStartY++;


            ImageStartX=(FrameInfo->frameScreenInfo.LeftPosition);
        }



        Sleep((FrameInfo->InterFrameDelay*10));
        WaitForSingleObject(hWnd,10); 
         if(    FrameInfo->DisposalMethod==2)
        {

            SETBACKGROUND(FrameInfo);
        }
        FrameInfo=FrameInfo->Next;

    }



}

这是相同代码的 Windows 版本。

extern  hWnd;
HDC DisplayCntrl;



void SETBACKGROUND(FrameData *tempInfo)
{

    unsigned int ImageStartX=0;
    unsigned int ImageStartY=0;



    ImageStartX=(tempInfo->frameScreenInfo.LeftPosition);
    ImageStartY=(tempInfo->frameScreenInfo.TopPosition);


    while(ImageStartY<(tempInfo->frameScreenInfo.ImageHeight))
    {

        while(ImageStartX<(tempInfo->frameScreenInfo.ImageWidth))
        {



            SetPixel(DisplayCntrl,ImageStartX,ImageStartY,RGB(255,255,255));

            ImageStartX++;
        }

        ImageStartX=(tempInfo->frameScreenInfo.LeftPosition);
        ImageStartY++;


    }



}


void Display(FrameData *FrameInfo)
{



    short int ImageStartX=0;
    short int ImageStartY=0;
    int Index=0;



    DisplayCntrl=GetDC(hWnd);


     printf("the size of short int is %d",sizeof(short int));

    while(1)
    {

        Index=0;
        ImageStartX=(FrameInfo->frameScreenInfo.LeftPosition);
        ImageStartY=(FrameInfo->frameScreenInfo.TopPosition);   

        while(ImageStartY<((FrameInfo->frameScreenInfo.ImageHeight)+(FrameInfo->frameScreenInfo.TopPosition)))
        {

            while(ImageStartX<((FrameInfo->frameScreenInfo.ImageWidth)+(FrameInfo->frameScreenInfo.LeftPosition)))
            {
                if(FrameInfo->frame[Index]!=FrameInfo->transperencyindex)
                {
                    SetPixel(DisplayCntrl,ImageStartX,ImageStartY,RGB(((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Red,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Green,((FrameInfo->CMAP)+(FrameInfo->frame[Index]))->Blue));
                }

                Index++;
                ImageStartX++;
            }


            ImageStartY++;


            ImageStartX=(FrameInfo->frameScreenInfo.LeftPosition);
        }



        Sleep((FrameInfo->InterFrameDelay*10));
        WaitForSingleObject(hWnd,10); 
    if( FrameInfo->DisposalMethod==2)
        {

            SETBACKGROUND(FrameInfo);
        }
        FrameInfo=FrameInfo->Next;

    }



}
4

1 回答 1

1

由于您使用本地内存缓冲区local_display_mem,因此是否有人会清除帧缓冲区并不重要 - 这memcpy将覆盖每个像素。

这意味着条件FrameInfo->frame[Index] != FrameInfo->transperencyindex总是true出于某种原因,因为这将导致算法再次设置每个像素,而不是仅更新更改的像素。

于 2013-08-26T11:53:46.453 回答