-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmapnik_image_save.cpp
More file actions
153 lines (140 loc) · 3.84 KB
/
mapnik_image_save.cpp
File metadata and controls
153 lines (140 loc) · 3.84 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
// mapnik
#include <mapnik/color.hpp> // for color
#include <mapnik/image.hpp> // for image types
#include <mapnik/image_any.hpp> // for image_any
#include <mapnik/image_util.hpp> // for save_to_string, guess_type, etc
#include "mapnik_image.hpp"
namespace {
struct AsyncSave : Napi::AsyncWorker
{
using Base = Napi::AsyncWorker;
// ctor
AsyncSave(image_ptr const& image, std::string const& filename, std::string const& format, Napi::Function const& callback)
: Base(callback),
image_(image),
filename_(filename),
format_(format)
{
}
void Execute() override
{
try
{
mapnik::save_to_file(*image_, filename_, format_);
}
catch (std::exception const& ex)
{
SetError(ex.what());
}
}
private:
image_ptr image_;
std::string filename_;
std::string format_;
};
} // namespace
/**
* Encode this image and save it to disk as a file.
*
* @name saveSync
* @param {string} filename
* @param {string} [format=png]
* @instance
* @memberof Image
* @example
* img.saveSync('foo.png');
*/
void Image::saveSync(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
if (info.Length() == 0 || !info[0].IsString())
{
Napi::TypeError::New(env, "filename required to save file").ThrowAsJavaScriptException();
return;
}
std::string filename = info[0].As<Napi::String>();
std::string format("");
if (info.Length() >= 2)
{
if (!info[1].IsString())
{
Napi::TypeError::New(env, "both 'filename' and 'format' arguments must be strings").ThrowAsJavaScriptException();
return;
}
format = info[1].As<Napi::String>();
}
else
{
format = mapnik::guess_type(filename);
if (format == "<unknown>")
{
std::ostringstream s("");
s << "unknown output extension for: " << filename << "\n";
Napi::Error::New(env, s.str().c_str()).ThrowAsJavaScriptException();
return;
}
}
try
{
mapnik::save_to_file(*image_, filename, format);
}
catch (std::exception const& ex)
{
Napi::Error::New(env, ex.what()).ThrowAsJavaScriptException();
}
}
/**
* Encode this image and save it to disk as a file.
*
* @name save
* @param {string} filename
* @param {string} [format=png]
* @param {Function} callback
* @instance
* @memberof Image
* @example
* img.save('image.png', 'png', function(err) {
* if (err) throw err;
* // your custom code
* });
*/
void Image::save(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
if (info.Length() == 0 || !info[0].IsString())
{
Napi::TypeError::New(env, "filename required to save file").ThrowAsJavaScriptException();
return;
}
if (!info[info.Length() - 1].IsFunction())
{
return saveSync(info);
}
// ensure callback is a function
Napi::Value callback_val = info[info.Length() - 1];
std::string filename = info[0].As<Napi::String>();
std::string format("");
if (info.Length() >= 3)
{
if (!info[1].IsString())
{
Napi::TypeError::New(env, "both 'filename' and 'format' arguments must be strings").ThrowAsJavaScriptException();
}
format = info[1].As<Napi::String>();
}
else
{
format = mapnik::guess_type(filename);
if (format == "<unknown>")
{
std::ostringstream s("");
s << "unknown output extension for: " << filename << "\n";
Napi::Error::New(env, s.str().c_str()).ThrowAsJavaScriptException();
return;
}
}
Napi::Function callback = callback_val.As<Napi::Function>();
auto* worker = new AsyncSave(image_, filename, format, callback);
worker->Queue();
return;
}