C#
2014.05.13 13:11
RAW to BMP Image Class
다물칸 주소복사
조회 수 861 추천 수 0 댓글 0
구분 | 자료 |
---|---|
출처 | 내가작성 |
class gDamuriRawToImage { private ArrayList pixels16; // To hold the pixel data Graphics g; // Graphics of the panel object int width, height; // Dimensions of the bitmap Bitmap bmp; public void LoadRawImage(string fileName) { pixels16 = new ArrayList(); ReadImageFile(fileName); string destFileName = Path.Combine(ProcGetFilePath(fileName), ProcGetFileName2(fileName) + ".bmp"); CreateBitmap(destFileName); } private string ProcGetFilePath(string strFile) // 경로를 포함한 파일에서 경로만 가져오는 함수 { string strRet = ""; if (strFile == "") return strRet; strRet = Path.GetDirectoryName(strFile); return strRet; } private string ProcGetFileName2(string strPath) // 경로나 파일에서 확장자를 포함한 파일명을 가져오는 함수 { string strRet = ""; if (strPath == "") return strRet; strRet = Path.GetFileName(strPath); return strRet; } // Read the image file, and populate the array list pixels16. private void ReadImageFile(string fileName) { BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)); ushort pixShort; int i; long iTotalSize = br.BaseStream.Length; for (i = 0; i < iTotalSize; i += 2) { pixShort = (ushort)(br.ReadInt16()); pixels16.Add(pixShort); } br.Close(); width = (int)(Math.Sqrt(pixels16.Count)); height = width; } // Create the Bitmap object and populate its pixel data with the stored pixel values. private void CreateBitmap(string DestFileName) { bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); // This 'unsafe' part of the code populates the bitmap bmp with data stored in pixel16. // It does so using pointers, and therefore the need for 'unsafe'. unsafe { int pixelSize = 3; int i, j, j1, i1; byte b; ushort sVal; double lPixval; for (i = 0; i < bmd.Height; ++i) { byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride); i1 = i * bmd.Height; for (j = 0; j < bmd.Width; ++j) { sVal = (ushort)(pixels16[i1 + j]); lPixval = (sVal / 255.0); // Convert to a 255 value range if (lPixval > 255) lPixval = 255; if (lPixval < 0) lPixval = 0; b = (byte)(lPixval); j1 = j * pixelSize; row[j1] = b; // Red row[j1 + 1] = b; // Green row[j1 + 2] = b; // Blue } } } bmp.UnlockBits(bmd); bmp.Save(DestFileName); } }