-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLLJTran.java
5752 lines (5380 loc) · 178 KB
/
LLJTran.java
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
/* MediaUtil LLJTran - $RCSfile: LLJTran.java,v $
* Copyright (C) 1999-2005 Dmitriy Rogatkin, Suresh Mahalingam. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* $Id: LLJTran.java,v 1.13 2008/09/01 04:10:12 drogatkin Exp $
*
* Some ideas and algorithms were borrowed from:
* Thomas G. Lane, and James R. Weeks
*
*/
package android.mediautil.image.jpeg;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import android.graphics.Rect;
import android.mediautil.generic.BasicIo;
import android.mediautil.generic.FileFormatException;
import android.mediautil.generic.Log;
import android.mediautil.generic.ProgressCallback;
import android.mediautil.generic.directio.IterativeReader;
import android.mediautil.generic.directio.IterativeWriter;
class IterativeReadVars {
public static final int READING_STAGE = 1;
public static final int READING_DCT_STAGE = 2;
public static final int READING_APPX_STAGE = 3;
public static final int IMAGE_READ_STAGE = 4;
public static final int DONE_STAGE = 5;
// For info
public int minReadRequest;
public int maxReadRequest;
public InputStream is;
public int readUpto;
public int stage;
public int sections;
public boolean keep_appxs, appxsCleared;
public int appxPos, appxLen;
public boolean throwException;
// Vars for readDCT
public double currentProgress, callbackProgress, progressPerMcu;
public int[] last_dc;
public int[][] DCT;
public int next_restart_num;
public int ix, iy;
}
class IterativeWriteVars {
public static final int WRITE_BEGIN = 0;
public static final int WRITE_COMMENTS = 1;
public static final int WRITE_APPXS = 2;
public static final int WRITE_DQT = 3;
public static final int WRITE_DHT = 4;
public static final int WRITE_START = 5;
public static final int WRITE_DCT = 6;
public static final int WRITE_COMPLETE = 7;
// For info
public int minWriteRequest;
public int maxWriteRequest;
public OutputStream os;
public int op;
public int options;
public int restart_interval;
public String comment;
public Class<?> custom_appx;
public int state = WRITE_COMPLETE;
public byte huffTables[];
public int currentAppxPos, currentAppx;
public byte saveAppxs[];
public int svX;
public int svY;
public int svWidthMCU;
public int svHeightMCU;
// initWriteDCT vars
public boolean transformDct;
public int[][][][][] new_dct_coefs;
public double currentProgress, callbackProgress, progressPerMcu;
public int[] last_dc;
public int restarts_to_go;
public int xCropOffsetMCU;
public int yCropOffsetMCU;
public boolean handleXEdge;
public boolean handleYEdge;
public int new_ix, new_iy;
public boolean pullDownMode;
// For unused method writeJpeg
public boolean restoreVars;
// if transformDct true it indicates if full Dct array needs to be allocated
// or if the rows of old dct array can be reused.
public boolean reuseDctRows = true;
public void freeMemory() {
huffTables = null;
new_dct_coefs = null;
last_dc = null;
}
}
/**
* LLJTran is a class for Lossless Jpeg Transformation.
* <p>
*
* With the help of Supporting classes like Exif it can also be used to change
* Image Header Information (Exif header) like Date, Thumbnail, Orientation etc.
* <p>
*
* Following are the key features:
* <p>
*
* <ul>
* <li>Supports lossless rotation, transpose, transverse and crop
* <li>Trimming or relocating Non Transformable edge blocks similiar to <a
* target=_blank href="http://www.ijg.org">jpegtran</a> or processing them like
* regular MCU blocks. Please see the documentation for the
* {@link #OPT_XFORM_TRIM} and {@link #OPT_XFORM_ADJUST_EDGES} for more info on
* this.
* <li>Reading and Modifying Image Header Information (Exif) including Thumbnail
* <li>Built-in transformation of Thumbnail and Orientation marker
* <li>Supports directio Interfaces {@link IterativeReader} and
* {@link IterativeWriter} enabling things like Sharing the jpeg input file with
* say jkd's ImageReader while reading
* <li>Does <b>not</b> Support Multi-Threading for the same Object to be used
* simultaneously by more than one thread. However different threads can have
* their own LLJTran Objects.
* </ul>
*
* @author Dmitriy Rogatkin & Suresh Mahalingam (msuresh@cheerful.com)
*/
public class LLJTran extends BasicJpegIo implements IterativeReader,
IterativeWriter {
/** Name of the JFIF format */
public static final String JFIF = "JFIF";
/** Name of the FPXR format */
public static final String FPXR = "FPXR";
/** Name of the JPEG format */
public static final String JPEG = "JPEG";
private static final String TAG = "LLJTran";
// TODO: move rotation constants to some interface
/** Identifies the identity transformation or no transformation */
public static final int NONE = 0;
/** Identifies the Horizontal Flip transformation */
public static final int FLIP_H = 1;
/** Identifies the Vertical Flip transformation */
public static final int FLIP_V = 2;
/** Identifies the Transpose transformation */
public static final int TRANSPOSE = 3;
/** Identifies the Transverse transformation */
public static final int TRANSVERSE = 4;
/** Identifies the Rotate 90 degrees clockwise transformation */
public static final int ROT_90 = 5;
/** Identifies the Rotate 180 degrees transformation */
public static final int ROT_180 = 6;
/** Identifies the Rotate 270 degrees clockwise transformation */
public static final int ROT_270 = 7;
/** Identifies the crop transformation */
public static final int CROP = 8;
/**
* Identifies the comment transformation. This is not used in LLJTran as of
* now
*/
public static final int COMMENT = 9;
protected static final int DCTSIZE2 = 64;
protected static final int DCTSIZE = 8;
protected static final int BYTE_SIZE = 8;
/**
* Identifies that no part of the Image has been read successfully. Not a
* valid option while reading
*/
public static final int READ_NONE = 0;
/** Identifies the option to read upto the Image Header Info while reading */
public static final int READ_INFO = 1;
/**
* Identifies the option to read upto the Image Header including image
* dimensions, parameters and tables for decoding while reading
*/
public static final int READ_HEADER = 2;
/** Identifies the option to read the entire image while reading */
public static final int READ_ALL = 3;
/**
* Flag to specify that the Image Header information (Exif width, height and
* resolution) should be adjusted when transforming the image
*/
public final static int OPT_XFORM_APPX = 0x1;
/**
* Flag to specify that the Thumbnail in the Image Header Information (Exif)
* should be transformed when transforming the image. OPT_XFORM_APPX must be
* set for this to take effect
*/
public final static int OPT_XFORM_THUMBNAIL = 0x2;
/**
* Flag to specify that Non Transformable edge blocks should be removed when
* transforming the image. Depending on the transformation the Image width,
* height or both may be trimmed to the nearest multiple of 8 or 16 pixels
* before the transform. Please note that this leads to loss of image pixels
* in the partial edge(es). When this flag is set the flag
* OPT_XFORM_ADJUST_EDGES is ignored even if set.
* <p>
*
* More Info: You have partial MCU blocks when the image width is not a
* multiple of getMCUWidth() or image height is not a multiple of
* getMCUHeight(). getMCUWidth() or getMCUHeight() are a multiple of 8,
* usually 8, 16 and sometimes 24.
* <p>
*
* Below lists the effect of Setting this flag on the Lossles
* Transformations:
* <p>
*
* FLIP_H: Image bottom trimmed before transform
* <p>
* FLIP_V: Image right trimmed before transform
* <p>
* TRANSPOSE: No trimming required before transform
* <p>
* TRANSVERSE: Image right and bottom trimmed before transform
* <p>
* ROT_90: Image bottom trimmed before transform
* <p>
* ROT_180: Image right and bottom trimmed before transform
* <p>
* ROT_270: Image right trimmed before transform
* <p>
* CROP: The x and y coordinates of the top-left corner of the crop area is
* adjusted to the nearest MCU boundary.
*/
public final static int OPT_XFORM_TRIM = 0x4;;
/**
* Flag to specifiy that Non Transformable edge blocks should be adjusted
* accordingly when transforming the image. Depending on the transformation
* either the right strip or the bottom strip of the image or both if
* present after the nearest MCU boundary maybe adjusted with a different
* transform.
* <p>
*
* This option is ignored if OPT_XFORM_TRIM is set. If both these flags are
* not set the transformation is applied without accounting for partial MCU
* edges which may result in visual distortion at the edges.
* <p>
*
* Below lists the effect of Setting this flag on the Lossles
* Transformations:
* <p>
*
* FLIP_H: Right partial MCU strip is left unchanged
* <p>
* FLIP_V: Bottom partial MCU strip is left unchanged
* <p>
* TRANSPOSE: No adjustment required
* <p>
* TRANSVERSE: Result is same as ROT_90 followed by FLIP_V using LLJTran. So
* the right partial MCU strip is rotated 90, bottom rotated 270 and
* bottom-right corner MCU block transposed.
* <p>
* ROT_90: Bottom partial MCU strip is transposed and comes in the right
* edge the new image.
* <p>
* ROT_180: Result is the same as if FLIP_H is followed by a FLIP_V using
* LLJTran. So the bottom partial MCU strip mirrored horizontal, the right
* strip mirrored vertical and the bottom-right corner MCU block is left
* unchanged.
* <p>
* ROT_270: Right partial MCU strip is transposed and comes in the bottom
* edge the new image.
* <p>
* CROP: The x and y coordinates of the top-left corner of the crop area is
* adjusted to the nearest MCU boundary.
*/
public final static int OPT_XFORM_ADJUST_EDGES = 0x8;
/**
* Flag to specify that the Orientation marker in the Image header
* information (Exif) should be changed to reflect the new Orientation of
* the image after transformation.
*/
public final static int OPT_XFORM_ORIENTATION = 0x10;
/**
* Flag to specify that the App markers should be written when saving the
* image
*/
public final static int OPT_WRITE_APPXS = 0x100;
/**
* Flag to specify that Jpeg comments should be written when saving the
* image
*/
public final static int OPT_WRITE_COMMENTS = 0x200;
/**
* Flag containing all OPT_WRITE_XXX flags except OPT_WRITE_OPTIMIZE_HUFF
* for convenience
*/
public final static int OPT_WRITE_ALL = OPT_WRITE_APPXS
| OPT_WRITE_COMMENTS;
/**
* Flag to specify that the Huffman tables should be optimized before saving
* the image. This leads to a slightly reduced image file size.
*/
public final static int OPT_WRITE_OPTIMIZE_HUFF = 0x400;
/**
* Flag containing defaults for convenience. Includes OPT_XFORM_APPX,
* OPT_XFORM_ADJUST_EDGES and OPT_WRITE_ALL flags
*/
public final static int OPT_DEFAULTS = (OPT_XFORM_APPX
| OPT_XFORM_ADJUST_EDGES | OPT_WRITE_ALL);
/**
* Flag indicating that an entity should be replaced with what is there in
* the LLJTran object during
* {@link #xferInfo(InputStream, OutputStream, int, int) xferInfo(..)}
*/
public final static int REPLACE = 0;
/**
* Flag indicating that an entity should be retained during
* {@link #xferInfo(InputStream, OutputStream, int, int) xferInfo(..)}
*/
public final static int RETAIN = 1;
/**
* Flag indicating that an entity should be removed during
* {@link #xferInfo(InputStream, OutputStream, int, int) xferInfo(..)}
*/
public final static int REMOVE = 2;
/**
* Flag which specifies that the image width or x coordinate of the crop
* origin is not okay for a perfect transform.
*
* @see #checkPerfect(int, Rect)
*/
public final static int IMPERFECT_X = 1;
/**
* Flag which specifies that the image height or y coordinate of the crop
* origin is not okay for a perfect transform.
*
* @see #checkPerfect(int, Rect)
*/
public final static int IMPERFECT_Y = 2;
// New LLJTran internal read flags
/** Internal flag indicating header section */
protected static final int HEADER_SECTION = 2;
/** Internal flag indicating body section */
protected static final int BODY_SECTION = 4;
// Specifies if ImageInfo needs to be created
// If only this flag is set it means that the info
// is read and the rest is thrown as it was in BasicJpeg
/** Internal flag indicating image header info section */
protected static final int INFO_SECTION = 1;
/** Internal flag indicating all sections */
protected static final int ALL_SECTIONS = 7;
/*
* For Huffman Table Generation. assumed maximum initial code length before
* the JPEG mandate of limiting it to 16 is done.
*/
private static final int MAX_CLEN = 32;
/** Table to get jpeg zigzag order for coefficient arrays */
protected static final int jpegzigzagorder[] = { 0, 1, 5, 6, 14, 15, 27,
28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9,
11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22,
33, 38, 46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48,
49, 57, 58, 62, 63 };
/** Table to get the natural order index for a zigzag order index */
protected static final int jpegnaturalorder[] = { 0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27,
20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22,
15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61,
54, 47, 55, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, // extra entries
// for safety in
// decoder
63, 63, 63, 63, 63, 63, 63, 63 };
// Borrowed from jcparam.c in libjpeg 6b
private static final byte stdHuffTables[] = {
0, // Code to indicate DC Table 0(Luminance). Below are entries
// #Symbols of lengths 1-16
0,
1,
5,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
// Symbols
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
1, // Code to indicate DC Table 1(Chrominance). Below are entries
// #Symbols of lengths 1-16
0,
3,
1,
1,
1,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
// Symbols
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
16, // Code to indicate AC Table 0(Luminance). Below are entries
// #Symbols of lengths 1-16
0,
2,
1,
3,
3,
2,
4,
3,
5,
5,
4,
4,
0,
0,
1,
(byte) 0x7d,
// Symbols
(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x04,
(byte) 0x11, (byte) 0x05, (byte) 0x12, (byte) 0x21, (byte) 0x31,
(byte) 0x41, (byte) 0x06, (byte) 0x13, (byte) 0x51, (byte) 0x61,
(byte) 0x07, (byte) 0x22, (byte) 0x71, (byte) 0x14, (byte) 0x32,
(byte) 0x81, (byte) 0x91, (byte) 0xa1, (byte) 0x08, (byte) 0x23,
(byte) 0x42, (byte) 0xb1, (byte) 0xc1, (byte) 0x15, (byte) 0x52,
(byte) 0xd1, (byte) 0xf0, (byte) 0x24, (byte) 0x33, (byte) 0x62,
(byte) 0x72, (byte) 0x82, (byte) 0x09, (byte) 0x0a, (byte) 0x16,
(byte) 0x17, (byte) 0x18, (byte) 0x19, (byte) 0x1a, (byte) 0x25,
(byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29, (byte) 0x2a,
(byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37, (byte) 0x38,
(byte) 0x39, (byte) 0x3a, (byte) 0x43, (byte) 0x44, (byte) 0x45,
(byte) 0x46, (byte) 0x47, (byte) 0x48, (byte) 0x49,
(byte) 0x4a,
(byte) 0x53,
(byte) 0x54,
(byte) 0x55,
(byte) 0x56,
(byte) 0x57,
(byte) 0x58,
(byte) 0x59,
(byte) 0x5a,
(byte) 0x63,
(byte) 0x64,
(byte) 0x65,
(byte) 0x66,
(byte) 0x67,
(byte) 0x68,
(byte) 0x69,
(byte) 0x6a,
(byte) 0x73,
(byte) 0x74,
(byte) 0x75,
(byte) 0x76,
(byte) 0x77,
(byte) 0x78,
(byte) 0x79,
(byte) 0x7a,
(byte) 0x83,
(byte) 0x84,
(byte) 0x85,
(byte) 0x86,
(byte) 0x87,
(byte) 0x88,
(byte) 0x89,
(byte) 0x8a,
(byte) 0x92,
(byte) 0x93,
(byte) 0x94,
(byte) 0x95,
(byte) 0x96,
(byte) 0x97,
(byte) 0x98,
(byte) 0x99,
(byte) 0x9a,
(byte) 0xa2,
(byte) 0xa3,
(byte) 0xa4,
(byte) 0xa5,
(byte) 0xa6,
(byte) 0xa7,
(byte) 0xa8,
(byte) 0xa9,
(byte) 0xaa,
(byte) 0xb2,
(byte) 0xb3,
(byte) 0xb4,
(byte) 0xb5,
(byte) 0xb6,
(byte) 0xb7,
(byte) 0xb8,
(byte) 0xb9,
(byte) 0xba,
(byte) 0xc2,
(byte) 0xc3,
(byte) 0xc4,
(byte) 0xc5,
(byte) 0xc6,
(byte) 0xc7,
(byte) 0xc8,
(byte) 0xc9,
(byte) 0xca,
(byte) 0xd2,
(byte) 0xd3,
(byte) 0xd4,
(byte) 0xd5,
(byte) 0xd6,
(byte) 0xd7,
(byte) 0xd8,
(byte) 0xd9,
(byte) 0xda,
(byte) 0xe1,
(byte) 0xe2,
(byte) 0xe3,
(byte) 0xe4,
(byte) 0xe5,
(byte) 0xe6,
(byte) 0xe7,
(byte) 0xe8,
(byte) 0xe9,
(byte) 0xea,
(byte) 0xf1,
(byte) 0xf2,
(byte) 0xf3,
(byte) 0xf4,
(byte) 0xf5,
(byte) 0xf6,
(byte) 0xf7,
(byte) 0xf8,
(byte) 0xf9,
(byte) 0xfa,
17, // Code to indicate AC Table 1(Chrominance). Below are entries
// #Symbols of lengths 1-16
0,
2,
1,
2,
4,
4,
3,
4,
7,
5,
4,
4,
0,
1,
2,
(byte) 0x77,
// Symbols
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x11,
(byte) 0x04, (byte) 0x05, (byte) 0x21, (byte) 0x31, (byte) 0x06,
(byte) 0x12, (byte) 0x41, (byte) 0x51, (byte) 0x07, (byte) 0x61,
(byte) 0x71, (byte) 0x13, (byte) 0x22, (byte) 0x32, (byte) 0x81,
(byte) 0x08, (byte) 0x14, (byte) 0x42, (byte) 0x91, (byte) 0xa1,
(byte) 0xb1, (byte) 0xc1, (byte) 0x09, (byte) 0x23, (byte) 0x33,
(byte) 0x52, (byte) 0xf0, (byte) 0x15, (byte) 0x62, (byte) 0x72,
(byte) 0xd1, (byte) 0x0a, (byte) 0x16, (byte) 0x24, (byte) 0x34,
(byte) 0xe1, (byte) 0x25, (byte) 0xf1, (byte) 0x17, (byte) 0x18,
(byte) 0x19, (byte) 0x1a, (byte) 0x26, (byte) 0x27, (byte) 0x28,
(byte) 0x29, (byte) 0x2a, (byte) 0x35, (byte) 0x36, (byte) 0x37,
(byte) 0x38, (byte) 0x39, (byte) 0x3a, (byte) 0x43, (byte) 0x44,
(byte) 0x45, (byte) 0x46, (byte) 0x47, (byte) 0x48, (byte) 0x49,
(byte) 0x4a, (byte) 0x53, (byte) 0x54, (byte) 0x55, (byte) 0x56,
(byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x5a, (byte) 0x63,
(byte) 0x64, (byte) 0x65, (byte) 0x66, (byte) 0x67, (byte) 0x68,
(byte) 0x69, (byte) 0x6a, (byte) 0x73, (byte) 0x74, (byte) 0x75,
(byte) 0x76, (byte) 0x77, (byte) 0x78, (byte) 0x79, (byte) 0x7a,
(byte) 0x82, (byte) 0x83, (byte) 0x84, (byte) 0x85, (byte) 0x86,
(byte) 0x87, (byte) 0x88, (byte) 0x89, (byte) 0x8a, (byte) 0x92,
(byte) 0x93, (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97,
(byte) 0x98, (byte) 0x99, (byte) 0x9a, (byte) 0xa2, (byte) 0xa3,
(byte) 0xa4, (byte) 0xa5, (byte) 0xa6, (byte) 0xa7, (byte) 0xa8,
(byte) 0xa9, (byte) 0xaa, (byte) 0xb2, (byte) 0xb3, (byte) 0xb4,
(byte) 0xb5, (byte) 0xb6, (byte) 0xb7, (byte) 0xb8, (byte) 0xb9,
(byte) 0xba, (byte) 0xc2, (byte) 0xc3, (byte) 0xc4, (byte) 0xc5,
(byte) 0xc6, (byte) 0xc7, (byte) 0xc8, (byte) 0xc9, (byte) 0xca,
(byte) 0xd2, (byte) 0xd3, (byte) 0xd4, (byte) 0xd5, (byte) 0xd6,
(byte) 0xd7, (byte) 0xd8, (byte) 0xd9, (byte) 0xda, (byte) 0xe2,
(byte) 0xe3, (byte) 0xe4, (byte) 0xe5, (byte) 0xe6, (byte) 0xe7,
(byte) 0xe8, (byte) 0xe9, (byte) 0xea, (byte) 0xf2, (byte) 0xf3,
(byte) 0xf4, (byte) 0xf5, (byte) 0xf6, (byte) 0xf7, (byte) 0xf8,
(byte) 0xf9, (byte) 0xfa };
/** Artist info. Not used by LLJTran as of now. */
protected String artist;
private String enc;
private void commonInit() {
iWriteVars = new IterativeWriteVars();
iReadVars = new IterativeReadVars();
}
/**
* Constructor.
*
* @param file
* File object specifying the file to read image from. An
* internal input stream is created which is closed when the
* image is fully read or if an error occurs. The internal input
* Stream can be closed explicity by calling
* {@link #closeInternalInputStream()}
*/
public LLJTran(File file) {
commonInit();
this.file = file;
markerid = new byte[2];
prevHuffOption = -1;
readProgressCallback = null;
writeProgressCallback = null;
}
/**
* Constructor.
*
* @param inStream
* Source to read the image. This stream is not closed by LLJTran
* and it is the callers responsibility to do so.
*/
public LLJTran(InputStream inStream) {
commonInit();
markerid = new byte[2];
this.inStream = inStream;
prevHuffOption = -1;
readProgressCallback = null;
writeProgressCallback = null;
}
/**
* Resets the input for loading the image. This method is mainly for loading
* the image after reading upto READ_INFO and closing the input. false can
* be passed for the keep_appxs param for the read following this to retain
* the existing Image Header Information (Exif). A Runtime exception results
* if the image is already read beyond INFO_SECTION.
* <p>
*
* The method uses inStream as the new input, sets readUpto to READ_NONE and
* closes any internal Input Stream.
*
* @param inStream
* New Input. If null then the previous input must have been a
* file which will be used.
*/
public void resetInput(InputStream inStream) {
if (readUpto > READ_INFO)
throw new RuntimeException(
"Restting Input not allowed if current input read beyond READ_INFO");
if (inStream == null) {
if (file == null)
throw new RuntimeException(
"inStream null and no existing file to read from");
} else
file = null;
closeInternalInputStream();
this.inStream = inStream;
readUpto = READ_NONE;
unprocessed_marker = 0;
}
/**
* Resets the input for loading the image. This method is mainly for loading
* the image after reading upto READ_INFO and closing the input. false can
* be passed for the keep_appxs param for the read following this to retain
* the existing Image Header Information (Exif). A Runtime exception results
* if the image is already read beyond INFO_SECTION.
* <p>
*
* The method uses file as the new input, sets readUpto to READ_NONE and
* closes any internal Input Stream.
*
* @param file
* New Input file.
*/
public void resetInput(File file) {
if (readUpto > READ_INFO)
throw new RuntimeException(
"Restting Input not allowed if current input read beyond READ_INFO");
closeInternalInputStream();
this.file = file;
inStream = null;
readUpto = READ_NONE;
unprocessed_marker = 0;
}
/**
* Sets the encoding for Jpeg comments.
*
* @param enc
* Name of a jdk supported Charset or null to use default
* encoding
*/
public void setEncoding(String enc) {
this.enc = enc;
}
/**
* Gets the encoding which will be used for comments.
*
* @return Encoding or null to indicate platform default encoding.
*/
public String getEncoding() {
return enc;
}
/**
* Gets the Name of the Source file.
*
* @return Name of the Source file. Returns Unknown/Stream if reading from a
* stream
*/
public String getName() {
if (file == null)
return "Unknown/Stream";
return file.getName();
}
/**
* Gets the Name of the Source file.
*
* @see #getName()
*/
@Override
public String toString() {
return getName();
}
/**
* Returns the File source or null if reading from a Stream
*/
public File getFile() {
return file;
}
/**
* Checks if JPEG objects are equal TODO: point to the same file can be not
* sufficient
*
* @param o
* @return True if equal, false otherwise
*/
@Override
public boolean equals(Object o) {
if (o instanceof LLJTran && ((LLJTran) o).getFile().equals(file))
return true;
return false;
}
/**
* This gets the current error message because of which LLJTran has stopped
* processing the jpeg input. This results in an exception when using
* regular read methods. However when using initRead and nextRead via
* classes in android.mediautil.generic.directio package this method should
* be called to check for error messages due to which the image was not
* processed.
*
* @return The error message due to which LLJTran cannot proceed. Null if
* there is no error message
* @see #nextRead(int)
*/
public String getErrorMsg() {
return errorMsg;
}
/**
* Returns pending error message. This occurs when you read only upto the
* header section and the jpeg format is unsupported. You can read the Image
* Header Info (Exif) and jpeg comments but the next call to a read method
* will give an error. Note that if getErrorMsg() returns a non null error
* message this will return the same message.
*
* @return pending error message which will cause an exception for the next
* read call or null if no pending error message
*/
public String getPendingErrorMsg() {
return unprocessedError;
}
/** method for sub classes to set an error */
protected void setErrorMsg(String msg) {
errorMsg = msg;
}
/**
* Gets the exception if any corresponding an error.
*
* @return The exception if any corresponding to an error message.
* @see #getErrorMsg()
*/
public Exception getException() {
return lljtError;
}
/** method for sub classes to set an exception */
protected void setException(Exception e) {
lljtError = e;
}
/**
* Internal method to create an input stream from File for reading image.
*/
protected InputStream createInputStream() {
try {
if (valid) {
readcounter = 0;
writecounter = 0;
if (file == null) {
if (inStream != null)
return inStream;
else
valid = false;
} else {
return new BufferedInputStream(new FileInputStream(file));
}
}
} catch (FileNotFoundException e) {
valid = false;
}
return null;
}
/**
* Returns an instance of the Image Header Info. The Class of the Object
* returned is a specific implementation of AbstractImageInfo like Exif. If
* there is no specific Image Header Information like Exif an instance of
* JPEG containing basic Image Info is returned.
*
* @return Image Header Information
*/
public AbstractImageInfo<?> getImageInfo() {
return imageinfo;
}
/**
* Gets the Jpeg comment present in the image or null if none present.
*
* @return Jpeg comment present in the image or null if none present
*/
public String getComment() {
return out_comment;
}
/**
* Sets the jpeg comment to be written.
*
* @param comment
* Comment to be written or null if none is to be written
*/
public void setComment(String comment) {
out_comment = comment;
}
/**
* Gets full path of the File source or null if reading from a stream
*
* @return Full path of the File source or null if reading from a stream
*/
public String getLocationName() {
return file == null ? null : file.getAbsolutePath();
}
/**
* Sets callback to update the progress of Image read.
*
* @param callback
* Callback to update the progress of read or null for no
* callback
*/
public void setReadProgressCallback(ProgressCallback callback) {
readProgressCallback = callback;
}
/**
* Sets callback to update the progress of Image write.
*
* @param callback
* Callback to update the progress of write or null for no
* callback
*/
public void setWriteProgressCallback(ProgressCallback callback) {
writeProgressCallback = callback;
}
/**
* Gets the current Callback Object for Image Read progress or null if no
* callback is present.
*
* @return Current Callback Object for Image Read progress or null if no
* callback is present
*/
public ProgressCallback getReadProgressCallback() {
return readProgressCallback;
}
/**
* Gets the current Callback Object for Image Write progress or null if no
* callback is present.
*
* @return Current Callback Object for Image Write progress or null if no
* callback is present
*/
public ProgressCallback getWriteProgressCallback() {
return writeProgressCallback;
}
/**
* Internal method which transforms the Image Header Info (Like Exif) and
* updates the Appxs array. This method essentially calls writeInfo on the
* stored imageInfo.
*
* @param op
* The transform operation
* @param options
* Options specifies how to manage exif or other header content
* including embedded thumbnail transformation. Please pass a
* bitwise OR (|) of the required set of OPT_XFORM_.. flags.
* @param modifyImageInfo
* specifies if the instance of the imageInfo should be modified
* or just the appxs array should be updated
* @return Returns true if the method was successful or false if no data was
* written by the imageInfo's writeInfo method. Note that the
* default implementation provided in AbstractImageInfo is empty and
* this method would fail unless the subclass overrides it which is
* done as of now only in Exif.
* @see AbstractImageInfo