c# 图像处理

在写dnf资源提取的时候,在网上找到的很好图像处理的代码。。。。。

 

 

代码
    
  1 private unsafe Bitmap FmtArgb1555(string DatName, Bitmap DatBitmap)
  2     {
  3         Bitmap bitmap;
  4         using (FileStream stream = new FileStream(DatName, FileMode.Open))
  5         {
  6             using (BinaryReader reader = new BinaryReader(stream))
  7             {
  8                 BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
  9                                                            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
 10                 byte* numPtr = (byte*) bitmapdata.Scan0;
 11                 int num = DatBitmap.Width * 4;
 12                 for (int i = 0; i < DatBitmap.Height; i++)
 13                 {
 14                     for (int j = 0; j < num; j += 4)
 15                     {
 16                         int num4 = reader.ReadUInt16();
 17                         numPtr[3] = (byte) ((num4 >> 15) * 0xff);
 18                         numPtr[2] = (byte) ((num4 >> 7) & 0xf8);
 19                         numPtr[1] = (byte) ((num4 >> 2) & 0xf8);
 20                         numPtr[0] = (byte) ((num4 << 3) & 0xf8);
 21                         numPtr += 4;
 22                     }
 23                 }
 24                 DatBitmap.UnlockBits(bitmapdata);
 25                 bitmap = DatBitmap;
 26             }
 27         }
 28         return bitmap;
 29     }
 30 
 31     private unsafe Bitmap FmtArgb4444(string DatName, Bitmap DatBitmap)
 32     {
 33         Bitmap bitmap;
 34         using (FileStream stream = new FileStream(DatName, FileMode.Open))
 35         {
 36             using (BinaryReader reader = new BinaryReader(stream))
 37             {
 38                 BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
 39                                                            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
 40                 byte* numPtr = (byte*) bitmapdata.Scan0;
 41                 int num = DatBitmap.Width * 4;
 42                 for (int i = 0; i < DatBitmap.Height; i++)
 43                 {
 44                     for (int j = 0; j < num; j += 4)
 45                     {
 46                         int num4 = reader.ReadUInt16();
 47                         numPtr[3] = (byte) ((num4 >> 8) & 240);
 48                         numPtr[2] = (byte) ((num4 >> 4) & 240);
 49                         numPtr[1] = (byte) (num4 & 240);
 50                         numPtr[0] = (byte) ((num4 << 4) & 240);
 51                         numPtr += 4;
 52                     }
 53                 }
 54                 DatBitmap.UnlockBits(bitmapdata);
 55                 bitmap = DatBitmap;
 56             }
 57         }
 58         return bitmap;
 59     }
 60 
 61     private unsafe Bitmap FmtArgb8888(string DatName, Bitmap DatBitmap)
 62     {
 63         Bitmap bitmap;
 64         using (FileStream stream = new FileStream(DatName, FileMode.Open))
 65         {
 66             using (BinaryReader reader = new BinaryReader(stream))
 67             {
 68                 BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height), 
 69                                                            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
 70                 byte* numPtr = (byte*) bitmapdata.Scan0;
 71                 int num = DatBitmap.Width * 4;
 72                 for (int i = 0; i < DatBitmap.Height; i++)
 73                 {
 74                     for (int j = 0; j < num; j += 4)
 75                     {
 76                         uint num4 = reader.ReadUInt32();
 77                         numPtr[3] = (byte) (num4 >> 0x18);
 78                         numPtr[2] = (byte) ((num4 >> 0x10) & 0xff);
 79                         numPtr[1] = (byte) ((num4 >> 8) & 0xff);
 80                         numPtr[0] = (byte) (num4 & 0xff);
 81                         numPtr += 4;
 82                     }
 83                 }
 84                 DatBitmap.UnlockBits(bitmapdata);
 85                 bitmap = DatBitmap;
 86             }
 87         }
 88         return bitmap;
 89     }
 90 
 91     private unsafe Bitmap FmtDxt1(string DatName, Bitmap DatBitmap)
 92     {
 93         byte[] sourceArray = new byte[] { 
 94             0x44, 0x44, 0x53, 0x20, 0x7c, 0, 0, 0, 7, 0x10, 8, 0, 0, 4, 0, 0, 
 95             0, 4, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
 96             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
 97             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
 98             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0, 
 99             4, 0, 0, 0, 0x44, 0x58, 0x54, 0x31, 0, 0, 0, 0, 0, 0, 0, 0, 
100             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10, 0, 0, 
101             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
102          };
103         sourceArray[13] = (byte) (DatBitmap.Height / 0x100);
104         sourceArray[0x11] = (byte) (DatBitmap.Width / 0x100);
105         byte[] buffer2 = File.ReadAllBytes(DatName);
106         byte[] destinationArray = new byte[sourceArray.Length + buffer2.Length];
107         Array.Copy(sourceArray, 0, destinationArray, 0, sourceArray.Length);
108         Array.Copy(buffer2, 0, destinationArray, sourceArray.Length, buffer2.Length);
109         using (Stream stream = new MemoryStream(destinationArray))
110         {
111             DdsFile file = new DdsFile();
112             file.Load(stream);
113             byte[] pixelData = file.GetPixelData();
114             BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
115                                                        ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
116             int num = DatBitmap.Width * 4;
117             byte* numPtr = (byte*) bitmapdata.Scan0;
118             for (int i = 0; i < DatBitmap.Height; i++)
119             {
120                 for (int j = 0; j < num; j += 4)
121                 {
122                     int index = (i * num) + j;
123                     numPtr[2] = pixelData[index];
124                     numPtr[1] = pixelData[index + 1];
125                     numPtr[0] = pixelData[index + 2];
126                     numPtr[3] = pixelData[index + 3];
127                     numPtr += 4;
128                 }
129             }
130             DatBitmap.UnlockBits(bitmapdata);
131             return DatBitmap;
132         }
133     }
134 
135     private unsafe Bitmap FmtDxt5(string DatName, Bitmap DatBitmap)
136     {
137         byte[] sourceArray = new byte[] { 
138             0x44, 0x44, 0x53, 0x20, 0x7c, 0, 0, 0, 7, 0x10, 8, 0, 0, 4, 0, 0, 
139             0, 4, 0, 0, 0, 0, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
140             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
141             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
142             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0, 
143             4, 0, 0, 0, 0x44, 0x58, 0x54, 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 
144             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10, 0, 0, 
145             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
146          };
147         sourceArray[13] = (byte) (DatBitmap.Height / 0x100);
148         sourceArray[0x11] = (byte) (DatBitmap.Width / 0x100);
149         byte[] buffer2 = File.ReadAllBytes(DatName);
150         byte[] destinationArray = new byte[sourceArray.Length + buffer2.Length];
151         Array.Copy(sourceArray, 0, destinationArray, 0, sourceArray.Length);
152         Array.Copy(buffer2, 0, destinationArray, sourceArray.Length, buffer2.Length);
153         using (Stream stream = new MemoryStream(destinationArray))
154         {
155             DdsFile file = new DdsFile();
156             file.Load(stream);
157             byte[] pixelData = file.GetPixelData();
158             BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
159                                                        ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
160             int num = DatBitmap.Width * 4;
161             byte* numPtr = (byte*) bitmapdata.Scan0;
162             for (int i = 0; i < DatBitmap.Height; i++)
163             {
164                 for (int j = 0; j < num; j += 4)
165                 {
166                     int index = (i * num) + j;
167                     numPtr[2] = pixelData[index];
168                     numPtr[1] = pixelData[index + 1];
169                     numPtr[0] = pixelData[index + 2];
170                     numPtr[3] = pixelData[index + 3];
171                     numPtr += 4;
172                 }
173             }
174             DatBitmap.UnlockBits(bitmapdata);
175             return DatBitmap;
176         }
177     }
178 
179     private unsafe Bitmap FmtRgb555(string DatName, Bitmap DatBitmap)
180     {
181         Bitmap bitmap;
182         using (FileStream stream = new FileStream(DatName, FileMode.Open))
183         {
184             using (BinaryReader reader = new BinaryReader(stream))
185             {
186                 BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
187                                                            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
188                 byte* numPtr = (byte*) bitmapdata.Scan0;
189                 int num = DatBitmap.Width * 4;
190                 for (int i = 0; i < DatBitmap.Height; i++)
191                 {
192                     for (int j = 0; j < num; j += 4)
193                     {
194                         int num4 = reader.ReadUInt16();
195                         numPtr[3] = 0xff;
196                         numPtr[2] = (byte) ((num4 >> 7) & 0xf8);
197                         numPtr[1] = (byte) ((num4 >> 2) & 0xf8);
198                         numPtr[0] = (byte) ((num4 << 3) & 0xf8);
199                         numPtr += 4;
200                     }
201                 }
202                 DatBitmap.UnlockBits(bitmapdata);
203                 bitmap = DatBitmap;
204             }
205         }
206         return bitmap;
207     }
208 
209     private unsafe Bitmap FmtRgb565(string DatName, Bitmap DatBitmap)
210     {
211         Bitmap bitmap;
212         using (FileStream stream = new FileStream(DatName, FileMode.Open))
213         {
214             using (BinaryReader reader = new BinaryReader(stream))
215             {
216                 BitmapData bitmapdata = DatBitmap.LockBits(new Rectangle(0, 0, DatBitmap.Width, DatBitmap.Height),
217                                                            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
218                 byte* numPtr = (byte*) bitmapdata.Scan0;
219                 int num = DatBitmap.Width * 4;
220                 for (int i = 0; i < DatBitmap.Height; i++)
221                 {
222                     for (int j = 0; j < num; j += 4)
223                     {
224                         int num4 = reader.ReadUInt16();
225                         numPtr[3] = 0xff;
226                         numPtr[2] = (byte) ((num4 >> 8) & 0xf8);
227                         numPtr[1] = (byte) ((num4 >> 3) & 0xfc);
228                         numPtr[0] = (byte) ((num4 << 3) & 0xf8);
229                         numPtr += 4;
230                     }
231                 }
232                 DatBitmap.UnlockBits(bitmapdata);
233                 bitmap = DatBitmap;
234             }
235         }
236         return bitmap;
237     }
238 

 

 

 

posted @ 2010-07-06 17:52  四条腿的人马  阅读(267)  评论(0)    收藏  举报