Changeset 415


Ignore:
Timestamp:
Feb 4, 2013, 10:37:29 PM (13 years ago)
Author:
dmik
Message:

jdk: Implement saving JPEG images through com.sun.image.codec.jpeg.JPEGImageEncoder.

Note that the whole com.sun.image.codec.jpeg package is a non-standard exgtension and is
deprecated. New apps should never use it. It will be thrown away in JDK 8. javax.imageio should be
used instead.

Location:
trunk/openjdk/jdk/src/share/classes/com/sun/image/codec/jpeg
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/openjdk/jdk/src/share/classes/com/sun/image/codec/jpeg/JPEGCodec.java

    r247 r415  
    1 /* JPEGCodec.java -- 
     1/* JPEGCodec.java --
    22   Copyright (C) 2007 Free Software Foundation, Inc.
    33   Copyright (C) 2007 Matthew Flaschen
     
    3939package com.sun.image.codec.jpeg;
    4040
     41import java.lang.RuntimeException;
     42import java.lang.UnsupportedOperationException;
     43
    4144import java.io.InputStream;
    4245import java.io.OutputStream;
    4346import java.io.IOException;
    4447
     48import java.awt.Graphics2D;
    4549import java.awt.image.BufferedImage;
    4650import java.awt.image.Raster;
     51import java.awt.image.ColorModel;
    4752
    4853import javax.imageio.*;
     
    6267        public static JPEGImageEncoder createJPEGEncoder(OutputStream os)
    6368        {
    64                 return null;
     69                return new ImageIOJPEGImageEncoder(os);
    6570        }
    6671
    6772        public static JPEGImageDecoder createJPEGDecoder(InputStream src, JPEGDecodeParam jdp)
    6873        {
    69                 return null;
    70         }
    71        
     74    return new ImageIOJPEGImageDecoder(src);
     75        }
     76
    7277        public static JPEGImageEncoder createJPEGEncoder(OutputStream dest, JPEGEncodeParam jep)
    7378        {
    74                 return null;
    75         }
    76        
     79    return new ImageIOJPEGImageEncoder(dest);
     80        }
     81
    7782        public static JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
    78         {
    79                 return null;
    80         }
    81        
     83  {
     84    throw new UnsupportedOperationException("FIX ME!");
     85  }
     86
    8287        public static JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands, int colorID)
    83         {
    84                 return null;
    85         }
    86                
     88  {
     89    throw new UnsupportedOperationException("FIX ME!");
     90  }
     91
    8792        public static JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
    88         {
    89                 return null;
    90         }
    91        
     93  {
     94    throw new UnsupportedOperationException("FIX ME!");
     95  }
     96
    9297        public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras, int colorID)
    93         {
    94                 return null;
    95         }
    96        
     98  {
     99    throw new UnsupportedOperationException("FIX ME!");
     100  }
     101
    97102
    98103        private static class ImageIOJPEGImageDecoder implements JPEGImageDecoder
    99104        {
    100                
     105
    101106                private static final String JPGMime = "image/jpeg";
    102    
     107
    103108                private ImageReader JPGReader;
    104                
     109
    105110                private InputStream in;
    106                
     111
    107112                private ImageIOJPEGImageDecoder (InputStream newIs)
    108113                {
    109114                        in = newIs;
    110                        
     115
    111116                        Iterator<ImageReader> JPGReaderIter = ImageIO.getImageReadersByMIMEType(JPGMime);
    112117                        if(JPGReaderIter.hasNext())
     
    114119                                JPGReader  = JPGReaderIter.next();
    115120                        }
    116                        
     121
    117122                        JPGReader.setInput(new MemoryCacheImageInputStream(in));
    118123                }
     
    122127                        return JPGReader.read(0);
    123128                }
    124                
     129
    125130                public Raster decodeAsRaster() throws IOException, ImageFormatException
    126131                {
    127132                        return JPGReader.readRaster(0, null);
    128133                }
    129                
     134
    130135                public InputStream getInputStream()
    131136                {
    132137                        return in;
    133138                }
    134                
     139
    135140                public JPEGDecodeParam getJPEGDecodeParam()
    136                 {
    137                         return null;
    138                 }
     141    {
     142      throw new UnsupportedOperationException("FIX ME!");
     143    }
    139144
    140145                public void setJPEGDecodeParam(JPEGDecodeParam jdp)
    141                 {
    142                         return;
    143                 }
    144 
    145         }
     146    {
     147      throw new UnsupportedOperationException("FIX ME!");
     148    }
     149
     150        }
     151
     152  private static class ImageIOJPEGEncodeParam implements JPEGEncodeParam
     153  {
     154    private ImageWriter writer;
     155    private ImageWriteParam p;
     156    private int width;
     157    private int height;
     158
     159    private ImageIOJPEGEncodeParam (ImageWriter _writer, int _width, int _height)
     160    {
     161      writer = _writer;
     162      p = _writer.getDefaultWriteParam();
     163      width = _width;
     164      height = _height;
     165    }
     166
     167    private ImageWriteParam getImageWriteParam()
     168    {
     169      return p;
     170    }
     171
     172    public void setQuality(float i, boolean b)
     173    {
     174      p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
     175      p.setCompressionQuality(i);
     176    }
     177
     178    public JPEGEncodeParam clone()
     179    {
     180      return new ImageIOJPEGEncodeParam(writer, width, height);
     181    }
     182
     183    public void setTableInfoValid(boolean b)
     184    {
     185      throw new UnsupportedOperationException("FIX ME!");
     186    }
     187
     188    public void setImageInfoValid(boolean b)
     189    {
     190      throw new UnsupportedOperationException("FIX ME!");
     191    }
     192
     193    public int getHorizontalSubsampling(int i)
     194    {
     195      throw new UnsupportedOperationException("FIX ME!");
     196    }
     197
     198    public int getVerticalSubsampling(int i)
     199    {
     200      throw new UnsupportedOperationException("FIX ME!");
     201    }
     202
     203    public int getWidth()
     204    {
     205      return width;
     206    }
     207
     208    public int getHeight()
     209    {
     210      return height;
     211    }
     212
     213    public int getDensityUnit()
     214    {
     215      throw new UnsupportedOperationException("FIX ME!");
     216    }
     217
     218    public int getXDensity()
     219    {
     220      throw new UnsupportedOperationException("FIX ME!");
     221    }
     222
     223    public int getYDensity()
     224    {
     225      throw new UnsupportedOperationException("FIX ME!");
     226    }
     227
     228    public int getRestartInterval()
     229    {
     230      throw new UnsupportedOperationException("FIX ME!");
     231    }
     232
     233    public JPEGQTable getQTable(int i)
     234    {
     235      throw new UnsupportedOperationException("FIX ME!");
     236    }
     237
     238    public void setDensityUnit(int i)
     239    {
     240      throw new UnsupportedOperationException("FIX ME!");
     241    }
     242
     243    public void setXDensity(int i)
     244    {
     245      throw new UnsupportedOperationException("FIX ME!");
     246    }
     247
     248    public void setYDensity(int i)
     249    {
     250      throw new UnsupportedOperationException("FIX ME!");
     251    }
     252
     253    public void setRestartInterval(int i)
     254    {
     255      throw new UnsupportedOperationException("FIX ME!");
     256    }
     257
     258    public void setQTable(int i, JPEGQTable jqt)
     259    {
     260      throw new UnsupportedOperationException("FIX ME!");
     261    }
     262  }
     263
     264  private static class ImageIOJPEGImageEncoder implements JPEGImageEncoder
     265  {
     266
     267    private static final String JPGMime = "image/jpeg";
     268
     269    private ImageWriter JPGWriter;
     270    private OutputStream out;
     271    private ImageIOJPEGEncodeParam param;
     272
     273    private ImageIOJPEGImageEncoder (OutputStream newOs)
     274    {
     275      out = newOs;
     276      param = null;
     277
     278      Iterator<ImageWriter> JPGWriterIter = ImageIO.getImageWritersByMIMEType(JPGMime);
     279      if(JPGWriterIter.hasNext())
     280      {
     281        JPGWriter = JPGWriterIter.next();
     282      }
     283
     284      JPGWriter.setOutput(new MemoryCacheImageOutputStream(out));
     285    }
     286
     287    private BufferedImage checkImage(BufferedImage bi)
     288    {
     289      boolean needsConversion = false;
     290
     291      int[] bandSizes = bi.getSampleModel().getSampleSize();
     292      for (int i = 0; i < bandSizes.length; i++)
     293      {
     294        if (bandSizes[i] > 8)
     295        {
     296          needsConversion = true;
     297          break;
     298        }
     299      }
     300
     301      if (needsConversion)
     302      {
     303        BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
     304        Graphics2D g = newBi.createGraphics();
     305        g.drawImage(bi, 0, 0, null);
     306        bi = newBi;
     307      }
     308
     309      return bi;
     310    }
     311
     312    public JPEGEncodeParam getJPEGEncodeParam()
     313    {
     314      return param;
     315    }
     316
     317    public void setJPEGEncodeParam(JPEGEncodeParam jep)
     318    {
     319      if (!(jep instanceof ImageIOJPEGEncodeParam))
     320        throw new RuntimeException("Must specify object returned by getDefaultJPEGEncodeParam()");
     321      param = (ImageIOJPEGEncodeParam)jep;
     322    }
     323
     324    public JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
     325    {
     326      return new ImageIOJPEGEncodeParam(JPGWriter, bi.getWidth(), bi.getHeight());
     327    }
     328
     329    public JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras, int colorID) throws ImageFormatException
     330    {
     331      return new ImageIOJPEGEncodeParam(JPGWriter, ras.getWidth(), ras.getHeight());
     332    }
     333
     334    public JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands, int colorID) throws ImageFormatException
     335    {
     336      throw new UnsupportedOperationException("FIX ME!");
     337    }
     338
     339    public JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp) throws ImageFormatException
     340    {
     341      throw new UnsupportedOperationException("FIX ME!");
     342    }
     343
     344    public int getDefaultColorId(ColorModel cm)
     345    {
     346      throw new UnsupportedOperationException("FIX ME!");
     347    }
     348
     349    public OutputStream getOutputStream()
     350    {
     351      return out;
     352    }
     353
     354    public void encode(BufferedImage bi, JPEGEncodeParam p) throws IOException, ImageFormatException
     355    {
     356      if (!(p instanceof ImageIOJPEGEncodeParam))
     357        throw new RuntimeException("Must specify object returned by getDefaultJPEGEncodeParam()");
     358      bi = checkImage(bi);
     359      JPGWriter.write(null, new IIOImage(bi, null, null), ((ImageIOJPEGEncodeParam)p).getImageWriteParam());
     360    }
     361
     362    public void encode(Raster ras) throws IOException, ImageFormatException
     363    {
     364      JPGWriter.write(null, new IIOImage(ras, null, null), param.getImageWriteParam());
     365    }
     366
     367    public void encode(BufferedImage bi) throws IOException, ImageFormatException
     368    {
     369      bi = checkImage(bi);
     370      JPGWriter.write(null, new IIOImage(bi, null, null), param.getImageWriteParam());
     371    }
     372
     373    public void encode(Raster ras, JPEGEncodeParam p) throws IOException, ImageFormatException
     374    {
     375      if (!(p instanceof ImageIOJPEGEncodeParam))
     376        throw new RuntimeException("Must specify object returned by getDefaultJPEGEncodeParam()");
     377      JPGWriter.write(null, new IIOImage(ras, null, null), ((ImageIOJPEGEncodeParam)p).getImageWriteParam());
     378    }
     379  }
    146380}
  • trunk/openjdk/jdk/src/share/classes/com/sun/image/codec/jpeg/JPEGEncodeParam.java

    r247 r415  
    1 /* JPEGEncodeParam.java -- 
     1/* JPEGEncodeParam.java --
    22   Copyright (C) 2007 Free Software Foundation, Inc.
    33
     
    3838package com.sun.image.codec.jpeg;
    3939
    40 public class JPEGEncodeParam
     40public interface JPEGEncodeParam
    4141{
    4242        public static final int COLOR_ID_UNKNOWN = 0;
     
    4848        public static final int COLOR_ID_YCbCr = 6;
    4949
    50         public JPEGEncodeParam()
    51         {
    52         }
     50        public void setQuality(float i, boolean b);
    5351
    54         public void setQuality(float i, boolean b)
    55         {
    56         }       
     52        public JPEGEncodeParam clone();
    5753
    58         public void setQuality(int i, boolean b)
    59         {
    60         }
     54        public void setTableInfoValid(boolean b);
    6155
    62         public JPEGEncodeParam clone()
    63         {
    64           return null;
    65         }
     56        public void setImageInfoValid(boolean b);
    6657
    67         public void setTableInfoValid(boolean b)
    68         {
    69         }
     58        public int getHorizontalSubsampling(int i);
    7059
    71         public void setImageInfoValid(boolean b)
    72         {
    73         }
     60        public int getVerticalSubsampling(int i);
    7461
    75         public int getHorizontalSubsampling(int i)
    76         {
    77           return 0;
    78         }
     62        public int getWidth();
    7963
    80         public int getVerticalSubsampling(int i)
    81         {
    82           return 0;
    83         }
     64        public int getHeight();
    8465
    85         public int getWidth()
    86         {
    87           return 0;
    88         }
     66        public int getDensityUnit();
    8967
    90         public int getHeight()
    91         {
    92           return 0;
    93         }
     68    public int getXDensity();
    9469
    95         public int getDensityUnit()
    96         {
    97           return 0;
    98         }
     70    public int getYDensity();
    9971
    100         public int getXDensity()
    101         {
    102           return 0;
    103         }
     72    public int getRestartInterval();
    10473
    105         public int getYDensity()
    106         {
    107           return 0;
    108         }
     74    public JPEGQTable getQTable(int i);
    10975
    110         public int getRestartInterval()
    111         {
    112           return 0;
    113         }
     76        public void setDensityUnit(int i);
    11477
    115         public JPEGQTable getQTable(int i)
    116         {
    117           return new JPEGQTable();
    118         }       
     78        public void setXDensity(int i);
    11979
    120         public void setDensityUnit(int i)
    121         {
    122         }
     80        public void setYDensity(int i);
    12381
    124         public void setXDensity(int i)
    125         {
    126         }
     82        public void setRestartInterval(int i);
    12783
    128         public void setYDensity(int i)
    129         {
    130         }
    131 
    132         public void setRestartInterval(int i)
    133         {
    134         }
    135 
    136         public void setQTable(int i, JPEGQTable jqt)
    137         {
    138         }
     84        public void setQTable(int i, JPEGQTable jqt);
    13985}
  • trunk/openjdk/jdk/src/share/classes/com/sun/image/codec/jpeg/JPEGImageEncoder.java

    r247 r415  
    1 /* JPEGImageEncoder.java -- 
     1/* JPEGImageEncoder.java --
    22   Copyright (C) 2007 Free Software Foundation, Inc.
    33
     
    4141import java.awt.image.BufferedImage;
    4242import java.awt.image.Raster;
     43import java.awt.image.ColorModel;
     44import java.io.OutputStream;
     45import java.io.IOException;
    4346
    44 public class JPEGImageEncoder
     47public interface JPEGImageEncoder
    4548{
    46         public JPEGImageEncoder()
    47         {
    48         }
    49        
    50         public JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
    51         {
    52                 return null;
    53         }
     49    public JPEGEncodeParam getJPEGEncodeParam();
    5450
    55         public void encode(BufferedImage bi, JPEGEncodeParam p)
    56         {
    57         }
     51    public void setJPEGEncodeParam(JPEGEncodeParam jep);
    5852
    59         public void encode(Raster bi)
    60         {
    61         }
     53    public JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi) throws ImageFormatException;
    6254
    63         public void encode(BufferedImage bi)
    64         {
    65         }
     55    public JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras, int colorID) throws ImageFormatException;
    6656
     57    public JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands, int colorID) throws ImageFormatException;
     58
     59    public JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp) throws ImageFormatException;
     60
     61    public int getDefaultColorId(ColorModel cm);
     62
     63    public OutputStream getOutputStream();
     64
     65        public void encode(BufferedImage bi, JPEGEncodeParam p) throws IOException, ImageFormatException;
     66
     67        public void encode(Raster ras) throws IOException, ImageFormatException;
     68
     69        public void encode(BufferedImage bi) throws IOException, ImageFormatException;
     70
     71    public void encode(Raster ras, JPEGEncodeParam p) throws IOException, ImageFormatException;
    6772}
Note: See TracChangeset for help on using the changeset viewer.