Image

Constructors

this
this(in Image img)

from other image

this
this(in Image img)

from other image

this
this(in Vector!(N, T) size, ElemInfo info, in void[] data = [])

from size, element info and data

this
this(in Vector!(N, T) size, size_t ch, DataType type, in void[] data = [])

from size, channel count, component type and data

Postblit

this(this)
this(this)

copy ctor

Members

Functions

clear
void clear()

fill data zeros

dump
immutable(void[]) dump()
index
size_t index(in T[] crd)

return line index by coordinate

pixel
T pixel(in C[] crd...)
const(T) pixel(in C[] crd...)
T pixel(in Vector!(N, C) crd)
const(T) pixel(in Vector!(N, C) crd)

access to pixel

Properties

dataSize
size_t dataSize [@property getter]

image data size

dup
auto dup [@property getter]

get copy of image

idup
auto idup [@property getter]

get immutable copy of image

mapAs
T[] mapAs [@property getter]
const(T)[] mapAs [@property getter]

cast data to T[]

pixelCount
size_t pixelCount [@property getter]

Static functions

external
auto external(in Vector!(N, T) size, ElemInfo info, void[] data)
auto external(in Vector!(N, T) size, size_t ch, DataType type, void[] data)

use external memory (not copy)

load
auto load(immutable(void[]) rawdata)

Variables

data
void[] data;
info
ElemInfo info;
size
CrdVector!0 size;

Examples

1 import std.stdio;
2 auto a = Image( ivec2(3,3), 3, DataType.UBYTE );
3 assert( a.data.length != 0 );
4 assert( eq( a.size, [3,3] ) );
5 a.pixel!bvec3(0,0) = bvec3(1,2,3);
6 auto b = a;
7 assert( b == a );
8 assert( eq( b.pixel!bvec3(0,0), bvec3(1,2,3) ) );
9 a.pixel!bvec3(1,1) = bvec3(3,4,5);
10 assert( b != a );
11 assert( b == Image.load( b.dump() ) );
12 assert( b == b.dup );
13 assert( b.data == b.idup.data.dup );
14 a = b;
15 assert( b == a );
16 assert( a == Image.load( b.dump() ) );
17 assert( a == b.dup );
18 assert( a.data == b.idup.data.dup );
19 auto crd = ivec2(1,2);
20 b.pixel!bvec3(crd) = bvec3(5,6,8);
21 assert( eq( b.pixel!bvec3(1,2), bvec3(5,6,8) ) );
22 
23 b.clear();
24 assert( eq( b.pixel!bvec3(1,2), bvec3(0,0,0) ) );
1 auto a = Image( ivec2(3,3), ElemInfo( 1, DataType.UBYTE ), to!(ubyte[])([ 1,2,3,4,5,6,7,8,9 ]) );
2 auto b = Image(a);
3 
4 assert( a.pixel!ubyte(0,0) == 1 );
5 assert( b.pixel!ubyte(0,0) == 1 );
6 a.pixel!ubyte(0,0) = 2;
7 assert( a.pixel!ubyte(0,0) == 2 );
8 assert( b.pixel!ubyte(0,0) == 1 );
9 
10 auto c = immutable Image(a);
11 assert( c.pixel!ubyte(0,0) == 2 );
1 auto a = Image( ivec!1(3), 1, DataType.UBYTE, to!(ubyte[])([ 1,2,3 ]) );
2 assert(  mustExcept!Throwable({ a.pixel!(ubyte)(7) = 0; }) );
3 assert( !mustExcept({ a.pixel!(ubyte)(0) = 0; }) );
4 
5 assert( a.pixel!ubyte(0) == 0 );
6 
7 auto b = Image(a);
8 b.size.length = 2;
9 b.size[1] = 1;
10 
11 assert( b.size[0] == 3 );
12 assert( b.size[1] == 1 );
13 
14 assert( b.pixel!ubyte(0,0) == 0 );
15 assert( b.pixel!ubyte(1,0) == 2 );
16 assert( mustExcept!Throwable({ b.pixel!ubyte(1,1) = 2; }) );
17 
18 auto c = Image(a);
19 c.size.length = 2;
20 c.size = ivec2(1,3);
21 
22 assert( c.size[0] == 1 );
23 assert( c.size[1] == 3 );
24 
25 assert( c.pixel!ubyte(0,0) == 0 );
26 assert( c.pixel!ubyte(0,1) == 2 );
27 assert( mustExcept!Throwable({ c.pixel!ubyte(1,1) = 2; }) );
28 
29 c.size = ivec2(2,2);
30 
31 assert( c.size[0] == 2 );
32 assert( c.size[1] == 2 );
1 auto a = Image( ivec2(3,3), 2, DataType.FLOAT );
2 
3 assert( a.index([1,2]) == 7 );
4 assert( a.index(ivec2(1,2)) == 7 );
5 
6 a.mapAs!(vec2)[a.index([1,2])] = vec2(1,1);
7 assert( a.pixel!vec2(1,2) == vec2(1,1) );
8 
9 a.pixel!vec2(1,2) = vec2(2,2);
10 assert( a.pixel!vec2(1,2) == vec2(2,2) );

use external memory

1 float[] data = [ 1.0, 2, 3, 4 ];
2 auto img = Image.external( ivec2(2,2), 1, DataType.FLOAT, data );
3 
4 img.pixel!float(0,0) = 8.0f;
5 
6 assert( data[0] == 8.0f );
1 Image img;
2 assert( img.size.length == 0 );
3 assert( img.data.length == 0 );
4 
5 img.size = ivec2(3,3);
6 img.info = ElemInfo( 3, DataType.NORM_FIXED );
7 img.clear();
8 
9 assert( img.data.length == 27 * float.sizeof );
10 assert( img.info.bpe == 3 * float.sizeof );
11 
12 img.pixel!vec3(0,1) = vec3( .2,.1,.3 );
13 assert( img.pixel!vec3(0,1) == vec3(.2,.1,.3) );
14 
15 auto di = Image.load( img.dump() );
16 assert( di.size == img.size );
17 assert( di.info == img.info );
18 assert( di.data == img.data );
19 
20 auto ii = immutable(Image)( img );
21 assert( ii.size == img.size );
22 assert( ii.info == img.info );
23 assert( ii.data == img.data );
24 
25 assert( ii.pixel!vec3(0,1) == vec3(.2,.1,.3) );
26 
27 auto dii = immutable(Image).load( ii.dump() );
28 static assert( is( typeof(dii) == Image ) );
29 assert( dii.size == img.size );
30 assert( dii.info == img.info );
31 assert( dii.data == img.data );
32 
33 auto dd = ii.dup;
34 static assert( is( typeof(dd) == Image ) );
35 assert( dd.size == img.size );
36 assert( dd.info == img.info );
37 assert( dd.data == img.data );
38 
39 auto ddi = ii.idup;
40 static assert( is( typeof(ddi) == immutable(Image) ) );
41 assert( ddi.size == img.size );
42 assert( ddi.info == img.info );
43 assert( ddi.data == img.data );
1 auto data =
2 [
3     vec2( 1, 2 ), vec2( 3, 4 ), vec2( 5, 6 ),
4     vec2( 7, 8 ), vec2( 9, 1 ), vec2( 1, 2 ),
5     vec2( 2, 3 ), vec2( 4, 5 ), vec2( 6, 7 )
6 ];
7 
8 auto img = Image( ivec2(3,3), 2, DataType.FLOAT, data );
9 
10 assert( img.size == ivec2(3,3) );
11 assert( img.info.bpe == 2 * float.sizeof );
12 
13 assert( img.pixel!vec2(1,1) == vec2(9,1) );
14 assert( img.info.type == DataType.FLOAT );
15 
16 auto imdata = img.mapAs!vec2;
17 assert( data == imdata );
18 
19 img.clear();
20 assert( img.pixel!vec2(1,1) == vec2(0,0) );
21 
22 img.mapAs!(vec2)[] = data[];
23 imdata = img.mapAs!vec2;
24 assert( data == imdata );
25 
26 auto constdata = img.idup.mapAs!vec2;
27 assertEq( constdata, imdata );
28 assert( is( typeof(constdata) == const(vec2)[] ) );
1 assert( mustExcept({ Image( ivec2(3,3), ElemInfo(3,DataType.UBYTE), [ 1, 2, 3 ] ); }) );
2 
3 auto dt = [ vec2(1,0), vec2(0,1) ];
4 assert( mustExcept({ Image( ivec2(3,3), 2, DataType.FLOAT, dt ); }) );
5 
6 auto img = Image( ivec2(3,3), ElemInfo( 3, DataType.NORM_FIXED ) );
7 assert( mustExcept({ auto d = img.mapAs!vec2; }) );
8 
9 assert( !mustExcept({ img.pixel!vec3(1,0) = vec3(1,1,1); }) );
10 assert(  mustExcept({ img.pixel!vec2(1,0) = vec2(1,1); }) );
11 static assert(  !__traits(compiles, { img.pixel!vec3(4,4) = vec3(1,1); }) );

Meta