source: trunk/Packages/bgrabitmap/bgrareadxpm.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 1.9 KB
Line 
1unit BGRAReadXPM;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FPReadXPM, FPimage;
9
10type
11
12 { TBGRAReaderXPM }
13
14 TBGRAReaderXPM = class(TFPReaderXPM)
15 protected
16 procedure InternalRead(Str: TStream; Img: TFPCustomImage); override;
17 function InternalCheck(Str: TStream): boolean; override;
18 public
19 class procedure ConvertToXPM3(ASource: TStream; ADestination: TStream);
20 end;
21
22implementation
23
24uses BGRABitmapTypes;
25
26{ TBGRAReaderXPM }
27
28procedure TBGRAReaderXPM.InternalRead(Str: TStream; Img: TFPCustomImage);
29var tempStream: TMemoryStream;
30begin
31 tempStream := TMemoryStream.Create;
32 try
33 ConvertToXPM3(Str, tempStream);
34 tempStream.Position:= 0;
35 try
36 img.UsePalette := true;
37 inherited InternalRead(tempStream, Img);
38 finally
39 end;
40 finally
41 tempStream.free;
42 end;
43end;
44
45function TBGRAReaderXPM.InternalCheck(Str: TStream): boolean;
46var {%H-}magic : array[0..5] of char;
47 l : integer;
48 prevPos: int64;
49begin
50 try
51 prevPos := str.Position;
52 l := str.Read ({%H-}magic[0],sizeof(magic));
53 str.Position:= prevPos;
54 result := (l = sizeof(magic)) and (magic = '! XPM2');
55 if not result then result := inherited InternalCheck(Str)
56 except
57 result := false;
58 end;
59end;
60
61class procedure TBGRAReaderXPM.ConvertToXPM3(ASource: TStream;
62 ADestination: TStream);
63var
64 lst: TStringList;
65 i : integer;
66begin
67 lst := TStringList.Create;
68 try
69 lst.LoadFromStream(ASource);
70 if (lst[0] = '! XPM2') and (lst.count > 1) then
71 begin
72 lst[0] := '/* XPM */';
73 lst.Insert(1, 'static char * data[] = {');
74 for i := 2 to lst.Count-2 do
75 lst[i] := '"' + lst[i] + '",';
76 lst[lst.count-1] := '"' + lst[lst.count-1] + '"';
77 lst.Add('}');
78 end;
79 lst.SaveToStream(ADestination);
80 finally
81 lst.free;
82 end;
83end;
84
85initialization
86
87 DefaultBGRAImageReader[ifXPixMap] := TBGRAReaderXPM;
88
89end.
90
Note: See TracBrowser for help on using the repository browser.