コード例 #1
0
        private byte[] GetImageBin(CaptureData captureData, byte[] binData)
        {
            if (binData == null)
            {
                return(null);
            }
            switch (captureData.compress)
            {
#if UNITY_EDITOR
            case TextureCompress.None:
                return(ImageConversion.EncodeArrayToPNG(binData,
                                                        UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_SRGB,
                                                        (uint)captureData.width, (uint)captureData.height));

            case TextureCompress.RGB_565:
                return(ImageConversion.EncodeArrayToPNG(binData,
                                                        UnityEngine.Experimental.Rendering.GraphicsFormat.R5G6B5_UNormPack16,
                                                        (uint)captureData.width, (uint)captureData.height));
#endif
            case TextureCompress.PNG:
            case TextureCompress.JPG_BufferRGB565:
            case TextureCompress.JPG_BufferRGBA:
                return(binData);
            }
            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Encode the input data as per provided image format.
        /// </summary>
        /// <param name="data">An array of data to be encoded.</param>
        /// <param name="width">Image width.</param>
        /// <param name="height">Image height.</param>
        /// <param name="format">Graphics format used by the render texture.</param>
        /// <param name="imageFormat">Format for encoding the data.</param>
        /// <param name="additionalParam">Additional flags to be passed for the encoding.</param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        public static Array EncodeArray(Array data, int width, int height, GraphicsFormat format, ImageFormat imageFormat, int additionalParam = 0)
        {
            switch (imageFormat)
            {
            case ImageFormat.Raw:
                return(data);

#if UNITY_2019_3_OR_NEWER
            case ImageFormat.Png:
                return(ImageConversion.EncodeArrayToPNG(data, format, (uint)width, (uint)height, 0));

            case ImageFormat.Exr:
                return(ImageConversion.EncodeArrayToEXR(data, format, (uint)width, (uint)height, 0, /*EXRFlags*/ (Texture2D.EXRFlags)additionalParam));

            case ImageFormat.Tga:
                return(ImageConversion.EncodeArrayToTGA(data, format, (uint)width, (uint)height, 0));
#endif
            case ImageFormat.Jpg:
#if USIM_USE_BUILTIN_JPG_ENCODER
                return(ImageConversion.EncodeArrayToJPG(data, format, (uint)width, (uint)height, 0, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75));
#else
                return(JpegEncoder.Encode(ArrayUtilities.Cast <byte>(data), width, height, GraphicsUtilities.GetBlockSize(format), format, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75));
#endif
            default:
                throw new NotSupportedException("ImageFormat is not supported");
            }
        }
コード例 #3
0
    private byte[] encodeFrame(uint[] rawData)
    {
        var data = ImageConversion.EncodeArrayToPNG(rawData, GraphicsFormat.R8G8B8_UNorm, IMAGE_WIDTH, IMAGE_HEIGHT);

        if (data.Length > 65535)
        {
            throw new Exception("Max image size exceeded");
        }
        byte lowerByte  = (byte)(data.Length & 0xff);
        byte higherByte = (byte)((data.Length & 0xff00) >> 8);

        //Debug.Log("Length " + data.Length + " " + higherByte + " " + lowerByte);
        byte[] lengthAsBytes = new byte[] { higherByte, lowerByte };
        byte[] encodedBytes  = lengthAsBytes.Concat(data).ToArray();
        return(encodedBytes);
    }
コード例 #4
0
        void CaptureRgbData(Camera cam)
        {
            Profiler.BeginSample("CaptureDataFromLastFrame");
            if (!captureRgbImages)
            {
                return;
            }

            var captureFilename = Path.Combine(Manager.Instance.GetDirectoryFor(RgbDirectory), $"{s_RgbFilePrefix}{Time.frameCount}.png");
            var dxRootPath      = Path.Combine(RgbDirectory, $"{s_RgbFilePrefix}{Time.frameCount}.png");

            SensorHandle.ReportCapture(dxRootPath, SensorSpatialData.FromGameObjects(m_EgoMarker == null ? null : m_EgoMarker.gameObject, gameObject), m_PersistentSensorData.Select(kvp => (kvp.Key, kvp.Value)).ToArray());

            Func <AsyncRequest <CaptureCamera.CaptureState>, AsyncRequest.Result> colorFunctor;
            var width  = cam.pixelWidth;
            var height = cam.pixelHeight;
            var flipY  = ShouldFlipY(cam);

            colorFunctor = r =>
            {
                using (s_WriteFrame.Auto())
                {
                    var dataColorBuffer = (byte[])r.data.colorBuffer;
                    if (flipY)
                    {
                        FlipImageY(dataColorBuffer, height);
                    }

                    byte[] encodedData;
                    using (s_EncodeAndSave.Auto())
                    {
                        encodedData = ImageConversion.EncodeArrayToPNG(dataColorBuffer, GraphicsFormat.R8G8B8A8_UNorm, (uint)width, (uint)height);
                    }

                    return(!FileProducer.Write(captureFilename, encodedData) ? AsyncRequest.Result.Error : AsyncRequest.Result.Completed);
                }
            };

            CaptureCamera.Capture(cam, colorFunctor);

            Profiler.EndSample();
        }
コード例 #5
0
        // execute data
        private void ExecuteBinData(CaptureData captureData, byte[] binData)
        {
            this.InitDirectory();

            string file = GetFilePath(captureData);

            byte[] pngBin = null;
#if UNITY_EDITOR
            pngBin = ImageConversion.EncodeArrayToPNG(binData,
                                                      UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_SRGB,
                                                      (uint)captureData.width, (uint)captureData.height);

            // debug!
//            File.WriteAllBytes(file.Replace("png", "bin"), binData);
#endif
            if (pngBin != null)
            {
                File.WriteAllBytes(file, pngBin);
            }
        }
コード例 #6
0
        void OnSemanticSegmentationImageRead(int frameCount, NativeArray <Color32> data)
        {
            if (!m_AsyncAnnotations.TryGetValue(frameCount, out var annotation))
            {
                return;
            }

            var datasetRelativePath = $"{k_SemanticSegmentationDirectory}/{k_SegmentationFilePrefix}{frameCount}.png";
            var localPath           = $"{Manager.Instance.GetDirectoryFor(k_SemanticSegmentationDirectory)}/{k_SegmentationFilePrefix}{frameCount}.png";

            annotation.ReportFile(datasetRelativePath);

            var asyncRequest = Manager.Instance.CreateRequest <AsyncRequest <AsyncSemanticSegmentationWrite> >();

            imageReadback?.Invoke(new ImageReadbackEventArgs
            {
                data          = data,
                frameCount    = frameCount,
                sourceTexture = targetTexture
            });
            asyncRequest.data = new AsyncSemanticSegmentationWrite
            {
                data   = new NativeArray <Color32>(data, Allocator.TempJob),
                width  = targetTexture.width,
                height = targetTexture.height,
                path   = localPath
            };
            asyncRequest.Enqueue((r) =>
            {
                Profiler.BeginSample("Encode");
                var pngBytes = ImageConversion.EncodeArrayToPNG(r.data.data.ToArray(), GraphicsFormat.R8G8B8A8_UNorm, (uint)r.data.width, (uint)r.data.height);
                Profiler.EndSample();
                Profiler.BeginSample("WritePng");
                File.WriteAllBytes(r.data.path, pngBytes);
                Manager.Instance.ConsumerFileProduced(r.data.path);
                Profiler.EndSample();
                r.data.data.Dispose();
                return(AsyncRequest.Result.Completed);
            });
            asyncRequest.Execute();
        }
コード例 #7
0
    async UniTask ShootAsync()
    {
        var rt = mycamera.targetTexture;



        var req = await AsyncGPUReadback.Request(rt, 0);

        Debug.Log("GPU Readback done?:" + req.done);

        var rawByteArray   = req.GetData <byte>().ToArray();
        var graphicsFormat = rt.graphicsFormat;
        var width          = (uint)rt.width;
        var height         = (uint)rt.height;

        Debug.Log("BytesSize:" + rawByteArray.Length);


        var imageBytes = ImageConversion.EncodeArrayToPNG(rawByteArray, graphicsFormat, width, height);


        File.WriteAllBytes("my_screenshot.png", imageBytes); // test
    }
コード例 #8
0
        // Build voxel object
        public override float Build(Storage voxels, Bounds bounds, Informer informer, object parameter)
        {
            processor.superSamplingCount = superSamplingCount;
            processor.powerOfTwo         = powerOfTwo;
            processor.expandEdges        = expandEdges;
            processor.backgroundColor    = backgroundColor;
            processor.hdr = fileFormat == FileFormat.EXR;

            // Execute real build-up method
            float progress = processor.Build(voxels, bounds);

            // Check if processing has been finished
            if (progress >= 1)
            {
                // Store file, if it is specified
                if (fileStoring && filePath != null && filePath.Length > 0 && Texture != null)
                {
                    try
                    {
                        // Build target path
                        switch (fileFormat)
                        {
#if UNITY_2020_2_OR_NEWER
                        case FileFormat.JPG:
                        case FileFormat.PNG:
                        case FileFormat.TGA:
                        case FileFormat.EXR:

                            var source = processor.Texture;
                            if (source)
                            {
                                var sourceData = source.GetPixelData <byte>(0);
                                if (sourceData != null)
                                {
                                    var length = source.width * source.height * source.depth;
                                    //var textureWidth = Mathf.CeilToInt(Mathf.Sqrt(length));

                                    var textureWidth = (int)Math.Pow(2, Math.Ceiling(Math.Log(Mathf.Sqrt(length)) / Math.Log(2)));

                                    var columnsCount = Mathf.CeilToInt((float)textureWidth / source.width);
                                    var rowsCount    = Mathf.CeilToInt((float)source.depth / columnsCount);

                                    textureWidth = source.width * columnsCount;
                                    var textureHeight = source.height * rowsCount;

                                    var pixelSize = processor.hdr ? 8 : 4;
                                    var lineSize  = source.width * pixelSize;

                                    var buffer     = new byte[lineSize];
                                    var targetData = new byte[textureWidth * textureHeight * pixelSize];
                                    if (targetData != null)
                                    {
                                        var sourceOffset = 0;

                                        // Process depth slices
                                        for (int slice = 0; slice < source.depth; ++slice)
                                        {
                                            // Calculate column and row of the target sub image
                                            var column = slice % columnsCount;
                                            var row    = rowsCount - slice / columnsCount - 1;

                                            // Process lines of the current slice
                                            for (int line = 0; line < source.height; ++line)
                                            {
                                                // Get data for line into the cache
                                                sourceData.GetSubArray(sourceOffset, lineSize).CopyTo(buffer);

                                                // Copy it to the target buffer
                                                Array.Copy(buffer, 0, targetData, (column + (line + row * source.height) * columnsCount) * lineSize, lineSize);

                                                sourceOffset += lineSize;
                                            }
                                        }

                                        byte[] data;
                                        switch (fileFormat)
                                        {
                                        case FileFormat.JPG:
                                            // Convert image data to JPEG data
                                            data = ImageConversion.EncodeArrayToJPG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, 100);
                                            break;

                                        case FileFormat.PNG:
                                            // Convert image data to PNG data
                                            data = ImageConversion.EncodeArrayToPNG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight);
                                            break;

                                        case FileFormat.TGA:
                                            // Convert image data to TGA data
                                            data = ImageConversion.EncodeArrayToTGA(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight);
                                            break;

                                        case FileFormat.EXR:
                                            // Convert image data to EXR data
                                            data = ImageConversion.EncodeArrayToEXR(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, UnityEngine.Texture2D.EXRFlags.CompressZIP);
                                            break;

                                        default:
                                            data = null;
                                            break;
                                        }

                                        if (data != null)
                                        {
                                            // Make sure the target folders exist
                                            var path = System.IO.Path.Combine(Application.dataPath, filePath);
                                            if (Helper.CreateDirectory(path, true))
                                            {
#if UNITY_EDITOR
                                                // Remove existing asset file
                                                if (System.IO.File.Exists(path))
                                                {
                                                    System.IO.File.Delete(path);
                                                }

                                                // Update database
                                                UnityEditor.AssetDatabase.Refresh();
#endif

                                                // Save data to file
                                                System.IO.File.WriteAllBytes(path, data);

#if UNITY_EDITOR
                                                // Store columns and rows for the asset to be applied later
                                                voxelTexturePaths.Add(System.IO.Path.GetFullPath(path), new Vector2Int(columnsCount, rowsCount));

                                                // Update database to enable changing import settings
                                                UnityEditor.AssetDatabase.Refresh();
#endif
                                            }
                                        }
                                    }
                                }
                            }

                            break;
#endif

                        default:
                            // Save texture as asset file
                            Helper.StoreAsset(processor.Texture, filePath, null);
                            break;
                        }
                    }
                    catch (System.Exception exception)
                    {
                        Debug.Log(exception.Message);
                    }
                }

#if UNITY_EDITOR
                // Add object creation undo operation
                if (!Application.isPlaying)
                {
                    UnityEditor.Undo.RegisterCreatedObjectUndo(processor.Texture, "\"VoxelTexture3D\" Creation");
                }
#endif

                // Execute informer callback
                informer?.Invoke(new UnityEngine.Object[] { processor.Texture }, parameter);
            }

            return(progress);
        }