This is an old topic, but I like to revisit it using Lazarus 3.0RC1 and I have updated my test program with a class array (method 6, 7) as proposed by @Mr.Madguy. See attachment.
The fastest method to copy an image as an array is method 5 or 6. The 3D data is stored in a 1D array.
However if individual pixel access is required using a 3D dynamic array (method 1, 17 sec)) is faster then putting the 3D data in a 1D dynamic array (method 4, 46 sec). The new class MyArray is very slow (method 7, execution time 2:17)
Using 3D dynamic array for image handling seems still the best option. Both duplicating and individual pixel access is fast.
Method 2:
type
image_array = array of array of array of Single;
function duplicate2(img:image_array) :image_array;
var
c,w,h,k,i: integer;
begin
c:=length(img);
h:=length(img[0]);
w:=length(img[0,0]);
setlength(result,c,h,w);
for k:=0 to c-1 do
for i:=0 to h-1 do
begin
result[k,i]:=copy(img[k,i],0,w);
end;
end;
The largest dimension of the array is best put last. So for a landscape image setlength(image_array,nrcolors,height,width);
Han
The test results:
Method 1, using a 3D array, for k:=0 to nrcolors-1 do for i:=0 to height2-1 do for j:=0 to width2-1 do D[k,i,j]:=C[k,i,j];
Excution time 00:00:17
Method 2, using a 3D array, for k:=0 to nrcolors-1 do for i:=0 to height2-1 do D[k,i]:=copy(C[k,i],0,width2);
Excution time 00:00:04
Method 3, Using 3D array, D:=C;{copy pointer} setlength(D,nrcolors,height2,width2);{make real duplicate}
Excution time 00:00:07
Method 4, Using a 1D array to store 3D data.
Excution time 00:00:46
Method 5, Using 1D array, F:=copy(E,0,nrcolors*height2*width2-1);
Excution time 00:00:05
Method 6, Using 3D array using a new class MyArray. H := G.Copy;
Excution time 00:00:05
Method 7, Using 3D array using a new class MyArray. For k:=0 to nrcolors-1 do for i:=0 to height2-1 do for j:=0 to width2-1 do H[k,i,j]:=G[k,i,j];
This is very slow and will typically take a few minutes.
Execution time 00:02:17