Last active
March 11, 2020 11:33
-
-
Save qtxie/20451f735d63ad6e190bdfe082f5931d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
转换的代码应该是平台无关的。Draw里只需要很少的改动。 | |
runtime/datatypes/image.reds 里增加一个函数: | |
``` | |
any-resize: func [ | |
;-- 4 points resize (also support 3 points) | |
src [red-image!] | |
dst [red-image!] | |
crop1 [red-pair!] | |
start [red-pair!] ;-- first point | |
end [red-pair!] ;-- end point | |
/local | |
h [integer!] | |
buf [int-ptr!] | |
new-buf [int-ptr!] | |
][ | |
h: 0 | |
buf: acquire-buffer src :h | |
; 做转换,结果保存再new-buf里 | |
release-buffer src :h no | |
OS-image/load-binary new-buf len | |
] | |
``` | |
Draw.reds里面: | |
``` | |
OS-draw-image: func [ | |
dc [draw-ctx!] | |
image [red-image!] | |
start [red-pair!] | |
end [red-pair!] | |
key-color [red-tuple!] | |
border? [logic!] | |
crop1 [red-pair!] | |
pattern [red-word!] | |
/local | |
img [integer!] | |
sub-img [integer!] | |
x [integer!] | |
y [integer!] | |
width [integer!] | |
height [integer!] | |
w [float32!] | |
h [float32!] | |
ww [float32!] | |
crop2 [red-pair!] | |
trans-img [red-image! value] | |
][ | |
either null? start [x: 0 y: 0][x: start/x y: start/y] | |
case [ | |
start = end [ | |
width: IMAGE_WIDTH(image/size) | |
height: IMAGE_HEIGHT(image/size) | |
] | |
start + 1 = end [ ;-- two control points | |
width: end/x - x | |
height: end/y - y | |
] | |
any [start + 2 = end start + 3 = end][ ;-- 三/四个点的情况。 | |
image/any-resize image trans-img crop1 start end | |
image: trans-img | |
crop1: null ;-- any-resize already handled crop | |
] | |
true [0] | |
] | |
img: OS-image/to-cgimage image | |
if crop1 <> null [ | |
crop2: crop1 + 1 | |
w: as float32! crop2/x | |
h: as float32! crop2/y | |
ww: w / h * (as float32! height) | |
width: as-integer ww | |
sub-img: CGImageCreateWithImageInRect | |
img | |
as float32! crop1/x | |
as float32! crop1/y | |
w | |
h | |
img: sub-img | |
] | |
CG-draw-image dc/raw img x y width height | |
if crop1 <> null [CGImageRelease img] | |
] | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment