< prev index next >

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

Print this page




  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 final 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)
 100         throws IOException {
 101         switch(key) {
 102             case bKGDChunk:
 103                 Color c = null;
 104                 switch(colorType) {
 105                     case COLOR:
 106                     case COLOR|ALPHA:
 107                         pngassert(len==6);
 108                         c = new Color(buf[st]&0xff,buf[st+2]&0xff,buf[st+4]&0xff);
 109                         break;
 110                     case COLOR|PALETTE:
 111                     case COLOR|PALETTE|ALPHA:
 112                         pngassert(len==1);


 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 final 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 final int getShort(int pos) {
 702         return (short)(((inbuf[pos  ]&0xFF)<<8)
 703                      | ((inbuf[pos+1]&0xFF)   ));
 704     }
 705     private final int getByte(int pos) {
 706         return inbuf[pos]&0xFF;
 707     }
 708     private final boolean getChunk() throws IOException {
 709         chunkLength = 0;
 710         if (!need(8)) return false;
 711         chunkLength = getInt(pos);
 712         chunkKey = getInt(pos+4);
 713         if(chunkLength<0) throw new PNGException("bogus length: "+chunkLength);
 714         if (!need(chunkLength+12)) return false;
 715         chunkCRC = getInt(pos+8+chunkLength);
 716         chunkStart = pos+8;
 717         int calcCRC = crc(inbuf,pos+4,chunkLength+4);
 718         if(chunkCRC!=calcCRC && checkCRC) throw new PNGException("crc corruption");
 719         pos+=chunkLength+12;
 720         return true;
 721     }
 722     private void readAll() throws IOException {
 723         while(getChunk()) handleChunk(chunkKey,inbuf,chunkStart,chunkLength);
 724     }
 725     boolean getData() throws IOException {
 726         while(chunkLength==0 && getChunk())
 727             if(handleChunk(chunkKey,inbuf,chunkStart,chunkLength))
 728                 chunkLength = 0;




  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)
 100         throws IOException {
 101         switch(key) {
 102             case bKGDChunk:
 103                 Color c = null;
 104                 switch(colorType) {
 105                     case COLOR:
 106                     case COLOR|ALPHA:
 107                         pngassert(len==6);
 108                         c = new Color(buf[st]&0xff,buf[st+2]&0xff,buf[st+4]&0xff);
 109                         break;
 110                     case COLOR|PALETTE:
 111                     case COLOR|PALETTE|ALPHA:
 112                         pngassert(len==1);


 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     }
 708     private boolean getChunk() throws IOException {
 709         chunkLength = 0;
 710         if (!need(8)) return false;
 711         chunkLength = getInt(pos);
 712         chunkKey = getInt(pos+4);
 713         if(chunkLength<0) throw new PNGException("bogus length: "+chunkLength);
 714         if (!need(chunkLength+12)) return false;
 715         chunkCRC = getInt(pos+8+chunkLength);
 716         chunkStart = pos+8;
 717         int calcCRC = crc(inbuf,pos+4,chunkLength+4);
 718         if(chunkCRC!=calcCRC && checkCRC) throw new PNGException("crc corruption");
 719         pos+=chunkLength+12;
 720         return true;
 721     }
 722     private void readAll() throws IOException {
 723         while(getChunk()) handleChunk(chunkKey,inbuf,chunkStart,chunkLength);
 724     }
 725     boolean getData() throws IOException {
 726         while(chunkLength==0 && getChunk())
 727             if(handleChunk(chunkKey,inbuf,chunkStart,chunkLength))
 728                 chunkLength = 0;


< prev index next >