Package com.sun.imageio.plugins.gif

Examples of com.sun.imageio.plugins.gif.GIFImageReader


   * @throws IOException
   */
  public static APNG convertGiftoAPNG(File readGif,I_UIupdater callback) throws IOException{
    APNG theApng=null;
    FileInputStream fis=null;
    GIFImageReader reader=null;
    try{
      fis=new FileInputStream(readGif);
     
      reader = (GIFImageReader) ImageIO.getImageReadersByFormatName("GIF").next();
          ImageInputStream iis = ImageIO.createImageInputStream(fis);
          reader.setInput(iis);
         
          int numImages=reader.getNumImages(true);
         
          boolean hasLocalColors=false;
          boolean hasTransparecy=false;
          int iTransparency=-1;
                     
          GIFStreamMetadata smd=(GIFStreamMetadata) reader.getStreamMetadata();
         
          //Analyze, which mode will be used
          GIFImageMetadata md;
          for(int i=0;i<numImages;i++){
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            if(md.transparentColorFlag){
              iTransparency=md.transparentColorIndex;
              hasTransparecy=true;
            }
            if(md.localColorTable!=null){
              hasLocalColors=true;
            }               
           
          }
          Emodes mode=Emodes.PALETTE; // for no local Colortables and no/single-transparency
                        // and NO animation
          int decision=0;
          if(hasLocalColors){
            decision+=1;
          }
          if(hasTransparecy){
            decision+=2;
          }
          if((numImages>1)){
            decision+=4;
          }
 
          switch(decision){
         
          default:
          case 0: case 2:
            mode=Emodes.PALETTE;
            break;
          case 1: case 4: case 5:
            mode=Emodes.RGB;
            break;
          case 3: case 6: case 7
            mode=Emodes.ARGB;
            break;
          }
          md=(GIFImageMetadata) reader.getImageMetadata(0);
          if(md.imageLeftPosition>0 || md.imageTopPosition>0
              || md.imageHeight!=smd.logicalScreenHeight
              || md.imageWidth!=smd.logicalScreenWidth){
          // first frame must have no offset!
            // mode set to ARGB, image will be altered before addFrame()
            mode=Emodes.ARGB;
          }
         
         
 
 
          int numLoops=ChunkacTL.INFINITE_LOOP;
          GifInfo readgifinfo=new GifInfo(readGif);
        numLoops=readgifinfo.getLoops();
 
        theApng=new APNG(smd.logicalScreenWidth,smd.logicalScreenHeight,mode, numLoops,
                       (mode==Emodes.PALETTE)? smd.globalColorTable : null, iTransparency);
         
          //now add frames (by using BufferedImage)
         
          //tell callback how many frame there are
          if(callback!=null){
            callback.updatemax(numImages);
          }
         
          // Disposal-Methods:
          // method name(gif-method-number -> apng-method-number):
          //  unknown(0->0(ignored)), no disposal(1->0)
          //  restore to background(2->1), restore to previous(3->2)
          for(int i=0;i<numImages;i++){
           
            //if stop-button is clicked
        if(Thread.currentThread().isInterrupted()){
          //close reader & stream
                reader.dispose();
                fis.close();
                throw new CancellationException("stop-button was pushed");
        }
       
            //callback current frame:
              if(callback!=null){
                callback.updatecur(i+1);
              }   
           
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            int idismet=md.disposalMethod;
            if(idismet>0 && idismet<=3){
              idismet=idismet-1;
            }
           
           
           
            if(i==0 && (md.imageLeftPosition>0 || md.imageTopPosition>0
                  || md.imageHeight!=smd.logicalScreenHeight
                  || md.imageWidth!=smd.logicalScreenWidth)){
              // first frame must have no offset!
              BufferedImage tmp=new BufferedImage(smd.logicalScreenWidth,
                        smd.logicalScreenHeight,BufferedImage.TYPE_INT_ARGB);
 
              Graphics tmpg=tmp.getGraphics();
              tmpg.setColor(new Color(0,0,0,0));
              tmpg.fillRect(0, 0, smd.logicalScreenWidth, smd.logicalScreenHeight);
              tmpg.drawImage(reader.read(i), md.imageLeftPosition, md.imageTopPosition, null);
             
              theApng.addFrame(tmp, 0, 0, md.delayTime, idismet);
            } else {
                theApng.addFrame(reader.read(i), md.imageLeftPosition, md.imageTopPosition,
                         md.delayTime, idismet)
            }
           
        
          } // add all frames
         

         
          //add tEXt-chunk here
          theApng.maChunks.add(new ChunktEXt()); //software-note
          //and all comments from the gif:
          theApng.maChunks.add(new ChunktEXt("Comment",readgifinfo.getComment()));
 
          //close reader & stream
          reader.dispose();
          fis.close();
         
          //callback completion of all frames / beginning of write-operation, which should follow
            if(callback!=null){
              callback.updatecur(numImages+1);
            }  
           
    } catch (IOException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
    } catch (RuntimeException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
View Full Code Here


   * @throws IOException
   */
  public static APNG convertGiftoAPNG(File readGif,I_UIupdater callback) throws IOException{
    APNG theApng=null;
    FileInputStream fis=null;
    GIFImageReader reader=null;
    try{
      fis=new FileInputStream(readGif);
     
      reader = (GIFImageReader) ImageIO.getImageReadersByFormatName("GIF").next();
          ImageInputStream iis = ImageIO.createImageInputStream(fis);
          reader.setInput(iis);
         
          int numImages=reader.getNumImages(true);
         
          boolean hasLocalColors=false;
          boolean hasTransparecy=false;
          int iTransparency=-1;
                     
          GIFStreamMetadata smd=(GIFStreamMetadata) reader.getStreamMetadata();
         
          //Analyze, which mode will be used
          GIFImageMetadata md;
          for(int i=0;i<numImages;i++){
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            if(md.transparentColorFlag){
              iTransparency=md.transparentColorIndex;
              hasTransparecy=true;
            }
            if(md.localColorTable!=null){
              hasLocalColors=true;
            }               
           
          }
          Emodes mode=Emodes.PALETTE; // for no local Colortables and no/single-transparency
                        // and NO animation
          int decision=0;
          if(hasLocalColors){
            decision+=1;
          }
          if(hasTransparecy){
            decision+=2;
          }
          if((numImages>1)){
            decision+=4;
          }
 
          switch(decision){
         
          default:
          case 0: case 2:
            mode=Emodes.PALETTE;
            break;
          case 1: case 4: case 5:
            mode=Emodes.RGB;
            break;
          case 3: case 6: case 7
            mode=Emodes.ARGB;
            break;
          }
          md=(GIFImageMetadata) reader.getImageMetadata(0);
          if(md.imageLeftPosition>0 || md.imageTopPosition>0
              || md.imageHeight!=smd.logicalScreenHeight
              || md.imageWidth!=smd.logicalScreenWidth){
          // first frame must have no offset!
            // mode set to ARGB, image will be altered before addFrame()
            mode=Emodes.ARGB;
          }
         
         
 
 
          int numLoops=ChunkacTL.INFINITE_LOOP;
          GifInfo readgifinfo=new GifInfo(readGif);
        numLoops=readgifinfo.getLoops();
 
        theApng=new APNG(smd.logicalScreenWidth,smd.logicalScreenHeight,mode, numLoops,
                       (mode==Emodes.PALETTE)? smd.globalColorTable : null, iTransparency);
         
          //now add frames (by using BufferedImage)
         
          //tell callback how many frame there are
          if(callback!=null){
            callback.updatemax(numImages);
          }
         
          // prepare our workers
          ExecutorService threadExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
         
          // Disposal-Methods:
          // method name(gif-method-number -> apng-method-number):
          //  unknown(0->0(ignored)), no disposal(1->0)
          //  restore to background(2->1), restore to previous(3->2)
          int seqNr = 0;
          for(int i=0;i<numImages;i++){
           
            //if stop-button is clicked
        if(Thread.currentThread().isInterrupted()){
          //close reader & stream
                reader.dispose();
                fis.close();
                throw new CancellationException("stop-button was pushed");
        }
       
            //callback current frame:
              if(callback!=null){
                callback.updatecur(i+1);
              }   
           
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            int idismet=md.disposalMethod;
            if(idismet>0 && idismet<=3){
              idismet=idismet-1;
            }
           
           
           
            if(i==0 && (md.imageLeftPosition>0 || md.imageTopPosition>0
                  || md.imageHeight!=smd.logicalScreenHeight
                  || md.imageWidth!=smd.logicalScreenWidth)){
              // first frame must have no offset!
              BufferedImage tmp=new BufferedImage(smd.logicalScreenWidth,
                        smd.logicalScreenHeight,BufferedImage.TYPE_INT_ARGB);
 
              Graphics tmpg=tmp.getGraphics();
              tmpg.setColor(new Color(0,0,0,0));
              tmpg.fillRect(0, 0, smd.logicalScreenWidth, smd.logicalScreenHeight);
              tmpg.drawImage(reader.read(i), md.imageLeftPosition, md.imageTopPosition, null);
             
              //theApng.addFrame(tmp, 0, 0, md.delayTime, idismet);
              FrameThread ft = new FrameThread(theApng,tmp, 0, 0, md.delayTime, idismet,seqNr);
              threadExecutor.execute(ft);
            } else {
              FrameThread ft = new FrameThread(theApng,reader.read(i), md.imageLeftPosition, md.imageTopPosition,md.delayTime, idismet,seqNr);
              threadExecutor.execute(ft);
            }
           
            // incr seqnr
            if (i == 0)
            {
              seqNr++;  // first idat has no seqnr
            }
            else
            {
              seqNr+=2;
            }
        
          } // add all frames
         
          // all threads should come to an end now
          threadExecutor.shutdown(); // shutdown worker threads
          while (!threadExecutor.isTerminated())
          {
            try
        {
          threadExecutor.awaitTermination(60,TimeUnit.SECONDS);
        }
        catch (InterruptedException e)
        {
          e.printStackTrace();
        }
          }
          // seperate chunk list has to be now integrated
          // this has to happen because of the parallel working threads
          theApng.integrateChunks();
         
          //add tEXt-chunk here
          theApng.maChunks.add(new ChunktEXt()); //software-note
          //and all comments from the gif:
          theApng.maChunks.add(new ChunktEXt("Comment",readgifinfo.getComment()));
 
          //close reader & stream
          reader.dispose();
          fis.close();
         
          //callback completion of all frames / beginning of write-operation, which should follow
            if(callback!=null){
              callback.updatecur(numImages+1);
            }  
           
    } catch (IOException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
    } catch (RuntimeException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
View Full Code Here

   * @throws IOException
   */
  public static APNG convertGiftoAPNG(File readGif,I_UIupdater callback) throws IOException{
    APNG theApng=null;
    FileInputStream fis=null;
    GIFImageReader reader=null;
    try{
      fis=new FileInputStream(readGif);
     
      reader = (GIFImageReader) ImageIO.getImageReadersByFormatName("GIF").next();
          ImageInputStream iis = ImageIO.createImageInputStream(fis);
          reader.setInput(iis);
         
          int numImages=reader.getNumImages(true);
         
          boolean hasLocalColors=false;
          boolean hasTransparecy=false;
          int iTransparency=-1;
                     
          GIFStreamMetadata smd=(GIFStreamMetadata) reader.getStreamMetadata();
         
          //Analyze, which mode will be used
          GIFImageMetadata md;
          for(int i=0;i<numImages;i++){
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            if(md.transparentColorFlag){
              iTransparency=md.transparentColorIndex;
              hasTransparecy=true;
            }
            if(md.localColorTable!=null){
              hasLocalColors=true;
            }               
           
          }
          Emodes mode=Emodes.PALETTE; // for no local Colortables and no/single-transparency
                        // and NO animation
          int decision=0;
          if(hasLocalColors){
            decision+=1;
          }
          if(hasTransparecy){
            decision+=2;
          }
          if((numImages>1)){
            decision+=4;
          }
 
          switch(decision){
         
          default:
          case 0: case 2:
            mode=Emodes.PALETTE;
            break;
          case 1: case 4: case 5:
            mode=Emodes.RGB;
            break;
          case 3: case 6: case 7
            mode=Emodes.ARGB;
            break;
          }
          md=(GIFImageMetadata) reader.getImageMetadata(0);
          if(md.imageLeftPosition>0 || md.imageTopPosition>0
              || md.imageHeight!=smd.logicalScreenHeight
              || md.imageWidth!=smd.logicalScreenWidth){
          // first frame must have no offset!
            // mode set to ARGB, image will be altered before addFrame()
            mode=Emodes.ARGB;
          }
         
         
 
 
          int numLoops=ChunkacTL.INFINITE_LOOP;
          GifInfo readgifinfo=new GifInfo(readGif);
        numLoops=readgifinfo.getLoops();
 
        theApng=new APNG(smd.logicalScreenWidth,smd.logicalScreenHeight,mode, numLoops,
                       (mode==Emodes.PALETTE)? smd.globalColorTable : null, iTransparency);
         
          //now add frames (by using BufferedImage)
         
          //tell callback how many frame there are
          if(callback!=null){
            callback.updatemax(numImages);
          }
         
          // Disposal-Methods:
          // method name(gif-method-number -> apng-method-number):
          //  unknown(0->0(ignored)), no disposal(1->0)
          //  restore to background(2->1), restore to previous(3->2)
          for(int i=0;i<numImages;i++){
           
            //if stop-button is clicked
        if(Thread.currentThread().isInterrupted()){
          //close reader & stream
                reader.dispose();
                fis.close();
                throw new CancellationException("stop-button was pushed");
        }
       
            //callback current frame:
              if(callback!=null){
                callback.updatecur(i+1);
              }   
           
            md=(GIFImageMetadata) reader.getImageMetadata(i);
            int idismet=md.disposalMethod;
            if(idismet>0 && idismet<=3){
              idismet=idismet-1;
            }
           
           
           
            if(i==0 && (md.imageLeftPosition>0 || md.imageTopPosition>0
                  || md.imageHeight!=smd.logicalScreenHeight
                  || md.imageWidth!=smd.logicalScreenWidth)){
              // first frame must have no offset!
              BufferedImage tmp=new BufferedImage(smd.logicalScreenWidth,
                        smd.logicalScreenHeight,BufferedImage.TYPE_INT_ARGB);
 
              Graphics tmpg=tmp.getGraphics();
              tmpg.setColor(new Color(0,0,0,0));
              tmpg.fillRect(0, 0, smd.logicalScreenWidth, smd.logicalScreenHeight);
              tmpg.drawImage(reader.read(i), md.imageLeftPosition, md.imageTopPosition, null);
             
              theApng.addFrame(tmp, 0, 0, md.delayTime, idismet);
            } else {
                theApng.addFrame(reader.read(i), md.imageLeftPosition, md.imageTopPosition,
                         md.delayTime, idismet)
            }
           
        
          } // add all frames
         

         
          //add tEXt-chunk here
          theApng.maChunks.add(new ChunktEXt()); //software-note
          //and all comments from the gif:
          theApng.maChunks.add(new ChunktEXt("Comment",readgifinfo.getComment()));
 
          //close reader & stream
          reader.dispose();
          fis.close();
         
          //callback completion of all frames / beginning of write-operation, which should follow
            if(callback!=null){
              callback.updatecur(numImages+1);
            }  
           
    } catch (IOException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
    } catch (RuntimeException ex){
      //close reader & stream
      if(reader!=null){
        reader.dispose();
      }
      if(fis!=null){
        fis.close();
      }
          throw ex;//and pass through
View Full Code Here

TOP

Related Classes of com.sun.imageio.plugins.gif.GIFImageReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.