Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rendering image markers with CairoMakie #2080

Merged
merged 19 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ end
# an array of markers is converted to string by itself, which is inconvenient for the iteration logic
_marker_convert(markers::AbstractArray) = map(m -> convert_attribute(m, key"marker"(), key"scatter"()), markers)
_marker_convert(marker) = convert_attribute(marker, key"marker"(), key"scatter"())
# image arrays need to be converted as a whole
_marker_convert(marker::AbstractMatrix{<:Colorant}) = [ convert_attribute(marker, key"marker"(), key"scatter"()) ]

function draw_atomic_scatter(scene, ctx, transfunc, colors, markersize, strokecolor, strokewidth, marker, marker_offset, rotations, model, positions, size_model, font, markerspace, space)
broadcast_foreach(positions, colors, markersize, strokecolor,
strokewidth, marker, marker_offset, remove_billboard(rotations)) do point, col,
strokewidth, marker, marker_offset, remove_billboard(rotations)) do point, col,
markersize, strokecolor, strokewidth, m, mo, rotation

scale = project_scale(scene, markerspace, markersize, size_model)
Expand Down Expand Up @@ -315,6 +317,24 @@ function draw_marker(ctx, marker::Rect, pos, scale, strokecolor, strokewidth, ma
end


function draw_marker(ctx, marker::Matrix{T}, pos, scale,
strokecolor #= unused =#, strokewidth #= unused =#,
marker_offset, rotation) where T<:Colorant

# convert marker to Cairo compatible image data
argb32_marker = convert.(ARGB32, marker)
argb32_marker = permutedims(argb32_marker, (2,1)) # swap x-y for Cairo
marker_surf = Cairo.CairoImageSurface(argb32_marker)

Cairo.translate(ctx, pos[1]+marker_offset[1], pos[2]+marker_offset[2])
Cairo.rotate(ctx, to_2d_rotation(rotation))
px_scale = scale ./ size(argb32_marker)
Cairo.scale(ctx, px_scale[1], px_scale[2])
Cairo.set_source_surface(ctx, marker_surf, 0, 0)
fatteneder marked this conversation as resolved.
Show resolved Hide resolved
Cairo.paint(ctx)
end


################################################################################
# Text #
################################################################################
Expand Down
2 changes: 0 additions & 2 deletions CairoMakie/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ excludes = Set([
"Order Independent Transparency",
"heatmap transparent colormap",
"fast pixel marker",
"Array of Images Scatter",
"Image Scatter different sizes",
"scatter with glow",
"scatter with stroke",
"heatmaps & surface"
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Allow `CairoMakie` to render `scatter` with images as markers [#2080](https://github.com/JuliaPlots/Makie.jl/pull/2080).

## v0.17.13

- Fix boundingboxes [#2184](https://github.com/JuliaPlots/Makie.jl/pull/2184).
Expand Down
20 changes: 20 additions & 0 deletions ReferenceTests/src/tests/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ end
end


@reference_test "scatter image markers" begin
pixel_types = [ RGBA, RGBAf, RGBA{Float16}, ARGB, ARGB{Float16}, RGB, RGBf, RGB{Float16} ]
rotations = [ 2pi/3 * (i-1) for i = 1:length(pixel_types) ]
s = Scene(resolution = (100+100*length(pixel_types), 400), camera = campixel!)
filename = Makie.assetpath("icon_transparent.png")
marker_image = FileIO.load(filename)
for (i, (rot, pxtype)) in enumerate(zip(rotations, pixel_types))
marker = convert.(pxtype, marker_image)
p = Point2f((i-1) * 100 + 100, 200)
scatter!(s,
p,
marker = marker,
markersize = 75,
rotations = rot,
)
end
s
end


@reference_test "basic polygon shapes" begin
s = Scene(resolution = (800, 800), camera = campixel!)
scalefactor = 70
Expand Down
1 change: 1 addition & 0 deletions WGLMakie/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ excludes = Set([
"Animated surface and wireframe",
"Array of Images Scatter",
"Image Scatter different sizes",
"scatter image markers",
"pattern barplot", # not implemented yet
"scatter with stroke",
"scatter with glow"
Expand Down