|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32test/multimedia/t_camera_bitmap.cpp |
|
15 // This is a basic Windows bitmap file writer, that can be used for converting YUV422 data into |
|
16 // RGB format and dumping it to a Windows .bmp file, for manual examination. |
|
17 // |
|
18 // |
|
19 |
|
20 #include <e32test.h> |
|
21 #include <f32file.h> |
|
22 #include "t_camera_bitmap.h" |
|
23 |
|
24 #define CLIP(a) if (a < 0) a = 0; else if (a > 255) a = 255; |
|
25 |
|
26 /** |
|
27 Converts a RGB565 buffer into 24 bit RGB format in a second buffer. |
|
28 @param aDest Pointer to the buffer into which to place the 24 bit RGB data |
|
29 @param aSource Pointer to the buffer containing the RGB565 data to be converted |
|
30 @param aWidth The width of the data in the buffer in pixels |
|
31 @param aHeight The height of the data in the buffer in pixels |
|
32 */ |
|
33 void RBitmap::RGBToRGB(TUint8* aDest, TUint8* aSource, TInt aWidth, TInt aHeight) |
|
34 { |
|
35 TUint16* source = (TUint16*) aSource; |
|
36 TUint16 pixel; |
|
37 |
|
38 for (TInt y = 0; y < aHeight; ++y) |
|
39 { |
|
40 for (TInt x = 0; x < aWidth; ++x) |
|
41 { |
|
42 pixel = *source++; |
|
43 *aDest++ = (TUint8) ((pixel & 0xf800) >> 8); |
|
44 *aDest++ = (TUint8) ((pixel & 0x07e0) >> 3); |
|
45 *aDest++ = (TUint8) ((pixel & 0x001f) << 3); |
|
46 } |
|
47 } |
|
48 } |
|
49 |
|
50 /** |
|
51 Converts a YUV422 buffer into 24 bit RGB format in a second buffer. |
|
52 @param aDest Pointer to the buffer into which to place the 24 bit RGB data |
|
53 @param aSource Pointer to the buffer containing the YUV422 data to be converted |
|
54 @param aWidth The width of the data in the buffer in pixels |
|
55 @param aHeight The height of the data in the buffer in pixels |
|
56 */ |
|
57 void RBitmap::YUVToRGB(TUint8* aDest, TUint8* aSource, TInt aWidth, TInt aHeight) |
|
58 { |
|
59 TInt y, u, v, r, g, b; |
|
60 |
|
61 aDest += ((aWidth * 3) * (aHeight - 1)); |
|
62 |
|
63 for (TInt l = 0; l < aHeight; ++l) |
|
64 { |
|
65 for (TInt x = 0; x < (aWidth / 2); ++x) |
|
66 { |
|
67 u = (aSource[0] - 128); |
|
68 v = (aSource[2] - 128); |
|
69 y = (aSource[3] - 16); |
|
70 |
|
71 r = ((298 * y + 409 * u) / 256); |
|
72 g = ((298 * y - 100 * v - 208 * u) / 256); |
|
73 b = ((298 * y + 516 * v) / 256); |
|
74 |
|
75 CLIP(r); |
|
76 CLIP(g); |
|
77 CLIP(b); |
|
78 |
|
79 *aDest++ = (TUint8) r; |
|
80 *aDest++ = (TUint8) g; |
|
81 *aDest++ = (TUint8) b; |
|
82 |
|
83 y = (aSource[1] - 16); |
|
84 |
|
85 r = ((298 * y + 409 * u) / 256); |
|
86 g = ((298 * y - 100 * v - 208 * u) / 256); |
|
87 b = ((298 * y + 516 * v) / 256); |
|
88 |
|
89 CLIP(r); |
|
90 CLIP(g); |
|
91 CLIP(b); |
|
92 |
|
93 *aDest++ = (TUint8) r; |
|
94 *aDest++ = (TUint8) g; |
|
95 *aDest++ = (TUint8) b; |
|
96 aSource += 4; |
|
97 } |
|
98 |
|
99 aDest -= (aWidth * 3 * 2); |
|
100 } |
|
101 } |
|
102 |
|
103 /** |
|
104 Converts a 32 bit long from whatever the host format is into little endian format in a user supplied buffer. |
|
105 @param aBuffer Pointer to the buffer into which to write the value |
|
106 @param aLong The value to be written |
|
107 */ |
|
108 void RBitmap::PutLong(TUint8* aBuffer, TInt aLong) |
|
109 { |
|
110 *aBuffer++ = (TUint8) (aLong & 0xff); |
|
111 *aBuffer++ = (TUint8) ((aLong >> 8) & 0xff); |
|
112 *aBuffer++ = (TUint8) ((aLong >> 16) & 0xff); |
|
113 *aBuffer++ = (TUint8) ((aLong >> 24) & 0xff); |
|
114 } |
|
115 |
|
116 /** |
|
117 Converts a 16 bit short from whatever the host format is into little endian format in a user supplied buffer. |
|
118 @param aBuffer Pointer to the buffer into which to write the value |
|
119 @param aShort The value to be written |
|
120 */ |
|
121 void RBitmap::PutShort(TUint8* aBuffer, TInt16 aShort) |
|
122 { |
|
123 *aBuffer++ = (TUint8) (aShort & 0xff); |
|
124 *aBuffer++ = (TUint8) ((aShort >> 8) & 0xff); |
|
125 } |
|
126 |
|
127 /** |
|
128 Writes a standard Windows .bmp header to a file, including the standard .bmp file header, followed |
|
129 by a V3 DIB header. |
|
130 @param aFile A reference to the file to which to write the header |
|
131 @param aWidth The width of the bitmap in pixels |
|
132 @param aHeight The height of the bitmap in pixels |
|
133 @return KErrNone if write was successful, otherwise one of the other system wide error codes |
|
134 */ |
|
135 TInt RBitmap::WriteHeader(RFile& aFile, TInt aWidth, TInt aHeight) |
|
136 { |
|
137 TBuf8<14> header(14); |
|
138 TUint8* buffer = (TUint8*) header.Ptr(); |
|
139 |
|
140 header.Fill(0); |
|
141 |
|
142 header[0] = 'B'; |
|
143 header[1] = 'M'; |
|
144 PutLong((buffer + 2), (14 + 40 + (aWidth * aHeight * 3))); |
|
145 PutLong((buffer + 10), (14 + 40)); |
|
146 |
|
147 TInt r = aFile.Write(header); |
|
148 |
|
149 if (r == KErrNone) |
|
150 { |
|
151 TBuf8<40> bitmapInfoHeader(40); |
|
152 TUint8* buffer = (TUint8*) bitmapInfoHeader.Ptr(); |
|
153 |
|
154 bitmapInfoHeader.Fill(0); |
|
155 |
|
156 PutLong(buffer, 40); |
|
157 PutLong((buffer + 4), aWidth); |
|
158 PutLong((buffer + 8), aHeight); |
|
159 PutShort((buffer + 12), 1); |
|
160 PutShort((buffer + 14), 24); |
|
161 PutLong((buffer + 20), (aWidth * aHeight * 3)); |
|
162 |
|
163 r = aFile.Write(bitmapInfoHeader); |
|
164 } |
|
165 |
|
166 return r; |
|
167 } |
|
168 |
|
169 /** |
|
170 Converts a YUV422 or RGB565 buffer into 24 bit RGB format and writes it to a file. |
|
171 @param aFile A reference to the file to which to write the data |
|
172 @param aBuffer A pointer to the buffer containing the data to be converted and written |
|
173 @param aPixelFormat UID specifying the format of the source data |
|
174 @param aWidth The width of the data in the buffer in pixels |
|
175 @param aHeight The height of the data in the buffer in pixels |
|
176 @return KErrNone if write was successful, otherwise one of the other system wide error codes |
|
177 */ |
|
178 TInt RBitmap::WriteBitmapData(RFile& aFile, TUint8* aBuffer, SDevCamPixelFormat aPixelFormat, TInt aWidth, TInt aHeight) |
|
179 { |
|
180 TInt length = (aWidth * aHeight * 3); |
|
181 TUint8* rgbBuffer = new TUint8[length]; |
|
182 |
|
183 TInt r = KErrNone; |
|
184 |
|
185 if (rgbBuffer) |
|
186 { |
|
187 if (aPixelFormat.iPixelFormat == EUidPixelFormatYUV_422Interleaved) |
|
188 { |
|
189 YUVToRGB(rgbBuffer, aBuffer, aWidth, aHeight); |
|
190 } |
|
191 else if (aPixelFormat.iPixelFormat == EUidPixelFormatRGB_565) |
|
192 { |
|
193 RGBToRGB(rgbBuffer, aBuffer, aWidth, aHeight); |
|
194 } |
|
195 else |
|
196 { |
|
197 r = KErrNotSupported; |
|
198 } |
|
199 |
|
200 if (r == KErrNone) |
|
201 { |
|
202 TPtr8 buffer(rgbBuffer, length, length); |
|
203 r = aFile.Write(buffer); |
|
204 } |
|
205 |
|
206 delete [] rgbBuffer; |
|
207 } |
|
208 else |
|
209 { |
|
210 r = KErrNoMemory; |
|
211 } |
|
212 |
|
213 return r; |
|
214 } |
|
215 |
|
216 /** |
|
217 Converts a YUV422 or RGB565 buffer into 24 bit RGB format and writes it to a Windows .bmp file. |
|
218 @param aFileName A reference to the fully qualified name of the file to write the .bmp file to |
|
219 @param aBuffer A pointer to the buffer containing the data to be converted and written |
|
220 @param aPixelFormat UID specifying the format of the source data |
|
221 @param aWidth The width of the data in the buffer in pixels |
|
222 @param aHeight The height of the data in the buffer in pixels |
|
223 @return KErrNone if write was successful, otherwise one of the other system wide error codes |
|
224 */ |
|
225 TInt RBitmap::WriteBMP(const TDesC& aFileName, TUint8* aBuffer, SDevCamPixelFormat aPixelFormat, TInt aWidth, TInt aHeight) |
|
226 { |
|
227 TInt r; |
|
228 RFile file; |
|
229 RFs fs; |
|
230 |
|
231 if ((r = fs.Connect()) == KErrNone) |
|
232 { |
|
233 if ((r = file.Replace(fs, aFileName, EFileWrite)) == KErrNone) |
|
234 { |
|
235 if ((r = WriteHeader(file, aWidth, aHeight)) == KErrNone) |
|
236 { |
|
237 r = WriteBitmapData(file, aBuffer, aPixelFormat, aWidth, aHeight); |
|
238 } |
|
239 |
|
240 file.Close(); |
|
241 |
|
242 // If anything went wrong, delete the file so that we do not leave partial files that |
|
243 // might cause confusion |
|
244 if (r != KErrNone) |
|
245 { |
|
246 fs.Delete(aFileName); |
|
247 } |
|
248 } |
|
249 |
|
250 fs.Close(); |
|
251 } |
|
252 |
|
253 return r; |
|
254 } |
|
255 |
|
256 /** |
|
257 Dumps a buffer straight to disk, without any kind of processing. |
|
258 @param aFileName A reference to the fully qualified name of the file to write the file to |
|
259 @param aBuffer A pointer to the buffer containing the data to be converted written |
|
260 @param aSize The size of the buffer to be written, in bytes |
|
261 @return KErrNone if write was successful, otherwise one of the other system wide error codes |
|
262 */ |
|
263 TInt RBitmap::WriteBuffer(const TDesC& aFileName, TUint8* aBuffer, TInt aSize) |
|
264 { |
|
265 TInt r; |
|
266 RFile file; |
|
267 RFs fs; |
|
268 |
|
269 if ((r = fs.Connect()) == KErrNone) |
|
270 { |
|
271 if ((r = file.Replace(fs, aFileName, EFileWrite)) == KErrNone) |
|
272 { |
|
273 TPtrC8 buffer(aBuffer, aSize); |
|
274 |
|
275 r = file.Write(buffer); |
|
276 |
|
277 file.Close(); |
|
278 |
|
279 // If anything went wrong, delete the file so that we do not leave partial files that |
|
280 // might cause confusion |
|
281 if (r != KErrNone) |
|
282 { |
|
283 fs.Delete(aFileName); |
|
284 } |
|
285 } |
|
286 |
|
287 fs.Close(); |
|
288 } |
|
289 |
|
290 return r; |
|
291 } |