Click here to Skip to main content
15,915,091 members
Home / Discussions / Graphics
   

Graphics

 
GeneralRe: this.CreateGraphics() Issue Pin
Christian Graus6-Mar-08 21:29
protectorChristian Graus6-Mar-08 21:29 
GeneralRe: this.CreateGraphics() Issue Pin
Harvey Saayman6-Mar-08 21:43
Harvey Saayman6-Mar-08 21:43 
GeneralPush mode/Pull mode Pin
Sugantha5-Mar-08 19:55
Sugantha5-Mar-08 19:55 
GeneralRe: Push mode/Pull mode Pin
Mark Salsbery6-Mar-08 5:59
Mark Salsbery6-Mar-08 5:59 
Generalrendering video Pin
jossion4-Mar-08 19:16
jossion4-Mar-08 19:16 
GeneralRe: rendering video Pin
Mark Salsbery4-Mar-08 19:46
Mark Salsbery4-Mar-08 19:46 
GeneralRe: rendering video Pin
jossion4-Mar-08 22:52
jossion4-Mar-08 22:52 
GeneralRe: rendering video [modified] Pin
Mark Salsbery5-Mar-08 5:59
Mark Salsbery5-Mar-08 5:59 
First, your code didn't irritate me.  You stated "Could anyone debug it out." and
I tried to do that. Smile | :)

Here's a breakdown of the rendering problem...

SetPixel is a really bad choice for rendering video, so moving to a bit-block-transfer
implementation is the right direction.

To use BitBlt()/StretchBlt(), you need a source DC with a bitmap containing the pixels
you want to render selected into it.

Since your pixel data comes in raw int format, you need a HBITMAP bitmap that allows you direct access
to its pixel data - thats what a DIBSection is for. 

Once you have all that, there's more that can be optimized - You don't have to recreate the DIB section
every time you render a frame.  It can be created once, before you start rendering frames.
Same applies to the memory DC - it only needs to be created once and have the DIBSection selected into it.
That means every 40ms, you only need to copy the pixel bits to the DIBSection, get a destination DC
for where you're going to draw, and blt.

Here's an expanded example...
<span style="color: Green;">// Video renderer variables</span>
extern int disp_frame[50][144][176];
LONG VideoWidth = 176;
LONG VideoHeight = 144;
WORD BitsPerPixel = 8;
LONG BytesPerDIBSectionRow = (((VideoWidth * (long)BitsPerPixel + 31L) & (~31L)) / 8L);
BITMAPINFO *pBMI = 0;
BYTE *pBitmapBits = 0;
HBITMAP hDIBSection = 0;
HGDIOBJ hOldMemBitmap = 0;
HDC memDC = 0;


bool InitVideoRenderer()
{
    pBMI = (BITMAPINFO *)new BYTE[sizeof(BITMAPINFO) + 255 * sizeof(RGBQUAD)];

    pBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pBMI->bmiHeader.biWidth = VideoWidth;
    pBMI->bmiHeader.biHeight = VideoHeight;
    pBMI->bmiHeader.biPlanes = 1;
    pBMI->bmiHeader.biBitCount = BitsPerPixel;
    pBMI->bmiHeader.biCompression = BI_RGB;
    pBMI->bmiHeader.biSizeImage = 0;
    pBMI->bmiHeader.biXPelsPerMeter = 0;
    pBMI->bmiHeader.biYPelsPerMeter = 0;
    pBMI->bmiHeader.biClrUsed = 0;
    pBMI->bmiHeader.biClrImportant = 0;

    for (int color_index = 0; color_index < 256; color_index++)
    {
        pBMI->bmiColors[color_index].rgbBlue = color_index;
        pBMI->bmiColors[color_index].rgbGreen = color_index;
        pBMI->bmiColors[color_index].rgbRed = color_index;
        pBMI->bmiColors[color_index].rgbReserved = 0;
    }

    hDIBSection = ::CreateDIBSection(NULL, pBMI, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

    if (hDIBSection)
    {
        memDC = ::CreateCompatibleDC(0);
        hOldMemBitmap = ::SelectObject(memDC, hDIBSection);

        return true;
    }
    else
    {
        delete[] (BYTE *)pBMI;
        pBMI = 0;

        return false;
    }

    
}

void RenderFrame(int FrameIndex, HWND destwnd, int x, int y)
{
    <span style="color: Green;">// Copy raw pixel data to the DIBSection pixel bits</span>

    BYTE *pCurDIBSectionRow = pBitmapBits;
    for (int row = 0; row < VideoHeight; row++)
    {
        for (int col = 0; col < VideoWidth; col++)
        {
            pCurDIBSectionRow[col] = (BYTE)(disp_frame[FrameIndex][row][col] >> 24); <span style="color: Green;">// Convert 32bpp to 8bpp...adjust for actual source int format</span>
        }
        pCurDIBSectionRow += BytesPerDIBSectionRow;
    }

    <span style="color: Green;">// Render the frame</span>

    HDC ClientDC = ::GetDC(destwnd);
    ::BitBlt(ClientDC, x, y, pBMI->bmiHeader.biWidth, pBMI->bmiHeader.biHeight, memDC, 0, 0, SRCCOPY);
    ::ReleaseDC(destwnd, ClientDC);
}

void CleanupVideoRenderer()
{
    if (hDIBSection)
    {
        ::SelectObject(memDC, hOldMemBitmap);
        hOldMemBitmap = 0;
        ::DeleteDC(memDC);
        memDC = 0;
        ::DeleteObject(hDIBSection);
        hDIBSection = 0;
        delete[] (BYTE *)pBMI;
        pBMI = 0;
    }
}


<span style="color: Green;">// example use...</span>

    if (InitVideoRenderer())
    {
        RenderFrame(0, *this, 0, 0);  <span style="color: Green;">// (call this every 40ms with proper index, window handle, and window coordinates!)</span>

        CleanupVideoRenderer();
    }

*edit* fixed 32bpp source to 8bpp destination pixel data conversion



Last modified: 10hrs 52mins after originally posted --


Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: rendering video Pin
jossion5-Mar-08 16:19
jossion5-Mar-08 16:19 
GeneralRe: rendering video Pin
Mark Salsbery5-Mar-08 16:50
Mark Salsbery5-Mar-08 16:50 
GeneralRe: rendering video Pin
jossion23-Feb-09 21:41
jossion23-Feb-09 21:41 
GeneralRe: rendering video Pin
Mark Salsbery24-Feb-09 5:39
Mark Salsbery24-Feb-09 5:39 
GeneralRe: rendering video Pin
jossion24-Feb-09 16:49
jossion24-Feb-09 16:49 
QuestionScreen Capture over Terminal Server Pin
intonet4-Mar-08 3:29
intonet4-Mar-08 3:29 
QuestionHow to mirror the screen. Pin
Tino126-Feb-08 20:58
Tino126-Feb-08 20:58 
AnswerRe: How to mirror the screen. [modified] Pin
Smithers-Jones28-Feb-08 11:10
Smithers-Jones28-Feb-08 11:10 
QuestionHow to draw text in OpenGL and what is problem in this Code Pin
Yasir Nawazish Ali26-Feb-08 17:10
Yasir Nawazish Ali26-Feb-08 17:10 
GeneralDraw text in C# using OpenGL Pin
Yasir Nawazish Ali26-Feb-08 0:17
Yasir Nawazish Ali26-Feb-08 0:17 
GeneralRe: Draw text in C# using OpenGL Pin
El Corazon26-Feb-08 6:02
El Corazon26-Feb-08 6:02 
GeneralRe: Draw text in C# using OpenGL Pin
Yasir Nawazish Ali26-Feb-08 17:07
Yasir Nawazish Ali26-Feb-08 17:07 
QuestionRender bitmap Pin
robban5145143225-Feb-08 20:38
robban5145143225-Feb-08 20:38 
QuestionHow to apply a texture to an irregular shape using GDI+ ? Pin
KaKa'24-Feb-08 5:06
KaKa'24-Feb-08 5:06 
GeneralRe: How to apply a texture to an irregular shape using GDI+ ? Pin
Shog924-Feb-08 9:40
sitebuilderShog924-Feb-08 9:40 
QuestionHow i can improve this website Pin
Saqib Yaqub24-Feb-08 4:19
Saqib Yaqub24-Feb-08 4:19 
AnswerRe: How i can improve this website http://www.whites-stationery.co.uk/ Pin
Pete O'Hanlon24-Feb-08 10:11
mvePete O'Hanlon24-Feb-08 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.