zoukankan      html  css  js  c++  java
  • Convert PGM To BMP in C#

    PGM(Portable Gray Map) is an image format which uses 1 or 2 bytes to store a grayscale pixel, the format specification is simple and straight forward, detail specification can be found here

    So, writing a simple converter from PGM to the most commonly used BMP format is really an easy job.

    Source:
    using System;
    using System.Text;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;

    namespace Adrian.Imaging.PGMConverter
    {
        
    public static class PGMUtil
        {
            
    private static ColorPalette grayScale;

            
    public static Bitmap ToBitmap(string filePath)
            {
                
    using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    
    using (BinaryReader reader = new BinaryReader(fs, Encoding.ASCII))
                    {
                        
    if (reader.ReadChar() == 'P' && reader.ReadChar() == '5')
                        {
                            reader.ReadChar();
                            
    int width = 0;
                            
    int height = 0;
                            
    int level = 0;
                            
    bool two = false;
                            StringBuilder sb 
    = new StringBuilder();
                            width 
    = ReadNumber(reader, sb);
                            height 
    = ReadNumber(reader, sb);
                            level 
    = ReadNumber(reader, sb);
                            two 
    = (level > 255);

                            Bitmap bmp 
    = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                            
    if (grayScale == null)
                            {
                                grayScale 
    = bmp.Palette;
                                
    for (int i = 0; i < 256; i++)
                                {
                                    grayScale.Entries[i] 
    = Color.FromArgb(i, i, i);
                                }
                            }
                            bmp.Palette 
    = grayScale;
                            BitmapData dt 
    = bmp.LockBits(new Rectangle(00, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                            
    int offset = dt.Stride - dt.Width;
                            
    unsafe
                            {
                                
    byte* ptr = (byte*)dt.Scan0;

                                
    for (int i = 0; i < height; i++)
                                {
                                    
    for (int j = 0; j < width; j++)
                                    {
                                        
    byte v;
                                        
    if (two)
                                        {
                                            v 
    = (byte)(((double)((reader.ReadByte() << 8+ reader.ReadByte()) / level) * 255.0);
                                        }
                                        
    else
                                        {
                                            v 
    = reader.ReadByte();
                                        }
                                        
    *ptr = v;
                                        ptr
    ++;
                                    }
                                    ptr 
    += offset;
                                }
                            }

                            bmp.UnlockBits(dt);
                            
    return bmp;
                        }
                        
    else
                        {
                            
    throw new InvalidOperationException("target file is not a PGM file");
                        }
                    }
                }
            }

            
    public static void SaveAsBitmap(string src, string dest)
            {
                ToBitmap(src).Save(dest, ImageFormat.Bmp);
            }

            
    private static int ReadNumber(BinaryReader reader, StringBuilder sb)
            {
                
    char c = '\0';
                sb.Length 
    = 0;
                
    while (Char.IsDigit(c = reader.ReadChar()))
                {
                    sb.Append(c);
                }
                
    return int.Parse(sb.ToString());
            }
        }
    }
  • 相关阅读:
    MLPclassifier,MLP 多层感知器的的缩写(Multi-layer Perceptron)
    linux 内存不足时候 应该及时回收page cache
    关闭swap的危害——一旦内存耗尽,由于没有SWAP的缓冲,系统会立即开始OOM
    使用Networkx进行图的相关计算——黑产集团挖掘,我靠,可以做dns ddos慢速攻击检测啊
    ARIMA模型实例讲解——网络流量预测可以使用啊
    http://www.secrepo.com 安全相关的数据获取源
    什么是HTTP Referer?
    列举某域名下所有二级域名的方法
    HMM(隐马尔科夫模型)——本质上就是要预测出股市的隐藏状态(牛市、熊市、震荡、反弹等)和他们之间的转移概率
    成都优步uber司机第五组奖励政策
  • 原文地址:https://www.cnblogs.com/Dah/p/846176.html
Copyright © 2011-2022 走看看