RAW文件的讀取

1.RAW結構是純像素數據文件,裏面只有每個像素的值,沒有文件頭、調色板等數據,所以要想正確顯示一個RAW文件圖像,必須人工指定它的長、寬和像素深度。
2.每個像素根據格式不同佔有不同的字節,如8位256色每個像素佔一個字節;24位真彩色每個像素佔三個字節。
3.要自己寫,注意:
(1)函數要有此RAW文件的長、寬和像素深度三個參數,從而得到BMP文件頭,存入strBMP[]的前面;
(2)函數裏把讀進來的RAW文件數據strRaw[]裏的數據進行行反轉放入strBMP[]中文件頭之後,即把第length-1-i行(從第0行開始記,i從0開始)的數據放到第i行,而每行裏的數據不變。這樣做是因爲BMP文件裏的像素數據是從最後一行即length-1開始的。
(3)使用顯示BMP文件的函數來顯示此strBMP[]裏的圖像文件。

 

 

1 參考代碼:
2 # #include "Raw.h"
3 #
4 # #ifdef _DEBUG
5 # #undef THIS_FILE
6 # static char THIS_FILE[]=__FILE__;
7 # #define new DEBUG_NEW
8 # #endif
9 #
10 # //////////////////////////////////////////////////////////////////////
11  # // Construction/Destruction
12  # //////////////////////////////////////////////////////////////////////
13  #
14 # CRaw::CRaw()
15 # //無參數初始化,不分配內存.
16 # {
17 # m_sizeImage= CSize(0,0);
18 # m_pBuff= NULL;
19 #
20 # }
21 #
22 # CRaw::CRaw(CSize sizeImage)
23 # //初始化,指定圖像大小,並分配相應的內存.
24 # {
25 # m_sizeImage= sizeImage;
26 # m_nWidth = m_sizeImage.cx;
27 # m_nHeight = m_sizeImage.cy;
28 # m_pBuff= new BYTE[sizeImage.cy*sizeImage.cx];
29 # memset(m_pBuff, 0, sizeImage.cy*sizeImage.cx*sizeof(BYTE));
30 # }
31 #
32 # CRaw::CRaw(CSize sizeImage, BYTE *pBuff)
33 # //初始化,sizeImage:圖像大小,pBuff:指向像素位的指針.
34 # {
35 # m_sizeImage= sizeImage;
36 # m_nWidth = m_sizeImage.cx;
37 # m_nHeight = m_sizeImage.cy;
38 # m_pBuff= new BYTE[sizeImage.cy*sizeImage.cx];
39 # memcpy(m_pBuff, pBuff, sizeImage.cy*sizeImage.cx*sizeof(BYTE));
40 # }
41 #
42 # CRaw::~CRaw()
43 # {
44 # if (m_pBuff!=NULL)
45 # delete m_pBuff;
46 #
47 # }
48 #
49 # //下面是從文件的路徑讀寫RAW格式的圖像, 這裏是文件存儲路徑
50 #
51 # BOOL CRaw::ReadFromFile(CString strFilename)
52 # //從文件中讀取Raw圖像,strFilename:源文件的完整路徑和文件名.
53 # {
54 # CFile file;
55 # CFileException ex;
56 # int nWidth, nHeight;
57 #
58 # CString strError1= "文件打開錯誤!";
59 # CString strError2= "非正確的raw格式文件!";
60 #
61 # if (!file.Open(strFilename, CFile::modeRead, &ex)){
62 # ex.ReportError();
63 # return FALSE;
64 # }
65 #
66 # if (file.Read(&nHeight, sizeof(int))!=sizeof(int)){
67 # AfxMessageBox(strError1, MB_OK|MB_ICONEXCLAMATION);
68 # file.Close();
69 # return FALSE;
70 # }
71 #
72 # if (file.Read(&nWidth, sizeof(int))!=sizeof(int)){
73 # AfxMessageBox(strError1, MB_OK|MB_ICONEXCLAMATION);
74 # file.Close();
75 # return FALSE;
76 # }
77 #
78 # m_sizeImage.cy= nHeight;
79 # m_sizeImage.cx= nWidth;
80 # m_nHeight = nHeight;
81 # m_nWidth = nWidth;
82 # m_pBuff= new BYTE[nHeight*nWidth];
83 #
84 # if (file.ReadHuge(m_pBuff, nHeight*nWidth)!=(nHeight*nWidth)){
85 # AfxMessageBox(strError2, MB_OK|MB_ICONEXCLAMATION);
86 # file.Close();
87 # return FALSE;
88 # }
89 #
90 # file.Close();
91 # return TRUE;
92 # }
93 #
94 #
95 # BOOL CRaw::WriteToFile(CString strFilename)
96 # //將Raw圖像寫到文件, strFilename:目標文件的完整路徑和文件名.
97 # {
98 # CFile file;
99 # CFileException ex;
100 # int nHeight, nWidth;
101 #
102 # nHeight= m_sizeImage.cy;
103 # nWidth= m_sizeImage.cx;
104 #
105 # if (!file.Open(strFilename, CFile::modeCreate|CFile::modeWrite, &ex)){
106 # ex.ReportError();
107 # return FALSE;
108 # }
109 #
110 # file.Write(&nHeight, sizeof(int));
111 # file.Write(&nWidth, sizeof(int));
112 #
113 # file.WriteHuge(m_pBuff, nHeight*nWidth*sizeof(BYTE));
114 #
115 # file.Close();
116 #
117 # return TRUE;
118 #
119 # }
120 #
121 # // 這下面是RAW圖像格式和BITMAP圖像格式的相互間的交互轉換
122 # CDib* CRaw::GetDib()
123 # //由Raw圖像獲得Dib位圖.
124 # {
125 # CDib* pDib= new CDib(m_sizeImage, 8);
126 # BYTE* pColorTable= (BYTE*) pDib->m_lpvColorTable;
127 # BYTE* pImage;
128 # CSize sizeDib;
129 # int nX, nY;
130 #
131 # if (m_sizeImage.cx%4==0)
132 # sizeDib.cx=m_sizeImage.cx;
133 # else
134 # sizeDib.cx=((m_sizeImage.cx)/4+1)*4;
135 # sizeDib.cy=m_sizeImage.cy;
136 #
137 # for (int i=0; i<256; i++){
138 # pColorTable[i*4]= i;
139 # pColorTable[i*4+1]= i;
140 # pColorTable[i*4+2]= i;
141 # pColorTable[i*4+3]= 0;
142 # }
143 #
144 # pImage= new BYTE[sizeDib.cy*sizeDib.cx];
145 # memset(pImage, 0, sizeDib.cy*sizeDib.cx);
146 #
147 # for (nY=0; nY<m_sizeimage.cy; ny++)="" for="" (nx="0;" nx=""><m_sizeimage.cx; nx++)="" pimage[ny*sizedib.cx+nx]="m_pBuff[(m_sizeImage.cy-1-nY)*m_sizeImage.cx+nX];" pdib-="">m_lpImage= pImage;
148 # return pDib;
149 # }
150 #
151 # BOOL CRaw::GetFromDib(CDib *pDib)
152 # //由Dib位圖獲得Raw圖像.
153 # {
154 # int nX, nY;
155 # int nDibWidth;
156 # BYTE* pImage= pDib->m_lpImage;
157 #
158 # if (pDib->m_lpBMIH->biBitCount!=8)
159 # return FALSE;
160 #
161 # m_sizeImage= pDib->GetDimensions();
162 # m_nWidth = m_sizeImage.cx;
163 # m_nHeight = m_sizeImage.cy;
164 # if ( (m_sizeImage.cx%4)!=0 )
165 # nDibWidth= (m_sizeImage.cx/4+1)*4;
166 # else
167 # nDibWidth= m_sizeImage.cx;
168 #
169 # m_pBuff= new BYTE[m_sizeImage.cx*m_sizeImage.cy];
170 #
171 # for (nY=0; nY<m_sizeimage.cy; ny++)="" for="" (nx="0;" nx=""><m_sizeimage.cx; nx++)="" m_pbuff[ny*m_sizeimage.cx+nx]="pImage[(m_sizeImage.cy-1-nY)*nDibWidth+nX];" return="" true;="" }="" void="" craw::serialize(carchive="" &ar)="" {="" dword="" dwpos;="" dwpos="ar.GetFile()-">GetPosition();
172 # TRACE("CRaw::Serialize -- pos = %d/n", dwPos);
173 # ar.Flush();
174 # dwPos = ar.GetFile()->GetPosition();
175 # TRACE("CRwa::Serialize -- pos = %d/n", dwPos);
176 #
177 # if(ar.IsStoring()) {
178 # Write(ar.GetFile());
179 # }
180 # else {
181 # Read(ar.GetFile());
182 # }
183 # }
184 #
185 # //下面是從文件中讀RAW圖像,以及向文件中寫RAW圖像
186 # BOOL CRaw::Write(CFile *pFile)
187 # {
188 # int nHeight, nWidth;
189 # nHeight= m_sizeImage.cy;
190 # nWidth= m_sizeImage.cx;
191 #
192 # try {
193 # pFile->Write(&nHeight, sizeof(int));
194 # pFile->Write(&nWidth, sizeof(int));
195 # pFile->WriteHuge(m_pBuff, nHeight*nWidth);
196 # }
197 # catch (CException *pe){
198 # pe->Delete();
199 # AfxMessageBox("File wirte error!", IDOK);
200 # return FALSE;
201 # }
202 #
203 # return TRUE;
204 # }
205 #
206 # BOOL CRaw::Read(CFile *pFile)
207 # {
208 # int nHeight, nWidth;
209 #
210 # try {
211 # pFile->Read(&nHeight, sizeof(int));
212 # pFile->Read(&nWidth, sizeof(int));
213 # m_nWidth = nWidth;
214 # m_nHeight - nHeight;
215 # m_sizeImage.cx= nWidth;
216 # m_sizeImage.cy= nHeight;
217 #
218 # m_pBuff= new BYTE[nHeight*nWidth];
219 #
220 # int nCount= pFile->ReadHuge(m_pBuff, nHeight*nWidth);
221 # if (nCount!=nWidth*nHeight)
222 # throw new CException;
223 # }
224 # catch (CException *pe){
225 # pe->Delete();
226 # AfxMessageBox("File read error!", IDOK);
227 # return FALSE;
228 # }
229 #
230 # return TRUE;
231 # }
232 #
233 #
234 # void CRaw::Empty()
235 # {
236 # if (m_pBuff!=NULL)
237 # delete m_pBuff;
238 # m_pBuff = NULL;
239 #
240 # }
241 #
242 # BOOL CRaw::IsEmpty()
243 # {
244 # if(m_pBuff != NULL)
245 # return FALSE;
246 # return TRUE;
247 # }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章