1 | unit BGRAReadXPM;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FPReadXPM, FPimage;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
22 | implementation
|
---|
23 |
|
---|
24 | uses BGRABitmapTypes;
|
---|
25 |
|
---|
26 | { TBGRAReaderXPM }
|
---|
27 |
|
---|
28 | procedure TBGRAReaderXPM.InternalRead(Str: TStream; Img: TFPCustomImage);
|
---|
29 | var tempStream: TMemoryStream;
|
---|
30 | begin
|
---|
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;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | function TBGRAReaderXPM.InternalCheck(Str: TStream): boolean;
|
---|
46 | var {%H-}magic : array[0..5] of char;
|
---|
47 | l : integer;
|
---|
48 | prevPos: int64;
|
---|
49 | begin
|
---|
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;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | class procedure TBGRAReaderXPM.ConvertToXPM3(ASource: TStream;
|
---|
62 | ADestination: TStream);
|
---|
63 | var
|
---|
64 | lst: TStringList;
|
---|
65 | i : integer;
|
---|
66 | begin
|
---|
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;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | initialization
|
---|
86 |
|
---|
87 | DefaultBGRAImageReader[ifXPixMap] := TBGRAReaderXPM;
|
---|
88 |
|
---|
89 | end.
|
---|
90 |
|
---|