• 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());
            }
        }
    }
  • 相关阅读:
    Windows 7下 搭建 基于 ssh 的sftp 服务器
    理解ThreadLocal(二)
    理解ThreadLocal(一)
    Putty使用公钥认证时,报错:Disconnected: No supported authentication methods available(server sent:public key) 问题的解决
    SFTP信任公钥配置及JSCH库
    怎样在WIN7系统下安装IIS
    深入研究B树索引(一)
    【转】EntityFramework(EF)贪婪加载和延迟加载的选择和使用
    【转】Ext JS 集合1713个icon图标的CSS文件
    [转]ASP.NET MVC 4 最佳实践宝典
  • 原文地址:https://www.cnblogs.com/Dah/p/846176.html
Copyright © 2020-2023  润新知