< prev index next >

src/java.desktop/share/classes/sun/awt/image/PNGImageDecoder.java

Print this page


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


  59     private static final int tIMEChunk = 0x74494D45;
  60     private static final int tRNSChunk = 0x74524E53;
  61     private static final int zTXtChunk = 0x7A545874;
  62 
  63     private int width;
  64     private int height;
  65     private int bitDepth;
  66     private int colorType;
  67     private int compressionMethod;
  68     private int filterMethod;
  69     private int interlaceMethod;
  70     private int gamma = 100000;
  71     private java.util.Hashtable<String, Object> properties;
  72   /* this is not needed
  73     ImageConsumer target;
  74     */
  75     private ColorModel cm;
  76     private byte[] red_map, green_map, blue_map, alpha_map;
  77     private int transparentPixel = -1;
  78     private byte[]  transparentPixel_16 = null; // we need 6 bytes to store 16bpp value
  79     private static ColorModel greyModels[] = new ColorModel[4];
  80   /* this is not needed
  81      PNGImageDecoder next;
  82      */
  83 
  84     private void property(String key,Object value) {
  85         if(value==null) return;
  86         if(properties==null) properties=new java.util.Hashtable<>();
  87         properties.put(key,value);
  88     }
  89     private void property(String key,float value) {
  90         property(key, Float.valueOf(value));
  91     }
  92     private void pngassert(boolean b) throws IOException {
  93         if(!b) {
  94             PNGException e = new PNGException("Broken file");
  95             e.printStackTrace();
  96             throw e;
  97         }
  98     }
  99     protected boolean handleChunk(int key, byte[] buf, int st, int len)


 270             int combinedType = colorType|(bitDepth<<3);
 271             int bitMask = (1<<(bitDepth>=8?8:bitDepth))-1;
 272             //Figure out the color model
 273             switch(colorType) {
 274                 case COLOR|PALETTE:
 275                 case COLOR|PALETTE|ALPHA:
 276                     if(red_map==null) throw new PNGException("palette expected");
 277                     if(alpha_map==null)
 278                         cm = new IndexColorModel(bitDepth,red_map.length,
 279                             red_map,green_map,blue_map);
 280                     else
 281                         cm = new IndexColorModel(bitDepth,red_map.length,
 282                             red_map,green_map,blue_map,alpha_map);
 283                     bPixels = new byte[pixSize];
 284                     break;
 285                 case GRAY:
 286                     {   int llog = logDepth>=4 ? 3 : logDepth;
 287                         if((cm=greyModels[llog]) == null) {
 288                             int size = 1<<(1<<llog);
 289 
 290                             byte ramp[] = new byte[size];
 291                             for(int i = 0; i<size; i++) ramp[i] = (byte)(255*i/(size-1));
 292 
 293                             if (transparentPixel == -1) {
 294                                 cm = new IndexColorModel(bitDepth,ramp.length,ramp,ramp,ramp);
 295                             } else {
 296                                 cm = new IndexColorModel(bitDepth,ramp.length,ramp,ramp,ramp,
 297                                                          (transparentPixel & 0xFF));
 298                             }
 299                             greyModels[llog] = cm;
 300                         }
 301                     }
 302                     bPixels = new byte[pixSize];
 303                     break;
 304                 case COLOR:
 305                 case COLOR|ALPHA:
 306                 case GRAY|ALPHA:
 307                     cm = ColorModel.getRGBdefault();
 308                     wPixels = new int[pixSize];
 309                     break;
 310                 default:


 547 
 548     private boolean sendPixels(int x, int y, int w, int h, int[] pixels,
 549                                int offset, int pixlength) {
 550         int count = setPixels(x, y, w, h, cm,
 551                               pixels, offset, pixlength);
 552         if (count <= 0) {
 553             aborted = true;
 554         }
 555         return !aborted;
 556     }
 557     private boolean sendPixels(int x, int y, int w, int h, byte[] pixels,
 558                                int offset, int pixlength) {
 559         int count = setPixels(x, y, w, h, cm,
 560                               pixels, offset, pixlength);
 561         if (count <= 0) {
 562             aborted = true;
 563         }
 564         return !aborted;
 565     }
 566 
 567     private void filterRow(byte rowByteBuffer[], byte[] prevRow,
 568                            int rowFilter, int rowByteWidth, int bytesPerSample)
 569         throws IOException {
 570         int x = 0;
 571         switch (rowFilter) {
 572           case 0:
 573             break;
 574           case 1:
 575             for (x = bytesPerSample; x < rowByteWidth; x++)
 576                 rowByteBuffer[x] += rowByteBuffer[x - bytesPerSample];
 577             break;
 578           case 2:
 579             if (prevRow != null)
 580                 for ( ; x < rowByteWidth; x++)
 581                     rowByteBuffer[x] += prevRow[x];
 582             break;
 583           case 3:
 584             if (prevRow != null) {
 585                 for ( ; x < bytesPerSample; x++)
 586                     rowByteBuffer[x] += (0xff & prevRow[x])>>1;
 587                 for ( ; x < rowByteWidth; x++)


 667             if(pos>0 && pos<limit) {
 668                 System.arraycopy(inbuf,pos,inbuf,0,limit-pos);
 669                 limit = limit-pos;
 670                 pos = 0;
 671             } else if(pos>=limit) {
 672                 pos = 0; limit = 0;
 673             }
 674             int bsize = inbuf.length;
 675             while(limit<bsize) {
 676                 int n = underlyingInputStream.read(inbuf,limit,bsize-limit);
 677                 if(n<=0) { seenEOF=true; break; }
 678                 limit += n;
 679             }
 680         }
 681     }
 682     private boolean need(int n) throws IOException {
 683         if(limit-pos>=n) return true;
 684         fill();
 685         if(limit-pos>=n) return true;
 686         if(seenEOF) return false;
 687         byte nin[] = new byte[n+100];
 688         System.arraycopy(inbuf,pos,nin,0,limit-pos);
 689         limit = limit-pos;
 690         pos = 0;
 691         inbuf = nin;
 692         fill();
 693         return limit-pos>=n;
 694     }
 695     private int getInt(int pos) {
 696         return ((inbuf[pos  ]&0xFF)<<24)
 697              | ((inbuf[pos+1]&0xFF)<<16)
 698              | ((inbuf[pos+2]&0xFF)<< 8)
 699              | ((inbuf[pos+3]&0xFF)    );
 700     }
 701     private int getShort(int pos) {
 702         return (short)(((inbuf[pos  ]&0xFF)<<8)
 703                      | ((inbuf[pos+1]&0xFF)   ));
 704     }
 705     private int getByte(int pos) {
 706         return inbuf[pos]&0xFF;
 707     }


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


  59     private static final int tIMEChunk = 0x74494D45;
  60     private static final int tRNSChunk = 0x74524E53;
  61     private static final int zTXtChunk = 0x7A545874;
  62 
  63     private int width;
  64     private int height;
  65     private int bitDepth;
  66     private int colorType;
  67     private int compressionMethod;
  68     private int filterMethod;
  69     private int interlaceMethod;
  70     private int gamma = 100000;
  71     private java.util.Hashtable<String, Object> properties;
  72   /* this is not needed
  73     ImageConsumer target;
  74     */
  75     private ColorModel cm;
  76     private byte[] red_map, green_map, blue_map, alpha_map;
  77     private int transparentPixel = -1;
  78     private byte[]  transparentPixel_16 = null; // we need 6 bytes to store 16bpp value
  79     private static ColorModel[] greyModels = new ColorModel[4];
  80   /* this is not needed
  81      PNGImageDecoder next;
  82      */
  83 
  84     private void property(String key,Object value) {
  85         if(value==null) return;
  86         if(properties==null) properties=new java.util.Hashtable<>();
  87         properties.put(key,value);
  88     }
  89     private void property(String key,float value) {
  90         property(key, Float.valueOf(value));
  91     }
  92     private void pngassert(boolean b) throws IOException {
  93         if(!b) {
  94             PNGException e = new PNGException("Broken file");
  95             e.printStackTrace();
  96             throw e;
  97         }
  98     }
  99     protected boolean handleChunk(int key, byte[] buf, int st, int len)


 270             int combinedType = colorType|(bitDepth<<3);
 271             int bitMask = (1<<(bitDepth>=8?8:bitDepth))-1;
 272             //Figure out the color model
 273             switch(colorType) {
 274                 case COLOR|PALETTE:
 275                 case COLOR|PALETTE|ALPHA:
 276                     if(red_map==null) throw new PNGException("palette expected");
 277                     if(alpha_map==null)
 278                         cm = new IndexColorModel(bitDepth,red_map.length,
 279                             red_map,green_map,blue_map);
 280                     else
 281                         cm = new IndexColorModel(bitDepth,red_map.length,
 282                             red_map,green_map,blue_map,alpha_map);
 283                     bPixels = new byte[pixSize];
 284                     break;
 285                 case GRAY:
 286                     {   int llog = logDepth>=4 ? 3 : logDepth;
 287                         if((cm=greyModels[llog]) == null) {
 288                             int size = 1<<(1<<llog);
 289 
 290                             byte[] ramp = new byte[size];
 291                             for(int i = 0; i<size; i++) ramp[i] = (byte)(255*i/(size-1));
 292 
 293                             if (transparentPixel == -1) {
 294                                 cm = new IndexColorModel(bitDepth,ramp.length,ramp,ramp,ramp);
 295                             } else {
 296                                 cm = new IndexColorModel(bitDepth,ramp.length,ramp,ramp,ramp,
 297                                                          (transparentPixel & 0xFF));
 298                             }
 299                             greyModels[llog] = cm;
 300                         }
 301                     }
 302                     bPixels = new byte[pixSize];
 303                     break;
 304                 case COLOR:
 305                 case COLOR|ALPHA:
 306                 case GRAY|ALPHA:
 307                     cm = ColorModel.getRGBdefault();
 308                     wPixels = new int[pixSize];
 309                     break;
 310                 default:


 547 
 548     private boolean sendPixels(int x, int y, int w, int h, int[] pixels,
 549                                int offset, int pixlength) {
 550         int count = setPixels(x, y, w, h, cm,
 551                               pixels, offset, pixlength);
 552         if (count <= 0) {
 553             aborted = true;
 554         }
 555         return !aborted;
 556     }
 557     private boolean sendPixels(int x, int y, int w, int h, byte[] pixels,
 558                                int offset, int pixlength) {
 559         int count = setPixels(x, y, w, h, cm,
 560                               pixels, offset, pixlength);
 561         if (count <= 0) {
 562             aborted = true;
 563         }
 564         return !aborted;
 565     }
 566 
 567     private void filterRow(byte[] rowByteBuffer, byte[] prevRow,
 568                            int rowFilter, int rowByteWidth, int bytesPerSample)
 569         throws IOException {
 570         int x = 0;
 571         switch (rowFilter) {
 572           case 0:
 573             break;
 574           case 1:
 575             for (x = bytesPerSample; x < rowByteWidth; x++)
 576                 rowByteBuffer[x] += rowByteBuffer[x - bytesPerSample];
 577             break;
 578           case 2:
 579             if (prevRow != null)
 580                 for ( ; x < rowByteWidth; x++)
 581                     rowByteBuffer[x] += prevRow[x];
 582             break;
 583           case 3:
 584             if (prevRow != null) {
 585                 for ( ; x < bytesPerSample; x++)
 586                     rowByteBuffer[x] += (0xff & prevRow[x])>>1;
 587                 for ( ; x < rowByteWidth; x++)


 667             if(pos>0 && pos<limit) {
 668                 System.arraycopy(inbuf,pos,inbuf,0,limit-pos);
 669                 limit = limit-pos;
 670                 pos = 0;
 671             } else if(pos>=limit) {
 672                 pos = 0; limit = 0;
 673             }
 674             int bsize = inbuf.length;
 675             while(limit<bsize) {
 676                 int n = underlyingInputStream.read(inbuf,limit,bsize-limit);
 677                 if(n<=0) { seenEOF=true; break; }
 678                 limit += n;
 679             }
 680         }
 681     }
 682     private boolean need(int n) throws IOException {
 683         if(limit-pos>=n) return true;
 684         fill();
 685         if(limit-pos>=n) return true;
 686         if(seenEOF) return false;
 687         byte[] nin = new byte[n+100];
 688         System.arraycopy(inbuf,pos,nin,0,limit-pos);
 689         limit = limit-pos;
 690         pos = 0;
 691         inbuf = nin;
 692         fill();
 693         return limit-pos>=n;
 694     }
 695     private int getInt(int pos) {
 696         return ((inbuf[pos  ]&0xFF)<<24)
 697              | ((inbuf[pos+1]&0xFF)<<16)
 698              | ((inbuf[pos+2]&0xFF)<< 8)
 699              | ((inbuf[pos+3]&0xFF)    );
 700     }
 701     private int getShort(int pos) {
 702         return (short)(((inbuf[pos  ]&0xFF)<<8)
 703                      | ((inbuf[pos+1]&0xFF)   ));
 704     }
 705     private int getByte(int pos) {
 706         return inbuf[pos]&0xFF;
 707     }


< prev index next >