Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:04:56+00:00 2026-06-04T19:04:56+00:00

Edited: Working on Windows platform. Problem: Less of a problem, more about advise. I’m

  • 0

Edited: Working on Windows platform.

Problem: Less of a problem, more about advise. I’m currently not incredibly versed in low-level program, but I am attempting to optimize the code below in an attempt to increase the performance of my overall code. This application depends on extremely high speed image processing.

Current Performance: On my computer, this currently computes at about 4-6ms for a 512×512 image. I’m trying to cut that in half if possible.

Limitations: Due to this projects massive size, fundamental changes to the application are very difficult to do, so things such as porting to DirectX or other GPU methods isn’t much of an option. The project currently works, I’m simply trying to figure out how to make it work faster.

Specific information about my use for this: Images going into this method are always going to be exactly square and some increment of 128. (Most likely 512 x 512) and they will always come out the same size. Other than that, there is not much else to it. The matrix is calculated somewhere else, so this is just the applying of the matrix to my image. The original image and the new image are both being used, so copying the image is necessary.

Here is my current implementation:

void ReprojectRectangle( double *mpProjMatrix, unsigned char *pDstScan0, unsigned char *pSrcScan0, 
        int NewBitmapDataStride, int SrcBitmapDataStride, int YOffset, double InversedAspect, int RectX, int RectY, int RectW, int RectH)
        {
            int      i, j;
            double   Xnorm, Ynorm;
            double  Ynorm_X_ProjMatrix4, Ynorm_X_ProjMatrix5, Ynorm_X_ProjMatrix7;;         
            double   SrcX, SrcY, T;
            int      SrcXnt, SrcYnt;
            int      SrcXec, SrcYec, SrcYnvDec;
            unsigned char   *pNewPtr, *pSrcPtr1, *pSrcPtr2, *pSrcPtr3, *pSrcPtr4;
            int      RectX2, RectY2;

            /* Compensate (or re-center) the Y-coordinate regarding the aspect ratio */
            RectY -= YOffset;   

            /* Compute the second point of the rectangle for the loops */
            RectX2 = RectX + RectW;
            RectY2 = RectY + RectH;

            /* Clamp values (be careful with aspect ratio */
            if (RectY < 0) RectY = 0;
            if (RectY2 < 0) RectY2 = 0;
            if ((double)RectY > (InversedAspect * 512.0)) RectY = (int)(InversedAspect * 512.0);
            if ((double)RectY2 > (InversedAspect * 512.0)) RectY2 = (int)(InversedAspect * 512.0);

            /* Iterate through each pixel of the scaled re-Proj */
            for (i=RectY; i<RectY2; i++)
            {      
            /* Normalize Y-coordinate and take the ratio into account */
            Ynorm = InversedAspect - (double)i / 512.0;

            /* Pre-compute some matrix coefficients */
            Ynorm_X_ProjMatrix4 = Ynorm * mpProjMatrix[4] + mpProjMatrix[12];
            Ynorm_X_ProjMatrix5 = Ynorm * mpProjMatrix[5] + mpProjMatrix[13];
            Ynorm_X_ProjMatrix7 = Ynorm * mpProjMatrix[7] + mpProjMatrix[15];

            for (j=RectX; j<RectX2; j++)
            {
                 /* Get a pointer to the pixel on (i,j) */
                 pNewPtr = pDstScan0 + ((i+YOffset) * NewBitmapDataStride) + j;

                 /* Normalize X-coordinates */
                 Xnorm = (double)j / 512.0;                  

                 /* Compute the corresponding coordinates in the source image, before Proj and normalize source coordinates*/
                 T =    (Xnorm * mpProjMatrix[3] + Ynorm_X_ProjMatrix7);
                 SrcY = (Xnorm * mpProjMatrix[0] + Ynorm_X_ProjMatrix4)/T;
                 SrcX = (Xnorm * mpProjMatrix[1] + Ynorm_X_ProjMatrix5)/T;

                // Compute the integer and decimal values of the coordinates in the sources image                     
                SrcXnt = (int) SrcX;
                SrcYnt = (int) SrcY;
                SrcXec = 64 - (int) ((SrcX - (double) SrcXnt) * 64);
                SrcYec = 64 - (int) ((SrcY - (double) SrcYnt) * 64);

                // Get the values of the four pixels up down right left
                pSrcPtr1 = pSrcScan0 + (SrcXnt * SrcBitmapDataStride) + SrcYnt;
                pSrcPtr2 = pSrcPtr1 + 1;
                pSrcPtr3 = pSrcScan0 + ((SrcXnt+1) * SrcBitmapDataStride) + SrcYnt;
                pSrcPtr4 = pSrcPtr3 + 1;

                SrcYnvDec = (64-SrcYec);

                (*pNewPtr) = (unsigned char)(((SrcYec * (*pSrcPtr1) + SrcYnvDec * (*pSrcPtr2)) * SrcXec + 
                                         (SrcYec * (*pSrcPtr3) + SrcYnvDec * (*pSrcPtr4)) * (64 - SrcXec)) >> 12);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:04:58+00:00Added an answer on June 4, 2026 at 7:04 pm

    Two things that could help: multiprocessing and SIMD. With multiprocessing you could break up the output image into tiles and have each processor work on the next available tile. You can use SIMD instructions (like SSE, AVX, AltiVec, etc.) to calculate multiple things at the same time, such as doing the same matrix math to multiple coordinates at the same time. You can even combine the two – use multiple processors running SIMD instructions to do as much work as possible. You didn’t mention what platform you’re working on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem getting SvnKit working on 64 bit Windows 7. I open
EDIT: I have edited my post... Working on a project (c#), I have a
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
We are working to certify our application for deployment on Windows Server 2008 and
Edited to make my requirements a little more clear I would love to find
I am trying to get gVim working on a windows 7 machine and am
Given a working section of Windows Workflow within the designer, I'd like to use
I'm working with a git repository on both windows and linux/mac. When I create
I'm trying to get Redmine working on Windows/IIS 7. I've found several walkthroughs online,
I'm working on a windows forms application (C#) where a user is entering data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.