public async Task <MediaComposition> TimeBasedReconstruction(Stream aedatFile, CameraParameters cam, EventColor onColor, EventColor offColor, int frameTime, int maxFrames, float playback_frametime)
        {
            byte[]           aedatBytes  = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))];  // Read 0.5 GB at a time
            MediaComposition composition = new MediaComposition();
            int    lastTime = -999999;
            int    timeStamp;
            int    frameCount  = 0;
            Stream pixelStream = InitBitMap(cam);

            byte[] currentFrame = new byte[pixelStream.Length];

            int bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);

            while (bytesRead != 0 && frameCount < maxFrames)
            {
                // Read through AEDAT file
                for (int i = 0, length = bytesRead; i < length; i += AedatUtilities.dataEntrySize)                    // iterate through file, 8 bytes at a time.
                {
                    AEDATEvent currentEvent = new AEDATEvent(aedatBytes, i, cam);

                    AedatUtilities.SetPixel(ref currentFrame, currentEvent.x, currentEvent.y, (currentEvent.onOff ? onColor.Color : offColor.Color), cam.cameraX);
                    timeStamp = currentEvent.time;

                    if (lastTime == -999999)
                    {
                        lastTime = timeStamp;
                    }
                    else
                    {
                        if (lastTime + frameTime <= timeStamp)                         // Collected events within specified timeframe, add frame to video
                        {
                            WriteableBitmap b = new WriteableBitmap(cam.cameraX, cam.cameraY);
                            using (Stream stream = b.PixelBuffer.AsStream())
                            {
                                await stream.WriteAsync(currentFrame, 0, currentFrame.Length);
                            }

                            SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Ignore);
                            CanvasBitmap   bitmap2      = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), outputBitmap);

                            // Set playback framerate
                            MediaClip mediaClip = MediaClip.CreateFromSurface(bitmap2, TimeSpan.FromSeconds(playback_frametime));

                            composition.Clips.Add(mediaClip);
                            frameCount++;

                            // Stop adding frames to video if max frames has been reached
                            if (frameCount >= maxFrames)
                            {
                                break;
                            }
                            currentFrame = new byte[pixelStream.Length];
                            lastTime     = timeStamp;
                        }
                    }
                }
                bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);
            }
            return(composition);
        }
        public async Task EventBasedReconstruction(byte[] aedatFile, CameraParameters cam, int eventsPerFrame, int maxFrames, StorageFolder folder, string fileName)
        {
            int frameCount       = 0;
            int eventCount       = 0;
            int endOfHeaderIndex = AedatUtilities.GetEndOfHeaderIndex(ref aedatFile);               // find end of aedat header

            string fileConent = "";

            // Read through AEDAT file
            for (int i = endOfHeaderIndex, length = aedatFile.Length; i < length; i += AedatUtilities.dataEntrySize)                // iterate through file, 8 bytes at a time.
            {
                AEDATEvent currentEvent = new AEDATEvent(aedatFile, i, cam);

                fileConent += $"{currentEvent.onOff},{currentEvent.x},{currentEvent.y},{currentEvent.time}\n";
                eventCount++;
                if (eventCount >= eventsPerFrame)                 // Collected events within specified timeframe, add frame to video
                {
                    eventCount = 0;
                    StorageFile file = await folder.CreateFileAsync(fileName + frameCount + ".csv", CreationCollisionOption.GenerateUniqueName);

                    await FileIO.WriteTextAsync(file, "On/Off,X,Y,Timestamp\n" + fileConent);

                    fileConent = "";
                    frameCount++;
                    // Stop adding frames to video if max frames has been reached
                    if (frameCount >= maxFrames)
                    {
                        return;
                    }
                }
            }
        }
        public async Task TimeBasedReconstruction(byte[] aedatFile, CameraParameters cam, int frameTime, int maxFrames, StorageFolder folder, string fileName)
        {
            int lastTime = -999999;
            int timeStamp;
            int frameCount = 0;

            int endOfHeaderIndex = AedatUtilities.GetEndOfHeaderIndex(ref aedatFile);               // find end of aedat header

            string fileConent = "";

            // Read through AEDAT file
            for (int i = endOfHeaderIndex, length = aedatFile.Length; i < length; i += AedatUtilities.dataEntrySize)                // iterate through file, 8 bytes at a time.
            {
                AEDATEvent currentEvent = new AEDATEvent(aedatFile, i, cam);

                fileConent += currentEvent.onOff + "," + currentEvent.x + "," + currentEvent.y + "," + currentEvent.time + "\n";
                timeStamp   = currentEvent.time;

                if (lastTime == -999999)
                {
                    lastTime = timeStamp;
                }
                else
                {
                    if (lastTime + frameTime <= timeStamp)                     // Collected events within specified timeframe, add frame to video
                    {
                        try
                        {
                            StorageFile file = await folder.CreateFileAsync(fileName + frameCount + ".csv", CreationCollisionOption.GenerateUniqueName);

                            await FileIO.WriteTextAsync(file, "On/Off,X,Y,Timestamp\n" + fileConent);
                        } catch { }

                        fileConent = "";
                        frameCount++;
                        // Stop adding frames to video if max frames has been reached
                        if (frameCount >= maxFrames)
                        {
                            break;
                        }
                        lastTime = timeStamp;
                    }
                }
            }
        }
        public async Task EventBasedReconstruction(Stream aedatFile, CameraParameters cam, int eventsPerFrame, int maxFrames, StorageFolder folder, string fileName)
        {
            byte[] bytes    = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))];          // Read 0.5 GB at a time
            int    lastTime = -999999;
            int    timeStamp;
            int    frameCount      = 0;
            int    writeBufferSize = 50000;                     // Maximum number of characters to collect before writing to disk

            // Create CSV file
            StorageFile file = await folder.CreateFileAsync(fileName + ".csv", CreationCollisionOption.GenerateUniqueName);

            await FileIO.WriteTextAsync(file, "On Count,Off Count, Duration\n");

            string fileConent = "";
            int    onCount    = 0;
            int    offCount   = 0;

            int bytesRead = aedatFile.Read(bytes, 0, bytes.Length);

            // Read through AEDAT file
            while (bytesRead != 0 && frameCount < maxFrames)
            {
                for (int i = 0, length = bytesRead; i < length; i += AedatUtilities.dataEntrySize)                    // iterate through file, 8 bytes at a time.
                {
                    AEDATEvent currentEvent = new AEDATEvent(bytes, i, cam);

                    _         = currentEvent.onOff ? onCount++ : offCount++;
                    timeStamp = currentEvent.time;

                    if (lastTime == -999999)
                    {
                        lastTime = timeStamp;
                    }
                    else
                    {
                        if (onCount + offCount >= eventsPerFrame)                       // Collected enough events, add frame to video
                        {
                            try
                            {
                                fileConent += onCount + "," + offCount + "," + (timeStamp - lastTime) + "\n";

                                // Write to file if buffer size is reached
                                if (fileConent.Length > writeBufferSize)
                                {
                                    await FileIO.AppendTextAsync(file, fileConent);

                                    fileConent = "";
                                }
                            }
                            catch { }

                            onCount  = 0;
                            offCount = 0;

                            frameCount++;
                            // Stop adding frames to video if max frames has been reached
                            if (frameCount >= maxFrames)
                            {
                                break;
                            }
                            lastTime = timeStamp;
                        }
                    }
                }
                bytesRead = aedatFile.Read(bytes, 0, bytes.Length);
            }
            // Append any remaining data
            await FileIO.AppendTextAsync(file, fileConent);
        }
Beispiel #5
0
        public async Task EventBasedReconstruction(Stream aedatFile, CameraParameters cam, EventColor onColor, EventColor offColor, int eventsPerFrame, int maxFrames, StorageFolder folder, string fileName)
        {
            byte[] aedatBytes  = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))];            // Read 0.5 GB at a time
            int    frameCount  = 0;
            int    eventCount  = 0;
            Stream pixelStream = InitBitMap(cam);

            byte[] currentFrame = new byte[pixelStream.Length];

            int bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);

            while (bytesRead != 0 && frameCount < maxFrames)
            {
                // Read through AEDAT file
                for (int i = 0, length = bytesRead; i < length; i += AedatUtilities.dataEntrySize)                    // iterate through file, 8 bytes at a time.
                {
                    AEDATEvent currentEvent = new AEDATEvent(aedatBytes, i, cam);

                    AedatUtilities.SetPixel(ref currentFrame, currentEvent.x, currentEvent.y, (currentEvent.onOff ? onColor.Color : offColor.Color), cam.cameraX);

                    eventCount++;
                    if (eventCount >= eventsPerFrame)                     // Collected events within specified timeframe, add frame to video
                    {
                        eventCount = 0;
                        WriteableBitmap b = new WriteableBitmap(cam.cameraX, cam.cameraY);
                        using (Stream stream = b.PixelBuffer.AsStream())
                        {
                            await stream.WriteAsync(currentFrame, 0, currentFrame.Length);
                        }
                        var file = await folder.CreateFileAsync(fileName + frameCount + ".png");

                        using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                        {
                            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                            Stream pixelStream2 = b.PixelBuffer.AsStream();
                            byte[] pixels       = new byte[pixelStream2.Length];
                            await pixelStream2.ReadAsync(pixels, 0, pixels.Length);

                            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                                 (uint)b.PixelWidth,
                                                 (uint)b.PixelHeight,
                                                 96.0,
                                                 96.0,
                                                 pixels);
                            await encoder.FlushAsync();
                        }
                        frameCount++;
                        // Stop adding frames to video if max frames has been reached
                        if (frameCount >= maxFrames)
                        {
                            return;
                        }

                        currentFrame = new byte[pixelStream.Length];
                    }
                }
                bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);
            }
            return;
        }