|
| 1 | +# Example for dithering an image to get a retro look. |
| 2 | +# |
| 3 | +# Use the mouse wheel to change the dithering algorithm |
| 4 | +# and press spacebar to show it in gray scale for the perfect |
| 5 | +# retro look. |
| 6 | +# |
| 7 | +# Author: Nick 'Milchreis' Müller |
| 8 | +load_library :ImageProcessing |
| 9 | +java_import 'milchreis.imageprocessing.Dithering' |
| 10 | +java_import 'milchreis.imageprocessing.Grayscale' |
| 11 | + |
| 12 | +attr_reader :my_image, :processed, :label, :index |
| 13 | + |
| 14 | +def settings |
| 15 | + size(550, 550) |
| 16 | +end |
| 17 | + |
| 18 | +def setup |
| 19 | + sketch_title 'Dithering' |
| 20 | + @index = 0 |
| 21 | + @my_image = load_image(data_path('example.jpg')) |
| 22 | +end |
| 23 | + |
| 24 | +def draw |
| 25 | + @processed = my_image |
| 26 | + @label = '' |
| 27 | + if key_pressed? && key == ' ' |
| 28 | + @processed = Grayscale.apply(processed) |
| 29 | + end |
| 30 | + return image(my_image, 0, 0) if mouse_pressed? |
| 31 | + case index |
| 32 | + when -1 |
| 33 | + @processed = Dithering.apply(processed) |
| 34 | + @label = 'BAYER_4x4 on default' |
| 35 | + when 0 |
| 36 | + @processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_2x2) |
| 37 | + @label = 'BAYER_2x2' |
| 38 | + when 1 |
| 39 | + @processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_4x4) |
| 40 | + @label = 'BAYER_4x4' |
| 41 | + when 2 |
| 42 | + @processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_8x8) |
| 43 | + @label = 'BAYER_8x8' |
| 44 | + end |
| 45 | + image(processed, 0, 0) |
| 46 | + fill(0) |
| 47 | + text(label, width / 2 - text_width(label) / 2, 30) |
| 48 | +end |
| 49 | + |
| 50 | +def mouseWheel(event) |
| 51 | + @index += event.get_count |
| 52 | + |
| 53 | + if index >= Dithering::Algorithm.values.length |
| 54 | + @index = -1 |
| 55 | + end |
| 56 | +end |
0 commit comments