# File lib/RMagick.rb, line 1131
1131: def [](*args)
1132: cols(args)
1133:
1134: # Both View::Pixels and Magick::Pixel implement Observable
1135: if @unique
1136: pixels = @view[@rows[0]*@width + @cols[0]]
1137: pixels.add_observer(self)
1138: else
1139: pixels = View::Pixels.new
1140: each do |x|
1141: p = @view[x]
1142: p.add_observer(self)
1143: pixels << p
1144: end
1145: end
1146: pixels
1147: end
# File lib/RMagick.rb, line 1149
1149: def []=(*args)
1150: rv = args.delete_at(1) # get rvalue
1151: if ! rv.is_a?(Pixel) # must be a Pixel or a color name
1152: begin
1153: rv = Pixel.from_color(rv)
1154: rescue TypeError
1155: Kernel.raise TypeError, "cannot convert #{rv.class} into Pixel"
1156: end
1157: end
1158: cols(args)
1159: each { |x| @view[x] = rv.dup }
1160: changed
1161: notify_observers(self)
1162: nil
1163: end
# File lib/RMagick.rb, line 1175
1175: def cols(*args)
1176: @cols = args[0] # remove the outermost array
1177: @unique = false
1178:
1179: # Convert @rows to an Enumerable object
1180: case @rows.length
1181: when 0 # Create a Range for all the rows
1182: @rows = Range.new(0, @height, true)
1183: when 1 # Range, Array, or a single integer
1184: # if the single element is already an Enumerable
1185: # object, get it.
1186: if @rows.first.respond_to? :each
1187: @rows = @rows.first
1188: else
1189: @rows = Integer(@rows.first)
1190: if @rows < 0
1191: @rows += @height
1192: end
1193: if @rows < 0 || @rows > @height-1
1194: Kernel.raise IndexError, "index [#{@rows}] out of range"
1195: end
1196: # Convert back to an array
1197: @rows = Array.new(1, @rows)
1198: @unique = true
1199: end
1200: when 2
1201: # A pair of integers representing the starting column and the number of columns
1202: start = Integer(@rows[0])
1203: length = Integer(@rows[1])
1204:
1205: # Negative start -> start from last row
1206: if start < 0
1207: start += @height
1208: end
1209:
1210: if start > @height || start < 0 || length < 0
1211: Kernel.raise IndexError, "index [#{@rows.first}] out of range"
1212: else
1213: if start + length > @height
1214: length = @height - length
1215: length = [length, 0].max
1216: end
1217: end
1218: # Create a Range for the specified set of rows
1219: @rows = Range.new(start, start+length, true)
1220: end
1221:
1222: case @cols.length
1223: when 0 # all rows
1224: @cols = Range.new(0, @width, true) # convert to range
1225: @unique = false
1226: when 1 # Range, Array, or a single integer
1227: # if the single element is already an Enumerable
1228: # object, get it.
1229: if @cols.first.respond_to? :each
1230: @cols = @cols.first
1231: @unique = false
1232: else
1233: @cols = Integer(@cols.first)
1234: if @cols < 0
1235: @cols += @width
1236: end
1237: if @cols < 0 || @cols > @width-1
1238: Kernel.raise IndexError, "index [#{@cols}] out of range"
1239: end
1240: # Convert back to array
1241: @cols = Array.new(1, @cols)
1242: @unique &&= true
1243: end
1244: when 2
1245: # A pair of integers representing the starting column and the number of columns
1246: start = Integer(@cols[0])
1247: length = Integer(@cols[1])
1248:
1249: # Negative start -> start from last row
1250: if start < 0
1251: start += @width
1252: end
1253:
1254: if start > @width || start < 0 || length < 0
1255: ; #nop
1256: else
1257: if start + length > @width
1258: length = @width - length
1259: length = [length, 0].max
1260: end
1261: end
1262: # Create a Range for the specified set of columns
1263: @cols = Range.new(start, start+length, true)
1264: @unique = false
1265: end
1266:
1267: end
iterator called from subscript methods
# File lib/RMagick.rb, line 1270
1270: def each
1271: maxrows = @height - 1
1272: maxcols = @width - 1
1273:
1274: @rows.each do |j|
1275: if j > maxrows
1276: Kernel.raise IndexError, "index [#{j}] out of range"
1277: end
1278: @cols.each do |i|
1279: if i > maxcols
1280: Kernel.raise IndexError, "index [#{i}] out of range"
1281: end
1282: yield j*@width + i
1283: end
1284: end
1285: nil # useless return value
1286: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.