<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Load raw pixel data into Allegro Bitmap</title>
		<link>http://www.allegro.cc/forums/view/609176</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 03 Jan 2012 17:49:53 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey guys,<br />I&#39;m trying to load pixel data from a TileStudio map into a Bitmap. Basically my code locks a square on the tileset bitmap and then freads it into the locked area.</p><div class="source-code snippet"><div class="inner"><pre>  bytesPerTile <span class="k3">=</span> <span class="n">4</span> <span class="k3">*</span> m_iTileHeight <span class="k3">*</span> m_iTileWidth<span class="k2">;</span>
  <span class="c">// now load the tiles</span>
  <span class="k1">for</span> <span class="k2">(</span> byCount <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> byCount <span class="k3">&lt;</span> m_iTileCount<span class="k2">;</span> byCount<span class="k3">+</span><span class="k3">+</span> <span class="k2">)</span> <span class="k2">{</span>

    <span class="c">// calculate the location on the grid of the current tile</span>
    x  <span class="k3">=</span> <span class="k2">(</span> byCount % m_byTilesPerRow <span class="k2">)</span> <span class="k3">*</span>  m_iTileWidth<span class="k2">;</span>
    y <span class="k3">=</span> <span class="k2">(</span> byCount <span class="k3">/</span> m_byTilesPerRow <span class="k2">)</span> <span class="k3">*</span> m_iTileHeight<span class="k2">;</span>

    <span class="c">// now load the tile into the bitmap</span>
    tileRegion <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_lock_bitmap_region"><span class="a">al_lock_bitmap_region</span></a><span class="k2">(</span> m_pTileBitmap, x, y, m_iTileWidth, m_iTileHeight, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_WRITEONLY <span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_fread"><span class="a">al_fread</span></a><span class="k2">(</span> pFile, tileRegion-&gt;data, bytesPerTile <span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_unlock_bitmap"><span class="a">al_unlock_bitmap</span></a><span class="k2">(</span> m_pTileBitmap <span class="k2">)</span><span class="k2">;</span>

  <span class="k2">}</span>
</pre></div></div><p>

However its showing up blue and distorted. Any reason why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Master)</author>
		<pubDate>Mon, 02 Jan 2012 17:28:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The pixel format has to be exactly the same between the file on disk and the ALLEGRO_BITMAP in (video)memory. Bitmaps usually have a header (data that is not pixels). ALLEGRO_BITMAPs can have &#39;padding&#39; between lines IIRC<span class="ref"><sup>[<a href="#">1</a>]</sup></span>. I.e. you need skip some bytes when writing to it. There was an example, but I can&#39;t remember where...<br />If it&#39;s only for loading purposes <span class="source-code"><a href="http://www.allegro.cc/manual/al_put_pixel"><span class="a">al_put_pixel</span></a></span> can also be used on locked bitmaps, and it takes care of the &#39;stride&#39; issue.<br />BTW I think a nested loop would look cleaner.
</p><div class="ref-block"><h2>References</h2><ol><li><a href="http://www.liballeg.org/a5docs/refman/graphics.html#allegro_locked_region">http://www.liballeg.org/a5docs/refman/graphics.html#allegro_locked_region</a></li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Mon, 02 Jan 2012 18:37:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Quick guess</p><p>Blue: You&#39;re only writing to the blue channel. Like loading single bytes from an 8 bit image and putting them into a 24 bit bitmap.</p><p>Distorted: A square (or any rectangular) area of a bitmap is <i>not</i> consecutive memory.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Mon, 02 Jan 2012 18:41:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ok, sounds good. I did use al_put_pixel at first, but I found it took up to a minute to load a 45-tile set with tile size 32x32. So I wanted something that ran faster.<br />I&#39;ll have a shot at using stride. Hopefully it&#39;ll work better. </p><p>-- EDIT --<br />Must say, not really digging the pitch approach. Just how much padding do the bitmaps have, anyway? Here&#39;s my new approach:
</p><div class="source-code snippet"><div class="inner"><pre>    <span class="c">// now load the tile into the bitmap</span>
    tileRegion <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_lock_bitmap_region"><span class="a">al_lock_bitmap_region</span></a><span class="k2">(</span> m_pTileBitmap, x, y, m_iTileWidth, m_iTileHeight, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_WRITEONLY <span class="k2">)</span><span class="k2">;</span>
    <span class="k1">for</span><span class="k2">(</span> j<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> j <span class="k3">&lt;</span> m_iTileHeight<span class="k2">;</span> j<span class="k3">+</span><span class="k3">+</span> <span class="k2">)</span> <span class="k2">{</span>
    
      p <span class="k3">=</span> <span class="k2">(</span><span class="k1">size_t</span><span class="k3">*</span><span class="k2">)</span>tileRegion-&gt;data <span class="k3">+</span> j <span class="k3">*</span> tileRegion-&gt;pitch<span class="k2">;</span>
      <a href="http://www.allegro.cc/manual/al_fread"><span class="a">al_fread</span></a><span class="k2">(</span> pFile, p, m_iTileWidth<span class="k3">*</span><span class="n">4</span> <span class="k2">)</span><span class="k2">;</span>

    <span class="k2">}</span>
    <a href="http://www.allegro.cc/manual/al_unlock_bitmap"><span class="a">al_unlock_bitmap</span></a><span class="k2">(</span> m_pTileBitmap <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Master)</author>
		<pubDate>Mon, 02 Jan 2012 18:54: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/609176/942207#target">The Master</a> said:</div><div class="quote"><p> Must say, not really digging the pitch approach.
</p></div></div><p>Do you have a choice? <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Just how much padding do the bitmaps have, anyway?
</p></div></div><p>In any case the ones in memory will have more than the ones on disk &gt;_&gt; Unless, <u>maybe</u> if you use a non-video memory bitmap.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Mon, 02 Jan 2012 19:19:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The pitch is in bytes, so I think you are advancing 4 or 8 times too far (depending if you are 32-bit or 64-bit). Try:</p><p><span class="source-code">p <span class="k3">=</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span>tileRegion-&gt;data <span class="k3">+</span> j <span class="k3">*</span> tileRegion-&gt;pitch<span class="k2">;</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 03 Jan 2012 00:22:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">weapon_S said:</div><div class="quote"><p>
Do you have a choice? <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div></div><p>

What I meant to say is, I&#39;ve tried the pitch approach, and it segfaulted. <br />I&#39;ll have a shot using what Leverton suggested.</p><p><s>-EDIT</s>-<br />Ok, that worked. However, the colour&#39;s all confused. The greens are greens, but the browns are blues, and the blues are yellow. I&#39;m still iffie on how colour conversion works.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Master)</author>
		<pubDate>Tue, 03 Jan 2012 05:20:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try other pixel formats<span class="ref"><sup>[<a href="#">1</a>]</sup></span>? There are differences in which order each color component is stored. (Apparently it is 32-bit and there is no header, when your image isn&#39;t distorted.)<br />You don&#39;t have any colour conversions; You&#39;re loading raw data.
</p><div class="ref-block"><h2>References</h2><ol><li><a href="http://www.liballeg.org/a5docs/refman/graphics.html#allegro_pixel_format">http://www.liballeg.org/a5docs/refman/graphics.html#allegro_pixel_format</a></li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Tue, 03 Jan 2012 13:38:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I got it working. Just the endian-ness issue. Anyway, now comes to the drawing part.<br />What I&#39;ve got now is a proper, non-colour-confused rendering of the map. However, when it renders tiles that have a portion transparent (like part of a pillar or something), it draws semi-transparent mask colour (as in [255,0,255]) where it should be drawing nothing. This didn&#39;t happen before when I just used put_pixel, and al_convert_mask_to_alpha doesn&#39;t seem to do anything. The bitmap format is ARGB 32bit.</p><p><s>-EDIT</s>-<br />Got it working. But it&#39;s a bit of an ugly hack with TileStudio. I had to fix the export so that instead of outputting the proper alpha channel from the tile, TileStudio just outputs 255. This will make it hard if/when i want to do transparency in tiles rather than just masks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (The Master)</author>
		<pubDate>Tue, 03 Jan 2012 15:10:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Loading the alpha should work perfectly. Maybe the blender was set wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Tue, 03 Jan 2012 17:49:53 +0000</pubDate>
	</item>
</rss>
