Source code: com/sixlegs/image/png/Interlacer.java
1 // Copyright (C) 1998, 1999, 2001 Chris Nokleberg
2 // Please see included LICENSE.TXT
3
4 package com.sixlegs.image.png;
5
6 abstract class Interlacer
7 {
8 abstract int numPasses();
9 abstract int getSpacingX(int pass);
10 abstract int getSpacingY(int pass);
11 abstract int getOffsetX(int pass);
12 abstract int getOffsetY(int pass);
13
14 private int maxSpacingX = 0;
15 private int maxSpacingY = 0;
16 private int widestPass = 0;
17
18 protected int w, h;
19
20 Interlacer(int w, int h)
21 {
22 this.w = w;
23 this.h = h;
24 int minSpacingX = w;
25 int n = numPasses();
26 for (int i = 0; i < n; i++) {
27 int sp_x = getSpacingX(i);
28 if (sp_x < minSpacingX) {
29 minSpacingX = sp_x;
30 widestPass = i;
31 }
32 maxSpacingX = Math.max(maxSpacingX, sp_x);
33 maxSpacingY = Math.max(maxSpacingY, getSpacingY(i));
34 }
35 }
36
37 final int getMaxSpacingX()
38 {
39 return maxSpacingX;
40 }
41
42 final int getMaxSpacingY()
43 {
44 return maxSpacingY;
45 }
46
47 final int getMaxPassWidth()
48 {
49 return getPassWidth(widestPass);
50 }
51
52 final int getPassWidth(int pass)
53 {
54 return ((w / maxSpacingX) * countPixelsX(pass, maxSpacingX) +
55 countPixelsX(pass, w % maxSpacingX));
56 }
57
58 final int getPassHeight(int pass)
59 {
60 return ((h / maxSpacingY) * countPixelsY(pass, maxSpacingY) +
61 countPixelsY(pass, h % maxSpacingY));
62 }
63
64 final int getBlockWidth(int pass)
65 {
66 return getSpacingX(pass) - getOffsetX(pass);
67 }
68
69 final int getBlockHeight(int pass)
70 {
71 return getSpacingY(pass) - getOffsetY(pass);
72 }
73
74 final int countPixelsX(int pass, int w)
75 {
76 int cur = 0;
77 int next = getOffsetX(pass);
78 int sp = getSpacingX(pass);
79 for (int x = 0; x < w; x++) {
80 if (x == next) {
81 cur++;
82 next = x + sp;
83 }
84 }
85 return cur;
86 }
87
88 final int countPixelsY(int pass, int h)
89 {
90 int cur = 0;
91 int next = getOffsetY(pass);
92 int sp = getSpacingY(pass);
93 for (int y = 0; y < h; y++) {
94 if (y == next) {
95 cur++;
96 next = y + sp;
97 }
98 }
99 return cur;
100 }
101 }