source: tools/Image resize/UBitmapSet.pas

Last change on this file was 276, checked in by chronos, 4 years ago
  • Added: Action to convert magenta transparency png images to images with alpha channel.
File size: 3.5 KB
Line 
1unit UBitmapSet;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, fgl, XMLRead, XMLWrite, DOM, UXMLUtils;
9
10type
11
12 { TBitmapDesc }
13
14 TBitmapDesc = class
15 Name: string;
16 Rect: TRect;
17 Color1: Cardinal;
18 Color2: Cardinal;
19 private
20 procedure LoadFromNode(Node: TDOMNode);
21 procedure SaveToNode(Node: TDOMNode);
22 end;
23
24 { TBitmapDescs }
25
26 TBitmapDescs = class(TFPGObjectList<TBitmapDesc>)
27 private
28 procedure LoadFromNode(Node: TDOMNode);
29 procedure SaveToNode(Node: TDOMNode);
30 end;
31
32 { TBitmapSet }
33
34 TBitmapSet = class
35 FileName: string;
36 ImageFileName: string;
37 Items: TBitmapDescs;
38 procedure LoadFromFile(FileName: string);
39 procedure SaveToFile(FileName: string);
40 constructor Create;
41 destructor Destroy; override;
42 end;
43
44
45implementation
46
47resourcestring
48 SWrongFileFormat = 'Wrong file format';
49
50
51procedure TBitmapDesc.SaveToNode(Node: TDOMNode);
52begin
53 WriteString(Node, 'Name', Name);
54 WriteInteger(Node, 'Left', Rect.Left);
55 WriteInteger(Node, 'Top', Rect.Top);
56 WriteInteger(Node, 'Width', Rect.Width);
57 WriteInteger(Node, 'Height', Rect.Height);
58 WriteInteger(Node, 'Color1', Color1);
59 WriteInteger(Node, 'Color2', Color2);
60end;
61
62procedure TBitmapDesc.LoadFromNode(Node: TDOMNode);
63begin
64 Name := ReadString(Node, 'Name', '');
65 Rect.Left := ReadInteger(Node, 'Left', 0);
66 Rect.Top := ReadInteger(Node, 'Top', 0);
67 Rect.Width := ReadInteger(Node, 'Width', 0);
68 Rect.Height := ReadInteger(Node, 'Height', 0);
69 Color1 := ReadInteger(Node, 'Color1', 0);
70 Color2 := ReadInteger(Node, 'Color2', 0);
71end;
72
73procedure TBitmapDescs.SaveToNode(Node: TDOMNode);
74var
75 I: Integer;
76 NewNode2: TDOMNode;
77begin
78 for I := 0 to Count - 1 do
79 with TBitmapDesc(Items[I]) do begin
80 NewNode2 := Node.OwnerDocument.CreateElement('Bitmap');
81 Node.AppendChild(NewNode2);
82 SaveToNode(NewNode2);
83 end;
84end;
85
86procedure TBitmapDescs.LoadFromNode(Node: TDOMNode);
87var
88 Node2: TDOMNode;
89 NewItem: TBitmapDesc;
90begin
91 Count := 0;
92 Node2 := Node.FirstChild;
93 while Assigned(Node2) and (Node2.NodeName = 'Bitmap') do begin
94 NewItem := TBitmapDesc.Create;
95 NewItem.LoadFromNode(Node2);
96 Add(NewItem);
97 Node2 := Node2.NextSibling;
98 end;
99end;
100
101{ TBitmapSet }
102
103constructor TBitmapSet.Create;
104begin
105 Items := TBitmapDescs.Create;
106end;
107
108destructor TBitmapSet.Destroy;
109begin
110 FreeAndNil(Items);
111 inherited;
112end;
113
114procedure TBitmapSet.LoadFromFile(FileName: string);
115var
116 NewNode: TDOMNode;
117 Doc: TXMLDocument;
118 RootNode: TDOMNode;
119begin
120 Self.FileName := FileName;
121 ReadXMLFile(Doc, FileName);
122 with Doc do try
123 if Doc.DocumentElement.NodeName <> 'BitmapSet' then
124 raise Exception.Create(SWrongFileFormat);
125 RootNode := Doc.DocumentElement;
126 with RootNode do begin
127 ImageFileName := ReadString(RootNode, 'Image', '');
128
129 NewNode := FindNode('Bitmaps');
130 if Assigned(NewNode) then
131 Items.LoadFromNode(NewNode);
132 end;
133 finally
134 Doc.Free;
135 end;
136end;
137
138procedure TBitmapSet.SaveToFile(FileName: string);
139var
140 NewNode: TDOMNode;
141 Doc: TXMLDocument;
142 RootNode: TDOMNode;
143begin
144 Self.FileName := FileName;
145 Doc := TXMLDocument.Create;
146 with Doc do try
147 RootNode := CreateElement('BitmapSet');
148 AppendChild(RootNode);
149 with RootNode do begin
150 WriteString(RootNode, 'Image', ImageFileName);
151
152 NewNode := OwnerDocument.CreateElement('Bitmaps');
153 AppendChild(NewNode);
154 Items.SaveToNode(NewNode);
155 end;
156 ForceDirectories(ExtractFileDir(FileName));
157 WriteXMLFile(Doc, FileName);
158 finally
159 Doc.Free;
160 end;
161end;
162
163end.
164
Note: See TracBrowser for help on using the repository browser.