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

Help obtaining accurate data values from ImageSurface.data with alpha values #333

Closed
cormullion opened this issue Aug 21, 2020 · 1 comment

Comments

@cormullion
Copy link
Member

Hoping you can help me work out how to use Cairo.jl to get better code for this. I'm trying to extract the data from a copied image surface. The values match if alpha is 1, but not if alpha is less than 1.0.

Here's the MWE:

using Cairo

alpha = 0.5
rgba(k) = reinterpret(UInt8,[k]) ./ 0xFF

############### source image

surface = CairoARGBSurface(256, 256)
cr = CairoContext(surface)

save(cr)
set_source_rgba(cr, 0.8, 0.8, 0.8, alpha)    
rectangle(cr, 0.0, 0.0, 256.0, 256.0) 
fill(cr)
restore(cr)

save(cr)
set_source_rgba(cr, 1.0, 0.5, 0.0, alpha)    
rectangle(cr, 64.0, 64.0, 64.0, 64.0) 
fill(cr)
restore(cr)

save(cr)
set_source_rgba(cr, 0.0, 1.0, 1.0, alpha)    
rectangle(cr, 100.0, 100.0, 64.0, 64.0) 
fill(cr)
restore(cr)

Cairo.write_to_png(surface,"/tmp/image1.png")

############## make a copy

z = zeros(UInt32, 256, 256)
imagesurface = CairoImageSurface(z, Cairo.FORMAT_ARGB32)
crdest = CairoContext(imagesurface)

set_source_surface(crdest, surface, 0, 0)
set_operator(crdest, Cairo.OPERATOR_SOURCE)
paint(crdest)

data = imagesurface.data              # this works

println(rgba(data[1]))                # but values are different

Cairo.write_to_png(imagesurface,"/tmp/image2.png")

If alpha is 1.0, the value printed is [0.8, 0.8, 0.8, 1.0], matching the first pixel's value, but if alpha is 0.5, the printed value is [0.4, 0.4, 0.4, 0.5019607843137255].

What I want to do is copy an RGB image to a matrix with accurate values. I think I'm missing some information about copying alpha color values...

@lobingera
Copy link
Contributor

Recomended read https://cairographics.org/operators/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants