-
Notifications
You must be signed in to change notification settings - Fork 4
/
sipskia.cpp
207 lines (166 loc) · 7.51 KB
/
sipskia.cpp
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
#include "SkCanvas.h"
#include "SkCommandLineFlags.h"
#include "SkData.h"
#include "SkDocument.h"
#include "SkGraphics.h"
#include "SkSurface.h"
#include "SkImage.h"
#include "SkImageFilter.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkPaint.h"
#include "SkImageGenerator.h"
#include <boost/python.hpp>
#include <stdexcept>
#ifdef SIPSKIA_BGRA
#define SIPSKIA_COLORTYPE kBGRA_8888_SkColorType
#else
#define SIPSKIA_COLORTYPE kN32_SkColorType
#endif
boost::python::object convert_webp(PyObject* raw, long canvasSize, int quality) {
int size = Py_SIZE(raw);
void* data = (void*)PyBytes_AsString(raw);
sk_sp<SkData> skdata(SkData::MakeWithoutCopy(data, size));
if(!data) return boost::python::object();
sk_sp<SkImage> image = SkImage::MakeFromEncoded(skdata);
if(!image) return boost::python::object();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
paint.setFilterQuality(kHigh_SkFilterQuality);
float left = 0.0f;
float right = 0.0f;
float top = 0.0f;
float bottom = 0.0f;
int imageWidth = image->width();
int imageHeight = image->height();
if(imageWidth > imageHeight) {
top = 0.0f;
bottom = imageHeight;
left = (imageWidth - imageHeight)/2.0f;
right = left + imageHeight;
}else{
left = 0.0f;
right = imageWidth;
top = (imageHeight - imageWidth)/2.0f;
bottom = top + imageWidth;
}
sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::Make(canvasSize, canvasSize, SIPSKIA_COLORTYPE, kPremul_SkAlphaType, nullptr)).release());
SkCanvas* canvas = surface->getCanvas();
canvas->drawImageRect(image, SkRect::MakeLTRB(left, top, right, bottom), SkRect::MakeXYWH(0.0f, 0.0f, canvasSize, canvasSize), &paint);
sk_sp<SkImage> img(surface->makeImageSnapshot());
if(!img) return boost::python::object();
sk_sp<SkData> webp(img->encodeToData(SkEncodedImageFormat::kWEBP, quality));
if(!webp) return boost::python::object();
return boost::python::object(boost::python::handle<>(PyBytes_FromStringAndSize((char*)webp->data(), webp->size())));
}
boost::python::object convert_jpg(PyObject* raw, long canvasSize, int quality) {
int size = Py_SIZE(raw);
void* data = (void*)PyBytes_AsString(raw);
// SkBitmap bm;
sk_sp<SkData> skdata(SkData::MakeWithoutCopy(data, size));
if(!data) return boost::python::object();
sk_sp<SkImage> image = SkImage::MakeFromEncoded(skdata);
if(!image) return boost::python::object();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
paint.setFilterQuality(kHigh_SkFilterQuality);
long canvasHeight = (long)((image->height() * canvasSize) / image->width());
if(canvasHeight < 1){
canvasHeight = 1;
}
sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::Make(canvasSize, canvasHeight, SIPSKIA_COLORTYPE, kPremul_SkAlphaType, nullptr)).release());
SkCanvas* canvas = surface->getCanvas();
canvas->drawImageRect(image, SkRect::MakeLTRB(0.0f, 0.0f, image->width(), image->height()), SkRect::MakeXYWH(0.0f, 0.0f, canvasSize, canvasHeight), &paint);
sk_sp<SkImage> img(surface->makeImageSnapshot());
if(!img) return boost::python::object();
sk_sp<SkData> jpg(img->encodeToData(SkEncodedImageFormat::kJPEG, quality));
if(!jpg) return boost::python::object();
return boost::python::object(boost::python::handle<>(PyBytes_FromStringAndSize((char*)jpg->data(), jpg->size())));
}
boost::python::object convert_origin_webp(PyObject* raw, int quality) {
int size = Py_SIZE(raw);
void* data = (void*)PyBytes_AsString(raw);
sk_sp<SkData> skdata(SkData::MakeWithoutCopy(data, size));
if(!data) return boost::python::object();
sk_sp<SkImage> image = SkImage::MakeFromEncoded(skdata);
if(!image) return boost::python::object();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
paint.setFilterQuality(kHigh_SkFilterQuality);
float canvasWidth = image->width();
float canvasHeight = image->height();
float left = 0.0f;
float right = canvasWidth;
float top = 0.0f;
float bottom = canvasHeight;
sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::Make(canvasWidth, canvasHeight, SIPSKIA_COLORTYPE, kPremul_SkAlphaType, nullptr)).release());
SkCanvas* canvas = surface->getCanvas();
canvas->drawImageRect(image, SkRect::MakeLTRB(left, top, right, bottom), SkRect::MakeXYWH(0.0f, 0.0f, canvasWidth, canvasHeight), &paint);
sk_sp<SkImage> img(surface->makeImageSnapshot());
if(!img) return boost::python::object();
sk_sp<SkData> webp(img->encodeToData(SkEncodedImageFormat::kWEBP, quality));
if(!webp) return boost::python::object();
return boost::python::object(boost::python::handle<>(PyBytes_FromStringAndSize((char*)webp->data(), webp->size())));
}
boost::python::object convert_origin_jpg(PyObject* raw, int quality) {
int size = Py_SIZE(raw);
void* data = (void*)PyBytes_AsString(raw);
sk_sp<SkData> skdata(SkData::MakeWithoutCopy(data, size));
if(!data) return boost::python::object();
sk_sp<SkImage> image = SkImage::MakeFromEncoded(skdata);
if(!image) return boost::python::object();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
paint.setFilterQuality(kHigh_SkFilterQuality);
float canvasWidth = image->width();
float canvasHeight = image->height();
float left = 0.0f;
float right = canvasWidth;
float top = 0.0f;
float bottom = canvasHeight;
sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::Make(canvasWidth, canvasHeight, SIPSKIA_COLORTYPE, kPremul_SkAlphaType, nullptr)).release());
SkCanvas* canvas = surface->getCanvas();
canvas->drawImageRect(image, SkRect::MakeLTRB(left, top, right, bottom), SkRect::MakeXYWH(0.0f, 0.0f, canvasWidth, canvasHeight), &paint);
sk_sp<SkImage> img(surface->makeImageSnapshot());
if(!img) return boost::python::object();
sk_sp<SkData> webp(img->encodeToData(SkEncodedImageFormat::kJPEG, quality));
if(!webp) return boost::python::object();
return boost::python::object(boost::python::handle<>(PyBytes_FromStringAndSize((char*)webp->data(), webp->size())));
}
boost::python::object convert_with_crop_jpg(PyObject* raw, long cropWidth, long cropHeight, long canvasWidth, long canvasHeight, int quality) {
int size = Py_SIZE(raw);
void* data = (void*)PyBytes_AsString(raw);
sk_sp<SkData> skdata(SkData::MakeWithoutCopy(data, size));
if(!data) return boost::python::object();
sk_sp<SkImage> image = SkImage::MakeFromEncoded(skdata);
if(!image) return boost::python::object();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
paint.setFilterQuality(kHigh_SkFilterQuality);
float width = cropWidth;
float height = cropHeight;
float top = image->height() / 2.0f - height / 2.0f;
float bottom = top + height;
sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::Make(canvasWidth, canvasHeight, SIPSKIA_COLORTYPE, kPremul_SkAlphaType, nullptr)).release());
SkCanvas* canvas = surface->getCanvas();
canvas->drawImageRect(image, SkRect::MakeLTRB(0, top, width, bottom), SkRect::MakeXYWH(0.0f, 0.0f, canvasWidth, canvasHeight), &paint);
sk_sp<SkImage> img(surface->makeImageSnapshot());
if(!img) return boost::python::object();
sk_sp<SkData> webp(img->encodeToData(SkEncodedImageFormat::kJPEG, quality));
if(!webp) return boost::python::object();
return boost::python::object(boost::python::handle<>(PyBytes_FromStringAndSize((char*)webp->data(), webp->size())));
}
using namespace boost::python;
BOOST_PYTHON_MODULE(sipskia)
{
def("convert_webp", &convert_webp);
def("convert_jpg", &convert_jpg);
def("convert_origin_webp", &convert_origin_webp);
def("convert_origin_jpg", &convert_origin_jpg);
def("convert_with_crop_jpg", &convert_with_crop_jpg);
}