| Class | Magick::Image::View::Rows |
| In: |
lib/RMagick.rb
|
| Parent: | Object |
# File lib/RMagick.rb, line 891
891: def initialize(view, width, height, rows)
892: @view = view
893: @width = width
894: @height = height
895: @rows = rows
896: end
# File lib/RMagick.rb, line 898
898: def [](*args)
899: cols(args)
900:
901: # Both View::Pixels and Magick::Pixel implement Observable
902: if @unique
903: pixels = @view[@rows[0]*@width + @cols[0]]
904: pixels.add_observer(self)
905: else
906: pixels = View::Pixels.new
907: each do |x|
908: p = @view[x]
909: p.add_observer(self)
910: pixels << p
911: end
912: end
913: pixels
914: end
# File lib/RMagick.rb, line 916
916: def []=(*args)
917: rv = args.delete_at(-1) # get rvalue
918: if ! rv.is_a?(Pixel) # must be a Pixel or a color name
919: begin
920: rv = Pixel.from_color(rv)
921: rescue TypeError
922: raise TypeError, "cannot convert #{rv.class} into Pixel"
923: end
924: end
925: cols(args)
926: each { |x| @view[x] = rv.dup }
927: changed
928: notify_observers(self)
929: nil
930: end
A pixel has been modified. Tell the view.
# File lib/RMagick.rb, line 933
933: def update(pixel)
934: changed
935: notify_observers(self)
936: pixel.delete_observer(self) # Don't need to hear again.
937: nil
938: end
# File lib/RMagick.rb, line 942
942: def cols(*args)
943: @cols = args[0] # remove the outermost array
944: @unique = false
945:
946: # Convert @rows to an Enumerable object
947: case @rows.length
948: when 0 # Create a Range for all the rows
949: @rows = Range.new(0, @height, true)
950: when 1 # Range, Array, or a single integer
951: # if the single element is already an Enumerable
952: # object, get it.
953: if @rows.first.respond_to? :each
954: @rows = @rows.first
955: else
956: @rows = Integer(@rows.first)
957: if @rows < 0
958: @rows += @height
959: end
960: if @rows < 0 || @rows > @height-1
961: raise IndexError, "index [#{@rows}] out of range"
962: end
963: # Convert back to an array
964: @rows = Array.new(1, @rows)
965: @unique = true
966: end
967: when 2
968: # A pair of integers representing the starting column and the number of columns
969: start = Integer(@rows[0])
970: length = Integer(@rows[1])
971:
972: # Negative start -> start from last row
973: if start < 0
974: start += @height
975: end
976:
977: if start > @height || start < 0 || length < 0
978: raise IndexError, "index [#{@rows.first}] out of range"
979: else
980: if start + length > @height
981: length = @height - length
982: length = [length, 0].max
983: end
984: end
985: # Create a Range for the specified set of rows
986: @rows = Range.new(start, start+length, true)
987: end
988:
989: case @cols.length
990: when 0 # all rows
991: @cols = Range.new(0, @width, true) # convert to range
992: @unique = false
993: when 1 # Range, Array, or a single integer
994: # if the single element is already an Enumerable
995: # object, get it.
996: if @cols.first.respond_to? :each
997: @cols = @cols.first
998: @unique = false
999: else
1000: @cols = Integer(@cols.first)
1001: if @cols < 0
1002: @cols += @width
1003: end
1004: if @cols < 0 || @cols > @width-1
1005: raise IndexError, "index [#{@cols}] out of range"
1006: end
1007: # Convert back to array
1008: @cols = Array.new(1, @cols)
1009: @unique &&= true
1010: end
1011: when 2
1012: # A pair of integers representing the starting column and the number of columns
1013: start = Integer(@cols[0])
1014: length = Integer(@cols[1])
1015:
1016: # Negative start -> start from last row
1017: if start < 0
1018: start += @width
1019: end
1020:
1021: if start > @width || start < 0 || length < 0
1022: ; #nop
1023: else
1024: if start + length > @width
1025: length = @width - length
1026: length = [length, 0].max
1027: end
1028: end
1029: # Create a Range for the specified set of columns
1030: @cols = Range.new(start, start+length, true)
1031: @unique = false
1032: end
1033:
1034: end
iterator called from subscript methods
# File lib/RMagick.rb, line 1037
1037: def each
1038: maxrows = @height - 1
1039: maxcols = @width - 1
1040:
1041: @rows.each do |j|
1042: if j > maxrows
1043: raise IndexError, "index [#{j}] out of range"
1044: end
1045: @cols.each do |i|
1046: if i > maxcols
1047: raise IndexError, "index [#{i}] out of range"
1048: end
1049: yield j*@width + i
1050: end
1051: end
1052: nil # useless return value
1053: end