### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: array of uint32 to TImage [SOLVED]  (Read 1130 times)

#### rforcen

• New member
• Posts: 5
##### array of uint32 to TImage [SOLVED]
« on: January 28, 2022, 10:06:10 am »
how can i directly convert an array of uint32 to a TImage without cycling with a SetPixel(x,y,color) ?
i've coded a sample loop solution using fpimage:

EDIT: i've used this to write a jpeg file of a mandelbrot fractal, complete code @ https://github.com/rforcen/fpc/tree/main/mandel

Code: Pascal  [Select][+][-]
1. {\$mode objfpc}{\$h+}
2. program Prog_au32totimage;
3.
4. uses classes, sysutils,
5.      FPImage, FPWriteJPEG;
6.
7. var
8.   au32 : array of uint32;
9.   w,h: integer;
10.
11.   i: integer;
12.
13. //
14. function au32ToImage(au32:array of uint32; w,h:integer) : TFPMemoryImage;
15. var
16.   i,j : integer;
17.   pix : uint32;
18.   col : TFPColor; // rgba in 0..\$ffff range -> (0..\$ff) << 8
19. begin
20.   au32ToImage := TFPMemoryImage.Create(w,h);
21.
22.   for i:=0 to w-1 do
23.     for j:=0 to h-1 do begin
24.       pix:=au32[i*w + j];
25.       with col do begin
26.         red:=(pix and \$FF) shl 8;
27.         green:=((pix shr 8) and \$FF) shl 8;
28.         blue:=((pix shr 16) and \$FF) shl 8;
29.         alpha:=((pix shr 24) and \$FF) shl 8;
30.       end;
31.       au32ToImage.Colors[i, j] := col;
32.     end;
33. end;
34.
35. //
36. procedure au32ToJPEG(au32:array of uint32; w,h:integer; fileName:string);
37. var
38.   writer: TFPCustomImageWriter;
39.   image : TFPMemoryImage;
40.
41. begin
42.   image:=au32ToImage(au32, w,h);
43.
44.   writer:=TFPWriterJPEG.Create;
45.   image.SaveToFile(fileName, writer);
46.
47.   image.free;
48.   writer.free;
49. end;
50.
51. /////
52. begin
53.   w:=1024; h:=w;
54.   SetLength(au32, w*h);
55.
56.   randomize;
57.   for i:=0 to w*h-1 do
58.     au32[i]:=random(\$ff000000);
59.
60.   au32ToJPEG(au32, w,h, 'au32.jpg');
61. end.
62.
« Last Edit: January 28, 2022, 12:15:11 pm by rforcen »

#### Zvoni

• Hero Member
• Posts: 1185
##### Re: array of uint32 to TImage
« Reply #1 on: January 28, 2022, 11:14:18 am »
i take it, your array of UInt32 contains the Color-Values?
The only thing coming to mind:
you would have to find the property of TImage which actually contains the color-information per x/y-coordinate (a TCanvas, TBitmap, whatever?).
That's assuming that Property is a 2D-Array representing height and width with the value being the color

IIRC, the memory-layout of a 2D-Array is "column"-wise, meaning:
e.g. Let's say you have 8 x 4 2D-Array (8 rows and 4 columns), so your array of UInt32 would have 32 Members [0..31]
"cell" [0,1] (First Row, second column) would have a memory-address of cell[7,0] (8th Row, 1st column) + 4 bytes

1000   0,0
1004   1,0
1008   2,0
1012   3,0
1016   4,0
1020   5,0
1024   6,0
1028   7,0
1032   0,1
1036   1,1
1040   2,1
1044   3,1

In that case: Bring your array of UInt32 in the "correct" order and then copy it over to [0,0]

Note: I have no idea how SetPixel works (as in: Is it just assigning the Value to x,y or is there some additional black magic happening)
Though, might be completely wrong here
« Last Edit: January 28, 2022, 11:16:19 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### rforcen

• New member
• Posts: 5
##### Re: array of uint32 to TImage
« Reply #2 on: January 28, 2022, 11:34:25 am »
yes the array contains 8 bit ARGB values coded in uint32,

i was looking for something more direct in the line of facilities provided by image magick or pillow in python, anyhow the coded added to my initial post can do the trick by now,
thankx

#### wp

• Hero Member
• Posts: 9589
##### Re: array of uint32 to TImage
« Reply #3 on: January 28, 2022, 12:07:57 pm »
In the attachment there is a solution based on TRawImage and TBitmap.LoadFromRawImage; it does not require a temporary image file.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### rforcen

• New member
• Posts: 5
##### Re: array of uint32 to TImage
« Reply #4 on: January 28, 2022, 12:14:39 pm »
Wow!, thankx that's exactly what i was looking for!

using it, coded a mandelbrot fractal browser with MT generation

https://github.com/rforcen/fpc/tree/main/mandelbrot

edit: changed generation engine to c++ and gained a x2 boost, same with:

https://github.com/rforcen/fpc/tree/main/voronoi
« Last Edit: February 01, 2022, 12:30:52 pm by rforcen »