summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/filters/SpreadFilter.java
blob: 7d14be9ec1ab29fd7ba917ddc427c045291cc4bc (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package imaging.filters;

import java.awt.image.*;
import java.util.Random;

public class SpreadFilter extends SpatialFilter
{
  private int pwidth_ = 5;
  private int pheight_ = 5;

  public SpreadFilter()
    {
    }
  
  public SpreadFilter(int pwidth, int pheight)
    {
      pwidth_ = pwidth;
      pheight_ = pheight;
    }

  public String info ()
  {
    return "Spreads an image (an effect of being under water).";
  }

  public void imageComplete(int status)
  {
    if (status == IMAGEERROR || status == IMAGEABORTED)
      {
	consumer.imageComplete(status);
	System.out.println("Image Error: " + status);
	return;
      }

    Random rand = new Random();
    int[] pixels = new int[columns_];
    int d, dx, dy, x1, y1, xrng, xoff, yrng, yoff;
    int minx, maxx, miny, maxy, rdist, tmp;
    
    for (int y = 0; y < rows_; y++)
      {

	for (int x = 0; x < columns_; x++)
	  {

	    if (pwidth_ < 0)
	      {
		d = (pwidth_ < 0 ? -pwidth_ : pwidth_);

		minx = x - d;
		if (minx < 0)
		  minx = 0;

		maxx = x + d;
		if (maxx >= columns_)
		  maxx = columns_ - 1;

		tmp = rand.nextInt();
		tmp = (tmp < 0 ? -tmp : tmp);
		x1 = minx + tmp % ((maxx - minx) + 1);
		
		miny = y - d;
		if (miny < 0)
		  miny = 0;

		maxy = y + d;
		if (maxy >= rows_)
		  maxy = rows_ - 1;

		rdist = d - (x1 < x ? -(x1 - x) : x1 - x);
		if (y - miny > rdist)
		  miny = (y - rdist);
		if (maxy - y > rdist)
		  maxy = (y + rdist);

		tmp = rand.nextInt();
		tmp = (tmp < 0 ? -tmp : tmp);
		y1 = miny + tmp % ((maxy - miny) + 1);
	      }
	    else
	      {
		minx = x - pwidth_;
		if (minx < 0)
		  minx = 0;

		maxx = x + pwidth_;
		if (maxx >= columns_)
		  maxx = columns_ - 1;

		tmp = rand.nextInt();
		tmp = (tmp < 0 ? -tmp : tmp);
		x1 = minx + tmp % ((maxx - minx) + 1);

		miny = y - pheight_;
		if (miny < 0)
		  miny = 0;

		maxy = y + pheight_;
		if (maxx >= columns_)
		  maxx = columns_ - 1;

		tmp = rand.nextInt();
		tmp = (tmp < 0 ? -tmp : tmp);
		y1 = miny + tmp % ((maxy - miny) + 1);
	      }

	    if (x1 >= 0 && y1 >= 0 && x1 < columns_ && y1 < rows_)
	      {
		int pixel = raster_[y1*columns_ + x1];
		int alpha = (pixel >> 24) & 0xff;
		int red = (pixel >> 16) & 0xff;
		int green = (pixel >> 8) & 0xff;
		int blue = pixel & 0xff;

		pixels[x] = (alpha << 24) | (red << 16) | (green << 8) | blue;
	      }
	  }

	consumer.setPixels(0, y, columns_, 1, defaultRGB_, pixels, 0, columns_);
      }
    
    consumer.imageComplete(status);
  }
}