📄 pgxview.cs
字号:
using System;using System.IO;using System.Text;using System.Text.RegularExpressions;using System.Drawing;using System.Windows.Forms;using System.Security.Cryptography;namespace J2000.Utils { class PGXReader { private static string ReadLine(BinaryReader br) { StringBuilder sb=new StringBuilder(); char c; while ((c=br.ReadChar())!='\n') { sb.Append(c); } return sb.ToString(); } public static Bitmap Read(string filename, out string hash) { FileStream f=new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader br=new BinaryReader(f); Regex r=new Regex(@"[\s]+"); string[] head=r.Split(ReadLine(br)); Console.WriteLine(String.Join(",",head)); if (head[0]!="PG") throw new Exception("Not a PGX file"); bool bigendian=head[1]=="ML"; int prec=Math.Abs(int.Parse(head[2])); bool sgnd=int.Parse(head[2])<0; int w=int.Parse(head[3]); int h=int.Parse(head[4]); Console.WriteLine("prec="+prec+", sgnd="+sgnd+", w="+w+", h="+h); HashAlgorithm md5=new MD5CryptoServiceProvider(); CryptoStream cs=new CryptoStream(Stream.Null, md5, CryptoStreamMode.Write); BinaryWriter bw=new BinaryWriter(cs); Bitmap bmp=new Bitmap(w, h); for (int j=0; j<h; j++) { for (int i=0; i<w; i++) { int v, c; if (prec<=8) { v=sgnd?(int)br.ReadSByte():(int)br.ReadByte(); } else if (prec<=16) { v=sgnd?(int)br.ReadInt16():(int)br.ReadUInt16(); if (bigendian) v=System.Net.IPAddress.HostToNetworkOrder((short)v); } else { v=sgnd?(int)br.ReadInt32():(int)br.ReadUInt32(); if (bigendian) v=System.Net.IPAddress.HostToNetworkOrder(v); } if (sgnd) c=(int)(128+v*127.0/(1<<(prec-1))); else c=(int)(v*255.0/(1<<prec)); bmp.SetPixel(i, j, Color.FromArgb(255, c, c, c)); bw.Write(v); } } bw.Close(); hash=Convert.ToBase64String(md5.Hash); return bmp; } }}class PGXView:Form { public static int Main(string[] args) { Application.Run(new PGXView(args[0])); return 0; } static string hash; static void PrintMD5(object sender, EventArgs e) { MessageBox.Show(hash, "MD5"); } public PGXView(string filename) { Text="J2000.PGXView"; FormBorderStyle=FormBorderStyle.FixedToolWindow; PictureBox pbox=new PictureBox(); ContextMenu cm=new ContextMenu(); cm.MenuItems.Add("&MD5", new EventHandler(PrintMD5)); pbox.ContextMenu=cm; try { pbox.Image=J2000.Utils.PGXReader.Read(filename, out hash); pbox.SizeMode=PictureBoxSizeMode.AutoSize; ClientSize=pbox.Size; } catch (Exception e) { Console.WriteLine(e.Message); } Controls.Add(pbox); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -