Created
          May 2, 2011 12:25 
        
      - 
      
- 
        Save jeffkreeftmeijer/951528 to your computer and use it in GitHub Desktop. 
    ChunkyPNG nearest-neighbor resize
  
        
        
        
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'chunky_png' | |
| image = ChunkyPNG::Image.from_file('bassie.png') | |
| [2,0.5].each do |scale| | |
| resized_image = ChunkyPNG::Image.new((image.width * scale).round, (image.height * scale).round) | |
| resized_image.pixels.map!.with_index do |pixel, index| | |
| x, y = index % resized_image.width, (index / resized_image.width).floor | |
| image[x / scale, y / scale] | |
| end | |
| resized_image.save("bassie_resized_#{scale}.png") | |
| end | 
I already implemented nearest neighbor resampling in ChunkyPNG: https://github.com/wvanbergen/chunky_png/blob/master/lib/chunky_png/canvas/resampling.rb
My code is significantly more complex then yours though, so patch so simplify it would be appreciated. Are you tackling bicubic resampling next? ;)
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            


Please let me know if you know any way of improving this, was having some trouble converting the pixels' indexes to x/y coordinates. :)