コード例 #1
0
        void Update()
        {
            if (_Initialized)
            {
                Color32[] pixels = _WebCamTexture.GetPixels32();
                int       originalColorImageSize = 4 * pixels.Length;

                _ColorImageTexture.SetPixels32(pixels);
                _ColorImageTexture.Apply();

                byte[] colorFrame = _ColorImageTexture.GetRawTextureData();

                _Stopwatch.Reset();
                _Stopwatch.Start();

                // VP8 Compression
                bool keyFrame = false;
                // _Encoder.EncodeFromBgra(ref colorFrame, _Width, _Height, keyFrame, ref _VP8EncodedData);
                _Encoder.EncodeFromRgba(ref colorFrame, _Width, _Height, keyFrame, ref _VP8EncodedData);

                _Stopwatch.Stop();
                long vp8EncodingTimeMillseconds = _Stopwatch.ElapsedMilliseconds;

                _Stopwatch.Reset();
                _Stopwatch.Start();

                // VP8 Decode
                _Decoder.Decode(ref _VP8EncodedData, _VP8EncodedData.Length, ref _RGBColorData);

                _Stopwatch.Stop();
                long vp8DecodingTimeMillseconds = _Stopwatch.ElapsedMilliseconds;

                // Visualize decoded color image
                _DecodedColorImageTexture.LoadRawTextureData(_RGBColorData);
                _DecodedColorImageTexture.Apply();

                // Display info
                int   vp8CompressedDataSize = _VP8EncodedData.Length;
                float vp8CompressionRatio   = originalColorImageSize / vp8CompressedDataSize;

                _ColorImageSize.text = string.Format("Size: {2:#,0} [bytes]  Resolution: {0}x{1}",
                                                     _ColorImageTexture.width, _ColorImageTexture.height, originalColorImageSize);
                _CompressedColorImageSize.text = string.Format("Size: {0:#,0} [bytes]  Data compression ratio: {1:F1}",
                                                               vp8CompressedDataSize, vp8CompressionRatio);
                _ProcessingTime.text = string.Format("Processing time:\n Encode: {0} [ms]\n Decode: {1} [ms]",
                                                     vp8EncodingTimeMillseconds, vp8DecodingTimeMillseconds);

                if (_JpegCompressionTest)
                {
                    byte[] jpegEncodedData = ImageConversion.EncodeToJPG(_ColorImageTexture);

                    _DecodedColorImageTexture2.LoadImage(jpegEncodedData);
                    _DecodedColorImageTexture2.Apply();

                    int   jpegCompressedDataSize = jpegEncodedData.Length;
                    float jpegCompressionRatio   = originalColorImageSize / jpegCompressedDataSize;

                    _CompressedColorImageSize2.text = string.Format("Size: {0:#,0} [bytes]  Data compression ratio: {1:F1}",
                                                                    jpegCompressedDataSize, jpegCompressionRatio);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Compress / encode a multi layer map file to an image
        /// </summary>
        /// <param name="input">Multi layer map in format x,y,layer</param>
        /// <param name="imageName">Output image name - image image index and extension will be added</param>
        /// <param name="exportPNG">True if a png is wanted</param>
        /// <param name="exportJPG">True if a jpg is wanted</param>
        public static void CompressToMultiChannelFileImage(string imageName, HeightMap r, HeightMap g, HeightMap b, HeightMap a, TextureFormat imageStorageFormat, GaiaConstants.ImageFileType imageFileType)
        {
            int width  = 0;
            int height = 0;

            if (r != null)
            {
                width  = r.Width();
                height = r.Depth();
            }
            else if (g != null)
            {
                width  = g.Width();
                height = g.Depth();
            }
            else if (b != null)
            {
                width  = b.Width();
                height = b.Depth();
            }
            else if (a != null)
            {
                width  = a.Width();
                height = a.Depth();
            }

            if (string.IsNullOrEmpty(imageName))
            {
                Debug.LogError("Cannot write image - no name supplied!");
                return;
            }

            if (width == 0 || height == 0)
            {
                Debug.LogError("Cannot write image - invalid dimensions : " + width + ", " + height);
                return;
            }

            Texture2D exportTexture = new Texture2D(width, height, imageStorageFormat, true, false);
            Color     pixelColor    = new Color();

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    pixelColor.r = r != null ? r[x, y] : 0f;
                    pixelColor.g = g != null ? g[x, y] : 0f;
                    pixelColor.b = b != null ? b[x, y] : 0f;
                    pixelColor.a = a != null ? a[x, y] : 1f;
                    exportTexture.SetPixel(x, y, pixelColor);
                }
            }
            exportTexture.Apply();

            #if UNITY_2017_1_OR_NEWER
            switch (imageFileType)
            {
            case GaiaConstants.ImageFileType.Jpg:
                byte[] jpgBytes = ImageConversion.EncodeToJPG(exportTexture, 100);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes);
                break;

            case GaiaConstants.ImageFileType.Png:
                byte[] pngBytes = ImageConversion.EncodeToPNG(exportTexture);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes);
                break;

            case GaiaConstants.ImageFileType.Exr:
                byte[] exrBytes = ImageConversion.EncodeToEXR(exportTexture, Texture2D.EXRFlags.CompressZIP);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes);
                break;
            }
            #else
            switch (imageFileType)
            {
            case GaiaConstants.ImageFileType.Jpg:
                byte[] jpgBytes = exportTexture.EncodeToJPG();
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes);
                break;

            case GaiaConstants.ImageFileType.Png:
                byte[] pngBytes = exportTexture.EncodeToPNG();
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes);
                break;

            case GaiaConstants.ImageFileType.Exr:
                byte[] exrBytes = exportTexture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes);
                break;
            }
            #endif

            #if UNITY_EDITOR
            AssetDatabase.Refresh();
            #endif

            //Lose the texture
            DestroyImmediate(exportTexture);
        }
コード例 #3
0
    IEnumerator ProcessImage(XRCpuImage image, Vector3 viewportScaling)
    {
        // Create the async conversion request.

        XRCpuImage.ConversionParams conv_params = new XRCpuImage.ConversionParams
        {
            // Use the full image.
            inputRect = new RectInt(0, 0, image.width, image.height),

            // Downsample by 2.
            outputDimensions = new Vector2Int(image.width, image.height),

            // Color image format.
            outputFormat = TextureFormat.RGBA32,

            // Flip across the Y axis.
            transformation = XRCpuImage.Transformation.MirrorY
        };

        var request = image.ConvertAsync(conv_params);

        // Wait for the conversion to complete.
        while (!request.status.IsDone())
        {
            yield return(null);
        }

        // Check status to see if the conversion completed successfully.
        if (request.status != XRCpuImage.AsyncConversionStatus.Ready)
        {
            // Something went wrong.
            Debug.LogErrorFormat("Request failed with status {0}", request.status);

            // Dispose even if there is an error.
            request.Dispose();
            yield break;
        }

        // Image data is ready. Let's apply it to a Texture2D.
        var rawData = request.GetData <byte>();

        // Create a texture if necessary.
        if (m_Texture == null)
        {
            m_Texture = new Texture2D(
                request.conversionParams.outputDimensions.x,
                request.conversionParams.outputDimensions.y,
                request.conversionParams.outputFormat,
                false);
        }

        // Copy the image data into the texture.
        m_Texture.LoadRawTextureData(rawData);
        m_Texture.Apply();

        Debug.Log("TEX: " + m_Texture.height + "h " + m_Texture.width + "w");
        Debug.Log("Screen: " + m_Texture.height + "h " + m_Texture.width + "w");


        Mat inputMat  = new Mat(image.height, image.width, CvType.CV_8UC4);
        Mat outputMat = new Mat(1500, 1500, CvType.CV_8UC4);

        Utils.fastTexture2DToMat(m_Texture, inputMat);

        if (tex2d == null)
        {
            tex2d = new Texture2D(1500,
                                  1500, conv_params.outputFormat, false);
        }

        Debug.Log("positionAnchor");
        Debug.Log(positionAnchor);

        Debug.Log("anchorRef");
        Debug.Log(anchorRef);

        int counter = 0;

        Point[] srcPointsVec = new Point[4];
        foreach (var point in anchorRef.getWorldPoints())
        {
            Vector3 screenPoint = mainCam.WorldToScreenPoint(point);
            srcPointsVec[counter] = new Point(screenPoint.y * viewportScaling.y / 3,
                                              100 - screenPoint.x * viewportScaling.x / 3);
            counter += 1;
        }


        MatOfPoint2f srcPoints = new MatOfPoint2f(new[]
        {
            srcPointsVec[0],
            srcPointsVec[1],
            srcPointsVec[2],
            srcPointsVec[3]
        });


        MatOfPoint2f dstPoints = new MatOfPoint2f(new[]
        {
            new Point(195 * 1.25, 0),
            new Point(0, 0),
            new Point(0, 280 * 1.25),
            new Point(195 * 1.25, 280 * 1.25),
        });

        Mat H = Calib3d.findHomography(srcPoints, dstPoints);


        Imgproc.warpPerspective(inputMat, outputMat, H, new Size(1500, 1500));

        Utils.fastMatToTexture2D(outputMat, tex2d);


        if (websocket.State == WebSocketState.Open && canProcess)
        {
            websocket.Send(ImageConversion.EncodeToJPG(tex2d, 50));
            canProcess = false;
        }

        inputMat.Dispose();
        inputMat = null;
        outputMat.Dispose();
        outputMat = null;
        request.Dispose();
    }
コード例 #4
0
        public void uploadFile(string filePath, string fbDir, Action <string> cb)
        {
            Texture2D t = resize(filePath);

            StorageUtils.instance.uploadAutoHash(ImageConversion.EncodeToJPG(t), fbDir + "/{0}.jpg", cb);
        }
コード例 #5
0
    public static IEnumerator TakeScreenshot()
    {
        Player       plr     = OptimizationVariables.MainPlayer;
        SteamChannel channel = plr.channel;

        switch (MiscOptions.AntiSpyMethod)
        {
        case 0:
        {
            bool flag = Time.realtimeSinceStartup - PlayerCoroutines.LastSpy < 0.5f || PlayerCoroutines.IsSpying;
            if (flag)
            {
                yield break;
            }
            PlayerCoroutines.IsSpying = true;
            PlayerCoroutines.LastSpy  = Time.realtimeSinceStartup;
            bool flag2 = !MiscOptions.PanicMode;
            if (flag2)
            {
                PlayerCoroutines.DisableAllVisuals();
            }
            yield return(new WaitForFixedUpdate());

            yield return(new WaitForEndOfFrame());

            Texture2D screenshotRaw = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false)
            {
                name      = "Screenshot_Raw",
                hideFlags = HideFlags.HideAndDontSave
            };
            screenshotRaw.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0, false);
            Texture2D screenshotFinal = new Texture2D(640, 480, TextureFormat.RGB24, false)
            {
                name      = "Screenshot_Final",
                hideFlags = HideFlags.HideAndDontSave
            };
            Color[] oldColors   = screenshotRaw.GetPixels();
            Color[] newColors   = new Color[screenshotFinal.width * screenshotFinal.height];
            float   widthRatio  = screenshotRaw.width / (float)screenshotFinal.width;
            float   heightRatio = screenshotRaw.height / (float)screenshotFinal.height;
            int     num10;
            for (int i = 0; i < screenshotFinal.height; i = num10 + 1)
            {
                int num  = (int)(i * heightRatio) * screenshotRaw.width;
                int num2 = i * screenshotFinal.width;
                for (int j = 0; j < screenshotFinal.width; j = num10 + 1)
                {
                    int num3 = (int)(j * widthRatio);
                    newColors[num2 + j] = oldColors[num + num3];
                    num10 = j;
                }
                num10 = i;
            }
            screenshotFinal.SetPixels(newColors);
            byte[] data  = ImageConversion.EncodeToJPG(screenshotFinal, 33);
            bool   flag3 = data.Length < 30000;
            if (flag3)
            {
                channel.longBinaryData = true;
                channel.openWrite();
                channel.write(data);
                channel.closeWrite("tellScreenshotRelay", ESteamCall.SERVER, ESteamPacket.UPDATE_RELIABLE_CHUNK_BUFFER);
                channel.longBinaryData = false;
            }
            yield return(new WaitForFixedUpdate());

            yield return(new WaitForEndOfFrame());

            PlayerCoroutines.IsSpying = false;
            bool flag4 = !MiscOptions.PanicMode;
            if (flag4)
            {
                PlayerCoroutines.EnableAllVisuals();
            }
            break;
        }

        case 1:
        {
            System.Random r       = new System.Random();
            string[]      files   = Directory.GetFiles(MiscOptions.AntiSpyPath);
            byte[]        dataRaw = File.ReadAllBytes(files[r.Next(files.Length)]);
            Texture2D     texRaw  = new Texture2D(2, 2);
            ImageConversion.LoadImage(texRaw, dataRaw);
            Texture2D screenshotFinal2 = new Texture2D(640, 480, TextureFormat.RGB24, false)
            {
                name      = "Screenshot_Final",
                hideFlags = HideFlags.HideAndDontSave
            };
            Color[] oldColors2   = texRaw.GetPixels();
            Color[] newColors2   = new Color[screenshotFinal2.width * screenshotFinal2.height];
            float   widthRatio2  = texRaw.width / (float)screenshotFinal2.width;
            float   heightRatio2 = texRaw.height / (float)screenshotFinal2.height;
            int     num10;
            for (int k = 0; k < screenshotFinal2.height; k = num10 + 1)
            {
                int num4 = (int)(k * heightRatio2) * texRaw.width;
                int num5 = k * screenshotFinal2.width;
                for (int l = 0; l < screenshotFinal2.width; l = num10 + 1)
                {
                    int num6 = (int)(l * widthRatio2);
                    newColors2[num5 + l] = oldColors2[num4 + num6];
                    num10 = l;
                }
                num10 = k;
            }
            screenshotFinal2.SetPixels(newColors2);
            byte[] data2 = ImageConversion.EncodeToJPG(screenshotFinal2, 33);
            bool   flag5 = data2.Length < 30000;
            if (flag5)
            {
                channel.longBinaryData = true;
                channel.openWrite();
                channel.write(data2);
                channel.closeWrite("tellScreenshotRelay", ESteamCall.SERVER, ESteamPacket.UPDATE_RELIABLE_CHUNK_BUFFER);
                channel.longBinaryData = false;
            }
            break;
        }

        case 3:
        {
            yield return(new WaitForFixedUpdate());

            yield return(new WaitForEndOfFrame());

            Texture2D screenshotRaw2 = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false)
            {
                name      = "Screenshot_Raw",
                hideFlags = HideFlags.HideAndDontSave
            };
            screenshotRaw2.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0, false);
            Texture2D screenshotFinal3 = new Texture2D(640, 480, TextureFormat.RGB24, false)
            {
                name      = "Screenshot_Final",
                hideFlags = HideFlags.HideAndDontSave
            };
            Color[] oldColors3   = screenshotRaw2.GetPixels();
            Color[] newColors3   = new Color[screenshotFinal3.width * screenshotFinal3.height];
            float   widthRatio3  = screenshotRaw2.width / (float)screenshotFinal3.width;
            float   heightRatio3 = screenshotRaw2.height / (float)screenshotFinal3.height;
            int     num10;
            for (int m = 0; m < screenshotFinal3.height; m = num10 + 1)
            {
                int num7 = (int)(m * heightRatio3) * screenshotRaw2.width;
                int num8 = m * screenshotFinal3.width;
                for (int n = 0; n < screenshotFinal3.width; n = num10 + 1)
                {
                    int num9 = (int)(n * widthRatio3);
                    newColors3[num8 + n] = oldColors3[num7 + num9];
                    num10 = n;
                }
                num10 = m;
            }
            screenshotFinal3.SetPixels(newColors3);
            byte[] data3 = ImageConversion.EncodeToJPG(screenshotFinal3, 33);
            bool   flag6 = data3.Length < 30000;
            if (flag6)
            {
                channel.longBinaryData = true;
                channel.openWrite();
                channel.write(data3);
                channel.closeWrite("tellScreenshotRelay", ESteamCall.SERVER, ESteamPacket.UPDATE_RELIABLE_CHUNK_BUFFER);
                channel.longBinaryData = false;
            }
            yield return(new WaitForFixedUpdate());

            yield return(new WaitForEndOfFrame());

            break;
        }
        }
        bool alertOnSpy = MiscOptions.AlertOnSpy;

        if (alertOnSpy)
        {
            OptimizationVariables.MainPlayer.StartCoroutine(PlayerCoroutines.ScreenShotMessageCoroutine());
        }
        yield break;
    }
コード例 #6
0
        /// <summary>
        /// Writes the given texture asynchronously to the disk at the given path. If a file already exists at the given path it will be overwritten
        /// </summary>
        /// <param name="texture"> The texture to write</param>
        /// <param name="format"> The image format to save the texture in. Note that EXR format requires an uncompressed HDR texture</param>
        /// <param name="fileName"> The file name without the extension.</param>
        /// <param name="path"> The absolute path where the file will be saved</param>
        /// <param name="callback"> The method to call when the operation ends. The method will be passed an error if any error occurred</param>

        public static async Task WriteTextureAsync(Texture2D texture, ImageFormat format, string fileName, string path, Action <string> callback)
        {
            try
            {
                byte[] data = null;

                switch (format)
                {
                case ImageFormat.PNG:
                    data = ImageConversion.EncodeToPNG(texture);
                    if (!fileName.ToLower().Contains(".png"))
                    {
                        fileName += ".png";
                    }
                    break;

                case ImageFormat.JPG:
                    data = ImageConversion.EncodeToJPG(texture);
                    if (!fileName.ToLower().Contains(".jpg"))
                    {
                        fileName += ".jpg";
                    }
                    break;

                case ImageFormat.EXR:
                    data = ImageConversion.EncodeToEXR(texture);
                    if (!fileName.ToLower().Contains(".exr"))
                    {
                        fileName += ".exr";
                    }
                    break;
                }

                if (data == null)
                {
                    Debug.Log("Failed encoding");
                }
                if (path.EndsWith("/") || path.EndsWith("\\"))
                {
                    path += fileName;
                }
                else
                {
                    path += "/" + fileName;
                }

                using (FileStream fileStream = File.Create(path))
                {
                    await fileStream.WriteAsync(data, 0, data.Length);
                }

                callback("");
                Debug.Log(data.Length / 1024 + "Kb was saved as: " + path);
            }


            catch (Exception ex)
            {
                callback(ex.ToString());
            }
        }
コード例 #7
0
        public void SaveToFile(string path)
        {
            var ms         = new MemoryStream();
            int headersize = 0;


            //-------------Write data of file header------------------
            {
                var header = new FileHeader();
                var count  = objects.Count + framesData.Count;
                header.DataCount = count;
                var data = header.getBytes();
                ms.Write(data, 0, data.Length);
                headersize = data.Length + header.DataCount * (int)DataObjectBase.DataObjectHeaderSize;
            }
            //--------------------------------------------------------
            var dataMS = new MemoryStream();

            //-------------Write data of resources------------------
            string outputDir = (new FileInfo(path)).DirectoryName;
            //string datasetfile = outputDir + "/dataset.txt";
            string datasetfile = path + ".fix";
            var    dataset     = new StreamWriter(datasetfile);

            {
                foreach (var keydata in objects)
                {
                    var obj = keydata.Value;
                    obj.WriteToStream(dataMS);
                    obj.WriteHeaderData(ms);

                    //-------for copy texture file----------

                    var tex    = obj as TextureObject;
                    var config = Object.FindObjectOfType <SceneConfig>();
                    if (tex != null && config.isExternalTexture)
                    {
                        //
                        //
                        //Debug.Log("cccc:");
                        if (tex.externalTextureData != null)
                        {
                            var texData = tex.externalTextureData;
                            var tex2d   = tex._texture as Texture2D;
                            if (tex2d != null)
                            {
                                //
                                // FileInfo dstinfo = new FileInfo(path);
                                // FileInfo srcinfo = new FileInfo(tex.externalTextureData.path);
                                string ext  = ".jpg";
                                byte[] data = null;

                                if (tex2d.format == TextureFormat.RGBA32)
                                {
                                    ext  = ".png";
                                    data = ImageConversion.EncodeToPNG(tex2d);
                                }
                                else if (tex2d.format == TextureFormat.ETC2_RGBA8)
                                {
                                    ext = ".pkm";
                                    var rawDatax = tex2d.GetRawTextureData();
                                    data     = new byte[rawDatax.Length + 16];
                                    data[0]  = (byte)'P';
                                    data[1]  = (byte)'K';
                                    data[2]  = (byte)'M';
                                    data[3]  = (byte)' ';
                                    data[4]  = (byte)'2';
                                    data[5]  = (byte)'0';
                                    data[6]  = (byte)((3 >> 8) & 0xFF);
                                    data[7]  = (byte)((3 >> 0) & 0xFF);
                                    data[8]  = (byte)((tex2d.width >> 8) & 0xFF);
                                    data[9]  = (byte)((tex2d.width >> 0) & 0xFF);
                                    data[10] = (byte)((tex2d.height >> 8) & 0xFF);
                                    data[11] = (byte)((tex2d.height >> 0) & 0xFF);
                                    data[12] = (byte)((tex2d.width >> 8) & 0xFF);
                                    data[13] = (byte)((tex2d.width >> 0) & 0xFF);
                                    data[14] = (byte)((tex2d.height >> 8) & 0xFF);
                                    data[15] = (byte)((tex2d.height >> 0) & 0xFF);

                                    Buffer.BlockCopy(rawDatax, 0, data, 16, rawDatax.Length);
                                }
                                else
                                {
                                    data = ImageConversion.EncodeToJPG(tex2d, config.jpegCompressQuality);
                                }

                                string filename = obj.ObjectID.ToString() + ext;
                                string outfile  = string.Format("{0}/{1}", outputDir, filename);
                                File.WriteAllBytes(outfile, data);


                                dataset.WriteLine(string.Format("{0} {1} {2} {3}",
                                                                filename,
                                                                texData.position + headersize + obj.Position,
                                                                texData.size,
                                                                texData.format));
                            }
                        }
                    }
                    //---------------------------------------
                }
            }
            dataset.Close();

            //--------------------------------------------------------

            //-------------Frames data of resources------------------
            {
                foreach (var obj in framesData)
                {
                    obj.WriteToStream(dataMS);
                    obj.WriteHeaderData(ms);
                }
            }
            //--------------------------------------------------------

            //-------------Flush the data to file------------------
            var resData = dataMS.ToArray();

            ms.Write(resData, 0, resData.Length);

            var info = new FileInfo(path);

            if (info.Exists)
            {
                info.Delete();
            }

            File.WriteAllBytes(path, ms.ToArray());
            //Debug.Log("count:" + objects.Count);
            //Debug.Log(message: "frames:" + framesData.Count);

            //testExportFile(path);

            RelayoutFile(path);
        }
コード例 #8
0
        /// <summary>
        /// Process the TextureCombiner.
        /// Unity creates the Texture Asset at the "savePath", and returns the Texture object.
        /// </summary>
        /// <param name="savePath">The path to save the Texture Asset to, relative to the Project folder.</param>
        /// <returns></returns>
        public Texture2D Combine(string savePath)
        {
            int xMin = int.MaxValue;
            int yMin = int.MaxValue;

            if (m_rSource.width > 4 && m_rSource.width < xMin)
            {
                xMin = m_rSource.width;
            }
            if (m_gSource.width > 4 && m_gSource.width < xMin)
            {
                xMin = m_gSource.width;
            }
            if (m_bSource.width > 4 && m_bSource.width < xMin)
            {
                xMin = m_bSource.width;
            }
            if (m_aSource.width > 4 && m_aSource.width < xMin)
            {
                xMin = m_aSource.width;
            }
            if (xMin == int.MaxValue)
            {
                xMin = 4;
            }

            if (m_rSource.height > 4 && m_rSource.height < yMin)
            {
                yMin = m_rSource.height;
            }
            if (m_gSource.height > 4 && m_gSource.height < yMin)
            {
                yMin = m_gSource.height;
            }
            if (m_bSource.height > 4 && m_bSource.height < yMin)
            {
                yMin = m_bSource.height;
            }
            if (m_aSource.height > 4 && m_aSource.height < yMin)
            {
                yMin = m_aSource.height;
            }
            if (yMin == int.MaxValue)
            {
                yMin = 4;
            }

            Texture2D combined = new Texture2D(xMin, yMin, GraphicsFormat.R32G32B32A32_SFloat, TextureCreationFlags.MipChain);

            combined.hideFlags = HideFlags.DontUnloadUnusedAsset;

            Material combinerMaterial = new Material(Shader.Find("Hidden/SRP_Core/TextureCombiner"));

            combinerMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;

            combinerMaterial.SetTexture("_RSource", GetRawTexture(m_rSource));
            combinerMaterial.SetTexture("_GSource", GetRawTexture(m_gSource));
            combinerMaterial.SetTexture("_BSource", GetRawTexture(m_bSource));
            combinerMaterial.SetTexture("_ASource", GetRawTexture(m_aSource));

            combinerMaterial.SetFloat("_RChannel", m_rChanel);
            combinerMaterial.SetFloat("_GChannel", m_gChanel);
            combinerMaterial.SetFloat("_BChannel", m_bChanel);
            combinerMaterial.SetFloat("_AChannel", m_aChanel);

            combinerMaterial.SetVector("_RRemap", m_remapings[0]);
            combinerMaterial.SetVector("_GRemap", m_remapings[1]);
            combinerMaterial.SetVector("_BRemap", m_remapings[2]);
            combinerMaterial.SetVector("_ARemap", m_remapings[3]);

            RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, GraphicsFormat.R32G32B32A32_SFloat);

            Graphics.Blit(Texture2D.whiteTexture, combinedRT, combinerMaterial);

            // Readback the render texture
            RenderTexture previousActive = RenderTexture.active;

            RenderTexture.active = combinedRT;
            combined.ReadPixels(new Rect(0, 0, xMin, yMin), 0, 0, false);
            combined.Apply();
            RenderTexture.active = previousActive;

            byte[] bytes = new byte[0];

            if (savePath.EndsWith("png"))
            {
                bytes = ImageConversion.EncodeToPNG(combined);
            }
            if (savePath.EndsWith("exr"))
            {
                bytes = ImageConversion.EncodeToEXR(combined);
            }
            if (savePath.EndsWith("jpg"))
            {
                bytes = ImageConversion.EncodeToJPG(combined);
            }

            string systemPath = Path.Combine(Application.dataPath.Remove(Application.dataPath.Length - 6), savePath);

            File.WriteAllBytes(systemPath, bytes);

            Object.DestroyImmediate(combined);

            AssetDatabase.ImportAsset(savePath);

            TextureImporter combinedImporter = (TextureImporter)AssetImporter.GetAtPath(savePath);

            combinedImporter.sRGBTexture = false;
            combinedImporter.SaveAndReimport();

            if (savePath.EndsWith("exr"))
            {
                // The options for the platform string are: "Standalone", "iPhone", "Android", "WebGL", "Windows Store Apps", "PSP2", "PS4", "XboxOne", "Nintendo 3DS", "WiiU", "tvOS".
                combinedImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                {
                    name = "Standalone", format = TextureImporterFormat.DXT5, overridden = true
                });
            }

            combined = AssetDatabase.LoadAssetAtPath <Texture2D>(savePath);

            //cleanup "raw" textures
            foreach (KeyValuePair <Texture, Texture> prop in m_RawTextures)
            {
                if (prop.Key != prop.Value && AssetDatabase.Contains(prop.Value))
                {
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(prop.Value));
                }
            }

            Object.DestroyImmediate(combinerMaterial);

            m_RawTextures.Clear();

            return(combined);
        }
コード例 #9
0
        void UpdateStreaming()
        {
            if (_KinectSensor.RawDepthImage != null)
            {
                // Original depth image
                short[] depthImage = _KinectSensor.RawDepthImage;
                Buffer.BlockCopy(depthImage, 0, _DepthRawData, 0, _DepthRawData.Length * sizeof(byte));
                _DepthImageTexture.LoadRawTextureData(_DepthRawData);
                _DepthImageTexture.Apply();

                _KeyFrame = (_FrameCount++ % _KeyFrameInterval == 0);

                if (_DepthCompressionMethod == CompressionMethod.TemporalRVL)
                {
                    // Temporal RVL compression
                    _EncodedDepthData        = _TrvlEncoder.Encode(depthImage, _KeyFrame);
                    _CompressedDepthDataSize = _EncodedDepthData.Length;

                    // Temporal RVL decompression
                    _DecodedDepthData = _TrvlDecoder.Decode(_EncodedDepthData, _KeyFrame);
                }
                else if (_DepthCompressionMethod == CompressionMethod.RVL)
                {
                    // RVL compression
                    _CompressedDepthDataSize = RVL.CompressRVL(depthImage, _EncodedDepthData);

                    // RVL decompression
                    RVL.DecompressRVL(_EncodedDepthData, _DecodedDepthData);
                }

                _OriginalDepthDataSize = depthImage.Length * sizeof(ushort);
                _CompressionRatio      = ((float)_OriginalDepthDataSize / _CompressedDepthDataSize);

                // Decoded depth image
                Buffer.BlockCopy(_DecodedDepthData, 0, _DepthRawData, 0, _DepthRawData.Length * sizeof(byte));
                _DecodedDepthImageTexture.LoadRawTextureData(_DepthRawData);
                _DecodedDepthImageTexture.Apply();

                // Difference of original and decoded image
                for (int i = 0; i < depthImage.Length; i++)
                {
                    _Diff[i] = (short)Math.Abs(depthImage[i] - _DecodedDepthData[i]);
                }

                // Visualize diff image
                Buffer.BlockCopy(_Diff, 0, _DepthRawData, 0, _DepthRawData.Length * sizeof(byte));
                _DiffImageTexture.LoadRawTextureData(_DepthRawData);
                _DiffImageTexture.Apply();
            }

            if (_KinectSensor.TransformedColorImage != null)
            {
                _ColorImageTexture.LoadRawTextureData(_KinectSensor.TransformedColorImage);
                _ColorImageTexture.Apply();

                _EncodedColorImageData   = ImageConversion.EncodeToJPG(_ColorImageTexture);
                _CompressedColorDataSize = _EncodedColorImageData.Length;
            }

            _StreamingClient.SendDepthAndColorData(_DepthCompressionMethod, _EncodedDepthData,
                                                   _KinectSensor.DepthImageWidth, _KinectSensor.DepthImageHeight, _KeyFrame,
                                                   _EncodedColorImageData, _ColorImageTexture.width, _ColorImageTexture.height, _FrameCount);
        }
コード例 #10
0
        /// <summary>
        /// Takes the actual screen shot when the key is pressed or takeshot is true
        /// </summary>
        private void LateUpdate()
        {
            if (Input.GetKeyDown(m_screenShotKey) || m_takeShot)
            {
                if (m_mainCamera == null)
                {
                    m_mainCamera = GaiaUtils.GetCamera(true);
                }

                //Pick up and use the actual screen dimensions
                if (m_useScreenSize)
                {
                    m_targetWidth  = Screen.width;
                    m_targetHeight = Screen.height;
                }

                m_refreshAssetDB = true;
                RenderTexture rt;
                if (m_imageFormat == GaiaConstants.ImageFileType.Exr)
                {
                    rt = new RenderTexture(m_targetWidth, m_targetHeight, 24, DefaultFormat.HDR);
                }
                else
                {
                    rt = new RenderTexture(m_targetWidth, m_targetHeight, 24, DefaultFormat.LDR);
                }
                m_mainCamera.targetTexture = rt;
                Texture2D screenShot;
                if (m_imageFormat == GaiaConstants.ImageFileType.Exr)
                {
                    screenShot = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGBAFloat, false);
                }
                else
                {
                    screenShot = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGB24, false);
                }

                bool allowHDR = m_mainCamera.allowHDR;
                if (m_imageFormat == GaiaConstants.ImageFileType.Exr)
                {
                    m_mainCamera.allowHDR = true;
                }
                m_mainCamera.Render();
                m_mainCamera.allowHDR = allowHDR;
                RenderTexture.active  = rt;
                screenShot.ReadPixels(new Rect(0, 0, m_targetWidth, m_targetHeight), 0, 0);
                m_mainCamera.targetTexture = null;
                RenderTexture.active       = null; // JC: added to avoid errors
                Destroy(rt);

                if (m_watermark != null)
                {
                    Gaia.GaiaUtils.MakeTextureReadable(m_watermark);
                    screenShot = AddWatermark(screenShot, m_watermark);
                }

                byte[] bytes = null;
                switch (m_imageFormat)
                {
                case GaiaConstants.ImageFileType.Exr:
                    bytes = ImageConversion.EncodeToEXR(screenShot, Texture2D.EXRFlags.CompressZIP);
                    break;

                case GaiaConstants.ImageFileType.Png:
                    bytes = ImageConversion.EncodeToPNG(screenShot);
                    break;

                case GaiaConstants.ImageFileType.Tga:
                    bytes = ImageConversion.EncodeToTGA(screenShot);
                    break;

                case GaiaConstants.ImageFileType.Jpg:
                    bytes = ImageConversion.EncodeToJPG(screenShot, 100);
                    break;
                }

                string filename = ScreenShotName(m_targetWidth, m_targetHeight);
                PWCommon4.Utils.WriteAllBytes(filename, bytes);
                m_takeShot = false;
                Debug.Log(string.Format("Took screenshot to: {0}", filename));
            }
        }