1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.png.scanlinefilters;
18
19 import java.io.IOException;
20
21 import org.apache.commons.imaging.ImagingException;
22
23 public class ScanlineFilterAverage implements ScanlineFilter {
24 private final int bytesPerPixel;
25
26 public ScanlineFilterAverage(final int bytesPerPixel) {
27 this.bytesPerPixel = bytesPerPixel;
28 }
29
30 @Override
31 public void unfilter(final byte[] src, final byte[] dst, final byte[] up) throws ImagingException, IOException {
32 for (int i = 0; i < src.length; i++) {
33 int raw = 0;
34 final int prevIndex = i - bytesPerPixel;
35 if (prevIndex >= 0) {
36 raw = dst[prevIndex];
37 }
38
39 int prior = 0;
40 if (up != null) {
41 prior = up[i];
42 }
43
44 final int average = ((0xff & raw) + (0xff & prior)) / 2;
45
46 dst[i] = (byte) ((src[i] + average) % 256);
47
48
49 }
50 }
51 }