Class | Magick::Image::View::Rows |
In: |
lib/RMagick.rb
|
Parent: | Object |
# File lib/RMagick.rb, line 1111 1111: def initialize(view, width, height, rows) 1112: @view = view 1113: @width = width 1114: @height = height 1115: @rows = rows 1116: end
# File lib/RMagick.rb, line 1118 1118: def [](*args) 1119: cols(args) 1120: 1121: # Both View::Pixels and Magick::Pixel implement Observable 1122: if @unique 1123: pixels = @view[@rows[0]*@width + @cols[0]] 1124: pixels.add_observer(self) 1125: else 1126: pixels = View::Pixels.new 1127: each do |x| 1128: p = @view[x] 1129: p.add_observer(self) 1130: pixels << p 1131: end 1132: end 1133: pixels 1134: end
# File lib/RMagick.rb, line 1136 1136: def []=(*args) 1137: rv = args.delete_at(-1) # get rvalue 1138: if ! rv.is_a?(Pixel) # must be a Pixel or a color name 1139: begin 1140: rv = Pixel.from_color(rv) 1141: rescue TypeError 1142: Kernel.raise TypeError, "cannot convert #{rv.class} into Pixel" 1143: end 1144: end 1145: cols(args) 1146: each { |x| @view[x] = rv.dup } 1147: changed 1148: notify_observers(self) 1149: nil 1150: end
A pixel has been modified. Tell the view.
# File lib/RMagick.rb, line 1153 1153: def update(pixel) 1154: changed 1155: notify_observers(self) 1156: pixel.delete_observer(self) # Don't need to hear again. 1157: nil 1158: end
# File lib/RMagick.rb, line 1162 1162: def cols(*args) 1163: @cols = args[0] # remove the outermost array 1164: @unique = false 1165: 1166: # Convert @rows to an Enumerable object 1167: case @rows.length 1168: when 0 # Create a Range for all the rows 1169: @rows = Range.new(0, @height, true) 1170: when 1 # Range, Array, or a single integer 1171: # if the single element is already an Enumerable 1172: # object, get it. 1173: if @rows.first.respond_to? :each 1174: @rows = @rows.first 1175: else 1176: @rows = Integer(@rows.first) 1177: if @rows < 0 1178: @rows += @height 1179: end 1180: if @rows < 0 || @rows > @height-1 1181: Kernel.raise IndexError, "index [#{@rows}] out of range" 1182: end 1183: # Convert back to an array 1184: @rows = Array.new(1, @rows) 1185: @unique = true 1186: end 1187: when 2 1188: # A pair of integers representing the starting column and the number of columns 1189: start = Integer(@rows[0]) 1190: length = Integer(@rows[1]) 1191: 1192: # Negative start -> start from last row 1193: if start < 0 1194: start += @height 1195: end 1196: 1197: if start > @height || start < 0 || length < 0 1198: Kernel.raise IndexError, "index [#{@rows.first}] out of range" 1199: else 1200: if start + length > @height 1201: length = @height - length 1202: length = [length, 0].max 1203: end 1204: end 1205: # Create a Range for the specified set of rows 1206: @rows = Range.new(start, start+length, true) 1207: end 1208: 1209: case @cols.length 1210: when 0 # all rows 1211: @cols = Range.new(0, @width, true) # convert to range 1212: @unique = false 1213: when 1 # Range, Array, or a single integer 1214: # if the single element is already an Enumerable 1215: # object, get it. 1216: if @cols.first.respond_to? :each 1217: @cols = @cols.first 1218: @unique = false 1219: else 1220: @cols = Integer(@cols.first) 1221: if @cols < 0 1222: @cols += @width 1223: end 1224: if @cols < 0 || @cols > @width-1 1225: Kernel.raise IndexError, "index [#{@cols}] out of range" 1226: end 1227: # Convert back to array 1228: @cols = Array.new(1, @cols) 1229: @unique &&= true 1230: end 1231: when 2 1232: # A pair of integers representing the starting column and the number of columns 1233: start = Integer(@cols[0]) 1234: length = Integer(@cols[1]) 1235: 1236: # Negative start -> start from last row 1237: if start < 0 1238: start += @width 1239: end 1240: 1241: if start > @width || start < 0 || length < 0 1242: ; #nop 1243: else 1244: if start + length > @width 1245: length = @width - length 1246: length = [length, 0].max 1247: end 1248: end 1249: # Create a Range for the specified set of columns 1250: @cols = Range.new(start, start+length, true) 1251: @unique = false 1252: end 1253: 1254: end
iterator called from subscript methods
# File lib/RMagick.rb, line 1257 1257: def each 1258: maxrows = @height - 1 1259: maxcols = @width - 1 1260: 1261: @rows.each do |j| 1262: if j > maxrows 1263: Kernel.raise IndexError, "index [#{j}] out of range" 1264: end 1265: @cols.each do |i| 1266: if i > maxcols 1267: Kernel.raise IndexError, "index [#{i}] out of range" 1268: end 1269: yield j*@width + i 1270: end 1271: end 1272: nil # useless return value 1273: end