-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTechSmithCodecCore.java
More file actions
1535 lines (1427 loc) · 58.7 KB
/
TechSmithCodecCore.java
File metadata and controls
1535 lines (1427 loc) · 58.7 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @(#)TechSmithCodecCore.java
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.util.zip.InflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteOrder;
import static java.lang.Math.*;
/**
* {@code TechSmithCodec} (tscc) encodes a BufferedImage as a byte[] array.
* <p>
* This codec does not encode the color palette of an image. This must be done
* separately.
* <p>
* Supported input formats:
* <ul>
* {@code Format} with {@code BufferedImage.class}, any width, any height,
* depth=8,16 or 24.
* </ul>
* Supported output formats:
* <ul>
* {@code Format} with {@code byte[].class}, same width and height as input
* format, depth=8,16 or 24.
* </ul>
* The codec supports lossless delta- and key-frame encoding of images with 8, 16 or
* 24 bits per pixel.
* <p>
* Compression of a frame is performed in two steps: In the first, step
* a frame is compressed line by line from bottom to top. In the second step
* the resulting data is compressed again using zlib compression.
* <p>
* Apart from the second compression step and the support for 16- and 24-bit
* data, this encoder is identical to the {@link RunLengthCodec}.
* <p>
* Each line of a frame is compressed individually. A line consists of two-byte
* op-codes optionally followed by data. The end of the line is marked with
* the EOL op-code.
* <p>
* The following op-codes are supported:
* <ul>
* <li>{@code 0x00 0x00}
* <br>Marks the end of a line.</li>
*
* <li>{@code 0x00 0x01}
* <br>Marks the end of the bitmap.</li>
*
* <li>{@code 0x00 0x02 dx dy}
* <br> Marks a delta (skip). {@code dx} and {@code dy}
* indicate the horizontal and vertical offset from the current position.
* {@code dx} and {@code dy} are unsigned 8-bit values.</li>
*
* <li>{@code 0x00 n pixel{n} 0x00?}
* <br> Marks a literal run. {@code n}
* gives the number of 8-, 16- or 24-bit pixels that follow.
* {@code n} must be between 3 and 255.
* If n is odd and 8-bit pixels are used, a pad byte with the value 0x00 must be
* added.
* </li>
* <li>{@code n pixel}
* <br> Marks a repetition. {@code n}
* gives the number of times the given pixel is repeated. {@code n} must be
* between 1 and 255.
* </li>
* </ul>
* Example:
* <pre>
* Compressed data Expanded data
*
* 03 04 04 04 04
* 05 06 06 06 06 06 06
* 00 03 45 56 67 00 45 56 67
* 02 78 78 78
* 00 02 05 01 Move 5 right and 1 down
* 02 78 78 78
* 00 00 End of line
* 09 1E 1E 1E 1E 1E 1E 1E 1E 1E 1E
* 00 01 End of RLE bitmap
* </pre>
*
* References:<br/>
* <a href="http://wiki.multimedia.cx/index.php?title=TechSmith_Screen_Capture_Codec"
* >http://wiki.multimedia.cx/index.php?title=TechSmith_Screen_Capture_Codec</a><br>
*
* <p><b>Palette colors</b></p>
* <p>In an AVI file, palette changes are stored in chunks with id's with the
* suffix "pc". "pc" chunks contain an AVIPALCHANGE struct as shown below.
* </p>
* <pre>
* /* ------------------
* * AVI Palette Change
* * ------------------
* * /
*
* // Values for this enum have been taken from:
* // http://biodi.sdsc.edu/Doc/GARP/garp-1.1/define.h
* enum {
* PC_EXPLICIT = 0x02,
* // Specifies that the low-order word of the logical palette entry
* // designates a hardware palette index. This flag allows the application to
* // show the contents of the display device palette.
* PC_NOCOLLAPSE = 0x04,
* // Specifies that the color be placed in an unused entry in the system
* // palette instead of being matched to an existing color in the system
* // palette. If there are no unused entries in the system palette, the color
* // is matched normally. Once this color is in the system palette, colors in
* // other logical palettes can be matched to this color.
* PC_RESERVED = 0x01
* // Specifies that the logical palette entry be used for palette animation.
* // This flag prevents other windows from matching colors to the palette
* // entry since the color frequently changes. If an unused system-palette
* // entry is available, the color is placed in that entry. Otherwise, the
* // color is not available for animation.
* } peFlagsEnum;
* /*
* * The PALETTEENTRY structure specifies the color and usage of an entry in a
* * logical palette. A logical palette is defined by a LOGPALETTE structure.
* * /
* typedef struct {
* BYTE peRed; // Specifies a red intensity value for the palette entry.
* BYTE peGreen; // Specifies a green intensity value for the palette entry.
* BYTE peBlue; // Specifies a blue intensity value for the palette entry.
* BYTE enum peFlagsEnum peFlags; // Specifies how the palette entry is to be used.
* } PALETTEENTRY;
*
* typedef struct {
* AVIPALCHANGE avipalchange;
* } AVIPALCHANGE0;
*
* typedef struct {
* PALETTEENTRY p[256];
* } PALETTEENTRY_ALLENTRIES;
*
* typedef struct {
* BYTE firstEntry;
* // Specifies the index of the first palette entry to change.
* BYTE numEntries;
* // Specifies the number of palette entries to change, or zero to change
* // all 256 palette entries.
* WORD flags;
* // Reserved.
* PALETTEENTRY peNew[numEntries];
* // Specifies an array of PALETTEENTRY structures, of size "numEntries".
* PALETTEENTRY_ALLENTRIES all[numEntries==0];
* } AVIPALCHANGE;
* </pre>
*
*
* @author Werner Randelshofer
* @version $Id: TechSmithCodecCore.java 136 2011-12-26 10:10:26Z werner $
*/
public class TechSmithCodecCore extends AbstractVideoCodecCore {
private ByteArrayImageOutputStream temp = new ByteArrayImageOutputStream(ByteOrder.LITTLE_ENDIAN);
private byte[] temp2;
private int[] palette;
public TechSmithCodecCore() {
reset();
}
public void reset() {
palette = null;
}
public int[] getPalette() {
if (palette == null) {
palette = new int[256];
// initalize palette with grayscale colors
for (int i = 0; i < palette.length; i++) {
palette[i] = (i) | (i << 8) | (i << 16);
}
}
return palette;
}
/** Decodes an AVI palette change chunk.
* FIXME - This could be moved out into a separate class.
*/
public void decodePalette(byte[] inDat, int off, int len) throws IOException {
getPalette();
ByteArrayImageInputStream in = new ByteArrayImageInputStream(inDat, off, len, ByteOrder.LITTLE_ENDIAN);
int firstEntry = in.readUnsignedByte();
int numEntries = in.readUnsignedByte();
if (numEntries == 0) {
numEntries = 256;
}
int flags = in.readUnsignedShort();
if (firstEntry + numEntries > 256) {
throw new IOException("Illegal headers in pc chunk. firstEntry=" + firstEntry + ", numEntries=" + numEntries);
}
in.setByteOrder(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < numEntries; i++) {
int rgbf = in.readInt();
palette[i + firstEntry] = rgbf >> 8;
}
}
/** Decodes to 8-bit palettised.
* Returns true if a key-frame was decoded.
*
*
* @param inDat
* @param off
* @param length
* @param outDat
* @param prevDat The pixels decoded in the previous frame. Since no double
* buffering is used, this can be the same array than
* {@code outDat}.
* @param width
* @param height
* @param onlyDecodeIfKeyframe
* @return True if a key-frame was decoded.
* @throws IOException
*/
public boolean decode8(byte[] inDat, int off, int length, byte[] outDat, byte[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) throws IOException {
// Handle delta frame with all identical pixels
if (length <= 2) {
return false;
}
UncachedImageInputStream in = new UncachedImageInputStream(
new InflaterInputStream(new ByteArrayInputStream(inDat, off, length)));
int offset = 0;
int scanlineStride = width;
int upsideDown = (height - 1) * scanlineStride + offset;
// Decode each scanline
int verticalOffset = 0;
boolean isKeyFrame = true;
try {
int y = 0;
int xy = upsideDown;
loop:
while (true) {
int opcode = in.readUnsignedByte();
if (opcode == 0) {
opcode = in.readUnsignedByte();
switch (opcode) {
case 0x0000: // end of line
y++;
xy = (height - 1 - y) * scanlineStride + offset;
break;
case 0x0001: // end of bitmap
break loop;
case 0x0002: // delta skip
isKeyFrame = false;
int dx = in.readUnsignedByte();
int dy = in.readUnsignedByte();
y += dy;
int end = xy + dx - dy * scanlineStride;
if (prevDat != outDat) {
System.arraycopy(prevDat, xy, outDat, xy, end - xy);
}
xy = end;
break;
default: // literal run
in.readFully(outDat, xy, opcode);
xy += opcode;
if ((opcode & 1) == 1) {
int pad = in.readByte() & 0xff;
if (pad != 0) {
throw new IOException("Illegal pad byte, pad=0x" + Integer.toHexString(pad));
}
}
break;
}
} else {
// repetition
byte v = in.readByte();
for (int end = xy + opcode; xy < end; xy++) {
outDat[xy] = v;
}
}
}
} catch (ArrayIndexOutOfBoundsException t) {
t.printStackTrace();
}
in.close();
return isKeyFrame;
}
/** Decodes to 24-bit direct color.
* Returns true if a key-frame was decoded.
*
*
* @param inDat
* @param off
* @param length
* @param outDat
* @param prevDat The pixels decoded in the previous frame. Since no double
* buffering is used, this can be the same array than
* {@code outDat}.
* @param width
* @param height
* @param onlyDecodeIfKeyframe
* @return True if a key-frame was decoded.
* @throws IOException
*/
public boolean decode8(byte[] inDat, int off, int length, int[] outDat, int[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) throws IOException {
// Handle delta frame with all identical pixels
if (length <= 2) {
return false;
}
if (temp2 == null || temp2.length < 255) {
temp2 = new byte[255];
}
getPalette();
UncachedImageInputStream in = new UncachedImageInputStream(
new InflaterInputStream(new ByteArrayInputStream(inDat, off, length)));
int offset = 0;
int scanlineStride = width;
int upsideDown = (height - 1) * scanlineStride + offset;
// Decode each scanline
int verticalOffset = 0;
boolean isKeyFrame = true;
try {
int y = 0;
int xy = upsideDown;
loop:
while (true) {
int opcode = in.readUnsignedByte();
if (opcode == 0) {
opcode = in.readUnsignedByte();
switch (opcode) {
case 0x0000: // end of line
y++;
xy = (height - 1 - y) * scanlineStride + offset;
break;
case 0x0001: // end of bitmap
break loop;
case 0x0002: { // delta skip
isKeyFrame = false;
int dx = in.readUnsignedByte();
int dy = in.readUnsignedByte();
y += dy;
int end = xy + dx - dy * scanlineStride;
if (prevDat != outDat) {
System.arraycopy(prevDat, xy, outDat, xy, end - xy);
}
xy = end;
break;
}
default: { // literal run
in.readFully(temp2, 0, opcode);
for (int i = 0; i < opcode; i++) {
outDat[xy + i] = palette[temp2[i] & 0xff];
}
xy += opcode;
if ((opcode & 1) == 1) {
int pad = in.readByte() & 0xff;
if (pad != 0) {
throw new IOException("Illegal pad byte, pad=0x" + Integer.toHexString(pad));
}
}
break;
}
}
} else {
// repetition
int v = palette[in.readUnsignedByte()];
for (int end = xy + opcode; xy < end; xy++) {
outDat[xy] = v;
}
}
}
} catch (ArrayIndexOutOfBoundsException t) {
t.printStackTrace();
}
in.close();
return isKeyFrame;
}
/** Decodes to 24-bit RGB.
* Returns true if a key-frame was decoded.
*/
public boolean decode24(byte[] inDat, int off, int length, int[] outDat, int[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) throws IOException {
// Handle delta frame with all identical pixels
if (length <= 2) {
return false;
}
UncachedImageInputStream in = new UncachedImageInputStream(
new InflaterInputStream(new ByteArrayInputStream(inDat, off, length)));
int offset = 0;
int scanlineStride = width;
int upsideDown = (height - 1) * scanlineStride + offset;
// Decode each scanline
int verticalOffset = 0;
boolean isKeyFrame = true;
try {
int y = 0;
int xy = upsideDown;
loop:
while (true) {
int opcode = in.readUnsignedByte();
if (opcode == 0) {
opcode = in.readUnsignedByte();
switch (opcode) {
case 0x0000: // end of line
y++;
xy = (height - 1 - y) * scanlineStride + offset;
break;
case 0x0001: // end of bitmap
break loop;
case 0x0002: {// delta skip
isKeyFrame = false;
int dx = in.readUnsignedByte();
int dy = in.readUnsignedByte();
y += dy;
int end = xy + dx - dy * scanlineStride;
if (prevDat != outDat) {
System.arraycopy(prevDat, xy, outDat, xy, end - xy);
}
xy = end;
break;
}
default: {// literal run
readInts24LE(in, outDat, xy, opcode);
xy += opcode;
break;
}
}
} else {
// repetition
int v = readInt24LE(in);
for (int end = xy + opcode; xy < end; xy++) {
outDat[xy] = v;
}
}
}
} catch (ArrayIndexOutOfBoundsException t) {
t.printStackTrace();
}
in.close();
return isKeyFrame;
}
/** Decodes from 16-bit to 24-bit RGB.
* Returns true if a key-frame was decoded.
*/
public boolean decode16(byte[] inDat, int off, int length, int[] outDat, int[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) throws IOException {
// Handle delta frame with all identical pixels
if (length <= 2) {
if (outDat!=prevDat) {
System.arraycopy(prevDat,0,outDat,0,width*height);
}
return false;
}
UncachedImageInputStream in = new UncachedImageInputStream(
new InflaterInputStream(new ByteArrayInputStream(inDat, off, length)), ByteOrder.LITTLE_ENDIAN);
int offset = 0;
int scanlineStride = width;
int upsideDown = (height - 1) * scanlineStride + offset;
// Decode each scanline
int verticalOffset = 0;
boolean isKeyFrame = true;
try {
int y = 0;
int xy = upsideDown;
loop:
while (true) {
int opcode = in.readUnsignedByte();
if (opcode == 0) {
opcode = in.readUnsignedByte();
switch (opcode) {
case 0x0000: // end of line
y++;
xy = (height - 1 - y) * scanlineStride + offset;
break;
case 0x0001: // end of bitmap
break loop;
case 0x0002: {// delta skip
isKeyFrame = false;
int dx = in.readUnsignedByte();
int dy = in.readUnsignedByte();
y += dy;
int end = xy + dx - dy * scanlineStride;
if (prevDat != outDat) {
System.arraycopy(prevDat, xy, outDat, xy, end - xy);
}
xy = end;
break;
}
default: {// literal run
readRGBs555to24(in, outDat, xy, opcode);
xy += opcode;
break;
}
}
} else {
// repetition
int v = readRGB555to24(in);
for (int end = xy + opcode; xy < end; xy++) {
outDat[xy] = v;
}
}
}
} catch (ArrayIndexOutOfBoundsException t) {
t.printStackTrace();
}
in.close();
return isKeyFrame;
}
/** Encodes an 8-bit delta frame with indexed colors.
*
* @param out The output stream.
* @param data The image data.
* @param prev The image data of the previous frame.
* @param offset The offset to the first pixel in the data array.
* @param width The width of the image in data elements.
* @param scanlineStride The number to add to offset to get to the next scanline.
*/
public void encodeDelta8(OutputStream out, byte[] data, byte[] prev, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
int ymax = offset + height * scanlineStride;
int upsideDown = ymax - scanlineStride + offset;
// Encode each scanline
int verticalOffset = 0;
for (int y = offset; y < ymax; y += scanlineStride) {
int xy = upsideDown - y;
int xymax = xy + width;
// determine skip count
int skipCount = 0;
for (; xy < xymax; ++xy, ++skipCount) {
if (data[xy] != prev[xy]) {
break;
}
}
if (skipCount == width) {
// => the entire line can be skipped
++verticalOffset;
continue;
}
while (verticalOffset > 0 || skipCount > 0) {
temp.write(0x00); // Escape code
temp.write(0x02); // Skip OP-code
temp.write(min(255, skipCount)); // horizontal offset
temp.write(min(255, verticalOffset)); // vertical offset
skipCount -= min(255, skipCount);
verticalOffset -= min(255, verticalOffset);
}
int literalCount = 0;
int repeatCount = 0;
for (; xy < xymax; ++xy) {
// determine skip count
for (skipCount = 0; xy < xymax; ++xy, ++skipCount) {
if (data[xy] != prev[xy]) {
break;
}
}
xy -= skipCount;
// determine repeat count
byte v = data[xy];
for (repeatCount = 0; xy < xymax && repeatCount < 255; ++xy, ++repeatCount) {
if (data[xy] != v) {
break;
}
}
xy -= repeatCount;
if (skipCount < 4 && xy + skipCount < xymax && repeatCount < 3) {
literalCount++;
} else {
while (literalCount > 0) {
if (literalCount < 3) {
temp.write(1); // Repeat OP-code
temp.write(data[xy - literalCount]);
literalCount--;
} else {
int literalRun = min(254, literalCount);
temp.write(0); // Escape code
temp.write(literalRun); // Literal OP-code
temp.write(data, xy - literalCount, literalRun);
if ((literalRun & 1) == 1) {
temp.write(0); // pad byte
}
literalCount -= literalRun;
}
}
if (xy + skipCount == xymax) {
// => we can skip until the end of the line without
// having to write an op-code
xy += skipCount - 1;
} else if (skipCount >= repeatCount) {
while (skipCount > 0) {
temp.write(0); // Escape code
temp.write(0x0002); // Skip OP-code
temp.write(min(255, skipCount));
temp.write(0);
xy += min(255, skipCount);
skipCount -= min(255, skipCount);
}
xy -= 1;
} else {
temp.write(repeatCount); // Repeat OP-code
temp.write(v);
xy += repeatCount - 1;
}
}
}
// flush literal run
while (literalCount > 0) {
if (literalCount < 3) {
temp.write(1); // Repeat OP-code
temp.write(data[xy - literalCount]);
literalCount--;
} else {
int literalRun = min(254, literalCount);
temp.write(0);
temp.write(literalRun); // Literal OP-code
temp.write(data, xy - literalCount, literalRun);
if ((literalRun & 1) == 1) {
temp.write(0); // pad byte
}
literalCount -= literalRun;
}
}
temp.write(0); // Escape code
temp.write(0x00); // End of line OP-code
}
temp.write(0); // Escape code
temp.write(0x01);// End of bitmap
if (temp.length() == 2) {
temp.toOutputStream(out);
} else {
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
}
/** Encodes an 8-bit delta frame with indexed colors to 24-bit.
*
* @param out The output stream.
* @param data The image data.
* @param prev The image data of the previous frame.
* @param offset The offset to the first pixel in the data array.
* @param width The width of the image in data elements.
* @param scanlineStride The number to add to offset to get to the next scanline.
*/
public void encodeDelta8to24(OutputStream out, byte[] data, byte[] prev, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
int ymax = offset + height * scanlineStride;
int upsideDown = ymax - scanlineStride + offset;
// Encode each scanline
int verticalOffset = 0;
for (int y = offset; y < ymax; y += scanlineStride) {
int xy = upsideDown - y;
int xymax = xy + width;
// determine skip count
int skipCount = 0;
for (; xy < xymax; ++xy, ++skipCount) {
if (data[xy] != prev[xy]) {
break;
}
}
if (skipCount == width) {
// => the entire line can be skipped
++verticalOffset;
continue;
}
while (verticalOffset > 0 || skipCount > 0) {
temp.write(0x00); // Escape code
temp.write(0x02); // Skip OP-code
temp.write(min(255, skipCount)); // horizontal offset
temp.write(min(255, verticalOffset)); // vertical offset
skipCount -= min(255, skipCount);
verticalOffset -= min(255, verticalOffset);
}
int literalCount = 0;
int repeatCount = 0;
for (; xy < xymax; ++xy) {
// determine skip count
for (skipCount = 0; xy < xymax; ++xy, ++skipCount) {
if (data[xy] != prev[xy]) {
break;
}
}
xy -= skipCount;
// determine repeat count
byte v = data[xy];
for (repeatCount = 0; xy < xymax && repeatCount < 255; ++xy, ++repeatCount) {
if (data[xy] != v) {
break;
}
}
xy -= repeatCount;
if (skipCount < 4 && xy + skipCount < xymax && repeatCount < 3) {
literalCount++;
} else {
while (literalCount > 0) {
if (literalCount < 3) {
temp.write(1); // Repeat OP-code
writeInt24LE(temp, palette[data[xy - literalCount]&0xff]);
literalCount--;
} else {
int literalRun = min(254, literalCount);
temp.write(0); // Escape code
temp.write(literalRun); // Literal OP-code
for (int i = xy - literalCount, end = xy - literalCount + literalRun; i < end; i++) {
writeInt24LE(temp, palette[data[i]&0xff]);
}
literalCount -= literalRun;
}
}
if (xy + skipCount == xymax) {
// => we can skip until the end of the line without
// having to write an op-code
xy += skipCount - 1;
} else if (skipCount >= repeatCount) {
while (skipCount > 0) {
temp.write(0); // Escape code
temp.write(0x0002); // Skip OP-code
temp.write(min(255, skipCount));
temp.write(0);
xy += min(255, skipCount);
skipCount -= min(255, skipCount);
}
xy -= 1;
} else {
temp.write(repeatCount); // Repeat OP-code
writeInt24LE(temp, palette[v&0xff]);
xy += repeatCount - 1;
}
}
}
// flush literal run
while (literalCount > 0) {
if (literalCount < 3) {
temp.write(1); // Repeat OP-code
writeInt24LE(temp, palette[data[xy - literalCount]]);
literalCount--;
} else {
int literalRun = min(254, literalCount);
temp.write(0);
temp.write(literalRun); // Literal OP-code
for (int i = xy - literalCount, end = xy - literalCount + literalRun; i < end; i++) {
writeInt24LE(temp, palette[data[i]&0xff]);
}
/*
temp.write(data, xy - literalCount, literalRun);
if (literalRun & 1 == 1) {
temp.write(0); // pad byte
}*/
literalCount -= literalRun;
}
}
temp.write(0); // Escape code
temp.write(0x00); // End of line OP-code
}
temp.write(0); // Escape code
temp.write(0x01);// End of bitmap
if (temp.length() == 2) {
temp.toOutputStream(out);
} else {
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
}
/** Encodes a delta frame which is known to have the same content than
* the previous frame.
*
* @param out
* @param data
* @param prev
* @param width
* @param height
* @param offset
* @param scanlineStride
* @throws IOException
*/
public void encodeSameDelta8(OutputStream out, byte[] data, byte[] prev, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
temp.write(0); // Escape code
temp.write(0x01);// End of bitmap
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
/** Encodes a delta frame which is known to have the same content than
* the previous frame.
*
* @param out
* @param data
* @param prev
* @param width
* @param height
* @param offset
* @param scanlineStride
* @throws IOException
*/
public void encodeSameDelta24(OutputStream out, int[] data, int[] prev, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
temp.write(0); // Escape code
temp.write(0x01);// End of bitmap
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
/** Encodes a delta frame which is known to have the same content than
* the previous frame.
*
* @param out
* @param data
* @param prev
* @param width
* @param height
* @param offset
* @param scanlineStride
* @throws IOException
*/
public void encodeSameDelta16(OutputStream out, short[] data, short[] prev, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
temp.write(0); // Escape code
temp.write(0x01);// End of bitmap
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
/** Encodes a 8-bit key frame with indexed colors.
*
* @param out The output stream.
* @param data The image data.
* @param offset The offset to the first pixel in the data array.
* @param width The width of the image in data elements.
* @param scanlineStride The number to add to offset to get to the next scanline.
*/
public void encodeKey8(OutputStream out, byte[] data, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
int ymax = offset + height * scanlineStride;
int upsideDown = ymax - scanlineStride + offset;
// Encode each scanline separately
for (int y = offset; y < ymax; y += scanlineStride) {
int xy = upsideDown - y;
int xymax = xy + width;
int literalCount = 0;
int repeatCount = 0;
for (; xy < xymax; ++xy) {
// determine repeat count
byte v = data[xy];
for (repeatCount = 0; xy < xymax && repeatCount < 255; ++xy, ++repeatCount) {
if (data[xy] != v) {
break;
}
}
xy -= repeatCount;
if (repeatCount < 3) {
literalCount++;
if (literalCount == 254) {
temp.write(0);
temp.write(literalCount); // Literal OP-code
temp.write(data, xy - literalCount + 1, literalCount);
literalCount = 0;
}
} else {
if (literalCount > 0) {
if (literalCount < 3) {
for (; literalCount > 0; --literalCount) {
temp.write(1); // Repeat OP-code
temp.write(data[xy - literalCount]);
}
} else {
temp.write(0);
temp.write(literalCount); // Literal OP-code
temp.write(data, xy - literalCount, literalCount);
if ((literalCount & 1) == 1) {
temp.write(0); // pad byte
}
literalCount = 0;
}
}
temp.write(repeatCount); // Repeat OP-code
temp.write(v);
xy += repeatCount - 1;
}
}
// flush literal run
if (literalCount > 0) {
if (literalCount < 3) {
for (; literalCount > 0; --literalCount) {
temp.write(1); // Repeat OP-code
temp.write(data[xy - literalCount]);
}
} else {
temp.write(0);
temp.write(literalCount);
temp.write(data, xy - literalCount, literalCount);
if ((literalCount & 1) == 1) {
temp.write(0); // pad byte
}
}
literalCount = 0;
}
temp.write(0);
temp.write(0x0000);// End of line
}
temp.write(0);
temp.write(0x0001);// End of bitmap
//temp.toOutputStream(out);
DeflaterOutputStream defl = new DeflaterOutputStream(out);
temp.toOutputStream(defl);
defl.finish();
}
/** Encodes a 8-bit key frame with indexed colors to 24-bit.
*
* @param out The output stream.
* @param data The image data.
* @param offset The offset to the first pixel in the data array.
* @param width The width of the image in data elements.
* @param scanlineStride The number to add to offset to get to the next scanline.
*/
public void encodeKey8to24(OutputStream out, byte[] data, int width, int height, int offset, int scanlineStride)
throws IOException {
temp.clear();temp.setByteOrder(ByteOrder.LITTLE_ENDIAN);
int ymax = offset + height * scanlineStride;
int upsideDown = ymax - scanlineStride + offset;
// Encode each scanline separately
for (int y = offset; y < ymax; y += scanlineStride) {
int xy = upsideDown - y;
int xymax = xy + width;
int literalCount = 0;
int repeatCount = 0;
for (; xy < xymax; ++xy) {
// determine repeat count
byte v = data[xy];
for (repeatCount = 0; xy < xymax && repeatCount < 255; ++xy, ++repeatCount) {
if (data[xy] != v) {
break;
}
}
xy -= repeatCount;
if (repeatCount < 3) {
literalCount++;