| Class | Magick::Image::View::Rows |
| In: |
lib/RMagick.rb
|
| Parent: | Object |
# File lib/RMagick.rb, line 844
844: def initialize(view, width, height, rows)
845: @view = view
846: @width = width
847: @height = height
848: @rows = rows
849: end
# File lib/RMagick.rb, line 851
851: def [](*args)
852: cols(args)
853:
854: # Both View::Pixels and Magick::Pixel implement Observable
855: if @unique
856: pixels = @view[@rows[0]*@width + @cols[0]]
857: pixels.add_observer(self)
858: else
859: pixels = View::Pixels.new
860: each do |x|
861: p = @view[x]
862: p.add_observer(self)
863: pixels << p
864: end
865: end
866: pixels
867: end
# File lib/RMagick.rb, line 869
869: def []=(*args)
870: rv = args.delete_at(-1) # get rvalue
871: if ! rv.is_a?(Pixel) # must be a Pixel or a color name
872: begin
873: rv = Pixel.from_color(rv)
874: rescue TypeError
875: raise TypeError, "cannot convert #{rv.class} into Pixel"
876: end
877: end
878: cols(args)
879: each { |x| @view[x] = rv.dup }
880: changed
881: notify_observers(self)
882: nil
883: end
A pixel has been modified. Tell the view.
# File lib/RMagick.rb, line 886
886: def update(pixel)
887: changed
888: notify_observers(self)
889: pixel.delete_observer(self) # Don't need to hear again.
890: nil
891: end
# File lib/RMagick.rb, line 895
895: def cols(*args)
896: @cols = args[0] # remove the outermost array
897: @unique = false
898:
899: # Convert @rows to an Enumerable object
900: case @rows.length
901: when 0 # Create a Range for all the rows
902: @rows = Range.new(0, @height, true)
903: when 1 # Range, Array, or a single integer
904: # if the single element is already an Enumerable
905: # object, get it.
906: if @rows.first.respond_to? :each
907: @rows = @rows.first
908: else
909: @rows = Integer(@rows.first)
910: if @rows < 0
911: @rows += @height
912: end
913: if @rows < 0 || @rows > @height-1
914: raise IndexError, "index [#{@rows}] out of range"
915: end
916: # Convert back to an array
917: @rows = Array.new(1, @rows)
918: @unique = true
919: end
920: when 2
921: # A pair of integers representing the starting column and the number of columns
922: start = Integer(@rows[0])
923: length = Integer(@rows[1])
924:
925: # Negative start -> start from last row
926: if start < 0
927: start += @height
928: end
929:
930: if start > @height || start < 0 || length < 0
931: raise IndexError, "index [#{@rows.first}] out of range"
932: else
933: if start + length > @height
934: length = @height - length
935: length = [length, 0].max
936: end
937: end
938: # Create a Range for the specified set of rows
939: @rows = Range.new(start, start+length, true)
940: end
941:
942: case @cols.length
943: when 0 # all rows
944: @cols = Range.new(0, @width, true) # convert to range
945: @unique = false
946: when 1 # Range, Array, or a single integer
947: # if the single element is already an Enumerable
948: # object, get it.
949: if @cols.first.respond_to? :each
950: @cols = @cols.first
951: @unique = false
952: else
953: @cols = Integer(@cols.first)
954: if @cols < 0
955: @cols += @width
956: end
957: if @cols < 0 || @cols > @width-1
958: raise IndexError, "index [#{@cols}] out of range"
959: end
960: # Convert back to array
961: @cols = Array.new(1, @cols)
962: @unique &&= true
963: end
964: when 2
965: # A pair of integers representing the starting column and the number of columns
966: start = Integer(@cols[0])
967: length = Integer(@cols[1])
968:
969: # Negative start -> start from last row
970: if start < 0
971: start += @width
972: end
973:
974: if start > @width || start < 0 || length < 0
975: ; #nop
976: else
977: if start + length > @width
978: length = @width - length
979: length = [length, 0].max
980: end
981: end
982: # Create a Range for the specified set of columns
983: @cols = Range.new(start, start+length, true)
984: @unique = false
985: end
986:
987: end
iterator called from subscript methods
# File lib/RMagick.rb, line 990
990: def each
991: maxrows = @height - 1
992: maxcols = @width - 1
993:
994: @rows.each do |j|
995: if j > maxrows
996: raise IndexError, "index [#{j}] out of range"
997: end
998: @cols.each do |i|
999: if i > maxcols
1000: raise IndexError, "index [#{i}] out of range"
1001: end
1002: yield j*@width + i
1003: end
1004: end
1005: nil # useless return value
1006: end