Class | Magick::Image::View::Rows |
In: |
lib/RMagick.rb
|
Parent: | Object |
# File lib/RMagick.rb, line 1028 1028: def initialize(view, width, height, rows) 1029: @view = view 1030: @width = width 1031: @height = height 1032: @rows = rows 1033: end
# File lib/RMagick.rb, line 1035 1035: def [](*args) 1036: cols(args) 1037: 1038: # Both View::Pixels and Magick::Pixel implement Observable 1039: if @unique 1040: pixels = @view[@rows[0]*@width + @cols[0]] 1041: pixels.add_observer(self) 1042: else 1043: pixels = View::Pixels.new 1044: each do |x| 1045: p = @view[x] 1046: p.add_observer(self) 1047: pixels << p 1048: end 1049: end 1050: pixels 1051: end
# File lib/RMagick.rb, line 1053 1053: def []=(*args) 1054: rv = args.delete_at(-1) # get rvalue 1055: if ! rv.is_a?(Pixel) # must be a Pixel or a color name 1056: begin 1057: rv = Pixel.from_color(rv) 1058: rescue TypeError 1059: Kernel.raise TypeError, "cannot convert #{rv.class} into Pixel" 1060: end 1061: end 1062: cols(args) 1063: each { |x| @view[x] = rv.dup } 1064: changed 1065: notify_observers(self) 1066: nil 1067: end
A pixel has been modified. Tell the view.
# File lib/RMagick.rb, line 1070 1070: def update(pixel) 1071: changed 1072: notify_observers(self) 1073: pixel.delete_observer(self) # Don't need to hear again. 1074: nil 1075: end
# File lib/RMagick.rb, line 1079 1079: def cols(*args) 1080: @cols = args[0] # remove the outermost array 1081: @unique = false 1082: 1083: # Convert @rows to an Enumerable object 1084: case @rows.length 1085: when 0 # Create a Range for all the rows 1086: @rows = Range.new(0, @height, true) 1087: when 1 # Range, Array, or a single integer 1088: # if the single element is already an Enumerable 1089: # object, get it. 1090: if @rows.first.respond_to? :each 1091: @rows = @rows.first 1092: else 1093: @rows = Integer(@rows.first) 1094: if @rows < 0 1095: @rows += @height 1096: end 1097: if @rows < 0 || @rows > @height-1 1098: Kernel.raise IndexError, "index [#{@rows}] out of range" 1099: end 1100: # Convert back to an array 1101: @rows = Array.new(1, @rows) 1102: @unique = true 1103: end 1104: when 2 1105: # A pair of integers representing the starting column and the number of columns 1106: start = Integer(@rows[0]) 1107: length = Integer(@rows[1]) 1108: 1109: # Negative start -> start from last row 1110: if start < 0 1111: start += @height 1112: end 1113: 1114: if start > @height || start < 0 || length < 0 1115: Kernel.raise IndexError, "index [#{@rows.first}] out of range" 1116: else 1117: if start + length > @height 1118: length = @height - length 1119: length = [length, 0].max 1120: end 1121: end 1122: # Create a Range for the specified set of rows 1123: @rows = Range.new(start, start+length, true) 1124: end 1125: 1126: case @cols.length 1127: when 0 # all rows 1128: @cols = Range.new(0, @width, true) # convert to range 1129: @unique = false 1130: when 1 # Range, Array, or a single integer 1131: # if the single element is already an Enumerable 1132: # object, get it. 1133: if @cols.first.respond_to? :each 1134: @cols = @cols.first 1135: @unique = false 1136: else 1137: @cols = Integer(@cols.first) 1138: if @cols < 0 1139: @cols += @width 1140: end 1141: if @cols < 0 || @cols > @width-1 1142: Kernel.raise IndexError, "index [#{@cols}] out of range" 1143: end 1144: # Convert back to array 1145: @cols = Array.new(1, @cols) 1146: @unique &&= true 1147: end 1148: when 2 1149: # A pair of integers representing the starting column and the number of columns 1150: start = Integer(@cols[0]) 1151: length = Integer(@cols[1]) 1152: 1153: # Negative start -> start from last row 1154: if start < 0 1155: start += @width 1156: end 1157: 1158: if start > @width || start < 0 || length < 0 1159: ; #nop 1160: else 1161: if start + length > @width 1162: length = @width - length 1163: length = [length, 0].max 1164: end 1165: end 1166: # Create a Range for the specified set of columns 1167: @cols = Range.new(start, start+length, true) 1168: @unique = false 1169: end 1170: 1171: end
iterator called from subscript methods
# File lib/RMagick.rb, line 1174 1174: def each 1175: maxrows = @height - 1 1176: maxcols = @width - 1 1177: 1178: @rows.each do |j| 1179: if j > maxrows 1180: Kernel.raise IndexError, "index [#{j}] out of range" 1181: end 1182: @cols.each do |i| 1183: if i > maxcols 1184: Kernel.raise IndexError, "index [#{i}] out of range" 1185: end 1186: yield j*@width + i 1187: end 1188: end 1189: nil # useless return value 1190: end