Debian Codegraph @

src/main/java/org/jfree/pdf/stream/PDFImage.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.stream;
33
34 import java.awt.Color;
35 import java.awt.Graphics2D;
36 import java.awt.Image;
37 import java.awt.image.BufferedImage;
38 import org.jfree.pdf.dictionary.Dictionary;
39 import org.jfree.pdf.util.Args;
40
41 /**
42 * Represents an image in a PDF document.
43 */
44 public class PDFImage extends Stream {
45
46 /** The width. */
47 int width;
48
49 /** The height. */
50 int height;
51
52 /** The image. */
53 Image image;
54
55 String softMaskImageRef;
56
57 /**
58 * Creates a new image object.
59 *
60 * @param number the PDF object number.
61 * @param img the AWT image object ({@code null} not permitted).
62 * @param softMaskImageRef the soft mask image reference ({@code null}
63 * permitted).
64 */
65 public PDFImage(int number, Image img, String softMaskImageRef) {
66 super(number);
67 Args.nullNotPermitted(img, "img");
68 this.width = img.getWidth(null);
69 this.height = img.getHeight(null);
70 this.image = img;
71 this.softMaskImageRef = softMaskImageRef;
72 }
73
74 /**
75 * Returns the raw image data. Each call will resample the image data
76 * and populate a new array. Note that the stream may encode this
77 * data before it is written to the PDF output.
78 *
79 * @return The raw stream data.
80 */
81 @Override
82 public byte[] getRawStreamData() {
83 BufferedImage bi;
84 if (!(this.image instanceof BufferedImage)) {
85 bi = new BufferedImage(this.width, this.height,
86 BufferedImage.TYPE_INT_ARGB);
87 Graphics2D g2 = bi.createGraphics();
88 g2.setBackground(new Color(255, 255, 255, 0));
89 g2.clearRect(0, 0, this.width, this.height);
90 g2.drawImage(this.image, 0, 0, null);
91 } else {
92 bi = (BufferedImage) this.image;
93 }
94 // create a byte array of the image data to go in the PDF
95 byte[] result = new byte[this.width * this.height * 3];
96 int i = 0;
97 for (int hh = this.height - 1; hh >= 0; hh--) {
98 for (int ww = 0; ww < this.width; ww++) {
99 int rgb = bi.getRGB(ww, hh);
100 result[i++] = (byte) (rgb >> 16);
101 result[i++] = (byte) (rgb >> 8);
102 result[i++] = (byte) rgb;
103 }
104 }
105 return result;
106 }
107
108 /**
109 * Creates a dictionary reflecting the current configuration for this
110 * image.
111 *
112 * @param streamLength the stream length.
113 *
114 * @return A dictionary.
115 */
116 @Override
117 protected Dictionary createDictionary(int streamLength) {
118 Dictionary dictionary = super.createDictionary(streamLength);
119 dictionary.setType("/XObject");
120 dictionary.put("/Subtype", "/Image");
121 dictionary.put("/ColorSpace", "/DeviceRGB");
122 dictionary.put("/BitsPerComponent", 8);
123 dictionary.put("/Width", this.width);
124 dictionary.put("/Height", this.height);
125 if (this.softMaskImageRef != null) {
126 dictionary.put("/SMask", this.softMaskImageRef);
127 }
128 return dictionary;
129 }
130 }