Array Operations: a[] + b[] etc.

Mike Wey mike-wey at example.com
Wed Nov 21 11:40:48 PST 2012


On 11/21/2012 07:02 PM, John Colvin wrote:
> //An example, lets imagine a greyscale image, stored as an array of
> pixel rows:
>
> double[][] img = read_bmp(fn,"grey");
>
> //we want to crop it to some user defined co-ords (x1,y1),(x2,y2):
>
> //Version A, current syntax
>
> auto img_cropped = img[y1..y2].dup;
> foreach(ref row; img_cropped) {
>      row = row[x1..x2];
> }
> //3 lines of code for a very simple idea.
>
> //Version B, new syntax
>
> auto img_cropped = img[y1..y2, x1..x2];
>
> //Very simple, easy to read code that is clear in it's purpose.
>
> I propose that Version B would be equivalent to A: An independent window
> on the data. Any reassignment of a row (i.e. pointing it to somewhere
> else, not copying new data in) will have no effect on the data. This
> scales naturally to higher dimensions and is in agreement with the
> normal slicing rules: the slice itself is independent of the original,
> but the data inside is shared.
>
> I believe this would be a significant improvement to D, particularly for
> image processing and scientific applications.

If you want to use this syntax with images, DMagick's ImageView might be 
interesting:
http://dmagick.mikewey.eu/docs/ImageView.html

-- 
Mike Wey


More information about the Digitalmars-d mailing list