public static FastBitmap Hide(FastBitmap bmp1, FastBitmap bmp2) { FastBitmap bmpSecret = new FastBitmap((Bitmap)bmp2.Bitmap.Clone()); Threshold(bmpSecret, 127); FastBitmap bmp = new FastBitmap(bmp1.Width, bmp1.Height); for (int i = 0; i < bmp1.Width; ++i) { for (int j = 0; j < bmp1.Height; ++j) { int newColor = bmp1[i, j].R & (0xFE); if (i < bmpSecret.Width && j < bmpSecret.Height) { newColor |= bmpSecret[i, j].R == 0 ? 0x00 : 0x01; } bmp[i, j] = Color.FromArgb(newColor, newColor, newColor); } } return(bmp); }
public UPOForm() { InitializeComponent(); ClientSize = new Size(276, 276 + statusStrip1.Height + buttonOK.Height + 10); graphicsPanel.Size = new Size(256, 256); graphicsPanel.Left = 10; graphicsPanel.Top = 10; graphics = graphicsPanel.CreateGraphics(); buttonOK.Top = 276; buttonCancel.Top = 276; upo = new byte[256]; for (int i = 0; i < 256; ++i) { upo[i] = (byte)i; } upoImage = new FastBitmap(256, 256); graphicsImage = upoImage.CreateGraphics(); RedrawImage(); }
private void adaptiveTresholdToolStripMenuItem_Click(object sender, EventArgs e) { valueForm = new ValueForm(3, 21, "Enter the matrix size", false, true); if (valueForm.ShowDialog() == DialogResult.OK) { FastBitmap bmp = ((ImageForm)ActiveMdiChild).Image; int size = valueForm.Value / 2; for (int i = size; i < bmp.Width - size; ++i) { for (int j = size; j < bmp.Height - size; ++j) { int mean = 0; for (int k = i - size; k < i + size + 1; ++k) { for (int l = j - size; l < j + size + 1; ++l) { mean += bmp[k, l].R; } } mean /= (2 * size + 1) * (2 * size + 1); if (bmp[i, j].R >= mean) { bmp[i, j] = Color.FromArgb(255, 255, 255, 255); } else { bmp[i, j] = Color.FromArgb(255, 0, 0, 0); } } } } ((ImageForm)ActiveMdiChild).Refresh(); ((ImageForm)ActiveMdiChild).Changed = true; }
public static FastBitmap Dylatacja(FastBitmap bmp, Spojnosc spojnosc) { int i, j, pam; int[,] dilate = new int[bmp.Width, bmp.Height]; int[,] tab = new int[bmp.Width, bmp.Height]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { tab[x, y] = bmp[x, y].G; } } for (i = 1; i < bmp.Height - 1; i++) { for (j = 1; j < bmp.Width - 1; j++) { pam = tab[j, i]; if (spojnosc == Spojnosc.kwadrat) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j + 1, i + 1]) { pam = tab[j + 1, i + 1]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i + 1]) { pam = tab[j - 1, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j - 1, i - 1]) { pam = tab[j - 1, i - 1]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } if (pam <= tab[j + 1, i - 1]) { pam = tab[j + 1, i - 1]; } } else if (spojnosc == Spojnosc.romb) { if (pam <= tab[j + 1, i]) { pam = tab[j + 1, i]; } if (pam <= tab[j, i + 1]) { pam = tab[j, i + 1]; } if (pam <= tab[j - 1, i]) { pam = tab[j - 1, i]; } if (pam <= tab[j, i - 1]) { pam = tab[j, i - 1]; } } dilate[j, i] = pam; } } for (i = 0; i < bmp.Height; i++) { for (j = 0; j < bmp.Width; j++) { byte color = (byte)dilate[j, i]; bmp[j, i] = Color.FromArgb(255, color, color, color); } } return(bmp); }
public static void EqualizeHistogram(FastBitmap bmp, Histogram hist, EqualizeMethod method) { int R = 0; double hInt = 0.0; double[] left = new double[256]; double[] right = new double[256]; int[] newValue = new int[256]; for (int i = 0; i < 256; ++i) { left[i] = R; hInt += hist.HistogramTable[i]; while (hInt > hist.Average) { hInt -= hist.Average; if (R < 255) { R++; } } right[i] = R; switch (method) { case EqualizeMethod.Averages: newValue[i] = (int)((left[i] + right[i]) / 2.0); break; case EqualizeMethod.Random: newValue[i] = (int)(right[i] - left[i]); break; case EqualizeMethod.Own: newValue[i] = (int)((left[i] + right[i]) / 2.0); break; } } for (int i = 0; i < bmp.Size.Width; ++i) { for (int j = 0; j < bmp.Size.Height; ++j) { Color color = bmp[i, j]; if (left[color.R] == right[color.R]) { bmp[i, j] = Color.FromArgb(color.A, (int)left[color.R], (int)left[color.R], (int)left[color.R]); } else { switch (method) { case EqualizeMethod.Averages: bmp[i, j] = Color.FromArgb(color.A, (int)newValue[color.R], (int)newValue[color.R], (int)newValue[color.R]); break; case EqualizeMethod.Random: Random rnd = new Random(); int value = (int)left[color.R] + rnd.Next(newValue[color.R] + 1); bmp[i, j] = Color.FromArgb(color.A, value, value, value); break; case EqualizeMethod.Neighborhood8: double average = 0; int count = 0; foreach (Point offset in new Point[] { new Point(1, 0), new Point(-1, 0), new Point(0, 1), new Point(0, -1), new Point(1, 1), new Point(-1, -1), new Point(-1, 1), new Point(1, -1) }) { if (i + offset.X >= 0 && i + offset.X < bmp.Width && j + offset.Y >= 0 && j + offset.Y < bmp.Height) { average += bmp[i + offset.X, j + offset.Y].R; ++count; } } average /= count; if (average > right[color.R]) { bmp[i, j] = Color.FromArgb(color.A, (int)right[color.R], (int)right[color.R], (int)right[color.R]); } else if (average < left[color.R]) { bmp[i, j] = Color.FromArgb(color.A, (int)left[color.R], (int)left[color.R], (int)left[color.R]); } else { bmp[i, j] = Color.FromArgb(color.A, (int)average, (int)average, (int)average); } break; case EqualizeMethod.Own: bmp[i, j] = Color.FromArgb(color.A, (int)newValue[color.R], (int)newValue[color.R], (int)newValue[color.R]); break; } } } } }
public static void Thinning(FastBitmap bmp) { int[] dx = { 0, 1, 1, 1, 0, -1, -1, -1 }; int[] dy = { 1, 1, 0, -1, -1, -1, 0, 1 }; bool[,] img = new bool[bmp.Width, bmp.Height]; int W = bmp.Width; int H = bmp.Height; for (int i = 0; i < bmp.Width; ++i) { for (int j = 0; j < bmp.Height; ++j) { img[i, j] = bmp[i, j].B < 128; } } bool pass = false; LinkedList <Point> list; do { pass = !pass; list = new LinkedList <Point>(); for (int x = 1; x < W - 1; ++x) { for (int y = 1; y < H - 1; ++y) { if (img[x, y]) { int cnt = 0; int hm = 0; bool prev = img[x - 1, y + 1]; for (int i = 0; i < 8; ++i) { bool cur = img[x + dx[i], y + dy[i]]; hm += cur ? 1 : 0; if (prev && !cur) { ++cnt; } prev = cur; } if (hm > 1 && hm < 7 && cnt == 1) { if (pass && (!img[x + 1, y] || !img[x, y + 1] || !img[x, y - 1] && !img[x - 1, y])) { list.AddLast(new Point(x, y)); } if (!pass && (!img[x - 1, y] || !img[x, y - 1] || !img[x, y + 1] && !img[x + 1, y])) { list.AddLast(new Point(x, y)); } } } } } foreach (Point p in list) { img[p.X, p.Y] = false; } } while (list.Count != 0); for (int x = 0; x < W; ++x) { for (int y = 0; y < H; ++y) { bmp[x, y] = img[x, y] ? Color.Black : Color.White; } } }
public static FastBitmap Turtle(FastBitmap bitmap) { FastBitmap bmp = new FastBitmap((Bitmap)(bitmap.Bitmap.Clone())); int[,] rtab = new int[bmp.Width, bmp.Height]; int[,] gtab = new int[bmp.Width, bmp.Height]; int[,] btab = new int[bmp.Width, bmp.Height]; int i, j; for (i = 1; i < bmp.Width - 1; i++) { for (j = 1; j < bmp.Height - 1; j++) { rtab[i, j] = bmp[i, j].R; gtab[i, j] = bmp[i, j].G; btab[i, j] = bmp[i, j].B; } } int d = 0; int pami = 0, pamj = 0, ja = 0, ia = 0; int x, y; int[] wynik = new int[bmp.Width * bmp.Height]; int[] droga = new int[bmp.Width * bmp.Height]; for (i = 1; i < bmp.Height - 1; i++) { for (j = 1; j < bmp.Width - 1; j++) { if (rtab[j, i] != 0 || gtab[j, i] != 0 || btab[j, i] != 0) { ja = j; ia = i; pamj = j; pami = i; wynik[bmp.Width * i + j] = 255; goto cont; } } } cont: j = pamj; i = pami - 1; wynik[bmp.Width * i + j] = 255; droga[d] = 1; do { x = j - pamj; y = i - pami; pamj = j; pami = i; d++; if (rtab[j, i] != 0 || gtab[j, i] != 0 || btab[j, i] != 0) { if (x == 0 && y == (-1)) { j--; droga[d] = 2; } if (x == 1 && y == 0) { i--; droga[d] = 1; } if (x == 0 && y == 1) { j++; droga[d] = 0; } if (x == (-1) && y == 0) { i++; droga[d] = 3; } } else { if (x == 0 && y == (-1)) { j++; droga[d] = 0; } if (x == 1 && y == 0) { i++; droga[d] = 3; } if (x == 0 && y == 1) { j--; droga[d] = 2; } if (x == (-1) && y == 0) { i--; droga[d] = 1; } } wynik[bmp.Width * i + j] = 255; }while (j != ja || i != ia); for (i = 0; i < bmp.Height; i++) { for (j = 0; j < bmp.Width; j++) { if (wynik[bmp.Width * i + j] == 255) { rtab[j, i] = 255; gtab[j, i] = 0; btab[j, i] = 0; } } } for (i = 0; i < bmp.Width; i++) { for (j = 0; j < bmp.Height; j++) { bmp[i, j] = Color.FromArgb(rtab[i, j], gtab[i, j], btab[i, j]); } } return(bmp); }
public static FastBitmap Operation(FastBitmap bmp1, FastBitmap bmp2, Operations op) { FastBitmap bmp = new FastBitmap(Math.Max(bmp1.Width, bmp2.Width), Math.Max(bmp1.Height, bmp2.Height)); for (int i = 0; i < bmp.Size.Width; ++i) { for (int j = 0; j < bmp.Size.Height; ++j) { Color c1; Color c2; if (isPixelValid(i, j, bmp1.Width, bmp1.Height)) { c1 = bmp1[i, j]; } else { c1 = Color.Black; } if (isPixelValid(i, j, bmp2.Width, bmp2.Height)) { c2 = bmp2[i, j]; } else { c2 = Color.Black; } switch (op) { case Operations.Add: bmp[i, j] = Color.FromArgb(255, Math.Min(255, c1.R + c2.R), Math.Min(255, c1.G + c2.G), Math.Min(255, c1.B + c2.B)); break; case Operations.Subtract: bmp[i, j] = Color.FromArgb(255, Math.Max(0, c1.R - c2.R), Math.Max(0, c1.G - c2.G), Math.Max(0, c1.B - c2.B)); break; case Operations.Multiply: bmp[i, j] = Color.FromArgb(255, Math.Min(255, c1.R * c2.R), Math.Min(255, c1.G * c2.G), Math.Min(255, c1.B * c2.B)); break; case Operations.Divide: bmp[i, j] = Color.FromArgb(255, c1.R / Math.Max((byte)1, c2.R), c1.G / Math.Max((byte)1, c2.G), c1.B / Math.Max((byte)1, c2.B)); break; case Operations.Difference: bmp[i, j] = Color.FromArgb(255, Math.Abs(c1.R - c2.R), Math.Abs(c1.G - c2.G), Math.Abs(c1.B - c2.B)); break; case Operations.AND: bmp[i, j] = Color.FromArgb(255, Math.Max(0, Math.Min(255, c1.R & c2.R)), Math.Max(0, Math.Min(255, c1.G & c2.G)), Math.Max(0, Math.Min(255, c1.B & c2.B))); break; case Operations.OR: bmp[i, j] = Color.FromArgb(255, Math.Max(0, Math.Min(255, c1.R | c2.R)), Math.Max(0, Math.Min(255, c1.G | c2.G)), Math.Max(0, Math.Min(255, c1.B | c2.B))); break; case Operations.XOR: bmp[i, j] = Color.FromArgb(255, Math.Max(0, Math.Min(255, c1.R ^ c2.R)), Math.Max(0, Math.Min(255, c1.G ^ c2.G)), Math.Max(0, Math.Min(255, c1.B ^ c2.B))); break; } } } return(bmp); }
private void compressREADToolStripMenuItem_Click(object sender, EventArgs e) { FastBitmap bmp = ((ImageForm)ActiveMdiChild).Image; const int PIXELLEN = 1; //4 bajty - 32 bity const int WORDLEN = 1; //4 bajty - 32 bity int newColor = 255, lastColor = 0; int repeatCount = 0; int total = 0; int width = bmp.Width; int height = bmp.Height; int fld = width * height; int before, after; before = fld * PIXELLEN; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color color = bmp[x, y]; newColor = (color.R + color.G + color.B) / 3; if (newColor == lastColor) { repeatCount++; } else { if (repeatCount > 0) { total += WORDLEN + PIXELLEN; repeatCount = 0; } total += WORDLEN + PIXELLEN; } color = bmp[x, y]; lastColor = (color.R + color.G + color.B) / 3; } } if (repeatCount > 0) { total += WORDLEN + PIXELLEN; repeatCount = 0; } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color color = bmp[x, y]; newColor = (color.R + color.G + color.B) / 3; if (newColor == lastColor) { repeatCount++; } else { if (repeatCount > 0) { total += WORDLEN + PIXELLEN; repeatCount = 0; } total += WORDLEN + PIXELLEN; } color = bmp[x, y]; lastColor = (color.R + color.G + color.B) / 3; } } if (repeatCount > 0) { total += WORDLEN + PIXELLEN; repeatCount = 0; } after = total / 2; MessageBox.Show("Original size: " + before + "\nNew size: " + after + "\nCompression level: " + (float)before / after, "Compression results"); }
private void compressBlockToolStripMenuItem_Click(object sender, EventArgs e) { FastBitmap bitmap = ((ImageForm)ActiveMdiChild).Image; valueForm = new ValueForm(1, 100, "Enter the block size"); if (valueForm.ShowDialog() == DialogResult.OK) { int size = valueForm.Value; int color = 0; float avg; float avgUP; float avgDOWN; int countUP; int countDOWN; int countAVG; int sizeAfter = 0; for (int y = 0; y < bitmap.Size.Height; y += size) { for (int x = 0; x < bitmap.Size.Width; x += size) { avg = 0; countAVG = 0; for (int yy = 0; yy < size; ++yy) { for (int xx = 0; xx < size; ++xx) { if (x + xx < bitmap.Size.Width && y + yy < bitmap.Size.Height) { color = bitmap[x + xx, y + yy].R; avg += color; ++countAVG; } } } avg /= countAVG; avg = (int)avg; avgUP = 0; avgDOWN = 0; countDOWN = 0; countUP = 0; for (int yy = 0; yy < size; ++yy) { for (int xx = 0; xx < size; ++xx) { if (x + xx < bitmap.Size.Width && y + yy < bitmap.Size.Height) { color = bitmap[x + xx, y + yy].R; if (color >= avg) { avgUP += color; ++countUP; } else { avgDOWN += color; ++countDOWN; } } } } avgUP /= countUP; avgDOWN /= countDOWN; avgUP = (int)avgUP; avgDOWN = (int)avgDOWN; for (int yy = 0; yy < size; ++yy) { for (int xx = 0; xx < size; ++xx) { if (x + xx < bitmap.Size.Width && y + yy < bitmap.Size.Height) { color = bitmap[x + xx, y + yy].R; if (color >= avg) { bitmap[x + xx, y + yy] = Color.FromArgb(255, (int)avgUP, (int)avgUP, (int)avgUP); } else { bitmap[x + xx, y + yy] = Color.FromArgb(255, (int)avgDOWN, (int)avgDOWN, (int)avgDOWN); } } } } sizeAfter += size * size + 16; } } int sizeBefore = bitmap.Size.Width * bitmap.Size.Height * 8; ((ImageForm)ActiveMdiChild).Refresh(); ((ImageForm)ActiveMdiChild).Changed = true; MessageBox.Show("Original size: " + sizeBefore + "\nNew size: " + sizeAfter + "\nCompression level: " + (float)sizeBefore / (float)sizeAfter, "Compression results"); } }
private void compressRLEToolStripMenuItem_Click(object sender, EventArgs e) { FastBitmap bitmap = ((ImageForm)ActiveMdiChild).Image; int oSize = ((ImageForm)ActiveMdiChild).Image.Width * ((ImageForm)ActiveMdiChild).Image.Height * 24 / 8; int nSize = oSize * 2; int licznik = 0; int przed; int po = 0; float stopien; int pam; int pam2 = -128; przed = bitmap.Width * bitmap.Height; for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { if (y >= bitmap.Height) { break; } pam = (int)((0.3 * (float)bitmap[x, y].R) + (0.59 * (float)bitmap[x, y].G) + (0.11 * (float)bitmap[x, y].B)); if (pam == pam2) { licznik = 0; while (((pam = (int)((0.3 * (float)bitmap[x, y].R) + (0.59 * (float)bitmap[x, y].G) + (0.11 * (float)bitmap[x, y].B))) == pam2) && (licznik < 127)) { licznik++; x++; if (x >= bitmap.Width - 1) { x = 0; y++; if (y >= bitmap.Height) { break; } } pam2 = pam; } po += 2; } else { licznik = 0; while (((pam = (int)((0.3 * (float)bitmap[x, y].R) + (0.59 * (float)bitmap[x, y].G) + (0.11 * (float)bitmap[x, y].B))) != pam2) && (licznik < 127)) { licznik++; x++; if (x >= bitmap.Width - 1) { x = 0; y++; if (y >= bitmap.Height) { break; } } pam2 = pam; } po = po + licznik + 1; } pam2 = pam; } } stopien = (float)przed / (float)po; oSize = przed; nSize = po; MessageBox.Show("Original size: " + oSize + "\nNew size: " + nSize + "\nCompression level: " + (float)oSize / nSize, "Compression results"); }