CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Posts
    133

    [RESOLVED] Faster image drawing

    Hi

    I've made a little application that connect to a camera server that send throught sockets JFIF images composed of a vertical line of squares of 16x16px. In the header of the socket i can retreive the number of squares in the image and the position of each. The thing is that the drawing is kind of slow (about one seconds for 16 squares) I'd like to know if there is a method that draw jpeg faster. here's mine
    Code:
    for (int i =0; i < nbofframes; i++) {
    				try {
    					CWaitCursor wc;
    
    					int xScale = 16; //(320/20)
    					int yScale = 16; // (240/15)
    
    					int x = bytesReceived[0x64+(i*2)];
    				int y = bytesReceived[0x64+(i*2)+1];
    					
    
    						dataStream = new System::IO::MemoryStream(jpegData);
    
    						image = Image::FromStream(dataStream);
    						//image->Save(S"c:\\jpg\\jpg" + jpgFile + S".jpg");
    
    						graphics = Graphics::FromHdc(dcCam1.GetSafeHdc());
    
    						RECT clientRect;
    						_this->m_cam1.GetClientRect(&clientRect);
    						clientRect.top = yScale * y;
    						clientRect.left = xScale * x;
    						clientRect.right = 16+clientRect.left;
    						clientRect.bottom = 16+clientRect.top;
    						Drawing::Rectangle clRect(clientRect.left, clientRect.top, 16, 16);
    						Drawing::Rectangle imageRect(0, i*16, 16, 16);
    						graphics->FillRectangle(SystemBrushes::Control, clientRect.left, clientRect.top, clientRect.right-clientRect.left, clientRect.bottom - clientRect.top);
    						//graphics->DrawImage(image,0,0, image->Width, image->Height);
    						graphics->DrawImage(image,clRect, imageRect, GraphicsUnit::Pixel);
    				}
    				catch(Exception* e) {
    					AfxMessageBox((CString)e->Message);
    				}
    				__finally {
    					if (graphics) graphics->Dispose();
    					if (image) image->Dispose();
    					if (dataStream) dataStream->Dispose();
    				}
    			}
    Thanks

    Shootrz
    Louis-Philippe Frenette
    Arobas Informatique Granby
    http://www.arobasinformatique.com

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Faster image drawing

    Quote Originally Posted by shootrz
    I've made a little application that connect to a camera server that send throught sockets JFIF images composed of a vertical line of squares of 16x16px. In the header of the socket i can retreive the number of squares in the image and the position of each. The thing is that the drawing is kind of slow (about one seconds for 16 squares) I'd like to know if there is a method that draw jpeg faster.
    Are you sure that JPEG drawing is taking most of the time? Did you profile your code?
    I'd suggest that you at least put some timing marks around main suspects.
    It looks like the dataStream and the image itself do not depend on the loop iteration. Why do you recreate them in each iteration?
    Are your clientRect and clRect the same?
    Why do you FillRectangle before you DrawImage right over it?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Faster image drawing

    I'm echoing VladmirF's point, I'm certain it's not the display of the bitmaps.

    Once an image is decoded, it's a bitmap. It's not longer a jpeg, in that it's not longer an encoded, compressed storage container, it's a raw collection of pixels. Formats for such bitmaps differ, to be sure, but once the jpeg is decoded, speeds are usually fairly good.

    On a typical GDI bitblit (Windows), a 1024 x 768 24bpp bitmap can be blitted to a typical low end display card (AGP 4x, GeForce MX4000 for example) in a 32 bit 1.5 Ghz CPU at a rate somewhere around 30 to 45 blits per second.

    16 small bitmaps (even, say, 64 x 64) would fly by at hundreds of blits per second. These timings do not reflect any delay in the order of 1 second or more, unless something were wrong.

    I'm certain it's the decoding, and perhaps the "dataStream = new System::IO::MemoryStream(jpegData);" inside the loop.

    I suspect, too, there may be different performance results in managed vs unmanaged targets, but not to the extent you're observing.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Faster image drawing

    If your program is too slow for showing the image, you can attempt to
    use double-buffering technique as following:

    1. Draw the image in other memory area instead of screen memory area and do any process you want in this memory area such as cropping, resizing, etc.

    2. After finished to draw it, then call memory transfer function such as memcpy() to transfer the image to screen memory area.

    I used this technique long time ago and I cleaned up flicker and the problem like you faced.

    Good luck.

  5. #5
    Join Date
    Aug 2005
    Posts
    133

    Resolved Re: Faster image drawing

    Thanks again.

    All your posts were usefull.

    i move some of my variable out of my for loop because they could be reused insted of being reinitilized.

    Also doing the processing in memory remove the flicker.

    Thanks again
    Louis-Philippe Frenette
    Arobas Informatique Granby
    http://www.arobasinformatique.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured