Class: ZIMG::Image

Inherits:
Object
  • Object
show all
Includes:
DeepCopyable
Defined in:
lib/zimg/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DeepCopyable

#deep_copy

Constructor Details

#initialize(x, h = {}) ⇒ Image

possible input params:

IO      of opened image file
String  with image file already readed
Hash    of image parameters to create new blank image


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zimg/image.rb', line 16

def initialize(x, h = {})
  @chunks = []
  @color_class = Color
  @extradata = []
  @verbose =
    case h[:verbose]
    when true then  1
    when false then 0
    else h[:verbose].to_i
    end

  case x
  when IO
    _from_io x
  when String
    _from_io StringIO.new(x)
  when Hash
    # XXX currently implicitly creates PNG
    extend PNG
    @format = :png
    _from_hash x
  else
    raise NotSupported, "unsupported input data type #{x.class}"
  end
end

Instance Attribute Details

#bppObject (readonly)

Returns the value of attribute bpp.



9
10
11
# File 'lib/zimg/image.rb', line 9

def bpp
  @bpp
end

#chunksObject (readonly)

Returns the value of attribute chunks.



9
10
11
# File 'lib/zimg/image.rb', line 9

def chunks
  @chunks
end

#color_classObject (readonly)

Returns the value of attribute color_class.



9
10
11
# File 'lib/zimg/image.rb', line 9

def color_class
  @color_class
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/zimg/image.rb', line 9

def format
  @format
end

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/zimg/image.rb', line 9

def height
  @height
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/zimg/image.rb', line 9

def 
  @metadata
end

#paletteObject (readonly)

Returns the value of attribute palette.



9
10
11
# File 'lib/zimg/image.rb', line 9

def palette
  @palette
end

#scanlinesObject

Returns the value of attribute scanlines.



10
11
12
# File 'lib/zimg/image.rb', line 10

def scanlines
  @scanlines
end

#verboseObject (readonly)

Returns the value of attribute verbose.



9
10
11
# File 'lib/zimg/image.rb', line 9

def verbose
  @verbose
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/zimg/image.rb', line 9

def width
  @width
end

Instance Method Details

#==(other) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/zimg/image.rb', line 86

def ==(other)
  return false unless other.is_a?(Image)
  return false if width  != other.width
  return false if height != other.height

  each_pixel do |c, x, y|
    return false if c != other[x, y]
  end
  true
end

#[](x, y) ⇒ Object



108
109
110
# File 'lib/zimg/image.rb', line 108

def [](x, y)
  scanlines[y][x]
end

#_from_io(io) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zimg/image.rb', line 42

def _from_io(io)
  io.binmode

  fmt = nil
  hdr = String.new
  ZIMG.magics.keys.sort_by(&:size).each do |magic|
    hdr << io.read(magic.size - hdr.size) if magic.size > hdr.size
    if hdr == magic
      fmt = ZIMG.magics[magic]
      break
    end
  end

  raise NotSupported, "Unsupported header #{hdr.inspect} in #{io.inspect}" unless fmt

  @format = fmt
  m = ZIMG.const_get(fmt.to_s.upcase)
  extend(m)
  send("read_#{fmt}", io)

  return if io.eof?

  offset     = io.tell
  @extradata << io.read
  return unless @verbose >= 1

  warn "[?] #{@extradata.last.size} bytes of extra data after image end (IEND), offset = 0x#{offset.to_s(16)}".red
end

#alpha_used?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/zimg/image.rb', line 137

def alpha_used?
  false
end

#each_pixel(&block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/zimg/image.rb', line 97

def each_pixel(&block)
  e = Enumerator.new do |ee|
    height.times do |y|
      width.times do |x|
        ee.yield(self[x, y], x, y)
      end
    end
  end
  block_given? ? e.each(&block) : e
end

#imagedata_sizeObject



78
79
80
81
82
83
84
# File 'lib/zimg/image.rb', line 78

def imagedata_size
  if new_image?
    @scanlines&.map(&:size)&.inject(&:+)
  else
    imagedata&.size
  end
end

#inspectObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zimg/image.rb', line 112

def inspect
  info =
    %w[format width height bpp colorspace chunks scanlines].map do |k|
      next unless respond_to?(k)

      v = case (v = send(k))
          when Array
            v.empty? ? "[]" : "[#{v.size} entries]"
          when String
            v.size > 40 ? "[#{v.bytesize} bytes]" : v.inspect
          else v.inspect
          end
      "#{k}=#{v}"
    end.compact.join(", ")
  Kernel.format("#<ZIMG::Image %s>", info)
end

#new_image?Boolean Also known as: new?

flag that image is just created, and NOT loaded from file as in Rails’ ActiveRecord::Base#new_record?

Returns:

  • (Boolean)


73
74
75
# File 'lib/zimg/image.rb', line 73

def new_image?
  @new_image
end

#pixelsObject



133
134
135
# File 'lib/zimg/image.rb', line 133

def pixels
  Pixels.new(self)
end

#to_ascii(*args) ⇒ Object



129
130
131
# File 'lib/zimg/image.rb', line 129

def to_ascii *args
  scanlines&.map { |l| l.to_ascii(*args) }&.join("\n")
end