🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: sci.image.processing
Article #296

Re: image processing loops: a c programming question

#296
From: Alberto Accomazzi
Date: Mon, 10 Aug 1992 22:12
57 lines
2004 bytes
From article <SLEHAR.92Aug7111245@kendall.bu.edu>, by slehar@kendall.bu.edu (Steve Lehar):
> 
> God forbid you should have to make any changes  to this  code!  I have
> tried to think of some kind of macro that would simplify things a bit,
> but I can't come up with anything  that is less  complicated than what
> you see here.  Does anyone have an elegant solution to this problem?
> 

This is what I use when I have to access the contents of an image on a 
pixel-to-pixel basis:

function (DATATYPE *data, int xsize, int ysize)
{
  int x, y;
#define PIX(a,b) ((a)+(b)*xsize)

  for (y = 0; y < ysize; y++)
    for (x = 0; x < xsize; x++)
      data[PIX(x,y)] = ...

}

this to me looks a little better than having the arithmetics explicitly 
plugged into the square brackets.  You could even use this macro:

function (DATATYPE *data, int xsize, int ysize)
{
  int x, y;
#define DATA(a,b) data[((a)+(b)*xsize)]

  for (y = 0; y < ysize; y++)
    for (x = 0; x < xsize; x++)
      DATA(x,y) = ...

}


A far as making the loop tight and fast, if you are only accessing your data
in a sequential way, the best thing to do is increment the pointer as in:


function (DATATYPE *data, int xsize, int ysize)
{
  int x, y;
  DATATYPE *dp;

  dp = data;
  for (y = 0; y < ysize; y++)
    for (x = 0; x < xsize; x++, dp++)
      *dp = ...

}

Inner loops can be written in a similar fashion.  As far as I know, this
will be as fast as you can get.

Hope this helps
Technical Details
Message-ID: <1992Aug10.221247.8016@cfa.harvard.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!think.com!paperboy.osf.org!hsdndev!cfa203!alberto
References: <SLEHAR.92Aug7111245@kendall.bu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!think.com!paperboy.osf.org!hsdndev!cfa203!alberto
From: alberto@cfa.harvard.edu (Alberto Accomazzi)
Newsgroups: sci.image.processing
Subject: Re: image processing loops: a c programming question
Message-ID: <1992Aug10.221247.8016@cfa.harvard.edu>
Date: 10 Aug 92 22:12:47 GMT
References: <SLEHAR.92Aug7111245@kendall.bu.edu>
Distribution: sci.image.processing
Organization: Smithsonian Astrophysical Observatory, Cambridge, MA,  USA
Lines: 57