/
to_c.rb
103 lines (82 loc) · 2.15 KB
/
to_c.rb
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
class Magick::RVG
private
def header_text(pgm, name)
pgm.puts <<"END_HEADER"
/*
Version: #{Magick_version}
gcc `Magick-config --cflags --cppflags` #{name}.c `Magick-config --ldflags --libs` -o #{name}
*/
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <magick/api.h>
int main(int argc,char **argv)
{
Image *image;
ImageInfo *info;
DrawInfo *draw;
const char * const primitives =
END_HEADER
end
def list_primitives(pgm, gc)
primitives = gc.inspect.split("\n")
indent = 0
primitives.each do |cmd|
indent -= 1 if cmd['pop ']
pgm.print(' ', (' '*indent), '"', cmd, '\n"', "\n")
indent += 1 if cmd['push ']
end
end
def trailer_text(pgm, name)
pgm.puts <<"END_TRAILER"
;
InitializeMagick("#{name}");
info = CloneImageInfo(NULL);
if (!info)
{
MagickError(ResourceLimitError,"Unable to allocate ImageInfo",
"Memory allocation failed");
}
info->size = AcquireMagickMemory(20);
sprintf(info->size, "%dx%d", #{@width.to_i}, #{@height.to_i});
image = AllocateImage(info);
if (!image)
{
MagickError(ResourceLimitError,"Unable to allocate Image",
"Memory allocation failed");
}
SetImage(image, OpaqueOpacity);
draw = CloneDrawInfo(info, NULL);
CloneString(&(draw->primitive), primitives);
DrawImage(image, draw);
if (image->exception.severity != UndefinedException)
{
printf("%s: %s", image->exception.reason, image->exception.description);
exit(1);
}
strcpy(image->filename, "#{name+".gif"}");
WriteImage(info, image);
DestroyDrawInfo(draw);
DestroyImage(image);
DestroyImageInfo(info);
DestroyMagick();
return 0;
}
END_TRAILER
end
public
# Convert an RVG object to a stand-alone C program
# suitable for reproducing a bug.
def to_c(name)
pgm = File.open(name+".c", "w")
header_text(pgm, name)
gc = Draw.new
add_primitives(gc)
list_primitives(pgm, gc)
trailer_text(pgm, name)
pgm.close
$stderr.puts "Done"
end
end # class Magick::RVG