5
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
6
* (the "License"); you may not use this file except in compliance with
7
7
* the License. You may obtain a copy of the License at
9
9
* http://www.apache.org/licenses/LICENSE-2.0
11
11
* Unless required by applicable law or agreed to in writing, software
12
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50
51
checkSourceFlavor(src);
51
52
ImageGraphics2D g2dImage = (ImageGraphics2D)src;
53
//TODO Make configurable!
54
Object formatIntent = hints.get(ImageProcessingHints.BITMAP_TYPE_INTENT);
55
int bitsPerPixel = 24;
56
if (ImageProcessingHints.BITMAP_TYPE_INTENT_GRAY.equals(formatIntent)) {
58
} else if (ImageProcessingHints.BITMAP_TYPE_INTENT_MONO.equals(formatIntent)) {
62
Object transparencyIntent = hints.get(ImageProcessingHints.TRANSPARENCY_INTENT);
55
63
boolean withAlpha = true;
56
int resolution = 300; //default: 300dpi
64
if (ImageProcessingHints.TRANSPARENCY_INTENT_IGNORE.equals(transparencyIntent)) {
68
int resolution = GraphicsConstants.DEFAULT_SAMPLE_DPI;
57
69
Number res = (Number)hints.get(ImageProcessingHints.TARGET_RESOLUTION);
59
71
resolution = res.intValue();
62
BufferedImage bi = paintToBufferedImage(g2dImage, gray, withAlpha, resolution);
74
BufferedImage bi = paintToBufferedImage(g2dImage, bitsPerPixel, withAlpha, resolution);
64
76
ImageBuffered bufImage = new ImageBuffered(src.getInfo(), bi, null);
69
81
* Paints a Graphics2D image on a BufferedImage and returns this bitmap.
70
82
* @param g2dImage the Graphics2D image
71
* @param gray true if the generated image should be in grayscales
83
* @param bitsPerPixel the desired number of bits per pixel (supported: 1, 8, 24)
72
84
* @param withAlpha true if the generated image should have an alpha channel
73
85
* @param resolution the requested bitmap resolution
74
86
* @return the newly created BufferedImage
76
88
protected BufferedImage paintToBufferedImage(ImageGraphics2D g2dImage,
77
boolean gray, boolean withAlpha, int resolution) {
89
int bitsPerPixel, boolean withAlpha, int resolution) {
78
90
ImageSize size = g2dImage.getSize();
92
RenderingHints additionalHints = null;
80
93
int bmw = (int)Math.ceil(UnitConv.mpt2px(size.getWidthMpt(), resolution));
81
94
int bmh = (int)Math.ceil(UnitConv.mpt2px(size.getHeightMpt(), resolution));
96
switch (bitsPerPixel) {
98
bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_BINARY);
100
//withAlpha is ignored in this case
101
additionalHints = new RenderingHints(null);
102
//The following usually has no effect but some class libraries might support it
103
additionalHints.put(RenderingHints.KEY_DITHERING,
104
RenderingHints.VALUE_DITHER_ENABLE);
85
108
bi = createGrayBufferedImageWithAlpha(bmw, bmh);
87
110
bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
91
115
bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
93
117
bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
96
121
Graphics2D g2d = bi.createGraphics();
98
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
123
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
99
124
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
100
125
setRenderingHintsForBufferedImage(g2d);
126
if (additionalHints != null) {
127
g2d.addRenderingHints(additionalHints);
102
130
g2d.setBackground(Color.white);
103
131
g2d.setColor(Color.black);
104
132
if (!withAlpha) {
153
181
* @param g2d the Graphics2D instance
155
183
protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
156
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
184
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
157
185
RenderingHints.VALUE_ANTIALIAS_OFF);
158
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
186
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
159
187
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);