1 50 51 package com.lowagie.text; 52 53 import java.awt.Graphics2D ; 54 import java.awt.color.ICC_Profile ; 55 import java.awt.image.BufferedImage ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.lang.reflect.Constructor ; 59 import java.net.MalformedURLException ; 60 import java.net.URL ; 61 import java.util.ArrayList ; 62 63 import com.lowagie.text.pdf.PRIndirectReference; 64 import com.lowagie.text.pdf.PdfArray; 65 import com.lowagie.text.pdf.PdfContentByte; 66 import com.lowagie.text.pdf.PdfDictionary; 67 import com.lowagie.text.pdf.PdfIndirectReference; 68 import com.lowagie.text.pdf.PdfName; 69 import com.lowagie.text.pdf.PdfNumber; 70 import com.lowagie.text.pdf.PdfOCG; 71 import com.lowagie.text.pdf.PdfObject; 72 import com.lowagie.text.pdf.PdfReader; 73 import com.lowagie.text.pdf.PdfTemplate; 74 import com.lowagie.text.pdf.PdfWriter; 75 import com.lowagie.text.pdf.RandomAccessFileOrArray; 76 import com.lowagie.text.pdf.codec.BmpImage; 77 import com.lowagie.text.pdf.codec.CCITTG4Encoder; 78 import com.lowagie.text.pdf.codec.GifImage; 79 import com.lowagie.text.pdf.codec.PngImage; 80 import com.lowagie.text.pdf.codec.TiffImage; 81 82 89 90 public abstract class Image extends Rectangle { 91 92 94 95 public static final int DEFAULT = 0; 96 97 98 public static final int RIGHT = 2; 99 100 101 public static final int LEFT = 0; 102 103 104 public static final int MIDDLE = 1; 105 106 107 public static final int TEXTWRAP = 4; 108 109 110 public static final int UNDERLYING = 8; 111 112 113 public static final int AX = 0; 114 115 116 public static final int AY = 1; 117 118 119 public static final int BX = 2; 120 121 122 public static final int BY = 3; 123 124 125 public static final int CX = 4; 126 127 128 public static final int CY = 5; 129 130 131 public static final int DX = 6; 132 133 134 public static final int DY = 7; 135 136 137 public static final int ORIGINAL_NONE = 0; 138 139 140 public static final int ORIGINAL_JPEG = 1; 141 142 143 public static final int ORIGINAL_PNG = 2; 144 145 146 public static final int ORIGINAL_GIF = 3; 147 148 149 public static final int ORIGINAL_BMP = 4; 150 151 152 public static final int ORIGINAL_TIFF = 5; 153 154 155 public static final int ORIGINAL_WMF = 6; 156 157 158 public static final int ORIGINAL_PS = 7; 159 160 162 163 protected int type; 164 165 166 protected URL url; 167 168 169 protected byte rawData[]; 170 171 172 protected int bpc = 1; 173 174 175 protected PdfTemplate template[] = new PdfTemplate[1]; 176 177 178 protected int alignment; 179 180 181 protected String alt; 182 183 184 protected float absoluteX = Float.NaN; 185 186 187 protected float absoluteY = Float.NaN; 188 189 190 protected float plainWidth; 191 192 193 protected float plainHeight; 194 195 196 protected float scaledWidth; 197 198 199 protected float scaledHeight; 200 201 202 protected Long mySerialId = getSerialId(); 203 204 206 212 public Image(URL url) { 213 super(0, 0); 214 this.url = url; 215 this.alignment = DEFAULT; 216 rotationRadians = 0; 217 } 218 219 229 public static Image getInstance(URL url) throws BadElementException, 230 MalformedURLException , IOException { 231 InputStream is = null; 232 try { 233 is = url.openStream(); 234 int c1 = is.read(); 235 int c2 = is.read(); 236 int c3 = is.read(); 237 int c4 = is.read(); 238 is.close(); 239 240 is = null; 241 if (c1 == 'G' && c2 == 'I' && c3 == 'F') { 242 GifImage gif = new GifImage(url); 243 Image img = gif.getImage(1); 244 return img; 245 } 246 if (c1 == 0xFF && c2 == 0xD8) { 247 return new Jpeg(url); 248 } 249 if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] 250 && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { 251 return PngImage.getImage(url); 252 } 253 if (c1 == 0xD7 && c2 == 0xCD) { 254 return new ImgWMF(url); 255 } 256 if (c1 == 'B' && c2 == 'M') { 257 return BmpImage.getImage(url); 258 } 259 if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) 260 || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { 261 RandomAccessFileOrArray ra = null; 262 try { 263 if (url.getProtocol().equals("file")) { 264 String file = url.getFile(); 265 file = Utilities.unEscapeURL(file); 266 ra = new RandomAccessFileOrArray(file); 267 } else 268 ra = new RandomAccessFileOrArray(url); 269 Image img = TiffImage.getTiffImage(ra, 1); 270 img.url = url; 271 return img; 272 } finally { 273 if (ra != null) 274 ra.close(); 275 } 276 277 } 278 throw new IOException (url.toString() 279 + " is not a recognized imageformat."); 280 } finally { 281 if (is != null) { 282 is.close(); 283 } 284 } 285 } 286 287 298 public static Image getInstance(String filename) 299 throws BadElementException, MalformedURLException , IOException { 300 return getInstance(Utilities.toURL(filename)); 301 } 302 303 313 public static Image getInstance(byte imgb[]) throws BadElementException, 314 MalformedURLException , IOException { 315 InputStream is = null; 316 try { 317 is = new java.io.ByteArrayInputStream (imgb); 318 int c1 = is.read(); 319 int c2 = is.read(); 320 int c3 = is.read(); 321 int c4 = is.read(); 322 is.close(); 323 324 is = null; 325 if (c1 == 'G' && c2 == 'I' && c3 == 'F') { 326 GifImage gif = new GifImage(imgb); 327 return gif.getImage(1); 328 } 329 if (c1 == 0xFF && c2 == 0xD8) { 330 return new Jpeg(imgb); 331 } 332 if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] 333 && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { 334 return PngImage.getImage(imgb); 335 } 336 if (c1 == 0xD7 && c2 == 0xCD) { 337 return new ImgWMF(imgb); 338 } 339 if (c1 == 'B' && c2 == 'M') { 340 return BmpImage.getImage(imgb); 341 } 342 if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) 343 || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { 344 RandomAccessFileOrArray ra = null; 345 try { 346 ra = new RandomAccessFileOrArray(imgb); 347 Image img = TiffImage.getTiffImage(ra, 1); 348 if (img.getOriginalData() == null) 349 img.setOriginalData(imgb); 350 return img; 351 } finally { 352 if (ra != null) 353 ra.close(); 354 } 355 356 } 357 throw new IOException ( 358 "The byte array is not a recognized imageformat."); 359 } finally { 360 if (is != null) { 361 is.close(); 362 } 363 } 364 } 365 366 383 public static Image getInstance(int width, int height, int components, 384 int bpc, byte data[]) throws BadElementException { 385 return Image.getInstance(width, height, components, bpc, data, null); 386 } 387 388 412 public static Image getInstance(int width, int height, boolean reverseBits, 413 int typeCCITT, int parameters, byte[] data) 414 throws BadElementException { 415 return Image.getInstance(width, height, reverseBits, typeCCITT, 416 parameters, data, null); 417 } 418 419 446 public static Image getInstance(int width, int height, boolean reverseBits, 447 int typeCCITT, int parameters, byte[] data, int transparency[]) 448 throws BadElementException { 449 if (transparency != null && transparency.length != 2) 450 throw new BadElementException( 451 "Transparency length must be equal to 2 with CCITT images"); 452 Image img = new ImgCCITT(width, height, reverseBits, typeCCITT, 453 parameters, data); 454 img.transparency = transparency; 455 return img; 456 } 457 458 478 public static Image getInstance(int width, int height, int components, 479 int bpc, byte data[], int transparency[]) 480 throws BadElementException { 481 if (transparency != null && transparency.length != components * 2) 482 throw new BadElementException( 483 "Transparency length must be equal to (componentes * 2)"); 484 if (components == 1 && bpc == 1) { 485 byte g4[] = CCITTG4Encoder.compress(data, width, height); 486 return Image.getInstance(width, height, false, Image.CCITTG4, 487 Image.CCITT_BLACKIS1, g4, transparency); 488 } 489 Image img = new ImgRaw(width, height, components, bpc, data); 490 img.transparency = transparency; 491 return img; 492 } 493 494 496 504 public static Image getInstance(PdfTemplate template) 505 throws BadElementException { 506 return new ImgTemplate(template); 507 } 508 509 511 527 public static Image getInstance(java.awt.Image image, java.awt.Color color, 528 boolean forceBW) throws BadElementException, IOException { 529 530 if(image instanceof BufferedImage ){ 531 BufferedImage bi = (BufferedImage ) image; 532 if(bi.getType()==BufferedImage.TYPE_BYTE_BINARY) { 533 forceBW=true; 534 } 535 } 536 537 java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber (image, 538 0, 0, -1, -1, true); 539 try { 540 pg.grabPixels(); 541 } catch (InterruptedException e) { 542 throw new IOException ( 543 "java.awt.Image Interrupted waiting for pixels!"); 544 } 545 if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) { 546 throw new IOException ("java.awt.Image fetch aborted or errored"); 547 } 548 int w = pg.getWidth(); 549 int h = pg.getHeight(); 550 int[] pixels = (int[]) pg.getPixels(); 551 if (forceBW) { 552 int byteWidth = (w / 8) + ((w & 7) != 0 ? 1 : 0); 553 byte[] pixelsByte = new byte[byteWidth * h]; 554 555 int index = 0; 556 int size = h * w; 557 int transColor = 1; 558 if (color != null) { 559 transColor = (color.getRed() + color.getGreen() 560 + color.getBlue() < 384) ? 0 : 1; 561 } 562 int transparency[] = null; 563 int cbyte = 0x80; 564 int wMarker = 0; 565 int currByte = 0; 566 if (color != null) { 567 for (int j = 0; j < size; j++) { 568 int alpha = (pixels[j] >> 24) & 0xff; 569 if (alpha < 250) { 570 if (transColor == 1) 571 currByte |= cbyte; 572 } else { 573 if ((pixels[j] & 0x888) != 0) 574 currByte |= cbyte; 575 } 576 cbyte >>= 1; 577 if (cbyte == 0 || wMarker + 1 >= w) { 578 pixelsByte[index++] = (byte) currByte; 579 cbyte = 0x80; 580 currByte = 0; 581 } 582 ++wMarker; 583 if (wMarker >= w) 584 wMarker = 0; 585 } 586 } else { 587 for (int j = 0; j < size; j++) { 588 if (transparency == null) { 589 int alpha = (pixels[j] >> 24) & 0xff; 590 if (alpha == 0) { 591 transparency = new int[2]; 592 transparency[0] = transparency[1] = ((pixels[j] & 0x888) != 0) ? 1 593 : 0; 594 } 595 } 596 if ((pixels[j] & 0x888) != 0) 597 currByte |= cbyte; 598 cbyte >>= 1; 599 if (cbyte == 0 || wMarker + 1 >= w) { 600 pixelsByte[index++] = (byte) currByte; 601 cbyte = 0x80; 602 currByte = 0; 603 } 604 ++wMarker; 605 if (wMarker >= w) 606 wMarker = 0; 607 } 608 } 609 return Image.getInstance(w, h, 1, 1, pixelsByte, transparency); 610 } else { 611 byte[] pixelsByte = new byte[w * h * 3]; 612 byte[] smask = null; 613 614 int index = 0; 615 int size = h * w; 616 int red = 255; 617 int green = 255; 618 int blue = 255; 619 if (color != null) { 620 red = color.getRed(); 621 green = color.getGreen(); 622 blue = color.getBlue(); 623 } 624 int transparency[] = null; 625 if (color != null) { 626 for (int j = 0; j < size; j++) { 627 int alpha = (pixels[j] >> 24) & 0xff; 628 if (alpha < 250) { 629 pixelsByte[index++] = (byte) red; 630 pixelsByte[index++] = (byte) green; 631 pixelsByte[index++] = (byte) blue; 632 } else { 633 pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff); 634 pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff); 635 pixelsByte[index++] = (byte) ((pixels[j]) & 0xff); 636 } 637 } 638 } else { 639 int transparentPixel = 0; 640 smask = new byte[w * h]; 641 boolean shades = false; 642 for (int j = 0; j < size; j++) { 643 byte alpha = smask[j] = (byte) ((pixels[j] >> 24) & 0xff); 644 645 if (!shades) { 646 if (alpha != 0 && alpha != -1) { 647 shades = true; 648 } else if (transparency == null) { 649 if (alpha == 0) { 650 transparentPixel = pixels[j] & 0xffffff; 651 transparency = new int[6]; 652 transparency[0] = transparency[1] = (transparentPixel >> 16) & 0xff; 653 transparency[2] = transparency[3] = (transparentPixel >> 8) & 0xff; 654 transparency[4] = transparency[5] = transparentPixel & 0xff; 655 } 656 } else if ((pixels[j] & 0xffffff) != transparentPixel) { 657 shades = true; 658 } 659 } 660 pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff); 661 pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff); 662 pixelsByte[index++] = (byte) ((pixels[j]) & 0xff); 663 } 664 if (shades) 665 transparency = null; 666 else 667 smask = null; 668 } 669 Image img = Image.getInstance(w, h, 3, 8, pixelsByte, transparency); 670 if (smask != null) { 671 Image sm = Image.getInstance(w, h, 1, 8, smask); 672 try { 673 sm.makeMask(); 674 img.setImageMask(sm); 675 } catch (DocumentException de) { 676 throw new ExceptionConverter(de); 677 } 678 } 679 return img; 680 } 681 } 682 683 697 public static Image getInstance(java.awt.Image image, java.awt.Color color) 698 throws BadElementException, IOException { 699 return Image.getInstance(image, color, false); 700 } 701 702 717 public static Image getInstance(PdfWriter writer, java.awt.Image awtImage, float quality) throws BadElementException, IOException { 718 return getInstance(new PdfContentByte(writer), awtImage, quality); 719 } 720 721 736 public static Image getInstance(PdfContentByte cb, java.awt.Image awtImage, float quality) throws BadElementException, IOException { 737 java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber (awtImage, 738 0, 0, -1, -1, true); 739 try { 740 pg.grabPixels(); 741 } catch (InterruptedException e) { 742 throw new IOException ( 743 "java.awt.Image Interrupted waiting for pixels!"); 744 } 745 if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) { 746 throw new IOException ("java.awt.Image fetch aborted or errored"); 747 } 748 int w = pg.getWidth(); 749 int h = pg.getHeight(); 750 PdfTemplate tp = cb.createTemplate(w, h); 751 Graphics2D g2d = tp.createGraphics(w, h, true, quality); 752 g2d.drawImage(awtImage, 0, 0, null); 753 g2d.dispose(); 754 return getInstance(tp); 755 } 756 757 759 764 private PdfIndirectReference directReference; 765 766 770 public PdfIndirectReference getDirectReference() { 771 return this.directReference; 772 } 773 774 778 public void setDirectReference(PdfIndirectReference directReference) { 779 this.directReference = directReference; 780 } 781 782 788 public static Image getInstance(PRIndirectReference ref) throws BadElementException { 789 PdfDictionary dic = (PdfDictionary)PdfReader.getPdfObjectRelease(ref); 790 int width = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue(); 791 int height = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue(); 792 Image imask = null; 793 PdfObject obj = dic.get(PdfName.SMASK); 794 if (obj != null && obj.isIndirect()) { 795 imask = getInstance((PRIndirectReference)obj); 796 } 797 else { 798 obj = dic.get(PdfName.MASK); 799 if (obj != null && obj.isIndirect()) { 800 PdfObject obj2 = PdfReader.getPdfObjectRelease(obj); 801 if (obj2 instanceof PdfDictionary) 802 imask = getInstance((PRIndirectReference)obj); 803 } 804 } 805 Image img = new ImgRaw(width, height, 1, 1, null); 806 img.imageMask = imask; 807 img.directReference = ref; 808 return img; 809 } 810 811 813 819 protected Image(Image image) { 820 super(image); 821 this.type = image.type; 822 this.url = image.url; 823 this.rawData = image.rawData; 824 this.bpc = image.bpc; 825 this.template = image.template; 826 this.alignment = image.alignment; 827 this.alt = image.alt; 828 this.absoluteX = image.absoluteX; 829 this.absoluteY = image.absoluteY; 830 this.plainWidth = image.plainWidth; 831 this.plainHeight = image.plainHeight; 832 this.scaledWidth = image.scaledWidth; 833 this.scaledHeight = image.scaledHeight; 834 this.mySerialId = image.mySerialId; 835 836 this.directReference = image.directReference; 837 838 this.rotationRadians = image.rotationRadians; 839 this.initialRotation = image.initialRotation; 840 this.indentationLeft = image.indentationLeft; 841 this.indentationRight = image.indentationRight; 842 this.spacingBefore = image.spacingBefore; 843 this.spacingAfter = image.spacingAfter; 844 845 this.widthPercentage = image.widthPercentage; 846 this.annotation = image.annotation; 847 this.layer = image.layer; 848 this.interpolation = image.interpolation; 849 this.originalType = image.originalType; 850 this.originalData = image.originalData; 851 this.deflated = image.deflated; 852 this.dpiX = image.dpiX; 853 this.dpiY = image.dpiY; 854 this.XYRatio = image.XYRatio; 855 856 this.colorspace = image.colorspace; 857 this.invert = image.invert; 858 this.profile = image.profile; 859 this.additional = image.additional; 860 this.mask = image.mask; 861 this.imageMask = image.imageMask; 862 this.smask = image.smask; 863 this.transparency = image.transparency; 864 } 865 866 873 public static Image getInstance(Image image) { 874 if (image == null) 875 return null; 876 try { 877 Class cs = image.getClass(); 878 Constructor constructor = cs 879 .getDeclaredConstructor(new Class [] { Image.class }); 880 return (Image) constructor.newInstance(new Object [] { image }); 881 } catch (Exception e) { 882 throw new ExceptionConverter(e); 883 } 884 } 885 886 888 893 894 public int type() { 895 return type; 896 } 897 898 900 906 907 public boolean isJpeg() { 908 return type == JPEG; 909 } 910 911 917 918 public boolean isImgRaw() { 919 return type == IMGRAW; 920 } 921 922 928 929 public boolean isImgTemplate() { 930 return type == IMGTEMPLATE; 931 } 932 933 935 941 942 public URL getUrl() { 943 return url; 944 } 945 946 952 public void setUrl(URL url) { 953 this.url = url; 954 } 955 956 964 public byte[] getRawData() { 965 return rawData; 966 } 967 968 976 public int getBpc() { 977 return bpc; 978 } 979 980 988 public PdfTemplate getTemplateData() { 989 return template[0]; 990 } 991 992 998 public void setTemplateData(PdfTemplate template) { 999 this.template[0] = template; 1000 } 1001 1002 1007 public int getAlignment() { 1008 return alignment; 1009 } 1010 1011 1017 1018 public void setAlignment(int alignment) { 1019 this.alignment = alignment; 1020 } 1021 1022 1027 1028 public String getAlt() { 1029 return alt; 1030 } 1031 1032 1038 1039 public void setAlt(String alt) { 1040 this.alt = alt; 1041 } 1042 1043 1049 1050 public void setAbsolutePosition(float absoluteX, float absoluteY) { 1051 this.absoluteX = absoluteX; 1052 this.absoluteY = absoluteY; 1053 } 1054 1055 1061 public boolean hasAbsoluteX() { 1062 return !Float.isNaN(absoluteX); 1063 } 1064 1065 1070 public float getAbsoluteX() { 1071 return absoluteX; 1072 } 1073 1074 1080 public boolean hasAbsoluteY() { 1081 return !Float.isNaN(absoluteY); 1082 } 1083 1084 1089 public float getAbsoluteY() { 1090 return absoluteY; 1091 } 1092 1093 1095 1100 public float getScaledWidth() { 1101 return scaledWidth; 1102 } 1103 1104 1109 public float getScaledHeight() { 1110 return scaledHeight; 1111 } 1112 1113 1118 public float getPlainWidth() { 1119 return plainWidth; 1120 } 1121 1122 1127 public float getPlainHeight() { 1128 return plainHeight; 1129 } 1130 1131 1139 public void scaleAbsolute(float newWidth, float newHeight) { 1140 plainWidth = newWidth; 1141 plainHeight = newHeight; 1142 float[] matrix = matrix(); 1143 scaledWidth = matrix[DX] - matrix[CX]; 1144 scaledHeight = matrix[DY] - matrix[CY]; 1145 } 1146 1147 1153 public void scaleAbsoluteWidth(float newWidth) { 1154 plainWidth = newWidth; 1155 float[] matrix = matrix(); 1156 scaledWidth = matrix[DX] - matrix[CX]; 1157 scaledHeight = matrix[DY] - matrix[CY]; 1158 } 1159 1160 1166 public void scaleAbsoluteHeight(float newHeight) { 1167 plainHeight = newHeight; 1168 float[] matrix = matrix(); 1169 scaledWidth = matrix[DX] - matrix[CX]; 1170 scaledHeight = matrix[DY] - matrix[CY]; 1171 } 1172 1173 1179 public void scalePercent(float percent) { 1180 scalePercent(percent, percent); 1181 } 1182 1183 1191 public void scalePercent(float percentX, float percentY) { 1192 plainWidth = (getWidth() * percentX) / 100f; 1193 plainHeight = (getHeight() * percentY) / 100f; 1194 float[] matrix = matrix(); 1195 scaledWidth = matrix[DX] - matrix[CX]; 1196 scaledHeight = matrix[DY] - matrix[CY]; 1197 } 1198 1199 1207 public void scaleToFit(float fitWidth, float fitHeight) { 1208 scalePercent(100); 1209 float percentX = (fitWidth * 100) / getScaledWidth(); 1210 float percentY = (fitHeight * 100) / getScaledHeight(); 1211 scalePercent(percentX < percentY ? percentX : percentY); 1212 } 1213 1214 1219 public float[] matrix() { 1220 float[] matrix = new float[8]; 1221 float cosX = (float) Math.cos(rotationRadians); 1222 float sinX = (float) Math.sin(rotationRadians); 1223 matrix[AX] = plainWidth * cosX; 1224 matrix[AY] = plainWidth * sinX; 1225 matrix[BX] = (-plainHeight) * sinX; 1226 matrix[BY] = plainHeight * cosX; 1227 if (rotationRadians < Math.PI / 2f) { 1228 matrix[CX] = matrix[BX]; 1229 matrix[CY] = 0; 1230 matrix[DX] = matrix[AX]; 1231 matrix[DY] = matrix[AY] + matrix[BY]; 1232 } else if (rotationRadians < Math.PI) { 1233 matrix[CX] = matrix[AX] + matrix[BX]; 1234 matrix[CY] = matrix[BY]; 1235 matrix[DX] = 0; 1236 matrix[DY] = matrix[AY]; 1237 } else if (rotationRadians < Math.PI * 1.5f) { 1238 matrix[CX] = matrix[AX]; 1239 matrix[CY] = matrix[AY] + matrix[BY]; 1240 matrix[DX] = matrix[BX]; 1241 matrix[DY] = 0; 1242 } else { 1243 matrix[CX] = 0; 1244 matrix[CY] = matrix[AY]; 1245 matrix[DX] = matrix[AX] + matrix[BX]; 1246 matrix[DY] = matrix[BY]; 1247 } 1248 return matrix; 1249 } 1250 1251 1253 1254 static long serialId = 0; 1255 1256 1257 static protected synchronized Long getSerialId() { 1258 ++serialId; 1259 return new Long (serialId); 1260 } 1261 1262 1267 public Long getMySerialId() { 1268 return mySerialId; 1269 } 1270 1271 1273 1274 protected float rotationRadians; 1275 1276 1277 private float initialRotation; 1278 1279 1283 public float getImageRotation() { 1284 double d = 2.0 * Math.PI; 1285 float rot = (float) ((rotationRadians - initialRotation) % d); 1286 if (rot < 0) { 1287 rot += d; 1288 } 1289 return rot; 1290 } 1291 1292 1298 public void setRotation(float r) { 1299 double d = 2.0 * Math.PI; 1300 rotationRadians = (float) ((r + initialRotation) % d); 1301 if (rotationRadians < 0) { 1302 rotationRadians += d; 1303 } 1304 float[] matrix = matrix(); 1305 scaledWidth = matrix[DX] - matrix[CX]; 1306 scaledHeight = matrix[DY] - matrix[CY]; 1307 } 1308 1309 1315 public void setRotationDegrees(float deg) { 1316 double d = Math.PI; 1317 setRotation(deg / 180 * (float) d); 1318 } 1319 1320 1324 public float getInitialRotation() { 1325 return this.initialRotation; 1326 } 1327 1328 1333 public void setInitialRotation(float initialRotation) { 1334 float old_rot = rotationRadians - this.initialRotation; 1335 this.initialRotation = initialRotation; 1336 setRotation(old_rot); 1337 } 1338 1339 1341 1342 protected float indentationLeft = 0; 1343 1344 1345 protected float indentationRight = 0; 1346 1347 1348 protected float spacingBefore; 1349 1350 1351 protected float spacingAfter; 1352 1353 1358 public float getIndentationLeft() { 1359 return indentationLeft; 1360 } 1361 1362 1367 public void setIndentationLeft(float f) { 1368 indentationLeft = f; 1369 } 1370 1371 1376 public float getIndentationRight() { 1377 return indentationRight; 1378 } 1379 1380 1385 public void setIndentationRight(float f) { 1386 indentationRight = f; 1387 } 1388 1389 1394 public float getSpacingBefore() { 1395 return spacingBefore; 1396 } 1397 1398 1404 1405 public void setSpacingBefore(float spacing) { 1406 this.spacingBefore = spacing; 1407 } 1408 1409 1414 public float getSpacingAfter() { 1415 return spacingAfter; 1416 } 1417 1418 1424 1425 public void setSpacingAfter(float spacing) { 1426 this.spacingAfter = spacing; 1427 } 1428 1429 1431 1434 private float widthPercentage = 100; 1435 1436 1441 public float getWidthPercentage() { 1442 return this.widthPercentage; 1443 } 1444 1445 1451 public void setWidthPercentage(float widthPercentage) { 1452 this.widthPercentage = widthPercentage; 1453 } 1454 1455 1457 1458 protected Annotation annotation = null; 1459 1460 1466 public void setAnnotation(Annotation annotation) { 1467 this.annotation = annotation; 1468 } 1469 1470 1475 public Annotation getAnnotation() { 1476 return annotation; 1477 } 1478 1479 1481 1482 protected PdfOCG layer; 1483 1484 1490 public PdfOCG getLayer() { 1491 return layer; 1492 } 1493 1494 1500 public void setLayer(PdfOCG layer) { 1501 this.layer = layer; 1502 } 1503 1504 1506 1507 protected boolean interpolation; 1508 1509 1514 public boolean isInterpolation() { 1515 return interpolation; 1516 } 1517 1518 1525 public void setInterpolation(boolean interpolation) { 1526 this.interpolation = interpolation; 1527 } 1528 1529 1531 1532 protected int originalType = ORIGINAL_NONE; 1533 1534 1535 protected byte[] originalData; 1536 1537 1543 public int getOriginalType() { 1544 return this.originalType; 1545 } 1546 1547 1554 public void setOriginalType(int originalType) { 1555 this.originalType = originalType; 1556 } 1557 1558 1564 public byte[] getOriginalData() { 1565 return this.originalData; 1566 } 1567 1568 1575 public void setOriginalData(byte[] originalData) { 1576 this.originalData = originalData; 1577 } 1578 1579 1581 1582 protected boolean deflated = false; 1583 1584 1590 public boolean isDeflated() { 1591 return this.deflated; 1592 } 1593 1594 1600 public void setDeflated(boolean deflated) { 1601 this.deflated = deflated; 1602 } 1603 1604 1606 1607 protected int dpiX = 0; 1608 1609 1610 protected int dpiY = 0; 1611 1612 1617 public int getDpiX() { 1618 return dpiX; 1619 } 1620 1621 1626 public int getDpiY() { 1627 return dpiY; 1628 } 1629 1630 1638 public void setDpi(int dpiX, int dpiY) { 1639 this.dpiX = dpiX; 1640 this.dpiY = dpiY; 1641 } 1642 1643 1645 1646 private float XYRatio = 0; 1647 1648 1653 public float getXYRatio() { 1654 return this.XYRatio; 1655 } 1656 1657 1663 public void setXYRatio(float XYRatio) { 1664 this.XYRatio = XYRatio; 1665 } 1666 1667 1669 1670 protected int colorspace = -1; 1671 1672 1679 public int getColorspace() { 1680 return colorspace; 1681 } 1682 1683 1684 protected boolean invert = false; 1685 1686 1691 public boolean isInverted() { 1692 return invert; 1693 } 1694 1695 1701 public void setInverted(boolean invert) { 1702 this.invert = invert; 1703 } 1704 1705 1706 protected ICC_Profile profile = null; 1707 1708 1714 public void tagICC(ICC_Profile profile) { 1715 this.profile = profile; 1716 } 1717 1718 1723 public boolean hasICCProfile() { 1724 return (this.profile != null); 1725 } 1726 1727 1732 public ICC_Profile getICCProfile() { 1733 return profile; 1734 } 1735 1736 1737 private PdfDictionary additional = null; 1738 1739 1744 public PdfDictionary getAdditional() { 1745 return this.additional; 1746 } 1747 1748 1754 public void setAdditional(PdfDictionary additional) { 1755 this.additional = additional; 1756 } 1757 1758 1761 public void simplifyColorspace() { 1762 if (additional == null) 1763 return; 1764 PdfObject value = additional.get(PdfName.COLORSPACE); 1765 if (value == null || !value.isArray()) 1766 return; 1767 PdfObject cs = simplifyColorspace(value); 1768 if (cs.isName()) 1769 value = cs; 1770 else { 1771 PdfObject first = (PdfObject)(((PdfArray)value).getArrayList().get(0)); 1772 if (PdfName.INDEXED.equals(first)) { 1773 ArrayList array = ((PdfArray)value).getArrayList(); 1774 if (array.size() >= 2 && ((PdfObject)array.get(1)).isArray()) { 1775 array.set(1, simplifyColorspace((PdfObject)array.get(1))); 1776 } 1777 } 1778 } 1779 additional.put(PdfName.COLORSPACE, value); 1780 } 1781 1782 1785 private PdfObject simplifyColorspace(PdfObject obj) { 1786 if (obj == null || !obj.isArray()) 1787 return obj; 1788 PdfObject first = (PdfObject)(((PdfArray)obj).getArrayList().get(0)); 1789 if (PdfName.CALGRAY.equals(first)) 1790 return PdfName.DEVICEGRAY; 1791 else if (PdfName.CALRGB.equals(first)) 1792 return PdfName.DEVICERGB; 1793 else 1794 return obj; 1795 } 1796 1797 1798 protected boolean mask = false; 1799 1800 1801 protected Image imageMask; 1802 1803 1804 private boolean smask; 1805 1806 1811 public boolean isMask() { 1812 return mask; 1813 } 1814 1815 1821 public void makeMask() throws DocumentException { 1822 if (!isMaskCandidate()) 1823 throw new DocumentException("This image can not be an image mask."); 1824 mask = true; 1825 } 1826 1827 1833 public boolean isMaskCandidate() { 1834 if (type == IMGRAW) { 1835 if (bpc > 0xff) 1836 return true; 1837 } 1838 return colorspace == 1; 1839 } 1840 1841 1846 public Image getImageMask() { 1847 return imageMask; 1848 } 1849 1850 1858 public void setImageMask(Image mask) throws DocumentException { 1859 if (this.mask) 1860 throw new DocumentException( 1861 "An image mask cannot contain another image mask."); 1862 if (!mask.mask) 1863 throw new DocumentException( 1864 "The image mask is not a mask. Did you do makeMask()?"); 1865 imageMask = mask; 1866 smask = (mask.bpc > 1 && mask.bpc <= 8); 1867 } 1868 1869 1875 public boolean isSmask() { 1876 return this.smask; 1877 } 1878 1879 1885 public void setSmask(boolean smask) { 1886 this.smask = smask; 1887 } 1888 1889 1890 protected int transparency[]; 1891 1892 1897 1898 public int[] getTransparency() { 1899 return transparency; 1900 } 1901 1902 1908 public void setTransparency(int transparency[]) { 1909 this.transparency = transparency; 1910 } 1911 1912 1914 1921 1922 public URL url() { 1923 return getUrl(); 1924 } 1925 1926 1935 public PdfTemplate templateData() { 1936 return getTemplateData(); 1937 } 1938 1939 1951 public static Image getInstance(java.util.Properties attributes) 1952 throws BadElementException, MalformedURLException , IOException { 1953 return com.lowagie.text.factories.ElementFactory.getImage(attributes); 1954 } 1955 1956 1962 public float indentationLeft() { 1963 return getIndentationLeft(); 1964 } 1965 1966 1972 public float indentationRight() { 1973 return getIndentationRight(); 1974 } 1975 1976 1982 public float spacingBefore() { 1983 return getSpacingBefore(); 1984 } 1985 1986 1992 public float spacingAfter() { 1993 return getSpacingAfter(); 1994 } 1995 1996 2005 public byte[] rawData() { 2006 return getRawData(); 2007 } 2008 2009 2018 public int bpc() { 2019 return getBpc(); 2020 } 2021 2022 2028 public Annotation annotation() { 2029 return getAnnotation(); 2030 } 2031 2032 2039 public boolean hasAbsolutePosition() { 2040 return hasAbsoluteY(); 2041 } 2042 2043 2049 public float absoluteX() { 2050 return getAbsoluteX(); 2051 } 2052 2053 2059 public float absoluteY() { 2060 return getAbsoluteY(); 2061 } 2062 2063 2069 public float plainWidth() { 2070 return getPlainWidth(); 2071 } 2072 2073 2079 public float plainHeight() { 2080 return getPlainHeight(); 2081 } 2082 2083 2089 public float scaledWidth() { 2090 return getScaledWidth(); 2091 } 2092 2093 2099 public float scaledHeight() { 2100 return getScaledHeight(); 2101 } 2102 2103 2109 public int alignment() { 2110 return getAlignment(); 2111 } 2112 2113 2119 2120 public String alt() { 2121 return getAlt(); 2122 } 2123 2124 2132 public int colorspace() { 2133 return getColorspace(); 2134 } 2135 2136 2144 public void setInvertMask(boolean invert) { 2145 setInverted(invert); 2146 } 2147 2148 2154 public boolean isInvertMask() { 2155 return isInverted(); 2156 } 2157} | Popular Tags |