<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>writing 256 color bitmap to textfile</title>
		<link>http://www.allegro.cc/forums/view/589149</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 21 Dec 2006 15:31:17 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Im trying to write a program that outputs to a textfile the content of a bitmap image (in 256 colors).  </p><p>I want to write a single byte (to the textfile) representing each pixel.  (that is, if the first pixel in the bitmap is black and black is the first color in the color palete, then i want the first character in the textfile to be char(0), if the second pixel was palete color 2, then the second character in the textfile should be char(1), etc.. )</p><p>But, the 8-bit color depths and color palete stuff doens&#39;t seem to have clear documentation.  Can someone help me?  </p><p>====================================================================<br />I have a working version of this program that outputs to a textfile while running in 16-bit color depth. I&#39;m guessing the missing step is to find where the coorisponding value of each pixel is located in the palete.<br />And here it is...</p><p>#include &lt;fstream&gt;<br />#include &quot;allegro.h&quot;<br />using namespace std;<br />int main(int argc, char *argv[])<br />{<br />    ofstream outFile(&quot;map.txt&quot;);<br />    allegro_init();<br />    install_keyboard();<br />    set_color_depth(16);<br />    set_gfx_mode(GFX_AUTODETECT, 420,420,0,0); </p><p>    BITMAP *my_pic; <br />    my_pic = load_bitmap(&quot;picture1.bmp&quot;, NULL); </p><p>    int c1,y,x;<br />    for (y=0; y&lt;my_pic-&gt;h; y++) {<br />        unsigned short *bmpline = (unsigned short *)(my_pic-&gt;line[y]);<br />        for (x=0; x&lt;my_pic-&gt;w; x++) {<br />            c1 = bmpline[x];<br />            outFile&lt;&lt;c1&lt;&lt;&quot; &quot;;<br />        }<br />        outFile&lt;&lt;endl;<br />    }<br />    destroy_bitmap(my_pic);<br />    return 0;<br />}<br />END_OF_MAIN()
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (idathunkit2)</author>
		<pubDate>Mon, 18 Dec 2006 01:52:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>And you can&#39;t use save_bitmap why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve Terry)</author>
		<pubDate>Mon, 18 Dec 2006 01:56:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t want to &#39;save&#39; the bitmap.  I want to convert it into a VERY simple format.  The fact that it&#39;s a bitmap is not important to the final product.  I&#39;m just using the bitmap as an easy way to create a simple map, then I want to convert it into my own data structure.  I plan to add other elements to the data structure later, but for now, I just want to converte the pixels into a 1-byte code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (idathunkit2)</author>
		<pubDate>Mon, 18 Dec 2006 02:08:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1. Load the bitmap without color conversion.<br />2. Use _getpixel or the line[] array to get at the data.<br />3. Save the returned values.<br />4. ???<br />5. Profit.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Mon, 18 Dec 2006 02:12:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks gnolam!<br />Here&#39;s what I did with your help.  I&#39;m posting it in case any other newbie like me wants to see how it works (specifically getpixel).</p><p>(where my_pic is a pointer to a BITMAP and outFile is an ofstream object<br /> and y,x, and c1 are int).</p><p>for (y=0; y&lt;my_pic-&gt;h; y++) <br />{<br />     for (x=0; x&lt;my_pic-&gt;w; x++) <br />{<br />          c1 = getpixel(my_pic,x,y);<br />          outFile&lt;&lt;char(c1)&lt;&lt;&quot; &quot;;<br />     }<br />     outFile&lt;&lt;endl;<br />}</p><p>more getpixel help...<br /><a href="http://www.allegro.cc/manual/api/drawing-primitives/getpixel">http://www.allegro.cc/manual/api/drawing-primitives/getpixel</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (idathunkit2)</author>
		<pubDate>Mon, 18 Dec 2006 02:35:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Psssst...  If you wrap your code in code-tags, then Allegro functions become links to their page in the manual:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span>y<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> y<span class="k3">&lt;</span>my_pic-&gt;h<span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> 
<span class="k2">{</span>
    <span class="k1">for</span> <span class="k2">(</span>x<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> x<span class="k3">&lt;</span>my_pic-&gt;w<span class="k2">;</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> 
    <span class="k2">{</span>
        c1 <span class="k3">=</span> <a href="http://www.allegro.cc/manual/getpixel" target="_blank"><span class="a">getpixel</span></a><span class="k2">(</span>my_pic,x,y<span class="k2">)</span><span class="k2">;</span>
        outFile<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="k1">char</span><span class="k2">(</span>c1<span class="k2">)</span><span class="k3">&lt;</span><span class="k3">&lt;</span><span class="s">" "</span><span class="k2">;</span>
    <span class="k2">}</span>
    outFile<span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Mon, 18 Dec 2006 06:46:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Um... You can&#39;t output char(0) and char(1) to a text file. Or you can, but it won&#39;t be a text file after that. Characters 0 - 31 are not text file characters, except line feed and carriage return, char(13) and char(10), I think. You&#39;d better treat the file as a binary file. In your code you output endl after each row. But endl outputs lf and cr, or either one, depending on system. But you might as well have other endl:s due to the char(c1), that might as well be cr or lf. Not to mention all other creepy characters in the range 0 - 31.</p><p>Either treat the file as a binary file and skip the endl thing, or output the byte as a hex value instead.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Mon, 18 Dec 2006 06:59:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you for some reason need to have it in &quot;readable&quot; format, here&#39;s an idéa:
</p><div class="source-code snippet"><div class="inner"><pre>map<span class="k3">&lt;</span><span class="k1">int</span>,char&gt; lookup<span class="k2">;</span>
lookup<span class="k2">[</span><a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'W'</span><span class="k2">;</span> <span class="c">// red = wall</span>
lookup<span class="k2">[</span><a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">0</span>,<span class="n">255</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'D'</span><span class="k2">;</span> <span class="c">// green = door</span>
lookup<span class="k2">[</span><a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">0</span>,<span class="n">0</span>,<span class="n">255</span><span class="k2">)</span><span class="k2">]</span> <span class="k3">=</span> <span class="s">'F'</span><span class="k2">;</span> <span class="c">// blue = floor</span>

<span class="k1">for</span> <span class="k2">(</span>y<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> y<span class="k3">&lt;</span>my_pic-&gt;h<span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> 
<span class="k2">{</span>
    <span class="k1">for</span> <span class="k2">(</span>x<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> x<span class="k3">&lt;</span>my_pic-&gt;w<span class="k2">;</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> 
    <span class="k2">{</span>
        <span class="k1">int</span> color <span class="k3">=</span> <a href="http://www.allegro.cc/manual/getpixel" target="_blank"><span class="a">getpixel</span></a><span class="k2">(</span>my_pic,x,y<span class="k2">)</span><span class="k2">;</span>
        outFile <span class="k3">&lt;</span><span class="k3">&lt;</span> lookup<span class="k2">[</span>color<span class="k2">]</span><span class="k2">;</span>
    <span class="k2">}</span>
    outFile<span class="k3">&lt;</span><span class="k3">&lt;</span>endl<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Now, paint a bitmap with the colors defined above for walls, doors floors etc.<br />I haven&#39;t tested it at all, it might blow up your computer, or at least the compiler <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ixilom)</author>
		<pubDate>Tue, 19 Dec 2006 01:14:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>wow! that&#39;s teh mega-cool, ixilom!</p><p>Is the &quot;&lt;int,char&gt;&quot; table a c++-ism?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Jensen)</author>
		<pubDate>Thu, 21 Dec 2006 09:52:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Thu, 21 Dec 2006 15:31:17 +0000</pubDate>
	</item>
</rss>
