-
Notifications
You must be signed in to change notification settings - Fork 23
/
Image.java
2008 lines (1793 loc) · 51.7 KB
/
Image.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
/*
* $Id: Image.java 3941 2009-05-28 14:52:26Z blowagie $
*
* Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import com.lowagie.text.pdf.PRIndirectReference;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfIndirectReference;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfOCG;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStream;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.BmpImage;
import com.lowagie.text.pdf.codec.CCITTG4Encoder;
import com.lowagie.text.pdf.codec.GifImage;
import com.lowagie.text.pdf.codec.JBIG2Image;
import com.lowagie.text.pdf.codec.PngImage;
import com.lowagie.text.pdf.codec.TiffImage;
/**
* An <CODE>Image</CODE> is the representation of a graphic element (JPEG, PNG
* or GIF) that has to be inserted into the document
*
* @see Element
* @see Rectangle
*/
public abstract class Image extends Rectangle {
// static final membervariables
/** this is a kind of image alignment. */
public static final int DEFAULT = 0;
/** this is a kind of image alignment. */
public static final int RIGHT = 2;
/** this is a kind of image alignment. */
public static final int LEFT = 0;
/** this is a kind of image alignment. */
public static final int MIDDLE = 1;
/** this is a kind of image alignment. */
public static final int TEXTWRAP = 4;
/** this is a kind of image alignment. */
public static final int UNDERLYING = 8;
/** This represents a coordinate in the transformation matrix. */
public static final int AX = 0;
/** This represents a coordinate in the transformation matrix. */
public static final int AY = 1;
/** This represents a coordinate in the transformation matrix. */
public static final int BX = 2;
/** This represents a coordinate in the transformation matrix. */
public static final int BY = 3;
/** This represents a coordinate in the transformation matrix. */
public static final int CX = 4;
/** This represents a coordinate in the transformation matrix. */
public static final int CY = 5;
/** This represents a coordinate in the transformation matrix. */
public static final int DX = 6;
/** This represents a coordinate in the transformation matrix. */
public static final int DY = 7;
/** type of image */
public static final int ORIGINAL_NONE = 0;
/** type of image */
public static final int ORIGINAL_JPEG = 1;
/** type of image */
public static final int ORIGINAL_PNG = 2;
/** type of image */
public static final int ORIGINAL_GIF = 3;
/** type of image */
public static final int ORIGINAL_BMP = 4;
/** type of image */
public static final int ORIGINAL_TIFF = 5;
/** type of image */
public static final int ORIGINAL_WMF = 6;
/** type of image */
public static final int ORIGINAL_PS = 7;
/** type of image */
public static final int ORIGINAL_JPEG2000 = 8;
/**
* type of image
* @since 2.1.5
*/
public static final int ORIGINAL_JBIG2 = 9;
// member variables
/** The image type. */
protected int type;
/** The URL of the image. */
protected URL url;
/** The raw data of the image. */
protected byte rawData[];
/** The bits per component of the raw image. It also flags a CCITT image. */
protected int bpc = 1;
/** The template to be treated as an image. */
protected PdfTemplate template[] = new PdfTemplate[1];
/** The alignment of the Image. */
protected int alignment;
/** Text that can be shown instead of the image. */
protected String alt;
/** This is the absolute X-position of the image. */
protected float absoluteX = Float.NaN;
/** This is the absolute Y-position of the image. */
protected float absoluteY = Float.NaN;
/** This is the width of the image without rotation. */
protected float plainWidth;
/** This is the width of the image without rotation. */
protected float plainHeight;
/** This is the scaled width of the image taking rotation into account. */
protected float scaledWidth;
/** This is the original height of the image taking rotation into account. */
protected float scaledHeight;
/**
* The compression level of the content streams.
* @since 2.1.3
*/
protected int compressionLevel = PdfStream.DEFAULT_COMPRESSION;
/** an iText attributed unique id for this image. */
protected Long mySerialId = getSerialId();
// image from file or URL
/**
* Constructs an <CODE>Image</CODE> -object, using an <VAR>url </VAR>.
*
* @param url
* the <CODE>URL</CODE> where the image can be found.
*/
public Image(URL url) {
super(0, 0);
this.url = url;
this.alignment = DEFAULT;
rotationRadians = 0;
}
/**
* Gets an instance of an Image.
*
* @param url
* an URL
* @return an Image
* @throws BadElementException
* @throws MalformedURLException
* @throws IOException
*/
public static Image getInstance(URL url) throws BadElementException,
MalformedURLException, IOException {
InputStream is = null;
try {
is = url.openStream();
int c1 = is.read();
int c2 = is.read();
int c3 = is.read();
int c4 = is.read();
// jbig2
int c5 = is.read();
int c6 = is.read();
int c7 = is.read();
int c8 = is.read();
is.close();
is = null;
if (c1 == 'G' && c2 == 'I' && c3 == 'F') {
GifImage gif = new GifImage(url);
Image img = gif.getImage(1);
return img;
}
if (c1 == 0xFF && c2 == 0xD8) {
return new Jpeg(url);
}
if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) {
return new Jpeg2000(url);
}
if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) {
return new Jpeg2000(url);
}
if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1]
&& c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) {
return PngImage.getImage(url);
}
if (c1 == 0xD7 && c2 == 0xCD) {
return new ImgWMF(url);
}
if (c1 == 'B' && c2 == 'M') {
return BmpImage.getImage(url);
}
if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
RandomAccessFileOrArray ra = null;
try {
if (url.getProtocol().equals("file")) {
String file = url.getFile();
file = Utilities.unEscapeURL(file);
ra = new RandomAccessFileOrArray(file);
} else
ra = new RandomAccessFileOrArray(url);
Image img = TiffImage.getTiffImage(ra, 1);
img.url = url;
return img;
} finally {
if (ra != null)
ra.close();
}
}
if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' &&
c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) {
RandomAccessFileOrArray ra = null;
try {
if (url.getProtocol().equals("file")) {
String file = url.getFile();
file = Utilities.unEscapeURL(file);
ra = new RandomAccessFileOrArray(file);
} else
ra = new RandomAccessFileOrArray(url);
Image img = JBIG2Image.getJbig2Image(ra, 1);
img.url = url;
return img;
} finally {
if (ra != null)
ra.close();
}
}
throw new IOException(url.toString()
+ " is not a recognized imageformat.");
} finally {
if (is != null) {
is.close();
}
}
}
/**
* Gets an instance of an Image.
*
* @param filename
* a filename
* @return an object of type <CODE>Gif</CODE>,<CODE>Jpeg</CODE> or
* <CODE>Png</CODE>
* @throws BadElementException
* @throws MalformedURLException
* @throws IOException
*/
public static Image getInstance(String filename)
throws BadElementException, MalformedURLException, IOException {
return getInstance(Utilities.toURL(filename));
}
/**
* gets an instance of an Image
*
* @param imgb
* raw image date
* @return an Image object
* @throws BadElementException
* @throws MalformedURLException
* @throws IOException
*/
public static Image getInstance(byte imgb[]) throws BadElementException,
MalformedURLException, IOException {
InputStream is = null;
try {
is = new java.io.ByteArrayInputStream(imgb);
int c1 = is.read();
int c2 = is.read();
int c3 = is.read();
int c4 = is.read();
is.close();
is = null;
if (c1 == 'G' && c2 == 'I' && c3 == 'F') {
GifImage gif = new GifImage(imgb);
return gif.getImage(1);
}
if (c1 == 0xFF && c2 == 0xD8) {
return new Jpeg(imgb);
}
if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) {
return new Jpeg2000(imgb);
}
if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) {
return new Jpeg2000(imgb);
}
if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1]
&& c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) {
return PngImage.getImage(imgb);
}
if (c1 == 0xD7 && c2 == 0xCD) {
return new ImgWMF(imgb);
}
if (c1 == 'B' && c2 == 'M') {
return BmpImage.getImage(imgb);
}
if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
RandomAccessFileOrArray ra = null;
try {
ra = new RandomAccessFileOrArray(imgb);
Image img = TiffImage.getTiffImage(ra, 1);
if (img.getOriginalData() == null)
img.setOriginalData(imgb);
return img;
} finally {
if (ra != null)
ra.close();
}
}
if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' ) {
is = new java.io.ByteArrayInputStream(imgb);
is.skip(4);
int c5 = is.read();
int c6 = is.read();
int c7 = is.read();
int c8 = is.read();
if ( c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) {
int file_header_flags = is.read();
int number_of_pages = -1;
if ( (file_header_flags & 0x2) == 0x2 ) {
number_of_pages = (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
}
is.close();
// a jbig2 file with a file header. the header is the only way we know here.
// embedded jbig2s don't have a header, have to create them by explicit use of Jbig2Image?
// nkerr, 2008-12-05 see also the getInstance(URL)
RandomAccessFileOrArray ra = null;
try {
ra = new RandomAccessFileOrArray(imgb);
Image img = JBIG2Image.getJbig2Image(ra, 1);
if (img.getOriginalData() == null)
img.setOriginalData(imgb);
return img;
} finally {
if (ra != null)
ra.close();
}
}
}
throw new IOException(
"The byte array is not a recognized imageformat.");
} finally {
if (is != null) {
is.close();
}
}
}
/**
* Gets an instance of an Image in raw mode.
*
* @param width
* the width of the image in pixels
* @param height
* the height of the image in pixels
* @param components
* 1,3 or 4 for GrayScale, RGB and CMYK
* @param data
* the image data
* @param bpc
* bits per component
* @return an object of type <CODE>ImgRaw</CODE>
* @throws BadElementException
* on error
*/
public static Image getInstance(int width, int height, int components,
int bpc, byte data[]) throws BadElementException {
return Image.getInstance(width, height, components, bpc, data, null);
}
/**
* Creates a JBIG2 Image.
* @param width the width of the image
* @param height the height of the image
* @param data the raw image data
* @param globals JBIG2 globals
* @since 2.1.5
*/
public static Image getInstance(int width, int height, byte[] data, byte[] globals) {
Image img = new ImgJBIG2(width, height, data, globals);
return img;
}
/**
* Creates an Image with CCITT G3 or G4 compression. It assumes that the
* data bytes are already compressed.
*
* @param width
* the exact width of the image
* @param height
* the exact height of the image
* @param reverseBits
* reverses the bits in <code>data</code>. Bit 0 is swapped
* with bit 7 and so on
* @param typeCCITT
* the type of compression in <code>data</code>. It can be
* CCITTG4, CCITTG31D, CCITTG32D
* @param parameters
* parameters associated with this stream. Possible values are
* CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFLINE and
* CCITT_ENDOFBLOCK or a combination of them
* @param data
* the image data
* @return an Image object
* @throws BadElementException
* on error
*/
public static Image getInstance(int width, int height, boolean reverseBits,
int typeCCITT, int parameters, byte[] data)
throws BadElementException {
return Image.getInstance(width, height, reverseBits, typeCCITT,
parameters, data, null);
}
/**
* Creates an Image with CCITT G3 or G4 compression. It assumes that the
* data bytes are already compressed.
*
* @param width
* the exact width of the image
* @param height
* the exact height of the image
* @param reverseBits
* reverses the bits in <code>data</code>. Bit 0 is swapped
* with bit 7 and so on
* @param typeCCITT
* the type of compression in <code>data</code>. It can be
* CCITTG4, CCITTG31D, CCITTG32D
* @param parameters
* parameters associated with this stream. Possible values are
* CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFLINE and
* CCITT_ENDOFBLOCK or a combination of them
* @param data
* the image data
* @param transparency
* transparency information in the Mask format of the image
* dictionary
* @return an Image object
* @throws BadElementException
* on error
*/
public static Image getInstance(int width, int height, boolean reverseBits,
int typeCCITT, int parameters, byte[] data, int transparency[])
throws BadElementException {
if (transparency != null && transparency.length != 2)
throw new BadElementException(
"Transparency length must be equal to 2 with CCITT images");
Image img = new ImgCCITT(width, height, reverseBits, typeCCITT,
parameters, data);
img.transparency = transparency;
return img;
}
/**
* Gets an instance of an Image in raw mode.
*
* @param width
* the width of the image in pixels
* @param height
* the height of the image in pixels
* @param components
* 1,3 or 4 for GrayScale, RGB and CMYK
* @param data
* the image data
* @param bpc
* bits per component
* @param transparency
* transparency information in the Mask format of the image
* dictionary
* @return an object of type <CODE>ImgRaw</CODE>
* @throws BadElementException
* on error
*/
public static Image getInstance(int width, int height, int components,
int bpc, byte data[], int transparency[])
throws BadElementException {
if (transparency != null && transparency.length != components * 2)
throw new BadElementException(
"Transparency length must be equal to (componentes * 2)");
if (components == 1 && bpc == 1) {
byte g4[] = CCITTG4Encoder.compress(data, width, height);
return Image.getInstance(width, height, false, Image.CCITTG4,
Image.CCITT_BLACKIS1, g4, transparency);
}
Image img = new ImgRaw(width, height, components, bpc, data);
img.transparency = transparency;
return img;
}
// images from a PdfTemplate
/**
* gets an instance of an Image
*
* @param template
* a PdfTemplate that has to be wrapped in an Image object
* @return an Image object
* @throws BadElementException
*/
public static Image getInstance(PdfTemplate template)
throws BadElementException {
return new ImgTemplate(template);
}
// images from a java.awt.Image
/**
* Gets an instance of an Image from a java.awt.Image.
*
* @param image
* the <CODE>java.awt.Image</CODE> to convert
* @param color
* if different from <CODE>null</CODE> the transparency pixels
* are replaced by this color
* @param forceBW
* if <CODE>true</CODE> the image is treated as black and white
* @return an object of type <CODE>ImgRaw</CODE>
* @throws BadElementException
* on error
* @throws IOException
* on error
*/
// public static Image getInstance(java.awt.Image image, java.awt.Color color,
// boolean forceBW) throws BadElementException, IOException {
//
// if(image instanceof BufferedImage){
// BufferedImage bi = (BufferedImage) image;
// if(bi.getType()==BufferedImage.TYPE_BYTE_BINARY) {
// forceBW=true;
// }
// }
//
// java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(image,
// 0, 0, -1, -1, true);
// try {
// pg.grabPixels();
// } catch (InterruptedException e) {
// throw new IOException(
// "java.awt.Image Interrupted waiting for pixels!");
// }
// if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) {
// throw new IOException("java.awt.Image fetch aborted or errored");
// }
// int w = pg.getWidth();
// int h = pg.getHeight();
// int[] pixels = (int[]) pg.getPixels();
// if (forceBW) {
// int byteWidth = (w / 8) + ((w & 7) != 0 ? 1 : 0);
// byte[] pixelsByte = new byte[byteWidth * h];
//
// int index = 0;
// int size = h * w;
// int transColor = 1;
// if (color != null) {
// transColor = (color.getRed() + color.getGreen()
// + color.getBlue() < 384) ? 0 : 1;
// }
// int transparency[] = null;
// int cbyte = 0x80;
// int wMarker = 0;
// int currByte = 0;
// if (color != null) {
// for (int j = 0; j < size; j++) {
// int alpha = (pixels[j] >> 24) & 0xff;
// if (alpha < 250) {
// if (transColor == 1)
// currByte |= cbyte;
// } else {
// if ((pixels[j] & 0x888) != 0)
// currByte |= cbyte;
// }
// cbyte >>= 1;
// if (cbyte == 0 || wMarker + 1 >= w) {
// pixelsByte[index++] = (byte) currByte;
// cbyte = 0x80;
// currByte = 0;
// }
// ++wMarker;
// if (wMarker >= w)
// wMarker = 0;
// }
// } else {
// for (int j = 0; j < size; j++) {
// if (transparency == null) {
// int alpha = (pixels[j] >> 24) & 0xff;
// if (alpha == 0) {
// transparency = new int[2];
// /* bugfix by M.P. Liston, ASC, was: ... ? 1: 0; */
// transparency[0] = transparency[1] = ((pixels[j] & 0x888) != 0) ? 0xff : 0;
// }
// }
// if ((pixels[j] & 0x888) != 0)
// currByte |= cbyte;
// cbyte >>= 1;
// if (cbyte == 0 || wMarker + 1 >= w) {
// pixelsByte[index++] = (byte) currByte;
// cbyte = 0x80;
// currByte = 0;
// }
// ++wMarker;
// if (wMarker >= w)
// wMarker = 0;
// }
// }
// return Image.getInstance(w, h, 1, 1, pixelsByte, transparency);
// } else {
// byte[] pixelsByte = new byte[w * h * 3];
// byte[] smask = null;
//
// int index = 0;
// int size = h * w;
// int red = 255;
// int green = 255;
// int blue = 255;
// if (color != null) {
// red = color.getRed();
// green = color.getGreen();
// blue = color.getBlue();
// }
// int transparency[] = null;
// if (color != null) {
// for (int j = 0; j < size; j++) {
// int alpha = (pixels[j] >> 24) & 0xff;
// if (alpha < 250) {
// pixelsByte[index++] = (byte) red;
// pixelsByte[index++] = (byte) green;
// pixelsByte[index++] = (byte) blue;
// } else {
// pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);
// pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff);
// pixelsByte[index++] = (byte) ((pixels[j]) & 0xff);
// }
// }
// } else {
// int transparentPixel = 0;
// smask = new byte[w * h];
// boolean shades = false;
// for (int j = 0; j < size; j++) {
// byte alpha = smask[j] = (byte) ((pixels[j] >> 24) & 0xff);
// /* bugfix by Chris Nokleberg */
// if (!shades) {
// if (alpha != 0 && alpha != -1) {
// shades = true;
// } else if (transparency == null) {
// if (alpha == 0) {
// transparentPixel = pixels[j] & 0xffffff;
// transparency = new int[6];
// transparency[0] = transparency[1] = (transparentPixel >> 16) & 0xff;
// transparency[2] = transparency[3] = (transparentPixel >> 8) & 0xff;
// transparency[4] = transparency[5] = transparentPixel & 0xff;
// }
// } else if ((pixels[j] & 0xffffff) != transparentPixel) {
// shades = true;
// }
// }
// pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);
// pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff);
// pixelsByte[index++] = (byte) ((pixels[j]) & 0xff);
// }
// if (shades)
// transparency = null;
// else
// smask = null;
// }
// Image img = Image.getInstance(w, h, 3, 8, pixelsByte, transparency);
// if (smask != null) {
// Image sm = Image.getInstance(w, h, 1, 8, smask);
// try {
// sm.makeMask();
// img.setImageMask(sm);
// } catch (DocumentException de) {
// throw new ExceptionConverter(de);
// }
// }
// return img;
// }
// }
/**
* Gets an instance of an Image from a java.awt.Image.
*
* @param image
* the <CODE>java.awt.Image</CODE> to convert
* @param color
* if different from <CODE>null</CODE> the transparency pixels
* are replaced by this color
* @return an object of type <CODE>ImgRaw</CODE>
* @throws BadElementException
* on error
* @throws IOException
* on error
*/
// public static Image getInstance(java.awt.Image image, java.awt.Color color)
// throws BadElementException, IOException {
// return Image.getInstance(image, color, false);
// }
/**
* Gets an instance of a Image from a java.awt.Image.
* The image is added as a JPEG with a user defined quality.
*
* @param writer
* the <CODE>PdfWriter</CODE> object to which the image will be added
* @param awtImage
* the <CODE>java.awt.Image</CODE> to convert
* @param quality
* a float value between 0 and 1
* @return an object of type <CODE>PdfTemplate</CODE>
* @throws BadElementException
* on error
* @throws IOException
*/
// public static Image getInstance(PdfWriter writer, java.awt.Image awtImage, float quality) throws BadElementException, IOException {
// return getInstance(new PdfContentByte(writer), awtImage, quality);
// }
/**
* Gets an instance of a Image from a java.awt.Image.
* The image is added as a JPEG with a user defined quality.
*
* @param cb
* the <CODE>PdfContentByte</CODE> object to which the image will be added
* @param awtImage
* the <CODE>java.awt.Image</CODE> to convert
* @param quality
* a float value between 0 and 1
* @return an object of type <CODE>PdfTemplate</CODE>
* @throws BadElementException
* on error
* @throws IOException
*/
// public static Image getInstance(PdfContentByte cb, java.awt.Image awtImage, float quality) throws BadElementException, IOException {
// java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(awtImage,
// 0, 0, -1, -1, true);
// try {
// pg.grabPixels();
// } catch (InterruptedException e) {
// throw new IOException(
// "java.awt.Image Interrupted waiting for pixels!");
// }
// if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) {
// throw new IOException("java.awt.Image fetch aborted or errored");
// }
// int w = pg.getWidth();
// int h = pg.getHeight();
// PdfTemplate tp = cb.createTemplate(w, h);
// Graphics2D g2d = tp.createGraphics(w, h, true, quality);
// g2d.drawImage(awtImage, 0, 0, null);
// g2d.dispose();
// return getInstance(tp);
// }
// image from indirect reference
/**
* Holds value of property directReference.
* An image is embedded into a PDF as an Image XObject.
* This object is referenced by a PdfIndirectReference object.
*/
private PdfIndirectReference directReference;
/**
* Getter for property directReference.
* @return Value of property directReference.
*/
public PdfIndirectReference getDirectReference() {
return this.directReference;
}
/**
* Setter for property directReference.
* @param directReference New value of property directReference.
*/
public void setDirectReference(PdfIndirectReference directReference) {
this.directReference = directReference;
}
/**
* Reuses an existing image.
* @param ref the reference to the image dictionary
* @throws BadElementException on error
* @return the image
*/
public static Image getInstance(PRIndirectReference ref) throws BadElementException {
PdfDictionary dic = (PdfDictionary)PdfReader.getPdfObjectRelease(ref);
int width = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue();
int height = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue();
Image imask = null;
PdfObject obj = dic.get(PdfName.SMASK);
if (obj != null && obj.isIndirect()) {
imask = getInstance((PRIndirectReference)obj);
}
else {
obj = dic.get(PdfName.MASK);
if (obj != null && obj.isIndirect()) {
PdfObject obj2 = PdfReader.getPdfObjectRelease(obj);
if (obj2 instanceof PdfDictionary)
imask = getInstance((PRIndirectReference)obj);
}
}
Image img = new ImgRaw(width, height, 1, 1, null);
img.imageMask = imask;
img.directReference = ref;
return img;
}
// copy constructor
/**
* Constructs an <CODE>Image</CODE> -object, using an <VAR>url </VAR>.
*
* @param image
* another Image object.
*/
protected Image(Image image) {
super(image);
this.type = image.type;
this.url = image.url;
this.rawData = image.rawData;
this.bpc = image.bpc;
this.template = image.template;
this.alignment = image.alignment;
this.alt = image.alt;
this.absoluteX = image.absoluteX;
this.absoluteY = image.absoluteY;
this.plainWidth = image.plainWidth;
this.plainHeight = image.plainHeight;
this.scaledWidth = image.scaledWidth;
this.scaledHeight = image.scaledHeight;
this.mySerialId = image.mySerialId;
this.directReference = image.directReference;
this.rotationRadians = image.rotationRadians;
this.initialRotation = image.initialRotation;
this.indentationLeft = image.indentationLeft;
this.indentationRight = image.indentationRight;
this.spacingBefore = image.spacingBefore;
this.spacingAfter = image.spacingAfter;
this.widthPercentage = image.widthPercentage;
this.annotation = image.annotation;
this.layer = image.layer;
this.interpolation = image.interpolation;
this.originalType = image.originalType;
this.originalData = image.originalData;
this.deflated = image.deflated;
this.dpiX = image.dpiX;
this.dpiY = image.dpiY;
this.XYRatio = image.XYRatio;
this.colorspace = image.colorspace;
this.invert = image.invert;
this.additional = image.additional;
this.mask = image.mask;
this.imageMask = image.imageMask;
this.smask = image.smask;
this.transparency = image.transparency;
}
/**
* gets an instance of an Image
*
* @param image
* an Image object
* @return a new Image object
*/
public static Image getInstance(Image image) {
if (image == null)
return null;
try {
Class cs = image.getClass();
Constructor constructor = cs
.getDeclaredConstructor(new Class[] { Image.class });
return (Image) constructor.newInstance(new Object[] { image });
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
// implementation of the Element interface
/**
* Returns the type.
*
* @return a type
*/
public int type() {
return type;
}
/**
* @see com.lowagie.text.Element#isNestable()
* @since iText 2.0.8
*/
public boolean isNestable() {
return true;
}
// checking the type of Image
/**
* Returns <CODE>true</CODE> if the image is a <CODE>Jpeg</CODE>
* -object.
*