forked from openfl/lime
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageHelper.hx
More file actions
177 lines (145 loc) · 3.72 KB
/
ImageHelper.hx
File metadata and controls
177 lines (145 loc) · 3.72 KB
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
package lime.tools;
import hxp.*;
#if (lime && lime_cffi && !macro)
import lime.graphics.Image;
import lime.graphics.ImageBuffer;
import lime.utils.UInt8Array;
#end
import lime.tools.Platform;
import sys.io.File;
import sys.io.FileSeek;
import sys.FileSystem;
class ImageHelper
{
public static function rasterizeSVG(path:String, width:Int, height:Int,
backgroundColor:Int = null):#if (lime && lime_cffi && !macro) Image #else Dynamic #end
{
// public static function rasterizeSVG (svg:Dynamic /*SVG*/, width:Int, height:Int, backgroundColor:Int = null):Image {
#if (lime && lime_cffi && !macro)
if (path == null) return null;
var temp = System.getTemporaryFile(".png");
try
{
System.runCommand("", "neko", [
Path.combine(Haxelib.getPath(new Haxelib(#if lime "lime" #else "hxp" #end)), "svg.n"),
"process",
path,
Std.string(width),
Std.string(height),
temp
], true, true);
if (FileSystem.exists(temp))
{
var image = Image.fromFile(temp);
try
{
FileSystem.deleteFile(temp);
}
catch (e:Dynamic) {}
if (image.buffer != null)
{
return image;
}
}
}
catch (e:Dynamic) {}
var rasterizer = Haxelib.getPath(new Haxelib("lime")) + "/templates/bin/batik/batik-rasterizer.jar";
var args = [
"-Dapple.awt.UIElement=true",
"-jar",
rasterizer,
"-d",
temp,
"-w",
Std.string(width),
"-h",
Std.string(height)
];
if (backgroundColor != null)
{
var a:Int = ((backgroundColor >> 24) & 0xFF);
var r:Int = ((backgroundColor >> 16) & 0xFF);
var g:Int = ((backgroundColor >> 8) & 0xFF);
var b:Int = (backgroundColor & 0xFF);
args.push("-bg");
args.push(a + "." + r + "." + g + "." + b);
}
args.push(path);
if (System.hostPlatform == MAC)
{
try
{
var found = false;
if (FileSystem.exists("/System/Library/Java/JavaVirtualMachines"))
{
found = (FileSystem.readDirectory("/System/Library/Java/JavaVirtualMachines").length > 0);
}
if (!found && FileSystem.exists("/Library/Java/JavaVirtualMachines"))
{
found = (FileSystem.readDirectory("/Library/Java/JavaVirtualMachines").length > 0);
}
if (!found)
{
if (Log.verbose) Log.warn("Skipping SVG to PNG rasterization step, no Java runtime detected");
return null;
}
}
catch (e:Dynamic) {}
}
if (Log.verbose)
{
System.runCommand("", "java", args, true, true);
}
else
{
System.runProcess("", "java", args, true, true, true);
}
if (FileSystem.exists(temp))
{
var image = Image.fromFile(temp);
try
{
FileSystem.deleteFile(temp);
}
catch (e:Dynamic) {}
if (image.buffer != null)
{
return image;
}
}
#end
return null;
}
public static function readPNGImageSize(path:String)
{
var toReturn = {width: 0, height: 0};
var fileInput = File.read(path);
var header = (fileInput.readByte() << 8) | fileInput.readByte();
if (header == 0x8950)
{
fileInput.seek(8 + 4 + 4, FileSeek.SeekBegin);
var width = (fileInput.readByte() << 24) | (fileInput.readByte() << 16) | (fileInput.readByte() << 8) | fileInput.readByte();
var height = (fileInput.readByte() << 24) | (fileInput.readByte() << 16) | (fileInput.readByte() << 8) | fileInput.readByte();
toReturn =
{
width: width,
height: height
};
}
fileInput.close();
return toReturn;
}
public static function resizeImage(image:#if (lime && lime_cffi && !macro) Image #else Dynamic #end, width:Int,
height:Int):#if (lime && lime_cffi && !macro) Image #else Dynamic #end
{
#if (lime && lime_cffi && !macro)
if (image == null) return null;
if (image.width == width && image.height == height)
{
return image;
}
image.resize(width, height);
#end
return image;
}
}