src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  26 package com.sun.imageio.plugins.jpeg;
  27 
  28 import javax.imageio.IIOException;
  29 import javax.imageio.ImageWriter;
  30 import javax.imageio.ImageWriteParam;
  31 import javax.imageio.IIOImage;
  32 import javax.imageio.ImageTypeSpecifier;
  33 import javax.imageio.metadata.IIOMetadata;
  34 import javax.imageio.metadata.IIOMetadataFormatImpl;
  35 import javax.imageio.metadata.IIOInvalidTreeException;
  36 import javax.imageio.spi.ImageWriterSpi;
  37 import javax.imageio.stream.ImageOutputStream;
  38 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
  39 import javax.imageio.plugins.jpeg.JPEGQTable;
  40 import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  41 
  42 import org.w3c.dom.Node;
  43 
  44 import java.awt.image.Raster;
  45 import java.awt.image.WritableRaster;
  46 import java.awt.image.SampleModel;
  47 import java.awt.image.DataBuffer;
  48 import java.awt.image.DataBufferByte;
  49 import java.awt.image.ColorModel;
  50 import java.awt.image.IndexColorModel;
  51 import java.awt.image.ColorConvertOp;
  52 import java.awt.image.RenderedImage;
  53 import java.awt.image.BufferedImage;
  54 import java.awt.color.ColorSpace;
  55 import java.awt.color.ICC_ColorSpace;
  56 import java.awt.color.ICC_Profile;
  57 import java.awt.Dimension;
  58 import java.awt.Rectangle;
  59 import java.awt.Transparency;
  60 
  61 import java.io.IOException;
  62 
  63 import java.util.List;
  64 import java.util.ArrayList;
  65 import java.util.Iterator;
  66 
  67 import sun.java2d.Disposer;


1031         int destWidth = (sourceWidth + periodX - 1)/periodX;
1032         int destHeight = (sourceHeight + periodY - 1)/periodY;
1033 
1034         // Create an appropriate 1-line databuffer for writing
1035         int lineSize = sourceWidth*numBandsUsed;
1036 
1037         DataBufferByte buffer = new DataBufferByte(lineSize);
1038 
1039         // Create a raster from that
1040         int [] bandOffs = JPEG.bandOffsets[numBandsUsed-1];
1041 
1042         raster = Raster.createInterleavedRaster(buffer,
1043                                                 sourceWidth, 1,
1044                                                 lineSize,
1045                                                 numBandsUsed,
1046                                                 bandOffs,
1047                                                 null);
1048 
1049         // Call the writer, who will call back for every scanline
1050 



1051         processImageStarted(currentImage);



1052 
1053         boolean aborted = false;
1054 
1055         if (debug) {
1056             System.out.println("inCsType: " + inCsType);
1057             System.out.println("outCsType: " + outCsType);
1058         }
1059 
1060         // Note that getData disables acceleration on buffer, but it is
1061         // just a 1-line intermediate data transfer buffer that does not
1062         // affect the acceleration of the source image.
1063         aborted = writeImage(structPointer,
1064                              buffer.getData(),
1065                              inCsType, outCsType,
1066                              numBandsUsed,
1067                              bandSizes,
1068                              sourceWidth,
1069                              destWidth, destHeight,
1070                              periodX, periodY,
1071                              qTables,


1208             sequencePrepared = false;
1209         } finally {
1210             clearThreadLock();
1211         }
1212     }
1213 
1214     public synchronized void abort() {
1215         setThreadLock();
1216         try {
1217             /**
1218              * NB: we do not check the call back lock here, we allow to abort
1219              * the reader any time.
1220              */
1221             super.abort();
1222             abortWrite(structPointer);
1223         } finally {
1224             clearThreadLock();
1225         }
1226     }
1227 

















1228     private void resetInternalState() {
1229         // reset C structures
1230         resetWriter(structPointer);
1231 
1232         // reset local Java structures
1233         srcRas = null;
1234         raster = null;
1235         convertTosRGB = false;
1236         currentImage = 0;
1237         numScans = 0;
1238         metadata = null;
1239     }
1240 
1241     public void reset() {
1242         setThreadLock();
1243         try {
1244             cbLock.check();
1245 
1246             super.reset();
1247         } finally {


   1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  26 package com.sun.imageio.plugins.jpeg;
  27 
  28 import javax.imageio.IIOException;
  29 import javax.imageio.ImageWriter;
  30 import javax.imageio.ImageWriteParam;
  31 import javax.imageio.IIOImage;
  32 import javax.imageio.ImageTypeSpecifier;
  33 import javax.imageio.metadata.IIOMetadata;
  34 import javax.imageio.metadata.IIOMetadataFormatImpl;
  35 import javax.imageio.metadata.IIOInvalidTreeException;
  36 import javax.imageio.spi.ImageWriterSpi;
  37 import javax.imageio.stream.ImageOutputStream;
  38 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
  39 import javax.imageio.plugins.jpeg.JPEGQTable;
  40 import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  41 
  42 import org.w3c.dom.Node;
  43 
  44 import java.awt.image.Raster;
  45 import java.awt.image.WritableRaster;


  46 import java.awt.image.DataBufferByte;
  47 import java.awt.image.ColorModel;
  48 import java.awt.image.IndexColorModel;
  49 import java.awt.image.ColorConvertOp;
  50 import java.awt.image.RenderedImage;
  51 import java.awt.image.BufferedImage;
  52 import java.awt.color.ColorSpace;
  53 import java.awt.color.ICC_ColorSpace;
  54 import java.awt.color.ICC_Profile;
  55 import java.awt.Dimension;
  56 import java.awt.Rectangle;
  57 import java.awt.Transparency;
  58 
  59 import java.io.IOException;
  60 
  61 import java.util.List;
  62 import java.util.ArrayList;
  63 import java.util.Iterator;
  64 
  65 import sun.java2d.Disposer;


1029         int destWidth = (sourceWidth + periodX - 1)/periodX;
1030         int destHeight = (sourceHeight + periodY - 1)/periodY;
1031 
1032         // Create an appropriate 1-line databuffer for writing
1033         int lineSize = sourceWidth*numBandsUsed;
1034 
1035         DataBufferByte buffer = new DataBufferByte(lineSize);
1036 
1037         // Create a raster from that
1038         int [] bandOffs = JPEG.bandOffsets[numBandsUsed-1];
1039 
1040         raster = Raster.createInterleavedRaster(buffer,
1041                                                 sourceWidth, 1,
1042                                                 lineSize,
1043                                                 numBandsUsed,
1044                                                 bandOffs,
1045                                                 null);
1046 
1047         // Call the writer, who will call back for every scanline
1048 
1049         clearAbortRequest();
1050         cbLock.lock();
1051         try {
1052             processImageStarted(currentImage);
1053         } finally {
1054             cbLock.unlock();
1055         }
1056 
1057         boolean aborted = false;
1058 
1059         if (debug) {
1060             System.out.println("inCsType: " + inCsType);
1061             System.out.println("outCsType: " + outCsType);
1062         }
1063 
1064         // Note that getData disables acceleration on buffer, but it is
1065         // just a 1-line intermediate data transfer buffer that does not
1066         // affect the acceleration of the source image.
1067         aborted = writeImage(structPointer,
1068                              buffer.getData(),
1069                              inCsType, outCsType,
1070                              numBandsUsed,
1071                              bandSizes,
1072                              sourceWidth,
1073                              destWidth, destHeight,
1074                              periodX, periodY,
1075                              qTables,


1212             sequencePrepared = false;
1213         } finally {
1214             clearThreadLock();
1215         }
1216     }
1217 
1218     public synchronized void abort() {
1219         setThreadLock();
1220         try {
1221             /**
1222              * NB: we do not check the call back lock here, we allow to abort
1223              * the reader any time.
1224              */
1225             super.abort();
1226             abortWrite(structPointer);
1227         } finally {
1228             clearThreadLock();
1229         }
1230     }
1231 
1232     @Override
1233     protected synchronized void clearAbortRequest() {
1234         setThreadLock();
1235         try {
1236             cbLock.check();
1237             if (abortRequested()) {
1238                 super.clearAbortRequest();
1239                 // reset C structures
1240                 resetWriter(structPointer);
1241                 // reset the native destination
1242                 setDest(structPointer);
1243             }
1244         } finally {
1245             clearThreadLock();
1246         }
1247     }
1248 
1249     private void resetInternalState() {
1250         // reset C structures
1251         resetWriter(structPointer);
1252 
1253         // reset local Java structures
1254         srcRas = null;
1255         raster = null;
1256         convertTosRGB = false;
1257         currentImage = 0;
1258         numScans = 0;
1259         metadata = null;
1260     }
1261 
1262     public void reset() {
1263         setThreadLock();
1264         try {
1265             cbLock.check();
1266 
1267             super.reset();
1268         } finally {