Search code examples
gdi+drawingdecoder

GDI+ 's Amazing decode speed, and terrible draw speed!



Thanks for answers,Actually I am not puzzled about draw 1024*768 pixels is slower than 100* 100 pixels... It is so simple a logic.. Which made me puzzled is that DrawImage's interpolation algorithm may be very slow, while there exists lots of better algorithm, and its decoder seems can decode from a jpg with a certain resolution, it is really cool, I search for sometime but do not find any free lib to do this...

It is really strange! I add the following code into on Paint method. c:\1.jpg is 5M jpg file, about 4000*3000

//--------------------------------------------------------------

HDC hdc = pDC->GetSafeHdc();
bitmap = Bitmap::FromFile(L"c:\\1.jpg",true);
Graphics graphics(hdc);
graphics.SetInterpolationMode( InterpolationModeNearestNeighbor );
graphics.DrawImage(bitmap,0,0,200,200);

The above is really fast! even real time! I don't think decode a 5m JPG can be that fast!

//--------------------------------------------------------------

HDC hdc = pDC->GetSafeHdc();
bitmap = Bitmap::FromFile(L"c:\\1.jpg",true);
Graphics graphics(hdc);
graphics.SetInterpolationMode( InterpolationModeNearestNeighbor );
graphics.DrawImage(bitmap,0,0,2000,2000);

The above code become really slow

//--------------------------------------------------------------

If I add Bitmap = Bitmap::FromFile(L"c:\1.jpg", true); // into construct

leave

    Graphics graphics(hdc);
    graphics.SetInterpolationMode( InterpolationModeNearestNeighbor );
    graphics.DrawImage(bitmap,0,0,2000,2000);

in OnPaint method, The code is still a bit slow~~~

//------------------------------------------------------------------

Comparing with decoding, the drawImage Process is really slow...

Why and How did they do that? Did Microsoft pay the men taking charge of decoder double salary than the men taking charge of writing drawingImage?


Solution

  • You don't need to decode JPGs if you're scaling down by a factor of 8. JPG images consist of blocks of 8 by 8 pixels, DCT-transformed. The average value of this block is the 0,0 coefficient of the DCT. So, scaling down a factor of 8 is merely a matter of throwing away all other components. Scaling down even further (eg 4000->200) is just a matter of scaling down from 4000 to 500, and then scaling normally from 500 to 200 pixels.