< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriter.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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


 139     }
 140 
 141     public boolean canWriteRasters() {
 142         return true;
 143     }
 144 
 145     public void write(IIOMetadata streamMetadata,
 146                       IIOImage image,
 147                       ImageWriteParam param) throws IOException {
 148 
 149         if (stream == null) {
 150             throw new IllegalStateException(I18N.getString("BMPImageWriter7"));
 151         }
 152 
 153         if (image == null) {
 154             throw new IllegalArgumentException(I18N.getString("BMPImageWriter8"));
 155         }
 156 
 157         clearAbortRequest();
 158         processImageStarted(0);




 159         if (param == null)
 160             param = getDefaultWriteParam();
 161 
 162         BMPImageWriteParam bmpParam = (BMPImageWriteParam)param;
 163 
 164         // Default is using 24 bits per pixel.
 165         int bitsPerPixel = 24;
 166         boolean isPalette = false;
 167         int paletteEntries = 0;
 168         IndexColorModel icm = null;
 169 
 170         RenderedImage input = null;
 171         Raster inputRaster = null;
 172         boolean writeRaster = image.hasRaster();
 173         Rectangle sourceRegion = param.getSourceRegion();
 174         SampleModel sampleModel = null;
 175         ColorModel colorModel = null;
 176 
 177         compImageSize = 0;
 178 


 566         if (compressionType == BI_JPEG ||
 567             compressionType == BI_PNG) {
 568 
 569             // prepare embedded buffer
 570             embedded_stream = new ByteArrayOutputStream();
 571             writeEmbedded(image, bmpParam);
 572             // update the file/image Size
 573             embedded_stream.flush();
 574             imageSize = embedded_stream.size();
 575 
 576             long endPos = stream.getStreamPosition();
 577             fileSize = offset + imageSize;
 578             stream.seek(headPos);
 579             writeSize(fileSize, 2);
 580             stream.seek(headPos);
 581             writeSize(imageSize, 34);
 582             stream.seek(endPos);
 583             stream.write(embedded_stream.toByteArray());
 584             embedded_stream = null;
 585 
 586             if (abortRequested()) {
 587                 processWriteAborted();
 588             } else {
 589                 processImageComplete();
 590                 stream.flushBefore(stream.getStreamPosition());
 591             }
 592 
 593             return;
 594         }
 595 
 596         int maxBandOffset = bandOffsets[0];
 597         for (int i = 1; i < bandOffsets.length; i++)
 598             if (bandOffsets[i] > maxBandOffset)
 599                 maxBandOffset = bandOffsets[i];
 600 
 601         int[] pixel = new int[maxBandOffset + 1];
 602 
 603         int destScanlineLength = destScanlineBytes;
 604 
 605         if (noTransform && noSubband) {
 606             destScanlineLength = destScanlineBytes / (DataBuffer.getDataTypeSize(dataType)>>3);
 607         }
 608         for (int i = 0; i < h; i++) {
 609             if (abortRequested()) {
 610                 break;
 611             }
 612 
 613             int row = minY + i;
 614 
 615             if (!isTopDown)
 616                 row = minY + h - i -1;
 617 
 618             // Get the pixels
 619             Raster src = inputRaster;
 620 
 621             Rectangle srcRect =
 622                 new Rectangle(minX * scaleX + xOffset,
 623                               row * scaleY + yOffset,
 624                               (w - 1)* scaleX + 1,
 625                               1);
 626             if (!writeRaster)
 627                 src = input.getData(srcRect);
 628 
 629             if (noTransform && noSubband) {
 630                 SampleModel sm = src.getSampleModel();
 631                 int pos = 0;


 707                 src.getPixels(srcRect.x, srcRect.y,
 708                               srcRect.width, srcRect.height, pixels);
 709 
 710                 if (scaleX != 1 || maxBandOffset != numBands - 1) {
 711                     for (int j = 0, k = 0, n=0; j < w;
 712                          j++, k += scaleX * numBands, n += numBands)
 713                     {
 714                         System.arraycopy(pixels, k, pixel, 0, pixel.length);
 715 
 716                         for (int m = 0; m < numBands; m++) {
 717                             // pixel data is provided here in RGB order
 718                             pixels[n + m] = pixel[sourceBands[m]];
 719                         }
 720                     }
 721                 }
 722                 writePixels(0, scanlineBytes, bitsPerPixel, pixels,
 723                             padding, numBands, icm);
 724             }
 725 
 726             processImageProgress(100.0f * (((float)i) / ((float)h)));



 727         }
 728 
 729         if (compressionType == BI_RLE4 ||
 730             compressionType == BI_RLE8) {
 731             // Write the RLE EOF marker and
 732             stream.writeByte(0);
 733             stream.writeByte(1);
 734             incCompImageSize(2);
 735             // update the file/image Size
 736             imageSize = compImageSize;
 737             fileSize = compImageSize + offset;
 738             long endPos = stream.getStreamPosition();
 739             stream.seek(headPos);
 740             writeSize(fileSize, 2);
 741             stream.seek(headPos);
 742             writeSize(imageSize, 34);
 743             stream.seek(endPos);
 744         }
 745 
 746         if (abortRequested()) {


   1 /*
   2  * Copyright (c) 2003, 2016, 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


 139     }
 140 
 141     public boolean canWriteRasters() {
 142         return true;
 143     }
 144 
 145     public void write(IIOMetadata streamMetadata,
 146                       IIOImage image,
 147                       ImageWriteParam param) throws IOException {
 148 
 149         if (stream == null) {
 150             throw new IllegalStateException(I18N.getString("BMPImageWriter7"));
 151         }
 152 
 153         if (image == null) {
 154             throw new IllegalArgumentException(I18N.getString("BMPImageWriter8"));
 155         }
 156 
 157         clearAbortRequest();
 158         processImageStarted(0);
 159         if (abortRequested()) {
 160             processWriteAborted();
 161             return;
 162         }
 163         if (param == null)
 164             param = getDefaultWriteParam();
 165 
 166         BMPImageWriteParam bmpParam = (BMPImageWriteParam)param;
 167 
 168         // Default is using 24 bits per pixel.
 169         int bitsPerPixel = 24;
 170         boolean isPalette = false;
 171         int paletteEntries = 0;
 172         IndexColorModel icm = null;
 173 
 174         RenderedImage input = null;
 175         Raster inputRaster = null;
 176         boolean writeRaster = image.hasRaster();
 177         Rectangle sourceRegion = param.getSourceRegion();
 178         SampleModel sampleModel = null;
 179         ColorModel colorModel = null;
 180 
 181         compImageSize = 0;
 182 


 570         if (compressionType == BI_JPEG ||
 571             compressionType == BI_PNG) {
 572 
 573             // prepare embedded buffer
 574             embedded_stream = new ByteArrayOutputStream();
 575             writeEmbedded(image, bmpParam);
 576             // update the file/image Size
 577             embedded_stream.flush();
 578             imageSize = embedded_stream.size();
 579 
 580             long endPos = stream.getStreamPosition();
 581             fileSize = offset + imageSize;
 582             stream.seek(headPos);
 583             writeSize(fileSize, 2);
 584             stream.seek(headPos);
 585             writeSize(imageSize, 34);
 586             stream.seek(endPos);
 587             stream.write(embedded_stream.toByteArray());
 588             embedded_stream = null;
 589 



 590             processImageComplete();
 591             stream.flushBefore(stream.getStreamPosition());

 592 
 593             return;
 594         }
 595 
 596         int maxBandOffset = bandOffsets[0];
 597         for (int i = 1; i < bandOffsets.length; i++)
 598             if (bandOffsets[i] > maxBandOffset)
 599                 maxBandOffset = bandOffsets[i];
 600 
 601         int[] pixel = new int[maxBandOffset + 1];
 602 
 603         int destScanlineLength = destScanlineBytes;
 604 
 605         if (noTransform && noSubband) {
 606             destScanlineLength = destScanlineBytes / (DataBuffer.getDataTypeSize(dataType)>>3);
 607         }
 608         for (int i = 0; i < h; i++) {



 609 
 610             int row = minY + i;
 611 
 612             if (!isTopDown)
 613                 row = minY + h - i -1;
 614 
 615             // Get the pixels
 616             Raster src = inputRaster;
 617 
 618             Rectangle srcRect =
 619                 new Rectangle(minX * scaleX + xOffset,
 620                               row * scaleY + yOffset,
 621                               (w - 1)* scaleX + 1,
 622                               1);
 623             if (!writeRaster)
 624                 src = input.getData(srcRect);
 625 
 626             if (noTransform && noSubband) {
 627                 SampleModel sm = src.getSampleModel();
 628                 int pos = 0;


 704                 src.getPixels(srcRect.x, srcRect.y,
 705                               srcRect.width, srcRect.height, pixels);
 706 
 707                 if (scaleX != 1 || maxBandOffset != numBands - 1) {
 708                     for (int j = 0, k = 0, n=0; j < w;
 709                          j++, k += scaleX * numBands, n += numBands)
 710                     {
 711                         System.arraycopy(pixels, k, pixel, 0, pixel.length);
 712 
 713                         for (int m = 0; m < numBands; m++) {
 714                             // pixel data is provided here in RGB order
 715                             pixels[n + m] = pixel[sourceBands[m]];
 716                         }
 717                     }
 718                 }
 719                 writePixels(0, scanlineBytes, bitsPerPixel, pixels,
 720                             padding, numBands, icm);
 721             }
 722 
 723             processImageProgress(100.0f * (((float)i) / ((float)h)));
 724             if (abortRequested()) {
 725                 break;
 726             }
 727         }
 728 
 729         if (compressionType == BI_RLE4 ||
 730             compressionType == BI_RLE8) {
 731             // Write the RLE EOF marker and
 732             stream.writeByte(0);
 733             stream.writeByte(1);
 734             incCompImageSize(2);
 735             // update the file/image Size
 736             imageSize = compImageSize;
 737             fileSize = compImageSize + offset;
 738             long endPos = stream.getStreamPosition();
 739             stream.seek(headPos);
 740             writeSize(fileSize, 2);
 741             stream.seek(headPos);
 742             writeSize(imageSize, 34);
 743             stream.seek(endPos);
 744         }
 745 
 746         if (abortRequested()) {


< prev index next >