Solved

Why is GDI+ so slow when extracting part of the photo?

Posted on 2006-06-12
1
241 Views
Last Modified: 2010-04-24
I'm using GDI+ to load photos.
If I load the full region of a 500m pixel jpg photo(2592*1944) into a 1024*1024 hdc, it takes 250ms.
But if I load the center square only (1944*1944), and scale it into 1024*1024, this takes 500ms, twice the time!
Shouldn't it be easier to decode just part of the jpg?
Besides, in this case I have to use RectF structure, which uses float point to define the rectangle, why should it be float?
My CPU: athlon xp 2500, OS: winxp pro sp2, VC++ 2005
My code is:

                #define HDC_W 1024
                #define HDC_H 1024

      Image img( m_photoPath );
      int oriWidth = img.GetWidth();
      int oriHeight = img.GetHeight();
      
      Status result = Ok;

      if (m_cropNum == 0)
      {
            // no cropping
            int zoomX, zoomY, zoomWidth, zoomHeight;

            if (oriWidth >= oriHeight)
            {
                  zoomWidth = HDC_W;
                  zoomHeight = oriHeight * HDC_H / oriWidth;

                  zoomX = 0;
                  zoomY = (HDC_H - zoomHeight)/2;

            }
            else
            {
                  zoomHeight = HDC_H;
                  zoomWidth = oriWidth * HDC_W / oriHeight;

                  zoomX = (HDC_W - zoomWidth) / 2;
                  zoomY = 0;

            }

            RECT rc;
            ::SetRect(&rc, 0, 0, HDC_W, HDC_H);
            ::FillRect(m_hdc, &rc, (HBRUSH)::GetStockObject(BLACK_BRUSH));

            result = m_pGraphics->DrawImage(&img, zoomX, zoomY, zoomWidth, zoomHeight);
      }
      else
      {
            // crop the center square out and show that only


            int srcX, srcY, srcWidth, srcHeight;

            if (oriWidth >= oriHeight)
            {
                  srcX = (oriWidth - oriHeight) / 2;
                  srcY = 0;
                  srcWidth = srcHeight = oriHeight;
            }
            else
            {
                  srcX = 0;
                  srcY = (oriHeight - oriWidth) / 2;
                  srcWidth = srcHeight = oriWidth;
            }


            RectF dst(0.0f, 0.0f, (float)HDC_W, (float)HDC_H);

            result = m_pGraphics->DrawImage(&img, dst, srcX, srcY, srcWidth, srcHeight, UnitPixel, NULL);
                   }
0
Comment
Question by:softimage
1 Comment
 
LVL 49

Accepted Solution

by:
DanRollins earned 125 total points
ID: 16891270
All work with an image is doen internally with bitmaps.  A 500M pixel image is 2Gb of data (four bytes per pixel).  That's more than most systems can keep in RAM at once.

JPEG encoding is not linear... that is there is no one-to-one corespondance between a scanline of the displyable bitmap and a particular part of the JPG file.  The system cannot just jump to say, offset 10Mb and start decoding... It pretty much needs to decode the whole thing.  

When you are decoding into a  2592*1944 image into a 1024*1024 hdc, the system can scale each of the JPG features as it goes.  It does not need to have the entire image in memory at once.

But when accessing a subset, the entire image must be decoded into RAM (2592*1944*4 = 20Mb) and then the central part (1K*1K*4= 5MB) is blitted into the destination hdc.  Using 24MB of RAM might force the system to use virtual memory and swap out some other programs or data to make room for it.  That operation is time-consuming.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now