Skip to content

Commit

Permalink
DPI metadata for png (#4812)
Browse files Browse the repository at this point in the history
* remove special cairomakie png method

* convert px_per_unit to dpi and store in png

* bump pngfiles compat to version introducing dpi keyword

* add changelog

* fix test by always going to rgba

* retrieve px_per_unit in more robust way
  • Loading branch information
jkrumbiegel authored Feb 20, 2025
1 parent 60d3fef commit 79e2449
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Changed `inspectable` to be inherited from the parent scenes theme. [#4739](https://github.com/MakieOrg/Makie.jl/pull/4739)
- Reverted change to `poly` which disallowed 3D geometries from being plotted [#4738](https://github.com/MakieOrg/Makie.jl/pull/4738)
- Enabled autocompletion on Block types, e.g. `?Axis.xti...` [#4786](https://github.com/MakieOrg/Makie.jl/pull/4786)
- Added `dpi` metadata to all rendered png files, where `px_per_unit = 1` means 96dpi, `px_per_unit = 2` means 192dpi, and so on. This gives frontends a chance to show plain Makie png images with the correct scaling [#4812](https://github.com/MakieOrg/Makie.jl/pull/4812).

## [0.22.1] - 2025-01-17

Expand Down
7 changes: 0 additions & 7 deletions CairoMakie/src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ function Makie.backend_show(screen::Screen{EPS}, io::IO, ::MIME"application/post
return screen
end

function Makie.backend_show(screen::Screen{IMAGE}, io::IO, ::MIME"image/png", scene::Scene)
Makie.push_screen!(scene, screen)
cairo_draw(screen, scene)
Cairo.write_to_png(screen.surface, io)
return screen
end

# Disabling mimes and showable

const DISABLED_MIMES = Set{String}()
Expand Down
3 changes: 3 additions & 0 deletions CairoMakie/src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ function Makie.apply_screen_config!(screen::Screen, config::ScreenConfig, scene:
Makie.apply_screen_config!(screen, config, scene, nothing, MIME"image/png"())
end

function Makie.px_per_unit(s::Screen)::Float64
return s.config.px_per_unit
end

function Screen(scene::Scene; screen_config...)
config = Makie.merge_screen_config(ScreenConfig, Dict{Symbol, Any}(screen_config))
Expand Down
6 changes: 6 additions & 0 deletions GLMakie/src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ function Screen(;
return screen
end

function Makie.px_per_unit(s::Screen)::Float64
config = s.config
config === nothing && return 1.0
return something(config.px_per_unit, 1.0)
end

function set_screen_visibility!(screen::Screen, visible::Bool)
if !screen.owns_glscreen
error(unimplemented_error)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Markdown = "1.0, 1.6"
MathTeXEngine = "0.5, 0.6"
Observables = "0.5.5"
OffsetArrays = "1"
PNGFiles = "0.4.3"
PNGFiles = "0.4.4"
Packing = "0.5"
PlotUtils = "1.4.2"
PolygonOps = "0.1.1"
Expand Down
7 changes: 3 additions & 4 deletions ReferenceTests/src/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ function get_frames(a, b)
return (get_frames(a), get_frames(b))
end

rgbf_convert(x::AbstractMatrix{<:RGB}) = convert(Matrix{RGBf}, x)
rgbf_convert(x::AbstractMatrix{<:RGBA}) = convert(Matrix{RGBAf}, x)
rgbaf_convert(x::AbstractMatrix{<:Union{RGB,RGBA}}) = convert(Matrix{RGBAf}, x)

function get_frames(video::AbstractString)
mktempdir() do folder
Expand All @@ -25,8 +24,8 @@ end

function compare_images(a::AbstractMatrix{<:Union{RGB,RGBA}}, b::AbstractMatrix{<:Union{RGB,RGBA}})

a = rgbf_convert(a)
b = rgbf_convert(b)
a = rgbaf_convert(a)
b = rgbaf_convert(b)

if size(a) != size(b)
@warn "images don't have the same size, difference will be Inf"
Expand Down
4 changes: 4 additions & 0 deletions WGLMakie/src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ mutable struct Screen <: Makie.MakieScreen
end
end

function Makie.px_per_unit(s::Screen)::Float64
return something(s.config.px_per_unit, 1.0)
end

function Screen(; config...)
config = Makie.merge_screen_config(ScreenConfig, Dict{Symbol,Any}(config))
return Screen(nothing, config)
Expand Down
6 changes: 5 additions & 1 deletion src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,14 @@ function colorbuffer(fig::FigureLike, format::ImageStorageFormat = JuliaNative;
end
end

px_per_unit(screen::MakieScreen)::Float64 = 1.0 # fallback for backends who don't have upscaling

# Fallback for any backend that will just use colorbuffer to write out an image
function backend_show(screen::MakieScreen, io::IO, ::MIME"image/png", scene::Scene)
img = colorbuffer(screen)
FileIO.save(FileIO.Stream{FileIO.format"PNG"}(Makie.raw_io(io)), img)
px_per_unit = Makie.px_per_unit(screen)::Float64
dpi = px_per_unit * 96 # attach dpi metadata corresponding to 1 unit == 1 CSS pixel
FileIO.save(FileIO.Stream{FileIO.format"PNG"}(Makie.raw_io(io)), img; dpi)
return
end

Expand Down

0 comments on commit 79e2449

Please sign in to comment.