🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: sci.image.processing
Article #274

Re: image processing loops: a c programming question

#274
From: Bryan Morse
Date: Fri, 07 Aug 1992 20:46
65 lines
2834 bytes
In article <SLEHAR.92Aug7111245@kendall.bu.edu> writes:
>
>I    have given much  thought to    an agonizing  trade-off in   image
>processing   loops in   c,  and have   not  come   up with a   totally
>satisfactory solution-   I wonder  whether    anyone  has  any    good
>suggestions.

[examples deleted]

>This works o.k,  but  the problem now is  that   you are performing  a
>multiplication  in  the innermost  loop  of the  program,  which slows
>things down quite a bit.  You  can fix this  by using offsets that are
>incremented  inside    the     loop- with    addition     instead   of
>multiplication...

A good optimizing compiler will correct this problem.  *If* you
have a good optimizer, the best solution is usually to write the loops
in the most straightforward manner possible.  The multiplications
can usually be written into a nice macro and the source code stays
pretty clean.  In general, let the compiler/optimizer do its work
and then see where it doesn't do well enough.

If you must microoptimize (which I must do on one of the machines I'm
working on), then try incrementing pointers.

For example, your code becomes

   unsigned char *p = data;
   for(y=0;y<ysize;y++){
      for(x=0;x<xsize;x++,p++){
         *p = ...
      }
   }

Efficient execution and pretty clean source.

Your convolution code becomes (something like)

        unsigned char *im1, *im2, *f;
        im1 = img1->data + ...;
        /**** for each pixel ****/
        for(y=hafy;y<ysize-hafy;y++){
          for(x=hafx;x<xsize-hafx;x++,im2++){
            *im2 = 0.0;
            /**** for each filter element ****/
            im1 = img1->data+ ...;
            f = filt->data + ...;
            for(yy=0;yy<fysize;yy++){
              for(xx=0;xx<fxsize;xx++,im1++,f++){
                *im2 += *im1 * *f;
              }
          }
        }

I'll leave you to figure out the correct pointer initializations
for the indexing variables that you are using, but you should get
the basic idea.  Notice the reinitialization inside the outermost
loop (the destination image) to accomodate the separate loops over 
the source image and the filter (technically, kernel).

I hope this helps.

-- 
Bryan Morse                University of North Carolina at Chapel Hill
morse@cs.unc.edu           Department of Computer Science
Technical Details
Message-ID: <14370@borg.cs.unc.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!concert!borg!satie!morse
References: <SLEHAR.92Aug7111245@kendall.bu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!concert!borg!satie!morse
From: morse@satie.cs.unc.edu (Bryan Morse)
Newsgroups: sci.image.processing
Subject: Re: image processing loops: a c programming question
Message-ID: <14370@borg.cs.unc.edu>
Date: 7 Aug 92 20:46:52 GMT
References: <SLEHAR.92Aug7111245@kendall.bu.edu>
Sender: news@cs.unc.edu
Distribution: sci.image.processing
Organization: The University of North Carolina at Chapel Hill
Lines: 65