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);
        }
Пример #2
0
        private async void ReadToBytesTime_Tapped(object sender, TappedRoutedEventArgs e)
        {
            long readToBytes_Time;
            var  picker = new Windows.Storage.Pickers.FileOpenPicker
            {
                ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
                SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
            };

            picker.FileTypeFilter.Add(".AEDAT");


            var file = await picker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            byte[] result = await AedatUtilities.ReadToBytes(file);                        // All of the bytes in the AEDAT file loaded into an array

            sw.Stop();
            readToBytes_Time = sw.ElapsedMilliseconds;

            ContentDialog readTimeDialog = new ContentDialog()
            {
                Title           = "Testing",
                Content         = "Read to bytes time: " + readToBytes_Time + " ms",
                CloseButtonText = "Close"
            };
            await readTimeDialog.ShowAsync();
        }
        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;
                    }
                }
            }
        }
Пример #4
0
        private async Task <List <AEDATEvent> > UpdateDataGrid(StorageFile file)
        {
            List <AEDATEvent> temp = await AedatUtilities.GetEvents(file);

            // Get camera type
            byte[] headerBytes = await AedatUtilities.ReadHeaderToBytes(file);

            string cameraTypeSearch = AedatUtilities.FindLineInHeader(AedatUtilities.hardwareInterfaceCheck, ref headerBytes);

            currentCamera = AedatUtilities.ParseCameraModel(cameraTypeSearch);
            return(temp);
        }
        public async void BulkExport(List <StorageFile> files, int maxFrames, int frameTime)
        {
            var picker2 = new FolderPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker2.FileTypeFilter.Add("*");
            // Select AEDAT file to be converted
            StorageFolder folder = await picker2.PickSingleFolderAsync();

            if (folder == null)
            {
                showLoading.IsActive      = false;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return;
            }

            foreach (StorageFile file in files)
            {
                var headerData = await AedatUtilities.GetHeaderData(file);

                var aedatFile = (await file.OpenReadAsync()).AsStreamForRead();
                aedatFile.Seek(headerData.Item1, SeekOrigin.Begin);//skip over header.

                // Determine camera type from AEDAT header
                CameraParameters cam = headerData.Item2;
                if (cam == null)
                {
                    await invalidCameraDataDialog.ShowAsync();

                    return;
                }
                showLoading.IsActive      = true;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Visible;

                StorageFolder folder2 = await folder.CreateFolderAsync(file.Name.Replace(".aedat", "") + " EventChunks");

                if (playbackType.IsOn)
                {
                    await TimeBasedReconstruction(aedatFile, cam, frameTime, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
                else
                {
                    int numOfEvents = Int32.Parse(numOfEventInput.Text);
                    await EventBasedReconstruction(aedatFile, cam, numOfEvents, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
            }
            showLoading.IsActive      = false;
            backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
        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;
                    }
                }
            }
        }
Пример #7
0
        private async void SearchTest_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker.FileTypeFilter.Add(".AEDAT");

            var file = await picker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }
            byte[] aedatFile = await AedatUtilities.ReadToBytes(file);

            string result = AedatUtilities.FindLineInHeader(AedatUtilities.hardwareInterfaceCheck, ref aedatFile);

            CameraParameters cameraParam = AedatUtilities.ParseCameraModel(result);

            if (cameraParam == null)
            {
                ContentDialog AEEE = new ContentDialog()
                {
                    Title           = "EEE",
                    Content         = "AEEEE",
                    CloseButtonText = "Close"
                };
                await AEEE.ShowAsync();
            }

            ContentDialog invaldInputDialogue = new ContentDialog()
            {
                Title           = "Testing...",
                Content         = cameraParam.cameraName,
                CloseButtonText = "Close"
            };

            await invaldInputDialogue.ShowAsync();
        }
        public static async Task <List <AEDATEvent> > GetEvents(StorageFile file)
        {
            var headerData = await AedatUtilities.GetHeaderData(file);

            int endOfHeaderIndex = headerData.Item1;
            CameraParameters cam = headerData.Item2;

            byte[] aedatBytes = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))];             // Read 0.5 GB at a time

            var aedatFile = (await file.OpenReadAsync()).AsStreamForRead();

            aedatFile.Seek(endOfHeaderIndex, SeekOrigin.Begin);                 // Skip over header.

            List <AEDATEvent> tableData = new List <AEDATEvent>();

            if (cam == null)
            {
                ContentDialog invalidData = new ContentDialog()
                {
                    Title           = "Error",
                    Content         = "Could not parse camera parameters.",
                    CloseButtonText = "Close"
                };
                await invalidData.ShowAsync();

                return(null);
            }

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

            while (bytesRead != 0)
            {
                for (int i = 0, length = bytesRead; i < length; i += dataEntrySize)
                {
                    tableData.Add(new AEDATEvent(aedatBytes, i, cam));
                }
                bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length);
            }
            return(tableData);
        }
Пример #9
0
        /// <summary>
        /// Gets the first timestamp and the last timestamp
        /// </summary>
        /// <param name="file"></param>
        private async void getData(StorageFile file)
        {
            byte[] currentDataEntry = new byte[AedatUtilities.dataEntrySize];
            int    timeStamp        = 0;

            Queue <byte> dataEntryQ = new Queue <byte>();

            byte[] result;                  // All of the bytes in the AEDAT file loaded into an array
            using (Stream stream = await file.OpenStreamForReadAsync())
            {
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    result = memoryStream.ToArray();
                }
            }

            int endOfHeaderIndex = AedatUtilities.GetEndOfHeaderIndex(ref result);

            foreach (byte byteIn in result.Skip(endOfHeaderIndex))            //get the first timestamp;
            {
                if (dataEntryQ.Count < AedatUtilities.dataEntrySize)
                {
                    dataEntryQ.Enqueue(byteIn);
                }
                else
                {
                    dataEntryQ.CopyTo(currentDataEntry, 0);
                    Array.Reverse(currentDataEntry);
                    timeStamp = BitConverter.ToInt32(currentDataEntry, 0);                          // Timestamp is found in the first four bytes
                    break;
                }
            }

            // Get final data entry
            int endingTime = 0;

            byte[] finalDataEntry = new byte[AedatUtilities.dataEntrySize];
            int    i = 0;

            for (int j = result.Count() - 1; j > result.Count() - 9; j--)
            {
                finalDataEntry[i] = result[j];
                i++;
            }
            endingTime = BitConverter.ToInt32(finalDataEntry, 0);               // Timestamp is found in the first four bytes

            // Convert to seconds
            double startingTime = (double)timeStamp / 1000000.000f;
            double endingTime2  = (double)endingTime / 1000000.000f;

            // Total number of events in the file
            double eventCount = (result.Count() - endOfHeaderIndex) / 8;

            // Add data to GUI
            tableData.Add(new AEDATData
            {
                name               = file.Name.Replace(".aedat", "").Replace(".AEDAT", ""),
                startingTime       = startingTime,
                eventCount         = eventCount,
                endingTime         = endingTime2,
                avgEventsPerSecond = eventCount / Math.Abs(endingTime2 - startingTime),
                duration           = endingTime2 - startingTime
            });
        }
        private async void SelectFile_Tapped(object sender, TappedRoutedEventArgs e)
        {
            EventColor onColor;
            EventColor offColor;
            int        frameTime;
            int        maxFrames;
            float      fps;

            try
            {
                // Grab video reconstruction settings from GUI
                // Will throw a FormatException if input is invalid (negative numbers or input has letters)
                (frameTime, maxFrames, onColor, offColor, fps) = ParseVideoSettings();
            }
            catch (FormatException)
            {
                await invaldVideoSettingsDialog.ShowAsync();

                return;
            }

            // Select AEDAT file to be converted to video
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker.FileTypeFilter.Add(".AEDAT");

            // Select AEDAT file to be converted
            StorageFile file = await picker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            var headerData = await AedatUtilities.GetHeaderData(file);

            var aedatFile = (await file.OpenReadAsync()).AsStreamForRead();

            aedatFile.Seek(headerData.Item1, SeekOrigin.Begin);                 // Skip over header.

            CameraParameters cam = headerData.Item2;

            if (cam == null)
            {
                await invalidCameraDataDialog.ShowAsync();

                return;
            }
            showLoading.IsActive      = true;
            backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Visible;
            float            playback_frametime = 1.0f / fps;
            MediaComposition composition;

            if (playbackType.IsOn)
            {
                composition = await TimeBasedReconstruction(aedatFile, cam, onColor, offColor, frameTime, maxFrames, playback_frametime);
            }
            else
            {
                int numOfEvents = Int32.Parse(numOfEventInput.Text);
                composition = await EventBasedReconstruction(aedatFile, cam, onColor, offColor, numOfEvents, maxFrames, playback_frametime);
            }
            SaveCompositionToFile(composition, file.DisplayName, cam.cameraX, cam.cameraY);
        }
        private async void SelectFile_Tapped(object sender, TappedRoutedEventArgs e)
        {
            int frameTime;
            int maxFrames;

            try
            {
                // Grab video reconstruction settings from GUI
                // Will throw a FormatException if input is invalid (negative numbers or input has letters)
                (frameTime, maxFrames) = ParseVideoSettings();
            }
            catch (FormatException)
            {
                await invaldVideoSettingsDialog.ShowAsync();

                return;
            }

            // Select AEDAT file to be converted to video
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker.FileTypeFilter.Add(".AEDAT");


            // Select AEDAT file to be converted
            IReadOnlyList <StorageFile> files = await picker.PickMultipleFilesAsync();

            if (files == null)
            {
                showLoading.IsActive      = false;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return;
            }


            var picker2 = new FolderPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker2.FileTypeFilter.Add("*");
            // Select AEDAT file to be converted
            StorageFolder folder = await picker2.PickSingleFolderAsync();

            if (folder == null)
            {
                showLoading.IsActive      = false;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return;
            }


            foreach (StorageFile file in files)
            {
                byte[] aedatFile = await AedatUtilities.ReadToBytes(file);

                // Determine camera type from AEDAT header
                string           cameraTypeSearch = AedatUtilities.FindLineInHeader(AedatUtilities.hardwareInterfaceCheck, ref aedatFile);
                CameraParameters cam = AedatUtilities.ParseCameraModel(cameraTypeSearch);
                if (cam == null)
                {
                    await invalidCameraDataDialog.ShowAsync();

                    return;
                }
                showLoading.IsActive      = true;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Visible;

                StorageFolder folder2 = await folder.CreateFolderAsync(file.Name.Replace(".aedat", "") + " Event Chunks");

                if (playbackType.IsOn)
                {
                    await TimeBasedReconstruction(aedatFile, cam, frameTime, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
                else
                {
                    int numOfEvents = Int32.Parse(numOfEventInput.Text);
                    await EventBasedReconstruction(aedatFile, cam, numOfEvents, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
            }
            showLoading.IsActive      = false;
            backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
Пример #12
0
        private async void SelectFile_Tapped(object sender, TappedRoutedEventArgs e)
        {
            EventColor onColor;
            EventColor offColor;
            int        frameTime;
            int        maxFrames;

            try
            {
                // Grab video reconstruction settings from GUI
                // Will throw a FormatException if input is invalid (negative numbers or input has letters)
                (frameTime, maxFrames, onColor, offColor) = ParseFrameSettings();
            }
            catch (FormatException)
            {
                await invaldVideoSettingsDialog.ShowAsync();

                return;
            }

            // Select AEDAT file to be converted to video
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker.FileTypeFilter.Add(".AEDAT");


            // Select AEDAT file to be converted
            IReadOnlyList <StorageFile> files = await picker.PickMultipleFilesAsync();

            if (files == null)
            {
                showLoading.IsActive      = false;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return;
            }

            var picker2 = new FolderPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };

            picker2.FileTypeFilter.Add("*");

            // Select AEDAT file to be converted
            StorageFolder folder = await picker2.PickSingleFolderAsync();

            if (folder == null)
            {
                showLoading.IsActive      = false;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return;
            }

            foreach (var file in files)
            {
                var headerData = await AedatUtilities.GetHeaderData(file);

                var aedatFile = (await file.OpenReadAsync()).AsStreamForRead();
                aedatFile.Seek(headerData.Item1, SeekOrigin.Begin);                 //Skip over header.

                CameraParameters cam = headerData.Item2;
                if (cam == null)
                {
                    await invalidCameraDataDialog.ShowAsync();

                    return;
                }
                showLoading.IsActive      = true;
                backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Visible;


                StorageFolder folder2 = await folder.CreateFolderAsync(file.Name.Replace(".aedat", "") + (playbackType.IsOn ? " time based" : " event based") + " Frames");

                if (playbackType.IsOn)
                {
                    await TimeBasedReconstruction(aedatFile, cam, onColor, offColor, frameTime, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
                else
                {
                    int numOfEvents = Int32.Parse(numOfEventInput.Text);
                    await EventBasedReconstruction(aedatFile, cam, onColor, offColor, numOfEvents, maxFrames, folder2, file.Name.Replace(".aedat", ""));
                }
            }
            showLoading.IsActive      = false;
            backgroundTint.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
Пример #13
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;
        }