# FBO Antialiasing / Format

**URL:** <https://discourse.libcinder.org/t/fbo-antialiasing-format/169>\
**Category:** Using Cinder\
**Created:** [July 21, 2016, 7:57pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169 "2016-07-21T19:57:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 21, 2016, 7:57pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/1 "2016-07-21T19:57:36Z")

</div>

I’m seeing some artifacts when I draw my Batch object to an FBO, and then drawing that FBO’s texture to screen (see attached screenshot).

I’m setting up the FBO like this:

`gl::Fbo::Format designFboFormat; designFboFormat.setSamples(4); mDesignFbo = gl::Fbo::create(getWindowWidth(), getWindowWidth(), gl::Fbo::Format());`

The BatchRef is created with a VboMesh made from triangulating an SVG document. I’m clearing the FBO with a transparent color before drawing the Batch object to it. Is there a different format that would result in me seeing the same thing whether I’m drawing to the FBO or not?

This screenshot is a result of drawing the Batch object the exact same way, once to the FBO and once directly to the screen. You can see the top version isn’t quite the same color and it has almost a slight border around the edges (not intentional).

Thanks!

 ![](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/fc4425ddf751692bea8e8f8f234ec0b0171bc743.png)

---

<div class="post-metadata">

**Author:** ![gabor\_papp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/gabor_papp/32/769_2.png) [@gabor\_papp](https://discourse.libcinder.org/u/gabor_papp)\
**Post date:** [July 21, 2016, 8:11pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/2 "2016-07-21T20:11:40Z")

</div>

I think you need to use premultiplied blending. Here’s [a similar question](https://forum.libcinder.org/topic/drawing-on-a-transparent-fbo) in the old forum.

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 21, 2016, 8:47pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/3 "2016-07-21T20:47:09Z")

</div>

Super, that did the trick. Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 22, 2016, 1:08am UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/4 "2016-07-22T01:08:44Z")

</div>

Hey thanks again, that definitely helped my on-window drawing issues.

In my project, I’m drawing some things to this FBO and showing it on screen, then eventually saving the current state of the FBO to disk as an image like this:

`ImageTarget::Options opts; opts.quality(1.0f); writeImage(imagePath, mDesignFbo->getColorTexture()->createSource(), opts, "png");`

The image is saving to disk where I want it to, but the resulting file has some of the same artifacts as the FBO did before applying the premultiplied blending. Is there something similar I can do before saving the file?

Thanks again, appreciate the help.

---

<div class="post-metadata">

**Author:** ![gabor\_papp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/gabor_papp/32/769_2.png) [@gabor\_papp](https://discourse.libcinder.org/u/gabor_papp)\
**Post date:** [July 22, 2016, 5:02am UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/5 "2016-07-22T05:02:02Z")

</div>

I think it is normal if you see artifacts when saving the premultiplied Fbo. I assume you open the saved image later in an image viewer, which does not use premultiplied blending when displaying the image. I’m not sure what your use case is, but you can save the contents of the screen instead.

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 22, 2016, 3:22pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/6 "2016-07-22T15:22:34Z")

</div>

I’m sending the finalized PNG file with transparency to a web service, which in turns saves a record of it and then forwards it on to a printing company for printing on some swag. I won’t be touching the image again on the machine the Cinder app is running on (ie, i’m not opening it in Photoshop and resaving or anything like that). Best case scenario is I can save the contents of the FBO to disk, with the included transparency and without the artifacts.

I’ve been trying to wrap my head around the concept of premultiplied alpha, and having a bit of a tough time. I see there are some methods in the `ip` namespace for `premultiply()` and `unpremultiply()`, would those be of any help to me when saving the image?

Thanks again, everything else is looking pretty good!

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 22, 2016, 3:32pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/7 "2016-07-22T15:32:56Z")

</div>

Ok, I just tried putting that `ip::unpremultiply()` method before calling `writeImage()` and it looks like it did work. From the earlier questions, I ended using premultiplied alpha when drawing to my FBO, so un-premultiply sounded like a rational choice to use before saving the image. Here’s what that snippet looks like now:

`fs::path imagePath = getHomeDirectory() / LOCAL_DESIGN_DIRECTORY / submission.designId; ImageTarget::Options opts; opts.quality(1.0f); Surface8u *sur = new Surface8u(mDesignFbo->getColorTexture()->createSource()); ip::unpremultiply(sur); writeImage(imagePath, *sur, opts, "png");`

Does this look reasonable? I am getting the results I want, but I still don’t _quite_ understand the premultiplication idea…

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 22, 2016, 3:43pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/8 "2016-07-22T15:43:00Z")

</div>

The article that opened my eyes to pre-multiplied alpha is [this one](https://blogs.msdn.microsoft.com/shawnhar/2009/11/06/premultiplied-alpha/) by Shawn Hargreaves. Hopefully this explains the difference. Your solution is a good one, as the compositing software probably can only handle normal alpha blending, not pre-multiplied.

One suggestion: try to avoid using raw pointers (or at the very least make sure to `delete` the `sur` variable after use). Here’s how you create a shared pointer that takes care of memory management for you:  
`auto sur = Surface8u::create(mDesignFbo->getColorTexture()->createSource()); ip::unpremultiply(sur.get()); writeImage(imagePath, *sur, opts, "png");`

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [July 22, 2016, 3:48pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/9 "2016-07-22T15:48:52Z")

</div>

Thanks for the resource Paul!

Also, yeah, I usually use the shared pointer `::create()` methods, but I couldn’t figure out how to pass that to the `unpremultiply()` method. Did not know about `.get()` method on shared pointers, thanks for the tip!

---

<div class="post-metadata">

**Author:** ![sansumbrella](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/sansumbrella/32/30_2.png) [@sansumbrella](https://discourse.libcinder.org/u/sansumbrella)\
**Post date:** [July 25, 2016, 2:24pm UTC](https://discourse.libcinder.org/t/fbo-antialiasing-format/169/10 "2016-07-25T14:24:34Z")

</div>

The service you are using probably assumes your PNG files conform to the w3 specification for [alpha representation in png](https://www.w3.org/TR/PNG/#6AlphaRepresentation). PNGs with alpha are officially non-premultiplied, though Apple premultiplies all PNGs bundled into an (iOS) application through XCode. So un-premultiplying for export seems like a good bet.
