~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2008, 2009 Volker Berlin (i-net software)
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
 
 
25
package com.sun.imageio.plugins.jpeg;
 
26
 
 
27
import javax.imageio.ImageWriter;
 
28
import javax.imageio.ImageWriteParam;
 
29
import javax.imageio.IIOException;
 
30
import javax.imageio.IIOImage;
 
31
import javax.imageio.ImageTypeSpecifier;
 
32
import javax.imageio.metadata.IIOMetadata;
 
33
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
 
34
import javax.imageio.plugins.jpeg.JPEGQTable;
 
35
import javax.imageio.spi.ImageWriterSpi;
 
36
import javax.imageio.stream.ImageOutputStream;
 
37
import java.awt.image.BufferedImage;
 
38
 
 
39
import java.io.IOException;
 
40
 
 
41
import cli.System.Drawing.Imaging.Encoder;
 
42
import cli.System.Drawing.Imaging.EncoderParameter;
 
43
import cli.System.Drawing.Imaging.EncoderParameters;
 
44
import cli.System.Drawing.Imaging.ImageCodecFlags;
 
45
import cli.System.Drawing.Imaging.ImageCodecInfo;
 
46
import cli.System.Drawing.Imaging.ImageFormat;
 
47
 
 
48
/**
 
49
 * JPEGImageWriter that use .NET features to write the the JPG file.
 
50
 */
 
51
public class JPEGImageWriter extends ImageWriter {
 
52
 
 
53
    /**
 
54
     * Default constructor, Sun compatible.
 
55
     */
 
56
    protected JPEGImageWriter(ImageWriterSpi originatingProvider){
 
57
        super(originatingProvider);
 
58
    }
 
59
 
 
60
    /**
 
61
     * {@inheritDoc}
 
62
     */
 
63
    @Override
 
64
    public IIOMetadata convertImageMetadata(IIOMetadata inData, ImageTypeSpecifier imageType, ImageWriteParam param){
 
65
        return inData;
 
66
    }
 
67
 
 
68
    /**
 
69
     * {@inheritDoc}
 
70
     */
 
71
    @Override
 
72
    public IIOMetadata convertStreamMetadata(IIOMetadata inData, ImageWriteParam param){
 
73
        return inData;
 
74
    }
 
75
 
 
76
    /**
 
77
     * {@inheritDoc}
 
78
     */
 
79
    @Override
 
80
    public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param){
 
81
        return null;
 
82
    }
 
83
 
 
84
    /**
 
85
     * {@inheritDoc}
 
86
     */
 
87
    @Override
 
88
    public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param){
 
89
        return null;
 
90
    }
 
91
 
 
92
    /**
 
93
     * {@inheritDoc}
 
94
     */
 
95
    @Override
 
96
    public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException{
 
97
    
 
98
        ImageCodecInfo codec = null;
 
99
        for (ImageCodecInfo ici : ImageCodecInfo.GetImageEncoders()) {
 
100
            if (ici.get_FormatID().equals(ImageFormat.get_Jpeg().get_Guid())
 
101
                && (ici.get_Flags().Value & ImageCodecFlags.Builtin) != 0) {
 
102
                codec = ici;
 
103
                break;
 
104
            }
 
105
        }
 
106
        if (codec == null) {
 
107
            throw new IIOException("JPEG codec not found");
 
108
        }
 
109
    
 
110
        BufferedImage img = (BufferedImage)image.getRenderedImage();
 
111
        cli.System.Drawing.Bitmap bitmap = img.getBitmap();
 
112
        
 
113
        ImageOutputStream imgOutput = (ImageOutputStream)getOutput();
 
114
        
 
115
        JPEGImageWriteParam jparam = null;
 
116
        JPEGQTable[] qTables = null;
 
117
        
 
118
        if (param != null) {
 
119
            switch (param.getCompressionMode()) {
 
120
                case ImageWriteParam.MODE_DISABLED:
 
121
                    throw new IIOException("JPEG compression cannot be disabled");
 
122
                case ImageWriteParam.MODE_EXPLICIT:
 
123
                    float quality = param.getCompressionQuality();
 
124
                    quality = JPEG.convertToLinearQuality(quality);
 
125
                    qTables = new JPEGQTable[2];
 
126
                    qTables[0] = JPEGQTable.K1Luminance.getScaledInstance(quality, true);
 
127
                    qTables[1] = JPEGQTable.K2Chrominance.getScaledInstance(quality, true);
 
128
                    break;
 
129
                case ImageWriteParam.MODE_DEFAULT:
 
130
                    qTables = new JPEGQTable[2];
 
131
                    qTables[0] = JPEGQTable.K1Div2Luminance;
 
132
                    qTables[1] = JPEGQTable.K2Div2Chrominance;
 
133
                    break;
 
134
            }
 
135
            if (param instanceof JPEGImageWriteParam) {
 
136
                jparam = (JPEGImageWriteParam)param;
 
137
            }
 
138
        }
 
139
        
 
140
        if (qTables == null) {
 
141
            if (jparam != null && jparam.areTablesSet()) {
 
142
                qTables = jparam.getQTables();
 
143
            } else {
 
144
                qTables = JPEG.getDefaultQTables();
 
145
            }
 
146
        }
 
147
        
 
148
        // Create a MemoryStream with publicly visible buffer
 
149
        cli.System.IO.MemoryStream stream = new cli.System.IO.MemoryStream(1024);
 
150
        EncoderParameters params = new EncoderParameters(2);
 
151
        try {
 
152
            params.get_Param()[0] = new EncoderParameter(Encoder.LuminanceTable, qTableToShortArray(qTables[0]));
 
153
            params.get_Param()[1] = new EncoderParameter(Encoder.ChrominanceTable, qTableToShortArray(qTables[1]));
 
154
            bitmap.Save(stream, codec, params);
 
155
        }
 
156
        finally {
 
157
            params.Dispose();
 
158
        }
 
159
        
 
160
        imgOutput.write(stream.GetBuffer(), 0, (int)stream.get_Length());
 
161
    }
 
162
    
 
163
    private static short[] qTableToShortArray(JPEGQTable table) {
 
164
        int[] array = table.getTable();
 
165
        short[] s = new short[64];
 
166
        for (int i = 0; i < 64; i++)
 
167
            s[i] = (short)array[i];
 
168
        return s;
 
169
    }
 
170
 
 
171
    /**
 
172
     * {@inheritDoc}
 
173
     */
 
174
    @Override
 
175
    public ImageWriteParam getDefaultWriteParam() {
 
176
        return new JPEGImageWriteParam(null);
 
177
    }
 
178
}