1

ゲームやその他のアプリケーション用に独自の画像フォーマットを開発しており、それらの画像の画像ビューアを作成したいと考えています。問題は、常により大きな画像 (1920x1080) をレンダリングしようとすると、少なくとも 10 秒かかり、アプリケーションが数秒間応答を停止し、すべてが消えることです。

class Renderer{
    //...
    inline void GetRGB(unsigned int color,unsigned char &r,unsigned char &g,unsigned char &b){
        r=((color>>16)&0xff); g=((color>>8)&0xff); b=((color)&0xff);
    }
    inline bool IsValidPos(unsigned int x,unsigned int y){
        if((x>=0&&x<width)&&(y>=0&&y<height))return true;
        else return false;
    }
    inline void GetPoint(unsigned int x,unsigned int y,unsigned int &color){
        if(IsValidPos(x,y))
            color=photostream[y*height+x];
    }
    void DrawPoint(unsigned int& x,unsigned int& y){
        unsigned char r,g,b;
        unsigned int col;
        GetPoint(x,y,col);
        //GetRGB(col,r,g,b);
        SetPixel(hDC,x,y,col);
    }
};
void RenderSHMP(ImageSHMP<unsigned int>& img,Renderer *renderer){
    ImageSHMP<unsigned int> tmpImg(img.GetX(),img.GetY()); //create new image, this one will be shown
    for(unsigned int x=0;x<tmpImg.GetX();x++)
        for(unsigned int y=0;y<tmpImg.GetY();y++)
            tmpImg.SetPoint(x,y,img.GetPoint(x,y)); //copy all data
    if(img.GetX()>WIDTH||img.GetY()>HEIGHT){ //resize image if it is bigger than window resolution
        double scale=1.0;
        scale=static_cast<double>(WIDTH)/static_cast<double>(img.GetX());
        tmpImg.Scale(scale);
    }
    //code above takes max. 0,2 seconds to execute
    unsigned int col=0;
    for(unsigned int x=0;x<tmpImg.GetX();x++)
        for(unsigned int y=0;y<tmpImg.GetY();y++){
            col=tmpImg.GetPoint(x,y);
            renderer->SetPoint(x,y,col);    //sets color in memory for later use
            renderer->DrawPoint(x,y);
        }
}

ImageSHMP には、幅 * 高さに等しいタイプ T の単純なバッファーが含まれています。すべてのデータはこのバッファに保存されるため、たとえばポイント [20,30] は [30*height+20] に配置されます。ウィンドウが「応答なし」状態にならないように、描画を高速化するにはどうすればよいですか? つまり、単純な Windows イメージ ビューアーと同じくらいの速さです。


最終的なコード:

void RenderSHMP(ImageSHMP<unsigned int>& img,Renderer *renderer){
    ImageSHMP<unsigned int> tmpImg(img.GetX(),img.GetY());
    for(unsigned int x=0;x<tmpImg.GetX();x++)
        for(unsigned int y=0;y<tmpImg.GetY();y++)
            tmpImg.SetPoint(x,y,img.GetPoint(x,y));
    if(img.GetX()>WIDTH||img.GetY()>HEIGHT){
        double scale=1.0;
        scale=static_cast<double>(WIDTH)/static_cast<double>(img.GetX());
        tmpImg.Scale(scale);
    }
    int w=tmpImg.GetX(),h=tmpImg.GetY();
    unsigned char r=0,g=0,b=0;
    BITMAPINFO bmi;
    HBITMAP hbm;
    unsigned char *bytes=new unsigned char[w*h*3];
    HDC hdc;
    renderer->GetHDC(hdc);
    unsigned int ctr=0;
    ZeroMemory(&bmi, sizeof(bmi));
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
    bmi.bmiHeader.biWidth = w;
    bmi.bmiHeader.biHeight = h;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 24;
    bmi.bmiHeader.biCompression = BI_RGB;
    hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS,(void **)&bytes, NULL, 0);
    HDC compatibleDC=CreateCompatibleDC(hdc);
    HBITMAP hbmp = CreateCompatibleBitmap  (hdc, w, h);
    for(int y=int(h)-1;y>=0;--y){
        for(int x=0;x<int(w);++x){
            renderer->GetRGB(tmpImg.GetPoint(x,y),r,g,b);
            bytes[ctr++]=b;
            bytes[ctr++]=g;
            bytes[ctr++]=r;
        }
    }
    SetDIBits (hdc, hbmp, 0, h, bytes,&bmi, DIB_RGB_COLORS);
    hbmp=(HBITMAP)SelectObject (compatibleDC, hbmp);
    BitBlt(hdc, 0, 0, w, h, compatibleDC, 0, 0, SRCCOPY);
    DeleteObject(SelectObject(compatibleDC, hbmp));
    DeleteDC(compatibleDC);
    delete [] bytes;
}
4

2 に答える 2