?
Solved

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

Posted on 2006-06-12
1
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 49

Accepted Solution

by:
DanRollins earned 375 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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