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