/// <summary> /// Constructor for the Image class, creates a image object /// </summary> /// <param name="metadata"></param> /// <param name="maxRange"></param> /// <param name="data"></param> public Image(string metadata, int maxRange, Pixel[,] data) { if (maxRange < 0) { throw new ArgumentException("Max Range cannot be negative"); } Metadata = metadata; MaxRange = maxRange; _data = new Pixel[data.GetLength(0), data.GetLength(1)]; for (int y = 0; y < data.GetLength(0); y++) { for (int x = 0; x < data.GetLength(1); x++) { if (data[y, x].Green > MaxRange || data[y, x].Blue > MaxRange || data[y, x].Red > MaxRange) { throw new ArgumentException("Pixel Values cannot be bigger than the MaxRange"); } else { _data[y, x] = data[y, x]; } } } }
public static int[,] CalculateEnergy(Pixel[,] ImageMatrix) { int Height = ImageMatrix.GetLength(0); int Width = ImageMatrix.GetLength(1); int[,] Energy = new int[Height, Width]; int dx, dy; for (int i = 0; i < Height - 1; i++) { for (int j = 0; j < Width - 1; j++) { dx = Math.Abs(ImageMatrix[i, j + 1].R - ImageMatrix[i, j].R); dy = Math.Abs(ImageMatrix[i + 1, j].R - ImageMatrix[i, j].R); dx += Math.Abs(ImageMatrix[i, j + 1].G - ImageMatrix[i, j].G); dy += Math.Abs(ImageMatrix[i + 1, j].G - ImageMatrix[i, j].G); dx += Math.Abs(ImageMatrix[i, j + 1].B - ImageMatrix[i, j].B); dy += Math.Abs(ImageMatrix[i + 1, j].B - ImageMatrix[i, j].B); Energy[i, j] = dx + dy; } //set energy value of last column same as the one of column before last Energy[i, Width - 1] = Energy[i, Width - 2]; } //set energy value of last row same as the one of row before last for (int j = 0; j < Width; j++) { Energy[Height - 1, j] = Energy[Height - 2, j]; } return(Energy); }
public static void DisplayImage(Pixel[,] ImageMatrix, PictureBox PicBox) { int Height = ImageMatrix.GetLength(0); int Width = ImageMatrix.GetLength(1); Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); unsafe { BitmapData bmd = ImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, ImageBMP.PixelFormat); int nWidth = 0; nWidth = Width * 3; int nOffset = bmd.Stride - nWidth; byte *p = (byte *)bmd.Scan0; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { p[0] = ImageMatrix[i, j].R; p[1] = ImageMatrix[i, j].G; p[2] = ImageMatrix[i, j].B; p += 3; } p += nOffset; } ImageBMP.UnlockBits(bmd); } PicBox.Image = ImageBMP; }
public string WriteToXPM() { // Use string builder since strings are immutable in C# and are really slow to concatenate with + operator in a tight loop string xpm = @"/* XPM */ static char* sco100[] = { /* width height num_colors chars_per_pixel */""" + m_width + " " + m_height + @" 2 1"", /*colors*/ ""- c #000000"", ""@ c #ff0000"" /*pixels*/"""; StringBuilder sb = new StringBuilder(xpm); for (int i = 0; i < m_pixelArray.GetLength(1); i++) { sb.Append(@""""); for (int j = 0; j < m_pixelArray.GetLength(0); j++) { if (m_pixelArray[j, i].draw) { sb.Append("@"); } else { sb.Append("-"); } } sb.Append(@""","); } //Remove that extra comma, even though it doesn't seem to be a problem sb.Remove(sb.Length - 1, 1); sb.Append("}"); return(sb.ToString()); }
private void UpdateSize() { var oldRows = _pixels.GetLength(0); var oldColumns = _pixels.GetLength(1); var newPixels = new Pixel[_rows, _columns]; for (int i = 0; i < _rows; ++i) { for (int j = 0; j < _columns; ++j) { if (i < oldRows && j < oldColumns) { newPixels[i, j] = _pixels[i, j]; } else { newPixels[i, j] = new Pixel(); } } } _pixels = newPixels; Pixels = _pixels.Cast <Pixel>().ToArray(); }
public override Pixel[,] GetRenderBuffer() { for (int unit = 0; unit < Window_Component.InteractiveUnitsCollection.Count; unit++) { Button tempUnit = Window_Component.InteractiveUnitsCollection[unit]; Pixel[,] tempRenderBuffer = tempUnit.GetRenderBuffer(); for (int j = 0; j < tempRenderBuffer.GetLength(1); j++) { for (int i = 0; i < tempRenderBuffer.GetLength(0); i++) { Window_Component.RenderBuffer[tempUnit.Anchor.X + i, tempUnit.Anchor.Y + j] = tempRenderBuffer[i, j]; } } } /* * for (int j = 0; j < Window_Component.Height; j++) * { * for (int i = 0; i < Window_Component.Width; i++) * { * VSystem.Layers[ProgramID][i + Window_Component.Anchor.X, j + Window_Component * .Anchor.Y] = Window_Component.RenderBuffer[i, j]; * } * } */ return(Window_Component.RenderBuffer); }
/// <summary> /// multiplie case par case deux matrices et fait la somme retourne un tableau d'entier /// </summary> /// <param name="parcelle"></param> /// <param name="noyau"></param> /// <returns></returns> static double[] applicationNoyau(Pixel[,] parcelle, double[,] noyau) { if (parcelle != null && noyau != null && parcelle.Length != 0 && noyau.Length != 0) { ///il faut retourner une valeur par couleur donc il y a 3 cases dans le tableau de retour double[] retour = new double[3]; for (int i = 0; i < parcelle.GetLength(0); i++) { for (int j = 0; j < parcelle.GetLength(1); j++) { retour[0] = (double)(retour[0] + parcelle[i, j].R * noyau[i, j]); retour[1] = (double)(retour[1] + parcelle[i, j].G * noyau[i, j]); retour[2] = (double)(retour[2] + parcelle[i, j].B * noyau[i, j]); } } for (int i = 0; i < retour.Length; i++)///on majore par 255 et minore par 0 pour éviter que ça dépasse les bornes du byte { if (retour[i] > 255) { retour[i] = 255; } if (retour[i] < 0) { retour[i] = 0; } } return(retour); } else { return(null); } }
/// <summary> /// /// </summary> /// <param name="screenArray">What to draw</param> /// <param name="drawTo">where to draw it to</param> public static unsafe void DrawToScreen(Pixel[,] screenArray, FormMain drawTo) { //simple error checking to keep program from crashing if array is oversized if (screenArray.GetLength(0) != bitmap.Width || screenArray.GetLength(0) != bitmap.Width) { throw new System.ArgumentException("Parameter array is not the same size as the screen", "original"); } //lock bits to draw screen buffer System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rectScreen, System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); //Pixel array to bitmap UInt32 *pixel = (UInt32 *)bmpData.Scan0.ToPointer(); for (uint x = 0; x < bitmap.Width; x++) { for (uint y = 0; y < bitmap.Height; y++) { pixel[y * bitmap.Width + x] = 0xFF000000 | (((UInt32)screenArray[x, y].R) << 16 | (((UInt32)screenArray[x, y].G) << 8)) | (UInt32)screenArray[x, y].B; } } //unlock bits then draw bitmap.UnlockBits(bmpData); drawTo.DrawImage(bitmap); }
/* * Return list of fields indexed from 0. * Fields grab containg Pixels from patch. */ public override List <Field> GetFields(Pixel[,] patch) { int pixWidth = patch.GetLength(0) / width; int pixHeight = patch.GetLength(1) / height; List <Field> fields = new List <Field>(); int fieldIndex = 0; // select Field from matrix for (int fHeigth = 0; fHeigth < height; fHeigth++) { for (int fWidth = 0; fWidth < width; fWidth++) { var field = new Field(); field.Index = fieldIndex++; //Select Pixels from patch; int i = pixHeight * fHeigth; for (int pHeight = i; pHeight < i + pixHeight; pHeight++) { int e = pixWidth * fWidth; for (int pWidth = e; pWidth < e + pixWidth; pWidth++) { field.Pixels.Add(patch[pWidth, pHeight]); } } fields.Add(field); } } return(fields); }
/// <summary> /// Affiche l'image de l'instance /// </summary> /// <param name="color">Couleur des pixels à afficher (Red / Blue / Green / All)</param> public void AfficherImage(String color) { for (int i = 0; i < imageComplete.GetLength(0); i++) { Console.Write("\n"); for (int j = 0; j < imageComplete.GetLength(1); j++) { color = color.ToLower(); switch (color) { case "red": Console.Write(imageComplete[i, j].Red + " "); break; case "green": Console.Write(imageComplete[i, j].Green + " "); break; case "blue": Console.Write(imageComplete[i, j].Blue + " "); break; default: Console.Write(imageComplete[i, j].Red + " " + imageComplete[i, j].Green + " " + imageComplete[i, j].Blue + ";"); break; } } } Console.ReadKey(); }
public static void NoirEtBlanc(MyImage image, int valeur = 128) { Pixel[,] matriceBGR = image.MatriceBGR; int moyenne = 0; for (int i = 0; i < matriceBGR.GetLength(0); i++) { for (int j = 0; j < matriceBGR.GetLength(1); j++) { moyenne = (matriceBGR[i, j].R + matriceBGR[i, j].V + matriceBGR[i, j].B) / 3; if (moyenne < valeur) { moyenne = 0; } else { moyenne = 255; } matriceBGR[i, j].R = moyenne; matriceBGR[i, j].V = moyenne; matriceBGR[i, j].B = moyenne; } } image.MatriceBGR = matriceBGR; }
/// <summary> /// Creates a square subarray of a 2D array from the middlepoint [a,b] and size neighbours*2+1 /// </summary> /// <param name="image">the array containing the new array</param> /// <param name="a">middle point line index</param> /// <param name="b">middle point columns index</param> /// <param name="neighbours">the number of neighbours from the middle point</param> /// <returns>the subarray</returns> private static Pixel[,] Subarray(Pixel[,] image, int a, int b, int neighbours) { Pixel[,] result = new Pixel[2 * neighbours + 1, 2 * neighbours + 1]; for (int i = a - neighbours; i < a + neighbours + 1; i++) { for (int j = b - neighbours; j < b + neighbours + 1; j++) { int iGrid = i; int jGrid = j; if (iGrid < 0) { iGrid *= -1; } if (jGrid < 0) { jGrid *= -1; } if (iGrid >= image.GetLength(0)) { iGrid = 2 * image.GetLength(0) - iGrid - 1; } if (jGrid >= image.GetLength(1)) { jGrid = 2 * image.GetLength(1) - jGrid - 1; } result[i + neighbours - a, j + neighbours - b] = new Pixel(image[iGrid, jGrid]); } } return(result); }
private Sides CheckPixels(Pixel[,] image, int offset) { for (int y = 0; y < image.GetLength(1); y++) { for (int x = 0; x < image.GetLength(0); x++) { var pixels = image[x, y]; if (pixels.CheckPixel(_imageDown, offset)) { return(Sides.Down); } if (pixels.CheckPixel(_imageUp, offset)) { return(Sides.Up); } if (pixels.CheckPixel(_imageLeft, offset)) { return(Sides.Left); } if (pixels.CheckPixel(_imageRight, offset)) { return(Sides.Right); } } } return(Sides.Null); }
/// <summary> /// Returns the closest pixel with manhattan distance /// </summary> /// <param name="image"></param> /// <param name="line">line index of the pixel</param> /// <param name="column">column index of the pixel</param> /// <param name="dist">do not enter numbre (for recursion)</param> /// <returns>the closest Pixel of the pixel at index [line,column]</returns> private static Pixel ClosePixel(Pixel[,] image, int line, int column, int dist = 1) { if (dist >= image.GetLength(0) && dist >= image.GetLength(1)) { return(Pixel.Black); } int distance = -1; Pixel result = new Pixel(0); for (int i = line - dist; i < line + dist + 1; i++) { for (int j = column - dist; j < column + dist + 1; j++) { int manhattanDistance = Math.Abs(i - line) + Math.Abs(j - column); if (i != j && i >= 0 && i < image.GetLength(0) && j >= 0 && j < image.GetLength(1) && image[i, j] != null && (distance == -1 || manhattanDistance < distance)) { result.SetValueTo(image[i, j]); } if (distance == 1) { return(result); } } } if (distance == -1) { return(ClosePixel(image, line, column, dist + 1)); } else { return(result); } }
public override List <Field> GetFields(Pixel[,] patch) { var fields = new List <Field>(); int cols = patch.GetLength(0); int rows = patch.GetLength(1); var colPixelZones = cols % 2 == 0 ? cols / 2 : (cols / 2) + 1; var rowPixelZones = rows % 2 == 0 ? rows / 2 : (rows / 2) + 1; var zones = colPixelZones < rowPixelZones ? colPixelZones : rowPixelZones; // slect zones for (var zone = 0; zone < zones; zone++) { Field field = new Field(); field.Index = zone; for (int col = zone; col < cols - zone; col++) { for (int row = zone; row < rows - zone; row++) { if (col == zone || row == zone) { field.Pixels.Add(patch[col, row]); field.Pixels.Add(patch[cols - col - 1, rows - row - 1]); // -1 : convert collon count to array index } } } fields.Add(field); } return(fields); }
public Frame(Pixel[,] pixels) { ResolutionX = pixels.GetLength(0); ResolutionY = pixels.GetLength(1); Pixels = pixels; }
Bitmap zzz2(Pixel[,] cuted_data) { Bitmap b = new Bitmap(cuted_data.GetLength(0), cuted_data.GetLength(1) / 3); BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, (b.Height)), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; // 扫描的宽度 unsafe { byte *p = (byte *)bmData.Scan0.ToPointer(); // 获取图像首地址 byte *p2 = null; int nOffset = stride - b.Width * 3; // 实际宽度与系统宽度的距离 for (int y = 0; y < (b.Height); ++y) { for (int x = 0; x < b.Width; ++x) { p[2] = (byte)rgbchange(cuted_data, x, y); p[1] = (byte)rgbchange(cuted_data, x, b.Height + y); p[3] = (byte)rgbchange(cuted_data, x, b.Height * 2 + y); p += 3; // 跳过3个字节处理下个像素点 } p += nOffset; // 加上间隔 } } b.UnlockBits(bmData); // 解锁 return(b); }
public bool Construct(uint width, uint height, uint cellX, uint cellY, string title, Color clearColor, bool fullscreen) { Title = title; this.fullscreen = fullscreen; resolution = new Vector2i((int)width, (int)height); this.cellX = (int)cellX; this.cellY = (int)cellY; backBuffer = new Pixel[width / cellX, height / cellY]; backBuffer.Populate(new Pixel().Construct(clearColor, cellX, cellY)); this.clearColor = clearColor; input = new InputHandler(); graphics = new GrapeGraphics(ref backBuffer); screen = new ScreenBuffer(backBuffer.GetLength(0), backBuffer.GetLength(1), (int)cellX, (int)cellY, ref backBuffer); vecGraph = new VertexGraphics(ref screen.vertexArr, backBuffer.GetLength(0), backBuffer.GetLength(1)); thread = new Thread(new ThreadStart(Start)); thread.Start(); return(true); }
private async Task <IEnumerable <Point> > ApplyFloodFillAsync( Pixel[,] bitmap, Point startPoint, Func <Pixel, bool> boundaryFunc) { var fastBitmap = (Pixel[, ])bitmap.Clone(); return(await Task.Run(delegate { Stack <Point> pixels = new Stack <Point>(); pixels.Push(startPoint); var marked = new Dictionary <Point, byte>(); while (pixels.Count > 0) { Point a = pixels.Pop(); if (a.X < bitmap.GetLength(0) && a.X > -1 && a.Y < bitmap.GetLength(1) && a.Y > -1) { var pixel = fastBitmap[a.X, a.Y]; if (boundaryFunc(pixel) && !marked.ContainsKey(a)) { marked.Add(a, pixel.G); pixels.Push(new Point(a.X - 1, a.Y)); pixels.Push(new Point(a.X + 1, a.Y)); pixels.Push(new Point(a.X, a.Y - 1)); pixels.Push(new Point(a.X, a.Y + 1)); } } } return marked.Keys; })); }
public static void Rotation(MyImage image, int angle) { Pixel[,] matriceBGR = image.MatriceBGR; double longueur = Math.Ceiling(Math.Sin(angle) * matriceBGR.GetLength(0) + Math.Cos(angle) * matriceBGR.GetLength(1)); double largeur = Math.Ceiling(Math.Cos(angle) * matriceBGR.GetLength(0) + Math.Sin(angle) * matriceBGR.GetLength(1)); Pixel[,] matriceBGRRotation = new Pixel[Convert.ToInt32(Math.Abs(longueur)), Convert.ToInt32(Math.Abs(largeur))]; for (int i = 0; i < matriceBGRRotation.GetLength(0); i++) { for (int j = 0; j < matriceBGRRotation.GetLength(1); j++) { matriceBGRRotation[i, j] = new Pixel(0, 0, 0, true); } } for (int i = 0; i < matriceBGR.GetLength(0); i++) { for (int j = 0; j < matriceBGR.GetLength(1); j++) { if (matriceBGR[i, j].PixelNoir != true) { matriceBGRRotation[Convert.ToInt32(Math.Abs(Math.Ceiling(Math.Sin(angle) * i + Math.Cos(angle) * j))), Convert.ToInt32(Math.Abs(Math.Ceiling(Math.Cos(angle) * i + Math.Sin(angle) * j)))] = matriceBGR[i, j]; } } } image.MatriceBGR = matriceBGRRotation; }
/// <summary> /// Add pixel matrix to a byte array, by converting each pixel into a byte array first /// </summary> /// <param name="array">the original array</param> /// <param name="mat">the pixel matrix to be added to the array</param> /// <param name="offset">the offeset it should be added to</param> /// <param name="padding">the padding value of the current picture</param> public static void ArrayAddPixels(byte[] array, Pixel[,] mat, int offset, int padding) { int count = 0; for (int i = mat.GetLength(0) - 1; i >= 0; i--) //for each line (descending line to reorder properly) { for (int j = 0; j < mat.GetLength(1); j++) { ArrayAddBytes(array, mat[i, j].toByteArray(), (offset + count)); //This could be prettier, change count with i*?+j*? count += 3; //Add Padding } if (padding != 0) { for (int k = 0; k < padding; k++) { //Console.WriteLine("Added a paddin byte"); ArrayAddBytes(array, new byte[] { 0 }, (offset + count)); count += 1; } } } }
public byte[] GenerateCode(byte imgId, byte animType, byte speed, byte loops, byte nextanimImg) { Pixel[,] array = this.GetPixelArray(0); var imgCode = new List <byte> { imgId, animType, speed, loops, nextanimImg }; Boolean init = true; byte prevValue = 0; byte sameValIncrement = 0; for (int Xcount = 0; Xcount < array.GetLength(0); Xcount++) { for (int Ycount = 0; Ycount < array.GetLength(1); Ycount++) { if (init) { prevValue = array[Xcount, Ycount].GetPixelValue(); imgCode.Add(prevValue); init = false; } else { if (array[Xcount, Ycount].GetPixelValue() == prevValue) { if (sameValIncrement < 200) { sameValIncrement++; } else { imgCode.Add(sameValIncrement); sameValIncrement = 0; } } else { prevValue = array[Xcount, Ycount].GetPixelValue(); if (sameValIncrement == 0) { imgCode.Add(prevValue); } else { imgCode.Add(sameValIncrement); imgCode.Add(prevValue); sameValIncrement = 0; } } } } } return(imgCode.ToArray()); }
/// <summary> /// sert à modifier la largeur et la hauteur d'un fichier bmp après y avoir fait des modifications /// </summary> /// <param name="picture"></param> correspond à la nouvelle matrice de pixel public void ChangerProprietees(Pixel[,] picture) { taille -= image.GetLength(0) * image.GetLength(1); largeur = picture.GetLength(1); hauteur = picture.GetLength(0); taille += largeur * hauteur * 9; image = picture; }
public Image(Pixel[,] pixels) { if (pixels.GetLength(0) != Height || pixels.GetLength(1) != Width) { throw new ArgumentException(nameof(pixels)); } _pixels = pixels; }
/// <summary> /// Méthodes qui permet d'enregistrer les différentes images /// </summary> /// <param name="newimage"></param> public void Enregistrement(Pixel[,] newimage) { int newHauteur = newimage.GetLength(1); int newLargeur = newimage.GetLength(0); int newTaille = newHauteur * newLargeur * 3 + 54; byte[] newfile = new byte[newTaille]; for (int j = 0; j < 54; j++) { newfile[j] = 0; } int i; newfile[0] = Convert.ToByte(66); ///on considère uniquement le bitmap classique ici newfile[1] = Convert.ToByte(77); byte[] tabsize = Convert_Int_To_Endian(newTaille); for (i = 2; i < 6; i++) { newfile[i] = Convert.ToByte(tabsize[i - 2]); } byte[] taboffsetsize = Convert_Int_To_Endian(this.tailleOffset); for (i = 10; i < 14; i++) { newfile[i] = Convert.ToByte(taboffsetsize[i - 10]); } newfile[14] = 40; byte[] tablength = Convert_Int_To_Endian(newLargeur); for (i = 18; i < 22; i++) { newfile[i] = Convert.ToByte(tablength[i - 18]); } byte[] tabheight = Convert_Int_To_Endian(newHauteur); for (i = 22; i < 26; i++) { newfile[i] = Convert.ToByte(tabheight[i - 22]); } newfile[26] = 1; byte[] tabbitpercolor = Convert_Int_To_Endian(this.nbBitCouleur); for (i = 28; i < 30; i++) { newfile[i] = Convert.ToByte(tabbitpercolor[i - 28]); } int index = 54; for (int k = 0; k < newHauteur; k++) { for (int j = 0; j < newLargeur; j++) { newfile[index] = Convert.ToByte(newimage[j, k].Rouge); newfile[index + 1] = Convert.ToByte(newimage[j, k].Vert); newfile[index + 2] = Convert.ToByte(newimage[j, k].Bleu); index += 3; } } File.WriteAllBytes("newimage.bmp", newfile); }
public void Proprietes(Pixel[,] image) { int temp = Conversion_Byte_to_Int(taillefich); temp -= Image.GetLength(0) * image.GetLength(1); Largeur = image.GetLength(0); Hauteur = image.GetLength(1); temp += Largeur * Hauteur * 9; Image = image; }
void MirrorX(Pixel[,] pixels) { for (int i = 0; i < pixels.GetLength(0) / 2; i++) { for (int j = 0; j < pixels.GetLength(1); j++) { pixels[i, j] = pixels[pixels.GetLength(0) - i - 1, j]; } } }
/// <summary> /// Fills the screen out with blank pixels /// </summary> public void ClearScreen() { for (var i = 0; i < _screen.GetLength(0); i++) // Go through each item in the screen { for (var j = 0; j < _screen.GetLength(1); j++) { _screen[i, j] = new Pixel(ConsoleColor.Black); // Place a blank pixel at the place } } }
public void DeletePixels(int left, int top, Pixel[,] pix) { for (int i = 0; i < pix.GetLength(0) && top + i < Pixels.GetLength(0); i++) { for (int j = 0; j < pix.GetLength(1) && left + j < Pixels.GetLength(1); j++) { Pixels[i + top, j + left] = Pixels[i + top, j + left].Equals(pix[i, j]) || pix[i, j] == null ? new Pixel(PixelTypes.Empty) : pix[i, j]; } } }
public void CombineWith(int left, int top, Pixel[,] pix) { for (int i = 0; i < pix.GetLength(0) && top + i < Pixels.GetLength(0); i++) { for (int j = 0; j < pix.GetLength(1) && left + j < Pixels.GetLength(1); j++) { Pixels[i + top, j + left] = pix[i, j] ?? Pixels[i + top, j + left]; } } }