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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
package jogamp.opengl.util.pngj;
import jogamp.opengl.util.pngj.ImageLineHelper.ImageLineStats;
/**
* Lightweight wrapper for an image scanline, used for read and write.
* <p>
* This object can be (usually it is) reused while iterating over the image
* lines.
* <p>
* See <code>scanline</code> field, to understand the format.
*/
public class ImageLine {
public final ImageInfo imgInfo;
/**
* tracks the current row number (from 0 to rows-1)
*/
private int rown = 0;
/**
* The 'scanline' is an array of integers, corresponds to an image line
* (row).
* <p>
* Except for 'packed' formats (gray/indexed with 1-2-4 bitdepth) each
* <code>int</code> is a "sample" (one for channel), (0-255 or 0-65535) in
* the corresponding PNG sequence: <code>R G B R G B...</code> or
* <code>R G B A R G B A...</tt>
* or <code>g g g ...</code> or <code>i i i</code> (palette index)
* <p>
* For bitdepth=1/2/4 , and if samplesUnpacked=false, each value is a PACKED
* byte!
* <p>
* To convert a indexed line to RGB balues, see
* <code>ImageLineHelper.palIdx2RGB()</code> (you can't do the reverse)
*/
public final int[] scanline;
/**
* Same as {@link #scanline}, but with one byte per sample. Only one of
* scanline and scanlineb is valid - this depends on {@link #sampleType}
*/
public final byte[] scanlineb;
protected FilterType filterUsed; // informational ; only filled by the reader
final int channels; // copied from imgInfo, more handy
final int bitDepth; // copied from imgInfo, more handy
final int elementsPerRow; // = imgInfo.samplePerRowPacked, if packed:imgInfo.samplePerRow elswhere
public enum SampleType {
INT, // 4 bytes per sample
// SHORT, // 2 bytes per sample
BYTE // 1 byte per sample
}
/**
* tells if we are using BYTE or INT to store the samples.
*/
public final SampleType sampleType;
/**
* true: each element of the scanline array represents a sample always, even
* for internally packed PNG formats
*
* false: if the original image was of packed type (bit depth less than 8)
* we keep samples packed in a single array element
*/
public final boolean samplesUnpacked;
/**
* default mode: INT packed
*/
public ImageLine(ImageInfo imgInfo) {
this(imgInfo, SampleType.INT, false);
}
/**
*
* @param imgInfo
* Inmutable ImageInfo, basic parameter of the image we are
* reading or writing
* @param stype
* INT or BYTE : this determines which scanline is the really
* used one
* @param unpackedMode
* If true, we use unpacked format, even for packed original
* images
*
*/
public ImageLine(ImageInfo imgInfo, SampleType stype, boolean unpackedMode) {
this(imgInfo, stype, unpackedMode, null, null);
}
/**
* If a preallocated array is passed, the copy is shallow
*/
ImageLine(ImageInfo imgInfo, SampleType stype, boolean unpackedMode, int[] sci, byte[] scb) {
this.imgInfo = imgInfo;
channels = imgInfo.channels;
bitDepth = imgInfo.bitDepth;
filterUsed = FilterType.FILTER_UNKNOWN;
this.sampleType = stype;
this.samplesUnpacked = unpackedMode || !imgInfo.packed;
elementsPerRow = this.samplesUnpacked ? imgInfo.samplesPerRow : imgInfo.samplesPerRowPacked;
if (stype == SampleType.INT) {
scanline = sci != null ? sci : new int[elementsPerRow];
scanlineb = null;
} else if (stype == SampleType.BYTE) {
scanlineb = scb != null ? scb : new byte[elementsPerRow];
scanline = null;
} else
throw new PngjExceptionInternal("bad ImageLine initialization");
this.rown = -1;
}
/** This row number inside the image (0 is top) */
public int getRown() {
return rown;
}
/** Sets row number (0 : Rows-1) */
public void setRown(int n) {
this.rown = n;
}
/*
* Unpacks scanline (for bitdepth 1-2-4)
*
* Arrays must be prealocated. src : samplesPerRowPacked dst : samplesPerRow
*
* This usually works in place (with src==dst and length=samplesPerRow)!
*
* If not, you should only call this only when necesary (bitdepth <8)
*
* If <code>scale==true<code>, it scales the value (just a bit shift) towards 0-255.
*/
static void unpackInplaceInt(final ImageInfo iminfo, final int[] src, final int[] dst, final boolean scale) {
final int bitDepth = iminfo.bitDepth;
if (bitDepth >= 8)
return; // nothing to do
final int mask0 = ImageLineHelper.getMaskForPackedFormatsLs(bitDepth);
final int scalefactor = 8 - bitDepth;
final int offset0 = 8 * iminfo.samplesPerRowPacked - bitDepth * iminfo.samplesPerRow;
int mask, offset, v;
if (offset0 != 8) {
mask = mask0 << offset0;
offset = offset0; // how many bits to shift the mask to the right to recover mask0
} else {
mask = mask0;
offset = 0;
}
for (int j = iminfo.samplesPerRow - 1, i = iminfo.samplesPerRowPacked - 1; j >= 0; j--) {
v = (src[i] & mask) >> offset;
if (scale)
v <<= scalefactor;
dst[j] = v;
mask <<= bitDepth;
offset += bitDepth;
if (offset == 8) {
mask = mask0;
offset = 0;
i--;
}
}
}
/*
* Unpacks scanline (for bitdepth 1-2-4)
*
* Arrays must be prealocated. src : samplesPerRow dst : samplesPerRowPacked
*
* This usually works in place (with src==dst and length=samplesPerRow)! If not, you should only call this only when
* necesary (bitdepth <8)
*
* The trailing elements are trash
*
*
* If <code>scale==true<code>, it scales the value (just a bit shift) towards 0-255.
*/
static void packInplaceInt(final ImageInfo iminfo, final int[] src, final int[] dst, final boolean scaled) {
final int bitDepth = iminfo.bitDepth;
if (bitDepth >= 8)
return; // nothing to do
final int mask0 = ImageLineHelper.getMaskForPackedFormatsLs(bitDepth);
final int scalefactor = 8 - bitDepth;
final int offset0 = 8 - bitDepth;
int v, v0;
int offset = 8 - bitDepth;
v0 = src[0]; // first value is special for in place
dst[0] = 0;
if (scaled)
v0 >>= scalefactor;
v0 = ((v0 & mask0) << offset);
for (int i = 0, j = 0; j < iminfo.samplesPerRow; j++) {
v = src[j];
if (scaled)
v >>= scalefactor;
dst[i] |= ((v & mask0) << offset);
offset -= bitDepth;
if (offset < 0) {
offset = offset0;
i++;
dst[i] = 0;
}
}
dst[0] |= v0;
}
static void unpackInplaceByte(final ImageInfo iminfo, final byte[] src, final byte[] dst, final boolean scale) {
final int bitDepth = iminfo.bitDepth;
if (bitDepth >= 8)
return; // nothing to do
final int mask0 = ImageLineHelper.getMaskForPackedFormatsLs(bitDepth);
final int scalefactor = 8 - bitDepth;
final int offset0 = 8 * iminfo.samplesPerRowPacked - bitDepth * iminfo.samplesPerRow;
int mask, offset, v;
if (offset0 != 8) {
mask = mask0 << offset0;
offset = offset0; // how many bits to shift the mask to the right to recover mask0
} else {
mask = mask0;
offset = 0;
}
for (int j = iminfo.samplesPerRow - 1, i = iminfo.samplesPerRowPacked - 1; j >= 0; j--) {
v = (src[i] & mask) >> offset;
if (scale)
v <<= scalefactor;
dst[j] = (byte) v;
mask <<= bitDepth;
offset += bitDepth;
if (offset == 8) {
mask = mask0;
offset = 0;
i--;
}
}
}
/**
* size original: samplesPerRow sizeFinal: samplesPerRowPacked (trailing
* elements are trash!)
**/
static void packInplaceByte(final ImageInfo iminfo, final byte[] src, final byte[] dst, final boolean scaled) {
final int bitDepth = iminfo.bitDepth;
if (bitDepth >= 8)
return; // nothing to do
final int mask0 = ImageLineHelper.getMaskForPackedFormatsLs(bitDepth);
final int scalefactor = 8 - bitDepth;
final int offset0 = 8 - bitDepth;
int v, v0;
int offset = 8 - bitDepth;
v0 = src[0]; // first value is special
dst[0] = 0;
if (scaled)
v0 >>= scalefactor;
v0 = ((v0 & mask0) << offset);
for (int i = 0, j = 0; j < iminfo.samplesPerRow; j++) {
v = src[j];
if (scaled)
v >>= scalefactor;
dst[i] |= ((v & mask0) << offset);
offset -= bitDepth;
if (offset < 0) {
offset = offset0;
i++;
dst[i] = 0;
}
}
dst[0] |= v0;
}
/**
* Creates a new ImageLine similar to this, but unpacked
*
* The caller must be sure that the original was really packed
*/
public ImageLine unpackToNewImageLine() {
ImageLine newline = new ImageLine(imgInfo, sampleType, true);
if (sampleType == SampleType.INT)
unpackInplaceInt(imgInfo, scanline, newline.scanline, false);
else
unpackInplaceByte(imgInfo, scanlineb, newline.scanlineb, false);
return newline;
}
/**
* Creates a new ImageLine similar to this, but packed
*
* The caller must be sure that the original was really unpacked
*/
public ImageLine packToNewImageLine() {
ImageLine newline = new ImageLine(imgInfo, sampleType, false);
if (sampleType == SampleType.INT)
packInplaceInt(imgInfo, scanline, newline.scanline, false);
else
packInplaceByte(imgInfo, scanlineb, newline.scanlineb, false);
return newline;
}
public FilterType getFilterUsed() {
return filterUsed;
}
public void setFilterUsed(FilterType ft) {
filterUsed = ft;
}
public int[] getScanlineInt() {
return scanline;
}
public byte[] getScanlineByte() {
return scanlineb;
}
/**
* Basic info
*/
public String toString() {
return "row=" + rown + " cols=" + imgInfo.cols + " bpc=" + imgInfo.bitDepth + " size=" + scanline.length;
}
/**
* Prints some statistics - just for debugging
*/
public static void showLineInfo(ImageLine line) {
System.out.println(line);
ImageLineStats stats = new ImageLineHelper.ImageLineStats(line);
System.out.println(stats);
System.out.println(ImageLineHelper.infoFirstLastPixels(line));
}
}
|