<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>al_map_rgba vs. al_map_rgba_f</title>
		<link>http://www.allegro.cc/forums/view/614684</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 02 Oct 2014 05:27:49 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was testing al_clear_to_color to draw a colored bitmap on top of the back buffer, which was colored, and alpha in the al_map_rgba version doesn&#39;t behave like I&#39;d think.</p><p>For example if the back buffer was red and the bitmap to draw was green (0 alpha) it would draw yellow as if it was combining them. I just add &quot;_f&quot; to &quot;al_map_rgba&quot; without changing arguments and it behaves as you&#39;d think, with 0 alpha. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felix-The-Ghost)</author>
		<pubDate>Tue, 30 Sep 2014 13:45:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Show code for loading and displaying your images...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 30 Sep 2014 17:44:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This code is from allegro 5.1:<br />al_map_rgba is defined like this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_COLOR"><span class="a">ALLEGRO_COLOR</span></a> <a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span>
   <span class="k1">unsigned</span> <span class="k1">char</span> r, <span class="k1">unsigned</span> <span class="k1">char</span> g, <span class="k1">unsigned</span> <span class="k1">char</span> b, <span class="k1">unsigned</span> <span class="k1">char</span> a<span class="k2">)</span>
<span class="k2">{</span>
   <a href="http://www.allegro.cc/manual/ALLEGRO_COLOR"><span class="a">ALLEGRO_COLOR</span></a> color<span class="k2">;</span>
   _AL_MAP_RGBA<span class="k2">(</span>color, r, g, b, a<span class="k2">)</span><span class="k2">;</span>
   <span class="k1">return</span> color<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
With _AL_MAP_RGBA expanding to:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define _AL_MAP_RGBA(_color, _r, _g, _b, _a)                                  \</span>
<span class="p">   do {                                                                       \ </span>
<span class="p">      (_color).r = _al_u8_to_float[_r];                                       \ </span>
<span class="p">      (_color).g = _al_u8_to_float[_g];                                       \ </span>
<span class="p">      (_color).b = _al_u8_to_float[_b];                                       \ </span>
<span class="p">      (_color).a = _al_u8_to_float[_a];                                       \ </span>
<span class="p">   } while (0) </span>
</pre></div></div><p>
_al_u8_to_float is an array of 256 floats and is filled in _al_init_pixels like this:
</p><div class="source-code snippet"><div class="inner"><pre>   <span class="k1">int</span> i<span class="k2">;</span>
   <span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">256</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
      _al_u8_to_float<span class="k2">[</span>i<span class="k2">]</span> <span class="k3">=</span> i <span class="k3">/</span> <span class="n">255</span>.<span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
The first entry in it should be 0. So it will put 0.0f as the .a component of the color struct, unless something fishy is going on.<br />al_map_rgba_f is way simpler, it just sets the values:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_COLOR"><span class="a">ALLEGRO_COLOR</span></a> <a href="http://www.allegro.cc/manual/al_map_rgba_f"><span class="a">al_map_rgba_f</span></a><span class="k2">(</span><span class="k1">float</span> r, <span class="k1">float</span> g, <span class="k1">float</span> b, <span class="k1">float</span> a<span class="k2">)</span>
<span class="k2">{</span>
   <a href="http://www.allegro.cc/manual/ALLEGRO_COLOR"><span class="a">ALLEGRO_COLOR</span></a> color<span class="k2">;</span>
   color.r <span class="k3">=</span> r<span class="k2">;</span>
   color.g <span class="k3">=</span> g<span class="k2">;</span>
   color.b <span class="k3">=</span> b<span class="k2">;</span>
   color.a <span class="k3">=</span> a<span class="k2">;</span>
   <span class="k1">return</span> color<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
So unless there&#39;s some weird memory corruption, those two calls should have the same effect.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Tue, 30 Sep 2014 17:50:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/614684/1005729#target">J-Gamer</a> said:</div><div class="quote"><p> So unless there&#39;s some weird memory corruption, those two calls should have the same effect. </p></div></div><p>Or you didn&#39;t initialize Allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Tue, 30 Sep 2014 18:49:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/614684/1005726#target">Felix-The-Ghost</a> said:</div><div class="quote"><p>draw a colored bitmap on top of the back buffer</p></div></div><p>
I don&#39;t think you can have a backbuffer if you don&#39;t initialize allegro. You will indeed run into the problem if you call al_map_rgba before initializing though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Tue, 30 Sep 2014 20:06:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/614684/1005726#target">Felix-The-Ghost</a> said:</div><div class="quote"><p>
I just add &quot;_f&quot; to &quot;al_map_rgba&quot; without changing arguments and it behaves as you&#39;d think, with 0 alpha.
</p></div></div><p>
Something important to note is that al_map_rgba_f takes values from 0.0 to 1.0, and al_map_rgba takes values from 0-255.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 01 Oct 2014 05:08:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the argument given is 0, that makes no difference.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Wed, 01 Oct 2014 14:01:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, I know that, thanks. I was pointing out that they take different parameters because it sounded like he was not aware of that.</p><p>Allegro uses pre multiplied alpha blending by default, for various reasons.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
// pre multiplied alpha blender, this is the default<br />al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA)</p><p>ALLEGRO_ADD</p><p> r = dr * dst + sr * src<br /> g = dg * dst + sg * src<br /> b = db * dst + sb * src<br /> a = da * dst + sa * src
</p></div></div><p>
With src of (0,255,0,0) and dst of (1,0,0,?)</p><p>r = dr*(1-sa) + sr*1.0<br />g = dg*(1-sa) + sg*1.0<br />b = db*(1-sa) + sb*1.0</p><p>r = 1.0*(1-0) + 0*1 = 1<br />g = 0*(1-0) + 1*1 = 1<br />b = 0*(1-0) + 0*1 = 0</p><p>Which is yellow, and is exactly what you were getting.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If you are using non-pre-multiplied alpha, you could use</p><p>al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA)
</p></div></div><p>

Also, why are you trying to draw a fully transparent bitmap anyway? What&#39;s the point?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 01 Oct 2014 21:36:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/614684/1005759#target">Edgar Reynaldo</a> said:</div><div class="quote"><p> Also, why are you trying to draw a fully transparent bitmap anyway? What&#39;s the point? </p></div></div><p>I bet he was trying to find out why partial transparency didn&#39;t work right either, like taking extreme cases.  I do stuff like that all the time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 02 Oct 2014 00:31:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I should have checked in earlier, I&#39;ve since removed the code.<br />What I was doing exactly was testing al_clear_to_color with al_set_clipping_rectangle to see if I could &quot;hide&quot; parts of a fully opaque bitmap.</p><p>When I first read the manual entry for al_map_rgba_f I thought it meant that the alpha is 0-1 and the others were still 0-255 :/ Either way it was the al_map_rgba version that was using the alpha strangely (the color values themselves seemed to affect the alpha -- 0, 0, 0, 0 was not visibly the same as 255, 0, 0, 0)</p><p>Edit: Add this code to any program after rendering Buffer before copying it to the back buffer:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_set_clipping_rectangle"><span class="a">al_set_clipping_rectangle</span></a><span class="k2">(</span><span class="n">5</span>, <span class="n">5</span>, <span class="n">20</span>, <span class="n">20</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// target should be your buffer and rectangle can be anything in its bounds</span>
<a href="http://www.allegro.cc/manual/al_clear_to_color"><span class="a">al_clear_to_color</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_set_clipping_rectangle"><span class="a">al_set_clipping_rectangle</span></a><span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>Buffer<span class="k2">)</span>, <a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>Buffer<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

That shows up as red to me, but if I change the al_map_rgba to al_map_rgba_f it works as I&#39;d think it&#39;d work, with 0 alpha meaning 0 alpha (aside from the fact that the color of a translucent alpha would be incorrect due to 255 not being between 0-1, but my point is alpha is working)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felix-The-Ghost)</author>
		<pubDate>Thu, 02 Oct 2014 03:53:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/614684/1005766#target">Felix-The-Ghost</a> said:</div><div class="quote"><p>
(the color values themselves seemed to affect the alpha -- 0, 0, 0, 0 was not visibly the same as 255, 0, 0, 0) 
</p></div></div><p>
That&#39;s because the default blending mode use pre-multiplied alpha which means the dst in the blending formula will always be 1 - source alpha. If sa is 0, the dest is added at full intensity. If sa is 1, the destination is ignored. Since you&#39;re using ALLEGRO_ADD in the default blender, and ALLEGRO_ONE for src, the source will always be added to the resulting color, even with a source alpha of 0.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
ALLEGRO_ADD</p><p>r = dr * dst + sr * src<br />g = dg * dst + sg * src<br />b = db * dst + sb * src<br />a = da * dst + sa * src 
</p></div></div><p>
where dst is (1-sa) and src is 1.</p><p>To use the default blender with a bitmap that you clear to a certain color, the color needs to be mulitiplied by the alpha first :
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_clear_to_color"><span class="a">al_clear_to_color</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span><span class="k2">(</span>r<span class="k3">*</span>a<span class="k2">)</span><span class="k3">/</span><span class="n">255</span><span class="k2">)</span> , <span class="k2">(</span>g<span class="k3">*</span>a<span class="k2">)</span><span class="k3">/</span><span class="n">255</span> , <span class="k2">(</span>b<span class="k3">*</span>a<span class="k2">)</span><span class="k3">/</span><span class="n">255</span> , a<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Then you could draw that bitmap normally with the default blender.</p><p><u>Edit</u>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That shows up as red to me, but if I change the al_map_rgba to al_map_rgba_f it works as I&#39;d think it&#39;d work, with 0 alpha meaning 0 alpha (aside from the fact that the color of a translucent alpha would be incorrect due to 255 not being between 0-1, but my point is alpha is working) 
</p></div></div><p>
That&#39;s probably just a quirk of the fact that 255.0f is not between 0.0 and 1.0.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 02 Oct 2014 04:10:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the info <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felix-The-Ghost)</author>
		<pubDate>Thu, 02 Oct 2014 05:27:49 +0000</pubDate>
	</item>
</rss>
