static Texture2D MakeGrayLevel(Texture2D img) { Xna.Color[] inBufer = new Xna.Color[img.Width * img.Height]; img.GetData(inBufer); Texture2D grayed = new Texture2D(graphicsDevice, img.Width, img.Height); Xna.Color[] outBufer = new Xna.Color[img.Width * img.Height]; for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { Xna.Color pixelColor = inBufer[x + y * img.Width]; float brightness = pixelColor.GetBrightness(); int rgb = (int)(255 * GRAYLEVEL_DIM_FACTOR * brightness); outBufer[x + y * img.Width] = new Xna.Color(rgb, rgb, rgb, pixelColor.A); } } grayed.SetData(outBufer); return(grayed); }
/// <summary> /// Returns a surface with the specified image rendered to it as characters. /// </summary> /// <param name="image">The image to render.</param> /// <returns>The surface.</returns> public TextSurface GetSurface(Texture2D image) { editor.Clear(); image.GetData<Color>(pixels); System.Threading.Tasks.Parallel.For(0, surface.Width * surface.Height, (i) => //for (int i = 0; i < surface.Width * surface.Height; i++) { int allR = 0; int allG = 0; int allB = 0; int min = i * fontPixels; int max = min + fontPixels; for (int pixel = min; pixel < max; pixel++) { Color color = pixels[indexes[pixel]]; allR += color.R; allG += color.G; allB += color.B; } // print our character byte sr = (byte)(allR / fontPixels); byte sg = (byte)(allG / fontPixels); byte sb = (byte)(allB / fontPixels); var newColor = new Color(sr, sg, sb); float sbri = newColor.GetBrightness() * 255; Point surfacePoint = surface.GetPointFromIndex(i); if (UseBlockMode) { if (sbri > 204) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 219, newColor); //█ else if (sbri > 152) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 178, newColor); //▓ else if (sbri > 100) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 177, newColor); //▒ else if (sbri > 48) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 176, newColor); //░ else editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black); } else { if (sbri > 230) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'#', newColor); else if (sbri > 207) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'&', newColor); else if (sbri > 184) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'$', newColor); else if (sbri > 161) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'X', newColor); else if (sbri > 138) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'x', newColor); else if (sbri > 115) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'=', newColor); else if (sbri > 92) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'+', newColor); else if (sbri > 69) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)';', newColor); else if (sbri > 46) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)':', newColor); else if (sbri > 23) editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'.', newColor); else editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black); } } ); return surface; }