TheAMM

Untitled

Dec 10th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9.  
  10. namespace ConsoleApplication1 {
  11.     class Program {
  12.  
  13.         static unsafe void hardlight(byte* topColor, byte* botcolor, byte* outColor) {
  14.             overlay(botcolor, topColor, outColor);
  15.         }
  16.  
  17.         static unsafe void overlay(byte* topData, byte* botData, byte* outData) {
  18.             outData[3] = topData[3];
  19.             float inA = topData[3] / 255f;
  20.             float layerA = botData[3] / 255f;
  21.  
  22.             float compAlpha = Math.Min(inA, layerA);
  23.  
  24.             float newAlpha = inA + (1f - inA) * compAlpha;
  25.  
  26.             if (compAlpha != 0 && newAlpha != 0) {
  27.                 float ratio = compAlpha / newAlpha;
  28.                 for (int i = 0; i < 3; i++) {
  29.                     float top = topData[i] / 255f;
  30.                     float bot = botData[i] / 255f;
  31.  
  32.                     float c;
  33.                     if (bot > 0.5f) {
  34.                         float value = (1f - bot) / 0.5f;
  35.                         float min = bot - (1f - bot);
  36.                         c = top * value + min;
  37.                     }
  38.                     else {
  39.                         float value = bot / 0.5f;
  40.                         c = top * value;
  41.                     }
  42.                     c = (c * ratio) + (top * (1f - ratio));
  43.                     outData[i] = (byte) Math.Max(0, Math.Min(255, (int)Math.Floor(c * 255)));
  44.                 }
  45.             }
  46.             else {
  47.                 for (int i = 0; i < 3; i++) {
  48.                     outData[i] = botData[i];
  49.                 }
  50.             }
  51.         }
  52.  
  53.         static unsafe void hardlightImage(BitmapData topBmd, BitmapData botBmd, BitmapData outBmd) {
  54.             int w = outBmd.Width;
  55.             int h = outBmd.Height;
  56.  
  57.             byte* topP = (byte*)topBmd.Scan0.ToPointer();
  58.             byte* botP = (byte*)botBmd.Scan0.ToPointer();
  59.             byte* outP = (byte*)outBmd.Scan0.ToPointer();
  60.  
  61.             int stride = botBmd.Stride;
  62.            
  63.             for (int y = 0; y < h; y++) {
  64.                 for (int x = 0; x < w; x++) {
  65.                     int i = y * stride + x * 4;
  66.  
  67.                     byte* top = topP + i;
  68.                     byte* bot = botP + i;
  69.                     byte* o = outP + i;
  70.  
  71.                     hardlight(top, bot, o);
  72.                 }
  73.             }
  74.         }
  75.  
  76.         static void Main(string[] args) {
  77.             Bitmap asuka = Bitmap.FromFile("AsukaBG.png") as Bitmap;
  78.             Bitmap color = Bitmap.FromFile("colora.png") as Bitmap;
  79.  
  80.             Bitmap output = new Bitmap(asuka.Width, asuka.Height);
  81.             Rectangle bounds = new Rectangle(0, 0, output.Width, output.Height);
  82.  
  83.             BitmapData asukaBmd = asuka.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  84.             BitmapData colorBmd = color.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  85.             BitmapData outBmd = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
  86.  
  87.             int loops = 10;
  88.             long start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  89.             for(int i=0; i < loops; i++) {
  90.                 hardlightImage(colorBmd, asukaBmd, outBmd);
  91.             }
  92.             long end = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  93.             // Per loop, ~220ms
  94.             Console.WriteLine("Took {0}ms for {1} loops, that is {2}ms per loop", end-start, loops, (end- start)/(float)loops);
  95.  
  96.             // Release memory
  97.             asuka.UnlockBits(asukaBmd);
  98.             color.UnlockBits(colorBmd);
  99.             output.UnlockBits(outBmd);
  100.  
  101.             output.Save("hardlight.png");
  102.  
  103.             Console.ReadLine();
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment