Solved

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

Posted on 2006-06-12
1
242 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Client socket stops responding on client 9 59
Cchar * Concating 7 91
getting a fire fox page (view page source ) in c++ 6 42
MS Visual Studio 2015 - Install Error 5 82
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…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

16 Experts available now in Live!

Get 1:1 Help Now