Click to See Complete Forum and Search --> : [RESOLVED] Faster image drawing


shootrz
August 7th, 2007, 06:49 PM
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

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

VladimirF
August 8th, 2007, 11:38 AM
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?

JVene
August 8th, 2007, 04:10 PM
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.

henky@nok.co.id
August 9th, 2007, 03:34 AM
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. :)

shootrz
August 10th, 2007, 03:08 PM
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