summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/filters/OilPaintFilter.java
blob: 68bcf79e5c274deb2b590636ef0dd6ea556ae809 (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
package imaging.filters; 

import java.awt.image.*; 

public class OilPaintFilter extends SpatialFilter 
{
  private int degree_ = 3; 

  public OilPaintFilter() 
    {
    }

  public OilPaintFilter(int degree)
    {
      degree_ = degree;
    }

  public String info ()
  {
    return "Converts an image into an oil painting.";
  }

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

    System.gc();
    
    int[] pixels = new int[columns_*rows_];
    int[] nnrect = new int[degree_*degree_];
    int offset = degree_/2, cnt = 0, maxcnt = 0, col = 0, tmp;
    int lcv1 = rows_,
      lcv2 = columns_,
      lcv3 = degree_*degree_;
    
    profile_timer_.start();
    
    for (int z = SpatialFilter.iterations_; z-- > 0;)
      {
	for (int y = 0, index = 0; y < lcv1; y++)
	  {
	    for (int x = 0; x < lcv2; x++, index++)
	      {
		cnt = 0;
		for (int i = y - offset; i < y + offset; i++)
		  {
		    tmp = i*columns_;
		    for (int j = x - offset; j < x + offset; j++, cnt++)
		      {
			if (i >= 0 && i < lcv1 && j >= 0 && j < lcv2)
			  nnrect[cnt] = raster_[tmp + j];
			else
			  nnrect[cnt] = -1;
		      }		    
		  }
		
		maxcnt = 0;
		col = 0;
		cnt = 0;
		for (int i = 0; i < lcv3; i++)
		  {
		    if (nnrect[i] != -1)
		      {
			cnt = 1;
			
			for (int j = i+1; j < lcv3; j++)
			  {
			    if (nnrect[i] == nnrect[j])
			      cnt++;
			  } 
			
			if (cnt > maxcnt)
			  {
			    col = nnrect[i];
			    maxcnt = cnt;
			  }
		      }
		  }
		
		pixels[index] = col;
	      }
	  }
      }

    System.err.println("Oil Paint should be finished.");
    
    profile_timer_.stop();
    
    consumer.setPixels(0, 0, columns_, rows_, defaultRGB_, pixels, 0, columns_);
    consumer.imageComplete(status);
  }
}