Advertisement
Normantas

NSBitmapFilter

Jun 5th, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5.  
  6. namespace GrayScaleFilter
  7. {
  8.     static class NSBitmapFilter
  9.     {   //<summary>
  10.         //<para> Applies Filter to Bitmap, returns Bitmap </para>
  11.         //<para>All Made by Normantas Stankevicius</para>
  12.         // Uses System.Common.Drawing Library
  13.         //</summary>
  14.  
  15.         public static Bitmap ConvertToGrayscale(Bitmap bitmap)
  16.         {
  17.             // Convers the pixels of a BitmapToGray
  18.             for (int x = 0; x < bitmap.Width; x++)
  19.                 for (int y = 0; y < bitmap.Height; y++)
  20.                 {
  21.                     Color pixel = bitmap.GetPixel(x, y);
  22.                     int grayColor = System.Convert.ToInt32(0.299 * pixel.R + 0.587 * pixel.G + 0.114 * pixel.B);
  23.                     pixel = Color.FromArgb(pixel.A, grayColor, grayColor, grayColor);
  24.                     bitmap.SetPixel(x, y, pixel);
  25.                 }
  26.             return bitmap;
  27.         }
  28.         public static Bitmap ConvertToNeonware(Bitmap bitmap)
  29.         {
  30.             // Convers the pixels of a Bitmap to vaporwave
  31.             for (int x = 0; x < bitmap.Width; x++)
  32.                 for (int y = 0; y < bitmap.Height; y++)
  33.                 {
  34.                     Color pixel = bitmap.GetPixel(x, y).ToNeonware();
  35.                     bitmap.SetPixel(x, y, pixel);
  36.                 }
  37.             return bitmap;
  38.         }
  39.         private static Color ToNeonware(this Color pixel)
  40.         {
  41.             //<Summary>
  42.             // Converts a pixel of an image to vaporwave colors
  43.             //</summary>
  44.             int grayScale = System.Convert.ToInt32(0.299 * pixel.R + 0.587 * pixel.G + 0.114 * pixel.B);
  45.             int epsilon = 7; // Allowed Deviation from the original
  46.             if (Math.Abs(pixel.R - grayScale) <= epsilon && Math.Abs(pixel.G - grayScale) <= epsilon && Math.Abs(pixel.B - grayScale) <= epsilon)
  47.                 return pixel;
  48.             else
  49.             {
  50.                 // Applies Vaporwave
  51.                 // R - Red, G - Green, B - Blue of a color
  52.                 // Y - Luma, U - Blue projection, V - Red Projection
  53.                 // RGB to YUV
  54.                
  55.                 double Y = 0.257 * pixel.R + 0.504 * pixel.G + 0.098 * pixel.B + 16;
  56.                 double U = -0.148 * pixel.R - 0.291 * pixel.G + 0.439 * pixel.B + 128;
  57.                 double V = 0.439 * pixel.R - 0.368 * pixel.G - 0.071 * pixel.B + 128;
  58.  
  59.                 double yMultiplier = 1.70 + (Math.Log(Y,0.9) / 100);
  60.                 double uMultiplier = 1.52 + (Math.Log(U, 0.9) / 100);
  61.                 double vMultiplier = 1.52 + (Math.Log(V, 0.9) / 100);
  62.                 Y = Math.Round(Y * yMultiplier,2);
  63.                 if (Math.Abs(U) > Math.Abs(V))
  64.                     U = Math.Round(U * uMultiplier,2);
  65.                 else if (Math.Abs(V) > Math.Abs(U))
  66.                     V = Math.Round(V * uMultiplier, 2);
  67.  
  68.  
  69.                 // YUV to RGB
  70.                 Y -= 16;
  71.                 U -= 128;
  72.                 V -= 128;
  73.                
  74.                 int R = Convert.ToInt32(1.164 * Y + 1.596 * V);
  75.                 int G = Convert.ToInt32(1.164 * Y - 0.392 * U - 0.813 * V);
  76.                 int B = Convert.ToInt32(1.164 * Y + 2.017 * U);
  77.                 double logRed = Math.Round(Math.Log(R, 0.89) / 100, 2);
  78.                 double logGreen = 0;
  79.                 double logBlue = Math.Round(Math.Log(B, 0.89) / 100, 2);
  80.                 double redMultiplier = 1.7 + logRed;
  81.                 double greenMultiplier = 0.7 + logGreen;
  82.                 double blueMultiplier = 1.6 + logBlue;
  83.                 R = Convert.ToInt32(R * redMultiplier);
  84.                 G = Convert.ToInt32(G * greenMultiplier);
  85.                 B = Convert.ToInt32(B * blueMultiplier);
  86.  
  87.                 // Safety Net
  88.                 if (R >= 255)
  89.                     R = 255;
  90.                 else if (R < 0)
  91.                     R = 0;
  92.  
  93.                 if (G >= 255)
  94.                     G = 255;
  95.                 else if (G < 0)
  96.                     G = 0;
  97.  
  98.                 if (B >= 255)
  99.                     B = 255;
  100.                 else if (B < 0)
  101.                     B = 0;
  102.  
  103.                 pixel = Color.FromArgb(pixel.A, R, G, B);
  104.             }
  105.             return pixel;
  106.         }
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement