Class: Cryptopunks::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptopunks.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Image

Returns a new instance of Image.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cryptopunks.rb', line 35

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

#zoomObject

Returns the value of attribute zoom.



23
24
25
# File 'lib/cryptopunks.rb', line 23

def zoom
  @zoom
end

Class Method Details

.read(path = './punks.png') ⇒ Object



17
18
19
20
# File 'lib/cryptopunks.rb', line 17

def self.read( path='./punks.png' )
  data = File.open( path, 'rb' ) { |f| f.read }
  new( data )
end

Instance Method Details

#[](index) ⇒ Object



66
67
68
# File 'lib/cryptopunks.rb', line 66

def []( index )
  @zoom == 1 ? crop( index ) : scale( index, @zoom )
end

#crop(index) ⇒ Object



71
72
73
74
# File 'lib/cryptopunks.rb', line 71

def crop( index  )
  y, x = index.divmod( PUNK_ROWS )
  @punks.crop( x*PUNK_WIDTH, y*PUNK_HEIGHT, PUNK_WIDTH, PUNK_HEIGHT )
end

#hexdigestObject



56
# File 'lib/cryptopunks.rb', line 56

def hexdigest()  @hexdigest end

#scale(index, zoom) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cryptopunks.rb', line 77

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

#sizeObject



64
# File 'lib/cryptopunks.rb', line 64

def size() PUNK_COUNT; end

#verify?Boolean Also known as: genuine?, original?

Returns:

  • (Boolean)


58
# File 'lib/cryptopunks.rb', line 58

def verify?()  @hexdigest == PUNK_HASH; end