示例#1
0
        public static byte[] GetDataStream(string mlvFileName, string content, int prefetchCount)
        {
            MLVCachedReader cache = GetReader(mlvFileName);
            eFileType       type  = GetFileType(content);

            lock (cache.cachedFiles)
            {
                if (cache.cachedFiles.ContainsKey(content))
                {
                    PrefetchNext(mlvFileName, content, prefetchCount - 1);

                    cache.cachedFiles[content].lastUseTime = DateTime.Now;

                    return(cache.cachedFiles[content].bufferedData);
                }
            }

            switch (type)
            {
            case eFileType.Txt:
                string info = GetInfoFields(mlvFileName).Aggregate <string>(addString);
                return(ASCIIEncoding.ASCII.GetBytes(info));

            case eFileType.Wav:
                return(GetWaveDataStream(mlvFileName));

            case eFileType.MJpeg:
                return(GetMJpegDataStream(mlvFileName));
            }

            int frame = GetFrameNumber(mlvFileName, content);

            /* read video frame with/out debayering dependig on filetype */
            switch (type)
            {
            case eFileType.Dng:
            case eFileType.Fits:
                cache.handler.DebayeringEnabled = false;
                break;

            case eFileType.Jpg:
                cache.handler.DebayeringEnabled = true;
                break;
            }

            byte[] data         = null;
            Bitmap currentFrame = null;

            MLVTypes.mlv_vidf_hdr_t vidfHeader = new MLVTypes.mlv_vidf_hdr_t();

            /* seek to the correct block */
            int block = cache.reader.GetVideoFrameBlockNumber((uint)frame);

            if (block < 0)
            {
                throw new FileNotFoundException("Requested video frame " + frame + " but thats not in the file index");
            }

            /* ensure that multiple threads dont conflict */
            lock (cache)
            {
                /* read it */
                cache.reader.CurrentBlockNumber    = block;
                cache.handler.VidfHeader.blockSize = 0;
                cache.reader.ReadBlock();

                /* now the VIDF should be read correctly */
                if (cache.handler.VidfHeader.blockSize == 0)
                {
                    throw new InvalidDataException("Requested video frame " + frame + " but the index points us wrong");
                }

                /* get all data we need */
                switch (type)
                {
                case eFileType.Dng:
                case eFileType.Fits:
                    data       = cache.handler.RawPixelData;
                    vidfHeader = cache.handler.VidfHeader;
                    break;

                case eFileType.Jpg:
                    if (cache.handler.CurrentFrame != null)
                    {
                        currentFrame = new Bitmap(cache.handler.CurrentFrame);
                    }
                    break;
                }
            }

            /* process it */
            switch (type)
            {
            case eFileType.Fits:
            {
                object[] metadata = cache.reader.GetVideoFrameMetadata((uint)frame);
                byte[]   stream   = FITSCreator.Create(mlvFileName, vidfHeader, data, metadata);

                PrefetchSave(mlvFileName, content, eFileType.Dng, frame, stream);
                PrefetchNext(mlvFileName, content, prefetchCount);

                return(stream);
            }

            case eFileType.Dng:
            {
                object[] metadata = cache.reader.GetVideoFrameMetadata((uint)frame);
                byte[]   stream   = DNGCreator.Create(mlvFileName, vidfHeader, data, metadata);

                PrefetchSave(mlvFileName, content, eFileType.Dng, frame, stream);
                PrefetchNext(mlvFileName, content, prefetchCount);

                return(stream);
            }

            case eFileType.Jpg:
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                Stream            stream  = new MemoryStream();

                encoder.QualityLevel = 90;
                if (currentFrame != null)
                {
                    encoder.Frames.Add(BitmapFrame.Create(BitmapSourceFromBitmap(currentFrame)));
                    encoder.Save(stream);

                    stream.Seek(0, SeekOrigin.Begin);
                }
                stream.Seek(0, SeekOrigin.Begin);

                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                PrefetchSave(mlvFileName, content, eFileType.Jpg, frame, buffer);
                PrefetchNext(mlvFileName, content, prefetchCount);

                return(buffer);
            }
            }

            throw new FileNotFoundException("Requested frame " + frame + " of type " + type + " but we dont support that");
        }