Debian Codegraph @

src/main/java/org/jfree/pdf/stream/PDFSoftMaskImage.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.Graphics2D;
35 import java.awt.Image;
36 import java.awt.image.BufferedImage;
37 import org.jfree.pdf.dictionary.Dictionary;
38 import org.jfree.pdf.util.Args;
39
40 /**
41 * Represents a soft mask image in a PDF document.
42 */
43 public class PDFSoftMaskImage extends Stream {
44
45 /** The width. */
46 int width;
47
48 /** The height. */
49 int height;
50
51 /** The image. */
52 Image image;
53
54 /**
55 * Creates a new soft mask image object.
56 *
57 * @param number the PDF object number.
58 * @param img the AWT image object ({@code null} not permitted).
59 */
60 public PDFSoftMaskImage(int number, Image img) {
61 super(number);
62 Args.nullNotPermitted(img, "img");
63 this.width = img.getWidth(null);
64 this.height = img.getHeight(null);
65 this.image = img;
66 }
67
68 /**
69 * Returns the raw image data. Each call will resample the image data
70 * and populate a new array. Note that the stream may encode this
71 * data before it is written to the PDF output.
72 *
73 * @return The raw stream data.
74 */
75 @Override
76 public byte[] getRawStreamData() {
77 BufferedImage bi;
78 if (!(this.image instanceof BufferedImage)) {
79 bi = new BufferedImage(this.width, this.height,
80 BufferedImage.TYPE_INT_ARGB);
81 Graphics2D g2 = bi.createGraphics();
82 g2.drawImage(this.image, 0, 0, null);
83 } else {
84 bi = (BufferedImage) this.image;
85 }
86 // create a byte array of the image data to go in the PDF
87 byte[] result = new byte[this.width * this.height];
88 int i = 0;
89 for (int hh = this.height - 1; hh >= 0; hh--) {
90 for (int ww = 0; ww < this.width; ww++) {
91 int rgb = bi.getRGB(ww, hh);
92 result[i++] = (byte) (rgb >> 24);
93 }
94 }
95 return result;
96 }
97
98 /**
99 * Creates a dictionary reflecting the current configuration for this
100 * image.
101 *
102 * @param streamLength the stream length.
103 *
104 * @return A dictionary.
105 */
106 @Override
107 protected Dictionary createDictionary(int streamLength) {
108 Dictionary dictionary = super.createDictionary(streamLength);
109 dictionary.setType("/XObject");
110 dictionary.put("/Subtype", "/Image");
111 dictionary.put("/ColorSpace", "/DeviceGray");
112 dictionary.put("/BitsPerComponent", 8);
113 dictionary.put("/Width", this.width);
114 dictionary.put("/Height", this.height);
115 return dictionary;
116 }
117 }