Cairo: How can I display an OpaqueImage?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Cairo: How can I display an OpaqueImage?

Mark Plas

Hi,

 

How do you display an OpaqueImage using Cairo in vw7.7?

 

I've put some code together that can display an Image, but I haven't found anything easy to display an OpaqueImage.

 

Here's some example workspace code I use to display an Image:

 

| view gc |

view := ScheduledControllers activeController view.

gc := view graphicsContext.

gc newCairoContextWhile: [:cr |

                img := BrowserListEntry CcanvasIcon image.

                pixmap := Pixmap extent: img extent.

                img displayOn: pixmap graphicsContext.

                pixmap newCairoSurfaceWhile: [:surface |

                               pattern := surface asPattern.

                               pattern extend: (ExtendStyle perform: #repeat).

                               cr source: pattern.

                               cr rectangle: view bounds.

                               cr fill.

]].

 

 But how can I display an OpaqueImage? I can think of a way of building Depth32Images by hand and using the alpha value as the mask, but it seems laborious and perhaps it exists somewhere in the Cairo library but I cannot find it...

 

And is the way I'm displaying an image in my example the 'recommended' way?

 

Thanks,

Mark


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Cairo: How can I display an OpaqueImage?

Travis Griggs-3

On Apr 23, 2010, at 12:12 AM, Mark Plas wrote:

Hi,
 
How do you display an OpaqueImage using Cairo in vw7.7?
 
I've put some code together that can display an Image, but I haven't found anything easy to display an OpaqueImage.
 
Here's some example workspace code I use to display an Image:
 
| view gc |
view := ScheduledControllers activeController view.
gc := view graphicsContext.
gc newCairoContextWhile: [:cr |
                img := BrowserListEntry CcanvasIcon image.
                pixmap := Pixmap extent: img extent.
                img displayOn: pixmap graphicsContext.
                pixmap newCairoSurfaceWhile: [:surface |
                               pattern := surface asPattern.
                               pattern extend: (ExtendStyle perform: #repeat).
                               cr source: pattern.
                               cr rectangle: view bounds.
                               cr fill.
]].
 
 But how can I display an OpaqueImage? I can think of a way of building Depth32Images by hand and using the alpha value as the mask, but it seems laborious and perhaps it exists somewhere in the Cairo library but I cannot find it...
 
And is the way I'm displaying an image in my example the 'recommended' way?

If you want to fill/tile a whole area with the image, then yes.

I've had mixed results doing what you're doing, mixed in the sense of different results on different platforms. Creating Cairo surfaces/contexts around VW graphics objects works well of course, but slapping a cairo surface around them, and then using that surface for other cairo surface<->surface operations as if it had been built up by Cairo, seems like it had issues once upon a time.

If it were me, I'd probably just sit down and write an asCairoImage extension on OpaqueImage. Brute force and cheesy, but then... drawing binary opaque images with Cairo is kinda cheesy too. :)

"not actually tested or even compiled, just coded here in mail"
asCairoImage
image := ImageSurface format: CairoFormat argb32 extent: self extent.
clear := mask palette indexOfPaintNearest: CoverageValue transparent.
image newCairoContextWhile: [:cr | 
0 to: self height - 1 do: [:y |
0 to: self width - 1 do: [:x |
(shape atX: x y: y) = clear ifFalse: [
cr source: (figure valueAtPoint: x @ y);
rectangle: (x @ y extent: 1 @ 1);
fill]]]].
^image

Chuckled at the way you got around the annoying compiler warnings about 'repeat'. :) I really need to do something about that.

--
Travis Griggs
Objologist
My Other Machine runs OSX. But then... so does this one.




_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Cairo: How can I display an OpaqueImage?

Mark Plas

Thanks Travis,

 

I'll try this out later and see how it works. Given your experiences with things not working too well when mixing VW & Cairo surfaces, would you suggest to make a similar method on Image that also avoids the Pixmap stuff?

 

" Chuckled at the way you got around the annoying compiler warnings about 'repeat'. :) I really need to do something about that.

"

 

 yes, a slightly different name would be nice :^)

 

Best regards,

Mark

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Travis Griggs
Sent: vrijdag 23 april 2010 10:29
To: VWNC NC
Subject: Re: [vwnc] Cairo: How can I display an OpaqueImage?

 

 

On Apr 23, 2010, at 12:12 AM, Mark Plas wrote:



Hi,

 

How do you display an OpaqueImage using Cairo in vw7.7?

 

I've put some code together that can display an Image, but I haven't found anything easy to display an OpaqueImage.

 

Here's some example workspace code I use to display an Image:

 

| view gc |

view := ScheduledControllers activeController view.

gc := view graphicsContext.

gc newCairoContextWhile: [:cr |

                img := BrowserListEntry CcanvasIcon image.

                pixmap := Pixmap extent: img extent.

                img displayOn: pixmap graphicsContext.

                pixmap newCairoSurfaceWhile: [:surface |

                               pattern := surface asPattern.

                               pattern extend: (ExtendStyle perform: #repeat).

                               cr source: pattern.

                               cr rectangle: view bounds.

                               cr fill.

]].

 

 But how can I display an OpaqueImage? I can think of a way of building Depth32Images by hand and using the alpha value as the mask, but it seems laborious and perhaps it exists somewhere in the Cairo library but I cannot find it...

 

And is the way I'm displaying an image in my example the 'recommended' way?

 

If you want to fill/tile a whole area with the image, then yes.

 

I've had mixed results doing what you're doing, mixed in the sense of different results on different platforms. Creating Cairo surfaces/contexts around VW graphics objects works well of course, but slapping a cairo surface around them, and then using that surface for other cairo surface<->surface operations as if it had been built up by Cairo, seems like it had issues once upon a time.

 

If it were me, I'd probably just sit down and write an asCairoImage extension on OpaqueImage. Brute force and cheesy, but then... drawing binary opaque images with Cairo is kinda cheesy too. :)

 

"not actually tested or even compiled, just coded here in mail"

asCairoImage

            image := ImageSurface format: CairoFormat argb32 extent: self extent.

            clear := mask palette indexOfPaintNearest: CoverageValue transparent.

            image newCairoContextWhile: [:cr | 

                        0 to: self height - 1 do: [:y |

                                   0 to: self width - 1 do: [:x |

                                               (shape atX: x y: y) = clear ifFalse: [

                                                           cr source: (figure valueAtPoint: x @ y);

                                                           rectangle: (x @ y extent: 1 @ 1);

                                                           fill]]]].

            ^image

 

Chuckled at the way you got around the annoying compiler warnings about 'repeat'. :) I really need to do something about that.

 

--

Travis Griggs

Objologist

My Other Machine runs OSX. But then... so does this one.



 

 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Cairo: How can I display an OpaqueImage?

Travis Griggs-3
On Apr 23, 2010, at 2:04 AM, Mark Plas wrote:

Thanks Travis,
 
I'll try this out later and see how it works. Given your experiences with things not working too well when mixing VW & Cairo surfaces, would you suggest to make a similar method on Image that also avoids the Pixmap stuff?

Personally, yes I would. It's nice when it just works of course. But I've been round and round and round with idosyncratic differences in image formats between all of these, that it's just easier to make the conversion very absolute.

A great example of this, is even the AlphaCompositedImage that's in VW, as kind of a stop gap to allow us to use some semi modern looking icons. You'd think the two were compatible, given the appropriate byte swizzling. But no, Cairo images with alpha information have premultiplied color values. Whereas AlphaCompositedImage is more like what you find in a PNG file.

And VW binary images are even funner. What could go wrong there? Well, they both pad the same on row byte boundaries, that was nice. But their bit endian backwards from each other. So to convert a VW bw image to Cairo, you basically have to mirror flip the bits of each byte.

Both of these were originally sleuthed out by John Brant, not me.

--
Travis Griggs
Objologist
"No other topic generates more heat and less light than code formatting" --Kent Beck


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc