I have a camera that returns raw images that can easily be converted to a bitmap that can be saved to a file by the following C# method (that I did not write). From various sources, I have determined that the pictures have 8 bits per pixel, and may or may not be grayscale.
private void rawImgToBmp(byte[] imgData, String fname) { Bitmap bmp = new Bitmap(getWidth(), getHeight(), System.Drawing.Imaging.PixelFormat.Format8bppIndexed); for (int i = 0; i < 256; i++) { bmp.Palette.Entries[i] = Color.FromArgb(255, i, i, i); } //Copy the data from the byte array into the bitmap BitmapData bmpData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); Marshal.Copy(imgData, 0, bmpData.Scan0, getWidth() * getHeight()); bmp.UnlockBits(bmpData); //Unlock the pixels bmp.Save(FileName); }
My question is: how would I go about writing the equivalent method in C++, using built in functions of Windows CE 4.2?
erisu: thanks for the palette code, I think it’s right. I’ve resorted to filling in the rest of the structs manually, according to the Wikipedia page.
This is the code that works for me. It is based on erisu’s answer and Wikipedia’s description of the BMP format. For anyone else using this answer, I recommend that you understand the BMP format as fully as possible, so you can adjust the header fields accordingly.
The complicated loop at the end is my workaround for an issue with my hardware/OS, where it would not write all of the data I supplied to fwrite. It should work in any environment, though.