11
2014
Image To Grayscale Script
Quick test trying to convert this Basic256 script into Unity c# (trying to keep it close to original code..)
maybe still some problems as the output seems darker than the example..
http://rosettacode.org/wiki/Grayscale_image#BASIC256
Source C# (Image2Grayscale.cs)
// Original source: http://rosettacode.org/wiki/Grayscale_image#BASIC256 using UnityEngine; using System.Collections; public class Image2Grayscale : MonoBehaviour { string fileName = "BASIC256_greyscale_Mona_Lisa"; Texture2D graph; void Start () { LoadImage(); ConvertToGrayscale(); } void LoadImage() { graph = Resources.Load(fileName, typeof(Texture2D)) as Texture2D; } void ConvertToGrayscale() { Color32[] pixels = graph.GetPixels32(); for (int x=0;x<graph.width;x++) { for (int y=0;y<graph.height;y++) { Color32 pixel = pixels[x+y*graph.width]; int p = ( (256 * 256 + pixel.r) * 256 + pixel.b) * 256 + pixel.g; int b = p % 256; p = Mathf.FloorToInt(p / 256); int g = p % 256; p = Mathf.FloorToInt (p / 256); int r = p % 256; float l = (0.2126f*r/255f) + 0.7152f*(g/255f) + 0.0722f*(b/255f); Color c = new Color(l,l,l,1); graph.SetPixel(x,y,c); } } graph.Apply(false); var bytes = graph.EncodeToPNG(); System.IO.File.WriteAllBytes(Application.dataPath + "ImageSaveTest.png", bytes); } }
Related Posts
Leave a comment
Recent posts
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
- Check if point is inside polygon 2D (using line intersection)
- UnityHub: Enable Create Project Button (without having to select Cloud Organization)
- UI: Make Tooltip background fit Text content length
- Pong-Wars in Unity
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub)
- UnityHub: Add support for custom project title (instead of folder name) or use ProductName
- Testing Unity MUSE AI
- QR Code Generator Online