Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace ConsoleApplication1 {
- class Program {
- static unsafe void hardlight(byte* topColor, byte* botcolor, byte* outColor) {
- overlay(botcolor, topColor, outColor);
- }
- static unsafe void overlay(byte* topData, byte* botData, byte* outData) {
- outData[3] = topData[3];
- float inA = topData[3] / 255f;
- float layerA = botData[3] / 255f;
- float compAlpha = Math.Min(inA, layerA);
- float newAlpha = inA + (1f - inA) * compAlpha;
- if (compAlpha != 0 && newAlpha != 0) {
- float ratio = compAlpha / newAlpha;
- for (int i = 0; i < 3; i++) {
- float top = topData[i] / 255f;
- float bot = botData[i] / 255f;
- float c;
- if (bot > 0.5f) {
- float value = (1f - bot) / 0.5f;
- float min = bot - (1f - bot);
- c = top * value + min;
- }
- else {
- float value = bot / 0.5f;
- c = top * value;
- }
- c = (c * ratio) + (top * (1f - ratio));
- outData[i] = (byte) Math.Max(0, Math.Min(255, (int)Math.Floor(c * 255)));
- }
- }
- else {
- for (int i = 0; i < 3; i++) {
- outData[i] = botData[i];
- }
- }
- }
- static unsafe void hardlightImage(BitmapData topBmd, BitmapData botBmd, BitmapData outBmd) {
- int w = outBmd.Width;
- int h = outBmd.Height;
- byte* topP = (byte*)topBmd.Scan0.ToPointer();
- byte* botP = (byte*)botBmd.Scan0.ToPointer();
- byte* outP = (byte*)outBmd.Scan0.ToPointer();
- int stride = botBmd.Stride;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- int i = y * stride + x * 4;
- byte* top = topP + i;
- byte* bot = botP + i;
- byte* o = outP + i;
- hardlight(top, bot, o);
- }
- }
- }
- static void Main(string[] args) {
- Bitmap asuka = Bitmap.FromFile("AsukaBG.png") as Bitmap;
- Bitmap color = Bitmap.FromFile("colora.png") as Bitmap;
- Bitmap output = new Bitmap(asuka.Width, asuka.Height);
- Rectangle bounds = new Rectangle(0, 0, output.Width, output.Height);
- BitmapData asukaBmd = asuka.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- BitmapData colorBmd = color.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- BitmapData outBmd = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
- int loops = 10;
- long start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
- for(int i=0; i < loops; i++) {
- hardlightImage(colorBmd, asukaBmd, outBmd);
- }
- long end = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
- // Per loop, ~220ms
- Console.WriteLine("Took {0}ms for {1} loops, that is {2}ms per loop", end-start, loops, (end- start)/(float)loops);
- // Release memory
- asuka.UnlockBits(asukaBmd);
- color.UnlockBits(colorBmd);
- output.UnlockBits(outBmd);
- output.Save("hardlight.png");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment