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 # }