CapturePanorama.CapturePanorama.SaveCubemapImage C# (CSharp) Method

SaveCubemapImage() private method

private SaveCubemapImage ( uint cameraPixels, string filenameBase, string suffix, string imagePath, int i, int bufferIdx ) : void
cameraPixels uint
filenameBase string
suffix string
imagePath string
i int
bufferIdx int
return void
        private void SaveCubemapImage(uint[] cameraPixels, string filenameBase, string suffix, string imagePath, int i, int bufferIdx)
        {
            Bitmap bitmap = new Bitmap(cameraWidth, cameraHeight, PixelFormat.Format32bppArgb);
            var bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            byte[] pixelValues = new byte[Math.Abs(bmpData.Stride) * bitmap.Height];
            int stride = bmpData.Stride;
            int height = bmpData.Height;
            int inputIdx = bufferIdx * cameraWidth * cameraHeight;
            for (int y = 0; y < cameraHeight; y++)
            {
                int outputIdx = stride * (height - 1 - y);
                for (int x = 0; x < cameraWidth; x++)
                {
                    uint c = cameraPixels[inputIdx];
                    pixelValues[outputIdx + 0] = (byte)(c & 0xFF);
                    pixelValues[outputIdx + 1] = (byte)((c >> 8) & 0xFF);
                    pixelValues[outputIdx + 2] = (byte)(c >> 16);
                    pixelValues[outputIdx + 3] = 255;
                    outputIdx += 4;
                    inputIdx++;
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, ptr, pixelValues.Length);
            bitmap.UnlockBits(bmpData);

            string cameraId;
            if (captureStereoscopic)
            {
                cameraId = i.ToString();
                Log("Saving lightfield camera image number " + cameraId);
            }
            else
            {
                cameraId = ((CubemapFace)i).ToString();
                Log("Saving cubemap image " + cameraId);
            }
            string cubeFilepath = imagePath + "/" + filenameBase + "_" + cameraId + suffix;
            // TODO: Use better image processing library to get decent JPEG quality out.
            bitmap.Save(cubeFilepath, FormatToDrawingFormat(imageFormat));
            bitmap.Dispose();
        }