0xAA55 发表于 2014-2-16 01:42:40

【C】将8位256色BMP图片黑白化的一个C语言程序

<style type="text/css">
span {
        font-family: '新宋体';
        font-size: 9pt;
        color: #000000;
}
.sc0 {
}
.sc2 {
        color: #008000;
}
.sc3 {
        color: #008080;
}
.sc4 {
        color: #FF0000;
}
.sc5 {
        color: #0000FF;
}
.sc6 {
        color: #FF0000;
}
.sc9 {
        color: #0000FF;
}
.sc10 {
        color: #000080;
}
.sc11 {
}
.sc16 {
        color: #0000FF;
}
</style>
<div style="float: left; white-space: pre; line-height: 1; background: #FFFFFF; "><span class="sc3">/******************************************************************************
技术宅的结界:<a href="http://www.0xaa55.com/?fromuid=1" target="_blank">http://www.0xAA55.com/</a>

大家放心,我一定是按照最没有节操的方式编写的。相信这点难度是一定难不倒大家的。
我相信大家是一定能把它读懂的!
用法:
BMPGRAY 输入文件.BMP 输出文件.BMP
注意输入文件必须是8位256色的BMP位图。嗯,就这些。
为了能让这个程序兼容DOS、c4droid等,我只让它输出英文信息,而且不包含windows.h
******************************************************************************/</span><span class="sc0">

</span><span class="sc9">#include&lt;stdio.h&gt;
#include&lt;malloc.h&gt;
</span><span class="sc0">
</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">signed</span><span class="sc0"> </span><span class="sc16">long</span><span class="sc0">   </span><span class="sc11">LONG</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">unsigned</span><span class="sc0"> </span><span class="sc16">char</span><span class="sc0">   </span><span class="sc11">BYTE</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">unsigned</span><span class="sc0"> </span><span class="sc5">short</span><span class="sc0"></span><span class="sc11">WORD</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">unsigned</span><span class="sc0"> </span><span class="sc16">long</span><span class="sc0">   </span><span class="sc11">DWORD</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc9">#pragma pack(push,1)
</span><span class="sc0">
</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">struct</span><span class="sc0"> </span><span class="sc11">tagBITMAPFILEHEADER</span><span class="sc0">
</span><span class="sc10">{</span><span class="sc0">
    </span><span class="sc11">WORD</span><span class="sc0">    </span><span class="sc11">bfType</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">bfSize</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">WORD</span><span class="sc0">    </span><span class="sc11">bfReserved1</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">WORD</span><span class="sc0">    </span><span class="sc11">bfReserved2</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">bfOffBits</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc10">}</span><span class="sc11">BITMAPFILEHEADER</span><span class="sc10">,*</span><span class="sc11">PBITMAPFILEHEADER</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">struct</span><span class="sc0"> </span><span class="sc11">tagBITMAPINFOHEADER</span><span class="sc0">
</span><span class="sc10">{</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">biSize</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">LONG</span><span class="sc0">    </span><span class="sc11">biWidth</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">LONG</span><span class="sc0">    </span><span class="sc11">biHeight</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">WORD</span><span class="sc0">    </span><span class="sc11">biPlanes</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">WORD</span><span class="sc0">    </span><span class="sc11">biBitCount</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">biCompression</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">biSizeImage</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">LONG</span><span class="sc0">    </span><span class="sc11">biXPelsPerMeter</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">LONG</span><span class="sc0">    </span><span class="sc11">biYPelsPerMeter</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">biClrUsed</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">biClrImportant</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc10">}</span><span class="sc11">BITMAPINFOHEADER</span><span class="sc10">,*</span><span class="sc11">PBITMAPINFOHEADER</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc5">typedef</span><span class="sc0"> </span><span class="sc16">struct</span><span class="sc0"> </span><span class="sc11">tagRGBQUAD</span><span class="sc0">
</span><span class="sc10">{</span><span class="sc0">
    </span><span class="sc11">BYTE</span><span class="sc0">    </span><span class="sc11">rgbBlue</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">BYTE</span><span class="sc0">    </span><span class="sc11">rgbGreen</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">BYTE</span><span class="sc0">    </span><span class="sc11">rgbRed</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">BYTE</span><span class="sc0">    </span><span class="sc11">rgbReserved</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc10">}</span><span class="sc11">RGBQUAD</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc9">#pragma pack(pop)
</span><span class="sc0">
</span><span class="sc11">RGBQUAD</span><span class="sc0"> </span><span class="sc11">PALGray</span><span class="sc10">[</span><span class="sc4">256</span><span class="sc10">]={</span><span class="sc4">0</span><span class="sc10">};</span><span class="sc0">
</span><span class="sc11">RGBQUAD</span><span class="sc0"> </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc4">256</span><span class="sc10">]={</span><span class="sc4">0</span><span class="sc10">};</span><span class="sc0">

</span><span class="sc16">static</span><span class="sc0"> </span><span class="sc16">char</span><span class="sc0"> </span><span class="sc11">szReadFail</span><span class="sc10">[]=</span><span class="sc6">"Unable to read data from %s\n"</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc16">static</span><span class="sc0"> </span><span class="sc16">char</span><span class="sc0"> </span><span class="sc11">szWriteFail</span><span class="sc10">[]=</span><span class="sc6">"Write data to %s failed.\n"</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc16">int</span><span class="sc0"> </span><span class="sc11">main</span><span class="sc10">(</span><span class="sc16">int</span><span class="sc0"> </span><span class="sc11">argc</span><span class="sc10">,</span><span class="sc16">char</span><span class="sc0"> </span><span class="sc10">**</span><span class="sc11">argv</span><span class="sc10">)</span><span class="sc0">
</span><span class="sc10">{</span><span class="sc0">
    </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">argc</span><span class="sc10">&gt;</span><span class="sc4">2</span><span class="sc10">)</span><span class="sc0">
    </span><span class="sc10">{</span><span class="sc2">//参数个数必须是2个以上
</span><span class="sc0">      </span><span class="sc11">FILE</span><span class="sc0"> </span><span class="sc10">*</span><span class="sc11">fp1</span><span class="sc10">,*</span><span class="sc11">fp2</span><span class="sc10">;</span><span class="sc0">
      </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">=</span><span class="sc11">fopen</span><span class="sc10">(</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">],</span><span class="sc6">"rb"</span><span class="sc10">))</span><span class="sc0">
      </span><span class="sc10">{</span><span class="sc2">//打开来源文件
</span><span class="sc0">            </span><span class="sc11">BITMAPFILEHEADER</span><span class="sc0"> </span><span class="sc11">BMFH</span><span class="sc10">;</span><span class="sc0">
            </span><span class="sc11">BITMAPINFOHEADER</span><span class="sc0"> </span><span class="sc11">BMIF</span><span class="sc10">;</span><span class="sc0">
            </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fread</span><span class="sc10">(&amp;</span><span class="sc11">BMFH</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMFH</span><span class="sc10">),</span><span class="sc11">fp1</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMFH</span><span class="sc10">))</span><span class="sc0">
            </span><span class="sc10">{</span><span class="sc2">//读取BMP文件头
</span><span class="sc0">                </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">BMFH</span><span class="sc10">.</span><span class="sc11">bfType</span><span class="sc10">==</span><span class="sc4">0x4D42</span><span class="sc10">)</span><span class="sc0">
                </span><span class="sc10">{</span><span class="sc2">//文件头标记必须是"BM"
</span><span class="sc0">                  </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fread</span><span class="sc10">(&amp;</span><span class="sc11">BMIF</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">),</span><span class="sc11">fp1</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">))</span><span class="sc0">
                  </span><span class="sc10">{</span><span class="sc2">//读取BMP信息头
</span><span class="sc0">                        </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biSize</span><span class="sc10">==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">))</span><span class="sc0">
                        </span><span class="sc10">{</span><span class="sc2">//BMP信息头的大小必须是对的
</span><span class="sc0">                            </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biBitCount</span><span class="sc10">==</span><span class="sc4">8</span><span class="sc10">)</span><span class="sc0">
                            </span><span class="sc10">{</span><span class="sc2">//BMP必须为256色8位
</span><span class="sc0">                              </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fread</span><span class="sc10">(</span><span class="sc11">PALOrg</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">PALOrg</span><span class="sc10">),</span><span class="sc11">fp1</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">PALOrg</span><span class="sc10">))</span><span class="sc0">
                              </span><span class="sc10">{</span><span class="sc2">//读取调色板
</span><span class="sc0">                                    </span><span class="sc16">unsigned</span><span class="sc0"> </span><span class="sc16">int</span><span class="sc0"> </span><span class="sc11">i</span><span class="sc10">;</span><span class="sc0">
                                    </span><span class="sc11">WORD</span><span class="sc0"> </span><span class="sc11">wAvr</span><span class="sc10">;</span><span class="sc0">
                                    </span><span class="sc5">for</span><span class="sc10">(</span><span class="sc11">i</span><span class="sc10">=</span><span class="sc4">0</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">&lt;</span><span class="sc4">256</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">++)</span><span class="sc0">
                                    </span><span class="sc10">{</span><span class="sc2">//将调色板黑白化
</span><span class="sc0">                                        </span><span class="sc11">wAvr</span><span class="sc10">=(</span><span class="sc0">
                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbRed</span><span class="sc10">+</span><span class="sc0">
                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbGreen</span><span class="sc10">+</span><span class="sc0">
                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbBlue</span><span class="sc10">)/</span><span class="sc4">3</span><span class="sc10">;</span><span class="sc2">//取得调色板灰度
</span><span class="sc0">                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbRed</span><span class="sc10">=</span><span class="sc0">
                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbGreen</span><span class="sc10">=</span><span class="sc0">
                                        </span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbBlue</span><span class="sc10">=(</span><span class="sc11">BYTE</span><span class="sc10">)</span><span class="sc11">wAvr</span><span class="sc10">;</span><span class="sc2">//然后赋值
</span><span class="sc0">                                    </span><span class="sc10">}</span><span class="sc0">
                                    </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">=</span><span class="sc11">fopen</span><span class="sc10">(</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">],</span><span class="sc6">"wb"</span><span class="sc10">))</span><span class="sc0">
                                    </span><span class="sc10">{</span><span class="sc2">//打开目标文件准备写入
</span><span class="sc0">                                        </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fwrite</span><span class="sc10">(&amp;</span><span class="sc11">BMFH</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMFH</span><span class="sc10">),</span><span class="sc11">fp2</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMFH</span><span class="sc10">))</span><span class="sc0">
                                        </span><span class="sc10">{</span><span class="sc2">//写入BMP文件头
</span><span class="sc0">                                          </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fwrite</span><span class="sc10">(&amp;</span><span class="sc11">BMIF</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">),</span><span class="sc11">fp2</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">))</span><span class="sc0">
                                          </span><span class="sc10">{</span><span class="sc2">//写入BMP信息头
</span><span class="sc0">                                                </span><span class="sc5">for</span><span class="sc10">(</span><span class="sc11">i</span><span class="sc10">=</span><span class="sc4">0</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">&lt;</span><span class="sc4">256</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">++)</span><span class="sc0">
                                                    </span><span class="sc11">PALGray</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbRed</span><span class="sc10">=</span><span class="sc0">
                                                    </span><span class="sc11">PALGray</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbGreen</span><span class="sc10">=</span><span class="sc0">
                                                    </span><span class="sc11">PALGray</span><span class="sc10">[</span><span class="sc11">i</span><span class="sc10">].</span><span class="sc11">rgbBlue</span><span class="sc10">=(</span><span class="sc11">BYTE</span><span class="sc10">)</span><span class="sc11">i</span><span class="sc10">;</span><span class="sc2">//建立灰度调色板
</span><span class="sc0">                                                </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fwrite</span><span class="sc10">(</span><span class="sc11">PALGray</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">PALGray</span><span class="sc10">),</span><span class="sc11">fp2</span><span class="sc10">)==</span><span class="sc5">sizeof</span><span class="sc10">(</span><span class="sc11">PALGray</span><span class="sc10">))</span><span class="sc0">
                                                </span><span class="sc10">{</span><span class="sc2">//写入调色板
</span><span class="sc0">                                                    </span><span class="sc11">DWORD</span><span class="sc0">   </span><span class="sc11">dwPitch</span><span class="sc10">=((</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biWidth</span><span class="sc10">-</span><span class="sc4">1</span><span class="sc10">)/</span><span class="sc4">4</span><span class="sc10">+</span><span class="sc4">1</span><span class="sc10">)*</span><span class="sc4">4</span><span class="sc10">;</span><span class="sc2">//原图、目标图每行字节数
</span><span class="sc0">                                                    </span><span class="sc11">BYTE</span><span class="sc0">    </span><span class="sc10">*</span><span class="sc11">pBits</span><span class="sc10">=(</span><span class="sc11">BYTE</span><span class="sc10">*)</span><span class="sc11">malloc</span><span class="sc10">(</span><span class="sc11">dwPitch</span><span class="sc10">);</span><span class="sc2">//分配内存准备操作
</span><span class="sc0">                                                    </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">)</span><span class="sc0">
                                                    </span><span class="sc10">{</span><span class="sc2">//如果分配到了内存
</span><span class="sc0">                                                      </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biHeight</span><span class="sc10">&lt;</span><span class="sc4">0</span><span class="sc10">)</span><span class="sc2">//考虑到顶到下型位图
</span><span class="sc0">                                                            </span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biHeight</span><span class="sc10">=-</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biHeight</span><span class="sc10">;</span><span class="sc0">
                                                      </span><span class="sc5">for</span><span class="sc10">(</span><span class="sc11">i</span><span class="sc10">=</span><span class="sc4">0</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">&lt;</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biHeight</span><span class="sc10">;</span><span class="sc11">i</span><span class="sc10">++)</span><span class="sc0">
                                                      </span><span class="sc10">{</span><span class="sc0">
                                                            </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fread</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc11">dwPitch</span><span class="sc10">,</span><span class="sc11">fp1</span><span class="sc10">)==</span><span class="sc11">dwPitch</span><span class="sc10">)</span><span class="sc0">
                                                            </span><span class="sc10">{</span><span class="sc0">
                                                                </span><span class="sc5">for</span><span class="sc10">(</span><span class="sc16">unsigned</span><span class="sc0"> </span><span class="sc16">int</span><span class="sc0"> </span><span class="sc11">x</span><span class="sc10">=</span><span class="sc4">0</span><span class="sc10">;</span><span class="sc11">x</span><span class="sc10">&lt;</span><span class="sc11">BMIF</span><span class="sc10">.</span><span class="sc11">biWidth</span><span class="sc10">;</span><span class="sc11">x</span><span class="sc10">++)</span><span class="sc0">
                                                                  </span><span class="sc11">pBits</span><span class="sc10">[</span><span class="sc11">x</span><span class="sc10">]=</span><span class="sc11">PALOrg</span><span class="sc10">[</span><span class="sc11">pBits</span><span class="sc10">[</span><span class="sc11">x</span><span class="sc10">]].</span><span class="sc11">rgbRed</span><span class="sc10">;</span><span class="sc2">//根据亮度进行处理
</span><span class="sc0">                                                                </span><span class="sc5">if</span><span class="sc10">(</span><span class="sc11">fwrite</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">,</span><span class="sc4">1</span><span class="sc10">,</span><span class="sc11">dwPitch</span><span class="sc10">,</span><span class="sc11">fp2</span><span class="sc10">)!=</span><span class="sc11">dwPitch</span><span class="sc10">)</span><span class="sc0">
                                                                </span><span class="sc10">{</span><span class="sc2">//保存一行。
</span><span class="sc0">                                                                  </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szWriteFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                                                  </span><span class="sc11">free</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">);</span><span class="sc0">
                                                                  </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                                  </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                                  </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                                                </span><span class="sc10">}</span><span class="sc0">
                                                            </span><span class="sc10">}</span><span class="sc0">
                                                            </span><span class="sc5">else</span><span class="sc0">
                                                            </span><span class="sc10">{</span><span class="sc0">
                                                                </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szReadFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                                                                </span><span class="sc11">free</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">);</span><span class="sc0">
                                                                </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                                </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                                </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                                            </span><span class="sc10">}</span><span class="sc0">
                                                      </span><span class="sc10">}</span><span class="sc0">
                                                      </span><span class="sc11">free</span><span class="sc10">(</span><span class="sc11">pBits</span><span class="sc10">);</span><span class="sc0">
                                                      </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                      </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                      </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">0</span><span class="sc10">;</span><span class="sc0">
                                                    </span><span class="sc10">}</span><span class="sc0">
                                                    </span><span class="sc5">else</span><span class="sc0">
                                                    </span><span class="sc10">{</span><span class="sc0">
                                                      </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc6">"No enough memory or %s is too wide.\n"</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                                      </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                      </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                      </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                                    </span><span class="sc10">}</span><span class="sc0">
                                                </span><span class="sc10">}</span><span class="sc0">
                                                </span><span class="sc5">else</span><span class="sc0">
                                                </span><span class="sc10">{</span><span class="sc0">
                                                    </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szWriteFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                                    </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                    </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                    </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                                </span><span class="sc10">}</span><span class="sc0">
                                          </span><span class="sc10">}</span><span class="sc0">
                                          </span><span class="sc5">else</span><span class="sc0">
                                          </span><span class="sc10">{</span><span class="sc0">
                                                </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szWriteFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                                </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                                </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                                </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                          </span><span class="sc10">}</span><span class="sc0">
                                        </span><span class="sc10">}</span><span class="sc0">
                                        </span><span class="sc5">else</span><span class="sc0">
                                        </span><span class="sc10">{</span><span class="sc0">
                                          </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szWriteFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                          </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp2</span><span class="sc10">);</span><span class="sc0">
                                          </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                          </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                        </span><span class="sc10">}</span><span class="sc0">
                                    </span><span class="sc10">}</span><span class="sc0">
                                    </span><span class="sc5">else</span><span class="sc0">
                                    </span><span class="sc10">{</span><span class="sc0">
                                        </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szWriteFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">2</span><span class="sc10">]);</span><span class="sc0">
                                        </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                        </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                                    </span><span class="sc10">}</span><span class="sc0">
                              </span><span class="sc10">}</span><span class="sc0">
                              </span><span class="sc5">else</span><span class="sc0">
                              </span><span class="sc10">{</span><span class="sc0">
                                    </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szReadFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                                    </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                                    </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                              </span><span class="sc10">}</span><span class="sc0">
                            </span><span class="sc10">}</span><span class="sc0">
                            </span><span class="sc5">else</span><span class="sc0">
                            </span><span class="sc10">{</span><span class="sc0">
                              </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc6">"%s is not a 8-bit BMP file.\n"</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                              </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                              </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                            </span><span class="sc10">}</span><span class="sc0">
                        </span><span class="sc10">}</span><span class="sc0">
                        </span><span class="sc5">else</span><span class="sc0">
                        </span><span class="sc10">{</span><span class="sc0">
                            </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc6">"%s is not a valid BMP file.(BITMAPINFOHEADER)\n"</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                            </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                            </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                        </span><span class="sc10">}</span><span class="sc0">
                  </span><span class="sc10">}</span><span class="sc0">
                  </span><span class="sc5">else</span><span class="sc0">
                  </span><span class="sc10">{</span><span class="sc0">
                        </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szReadFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                        </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                        </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                  </span><span class="sc10">}</span><span class="sc0">
                </span><span class="sc10">}</span><span class="sc0">
                </span><span class="sc5">else</span><span class="sc0">
                </span><span class="sc10">{</span><span class="sc0">
                  </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc6">"%s is not a valid BMP file.(BITMAPFILEHEADER)\n"</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                  </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                  </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
                </span><span class="sc10">}</span><span class="sc0">
            </span><span class="sc10">}</span><span class="sc0">
            </span><span class="sc5">else</span><span class="sc0">
            </span><span class="sc10">{</span><span class="sc0">
                </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szReadFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
                </span><span class="sc11">fclose</span><span class="sc10">(</span><span class="sc11">fp1</span><span class="sc10">);</span><span class="sc0">
                </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
            </span><span class="sc10">}</span><span class="sc0">
      </span><span class="sc10">}</span><span class="sc0">
      </span><span class="sc5">else</span><span class="sc0">
      </span><span class="sc10">{</span><span class="sc0">
            </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc11">szReadFail</span><span class="sc10">,</span><span class="sc11">argv</span><span class="sc10">[</span><span class="sc4">1</span><span class="sc10">]);</span><span class="sc0">
            </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">2</span><span class="sc10">;</span><span class="sc0">
      </span><span class="sc10">}</span><span class="sc0">
    </span><span class="sc10">}</span><span class="sc0">
    </span><span class="sc5">else</span><span class="sc0">
    </span><span class="sc10">{</span><span class="sc0">
      </span><span class="sc11">printf</span><span class="sc10">(</span><span class="sc0">
            </span><span class="sc6">"USAGE:\n"</span><span class="sc0">
            </span><span class="sc6">"BMPGRAY INPUT.BMP OUTPUT.BMP\n"</span><span class="sc10">);</span><span class="sc0">
      </span><span class="sc5">return</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc10">}</span><span class="sc0">
</span><span class="sc10">}</span><span class="sc0">
</span></div>

0xAA55 发表于 2014-2-16 01:51:04

那么,二楼放源码和可执行文件EXE。
把下面这张图下载保存之后把后缀改成ZIP就能解压得到源码。MinGW编译通过。

0xAA55 发表于 2014-2-16 18:27:23

嗯,这才是真正的“面向过程”
看!缩进拳!

鱼头之王 发表于 2019-2-22 15:05:08

写成这样的缩进,会不会被老板打?

Ink_Hin_fifteen 发表于 2019-5-10 19:53:56

收藏收藏。
页: [1]
查看完整版本: 【C】将8位256色BMP图片黑白化的一个C语言程序