First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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.
22 #include "t_camera_bitmap.h"
24 #define CLIP(a) if (a < 0) a = 0; else if (a > 255) a = 255;
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
33 void RBitmap::RGBToRGB(TUint8* aDest, TUint8* aSource, TInt aWidth, TInt aHeight)
35 TUint16* source = (TUint16*) aSource;
38 for (TInt y = 0; y < aHeight; ++y)
40 for (TInt x = 0; x < aWidth; ++x)
43 *aDest++ = (TUint8) ((pixel & 0xf800) >> 8);
44 *aDest++ = (TUint8) ((pixel & 0x07e0) >> 3);
45 *aDest++ = (TUint8) ((pixel & 0x001f) << 3);
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
57 void RBitmap::YUVToRGB(TUint8* aDest, TUint8* aSource, TInt aWidth, TInt aHeight)
59 TInt y, u, v, r, g, b;
61 aDest += ((aWidth * 3) * (aHeight - 1));
63 for (TInt l = 0; l < aHeight; ++l)
65 for (TInt x = 0; x < (aWidth / 2); ++x)
67 u = (aSource[0] - 128);
68 v = (aSource[2] - 128);
69 y = (aSource[3] - 16);
71 r = ((298 * y + 409 * u) / 256);
72 g = ((298 * y - 100 * v - 208 * u) / 256);
73 b = ((298 * y + 516 * v) / 256);
79 *aDest++ = (TUint8) r;
80 *aDest++ = (TUint8) g;
81 *aDest++ = (TUint8) b;
83 y = (aSource[1] - 16);
85 r = ((298 * y + 409 * u) / 256);
86 g = ((298 * y - 100 * v - 208 * u) / 256);
87 b = ((298 * y + 516 * v) / 256);
93 *aDest++ = (TUint8) r;
94 *aDest++ = (TUint8) g;
95 *aDest++ = (TUint8) b;
99 aDest -= (aWidth * 3 * 2);
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
108 void RBitmap::PutLong(TUint8* aBuffer, TInt aLong)
110 *aBuffer++ = (TUint8) (aLong & 0xff);
111 *aBuffer++ = (TUint8) ((aLong >> 8) & 0xff);
112 *aBuffer++ = (TUint8) ((aLong >> 16) & 0xff);
113 *aBuffer++ = (TUint8) ((aLong >> 24) & 0xff);
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
121 void RBitmap::PutShort(TUint8* aBuffer, TInt16 aShort)
123 *aBuffer++ = (TUint8) (aShort & 0xff);
124 *aBuffer++ = (TUint8) ((aShort >> 8) & 0xff);
128 Writes a standard Windows .bmp header to a file, including the standard .bmp file header, followed
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
135 TInt RBitmap::WriteHeader(RFile& aFile, TInt aWidth, TInt aHeight)
137 TBuf8<14> header(14);
138 TUint8* buffer = (TUint8*) header.Ptr();
144 PutLong((buffer + 2), (14 + 40 + (aWidth * aHeight * 3)));
145 PutLong((buffer + 10), (14 + 40));
147 TInt r = aFile.Write(header);
151 TBuf8<40> bitmapInfoHeader(40);
152 TUint8* buffer = (TUint8*) bitmapInfoHeader.Ptr();
154 bitmapInfoHeader.Fill(0);
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));
163 r = aFile.Write(bitmapInfoHeader);
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
178 TInt RBitmap::WriteBitmapData(RFile& aFile, TUint8* aBuffer, SDevCamPixelFormat aPixelFormat, TInt aWidth, TInt aHeight)
180 TInt length = (aWidth * aHeight * 3);
181 TUint8* rgbBuffer = new TUint8[length];
187 if (aPixelFormat.iPixelFormat == EUidPixelFormatYUV_422Interleaved)
189 YUVToRGB(rgbBuffer, aBuffer, aWidth, aHeight);
191 else if (aPixelFormat.iPixelFormat == EUidPixelFormatRGB_565)
193 RGBToRGB(rgbBuffer, aBuffer, aWidth, aHeight);
197 r = KErrNotSupported;
202 TPtr8 buffer(rgbBuffer, length, length);
203 r = aFile.Write(buffer);
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
225 TInt RBitmap::WriteBMP(const TDesC& aFileName, TUint8* aBuffer, SDevCamPixelFormat aPixelFormat, TInt aWidth, TInt aHeight)
231 if ((r = fs.Connect()) == KErrNone)
233 if ((r = file.Replace(fs, aFileName, EFileWrite)) == KErrNone)
235 if ((r = WriteHeader(file, aWidth, aHeight)) == KErrNone)
237 r = WriteBitmapData(file, aBuffer, aPixelFormat, aWidth, aHeight);
242 // If anything went wrong, delete the file so that we do not leave partial files that
243 // might cause confusion
246 fs.Delete(aFileName);
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
263 TInt RBitmap::WriteBuffer(const TDesC& aFileName, TUint8* aBuffer, TInt aSize)
269 if ((r = fs.Connect()) == KErrNone)
271 if ((r = file.Replace(fs, aFileName, EFileWrite)) == KErrNone)
273 TPtrC8 buffer(aBuffer, aSize);
275 r = file.Write(buffer);
279 // If anything went wrong, delete the file so that we do not leave partial files that
280 // might cause confusion
283 fs.Delete(aFileName);