<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DevNet &#187; tanya</title>
	<atom:link href="http://devnetwork.ru/author/tanya/feed" rel="self" type="application/rss+xml" />
	<link>http://devnetwork.ru</link>
	<description>в помощь разработчику</description>
	<lastBuildDate>Mon, 21 Sep 2009 20:04:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Парсинг BMP средствами ActionScript</title>
		<link>http://devnetwork.ru/parsing-bmp-sredstvami-actionscript</link>
		<comments>http://devnetwork.ru/parsing-bmp-sredstvami-actionscript#comments</comments>
		<pubDate>Mon, 21 Sep 2009 20:04:15 +0000</pubDate>
		<dc:creator>tanya</dc:creator>
				<category><![CDATA[AS3]]></category>

		<guid isPermaLink="false">http://devnetwork.ru/parsing-bmp-sredstvami-actionscript</guid>
		<description><![CDATA[Майк Чамберс в своём блоге показал как парсить обыкновенный .bmp-файл несколькими строками ActionScript-кода.
Оказывается, делается это следующим образом:

package
{
    import flash.filesystem.File;
    import flash.filesystem.FileStream;
    import flash.filesystem.FileMode;

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.utils.Endian;

    [...]]]></description>
			<content:encoded><![CDATA[<p>Майк Чамберс в своём блоге <a href="http://www.mikechambers.com/blog/2009/09/17/parsing-and-displaying-bmp-files-via-actionscript/#more-1830">показал как парсить обыкновенный .bmp-файл</a> несколькими строками ActionScript-кода.</p>
<p>Оказывается, делается это следующим образом:</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">package
{
    import flash.filesystem.File;
    import flash.filesystem.FileStream;
    import flash.filesystem.FileMode;

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.utils.Endian;

    import flash.geom.Rectangle;

    [SWF(width=<span style="color: #006080">'550'</span>, height=<span style="color: #006080">'400'</span>, backgroundColor=<span style="color: #006080">'#FFFFFF'</span>, frameRate=<span style="color: #006080">'12'</span>)]
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BMPViewer extends Sprite
    {
        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">const</span> MAGIC_NUMBER:String = <span style="color: #006080">&quot;BM&quot;</span>;
        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">const</span> BMP_DATA_OFFSET_POSITION:<span style="color: #0000ff">int</span> = 0xA;
        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">const</span> WIDTH_POSITION:<span style="color: #0000ff">int</span> = 0x12;
        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">const</span> HEIGHT_POSITION:<span style="color: #0000ff">int</span> = 0x16;

        <span style="color: #0000ff">public</span> function BMPViewer()
        {
            loadBMP();
            super();
        }

        <span style="color: #008000">/*</span>
<span style="color: #008000">            Loads and reads a 24 Bit bitmap file.</span>
<span style="color: #008000">            </span>
<span style="color: #008000">            Based on BMP info from:</span>
<span style="color: #008000">            http://en.wikipedia.org/wiki/BMP%5Ffile%5Fformat</span>
<span style="color: #008000">        */</span>
        <span style="color: #0000ff">private</span> function loadBMP():<span style="color: #0000ff">void</span>
        {
            <span style="color: #008000">//Load BMP. This requires AIR.</span>
            <span style="color: #008000">//Use FileReference.browse for</span>
            <span style="color: #008000">//Flash Player</span>
            var bmpFile:File = <span style="color: #0000ff">new</span> File(<span style="color: #006080">&quot;app:/image.bmp&quot;</span>);
            var fs:FileStream = <span style="color: #0000ff">new</span> FileStream();

            <span style="color: #008000">//BMP files are Little Endian, which means their</span>
            <span style="color: #008000">//least significant byte is first (right to left)</span>
            fs.endian = Endian.LITTLE_ENDIAN;

            <span style="color: #008000">//open the file in READ mode</span>
            fs.open(bmpFile, FileMode.READ);

            <span style="color: #008000">//check the first two bytes to make sure</span>
            <span style="color: #008000">//it is a valid BMP file</span>
            <span style="color: #0000ff">if</span>(fs.readUTFBytes(2) != MAGIC_NUMBER)
            {
                trace(<span style="color: #006080">&quot;FAIL : NOT A BMP FILE&quot;</span>);

                <span style="color: #008000">//not a BMP file, close steam</span>
                <span style="color: #008000">//and exit</span>
                fs.close();
                <span style="color: #0000ff">return</span>;
            }

            <span style="color: #008000">//note, we could also grab the length from the </span>
            <span style="color: #008000">//header and make sure the file was the correct</span>
            <span style="color: #008000">//length</span>

            <span style="color: #008000">//change the cursors position to the point</span>
            <span style="color: #008000">//in the header that contains the value / offset</span>
            <span style="color: #008000">//of where the actual bitmap data begins</span>
            <span style="color: #008000">//read in the 4 Bytes that contain the value</span>
            fs.position = BMP_DATA_OFFSET_POSITION;
            var dataPosition:<span style="color: #0000ff">int</span> = fs.readInt();

            <span style="color: #008000">//set cursor position to where the BMP</span>
            <span style="color: #008000">//width is stored</span>
            fs.position = WIDTH_POSITION;

            <span style="color: #008000">//read in the 4 Bytes that contain the width</span>
            var bmpWidth:<span style="color: #0000ff">int</span> = fs.readInt();

            <span style="color: #008000">//read in the 4 Bytes that contain the height</span>
            var bmpHeight:<span style="color: #0000ff">int</span> = fs.readInt();

            <span style="color: #008000">//set cursor to where the BMP pixel data begins</span>
            fs.position = dataPosition;

            var row:<span style="color: #0000ff">int</span> = 0;
            var column:<span style="color: #0000ff">int</span> = 0;

            <span style="color: #008000">//every row length in a BMP file must bee a multiple</span>
            <span style="color: #008000">//of 4 (see the spec). So, we need to determine how much</span>
            <span style="color: #008000">//padding we need to add at the end of each line. </span>
            var padding:<span style="color: #0000ff">int</span> = (bmpWidth % 4);

            <span style="color: #008000">//create a fixed length Vector to store the pixel</span>
            <span style="color: #008000">//values as we read them.</span>
            var pixels:Vector.&lt;<span style="color: #0000ff">uint</span>&gt; = <span style="color: #0000ff">new</span> Vector.&lt;<span style="color: #0000ff">uint</span>&gt;(bmpWidth * bmpHeight, <span style="color: #0000ff">true</span>);

            <span style="color: #008000">//loop through data (rows and columns)</span>
            <span style="color: #008000">//note that data stored in BMP is backwards to Flash and is</span>
            <span style="color: #008000">//stored from bottom row up, not top row down.</span>
            <span style="color: #008000">//So we have to loop backwards</span>
            var counter:<span style="color: #0000ff">int</span> = 0;
            <span style="color: #0000ff">for</span>(var i:<span style="color: #0000ff">int</span> = bmpHeight; i &gt; 0; i--)
            {
                <span style="color: #0000ff">for</span>(var k:<span style="color: #0000ff">int</span> = 0; k &lt; bmpWidth; k++)
                {

                    var position:<span style="color: #0000ff">int</span> = ((i - 1) * bmpWidth) + k;
                    <span style="color: #008000">/*</span>
<span style="color: #008000">                        This is the original code that I had which works fine</span>
<span style="color: #008000">                        but is not as effecient as what I have now.</span>
<span style="color: #008000">                        </span>
<span style="color: #008000">                        Basically, Pixels are stored within 3 sucessive Bytes</span>
<span style="color: #008000">                        in a BMP file, with one Byte each for Blue, Green and</span>
<span style="color: #008000">                        Red values (in that order).</span>
<span style="color: #008000">                        </span>
<span style="color: #008000">                        So, this reads the Bytes for each pixel, one at a time</span>
<span style="color: #008000">                        and then combines them into a single value which is</span>
<span style="color: #008000">                        the combined RGB pixel value.</span>
<span style="color: #008000">                        </span>
<span style="color: #008000">                        I left the code as I think it make it a little easier to</span>
<span style="color: #008000">                        understand what is going on, as well as how some of these</span>
<span style="color: #008000">                        calls can be optimized.</span>
<span style="color: #008000">                    */</span>

                    <span style="color: #008000">/*</span>
<span style="color: #008000">                    var blue:int = fs.readUnsignedByte();</span>
<span style="color: #008000">                    var green:int = fs.readUnsignedByte();</span>
<span style="color: #008000">                    var red:int = fs.readUnsignedByte();</span>
<span style="color: #008000">                    </span>
<span style="color: #008000">                    pixels[position] = (red &lt;&lt; 16 ^ green &lt;&lt; 8 ^ blue);</span>
<span style="color: #008000">                    */</span>

                    <span style="color: #008000">/*</span>
<span style="color: #008000">                        Here is the final code which is more efficient, as it only</span>
<span style="color: #008000">                        needs to make 2 read calls in order to get the values.</span>
<span style="color: #008000">                        </span>
<span style="color: #008000">                        Thanks to Thibault Imbert (bytearray.org) for pointing out</span>
<span style="color: #008000">                        and helping me understand the optimization.</span>
<span style="color: #008000">                    */</span>

                    <span style="color: #008000">//bytes in file are in Blue, Green, Red order</span>
                    <span style="color: #008000">//int is 32 bits (8 bytes). So, we store the first two bytes of the pixel</span>
                    <span style="color: #008000">// (which contain the Red value), and</span>
                    <span style="color: #008000">//then shift everything over 1 byte (8bits) to make room for</span>
                    <span style="color: #008000">//the green and blue values (remember the file is little endian), which we</span>
                    <span style="color: #008000">// then write into the int in the right position</span>
                    <span style="color: #008000">//The final value has the colors in the correct order (Red, Green, Blue)</span>

                    var pixelValue:<span style="color: #0000ff">uint</span> = fs.readUnsignedByte() | fs.readUnsignedShort() &lt;&lt; 8;
                    pixels[position] = pixelValue;
                }

                <span style="color: #008000">//we are at the end of the row, so now we have to move the cursor</span>
                <span style="color: #008000">//forward so it ends on a multiple of 4</span>
                <span style="color: #0000ff">if</span>(padding)
                {
                    fs.position += padding;
                }
            }

            <span style="color: #008000">//done reading file, close stream.</span>
            fs.close();

            <span style="color: #008000">//create a Rectangle with width / height of Bitmap</span>
            var rect:Rectangle = <span style="color: #0000ff">new</span> Rectangle(0, 0, bmpWidth, bmpHeight);

            <span style="color: #008000">//create the BitmapData object to hold hold the BMP data.</span>
            <span style="color: #008000">//we do a red fill here so it is easier to see if we have any errors</span>
            <span style="color: #008000">//in our code</span>
            var bmpData:BitmapData = <span style="color: #0000ff">new</span> BitmapData(bmpWidth, bmpHeight, <span style="color: #0000ff">false</span>, 0xFF0000);

            <span style="color: #008000">//copy the BMP pixel data into the BitmapData</span>
            bmpData.setVector(rect, pixels);

            <span style="color: #008000">//create a new Bitmap instance using the BitmapData</span>
            var bitmap:Bitmap = <span style="color: #0000ff">new</span> Bitmap(bmpData);
            bitmap.x = 10;
            bitmap.y = 10;

            <span style="color: #008000">//add Bitmap to the display list</span>
            addChild(bitmap);
        }
    }
}</pre>
</div>
<p><a href="http://www.mikechambers.com/blog/files/examples/BMPViewer.zip">Готовый пример</a> тоже можно скачать, что я уже сделала и терорризирую <a href="http://adobeusergroup.ru/">авиатора</a>, поскольку он спец по AS. </p>
]]></content:encoded>
			<wfw:commentRss>http://devnetwork.ru/parsing-bmp-sredstvami-actionscript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Представление Flash Catalyst</title>
		<link>http://devnetwork.ru/predstavlenie-flash-catalyst</link>
		<comments>http://devnetwork.ru/predstavlenie-flash-catalyst#comments</comments>
		<pubDate>Mon, 21 Sep 2009 11:33:00 +0000</pubDate>
		<dc:creator>tanya</dc:creator>
				<category><![CDATA[Flash / Flex / CF / DW]]></category>

		<guid isPermaLink="false">http://devnetwork.ru/predstavlenie-flash-catalyst</guid>
		<description><![CDATA[


]]></description>
			<content:encoded><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:23c874d2-7b07-471b-b4fe-bdf832bb1f31" class="wlWriterEditableSmartContent">
<div><object width="425" height="256"><param name="movie" value="http://images.tv.adobe.com//swf/player.swf"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="FlashVars" value="fileID=2182&amp;context=143&amp;embeded=true&amp;environment=production"></param><embed src="http://images.tv.adobe.com//swf/player.swf" flashvars="fileID=2182&#038;context=143&#038;embeded=true&#038;environment=production" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="256"></embed></object></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://devnetwork.ru/predstavlenie-flash-catalyst/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
