home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / smooth.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  2KB  |  99 lines

  1. /* smooth.c:
  2.  *
  3.  * this performs a smoothing convolution using a 3x3 area.
  4.  *
  5.  * jim frost 09.20.90
  6.  *
  7.  * Copyright 1990, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. static Image *doSmooth(image)
  15.      Image *image;
  16. { Image *old, *new;
  17.   int    x, y, x1, y1, linelen;
  18.   int    xindex[3];
  19.   byte  *yindex[3];
  20.   byte  *destptr;
  21.   Pixel  pixval;
  22.   unsigned long avgred, avggreen, avgblue;
  23.  
  24.   /* build true color image from old image and allocate new image
  25.    */
  26.  
  27.   old= expand(image);
  28.   new= newTrueImage(image->width, image->height);
  29.   if (image->title) {
  30.       new->title= (char *)lmalloc(strlen(image->title) + 12);
  31.       sprintf(new->title, "%s (smoothed)", image->title);
  32.   }
  33.  
  34.   /* run through image and take a guess as to what the color should
  35.    * actually be.
  36.    */
  37.  
  38.   destptr= new->data;
  39.   linelen= old->pixlen * old->width;
  40.   for (y= 0; y < old->height; y++) {
  41.     yindex[1]= old->data + (y * linelen);
  42.     yindex[0]= yindex[1] - (y > 0 ? linelen : 0);
  43.     yindex[2]= yindex[1] + (y < old->height - 1 ? linelen : 0);
  44.     for (x= 0; x < old->width; x++) {
  45.       avgred= avggreen= avgblue= 0;
  46.       xindex[1]= x * old->pixlen;
  47.       xindex[0]= xindex[1] - (x > 0 ? old->pixlen : 0);
  48.       xindex[2]= xindex[1] + (x < old->width - 1 ? old->pixlen : 0);
  49.       for (y1= 0; y1 < 3; y1++) {
  50.     for (x1= 0; x1 < 3; x1++) {
  51.       pixval= memToVal(yindex[y1] + xindex[x1], old->pixlen);
  52.       avgred += TRUE_RED(pixval);
  53.       avggreen += TRUE_GREEN(pixval);
  54.       avgblue += TRUE_BLUE(pixval);
  55.     }
  56.       }
  57.  
  58.       /* average the pixel values
  59.        */
  60.  
  61.       avgred= ((avgred + 8) / 9);
  62.       avggreen= ((avggreen + 8) / 9);
  63.       avgblue= ((avgblue + 8) / 9);
  64.       pixval= (avgred << 16) | (avggreen << 8) | avgblue;
  65.       valToMem(pixval, destptr, new->pixlen);
  66.       destptr += new->pixlen;
  67.     }
  68.   }
  69.  
  70.   if (old != image)
  71.     freeImage(old);
  72.   return(new);
  73. }
  74.  
  75. Image *smooth(image, iterations, verbose)
  76.      Image *image;
  77.      unsigned int verbose;
  78. { int a;
  79.   Image *old, *new;
  80.  
  81.   if (verbose) {
  82.     printf("  Smoothing...");
  83.     fflush(stdout);
  84.   }
  85.  
  86.   old= image;
  87.   for (a= 0; a < iterations; a++) {
  88.     new= doSmooth(old);
  89.     if (image != old)
  90.       freeImage(old);
  91.     old= new;
  92.   }
  93.  
  94.   if (verbose)
  95.     printf("done\n");
  96.  
  97.   return(old);
  98. }
  99.