next up previous contents index
Next: A little more complex Up: Getting started Previous: A simple module   Contents   Index


Some optimizations

The main part of the algorithm in fadd1 uses standard MegaWave2 functions to access to the pixels. Since fast pixel access is critical for image processing, we give in this section two alternatives to speed the execution of the module. You may find more tips in the Volume two: ``MegaWave2 System Library'', at this time keep just in mind that this kind of optimization can be adapted to other memory types such signals or movies.

The first alternative is given by the module fadd2. Here is the listing:

/*--------------------------- MegaWave2 module -----------------------------*/
/* mwcommand
 name = {fadd2};
 author = {"Jacques Froment"};
 version = {"1.0"};
 function = {"Adds the pixel's gray-levels of two fimages (for demo #2)"};
 usage = {
   fimage1->A 
      "Input fimage #1", 
   fimage2->B
      "Input fimage #2", 
   result<-C
      "Output image"
};
*/
/*--------------------------------------------------------------------------*/

#include <stdio.h>
#include  "mw.h"

#define _(a,i,j) ((a)->gray[(j)*(a)->ncol+(i)])

void fadd2(A,B,C)

Fimage  A,B,C;

{ int x,y;

  if((A->nrow != B->nrow) || (A->ncol != B->ncol))
    mwerror(FATAL, 1, "The input images have not the same size!\n");

  if ((C = mw_change_fimage(C,A->nrow,A->ncol)) == NULL)
    mwerror(FATAL, 1, "Not enough memory !\n");  

  for (x=0;x<A->ncol;x++) for (y=0;y<A->nrow;y++)
    _(C,x,y) = _(A,x,y) + _(B,x,y);
}
This module does not really optimize the speed of the execution (the saving is small) but it makes the listing more readable for people: once you have defined the C macro _(a,i,j), you can access to the pixel (x, y) on an image I by writing _(I,x,y) only. But be aware that some checking are deconnected in this way: do not exceed the maximum range for (x, y).


The second alternative is given by the module fadd3. This one really improves the speed of the execution. But and unlike the former example, it cannot be used in all algorithms: the pixel cannot be addressed in random order. The image has to be read (or written) in the ``natural'' order that is, from up to down and left to right. Here is the listing:

/*--------------------------- MegaWave2 module -----------------------------*/
/* mwcommand
 name = {fadd3};
 author = {"Jacques Froment"};
 version = {"1.0"};
 function = {"Adds the pixel's gray-levels of two fimages (demo #3)"};
 usage = {
   fimage1->A 
      "Input fimage #1", 
   fimage2->B
      "Input fimage #2", 
   result<-C
      "Output image"
};
*/
/*--------------------------------------------------------------------------*/

#include <stdio.h>
#include  "mw.h"

void fadd3(A,B,C)

Fimage  A,B,C;

{
  register float *ptr1,*ptr2,*ptr3;
  register int i;

  if((A->nrow != B->nrow) || (A->ncol != B->ncol))
    mwerror(FATAL, 1, "The input images have not the same size!\n");

  if ((C = mw_change_fimage(C,A->nrow,A->ncol)) == NULL)
    mwerror(FATAL, 1, "Not enough memory !\n");  

  for (ptr1=A->gray, ptr2=B->gray, ptr3=C->gray, i=0;
       i < A->nrow*A->ncol; ptr1++, ptr2++, ptr3++, i++)
    *ptr3 = *ptr1 + *ptr2;
}


The third alternative is given by the module fadd4. The speed of the execution for this module is not so fast that the one for the previous module fadd3, but this last method can be used in any algorithms, even if the pixels are not scanned in the ``natural'' order. This method consists to allocate, using a MegaWave2 function, a bi-dimensional tab which is actually an one-dimensional tab of pointers, and each pointer points to the beginning of a image's line. Here is the listing:

/*--------------------------- MegaWave2 module -----------------------------*/
/* mwcommand
 name = {fadd4};
 author = {"Jacques Froment"};
 version = {"1.0"};
 function = {"Adds the pixel's gray-levels of two fimages (for demo #4)"};
 usage = {
   fimage1->A 
      "Input fimage #1", 
   fimage2->B
      "Input fimage #2", 
   result<-C
      "Output image"
};
*/
/*--------------------------------------------------------------------------*/

#include <stdio.h>
#include  "mw.h"

void fadd4(A,B,C)

Fimage  A,B,C;

{ int x,y;
  float **TA,**TB,**TC;

  if((A->nrow != B->nrow) || (A->ncol != B->ncol))
    mwerror(FATAL, 1, "The input images have not the same size!\n");

  if ((C = mw_change_fimage(C,A->nrow,A->ncol)) == NULL)
    mwerror(FATAL, 1, "Not enough memory !\n");  

  if ( ((TA = mw_newtab_gray_fimage(A)) == NULL) ||
       ((TB = mw_newtab_gray_fimage(B)) == NULL) ||
       ((TC = mw_newtab_gray_fimage(C)) == NULL) )
    mwerror(FATAL, 1, "Not enough memory !\n");  

  for (x=0;x<A->ncol;x++) for (y=0;y<A->nrow;y++)
    TC[y][x] = TA[y][x] + TB[y][x];

  free(TC);
  free(TB);
  free(TA); 
}


next up previous contents index
Next: A little more complex Up: Getting started Previous: A simple module   Contents   Index
mw 2004-05-05