Created
June 29, 2011 19:25
-
-
Save deathbob/1054684 to your computer and use it in GitHub Desktop.
Entry to CodeBrawl - ChunkyPNG edition
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
| #! /usr/bin/env ruby | |
| require 'rubygems' | |
| require 'chunky_png' | |
| image = ChunkyPNG::Image.from_file('input.png') | |
| h = image.dimension.height | |
| print "height #{h} \n" | |
| w = image.dimension.width | |
| print "width #{w} \n" | |
| def pixel_bomb(x, y, image) | |
| currentGreen = ChunkyPNG::Color.g(image[x,y]) | |
| color = ChunkyPNG::Color(image[x,y]) | |
| sz = case currentGreen | |
| when 0..63 | |
| rand(2) | |
| when 63..127 | |
| 1 | |
| when 127..191 | |
| 2 | |
| when 191..255 | |
| 4 | |
| end | |
| (-sz..sz).to_a.each do |xn| | |
| (-sz..sz).to_a.each do |yn| | |
| p = x + xn | |
| q = y + yn | |
| image.set_pixel_if_within_bounds(p,q, color) | |
| end | |
| end | |
| end | |
| def back_to_xy(int, width, height) | |
| x = int % width | |
| y = (int / width) | |
| [x, y] | |
| end | |
| hash = Hash.new{|h, k| h[k] = []} | |
| (0..h-1).each do |y| | |
| (0..w-1).each do |x| | |
| this_one = (y * w) + x | |
| currentGreen = ChunkyPNG::Color.g(image[x,y]) | |
| hash[currentGreen] << this_one | |
| end | |
| end | |
| shash = hash.sort | |
| shash.each do |k, v| | |
| v.each do |pixel| | |
| x, y = back_to_xy(pixel, w, h) | |
| pixel_bomb(x, y, image) | |
| end | |
| end | |
| image.save('output_' + Time.now.to_i.to_s + '.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment