<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>8 bit paletted image loading for Allegro 5</title>
		<link>http://www.allegro.cc/forums/view/610190</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 09 May 2012 15:13:20 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I added a function to Allegro SVN today called al_create_user_bitmap(). It allows you to load custom texture format images (only tested with OpenGL) like say PVR or ETC1, but in my case I&#39;m using it to load 8 bit paletted images on Android (should work on iOS too) with the OES_compressed_paletted_texture extension (which Allegro loads itself.)</p><p>The images save some memory (though that can depend on the device/drivers as some may just convert to a 32 bit image internally.) The code is C++ but the interface can be used from C. Loading a paletted image is a little bit round-about because you have to have created or loaded the image you want to convert to paletted beforehand, but it doesn&#39;t seem to slow my game down during loading much.</p><p>This is basically all there is to it:</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>w <span class="k3">=</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>bitmap<span class="k2">)</span><span class="k2">;</span>
<span class="number">  2</span>h <span class="k3">=</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>bitmap<span class="k2">)</span><span class="k2">;</span>
<span class="number">  3</span><span class="k1">int</span> true_w, true_h<span class="k2">;</span>
<span class="number">  4</span><a href="http://www.allegro.cc/manual/al_get_opengl_texture_size"><span class="a">al_get_opengl_texture_size</span></a><span class="k2">(</span>bitmap, <span class="k3">&amp;</span>true_w, <span class="k3">&amp;</span>true_h<span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span><span class="k1">int</span> sz <span class="k3">=</span> <span class="k2">(</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span><span class="k2">)</span> <span class="k3">+</span> <span class="k2">(</span>true_w <span class="k3">*</span> true_h<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span><span class="k1">unsigned</span> <span class="k1">char</span> <span class="k3">*</span>imgdata <span class="k3">=</span> <span class="k1">new</span> <span class="k1">unsigned</span> <span class="k1">char</span><span class="k2">[</span>sz<span class="k2">]</span><span class="k2">;</span>
<span class="number">  7</span><span class="k1">uint16_t</span> <span class="k3">*</span>exact <span class="k3">=</span> <span class="k1">new</span> <span class="k1">uint16_t</span><span class="k2">[</span><span class="n">256</span><span class="k2">]</span><span class="k2">;</span>
<span class="number">  8</span>gen_palette<span class="k2">(</span>bitmap, imgdata, exact<span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>gen_paletted_image<span class="k2">(</span>bitmap, imgdata<span class="k3">+</span><span class="k2">(</span><span class="n">256</span><span class="k3">*</span><span class="n">4</span><span class="k2">)</span>, exact<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span><span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> exact<span class="k2">;</span>
<span class="number"> 11</span>Paletted_Image_Data p<span class="k2">;</span>
<span class="number"> 12</span>p.data <span class="k3">=</span> imgdata<span class="k2">;</span>
<span class="number"> 13</span>p.size <span class="k3">=</span> sz<span class="k2">;</span>
<span class="number"> 14</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span>pal <span class="k3">=</span> al_create_custom_bitmap<span class="k2">(</span>w, h, upload_paletted_image, <span class="k3">&amp;</span>p<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span><span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> imgdata<span class="k2">;</span>
<span class="number"> 16</span><span class="c">/* Could now delete bitmap as 'pal' is an 8 bit copy */</span>
</div></div><p>

Make a function with that and you can load, then convert to 8 bit. I don&#39;t know if and how palette effects are possible with this. I don&#39;t know if you can modify the palette. I didn&#39;t make it for that purpose but it would be neat if you can. The code is attached, may require a tiny bit of work to get compiled and working as I ripped it from my game and removed anything specific to my game from it.</p><p>NOTE: You can lock these images READONLY but not for writing.<br />NOTE2: I haven&#39;t tried this with ANY images with a lot of colors. For all I know, and it&#39;s highly probably that I&#39;ve only used images with &lt; 256 colors, so the quantization isn&#39;t tested. I based it off reading the description of how the Allegro 4 quantization works from the source code (I was going to copy the code but it seemed like it&#39;d be simpler to do it myself.)</p><p>EDIT: I&#39;ve been working on it, since I tested with higher color images and it didn&#39;t work. It works pretty good now and is a lot faster. Still not super fast, but on my fairly slow tablet it takes 0.87 seconds to convert the 800x600 image below. The results aren&#39;t bad.</p><p>Before:<br /><span class="remote-thumbnail"><span class="json">{"name":"8urcat.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a660ad12066301ca7617dbc3c37c915c.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a660ad12066301ca7617dbc3c37c915c"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/6/a660ad12066301ca7617dbc3c37c915c-240.jpg" alt="8urcat.png" width="240" height="180" /></span></p><p>After:<br /><span class="remote-thumbnail"><span class="json">{"name":"8urcat-quantized.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/9\/9940c6f9a09930e0da2a2842e53fdffc.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/9\/9940c6f9a09930e0da2a2842e53fdffc"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/9/9/9940c6f9a09930e0da2a2842e53fdffc-240.jpg" alt="8urcat-quantized.png" width="240" height="180" /></span></p><p>The bright light in the window is the worst part. If anyone wants me to post the new source I will.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 09 May 2012 12:03:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmm, I can&#39;t say I understood it. Doesn&#39;t this API pretty much require the use to dig into Allegro internals? Can you post the example?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Wed, 09 May 2012 14:58:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The example is identical to the code block I pasted. I have that in my game which is exactly what I used to generate that image. Well, exactly as in I made some minor modifications to the api, but those are minor (I just removed the need for the &quot;exact&quot; parameter.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 09 May 2012 15:06:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, I didn&#39;t see the attachment.  I was looking for the upload_paletted_image function.</p><p>Indeed it casts the bitmap to ALLEGRO_BITMAP_EXTRA_OPENGL and works with the internals. I think we need to find another way. Maybe an OpenGL-specific function that creates a bitmap out of an existing texture?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Wed, 09 May 2012 15:11:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>All it would need is an al_set_opengl_texture really.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 09 May 2012 15:13:20 +0000</pubDate>
	</item>
</rss>
