-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPngDeinterlacer.cs
245 lines (224 loc) · 7.99 KB
/
PngDeinterlacer.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using Hjg.Pngcs;
using System;
using System.Collections.Generic;
using System.Text;
namespace Hjg.Pngcs {
class PngDeinterlacer {
private readonly ImageInfo imi;
private int pass; // 1-7
private int rows, cols, dY, dX, oY, oX, oXsamples, dXsamples; // at current pass
// current row in the virtual subsampled image; this incrementes from 0 to cols/dy 7 times
private int currRowSubimg = -1;
// in the real image, this will cycle from 0 to im.rows in different steps, 7 times
private int currRowReal = -1;
private readonly int packedValsPerPixel;
private readonly int packedMask;
private readonly int packedShift;
private int[][] imageInt; // FULL image -only used for PngReader as temporary storage
private byte[][] imageByte;
internal PngDeinterlacer(ImageInfo iminfo) {
this.imi = iminfo;
pass = 0;
if (imi.Packed) {
packedValsPerPixel = 8 / imi.BitDepth;
packedShift = imi.BitDepth;
if (imi.BitDepth == 1)
packedMask = 0x80;
else if (imi.BitDepth == 2)
packedMask = 0xc0;
else
packedMask = 0xf0;
} else {
packedMask = packedShift = packedValsPerPixel = 1;// dont care
}
setPass(1);
setRow(0);
}
/** this refers to the row currRowSubimg */
internal void setRow(int n) {
currRowSubimg = n;
currRowReal = n * dY + oY;
if (currRowReal < 0 || currRowReal >= imi.Rows)
throw new PngjExceptionInternal("bad row - this should not happen");
}
internal void setPass(int p) {
if (this.pass == p)
return;
pass = p;
switch (pass) {
case 1:
dY = dX = 8;
oX = oY = 0;
break;
case 2:
dY = dX = 8;
oX = 4;
oY = 0;
break;
case 3:
dX = 4;
dY = 8;
oX = 0;
oY = 4;
break;
case 4:
dX = dY = 4;
oX = 2;
oY = 0;
break;
case 5:
dX = 2;
dY = 4;
oX = 0;
oY = 2;
break;
case 6:
dX = dY = 2;
oX = 1;
oY = 0;
break;
case 7:
dX = 1;
dY = 2;
oX = 0;
oY = 1;
break;
default:
throw new PngjExceptionInternal("bad interlace pass" + pass);
}
rows = (imi.Rows - oY) / dY + 1;
if ((rows - 1) * dY + oY >= imi.Rows)
rows--; // can be 0
cols = (imi.Cols - oX) / dX + 1;
if ((cols - 1) * dX + oX >= imi.Cols)
cols--; // can be 0
if (cols == 0)
rows = 0; // really...
dXsamples = dX * imi.Channels;
oXsamples = oX * imi.Channels;
}
// notice that this is a "partial" deinterlace, it will be called several times for the same row!
internal void deinterlaceInt(int[] src, int[] dst, bool readInPackedFormat) {
if (!(imi.Packed && readInPackedFormat))
for (int i = 0, j = oXsamples; i < cols * imi.Channels; i += imi.Channels, j += dXsamples)
for (int k = 0; k < imi.Channels; k++)
dst[j + k] = src[i + k];
else
deinterlaceIntPacked(src, dst);
}
// interlaced+packed = monster; this is very clumsy!
private void deinterlaceIntPacked(int[] src, int[] dst) {
int spos, smod, smask; // source byte position, bits to shift to left (01,2,3,4
int tpos, tmod, p, d;
spos = 0;
smask = packedMask;
smod = -1;
// can this really work?
for (int i = 0, j = oX; i < cols; i++, j += dX) {
spos = i / packedValsPerPixel;
smod += 1;
if (smod >= packedValsPerPixel)
smod = 0;
smask >>= packedShift; // the source mask cycles
if (smod == 0)
smask = packedMask;
tpos = j / packedValsPerPixel;
tmod = j % packedValsPerPixel;
p = src[spos] & smask;
d = tmod - smod;
if (d > 0)
p >>= (d * packedShift);
else if (d < 0)
p <<= ((-d) * packedShift);
dst[tpos] |= p;
}
}
// yes, duplication of code is evil, normally
internal void deinterlaceByte(byte[] src, byte[] dst, bool readInPackedFormat) {
if (!(imi.Packed && readInPackedFormat))
for (int i = 0, j = oXsamples; i < cols * imi.Channels; i += imi.Channels, j += dXsamples)
for (int k = 0; k < imi.Channels; k++)
dst[j + k] = src[i + k];
else
deinterlacePackedByte(src, dst);
}
private void deinterlacePackedByte(byte[] src, byte[] dst) {
int spos, smod, smask; // source byte position, bits to shift to left (01,2,3,4
int tpos, tmod, p, d;
// what the heck are you reading here? I told you would not enjoy this. Try Dostoyevsky or Simone Weil instead
spos = 0;
smask = packedMask;
smod = -1;
// Arrays.fill(dst, 0);
for (int i = 0, j = oX; i < cols; i++, j += dX) {
spos = i / packedValsPerPixel;
smod += 1;
if (smod >= packedValsPerPixel)
smod = 0;
smask >>= packedShift; // the source mask cycles
if (smod == 0)
smask = packedMask;
tpos = j / packedValsPerPixel;
tmod = j % packedValsPerPixel;
p = src[spos] & smask;
d = tmod - smod;
if (d > 0)
p >>= (d * packedShift);
else if (d < 0)
p <<= ((-d) * packedShift);
dst[tpos] |= (byte)p;
}
}
/**
* Is current row the last row for the lass pass??
*/
internal bool isAtLastRow() {
return pass == 7 && currRowSubimg == rows - 1;
}
/**
* current row number inside the "sub image"
*/
internal int getCurrRowSubimg() {
return currRowSubimg;
}
/**
* current row number inside the "real image"
*/
internal int getCurrRowReal() {
return currRowReal;
}
/**
* current pass number (1-7)
*/
internal int getPass() {
return pass;
}
/**
* How many rows has the current pass?
**/
internal int getRows() {
return rows;
}
/**
* How many columns (pixels) are there in the current row
*/
internal int getCols() {
return cols;
}
internal int getPixelsToRead() {
return getCols();
}
internal int[][] getImageInt() {
return imageInt;
}
internal void setImageInt(int[][] imageInt) {
this.imageInt = imageInt;
}
internal byte[][] getImageByte() {
return imageByte;
}
internal void setImageByte(byte[][] imageByte) {
this.imageByte = imageByte;
}
}
}