🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: sci.image.processing
Article #269

Re: image processing loops: a c programming question

#269
From: Thomas M. Breuel
Date: Fri, 07 Aug 1992 23:35
79 lines
2996 bytes
In article <SLEHAR.92Aug7111245@kendall.bu.edu> slehar@kendall.bu.edu (Steve Lehar) 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.
 > 
 > [...]
 > 
 > now it is much more elegant to pass the  image size in with the image,
 > to make   the  routine more  general.  
 > 
 >   subroutine(data,xsize,ysize)
 >   int xsize,ysize;
 >   unsigned char data[ysize][xsize];  <=== PROBLEM!
 >   {
 >     int x,y;
 > 
 >     for(y=0;y<YSIZE;y++){
 > 	 for(x=0;x<XSIZE;x++){
 > 	   data[y][x] = ...
 > 	 }
 >     }
 >   }
 > 
 > The problem is that c will not allow the array data[][] to be declared
 > without   specifying  at compile-time at  LEAST  the  dimension of the
 > xsize.

In C, the best solution is to use an array of pointers. For example,

   unsigned char **ucialloc(int w, int h)
   {
   	unsigned char *base = malloc(w*h*sizeof (unsigned char));
   	unsigned char **ptr = malloc(w*sizeof (unsigned char *));
   	int i;
   	for(i=0;i<w;i++) ptr[i]=base+i*h;
   	return ptr;
   }

   void ucifree(unsigned char **ptr)
   {
   	free(ptr[0]);
   	free(ptr);
   }

The resulting object can be passed around and used like a
multidimensional array:

   void invert(unsigned char **image,int w,int h) {
	for(x=0;x<w;x++) for(y=0;y<h;y++) {
		image[x][y] = -image[x][y];
	}
   }

   void use_it(int w,int h) {
	unsigned char **foo = ucialloc(w,h);
	...
	invert(foo,w,h);
	...
   }

This works well in practice. Memory access is faster than integer
multiplication on many machines (even taking into account cache
effects), so that this tends to be faster than index computations as
well. The only disadvantage is that the compiler generally can't
optimize loops in which the indices have been reversed.  Also, you pay
a little bit of storage overhead for the extra vector, but this is
very moderate.

The other solution is to use C++, which lets you define your own array
types.

I have a library of image processing and vision functions in C, based
on the above representation, as well as the beginnings of a library of
C++ functions. I hope that I'll be able to release the C library in a
few weeks (stay tuned).

					Thomas.
Technical Details
Message-ID: <TMB.92Aug7193513@arolla.idiap.ch>
Path: arc1-ca.pugleaf.net!sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
References: <SLEHAR.92Aug7111245@kendall.bu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
From: tmb@arolla.idiap.ch (Thomas M. Breuel)
Newsgroups: sci.image.processing
Subject: Re: image processing loops: a c programming question
Message-ID: <TMB.92Aug7193513@arolla.idiap.ch>
Date: 7 Aug 92 23:35:13 GMT
References: <SLEHAR.92Aug7111245@kendall.bu.edu>
Sender: news@ai.mit.edu
Reply-To: tmb@idiap.ch
Distribution: sci.image.processing
Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
 Perceptive)
Lines: 79
In-reply-to: slehar@kendall.bu.edu's message of 7 Aug 92 15:12:45 GMT