<!DOCTYPE html>
<html>
<head profile="http://gmpg.org/xfn/11">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>画像フォーマット　PPM　その3 | Code Life</title>
  <meta name="description" content="EVERYTHING WILL BE OK">
  
  
  
  <link rel="stylesheet" href="/wp-content/themes/flat/style.css" type="text/css">
  <link rel="stylesheet" href="/wp-content/themes/flat/comment-style.css" type="text/css">
  <link rel="stylesheet" href="/wp-content/themes/flat/japanese.css" type="text/css">
  
  <script type='text/javascript' src='/wp-includes/js/jquery/jquery.js?ver=1.11.0'></script>
  <script type='text/javascript' src='/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
  
  
  <link rel='prev' title='javascriptのデータ型まとめ' href='/?p=709'>
  <link rel='next' title='FMS接続時のおまじない' href='/?p=722'>
  <link rel='canonical' href='/?p=717'>
  <link rel='shortlink' href='/?p=717'>
  <link rel="stylesheet" href="/wp-content/plugins/shutter-reloaded/shutter-reloaded.css?ver=2.4" type="text/css" media="screen">
  <style type="text/css">
  .recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}
  </style>
  
  <style type="text/css">
  .wp_social_bookmarking_light{
    border: 0 !important;
    padding: 10px 0 20px 0 !important;
    margin: 0 !important;
  }
  .wp_social_bookmarking_light div{
    float: left !important;
    border: 0 !important;
    padding: 0 !important;
    margin: 0 5px 0px 0 !important;
    min-height: 30px !important;
    line-height: 18px !important;
    text-indent: 0 !important;
  }
  .wp_social_bookmarking_light img{
    border: 0 !important;
    padding: 0;
    margin: 0;
    vertical-align: top !important;
  }
  .wp_social_bookmarking_light_clear{
    clear: both !important;
  }
  
  
  
  </style>
  <style type="text/css" id="syntaxhighlighteranchor"></style>
  <script type="text/javascript" src="/wp-content/themes/flat/js/jscript.js"></script>
  <script type="text/javascript" src="/wp-content/themes/flat/js/comment.js"></script>
  <script type="text/javascript" src="/wp-content/themes/flat/js/rollover.js"></script>
  <style type="text/css">
  a, .post .title a:hover, .post_meta a:hover, #bread_crumb ul li a:hover, #previous_post a:hover, #next_post a:hover, .post a.more-link:hover, #right_col li a:hover, #copyright li a:hover, #archive_headline #keyword,
   #comments_wrapper a:hover, #comment_header_right .comment_switch_active a, #comment_header_right .comment_switch_active a:hover, #comment_pager .current
   { color:#00A19E; }

  #no_post a.back:hover, #wp-calendar td a:hover, #wp-calendar #prev a:hover, #wp-calendar #next a:hover, .page_navi a:hover, #submit_comment:hover
  { background-color:#00A19E; }

  #guest_info input:focus, #comment_textarea textarea:focus
  { border:1px solid #00A19E; }

  a:hover
  { color:#2F8EBD; }

  body { font-size:14px; }



  </style>
</head>
<body>
  <div id="header">
    <div class="header_menu"></div>
    <h1 class="logo_text"><a href="/">Code Life</a></h1>
    <h2 id='site_description'>EVERYTHING WILL BE OK</h2>
  </div>
  <div id="main_content" class="clearfix">
    <div id="left_col">
      <div id="bread_crumb">
        <ul class="clearfix">
          <li id="bc_home">
            <a href="/" title="ホーム">ホーム</a>
          </li>
          <li id="bc_cat">
            <a href="/?cat=7" title="C# の投稿をすべて表示">C#</a>
          </li>
          <li class="last">画像フォーマット　PPM　その3</li>
        </ul>
      </div>
      <div class="post_wrap clearfix" id="single">
        <div class="post">
          <h3 class="title"><span>画像フォーマット　PPM　その3</span></h3>
          <div class="post_content">
            <div class='wp_social_bookmarking_light'>
              
              
              <div class="wsbl_hatena_button">
                <a href="http://b.hatena.ne.jp/entry/https://code-life.net/?p=717" class="hatena-bookmark-button" data-hatena-bookmark-title="画像フォーマット　PPM　その3" data-hatena-bookmark-layout="standard" title="このエントリーをはてなブックマークに追加"><img src="//b.hatena.ne.jp/images/entry-button/button-only@2x.png" alt="このエントリーをはてなブックマークに追加" width="20" height="20" style="border: none;"></a> 
                <script type="text/javascript" src="//b.hatena.ne.jp/js/bookmark_button.js" charset="utf-8" async="async"></script>
              </div>
              <div class="wsbl_evernote">
                <a href="#" onclick="Evernote.doClip({ title:'画像フォーマット　PPM　その3', url:'https://code-life.net/?p=717' });return false;"><img src="http://static.evernote.com/article-clipper.png"></a>
              </div>
            </div><br class='wp_social_bookmarking_light_clear'>
            <p>chakemiです。風邪がまったく治りません。。。<br>
            更新が遅くなってしまいましたが、本日はPPMファイルをダンプして<br>
            BMPファイルに書き出してみました。<br>
            <span id="more-717"></span><br>
            開発環境：<br>
            Windows7<br>
            VisualStudio2010Express</p>
            <p>今回は、処理がめんどくさかったので、バイナリ形式（P6）のファイルのみを対象としました。<br>
            また、コメント行を保持した24bitカラーのファイルのみ処理するようにしました。</p>
            <p>単純に上から順に読み込んで処理をするだけですが、<br>
            まぁ素人なんでこんなもんでしょ。<br>
            &nbsp;<br>
            コード全体</p>
            <pre class="brush: csharp; title: ; notranslate" title="">
using System;
using System.IO;
using System.Collections;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace PpmTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int readSize;
            byte[] P = new byte[2];
            byte[] lf = new byte[1];
            string line=&quot;&quot;;
            long fPos;

            FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read);

            readSize = fs.Read(P, 0, P.Length);
            string bufString = Encoding.UTF8.GetString(P);
            if (bufString != &quot;P6&quot;)
            {
                Console.WriteLine(&quot;PPMファイルがバイナリ形式ではありません&quot;);
                fs.Dispose();
                return;
            }

            //改行コード読み飛ばし
            readSize = fs.Read(lf, 0, lf.Length);
            bufString = Encoding.UTF8.GetString(lf);
            fPos = fs.Position;


            StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(&quot;UTF-8&quot;));
            line = sr.ReadLine();
            if(line.Substring(0,1) != &quot;#&quot;)
            {
                //コメント行のないファイルは対象外
                Console.WriteLine(&quot;対象外ファイルです&quot;);
                fs.Dispose();
                return;
            }
            Console.WriteLine(line);
            fs.Seek(line.Length +fPos, SeekOrigin.Begin);


            //改行コード読み飛ばし
            readSize = fs.Read(lf, 0, lf.Length);
            bufString = Encoding.UTF8.GetString(lf);
            fPos = fs.Position;

            //横・縦サイズ取得
            line = sr.ReadLine();
            Console.WriteLine(line);

            string[] size = line.Split(new char[]{' '});
            int x = int.Parse(size[0]);
            int y = int.Parse(size[1]);
            fs.Seek(line.Length+fPos, SeekOrigin.Begin);
            
            //改行コード読み飛ばし
            readSize = fs.Read(lf, 0, lf.Length);
            bufString = Encoding.UTF8.GetString(lf);
            fPos = fs.Position;

            line = sr.ReadLine();
            Console.WriteLine(line);
            if (line != &quot;255&quot;)
            {
                Console.WriteLine(&quot;24bitカラーではありません&quot;);
                fs.Dispose();
                return;
            }
            fs.Seek(line.Length+fPos, SeekOrigin.Begin);

            //改行コード読み飛ばし
            readSize = fs.Read(lf, 0, lf.Length);
            bufString = Encoding.UTF8.GetString(lf);

            
            byte[,] datar = new byte[x, y];
            byte[,] datag = new byte[x, y];
            byte[,] datab = new byte[x, y];


            for (int i = 0; i &amp;lt; y; i++)
            {
                for (int j = 0; j &amp;lt; x; j++)
                {
                    datar[j, i] = (Byte)fs.ReadByte();
                    datag[j, i] = (Byte)fs.ReadByte();
                    datab[j, i] = (Byte)fs.ReadByte();
                }
            }

            Bitmap bitmap = new Bitmap(x, y, PixelFormat.Format24bppRgb);
            int[,] r = new int[x, y];
            int[,] g = new int[x, y];
            int[,] b = new int[x, y];


            for (int yy = 0; yy &amp;lt; y; yy++)
            {
                for (int xx = 0; xx &amp;lt; x; xx++)
                {
                    r[xx, yy] = ((int)datar[xx, yy]);
                    g[xx, yy] = ((int)datag[xx, yy]);
                    b[xx, yy] = ((int)datab[xx, yy]);

                    bitmap.SetPixel(xx, yy,
                    Color.FromArgb(
                    r[xx, yy],
                    g[xx, yy],
                    b[xx, yy]
                    ));

                }
            }

            Console.WriteLine(&amp;quot;complete&amp;quot;);
            bitmap.Save(&amp;quot;outbmp.bmp&amp;quot;);
        }
    }
}

</pre>
            <p>とりあえず、変換したビットマップ<br>
            <a href="/wp-content/uploads/2011/11/outbmp.png"><img src="/wp-content/uploads/2011/11/outbmp-300x300.png" alt="" width="300" height="300" class="alignnone size-medium wp-image-718"></a><br>
            うん、とりあえず、ちゃんと確認したから大丈夫なはず。</p>
            <p>ヘッダが単純すぎて、逆に不定な値をどうとったらお洒落か考えているうちに時間なくなっちゃって、<br>
            若干、怪しい部分もありますが、引き続き勉強のためにちゃんと書き直そうと思います。</p>
          </div>
        </div>
        <div class="meta">
          <ul>
            <li class="post_date clearfix"><span class="date">05</span> <span class="month">Nov</span> <span class="year">2011</span></li>
            <li class="post_category">
              <a href="/?cat=7" title="C# の投稿をすべて表示" rel="category">C#</a>
            </li>
            <li class="post_tag">
              <a href="/?tag=c" rel="tag">C#</a>, <a href="/?tag=ppm" rel="tag">ppm</a>
            </li>
            <li class="post_comment">
              <a href="/?p=717#respond" title="画像フォーマット　PPM　その3 へのコメント">コメントを書く</a>
            </li>
          </ul>
        </div>
      </div>
      <div id="comments_wrapper">
        <div id="comment_header" class="clearfix">
          <ul id="comment_header_left">
            <li id="add_comment">
              <a href="#respond">コメントを書く</a>
            </li>
            
          </ul>
          <ul id="comment_header_right">
            
            <li id="comment_switch" class="comment_switch_active">
              <a href="javascript:void(0);">コメント ( 0 )</a>
            </li>
          </ul>
        </div>
        <div id="comments">
          <div id="comment_area">
            <ol class="commentlist">
              <li class="comment">
                <div class="comment-content">
                  <p>コメント 0</p>
                </div>
              </li>
            </ol>
          </div>
          
          
        </div>
      </div>
      <div id="previous_next_post_single">
        <div class="clearfix">
          <p id="previous_post"><a href="/?p=709" rel="prev">javascriptのデータ型まとめ</a></p>
          <p id="next_post"><a href="/?p=722" rel="next">FMS接続時のおまじない</a></p>
        </div>
      </div>
      <script src="/wp-content/plugins/shutter-reloaded//shutter-reloaded.js?ver=2.5" type="text/javascript"></script> 
      <script type="text/javascript">


        var shutterSettings = {"imgDir":"http:\/\/code-life.net\/wp-content\/plugins\/shutter-reloaded\/\/menu\/","imageCount":true,"FS":false,"textBtns":false,"oneSet":true};
        try{shutterReloaded.init();}catch(e){}  
      </script>
    </div>
    <div id="container"></div>
    <div id="right_col">
      <div class="side_box clearfix" id="side_meta_content">
        <ul id="social_link" class="clearfix">
          
          
        </ul>
        
      </div>
      <div id="side_top">
        
        <div class="side_box widget_recent_entries" id="recent-posts-3">
          <h3 class="side_title">最近の投稿</h3>
          <ul>
            <li>
              <a href="/?p=2903">ブログ移転しました</a>
            </li>
            <li>
              <a href="/?p=2893">[CakePHP2] CssController could not be found が発生する場合</a>
            </li>
            <li>
              <a href="/?p=2889">[MySQL] DB と ユーザーを作成する</a>
            </li>
            <li>
              <a href="/?p=2875">[CakePHP2] Htmlヘルパーで出力されるタグを変更する</a>
            </li>
            <li>
              <a href="/?p=2864">[CakePHP2] モデルからログインしているユーザー情報を取得する</a>
            </li>
          </ul>
        </div>
        <div class="side_box widget_recent_comments" id="recent-comments-3">
          <h3 class="side_title">最近のコメント</h3>
          <ul id="recentcomments">
            <li class="recentcomments">
              <a href="/?p=850#comment-521">連想配列のarray_push</a> に 通りすがり より
            </li>
            
            <li class="recentcomments">
              <a href="/?p=1895#comment-496">HTML5の新要素にidやclassを設定する理由</a> に ゆうり より
            </li>
            <li class="recentcomments">
              <a href="/?p=2726#comment-355">HiveServerを使用してPHPからHiveQLを実行する</a> に <a href='/?p=2766' rel='external nofollow' class='url'>ThriftHiveClient を少し便利にしてみた | Code Life</a> より
            </li>
            
          </ul>
        </div>
        <div class="side_box widget_archive" id="archives-3">
          <h3 class="side_title">アーカイブ</h3>
          <ul>
            <li>
              <a href='/?m=201304'>2013年4月</a>
            </li>
            <li>
              <a href='/?m=201303'>2013年3月</a>
            </li>
            <li>
              <a href='/?m=201302'>2013年2月</a>
            </li>
            <li>
              <a href='/?m=201212'>2012年12月</a>
            </li>
            <li>
              <a href='/?m=201210'>2012年10月</a>
            </li>
            <li>
              <a href='/?m=201209'>2012年9月</a>
            </li>
            <li>
              <a href='/?m=201208'>2012年8月</a>
            </li>
            <li>
              <a href='/?m=201207'>2012年7月</a>
            </li>
            <li>
              <a href='/?m=201206'>2012年6月</a>
            </li>
            <li>
              <a href='/?m=201205'>2012年5月</a>
            </li>
            <li>
              <a href='/?m=201204'>2012年4月</a>
            </li>
            <li>
              <a href='/?m=201203'>2012年3月</a>
            </li>
            <li>
              <a href='/?m=201202'>2012年2月</a>
            </li>
            <li>
              <a href='/?m=201201'>2012年1月</a>
            </li>
            <li>
              <a href='/?m=201112'>2011年12月</a>
            </li>
            <li>
              <a href='/?m=201111'>2011年11月</a>
            </li>
            <li>
              <a href='/?m=201110'>2011年10月</a>
            </li>
            <li>
              <a href='/?m=201109'>2011年9月</a>
            </li>
            <li>
              <a href='/?m=201108'>2011年8月</a>
            </li>
          </ul>
        </div>
        <div class="side_box widget_categories" id="categories-3">
          <h3 class="side_title">カテゴリー</h3>
          <ul>
            <li class="cat-item cat-item-12">
              <a href="/?cat=12" title="ActionScript3.0 に含まれる投稿をすべて表示">ActionScript3.0</a> (42)
            </li>
            <li class="cat-item cat-item-11">
              <a href="/?cat=11" title="Apache に含まれる投稿をすべて表示">Apache</a> (7)
            </li>
            <li class="cat-item cat-item-7">
              <a href="/?cat=7" title="C# に含まれる投稿をすべて表示">C#</a> (57)
            </li>
            <li class="cat-item cat-item-5">
              <a href="/?cat=5" title="CakePHP に含まれる投稿をすべて表示">CakePHP</a> (37)
            </li>
            <li class="cat-item cat-item-153">
              <a href="/?cat=153" title="css に含まれる投稿をすべて表示">css</a> (8)
            </li>
            <li class="cat-item cat-item-10">
              <a href="/?cat=10" title="eclipse に含まれる投稿をすべて表示">eclipse</a> (9)
            </li>
            <li class="cat-item cat-item-198">
              <a href="/?cat=198" title="Hadoop に含まれる投稿をすべて表示">Hadoop</a> (1)
            </li>
            <li class="cat-item cat-item-199">
              <a href="/?cat=199" title="Hive に含まれる投稿をすべて表示">Hive</a> (2)
            </li>
            <li class="cat-item cat-item-151">
              <a href="/?cat=151" title="HTML に含まれる投稿をすべて表示">HTML</a> (10)
            </li>
            <li class="cat-item cat-item-9">
              <a href="/?cat=9" title="Java に含まれる投稿をすべて表示">Java</a> (4)
            </li>
            <li class="cat-item cat-item-8">
              <a href="/?cat=8" title="javascript に含まれる投稿をすべて表示">javascript</a> (13)
            </li>
            <li class="cat-item cat-item-152">
              <a href="/?cat=152" title="JQuery に含まれる投稿をすべて表示">JQuery</a> (4)
            </li>
            <li class="cat-item cat-item-6">
              <a href="/?cat=6" title="Linux に含まれる投稿をすべて表示">Linux</a> (22)
            </li>
            <li class="cat-item cat-item-17">
              <a href="/?cat=17" title="Mac に含まれる投稿をすべて表示">Mac</a> (20)
            </li>
            <li class="cat-item cat-item-201">
              <a href="/?cat=201" title="Movable Type に含まれる投稿をすべて表示">Movable Type</a> (5)
            </li>
            <li class="cat-item cat-item-19">
              <a href="/?cat=19" title="MySQL に含まれる投稿をすべて表示">MySQL</a> (3)
            </li>
            <li class="cat-item cat-item-200">
              <a href="/?cat=200" title="Nginx に含まれる投稿をすべて表示">Nginx</a> (2)
            </li>
            <li class="cat-item cat-item-4">
              <a href="/?cat=4" title="PHP に含まれる投稿をすべて表示">PHP</a> (58)
            </li>
            <li class="cat-item cat-item-197">
              <a href="/?cat=197" title="Vim に含まれる投稿をすべて表示">Vim</a> (9)
            </li>
            <li class="cat-item cat-item-16">
              <a href="/?cat=16" title="Windows に含まれる投稿をすべて表示">Windows</a> (71)
            </li>
            <li class="cat-item cat-item-1">
              <a href="/?cat=1" title="未分類 に含まれる投稿をすべて表示">未分類</a> (45)
            </li>
          </ul>
        </div>
      </div>
      <div id="side_middle" class="clearfix">
        <div id="side_left"></div>
        <div id="side_right"></div>
      </div>
      <div id="side_bottom"></div>
    </div>
    <div id="footer">
      <ul id="copyright">
        <li>Copyright &copy;&nbsp; <a href="/">Code Life</a>
        </li>
        <li>Theme designed by <a class="target_blank" href="http://www.mono-lab.net/">mono-lab</a>
        </li>
        <li class="last">Powered by <a class="target_blank" href="http://wordpress.org/">WordPress</a>
        </li>
      </ul>
    </div>
  </div>
  <p id="return_top"><a href="#header">return top</a></p>
   
  <script type="text/javascript" src="http://static.evernote.com/noteit.js"></script> 
  <script type='text/javascript' src='/wp-content/plugins/akismet/_inc/form.js?ver=3.0.1'></script> 
  <script type='text/javascript' src='/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/scripts/shCore.js?ver=3.0.9'></script> 
  <script type='text/javascript' src='/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/scripts/shBrushCSharp.js?ver=3.0.9'></script> 
  <script type='text/javascript'>


        (function(){
                var corecss = document.createElement('link');
                var themecss = document.createElement('link');
                var corecssurl = "https://code-life.net/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shCore.css?ver=3.0.9";
                if ( corecss.setAttribute ) {
                                corecss.setAttribute( "rel", "stylesheet" );
                                corecss.setAttribute( "type", "text/css" );
                                corecss.setAttribute( "href", corecssurl );
                } else {
                                corecss.rel = "stylesheet";
                                corecss.href = corecssurl;
                }
                document.getElementsByTagName("head")[0].insertBefore( corecss, document.getElementById("syntaxhighlighteranchor") );
                var themecssurl = "https://code-life.net/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shThemeMidnight.css?ver=3.0.9";
                if ( themecss.setAttribute ) {
                                themecss.setAttribute( "rel", "stylesheet" );
                                themecss.setAttribute( "type", "text/css" );
                                themecss.setAttribute( "href", themecssurl );
                } else {
                                themecss.rel = "stylesheet";
                                themecss.href = themecssurl;
                }
                //document.getElementById("syntaxhighlighteranchor").appendChild(themecss);
                document.getElementsByTagName("head")[0].insertBefore( themecss, document.getElementById("syntaxhighlighteranchor") );
        })();
        SyntaxHighlighter.config.strings.expandSource = 'ソースを表示';
        SyntaxHighlighter.config.strings.help = 'SyntaxHighlighterについて';
        SyntaxHighlighter.config.strings.alert = 'SyntaxHighlighter\n\n';
        SyntaxHighlighter.config.strings.noBrush = '指定のブラシが見つかりませんでした: ';
        SyntaxHighlighter.config.strings.brushNotHtmlScript = 'HTMLスクリプトのオプションのためにブラシが構成されませんでした: ';
        SyntaxHighlighter.defaults['pad-line-numbers'] = true;
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.defaults['wrap-lines'] = false;
        SyntaxHighlighter.all();
  </script> 
  <script type='text/javascript' src='/wp-includes/js/comment-reply.min.js?ver=3.9.2'></script>
</body>
</html>
