Class | Magick::Image::View::Rows |
In: |
lib/RMagick.rb
|
Parent: | Object |
# File lib/RMagick.rb, line 1078 1078: def initialize(view, width, height, rows) 1079: @view = view 1080: @width = width 1081: @height = height 1082: @rows = rows 1083: end
# File lib/RMagick.rb, line 1085 1085: def [](*args) 1086: cols(args) 1087: 1088: # Both View::Pixels and Magick::Pixel implement Observable 1089: if @unique 1090: pixels = @view[@rows[0]*@width + @cols[0]] 1091: pixels.add_observer(self) 1092: else 1093: pixels = View::Pixels.new 1094: each do |x| 1095: p = @view[x] 1096: p.add_observer(self) 1097: pixels << p 1098: end 1099: end 1100: pixels 1101: end
# File lib/RMagick.rb, line 1103 1103: def []=(*args) 1104: rv = args.delete_at(-1) # get rvalue 1105: if ! rv.is_a?(Pixel) # must be a Pixel or a color name 1106: begin 1107: rv = Pixel.from_color(rv) 1108: rescue TypeError 1109: Kernel.raise TypeError, "cannot convert #{rv.class} into Pixel" 1110: end 1111: end 1112: cols(args) 1113: each { |x| @view[x] = rv.dup } 1114: changed 1115: notify_observers(self) 1116: nil 1117: end
A pixel has been modified. Tell the view.
# File lib/RMagick.rb, line 1120 1120: def update(pixel) 1121: changed 1122: notify_observers(self) 1123: pixel.delete_observer(self) # Don't need to hear again. 1124: nil 1125: end
# File lib/RMagick.rb, line 1129 1129: def cols(*args) 1130: @cols = args[0] # remove the outermost array 1131: @unique = false 1132: 1133: # Convert @rows to an Enumerable object 1134: case @rows.length 1135: when 0 # Create a Range for all the rows 1136: @rows = Range.new(0, @height, true) 1137: when 1 # Range, Array, or a single integer 1138: # if the single element is already an Enumerable 1139: # object, get it. 1140: if @rows.first.respond_to? :each 1141: @rows = @rows.first 1142: else 1143: @rows = Integer(@rows.first) 1144: if @rows < 0 1145: @rows += @height 1146: end 1147: if @rows < 0 || @rows > @height-1 1148: Kernel.raise IndexError, "index [#{@rows}] out of range" 1149: end 1150: # Convert back to an array 1151: @rows = Array.new(1, @rows) 1152: @unique = true 1153: end 1154: when 2 1155: # A pair of integers representing the starting column and the number of columns 1156: start = Integer(@rows[0]) 1157: length = Integer(@rows[1]) 1158: 1159: # Negative start -> start from last row 1160: if start < 0 1161: start += @height 1162: end 1163: 1164: if start > @height || start < 0 || length < 0 1165: Kernel.raise IndexError, "index [#{@rows.first}] out of range" 1166: else 1167: if start + length > @height 1168: length = @height - length 1169: length = [length, 0].max 1170: end 1171: end 1172: # Create a Range for the specified set of rows 1173: @rows = Range.new(start, start+length, true) 1174: end 1175: 1176: case @cols.length 1177: when 0 # all rows 1178: @cols = Range.new(0, @width, true) # convert to range 1179: @unique = false 1180: when 1 # Range, Array, or a single integer 1181: # if the single element is already an Enumerable 1182: # object, get it. 1183: if @cols.first.respond_to? :each 1184: @cols = @cols.first 1185: @unique = false 1186: else 1187: @cols = Integer(@cols.first) 1188: if @cols < 0 1189: @cols += @width 1190: end 1191: if @cols < 0 || @cols > @width-1 1192: Kernel.raise IndexError, "index [#{@cols}] out of range" 1193: end 1194: # Convert back to array 1195: @cols = Array.new(1, @cols) 1196: @unique &&= true 1197: end 1198: when 2 1199: # A pair of integers representing the starting column and the number of columns 1200: start = Integer(@cols[0]) 1201: length = Integer(@cols[1]) 1202: 1203: # Negative start -> start from last row 1204: if start < 0 1205: start += @width 1206: end 1207: 1208: if start > @width || start < 0 || length < 0 1209: ; #nop 1210: else 1211: if start + length > @width 1212: length = @width - length 1213: length = [length, 0].max 1214: end 1215: end 1216: # Create a Range for the specified set of columns 1217: @cols = Range.new(start, start+length, true) 1218: @unique = false 1219: end 1220: 1221: end
iterator called from subscript methods
# File lib/RMagick.rb, line 1224 1224: def each 1225: maxrows = @height - 1 1226: maxcols = @width - 1 1227: 1228: @rows.each do |j| 1229: if j > maxrows 1230: Kernel.raise IndexError, "index [#{j}] out of range" 1231: end 1232: @cols.each do |i| 1233: if i > maxcols 1234: Kernel.raise IndexError, "index [#{i}] out of range" 1235: end 1236: yield j*@width + i 1237: end 1238: end 1239: nil # useless return value 1240: end