From: schultz@halley.est.3m.com (John C. Schultz)
Subject: Bug? and correction in lvmdilate
Organization: 3M Company, 3M Center, Minnesota, USA
Distribution: comp
Date: Fri, 22 Jan 93 16:35:09 GMT

lvmdilate.c (src/vipl/Lib) produces a result which is shifted wrt to
the original image by 1/2 of the kernel dimensions.  This is a bug in
my opinion.

The following diff can be applied with patch to fix lvmdilate.c

vmdilate will now work exactly for odd kernels and shift the image
down and to the right by 1 pixel for even kernels.  Note that this may
not be compatible with vmerode, vmcdilate, ... but the patched version
is correct in IMHO.

e.g. 
     00000                          00000              00000
     00000     111                  01110              00000
     00100   w 111  will generate   01110  rather than 00111
     00000     111                  01110              00111
     00000                          00000              00111

     00000                           00000
     00000     1111                  01111
     00100   w 1111  will generate   01111
     00000     1111                  01111
     00000                           00000




*** /usr/khoros/src/vipl/Lib/lvmdilate.c	Fri Mar 20 17:04:10 1992
--- lvmdilate.c	Fri Jan 22 10:05:34 1993
***************
*** 209,215 ****
      c3 = result;
  
      k = nc * nr;                                /* Normal operation count */
!     for (j=0;j<k;j++) *(c3 +j)=0;
  
  
      for (i=0;i<nr;i++) {
--- 209,215 ----
      c3 = result;
  
      k = nc * nr;                                /* Normal operation count */
!     bzero(c3, k * sizeof(unsigned char));
  
  
      for (i=0;i<nr;i++) {
***************
*** 218,229 ****
  
              if (*(c1+i*nc+j) != 0) {
             
!                 for (ii=0;ii<kernr;ii++) {
  
-                     iii = i+ii+starti;
                      if (iii<0 || iii>=nr) continue;
!                     for (jj=0; jj<kernc; jj++) {
!                         jjj = j+jj+startj;
                          if ( jjj>=0 && jjj<nc ) 
                             if (*(c2+ii*kernc+jj) != 0) 
                                 *(c3+ iii*nc + jjj) = 255;
--- 218,230 ----
  
              if (*(c1+i*nc+j) != 0) {
             
!                 for (ii = 0; ii < kernr; ii++) {
!                     iii = i+ii+starti + ((kernr & 0X01) ? -kernr/2 : -kernc/2 + 1);
  
                      if (iii<0 || iii>=nr) continue;
!                     for (jj = 0; jj < kernc; jj++) {
! 
!                         jjj = j+jj+startj + ((kernc & 0X01) ? -kernc/2 : -kernc/2 + 1);
                          if ( jjj>=0 && jjj<nc ) 
                             if (*(c2+ii*kernc+jj) != 0) 
                                 *(c3+ iii*nc + jjj) = 255;
***************
*** 234,240 ****
          }
      c1 = (unsigned char *) image->imagedata;
      c3 = result;
!     for (i=0;i<k;i++) *c1++ = *c3++;
  
  
      free(result);
--- 235,241 ----
          }
      c1 = (unsigned char *) image->imagedata;
      c3 = result;
!     bcopy(c3, c1, k * sizeof(unsigned char));
  
  
      free(result);*** lvmdilate.c	Fri Jan 22 10:05:34 1993
--- /usr/khoros/src/vipl/Lib/lvmdilate.c	Fri Mar 20 17:04:10 1992
***************
*** 209,215 ****
      c3 = result;
  
      k = nc * nr;                                /* Normal operation count */
!     bzero(c3, k * sizeof(unsigned char));
  
  
      for (i=0;i<nr;i++) {
--- 209,215 ----
      c3 = result;
  
      k = nc * nr;                                /* Normal operation count */
!     for (j=0;j<k;j++) *(c3 +j)=0;
  
  
      for (i=0;i<nr;i++) {
***************
*** 218,230 ****
  
              if (*(c1+i*nc+j) != 0) {
             
!                 for (ii = 0; ii < kernr; ii++) {
!                     iii = i+ii+starti + ((kernr & 0X01) ? -kernr/2 : -kernc/2 + 1);
  
                      if (iii<0 || iii>=nr) continue;
!                     for (jj = 0; jj < kernc; jj++) {
! 
!                         jjj = j+jj+startj + ((kernc & 0X01) ? -kernc/2 : -kernc/2 + 1);
                          if ( jjj>=0 && jjj<nc ) 
                             if (*(c2+ii*kernc+jj) != 0) 
                                 *(c3+ iii*nc + jjj) = 255;
--- 218,229 ----
  
              if (*(c1+i*nc+j) != 0) {
             
!                 for (ii=0;ii<kernr;ii++) {
  
+                     iii = i+ii+starti;
                      if (iii<0 || iii>=nr) continue;
!                     for (jj=0; jj<kernc; jj++) {
!                         jjj = j+jj+startj;
                          if ( jjj>=0 && jjj<nc ) 
                             if (*(c2+ii*kernc+jj) != 0) 
                                 *(c3+ iii*nc + jjj) = 255;
***************
*** 235,241 ****
          }
      c1 = (unsigned char *) image->imagedata;
      c3 = result;
!     bcopy(c3, c1, k * sizeof(unsigned char));
  
  
      free(result);
--- 234,240 ----
          }
      c1 = (unsigned char *) image->imagedata;
      c3 = result;
!     for (i=0;i<k;i++) *c1++ = *c3++;
  
  
      free(result);
--
   John C. Schultz   +1 (612) 733-4047   schultz@halley.serc.3m.com
   How to include the taste of Glendronach in a multi-media system?
Xref: sparky comp.soft-sys.khoros:3282 alt.3d:1763