File: smooth.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (98 lines) | stat: -rw-r--r-- 2,318 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* smooth.c:
 *
 * this performs a smoothing convolution using a 3x3 area.
 *
 * jim frost 09.20.90
 *
 * Copyright 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "image.h"

static Image *doSmooth(image)
     Image *image;
{ Image *old, *new;
  int    x, y, x1, y1, linelen;
  int    xindex[3];
  byte  *yindex[3];
  byte  *destptr;
  Pixel  pixval;
  unsigned long avgred, avggreen, avgblue;

  /* build true color image from old image and allocate new image
   */

  old= expand(image);
  new= newTrueImage(image->width, image->height);
  if (image->title) {
      new->title= (char *)lmalloc(strlen(image->title) + 12);
      sprintf(new->title, "%s (smoothed)", image->title);
  }

  /* run through image and take a guess as to what the color should
   * actually be.
   */

  destptr= new->data;
  linelen= old->pixlen * old->width;
  for (y= 0; y < old->height; y++) {
    yindex[1]= old->data + (y * linelen);
    yindex[0]= yindex[1] - (y > 0 ? linelen : 0);
    yindex[2]= yindex[1] + (y < old->height - 1 ? linelen : 0);
    for (x= 0; x < old->width; x++) {
      avgred= avggreen= avgblue= 0;
      xindex[1]= x * old->pixlen;
      xindex[0]= xindex[1] - (x > 0 ? old->pixlen : 0);
      xindex[2]= xindex[1] + (x < old->width - 1 ? old->pixlen : 0);
      for (y1= 0; y1 < 3; y1++) {
	for (x1= 0; x1 < 3; x1++) {
	  pixval= memToVal(yindex[y1] + xindex[x1], old->pixlen);
	  avgred += TRUE_RED(pixval);
	  avggreen += TRUE_GREEN(pixval);
	  avgblue += TRUE_BLUE(pixval);
	}
      }

      /* average the pixel values
       */

      avgred= ((avgred + 8) / 9);
      avggreen= ((avggreen + 8) / 9);
      avgblue= ((avgblue + 8) / 9);
      pixval= (avgred << 16) | (avggreen << 8) | avgblue;
      valToMem(pixval, destptr, new->pixlen);
      destptr += new->pixlen;
    }
  }

  if (old != image)
    freeImage(old);
  return(new);
}

Image *smooth(image, iterations, verbose)
     Image *image;
     unsigned int verbose;
{ int a;
  Image *old, *new;

  if (verbose) {
    printf("  Smoothing...");
    fflush(stdout);
  }

  old= image;
  for (a= 0; a < iterations; a++) {
    new= doSmooth(old);
    if (image != old)
      freeImage(old);
    old= new;
  }

  if (verbose)
    printf("done\n");

  return(old);
}