private void OnValidate() { Texture2D sourceTexture = (Texture2D)sourceImage.texture; Color[] data = sourceTexture.GetPixels(); DownSampledImage downSampledImage = DownSampling.CompressImage(data); byte[] uncompressedData = DownSampling.DecompressImage(downSampledImage); Texture2D tex = new Texture2D(sourceImage.texture.width, sourceImage.texture.height, TextureFormat.RGBA32, false); tex.LoadRawTextureData(uncompressedData); tex.Apply(); targetImage.texture = tex; }
private Texture2D getUncompressedTextureFromBytes(byte[] byteArray) { byte compressionRate = byteArray[byteArray.Length - 1]; byteArray = byteArray.Take(byteArray.Count() - 1).ToArray(); DownSampledImage image = new DownSampledImage(byteArray, compressionRate); byte[] uncompressedData = DownSampling.DecompressImage(image); int rowSize = (int)Mathf.Sqrt(byteArray.Length) * compressionRate; Texture2D tex = new Texture2D(rowSize, rowSize, TextureFormat.RGBA32, false); tex.LoadRawTextureData(uncompressedData); tex.Apply(); return(tex); }
public void SendImageToMaster(RawImage image, string playerNickname) { Texture2D sourceTexture = (Texture2D)image.texture; Color[] data = sourceTexture.GetPixels(); DownSampledImage downSampledImage = DownSampling.CompressImage(data); byte[] byteArray = new byte[downSampledImage.data.Length + 1]; for (int i = 0; i < byteArray.Length; i++) { if (i == byteArray.Length - 1) { byteArray[i] = downSampledImage.comppresionRate; break; } byteArray[i] = downSampledImage.data[i]; } photonView.RPC("ReceiveImageFromPlayer", RpcTarget.MasterClient, byteArray as object, playerNickname); }
public DownSamplingProFunctionChart(ChartView BaseChart) { points = new Points(); line = new Line(); downSampling = new DownSampling(); var = new Variables.Variables(); BaseChart.Chart.Title.Text = "Downsampling"; BaseChart.Chart.Axes.Left.Automatic = true; BaseChart.Chart.Axes.Left.Ticks.Transparency = 100; BaseChart.Chart.Axes.Bottom.Automatic = true; BaseChart.Chart.Axes.Left.Increment = 200; points.FillSampleValues(500); points.Title = "Data"; points.Color = var.GetPaletteBasic[0]; points.Pointer.VertSize -= 2; points.Pointer.HorizSize -= 2; points.Pointer.Style = PointerStyles.Triangle; points.Marks.Transparency = 100; line.DataSource = points; line.Function = downSampling; line.Title = "Reduced Data"; line.LinePen.Width = 3; line.Color = var.GetPaletteBasic[1]; line.Marks.DrawEvery = 3; downSampling.Tolerance = 10; downSampling.DisplayedPointCount = 0; downSampling.Period = 1; downSampling.Method = DownSamplingMethod.Average; BaseChart.Chart.Series.Add(points); BaseChart.Chart.Series.Add(line); }
public void Update ( Texture sourceTexture, float sourceValueThreshold, DownSampling downSampling = DownSampling.None, Precision precision = Precision._32, bool _showSource = false ) { if (!sourceTexture) { return; } if (!_computeShader) { _computeShader = Object.Instantiate <ComputeShader>(Resources.Load <ComputeShader>(nameof(SDFTextureGenerator) + "Compute")); _computeShader.hideFlags = HideFlags.HideAndDontSave; _seedKernel = _computeShader.FindKernel("Seed"); _floodKernel = _computeShader.FindKernel("Flood"); _distKernel = _computeShader.FindKernel("Dist"); _showSeedsKernel = _computeShader.FindKernel("ShowSeeds"); } Vector2Int resolution = new Vector2Int(sourceTexture.width, sourceTexture.height); switch (downSampling) { case DownSampling.Half: resolution /= 2; break; case DownSampling.Quater: resolution /= 4; break; } GraphicsFormat sdfFormat = precision == Precision._32 ? GraphicsFormat.R32_SFloat : GraphicsFormat.R16_SFloat; if (!_sdfTexture || _sdfTexture.width != resolution.x || _sdfTexture.height != resolution.y || _sdfTexture.graphicsFormat != sdfFormat) { if (_sdfTexture) { _sdfTexture.Release(); } _sdfTexture = new RenderTexture(resolution.x, resolution.y, 0, sdfFormat, 0); _sdfTexture.name = "SDFTexture"; _sdfTexture.autoGenerateMips = false; _sdfTexture.enableRandomWrite = true; _sdfTexture.Create(); } if (!_jfaTexture || _jfaTexture.width != resolution.x || _jfaTexture.height != resolution.y) { if (_jfaTexture) { _jfaTexture.Release(); } //_jfaTexture = new RenderTexture( resolution.x, resolution.y, 0, GraphicsFormat.R16G16B16A16_SInt, 0 ); _jfaTexture = new RenderTexture(resolution.x, resolution.y, 0, RenderTextureFormat.ARGBInt, RenderTextureReadWrite.Linear); _jfaTexture.name = "JFATexture"; _jfaTexture.autoGenerateMips = false; _jfaTexture.enableRandomWrite = true; _jfaTexture.Create(); _computeShader.SetTexture(_seedKernel, "_FloodTex", _jfaTexture); _computeShader.SetTexture(_floodKernel, "_FloodTex", _jfaTexture); _computeShader.SetTexture(_distKernel, "_FloodTexRead", _jfaTexture); } _computeShader.SetTexture(_seedKernel, ShaderIDs.seedTexRead, sourceTexture); _computeShader.SetTexture(_distKernel, ShaderIDs.sdfTex, _sdfTexture); // Seed. _computeShader.SetTexture(_seedKernel, ShaderIDs.seedTexRead, sourceTexture); _computeShader.SetVector(ShaderIDs.texelSize, _sdfTexture.texelSize); _computeShader.SetFloat(ShaderIDs.seedThreshold, sourceValueThreshold); _computeShader.Dispatch(_seedKernel, resolution.x, resolution.y, 1); //Debug.Log( "Dispatch: " + resolution.x + "x" + resolution.y ); // Show seeds. if (_showSource) { _computeShader.SetTexture(_showSeedsKernel, ShaderIDs.floodTexRead, _jfaTexture); _computeShader.SetTexture(_showSeedsKernel, ShaderIDs.sdfTex, _sdfTexture); _computeShader.Dispatch(_showSeedsKernel, resolution.x, resolution.y, 1); return; } // Flood. int sizeMax = Mathf.Max(resolution.x, resolution.y); int stepMax = (int)Mathf.Log(Mathf.NextPowerOfTwo(sizeMax), 2); // 2^c_maxSteps is max image size on x and y for (int n = stepMax; n >= 0; n--) { int stepSize = n > 0 ? (int)Mathf.Pow(2, n) : 1; _computeShader.SetInt(ShaderIDs.stepSize, stepSize); _computeShader.Dispatch(_floodKernel, resolution.x, resolution.y, 1); } // Compute SDF. _computeShader.Dispatch(_distKernel, resolution.x, resolution.y, 1); }