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
|
#!/usr/bin/perl
#- convert in.png -colors 128 -treedepth 6 -dither out.gif
use strict;
use lib '/usr/lib/libDrakX';
use Getopt::Long;
use ugtk2 qw(:all);
use Gtk2::Gdk::Keysyms;
use MDK::Common;
use Image::Magick;
use bootsplash qw(:drawing);
my $kernel_size = 1500; #- in KiB
my $initrd_size = 130; #- in KiB
my $lilo_block_size = 50; #- in KiB
my $isolinux_block_size = 64; #- in KiB
my @modes = (
{ Vesa => 0x101, X => 640, Y => 480 },
{ Vesa => 0x103, X => 800, Y => 600 },
{ Vesa => 0x105, X => 1024, Y => 768 },
{ Vesa => 0x107, X => 1280, Y => 1024 },
);
my (%image_size, $kernel_and_initrd_size);
my ($progress_rect, $progress_color) = ([], {});
my ($timer_pos, $timer_bg, $timer_fg) = ({}, {}, {});
my ($entry_rect, $entry_selected_color, $entry_color) = ([], {}, {});
my $isolinux_mode;
my $magick = Image::Magick->new;
my ($current_rect, $current_point);
my ($image_area, $image_pixbuf);
sub move_point {
my ($up_down, $direction) = @_;
my $wanted = $current_point->{$direction} + $up_down;
if (0 <= $wanted && $wanted < $image_size{$direction}) {
$current_point->{$direction} += $up_down;
}
$image_area->queue_draw;
}
sub create_image_area() {
$image_area = Gtk2::DrawingArea->new;
$image_area->can_focus(1);
$image_area->add_events('button-press-mask');
$image_area->signal_connect(button_press_event => \&image_button_pressed);
$image_area->signal_connect(expose_event => \&image_expose);
gtkmodify_font($image_area, 'Monospace 12');
my $keys = {
$Gtk2::Gdk::Keysyms{Down} => sub { move_point( ($_[0] ? 5 : 1), 'Y') },
$Gtk2::Gdk::Keysyms{Up} => sub { move_point(-($_[0] ? 5 : 1), 'Y') },
$Gtk2::Gdk::Keysyms{Left} => sub { move_point(-($_[0] ? 5 : 1), 'X') },
$Gtk2::Gdk::Keysyms{Right} => sub { move_point( ($_[0] ? 5 : 1), 'X') },
$Gtk2::Gdk::Keysyms{q} => sub { Gtk2->main_quit },
$Gtk2::Gdk::Keysyms{Escape} => sub { Gtk2->main_quit },
};
$image_area->signal_connect(key_press_event => sub {
my (undef, $event) = @_;
if (my $f = $keys->{$event->keyval}) {
$f->(member('control-mask', @{$event->state}));
}
1;
});
$image_area->grab_focus;
$image_area->show;
$image_area;
}
sub image_expose {
my ($widget) = @_;
my $window = $widget->window;
$window->draw_pixbuf($widget->style->white_gc, $image_pixbuf, 0, 0, 0, 0, -1, -1, 'none', 0, 0);
if (!$isolinux_mode) {
{
my $layout = $widget->create_pango_layout('--:--');
my ($width, $height) = $layout->get_pixel_size;
$window->draw_rectangle(color_index2gc($window, $timer_bg), 1, $timer_pos->{X}, $timer_pos->{Y}, $width, $height);
$window->draw_layout(color_index2gc($window, $timer_fg), $timer_pos->{X}, $timer_pos->{Y}, $layout);
}
my ($x, $y, $w, $h) = rectangle2xywh($entry_rect);
my @std_labels = ('linux', 'failsafe', '2.6.3-7mdk', 'X' x ($w / 8));
for (my $nb = 0; $nb < int($h / 16); $y += 16, $nb++) {
my $label = shift(@std_labels) || 'label_' . ($nb+1);
my $gc = color_index2gc($window, !$nb ? $entry_selected_color : $entry_color);
my $layout = $widget->create_pango_layout($label);
$window->draw_layout($gc, $x, $y, $layout);
}
}
{
my ($x, $y, $w, $h) = rectangle2xywh($progress_rect);
my $w2 = $w - $w % nb_steps();
if ($current_rect) {
$window->draw_rectangle(color_index2gc($window, $progress_color), 1, $x, $y, $w2, $h);
$window->draw_rectangle($widget->style->black_gc, 0, $x, $y, $w, $h);
} else {
$window->draw_rectangle(color_index2gc($window, $progress_color), 1, $x, $y, $w, $h);
}
}
0;
}
sub image_button_pressed {
my (undef, $event) = @_;
if ($event->button eq '3') {
create_popup()->popup(undef, undef, undef, undef, '3', $event->time);
return 1;
}
my $point = { X => $event->x, Y => $event->y };
if ($current_rect) {
if (!@$current_rect) {
my @corners = ({ X => 0, Y => 0 },
{ X => 0, Y => $image_size{Y} - 1 },
{ X => $image_size{X} - 1, Y => 0 },
{ X => $image_size{X} - 1, Y => $image_size{Y} - 1 });
@$current_rect = ($corners[farthest($point, @corners)], nearest($point, @corners));
}
$current_point = nearest($point, @$current_rect);
} elsif ($current_point) {
%$current_point or %$current_point = %$point;
} else {
return;
}
%$current_point = %$point;
$current_point->{Color} = $magick->Get("index[$point->{X},$point->{Y}]");
$image_area->queue_draw;
1;
}
sub image_set_file {
my ($file) = @_;
$image_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($file);
%image_size = (X => $image_pixbuf->get_width, Y => $image_pixbuf->get_height);
$image_area->set_size_request($image_size{X}, $image_size{Y});
$image_area->queue_draw;
}
sub color_index2gc {
my ($window, $color) = @_;
my ($r, $g, $b) = split(',', $magick->Get("colormap[$color->{Color}]"));
my $gc = Gtk2::Gdk::GC->new($window);
my $gdk_color = Gtk2::Gdk::Color->new($r, $g, $b) or die "bad color for $r,$g,$b";
$window->get_colormap->rgb_find_color($gdk_color);
$gc->set_rgb_fg_color($gdk_color);
$gc;
}
sub create_popup() {
my @l = (
'Progress bar position' => sub { $current_rect = $progress_rect },
'Progress bar color' => sub { $current_point = $progress_color },
'' => sub {},
$isolinux_mode ? (
'switch to lilo mode' => sub { $isolinux_mode = 0 },
) : (
'Timer position' => sub { $current_point = $timer_pos },
'Timer text color' => sub { $current_point = $timer_fg },
'Timer background' => sub { $current_point = $timer_bg },
'' => sub {},
'Entry position' => sub { $current_rect = $entry_rect },
'Entry text selected color' => sub { $current_point = $entry_selected_color },
'Entry text color' => sub { $current_point = $entry_color },
'' => sub {},
'switch to isolinux mode' => sub { $isolinux_mode = 1 },
),
);
my $popup = Gtk2::Menu->new;
foreach (group_by2(@l)) {
my ($descr, $f) = @$_;
gtkappend($popup,
gtksignal_connect(Gtk2::MenuItem->new_with_label($descr),
activate => sub {
$current_rect = $current_point = undef;
$f->();
}));
}
$popup;
}
sub nb_steps() {
$kernel_and_initrd_size / ($isolinux_mode ? $isolinux_block_size : $lilo_block_size);
}
sub read_parameters {
my ($file) = @_;
my %h = getVarsFromSh($file);
$entry_rect->[0]{X} = $h{entry_x};
$entry_rect->[0]{Y} = $h{entry_y};
$entry_rect->[1]{X} = $h{entry_x} + $h{entry_w};
$entry_rect->[1]{Y} = $h{entry_y} + $h{entry_h};
$entry_color->{Color} = $h{entry_bg} - 64;
$entry_selected_color->{Color} = $h{entry_fg} - 64;
$timer_pos->{X} = $h{timer_x};
$timer_pos->{Y} = $h{timer_y};
$timer_bg->{Color} = $h{timer_bg} - 64;
$timer_fg->{Color} = $h{timer_fg} - 64;
$progress_color->{Color} = $h{progress_c} - 64;
$progress_rect->[0]{X} = $h{progress_x};
$progress_rect->[0]{Y} = $h{progress_y};
$progress_rect->[1]{X} = $h{progress_x} + $h{progress_real_w};
$progress_rect->[1]{Y} = $h{progress_y} + $h{progress_h};
$isolinux_mode = $h{isolinux_mode};
}
sub save_parameters {
my ($file) = @_;
my %h;
$h{mode} = (find { $image_size{X} eq $_->{X} } @modes)->{Vesa};
$h{clear_h} = $image_size{X};
$h{clear_w} = $image_size{Y};
if (!$isolinux_mode) {
($h{entry_x}, $h{entry_y}, $h{entry_w}, $h{entry_h}) = rectangle2xywh($entry_rect);
$h{entry_w_chr} = int($h{entry_w} / 8);
$h{entry_h_chr} = int($h{entry_h} / 16);
$h{entry_bg} = $entry_color->{Color} + 64;
$h{entry_fg} = $entry_selected_color->{Color} + 64;
$h{timer_x} = $timer_pos->{X};
$h{timer_y} = $timer_pos->{Y};
$h{timer_bg} = $timer_bg->{Color} + 64;
$h{timer_fg} = $timer_fg->{Color} + 64;
}
$h{progress_c} = $progress_color->{Color} + 64;
($h{progress_x}, $h{progress_y}, $h{progress_real_w}, $h{progress_h}) = rectangle2xywh($progress_rect);
$h{progress_w} = int($h{progress_real_w} / nb_steps());
$h{isolinux_mode} = $isolinux_mode;
output($file, map { "$_=$h{$_}\n" } sort keys %h);
}
# MAIN #########################################################################
my $usage = <<EOF;
usage: $0 [--kernel <kernel> --initrd <initrd>] [--size <size in KiB>] <image>
(kernel and initrd are used to compute the size of data to load)
EOF
GetOptions('kernel=s' => \ (my $kernel),
'initrd=s' => \ (my $initrd),
'size=s' => \$kernel_and_initrd_size,
'isolinux' => \ (my $force_isolinux_mode),
) or die $usage;
my ($file) = @ARGV;
@ARGV == 1 && -e $file or die $usage;
if ($kernel_and_initrd_size) {
$kernel and die "give kernel and initrd or size, not both\n";
} else {
if ($kernel) {
$initrd or die "give both kernel and initrd\n";
$kernel_size = (-s $kernel) / 1024 or die "bad file $kernel: $!\n";
$initrd_size = (-s $initrd) / 1024 or die "bad file $initrd: $!\n";
}
$kernel_and_initrd_size = $kernel_size + $initrd_size;
}
my $err;
$err = $magick->Read($file) and die $err;
my $bmp_file_name = $file;
$bmp_file_name =~ s/\.\w\w\w?$//;
$bmp_file_name .= '.bmp';
if ($file ne $bmp_file_name || $magick->Get('colors') > 128) {
warn "writing $bmp_file_name\n";
$err = $magick->Quantize(colors => 128, dither => 'True', treedepth => 6) and die $err;
$err = $magick->Write(filename => "bmp3:$bmp_file_name", compression => 'None') and die $err;
}
read_parameters("$bmp_file_name.parameters");
$isolinux_mode ||= $force_isolinux_mode;
my $window_widget = Gtk2::Window->new('toplevel');
$window_widget->signal_connect(destroy => sub { Gtk2->main_quit });
gtkadd($window_widget,
gtkpack(Gtk2::VBox->new(0,0), create_image_area()));
image_set_file($bmp_file_name);
$window_widget->show;
$image_area->window->set_cursor(Gtk2::Gdk::Cursor->new('crosshair'));
Gtk2->main;
save_parameters("$bmp_file_name.parameters");
|