<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] al_load_bitmap issue with PNG</title>
		<link>http://www.allegro.cc/forums/view/603480</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 17 Mar 2010 05:51:43 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m seeing intermittent issues when loading and rendering PNG files via the iPhone port. Occasionally the images will load fine, other times I&#39;ll witness graphical distortion. I made a test case. All I need to do is run this a few times via the simulator to see a number of artifacts. Some are shown here:</p><p><span class="remote-thumbnail"><span class="json">{"name":"600727","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/0\/b070017ef2663d1ccb5e82ded96f99eb.jpg","w":300,"h":300,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/0\/b070017ef2663d1ccb5e82ded96f99eb"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/b/0/b070017ef2663d1ccb5e82ded96f99eb-240.jpg" alt="600727" width="240" height="240" /></span></p><p>Here&#39;s the code used when seeing the above artifacts. I&#39;ve also attached the png files in question.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>    
<span class="number">  3</span>  al_init<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  4</span>  
<span class="number">  5</span>  <span class="c">// Setup display</span>
<span class="number">  6</span>  ALLEGRO_DISPLAY <span class="k3">*</span>display <span class="k3">=</span> al_create_display<span class="k2">(</span><span class="n">480</span>, <span class="n">320</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>  <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>display<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  8</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Error creating display.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>    <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span>
<span class="number"> 10</span>  <span class="k2">}</span>  
<span class="number"> 11</span>  
<span class="number"> 12</span>  al_init_image_addon<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>  
<span class="number"> 14</span>  ALLEGRO_TIMER <span class="k3">*</span>timer <span class="k3">=</span> al_install_timer<span class="k2">(</span><span class="n">1</span>.<span class="n">0</span> <span class="k3">/</span> <span class="n">60</span><span class="k2">)</span><span class="k2">;</span>  
<span class="number"> 15</span>  ALLEGRO_EVENT_QUEUE <span class="k3">*</span>queue <span class="k3">=</span> al_create_event_queue<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>  al_register_event_source<span class="k2">(</span>queue, al_get_display_event_source<span class="k2">(</span>display<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>  al_register_event_source<span class="k2">(</span>queue, al_get_timer_event_source<span class="k2">(</span>timer<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>  al_start_timer<span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>  
<span class="number"> 19</span>  
<span class="number"> 20</span>  ALLEGRO_BITMAP <span class="k3">*</span>t1 <span class="k3">=</span> al_load_bitmap<span class="k2">(</span><span class="s">"walk.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</span>  ALLEGRO_BITMAP <span class="k3">*</span>t2 <span class="k3">=</span> al_load_bitmap<span class="k2">(</span><span class="s">"run.png"</span><span class="k2">)</span><span class="k2">;</span>  
<span class="number"> 22</span>  
<span class="number"> 23</span>  <span class="k1">bool</span> needRender <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 24</span>  
<span class="number"> 25</span>  ALLEGRO_EVENT event<span class="k2">;</span>
<span class="number"> 26</span>  
<span class="number"> 27</span>  <span class="k1">while</span> <span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 28</span>    al_wait_for_event<span class="k2">(</span>queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 29</span>    
<span class="number"> 30</span>    <span class="k1">switch</span><span class="k2">(</span>event.type<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 31</span>      <span class="k1">case</span> ALLEGRO_EVENT_TIMER:
<span class="number"> 32</span>        needRender <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 33</span>        <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 34</span>      <span class="k1">case</span> ALLEGRO_EVENT_DISPLAY_CLOSE:
<span class="number"> 35</span>        <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 36</span>      <span class="k2">}</span>
<span class="number"> 37</span>    
<span class="number"> 38</span>    <span class="k1">if</span> <span class="k2">(</span>needRender <span class="k3">&amp;</span><span class="k3">&amp;</span> al_event_queue_is_empty<span class="k2">(</span>queue<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 39</span>      al_draw_bitmap<span class="k2">(</span>t1, <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="number"> 40</span>      al_draw_bitmap<span class="k2">(</span>t2, <span class="n">0</span>, <span class="n">25</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 41</span>      al_flip_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 42</span>      needRender <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 43</span>    <span class="k2">}</span>
<span class="number"> 44</span>  <span class="k2">}</span>
<span class="number"> 45</span>  
<span class="number"> 46</span>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 47</span><span class="k2">}</span>
</div></div><p>

I&#39;m not sure if this is a genuine issue, or a mistake on my part.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Tue, 16 Mar 2010 23:58:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Couple questions. Are you using (to your knowledge) the libpng support in the Allegro image addon or the Apple WhateverYouCallIt loader?</p><p>Also, I didn&#39;t think it was true for the simulator, but it may be, and definitely is on the device... Apple has this brilliant way of compressing pngs in your bundle that makes them incompatible with libpng. So try adding this key value pair to your project settings:</p><p>IPHONE_OPTIMIZE_OPTIONS       -skip-PNGs
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 00:19:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To my knowledge, I&#39;m using libpng. I certainly installed it with DarwinPorts before running cmake to compile Allegro. Is there anyway to tell which is being used, or is it a conscious decision on my part?</p><p>I also added the -skip-PNGs flag for All Configurations in the Project Settings pane - but this doesn&#39;t seem to have helped much.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Wed, 17 Mar 2010 00:32:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The code you posted looks completely different from the screenshot. I get both images drawn to the top left (if holding in landscape). No garbage. Can you post the code that produces the image above?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 01:07:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The image above was the result of me running the code posted above a few times. I&#39;ve just positioned multiple results in one image for convenience.</p><p>As mentioned, occasionally the images will load fine. Other times, I&#39;ll get the artifacts shown above.</p><p>EDIT: To clarify, the screenshot shows the results of two executions of my test case. In the first set, walk.png is rendered fine, while run.png experiences strange artifacts.</p><p>In the second, both walk.png and run.png are distorted.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Wed, 17 Mar 2010 01:19:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I see. Well I tried it about 10 times with no garbage here... btw I just found out you are probably using the Apple native image loader... that&#39;s the default now. I don&#39;t have any experience using that, so I can&#39;t say how good it is. In my game I&#39;m using like allegro 4.9.16 or something with some patches. But since it works fine here each time I try it, I&#39;m not sure the problem. Do you get any artifacts with any other opengl programs on your mac?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 01:24:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nope. I haven&#39;t had any graphic artifacts elsewhere. </p><p>Is it possible that the PNG is causing the issue? You mentioned compression/optimization on the iPhone end. Currently, I&#39;m saving out my graphics via Photoshop. Perhaps it&#39;s choking on the format?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Wed, 17 Mar 2010 01:32:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If that were the case I would expect corruption everytime, and on my end too. But it may be that we&#39;re using different sdks. I don&#39;t think I&#39;m on the very latest or the beta... It wouldn&#39;t hurt to try re saving them with Preview or something though, and see if the garbage goes away. It&#39;s highly possible there is a bug in Allegro&#39;s image loader or locking routines too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 01:37:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks to <a href="http://stackoverflow.com/questions/1079884/opengl-es-textures-from-pngs-with-transparency-are-being-rendered-with-weird-arti">this thread</a> I managed to fix my rendering issues by clearing the drawing context in the image addon iphone.m file.</p><p>It seems like PNGs with transparency were being layered over the top of each other (which explained why bitmaps loaded fine), and this seems to resolve the issue.</p><div class="source-code snippet"><div class="inner"><pre>   <span class="c">/* Now we need to draw the image into a memory buffer. */</span>
   pixels <span class="k3">=</span> _AL_MALLOC<span class="k2">(</span>w <span class="k3">*</span> h <span class="k3">*</span> <span class="n">4</span><span class="k2">)</span><span class="k2">;</span>
   CGContextRef context <span class="k3">=</span> CGBitmapContextCreate<span class="k2">(</span>pixels, w, h, <span class="n">8</span>, w <span class="k3">*</span> <span class="n">4</span>,
      CGImageGetColorSpace<span class="k2">(</span>uiimage.CGImage<span class="k2">)</span>,
      kCGImageAlphaPremultipliedLast<span class="k2">)</span><span class="k2">;</span>
  CGContextSetBlendMode<span class="k2">(</span>context, kCGBlendModeCopy<span class="k2">)</span><span class="k2">;</span> <span class="c">// Clear context</span>
   CGContextDrawImage<span class="k2">(</span>context, CGRectMake<span class="k2">(</span><span class="n">0</span>.<span class="n">0</span>, <span class="n">0</span>.<span class="n">0</span>, <span class="k2">(</span>CGFloat<span class="k2">)</span>w, <span class="k2">(</span>CGFloat<span class="k2">)</span>h<span class="k2">)</span>,
      uiimage.CGImage<span class="k2">)</span><span class="k2">;</span>
   CGContextRelease<span class="k2">(</span>context<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

I&#39;m not sure whether this is a solid fix, or if it could help anyone else - but it seems to have worked for me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Wed, 17 Mar 2010 03:05:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve added it to the bug tracker, as I don&#39;t know the code well myself. Elias could probably tell if this is a good fix or not.</p><p>Edit: Ok, should be fixed in SVN. Thanks for that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 03:12:41 +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/603480/856394#target">Trent Gamblin</a> said:</div><div class="quote"><p>Edit: Ok, should be fixed in SVN. Thanks for that.</p></div></div><p>
The OS X native loader probably needs a similar fix. Did you do that too?<br />(I could just look at the change log, but I&#39;m feeling lazy).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 17 Mar 2010 05:42:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, I did not.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 17 Mar 2010 05:43:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok. Actually looking at the code, it probably doesn&#39;t need it. It uses the Cocoa NSImage interface to do the drawing, and it explicitly sets NSCompositeCopy, which should be what we want.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 17 Mar 2010 05:51:43 +0000</pubDate>
	</item>
</rss>
