Skip to content

Commit 54f1003

Browse files
authored
Add image drawing support (#14)
This PR adds basic wrapping of the blContextBlitImageD and blContextBlitScaledImageD functions.
1 parent 469cdd4 commit 54f1003

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ __pycache__/
99
blend2d/*.so
1010
src/*.c
1111
*.py[cd]
12+
*.png

examples/image.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import blend2d
2+
import numpy as np
3+
from skimage.io import imsave
4+
5+
6+
def random_lines(context, width, height):
7+
xs = np.random.randint(0, high=width, size=(100, 2))
8+
ys = np.random.randint(0, high=height, size=(100, 2))
9+
colors = np.random.random_sample(size=(100, 3))
10+
11+
context.set_fill_style((1.0, 1.0, 1.0))
12+
context.fill_all()
13+
14+
path = blend2d.Path()
15+
for (x0, x1), (y0, y1), color in zip(xs, ys, colors):
16+
context.set_stroke_style(color)
17+
path.reset()
18+
path.move_to(x0, y0)
19+
path.line_to(x1, y1)
20+
context.stroke_path(path)
21+
22+
23+
if __name__ == '__main__':
24+
image_array = np.empty((256, 256, 4), dtype=np.uint8)
25+
image = blend2d.Image(image_array)
26+
image_context = blend2d.Context(image)
27+
random_lines(image_context, 256, 256)
28+
29+
array = np.empty((512, 512, 4), dtype=np.uint8)
30+
context = blend2d.Context(blend2d.Image(array))
31+
context.clear()
32+
33+
src = blend2d.Rect(0, 0, *image_array.shape[:2])
34+
dst = blend2d.Rect(0, 0, 100, 100)
35+
context.blit_scaled_image(dst, image, src)
36+
37+
context.rotate(-np.pi/12)
38+
context.blit_image((10.0, 100.0), image, src)
39+
40+
imsave('image.png', array)

src/context.pxi

+18
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ cdef class Context:
244244
def set_stroke_width(self, float width):
245245
_capi.blContextSetStrokeWidth(&self._self, width)
246246

247+
def blit_image(self, position, Image img, Rect img_area):
248+
cdef:
249+
_capi.BLPoint point
250+
_capi.BLRectI rect
251+
252+
point.x = position[0]; point.y = position[1]
253+
rect.x = <int>img_area._self.x; rect.y = <int>img_area._self.y
254+
rect.w = <int>img_area._self.w; rect.h = <int>img_area._self.h
255+
_capi.blContextBlitImageD(&self._self, &point, &img._self, &rect)
256+
257+
def blit_scaled_image(self, Rect rect, Image img, Rect img_area):
258+
cdef:
259+
_capi.BLRectI dst_rect
260+
261+
dst_rect.x = <int>img_area._self.x; dst_rect.y = <int>img_area._self.y
262+
dst_rect.w = <int>img_area._self.w; dst_rect.h = <int>img_area._self.h
263+
_capi.blContextBlitScaledImageD(&self._self, &rect._self, &img._self, &dst_rect)
264+
247265
def fill_all(self):
248266
_capi.blContextFillAll(&self._self)
249267

0 commit comments

Comments
 (0)