@@ -1129,14 +1129,10 @@ def [] index
1129
1129
end
1130
1130
end
1131
1131
1132
- # Convert to an Array. This will be slow for large images .
1132
+ # Convert to an Enumerator. Similar to `#to_a` but lazier .
1133
1133
#
1134
- # @return [Array] array of Fixnum
1135
- def to_a
1136
- # we render the image to a big string, then unpack
1137
- # as a Ruby array of the correct type
1138
- memory = write_to_memory
1139
-
1134
+ # @return [Enumerator] Enumerator of Enumerators of Arrays of Numerics
1135
+ def to_enum
1140
1136
# make the template for unpack
1141
1137
template = {
1142
1138
char : "c" ,
@@ -1151,14 +1147,22 @@ def to_a
1151
1147
dpcomplex : "d"
1152
1148
} [ format ] + "*"
1153
1149
1154
- # and unpack into something like [1, 2, 3, 4 ..]
1155
- array = memory . unpack ( template )
1150
+ # we render the image to a big string, then unpack into
1151
+ # one-dimensional array as a Ruby array of the correct type
1152
+ array = write_to_memory . unpack template
1153
+
1154
+ # gather bands of a pixel together
1155
+ pixel_array = array . each_slice bands
1156
1156
1157
- # gather band elements together
1158
- pixel_array = array . each_slice ( bands ) . to_a
1157
+ # gather pixels of a row together
1158
+ pixel_array . each_slice width
1159
+ end
1159
1160
1160
- # build rows
1161
- pixel_array . each_slice ( width ) . to_a
1161
+ # Convert to an Array. This will be slow for large images.
1162
+ #
1163
+ # @return [Array] Array of Arrays of Arrays of Numerics
1164
+ def to_a
1165
+ to_enum . to_a
1162
1166
end
1163
1167
1164
1168
# Return the largest integral value not greater than the argument.
0 commit comments