Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; using System.Runtime.InteropServices; namespace LowLevelBitmapEdit { /// <summary> /// This static class accessing the given picture via its Scan0 ptr. /// <para /> /// The normal access goes via: /// <para /> /// <remarks> /// void Method(Bitmap bmp)<para /> /// {<para /> /// BitmapData bmpDat;<para /> /// bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);<para /> /// unsafe<para /> /// {<para /> /// byte* picPtr = (byte*)bmpDat.Scan0;<para /> /// int size = bmp.Height * bmp.Width;<para /> /// for (int i = 0; i < size; i++)<para /> /// {<para /> /// //do some operations here<para /> /// }<para /> /// }<para /> /// bmp.UnlockBits(bmpDat);<para /> /// }<para /> /// </remarks> /// </summary> public static class Filters { #region Manipulating Methods /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void SetOnlyBlue(Bitmap bmp) { BitmapData bmpDat; byte blue; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = (bmp.Height) * (bmp.Width); for (int i = 0; i < size; i++) { blue = picPtr[0]; picPtr[0] = 0; picPtr += 4; } } bmp.UnlockBits(bmpDat); } /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void Invertion(Bitmap bmp) { BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width * 4; for (int i = 0; i < size; i++) picPtr[i] = (byte)(255 - picPtr[i]); } bmp.UnlockBits(bmpDat); } /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void GreyScale(Bitmap bmp) { BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width; for (int i = 0; i < size; i++) { //getting the rgb colors/values byte blue = picPtr[0]; byte green = picPtr[1]; byte red = picPtr[2]; //computing the grey colors: byte grey = (byte)((77 * blue + 151 * green + 28 * red) / 256); //write it back to the picture: picPtr[0] = picPtr[1] = picPtr[2] = grey; picPtr += 4; } } bmp.UnlockBits(bmpDat); } /// <summary> /// /// </summary> /// <param name="bmp"></param> /// <param name="val"></param> public static void Brightness(Bitmap bmp, short val) { //at first we computes a so called Look-Up-Table (lut) //that makes sense for the performance by very large pictures. //because in this way we writes the picture bytes into an array, //and could simply accessing the needed bytes via this array. //here we go with our LUT: byte[] picBytes = new byte[256]; for (int i = 0; i < 256; i++) { picBytes[i] = Norming(i + val); } BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width; for (int i = 0; i < size; i++) { //and now we simply get the values from our LUT: picPtr[0] = picBytes[picPtr[0]]; //blue picPtr[1] = picBytes[picPtr[1]]; //green picPtr[2] = picBytes[picPtr[2]]; //red //increment our ptr with 4; picPtr += 4; } } bmp.UnlockBits(bmpDat); } /// <summary> /// /// </summary> /// <param name="bmp"></param> /// <param name="val"></param> public static void SetContrast(Bitmap bmp, float val) { byte[] picBytes = new byte[256]; val = (1 + val / 100); //creating our LUT: for (int i = 0; i < 256; i++) { picBytes[i] = Norming((int)((i - 128) * val) + 128); } BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width; for (int i = 0; i < size; i++) { //and now we simply get the values from our LUT: picPtr[0] = picBytes[picPtr[0]]; //blue picPtr[1] = picBytes[picPtr[1]]; //green picPtr[2] = picBytes[picPtr[2]]; //red //increment our ptr with 4; picPtr += 4; } } bmp.UnlockBits(bmpDat); } /// <summary> /// /// </summary> /// <param name="bmp"></param> /// <param name="val"></param> public static void SetGamma(Bitmap bmp, double val) { byte[] picBytes = new byte[256]; for (int i = 0; i < 256; i++) { picBytes[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / val)) + 0.5)); } BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width; for (int i = 0; i < size; i++) { //and now we simply get the values from our LUT: picPtr[0] = picBytes[picPtr[0]]; //blue picPtr[1] = picBytes[picPtr[1]]; //green picPtr[2] = picBytes[picPtr[2]]; //red //increment our ptr with 4; picPtr += 4; } } bmp.UnlockBits(bmpDat); } /// <summary> /// This Method is needed to stretch the Histograph of a Picture.<para /> /// This could be useful for Photos with a wrong exposure. /// </summary> /// <param name="bmp"></param> public static void AutoAdjustment(Bitmap bmp) { //this time we need a little bit more computations. //at first we start also with our LUT, but we need to compute every color-layer: int[,] picBytes = new int[256, 3]; BitmapData bmpDat; bmpDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* picPtr = (byte*)bmpDat.Scan0; int size = bmp.Height * bmp.Width; for (int i = 0; i < size; i++) { //no we create the histograph with our LUT, //and counting the given color/byte values picBytes[picPtr[0], 0]++; //blue layer picBytes[picPtr[1], 1]++; //green layer picBytes[picPtr[2], 2]++; //red layer picPtr += 4; } //here we have to compute every single color-channel: for (int colr = 0; colr < 3; colr++) { //determine whats the minimal and the maximal uses color value: //min: int i = 0; while (picBytes[i, colr] == 0) i++; int min = i; //max: i = 255; while (picBytes[i, colr] == 0) i--; int max = i; //compute the scale float scale = 255 / (float)(max - min); //getting the LUT for the specific color channel for (int k = 0; k < 244; k++) { picBytes[k, colr] = Norming((int)((k - min) * scale)); } } //setting the ptr to the first byte: picPtr = (byte*)bmpDat.Scan0; //working on all color-channels via the LUT: for (int i = 0; i < size; i++) { //and now we simply get the values from our LUT: picPtr[0] = (byte)picBytes[picPtr[0], 0]; //blue picPtr[1] = (byte)picBytes[picPtr[1], 1]; //green picPtr[2] = (byte)picBytes[picPtr[2], 2]; //red //increment our ptr with 4; picPtr += 4; } } bmp.UnlockBits(bmpDat); } #region ShortedMethods /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void GaussianSmoothing(Bitmap bmp) { GraphicFilter gf = new GraphicFilter(); gf.Divider = 16; gf.ColorMatrix[0, 0] = 1; gf.ColorMatrix[1, 0] = 2; gf.ColorMatrix[2, 0] = 1; gf.ColorMatrix[0, 1] = 2; gf.ColorMatrix[1, 1] = 4; gf.ColorMatrix[2, 1] = 2; gf.ColorMatrix[0, 2] = 1; gf.ColorMatrix[1, 2] = 2; gf.ColorMatrix[2, 2] = 1; gf.Execution(bmp); } /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void Emboss(Bitmap bmp) { GraphicFilter gf = new GraphicFilter(); gf.Divider = 1; gf.Offset = 127; gf.ColorMatrix[0, 0] = -1; gf.ColorMatrix[1, 0] = 0; gf.ColorMatrix[2, 0] = -1; gf.ColorMatrix[0, 1] = 0; gf.ColorMatrix[1, 1] = 4; gf.ColorMatrix[2, 1] = 0; gf.ColorMatrix[0, 2] = -1; gf.ColorMatrix[1, 2] = 0; gf.ColorMatrix[2, 2] = -1; gf.Execution(bmp); } /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void Smoothing(Bitmap bmp) { GraphicFilter gf = new GraphicFilter(); gf.Divider = 8; gf.ColorMatrix[0, 0] = 1; gf.ColorMatrix[1, 0] = 1; gf.ColorMatrix[2, 0] = 1; gf.ColorMatrix[0, 1] = 1; gf.ColorMatrix[1, 1] = 1; gf.ColorMatrix[2, 1] = 1; gf.ColorMatrix[0, 2] = 1; gf.ColorMatrix[1, 2] = 1; gf.ColorMatrix[2, 2] = 1; gf.Execution(bmp); } /// <summary> /// /// </summary> /// <param name="bmp"></param> public static void Sharpen(Bitmap bmp) { GraphicFilter gf = new GraphicFilter(); gf.Divider = 3; gf.ColorMatrix[0, 0] = 0; gf.ColorMatrix[1, 0] = -2; gf.ColorMatrix[2, 0] = 0; gf.ColorMatrix[0, 1] = -2; gf.ColorMatrix[1, 1] = 11; gf.ColorMatrix[2, 1] = -2; gf.ColorMatrix[0, 2] = 0; gf.ColorMatrix[1, 2] = -2; gf.ColorMatrix[2, 2] = 0; gf.Execution(bmp); } #endregion #endregion #region helperMethod /// <summary> /// Ensures that the values got its correct/allowed values between 0 and 255. /// </summary> /// <param name="val"><see cref="System.Int32"/>.</param> /// <returns>A <see cref="System.Byte"/>.</returns> private static byte Norming(int val) { if (val < 0) return 0; if (val > 255) return 255; return (byte)val; } #endregion public static Bitmap MedianFilter(Bitmap Image, int Size) { Bitmap TempBitmap = Image; Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height); Graphics NewGraphics = Graphics.FromImage(NewBitmap); NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), GraphicsUnit.Pixel); NewGraphics.Dispose(); Random TempRandom = new Random(); int ApetureMin = -(Size / 2); int ApetureMax = (Size / 2); for (int x = 0; x < NewBitmap.Width; ++x) { for (int y = 0; y < NewBitmap.Height; ++y) { List<int> RValues = new List<int>(); List<int> GValues = new List<int>(); List<int> BValues = new List<int>(); for (int x2 = ApetureMin; x2 < ApetureMax; ++x2) { int TempX = x + x2; if (TempX >= 0 && TempX < NewBitmap.Width) { for (int y2 = ApetureMin; y2 < ApetureMax; ++y2) { int TempY = y + y2; if (TempY >= 0 && TempY < NewBitmap.Height) { Color TempColor = TempBitmap.GetPixel(TempX, TempY); RValues.Add(TempColor.R); GValues.Add(TempColor.G); BValues.Add(TempColor.B); } } } } RValues.Sort(); GValues.Sort(); BValues.Sort(); Color MedianPixel = Color.FromArgb( RValues[RValues.Count / 2], GValues[GValues.Count / 2], BValues[BValues.Count / 2]); NewBitmap.SetPixel(x, y, MedianPixel); } } return NewBitmap; } public static Bitmap MedianFilter(Bitmap bmp) { int Size = 2; List<byte> R = new List<byte>(); List<byte> G = new List<byte>(); List<byte> B = new List<byte>(); int ApetureMin = -(Size / 2); int ApetureMax = (Size / 2); BitmapData imageData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* start = (byte*)imageData.Scan0; for (int x = 0; x < imageData.Width; x++) { for (int y = 0; y < imageData.Height; y++) { for (int x1 = ApetureMin; x1 < ApetureMax; x1++) { int valx = x + x1; if (valx >= 0 && valx < imageData.Width) { for (int y1 = ApetureMin; y1 < ApetureMax; y1++) { int valy = y + y1; if (valy >= 0 && valy < imageData.Height) { // error come from here Color tempColor = bmp.GetPixel(valx, valy); R.Add(tempColor.R); G.Add(tempColor.G); B.Add(tempColor.B); } } } } } } R.Sort(); G.Sort(); B.Sort(); } bmp.UnlockBits(imageData); return bmp; } } /// <summary> /// This Class helps us to encapsulates and to compute the nearly same actions. /// </summary> public class GraphicFilter { /// <summary> /// /// </summary> public int[,] ColorMatrix = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; /// <summary> /// /// </summary> public int Divider = 0; /// <summary> /// /// </summary> public int Offset = 0; /// <summary> /// Ensures that the values got its correct/allowed values between 0 and 255. /// </summary> /// <param name="val"><see cref="System.Int32"/>.</param> /// <returns>A <see cref="System.Byte"/>.</returns> private byte Norming(int val) { if (val < 0) return 0; if (val > 255) return 255; return (byte)val; } /// <summary> /// This Method represents all the actions we have to do by manipulating Bitmaps. /// </summary> /// <param name="bmp"></param> public void Execution(Bitmap bmp) { //create a copy of the bmp: Bitmap bmpSource = (Bitmap)bmp.Clone(); BitmapData bmpSourceDat = bmpSource.LockBits(new Rectangle(0, 0, bmpSource.Width, bmpSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); BitmapData bmpDestinationDat = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); unsafe { byte* pByte = (byte*)bmpDestinationDat.Scan0 + bmp.Width * 4 + 4; byte* pLine0 = (byte*)bmpSourceDat.Scan0; byte* pLine1 = (byte*)bmpSourceDat.Scan0 + bmp.Width * 4; byte* pLine2 = (byte*)bmpSourceDat.Scan0 + bmp.Width * 8; int size = (bmp.Width - 2) * (bmp.Height - 2); for (int i = 0; i < size; i++) { pByte[0] = Norming((((pLine0[0] * ColorMatrix[0, 0]) + (pLine0[4] * ColorMatrix[1, 0]) + (pLine0[8] * ColorMatrix[2, 0]) + (pLine1[0] * ColorMatrix[0, 1]) + (pLine1[4] * ColorMatrix[1, 1]) + (pLine1[8] * ColorMatrix[2, 1]) + (pLine2[0] * ColorMatrix[0, 2]) + (pLine2[4] * ColorMatrix[1, 2]) + (pLine2[8] * ColorMatrix[2, 2])) / Divider) + Offset); pByte[1] = Norming((((pLine0[1] * ColorMatrix[0, 0]) + (pLine0[5] * ColorMatrix[1, 0]) + (pLine0[9] * ColorMatrix[2, 0]) + (pLine1[1] * ColorMatrix[0, 1]) + (pLine1[5] * ColorMatrix[1, 1]) + (pLine1[9] * ColorMatrix[2, 1]) + (pLine2[1] * ColorMatrix[0, 2]) + (pLine2[5] * ColorMatrix[1, 2]) + (pLine2[9] * ColorMatrix[2, 2])) / Divider) + Offset); pByte[2] = Norming((((pLine0[2] * ColorMatrix[0, 0]) + (pLine0[6] * ColorMatrix[1, 0]) + (pLine0[10] * ColorMatrix[2, 0]) + (pLine1[2] * ColorMatrix[0, 1]) + (pLine1[6] * ColorMatrix[1, 1]) + (pLine1[10] * ColorMatrix[2, 1]) + (pLine2[2] * ColorMatrix[0, 2]) + (pLine2[6] * ColorMatrix[1, 2]) + (pLine2[10] * ColorMatrix[2, 2])) / Divider) + Offset); pByte += 4; pLine0 += 4; pLine1 += 4; pLine2 += 4; } } bmp.UnlockBits(bmpDestinationDat); bmpSource.UnlockBits(bmpSourceDat); bmpSource.Dispose(); } } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
42 min ago | 14.56 KB
py_PYTHON-FROM-CLI-RUN-HIDDEN-PYW
2 hours ago | 0.05 KB
Untitled
2 hours ago | 14.19 KB
Untitled
4 hours ago | 13.68 KB
Untitled
6 hours ago | 14.74 KB
Untitled
8 hours ago | 16.88 KB
Warum Systeme scheitern
8 hours ago | 33.75 KB
supahffej
9 hours ago | 7.50 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!