Salut tout le monde.

J'ai besoin de manipuler des fichiers d'images. Pour ce faire, j'ai écrit ce code source simple (d'après ce que j'ai lu sur le forum et le wiki de Lazarus) :

Code Pascal : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
uses
 Graphics;
 
const
 MAXIMUM_BYTE_VALUE = 255;
 
 COLOR_COMPONENTS_NUMBER = 3;
 
 FIRST_COLOR_COMPONENT = 0;
 
 SECOND_COLOR_COMPONENT = 1;
 
 THIRD_COLOR_COMPONENT = 2;
 
type
 TRGBColor = record
              Blue, Green, Red: Byte
             end;
 
 PRGBColor = ^TRGBColor;
 
 TColor = array [0 .. COLOR_COMPONENTS_NUMBER - 1] of Double;
 
 TPixels = array of array of TColor;
 
 TImage = record
           Height, Width: LongInt;
           Pixels: TPixels
          end; 
 
function Make(C1, C2, C3: Double): TColor;
begin
 Result[FIRST_COLOR_COMPONENT] := C1;
 Result[SECOND_COLOR_COMPONENT] := C2;
 Result[THIRD_COLOR_COMPONENT] := C3
end;
 
procedure Load(var Image: TImage; Filename: String);
var
 Picture: TPicture;
 Bitmap: TBitmap;
 Row, Pixel: PByte;
 BPL, Y, X: LongInt;
 Color: TRGBColor;
begin
 Picture := TPicture.Create;
 Picture.LoadFromFile(Filename);
 Bitmap := TBitmap.Create;
 Bitmap.BeginUpdate(FALSE);
 Bitmap.Height := Picture.Height;
 Bitmap.Width := Picture.Width;
 Bitmap.PixelFormat := pf24bit;
 Bitmap.Canvas.Draw(0, 0, Picture.Graphic);
 Image.Height := Bitmap.Height;
 Image.Width := Bitmap.Width;
 SetLength(Image.Pixels, Image.Height, Image.Width);
 Row := Bitmap.RawImage.Data;
 BPL := Bitmap.RawImage.Description.BytesPerLine;
 for Y := 0 to Image.Height - 1 do
  begin
   Pixel := Row;
   for X := 0 to Image.Width - 1 do
    begin
     Color := PRGBColor(Pixel)^;
     Image.Pixels[Y, X] := Make(Color.Red / MAXIMUM_BYTE_VALUE, Color.Green / MAXIMUM_BYTE_VALUE, Color.Blue / MAXIMUM_BYTE_VALUE);
     Inc(Pixel, COLOR_COMPONENTS_NUMBER)
    end;
   Inc(Row, BPL)
  end;
 Bitmap.EndUpdate(FALSE);
 Bitmap.Free;
 Picture.Free
end;

De cette manière, ce code permet de :
  • lire plusieurs formats d'images à l'aide de TPicture ;
  • bénéficier d'un accès rapide à l'image lue grâce à la propriété RawImage de TBitmap;
  • convertir l'image lue en un format utilisable directement grâce à TImage (format tableau 2D).


Le code en question marche très bien ; ma question quant à elle, concerne les possibles améliorations à faire sur ce code, notamment, pour le rendre plus rapide à exécuter et portable (multiplateforme).