Class: Cryptopunks::Image
- Inherits:
-
Object
- Object
- Cryptopunks::Image
- Defined in:
- lib/cryptopunks/image.rb
Constant Summary collapse
- PUNK_ROWS =
100- PUNK_COLS =
100- PUNK_COUNT =
10_000 = 100x100 (24000x24000 pixel)
PUNK_ROWS * PUNK_COLS
- PUNK_HEIGHT =
24- PUNK_WIDTH =
24- PUNK_HASH =
'ac39af4793119ee46bbff351d8cb6b5f23da60222126add4268e261199a2921b'
Instance Attribute Summary collapse
-
#zoom ⇒ Object
Returns the value of attribute zoom.
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #crop(index) ⇒ Object
- #hexdigest ⇒ Object
-
#initialize(data) ⇒ Image
constructor
A new instance of Image.
- #scale(index, zoom) ⇒ Object
- #size ⇒ Object
- #verify? ⇒ Boolean (also: #genuine?, #original?)
Constructor Details
#initialize(data) ⇒ Image
Returns a new instance of Image.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cryptopunks/image.rb', line 21 def initialize( data ) @punks = ChunkyPNG::Image.from_blob( data ) puts " #{@punks.height}x#{@punks.width} (height x width)" ## check sha256 checksum @hexdigest = sha256( data ) if original? puts " >#{@hexdigest}< SHA256 hash matching" puts " ✓ True Official Genuine CryptoPunks™ verified" else puts " !! ERROR: >#{hexdigest}< SHA256 hash NOT matching" puts " >#{PUNK_HASH}< expected for True Official Genuine CryptoPunks™." puts "" puts " Sorry, please download the original." exit 1 end @zoom = 1 end |
Instance Attribute Details
#zoom ⇒ Object
Returns the value of attribute zoom.
9 10 11 |
# File 'lib/cryptopunks/image.rb', line 9 def zoom @zoom end |
Class Method Details
.read(path = './punks.png') ⇒ Object
3 4 5 6 |
# File 'lib/cryptopunks/image.rb', line 3 def self.read( path='./punks.png' ) data = File.open( path, 'rb' ) { |f| f.read } new( data ) end |
Instance Method Details
#[](index) ⇒ Object
52 53 54 |
# File 'lib/cryptopunks/image.rb', line 52 def []( index ) @zoom == 1 ? crop( index ) : scale( index, @zoom ) end |
#crop(index) ⇒ Object
57 58 59 60 |
# File 'lib/cryptopunks/image.rb', line 57 def crop( index ) y, x = index.divmod( PUNK_ROWS ) @punks.crop( x*PUNK_WIDTH, y*PUNK_HEIGHT, PUNK_WIDTH, PUNK_HEIGHT ) end |
#hexdigest ⇒ Object
42 |
# File 'lib/cryptopunks/image.rb', line 42 def hexdigest() @hexdigest end |
#scale(index, zoom) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cryptopunks/image.rb', line 63 def scale( index, zoom ) punk = ChunkyPNG::Image.new( PUNK_WIDTH*zoom, PUNK_HEIGHT*zoom, ChunkyPNG::Color::WHITE ) ## (x,y) offset in big all-in-one punks image y, x = index.divmod( PUNK_ROWS ) ## copy all 24x24 pixels PUNK_WIDTH.times do |i| PUNK_HEIGHT.times do |j| pixel = @punks[i+x*PUNK_WIDTH, j+y*PUNK_HEIGHT] zoom.times do |n| zoom.times do |m| punk[n+zoom*i,m+zoom*j] = pixel end end end end punk end |
#size ⇒ Object
50 |
# File 'lib/cryptopunks/image.rb', line 50 def size() PUNK_COUNT; end |
#verify? ⇒ Boolean Also known as: genuine?, original?
44 |
# File 'lib/cryptopunks/image.rb', line 44 def verify?() @hexdigest == PUNK_HASH; end |