~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/image/loader/impl/ImageConverterG2D2Bitmap.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
8
 
 * 
 
8
 *
9
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 * 
 
10
 *
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.
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: ImageConverterG2D2Bitmap.java 606580 2007-12-23 17:45:02Z jeremias $ */
19
 
 
 
18
/* $Id: ImageConverterG2D2Bitmap.java 798806 2009-07-29 08:11:13Z maxberger $ */
 
19
 
20
20
package org.apache.xmlgraphics.image.loader.impl;
21
21
 
22
22
import java.awt.Color;
34
34
import java.awt.image.WritableRaster;
35
35
import java.util.Map;
36
36
 
 
37
import org.apache.xmlgraphics.image.GraphicsConstants;
37
38
import org.apache.xmlgraphics.image.loader.Image;
38
39
import org.apache.xmlgraphics.image.loader.ImageFlavor;
39
40
import org.apache.xmlgraphics.image.loader.ImageProcessingHints;
50
51
        checkSourceFlavor(src);
51
52
        ImageGraphics2D g2dImage = (ImageGraphics2D)src;
52
53
 
53
 
        //TODO Make configurable!
54
 
        boolean gray = false;
 
54
        Object formatIntent = hints.get(ImageProcessingHints.BITMAP_TYPE_INTENT);
 
55
        int bitsPerPixel = 24;
 
56
        if (ImageProcessingHints.BITMAP_TYPE_INTENT_GRAY.equals(formatIntent)) {
 
57
            bitsPerPixel = 8;
 
58
        } else if (ImageProcessingHints.BITMAP_TYPE_INTENT_MONO.equals(formatIntent)) {
 
59
            bitsPerPixel = 1;
 
60
        }
 
61
 
 
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)) {
 
65
            withAlpha = false;
 
66
        }
 
67
 
 
68
        int resolution = GraphicsConstants.DEFAULT_SAMPLE_DPI;
57
69
        Number res = (Number)hints.get(ImageProcessingHints.TARGET_RESOLUTION);
58
70
        if (res != null) {
59
71
            resolution = res.intValue();
60
72
        }
61
 
        
62
 
        BufferedImage bi = paintToBufferedImage(g2dImage, gray, withAlpha, resolution);
63
 
        
 
73
 
 
74
        BufferedImage bi = paintToBufferedImage(g2dImage, bitsPerPixel, withAlpha, resolution);
 
75
 
64
76
        ImageBuffered bufImage = new ImageBuffered(src.getInfo(), bi, null);
65
77
        return bufImage;
66
78
    }
68
80
    /**
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
75
87
     */
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();
79
 
        
 
91
 
 
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));
82
95
        BufferedImage bi;
83
 
        if (gray) {
 
96
        switch (bitsPerPixel) {
 
97
        case 1:
 
98
            bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_BINARY);
 
99
            withAlpha = false;
 
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);
 
105
            break;
 
106
        case 8:
84
107
            if (withAlpha) {
85
108
                bi = createGrayBufferedImageWithAlpha(bmw, bmh);
86
109
            } else {
87
110
                bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
88
111
            }
89
 
        } else {
 
112
            break;
 
113
        default:
90
114
            if (withAlpha) {
91
115
                bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
92
116
            } else {
93
117
                bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
94
118
            }
95
119
        }
 
120
 
96
121
        Graphics2D g2d = bi.createGraphics();
97
122
        try {
98
 
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, 
 
123
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
99
124
                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
100
125
            setRenderingHintsForBufferedImage(g2d);
101
 
            
 
126
            if (additionalHints != null) {
 
127
                g2d.addRenderingHints(additionalHints);
 
128
            }
 
129
 
102
130
            g2d.setBackground(Color.white);
103
131
            g2d.setColor(Color.black);
104
132
            if (!withAlpha) {
153
181
     * @param g2d the Graphics2D instance
154
182
     */
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);
160
188
    }
161
189