🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: sci.image.processing
Article #503

Re: image processing loops: a c programming question

#503
From: Klamer Schutte
Date: Wed, 19 Aug 1992 07:23
63 lines
3230 bytes
In <ben.714157437@galaxy> ben@ph.tn.tudelft.nl (Ben Verwer) writes:

>The basic question is how to program for images with variable sizes. The 
>solution presented by the author here (malloc a linear array of xsize*ysize) is
>imo the best. Those numerical recipies in C readers who propose the famous 
>array of pointers solution are thinking rather 2d. What if you go to 3d: an 
>image of pointers? Which is of course implemented as an array of pointers to 
>pointers. Good luck!

Yes, an 3d image can be implemented as an 2-d array (not image) of pointers.
And a n-d image as an (n-1)-d array of pointers. No problem! Note that the
overhead involved in space typically is sizeof( any * ), for each row.
And since row sizes typically are >> sizeof(any *), there is no big
problem.

A problem with using a linear array is present when multiplication is
slower as pointer indirection (for instance, on a sparc, or a 68000).
Accessing image pixel (x,y) will involve accessing image[y*xsize+x];
otherwise *(imageptr[y]+x) is used.
Off course you can use a pointer array, and allocate your image memory
continuous -- this will give you best of to worlds! (at the cost of
setting up your pointer array, which typically is of order columnsize,
instead of order columnsize * rowsize for the full operation. So the
overhead is rather small.)

>The loop I most often use is:

>pixel *p = beginofdata;
>int i;  /* make sure this is 32 bits! */

>for (i=xsize*ysize; --i>=0;) *p++ = ...;

>This is the fastest on 68000Us! And also on sparcstations. Try it. (thanx to 
>Gert van Antwerpen)

It is not on sparcstations (and i doubt it on 68k). Faster is:

pixel *p_end = beginofdata+sizeofdata+1;

for(p=beginofdata;p<p_end;) *p++ = ...;
(At least with /bin/cc and Sun C 1.4. gcc 2.2.1 didn't care...)

>Make sure the optimizer is on. Else write register pixel *p and register int i;
Very true! Use highest optimizer option (-O4 on sun compilers, -O2 on gcc).
Check best compiler for best performance! (differences easily can be about
25% !)

>Ben Verwer. Below see a real program (coauthored by Henri Vrooman).

Don't read that program. Above principles are not correct applied!
(i.e. lot of looping with pointer[index] instead of *pointer++ ! )
Also statements which are in the loop can be easily placed outside!


Please correct if i am wrong. This kind of discussions are of help to
everybody!

Klamer

--
Klamer Schutte			Tel: +31-53-892778	Fax: +31-53-340045
Faculty of electrical engineering -- University of Twente, The Netherlands
preferred: klamer@mi.el.utwente.nl     SMTP: klamer@utelmi01.el.utwente.nl
Technical Details
Message-ID: <klamer.714209014@mi.el.utwente.nl>
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!mips!decwrl!deccrl!bloom-beacon!eru.mt.luth.se!lunic!sunic!mcsun!sun4nl!utrcu1!klamer
References: <SLEHAR.92Aug7111245@kendall.bu.edu> <ben.714157437@galaxy>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!mips!decwrl!deccrl!bloom-beacon!eru.mt.luth.se!lunic!sunic!mcsun!sun4nl!utrcu1!klamer
From: klamer@mi.el.utwente.nl (Klamer Schutte)
Newsgroups: sci.image.processing
Subject: Re: image processing loops: a c programming question
Message-ID: <klamer.714209014@mi.el.utwente.nl>
Date: 19 Aug 92 07:23:34 GMT
References: <SLEHAR.92Aug7111245@kendall.bu.edu> <ben.714157437@galaxy>
Sender: news@utwente.nl (USENET News System)
Distribution: sci.image.processing
Organization: University of Twente, BSC-El
Lines: 63