Advertisement
Guest User

Conversion Tools

a guest
Jan 17th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.54 KB | None | 0 0
  1. #===============================================================================
  2. #  Pokemon Sprite GIF to PNG converter
  3. #    by Luka S.J.
  4. #    tweaked by Mej
  5. #
  6. #  Create a directory called "Convert" in your game's root folder
  7. #  and place all the GIFs you want to convert in it.
  8. #  Newly formatted PNGs will retain old GIF names.
  9. #===============================================================================
  10. class GifExtra
  11.   def initialize(file=nil,viewport=nil)
  12.     @file=file
  13.     @sprite=SpriteWrapper.new(viewport)
  14.     if file
  15.       @bitmap=AnimatedBitmap.new(file)
  16.       @sprite.bitmap=@bitmap.bitmap
  17.     end
  18.   end
  19.   def totalFrames; @bitmap.totalFrames; end
  20.   def bitmap; @bitmap.bitmap; end
  21.   def visible=(val); @sprite.visible=val; end
  22.   def dispose; @sprite.dispose; end
  23.   def update
  24.     if @bitmap
  25.       @bitmap.update
  26.       @sprite.update
  27.       if self.bitmap!=@bitmap.bitmap
  28.         oldrc=self.src_rect
  29.         self.bitmap=@bitmap.bitmap
  30.         self.src_rect=oldrc
  31.       end
  32.     end
  33.   end
  34. end
  35.  
  36. def readDirectoryFiles(directory,formats)
  37.   files=[]
  38.   Dir.chdir(directory){
  39.     for i in 0...formats.length
  40.       Dir.glob(formats[i]){|f| files.push(f) }
  41.     end
  42.   }
  43.   return files
  44. end
  45.  
  46. def resizePngs
  47.   destination="./Convert/"
  48.   search_for=["*.png"]
  49.  
  50.   @files=readDirectoryFiles(destination,search_for)
  51.   @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
  52.   @viewport.z=999999
  53.  
  54.   @bar=Sprite.new(@viewport)
  55.   @bar.bitmap=Bitmap.new(Graphics.width,34)
  56.   pbSetSystemFont(@bar.bitmap)
  57.  
  58.   for i in 0...@files.length
  59.     @files[i]=@files[i].gsub(/.png/) {""}
  60.   end
  61.  
  62.   return false if !Kernel.pbConfirmMessage(_INTL("There is a total of #{@files.length} PNG(s) available for conversion. Would you like to begin the process?"))
  63.   for i in 0...@files.length
  64.     file=@files[i]
  65.    
  66.     width=((i*1.000)/@files.length)*Graphics.width
  67.     @bar.bitmap.clear
  68.     @bar.bitmap.fill_rect(0,0,Graphics.width,34,Color.new(255,255,255))
  69.     @bar.bitmap.fill_rect(0,0,Graphics.width,32,Color.new(0,0,0))
  70.     @bar.bitmap.fill_rect(0,0,width,32,Color.new(25*4,90*2,25*4))
  71.     text=[["#{i}/#{@files.length}",Graphics.width/2,2,2,Color.new(255,255,255),nil]]
  72.     pbDrawTextPositions(@bar.bitmap,text)
  73.    
  74.     next if RTP.exists?("#{destination}New/#{file}.png")
  75.    
  76.     sprite=pbBitmap("#{destination}#{file}.png")
  77.     width=sprite.width
  78.     height=sprite.height
  79.      
  80.     bitmap=Bitmap.new(width/2,height/2)
  81.     bitmap.stretch_blt(Rect.new(0,0,width/2,height/2),sprite,Rect.new(0,0,width,height))
  82.     bitmap.saveToPng("#{destination}New/#{file}.png")
  83.     sprite.dispose
  84.     pbWait(1)
  85.     RPG::Cache.clear
  86.   end
  87.   @bar.dispose
  88.   @viewport.dispose
  89.   Kernel.pbMessage(_INTL("Done!"))
  90. end  
  91.  
  92. def decompressGifs
  93.   destination="./Convert/"
  94.   search_for=["*.gif"]
  95.  
  96.   @files=readDirectoryFiles(destination,search_for)
  97.   @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
  98.   @viewport.z=999999
  99.  
  100.   @bar=Sprite.new(@viewport)
  101.   @bar.bitmap=Bitmap.new(Graphics.width,34)
  102.   pbSetSystemFont(@bar.bitmap)
  103.  
  104.   for i in 0...@files.length
  105.     @files[i]=@files[i].gsub(/.gif/) {""}
  106.   end
  107.  
  108.   return false if !Kernel.pbConfirmMessage(_INTL("There is a total of #{@files.length} GIF(s) available for conversion. Would you like to begin the process?"))
  109.    
  110.   for i in 0...@files.length
  111.     file=@files[i]
  112.    
  113.     width=((i*1.000)/@files.length)*Graphics.width
  114.     @bar.bitmap.clear
  115.     @bar.bitmap.fill_rect(0,0,Graphics.width,34,Color.new(255,255,255))
  116.     @bar.bitmap.fill_rect(0,0,Graphics.width,32,Color.new(0,0,0))
  117.     @bar.bitmap.fill_rect(0,0,width,32,Color.new(25*4,90*2,25*4))
  118.     text=[["#{i}/#{@files.length}",Graphics.width/2,2,2,Color.new(255,255,255),nil]]
  119.     pbDrawTextPositions(@bar.bitmap,text)
  120.    
  121.     next if RTP.exists?("#{destination}#{file}.png")
  122.    
  123.     sprite=GifExtra.new("#{destination}#{file}.gif")
  124.     frames=sprite.totalFrames
  125.     width=sprite.bitmap.width
  126.     height=sprite.bitmap.height
  127.     sprite.visible=false  
  128.    
  129.     if width<height
  130.       size=height
  131.     elsif height<width
  132.       size=width
  133.     else
  134.       size=width
  135.     end
  136.    
  137.     bitmap=Bitmap.new(size*frames,size)
  138.     x=0
  139.     ox=((size-width)/2)
  140.     oy=((size-height)/2)
  141.     rect=Rect.new(0,0,width,height)
  142.     frames.times do
  143.       bitmap.blt((x*size)+ox,0+oy,sprite.bitmap,rect)
  144.       sprite.update
  145.       x+=1
  146.     end
  147.     bitmap.saveToPng("#{destination}#{file}.png")
  148.     sprite.dispose
  149.     pbWait(1)
  150.     RPG::Cache.clear
  151.   end
  152.   @bar.dispose
  153.   @viewport.dispose
  154.   Kernel.pbMessage(_INTL("Done!"))
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement