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