public override void WriteToBitmap(Canvas canvas) { PrepareWriteBitmap(canvas); /* int[,] array1 = new int[a_bmp.Width,a_bmp.Height]; int[,] array2 = new int[a_bmp.Width,a_bmp.Height]; int[,] arrayTmp; for(int x=0; x<a_bmp.Width; x++) { for(int y=0; y<a_bmp.Height; y++) array2[x,y] = (int)(255*(0.5*GetRandomValue(x, y)+0.5)); } int m_nNumIterations = 1; for (int i=0; i<m_nNumIterations; i++) { for(int x=1; x<a_bmp.Width-1; x++) { for(int y=1; y<a_bmp.Height-1; y++) { array1[x,y] = ((array2[x-1, y-1]+array2[x+1, y-1]+ array2[x-1, y+1]+array2[x+1, y+1]+ array2[x-1, y] +array2[x+1, y]+ array2[x, y-1] +array2[x, y+1]+ array2[x, y]) / 9)+1; } arrayTmp = (int[,])array2.Clone(); array2 = (int[,])array1.Clone(); array1 = (int[,])arrayTmp.Clone(); } } for(int x=0; x<a_bmp.Width; x++) { for(int y=0; y<a_bmp.Height; y++) a_bmp.SetPixel(x,y, Color.FromArgb(array2[x,y],0,0)); } return; */ for (int x = canvas.Width - 1; x >= 0; x--) { for (int y = canvas.Height - 1; y >= 0; y--) { float xValue = (x - canvas.Width / 2) / (float)canvas.Width; float yValue = (y - canvas.Height / 2) / (float)canvas.Height; double distValue = Math.Sqrt(xValue * xValue + yValue * yValue); double d = distValue + m_fTurbulence * GetNoiseValue(x, y, m_nOctaves); float total = (float)Math.Sin(2 * m_fNumCircles * d * 3.14159); total = total*0.5f + 0.5f; int fin = (int)(total*255); Color clr = (Color)m_aColorTable[fin]; canvas.SetPixel(x, y, clr); } } }
public override void WriteToBitmap(Canvas canvas) { Random rnd = new Random(); this.PrepareWriteBitmap(canvas); int[,] array1 = new int[canvas.Width, canvas.Height]; int[,] array2 = new int[canvas.Width, canvas.Height]; for (int x = 0; x < canvas.Width; x++) { for (int y = 0; y < canvas.Height; y++) { array1[x,y] = rnd.Next(255); } } double m_dRate = 0.1; for (int nStep = 0; nStep < 50; nStep++) { //' Run the cellular automata step: for (int x = 0; x < canvas.Width; x++) { for (int y = 0; y < canvas.Height; y++) { double dTot = 0; for(int iX=-1; iX<=1; iX++) { int i = x + iX; int j = 0; if (i < 0) i = canvas.Width - 1; else if (i >= canvas.Width) i = 0; for(int iY=0; iY<=1; iY++) { j = y + iY; if (j < 0) j = canvas.Height - 1; else if (j >= canvas.Height) j = 0; dTot+= array1[i,j]; } dTot/= 9; dTot+= m_dRate; dTot = dTot - (int)dTot; array2[i, j] = (int)(dTot * 255.0); } } } int[,] arrayTmp; arrayTmp = array2; array2= array1; array1 = arrayTmp; } for (int x = canvas.Width - 1; x >= 0; x--) { for (int y = canvas.Height - 1; y >= 0; y--) { Color clr = (Color)m_aColorTable[array2[x,y]]; canvas.SetPixel(x, y, clr); } } }
public static Canvas Convolve(Canvas canvas, float[,] kernel) { //Bitmap bmp = ((Canvas)canvas).ToBitmap(); Canvas canvasDst = canvas.CreateSimilar(); //Bitmap bmpDst = new Bitmap(canvas.Width, canvas.Height, bmp.PixelFormat); //Canvas canvasDst = Canvas.Create(bmpDst); canvasDst.Locked = true; canvas.Locked = true; EPoint pntKernelSize = new EPoint(kernel.GetLength(0), kernel.GetLength(1)); int nBpp = canvas.BitsPerPixel / 8; //TODO: what to do with palettes in D3D..? //if (nBpp == 1) // bmpDst.Palette = bmp.Palette; EPoint canvasSize = new EPoint(canvas.Width, canvas.Height); float[] vals = new float[nBpp]; for (int y = pntKernelSize.Y / 2; y < canvasSize.Y - pntKernelSize.Y / 2; y++) { for (int x = pntKernelSize.X / 2; x < canvasSize.X - pntKernelSize.X / 2; x++) { for (int channel = 0; channel < nBpp; channel++) vals[channel] = 0; for (int yKernel = 0; yKernel < pntKernelSize.Y; yKernel++) { for (int xKernel = 0; xKernel < pntKernelSize.X; xKernel++) { int clr = canvas.GetPixelInt( x + xKernel - pntKernelSize.X / 2, y + yKernel - pntKernelSize.Y / 2); for (int channel = 0; channel < nBpp; channel++) { vals[channel] += kernel[xKernel, yKernel] * (float)(clr & 0xff); clr >>= 8; } } } if (nBpp == 1) canvasDst.SetPixelInt(x, y, vals[0] > 255 ? 255 : (int)vals[0]); else if (nBpp == 3) canvasDst.SetPixel(x, y, Color.FromArgb( vals[2] > 255 ? 255 : (int)vals[2], vals[1] > 255 ? 255 : (int)vals[1], vals[0] > 255 ? 255 : (int)vals[0])); } } canvas.Locked = false; canvasDst.Locked = false; return canvasDst; }
public override void WriteToBitmap(Canvas canvas) { this.PrepareWriteBitmap(canvas); Random rnd = new Random(); double dMasterWL = 0.001/m_fFreq; double dXWLFact = rnd.NextDouble()*2+2; double dXYWLFact = rnd.NextDouble()*2+2; double dYWLFact = rnd.NextDouble()*2+2; double[] aOffsetsPerOctave = new double[3*m_nOctaves]; for (int nOctave = 0; nOctave < m_nOctaves; nOctave++) { for (int i = 0; i < 3; i++) aOffsetsPerOctave[nOctave*3+i] = rnd.NextDouble()*3.1; } double dMax = -1000; double dMin = 1000; double[,] array = new double[canvas.Width, canvas.Height]; for (int x = 0; x < canvas.Width; x++) { for (int y = 0; y < canvas.Height; y++) { double dAmplitude = 1.0; double dTotal = 0; for (int nOctave = 1; nOctave <= m_nOctaves; nOctave++) { double z1 = Math.Sin(dXWLFact*x*nOctave*dMasterWL +aOffsetsPerOctave[(nOctave-1)*3+0]); double z2 = Math.Sin((dXYWLFact*x*nOctave+y*nOctave)*dMasterWL +aOffsetsPerOctave[(nOctave-1)*3+1]); double z3 = Math.Sin(dYWLFact*y*nOctave*dMasterWL+aOffsetsPerOctave[(nOctave-1)*3+2]); dTotal+=dAmplitude*Math.Abs(z1+z2+z3); dAmplitude*=m_fDecay; } array[x,y] = dTotal; dMax = Math.Max(dMax, dTotal); dMin = Math.Min(dMin, dTotal); } } for (int x = canvas.Width - 1; x >= 0; x--) { for (int y = canvas.Height - 1; y >= 0; y--) { Color clr = (Color)m_aColorTable[(int)((array[x,y]-dMin)/(dMax+dMin)*255)]; canvas.SetPixel(x, y, clr); } } }
public override void WriteToBitmap(Canvas canvas) { this.PrepareWriteBitmap(canvas); for (int x = canvas.Width - 1; x >= 0; x--) { for (int y = canvas.Height - 1; y >= 0; y--) { float xyValue = 1f*(float)x * m_pntPeriods.X / m_nNoiseWidth + (float)y * m_pntPeriods.Y / m_nNoiseHeight; //xyValue += (float)x * xPeriod / a_bmp.Width + (float)y * yPeriod / a_bmp.Height; xyValue += m_fTurbulence*GetNoiseValue(x, y, m_nOctaves); float total = (float)(Math.Sin(xyValue * 3.14159)); //Math.Abs total = total*0.5f + 0.5f; int fin = (int)(total*255); Color clr = (Color)m_aColorTable[fin]; canvas.SetPixel(x, y, clr); } } }
protected void PrepareWriteBitmap(Canvas canvas) { m_fScaleX = (float)m_nNoiseWidth / canvas.Width; m_fScaleY = (float)m_nNoiseHeight / canvas.Height; }
public virtual void WriteToBitmap(Canvas canvas) { this.PrepareWriteBitmap(canvas); for (int x = canvas.Width - 1; x >= 0; x--) { for (int y = canvas.Height - 1; y >= 0; y--) { float total = GetNoiseValue((x+m_pntOffset.X)*m_fFreq, (y+m_pntOffset.Y)*m_fFreq, m_nOctaves); total = total*0.5f + 0.5f; int fin = (int)(total*255); Color clr = (Color)m_aColorTable[fin]; canvas.SetPixel(x, y, clr); } } }