< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java

Print this page


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


 634                 cs.write(metadata.tRNS_alpha);
 635             } else if (colorType == PNGImageReader.PNG_COLOR_GRAY) {
 636                 cs.writeShort(metadata.tRNS_gray);
 637             } else if (colorType == PNGImageReader.PNG_COLOR_RGB) {
 638                 cs.writeShort(chunkRed);
 639                 cs.writeShort(chunkGreen);
 640                 cs.writeShort(chunkBlue);
 641             } else {
 642                 throw new IIOException("tRNS chunk for color type 4 or 6!");
 643             }
 644             cs.finish();
 645         }
 646     }
 647 
 648     private void write_bKGD() throws IOException {
 649         if (metadata.bKGD_present) {
 650             ChunkStream cs = new ChunkStream(PNGImageReader.bKGD_TYPE, stream);
 651             int colorType = metadata.IHDR_colorType & 0x3;
 652             int chunkType = metadata.bKGD_colorType;
 653 



 654             // Special case: image is RGB(A) and chunk is Gray
 655             // Promote chunk contents to RGB
 656             int chunkRed = metadata.bKGD_red;
 657             int chunkGreen = metadata.bKGD_red;
 658             int chunkBlue = metadata.bKGD_red;
 659             if (colorType == PNGImageReader.PNG_COLOR_RGB &&
 660                 chunkType == PNGImageReader.PNG_COLOR_GRAY) {
 661                 // Make a gray bKGD chunk look like RGB
 662                 chunkType = colorType;
 663                 chunkRed = chunkGreen = chunkBlue =
 664                     metadata.bKGD_gray;
 665             }
 666 
 667             // Ignore status of alpha in colorType
 668             if (chunkType != colorType) {
 669                 processWarningOccurred(0,
 670 "bKGD metadata has incompatible color type.\n" +
 671 "The chunk will not be written.");
 672                 return;
 673             }
 674 
 675             if (colorType == PNGImageReader.PNG_COLOR_PALETTE) {
 676                 cs.writeByte(metadata.bKGD_index);
 677             } else if (colorType == PNGImageReader.PNG_COLOR_GRAY ||
 678                        colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {


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


 634                 cs.write(metadata.tRNS_alpha);
 635             } else if (colorType == PNGImageReader.PNG_COLOR_GRAY) {
 636                 cs.writeShort(metadata.tRNS_gray);
 637             } else if (colorType == PNGImageReader.PNG_COLOR_RGB) {
 638                 cs.writeShort(chunkRed);
 639                 cs.writeShort(chunkGreen);
 640                 cs.writeShort(chunkBlue);
 641             } else {
 642                 throw new IIOException("tRNS chunk for color type 4 or 6!");
 643             }
 644             cs.finish();
 645         }
 646     }
 647 
 648     private void write_bKGD() throws IOException {
 649         if (metadata.bKGD_present) {
 650             ChunkStream cs = new ChunkStream(PNGImageReader.bKGD_TYPE, stream);
 651             int colorType = metadata.IHDR_colorType & 0x3;
 652             int chunkType = metadata.bKGD_colorType;
 653 
 654             int chunkRed = metadata.bKGD_red;
 655             int chunkGreen = metadata.bKGD_green;
 656             int chunkBlue = metadata.bKGD_blue;
 657             // Special case: image is RGB(A) and chunk is Gray
 658             // Promote chunk contents to RGB



 659             if (colorType == PNGImageReader.PNG_COLOR_RGB &&
 660                 chunkType == PNGImageReader.PNG_COLOR_GRAY) {
 661                 // Make a gray bKGD chunk look like RGB
 662                 chunkType = colorType;
 663                 chunkRed = chunkGreen = chunkBlue =
 664                     metadata.bKGD_gray;
 665             }
 666 
 667             // Ignore status of alpha in colorType
 668             if (chunkType != colorType) {
 669                 processWarningOccurred(0,
 670 "bKGD metadata has incompatible color type.\n" +
 671 "The chunk will not be written.");
 672                 return;
 673             }
 674 
 675             if (colorType == PNGImageReader.PNG_COLOR_PALETTE) {
 676                 cs.writeByte(metadata.bKGD_index);
 677             } else if (colorType == PNGImageReader.PNG_COLOR_GRAY ||
 678                        colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {


< prev index next >