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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 | -- --
-- procedure XPM2GtkAda Copyright (c) Dmitry A. Kazakov --
-- XPM to GtkAda converter Luebeck --
-- Summer, 2006 --
-- Implementation --
-- Last revision : 07:53 21 Jul 2016 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
--
-- XPM to GtkAda converter is a small utility which reads an XPM file
-- from the standard input and creates Ada packages for embedding the
-- image into a GtkAda application. The generated files are named after
-- the XPM image name stored in the XPM file. Each XPM file usually
-- starts as:
--
-- /* XPM */
-- static char * <name> [] = {
--
-- The files created will be named as:
--
-- <name>.ads -- Contains pixels and pixbuf creation function
-- <name>-image.ads -- Contains a child package to get a Gtk_Image
-- <name>-image.adb -- The implementation of
--
-- The package <name>.ads will look like:
--
-- package <name> is
-- X_Size : constant GInt := ...
-- Y_Size : constant GInt := ...
-- type Pixbuf_Image is array (Natural range ...) of GUChar;
-- pragma Convention (C, Pixbuf_Image);
-- Pixels : constant Pixbuf_Image := ...
-- function Get_Pixbuf ...
-- return Gdk_Pixbuf;
--
-- The function Pixbuf can be used to the image in Pixbuf format.
-- The result object references to the data in the array Pixels.
-- Basically, it is a just a reference.
--
-- The child function <name>-image.ads provides an easy way to
-- create an image object on demand:
--
-- function <name>.Image return Gtk_Image;
--
-- It creates and returns a Gtk_Image object corresponding to the
-- image.
--
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Text_IO; use Ada.Text_IO;
with Strings_Edit; use Strings_Edit;
with Strings_Edit.Integers; use Strings_Edit.Integers;
with Parsers.Multiline_Source.XPM;
use Parsers.Multiline_Source.XPM;
with Parsers.Multiline_Source.Standard_Input;
use Parsers.Multiline_Source.Standard_Input;
procedure XPM2GtkAda is
procedure Create_Pixbuf
( Header : Descriptor;
Picture : Pixel_Buffer
) is
Output : File_Type;
Bytes : Integer := 3;
Pointer : Integer := 1;
Pixel : Natural := 0;
RGB : Integer;
Line : String (1..72);
begin
Create (Output, Out_File, Header.Name & ".ads");
for Row in Picture'Range (1) loop
for Column in Picture'Range (2) loop
if Picture (Row, Column) = Transparent then
Bytes := 4;
exit;
end if;
end loop;
end loop;
Put_Line (Output, "with Gdk.Pixbuf; use Gdk.Pixbuf;");
Put_Line (Output, "with GLib; use GLib;");
Put_Line (Output, "with Interfaces.C; use Interfaces.C;");
Put_Line (Output, "with System; use System;");
New_Line (Output);
Put_Line (Output, "package " & Header.Name & " is");
Put_Line
( Output,
" X_Size : constant GInt := "
& Image (Header.Width)
& ";"
);
Put_Line
( Output,
" Y_Size : constant GInt := "
& Image (Header.Height)
& ";"
);
Put_Line
( Output,
( " type Pixbuf_Image is array (Natural range 0.."
& Image (Header.Height * Header.Width * Bytes - 1)
& ") of GUChar;"
) );
Put_Line (Output, " pragma Convention (C, Pixbuf_Image);");
Put_Line (Output, " Pixels : constant Pixbuf_Image :=");
Put (Line, Pointer, " ( ");
if Bytes = 4 then
for Row in Picture'Range (1) loop
for Column in Picture'Range (2) loop
Pixel := Pixel + 1;
RGB := Integer (Picture (Row, Column));
if RGB > 16#FFFFFF# then
Put (Line, Pointer, "255,255,255, 0");
else
Put
( Line,
Pointer,
RGB / 16#10000#,
Field => 3,
Justify => Right
);
Put (Line, Pointer, ",");
Put
( Line,
Pointer,
(RGB mod 16#10000#) / 16#100#,
Field => 3,
Justify => Right
);
Put (Line, Pointer, ",");
Put
( Line,
Pointer,
RGB mod 16#100#,
Field => 3,
Justify => Right
);
Put (Line, Pointer, ",255");
end if;
if ( Row /= Picture'Last (1)
or else
Column /= Picture'Last (2)
) then
Put (Line, Pointer, ",");
end if;
if Pixel = 4 then
Put_Line (Output, Line (1..Pointer - 1));
Pixel := 0;
Pointer := 1;
Put (Line, Pointer, " ");
end if;
end loop;
end loop;
else
for Row in Picture'Range (1) loop
for Column in Picture'Range (2) loop
Pixel := Pixel + 1;
RGB := Integer (Picture (Row, Column));
Put
( Line,
Pointer,
RGB / 16#10000#,
Field => 3,
Justify => Right
);
Put (Line, Pointer, ",");
Put
( Line,
Pointer,
(RGB mod 16#10000#) / 16#100#,
Field => 3,
Justify => Right
);
Put (Line, Pointer, ",");
Put
( Line,
Pointer,
RGB mod 16#100#,
Field => 3,
Justify => Right
);
if ( Row /= Picture'Last (1)
or else
Column /= Picture'Last (2)
) then
Put (Line, Pointer, ",");
end if;
if Pixel = 5 then
Put_Line (Output, Line (1..Pointer - 1));
Pixel := 0;
Pointer := 1;
Put (Line, Pointer, " ");
end if;
end loop;
end loop;
end if;
if Pixel > 0 then
Put_Line (Output, Line (1..Pointer - 1));
end if;
Put_Line (Output, " );");
Put_Line (Output, " function Get_Pixbuf return Gdk_Pixbuf;");
Put_Line (Output, "end " & Header.Name & ";");
Close (Output);
Create (Output, Out_File, Header.Name & ".adb");
Put_Line (Output, "with Gdk.Pixbuf.Conversions;");
Put_Line (Output, "package body " & Header.Name & " is");
Put_Line (Output, " function Get_Pixbuf return Gdk_Pixbuf is");
Put_Line (Output, " function Internal");
Put_Line (Output, " ( Data : Pixbuf_Image;");
Put_Line
( Output,
" Colorspace : Gdk_Colorspace;"
);
Put_Line (Output, " Has_Alpha : GBoolean;");
Put_Line (Output, " Bits : Int;");
Put_Line (Output, " Width : Int;");
Put_Line (Output, " Height : Int;");
Put_Line (Output, " Rowstride : Int;");
Put_Line (Output, " Fn : Address;");
Put_Line (Output, " Fn_Data : Address");
Put_Line (Output, " ) return Address;");
Put_Line
( Output,
( " pragma Import (C, Internal, "
& """gdk_pixbuf_new_from_data"");"
) );
Put_Line (Output, " begin");
Put_Line (Output, " return Gdk.Pixbuf.Conversions."
& "From_Address");
Put_Line (Output, " ( Internal");
Put_Line (Output, " ( Pixels,");
Put_Line (Output, " Colorspace_RGB,");
if Bytes = 4 then
Put_Line (Output, " 1,");
else
Put_Line (Output, " 0,");
end if;
Put_Line (Output, " 8,");
Put_Line (Output, " Int (X_Size),");
Put_Line (Output, " Int (Y_Size),");
Put_Line (Output, " "
& Image (Header.Width * Bytes) & ",");
Put_Line (Output, " Null_Address,");
Put_Line (Output, " Null_Address");
Put_Line (Output, " ) );");
Put_Line (Output, " end Get_Pixbuf;");
Put_Line (Output, "end " & Header.Name & ";");
Close (Output);
Create (Output, Out_File, Header.Name & "-image.ads");
Put_Line (Output, "with Gtk.Image; use Gtk.Image;");
New_Line (Output);
Put_Line (Output, "function " & Header.Name & ".Image");
Put_Line (Output, " return Gtk_Image;");
Close (Output);
Create (Output, Out_File, Header.Name & "-image.adb");
Put_Line (Output, "function " & Header.Name & ".Image");
Put_Line (Output, " return Gtk_Image is");
Put_Line (Output, " Pic : constant Gdk_Pixbuf := Get_Pixbuf;");
Put_Line (Output, " Result : Gtk_Image;");
Put_Line (Output, "begin");
Put_Line (Output, " Gtk_New (Result, Pic);");
Put_Line (Output, " Unref (Pic);");
Put_Line (Output, " return Result;");
Put_Line (Output, "end " & Header.Name & ".Image;");
Close (Output);
end Create_Pixbuf;
type Action is (Error, Pixbuf);
To_Do : Action;
begin
case Argument_Count is
when 0 =>
To_Do := Pixbuf;
when others =>
To_Do := Error;
end case;
if To_Do = Error then
Put_Line (Standard_Error, "Use: xpm2ada < <input>");
Set_Exit_Status (Failure);
return;
end if;
declare
Input : aliased Source;
Header : Descriptor := Get (Input'Access);
Map : Color_Tables.Table := Get (Input'Access, Header);
Picture : Pixel_Buffer := Get (Input'Access, Header, Map);
begin
Create_Pixbuf (Header, Picture);
end;
Set_Exit_Status (Success);
exception
when Error : others =>
Put_Line (Standard_Error, Exception_Message (Error));
Set_Exit_Status (Failure);
end XPM2GtkAda;
|