Ejemplo n.º 1
0
		public void DrawCircle(int x0, int y0, uint radius, Color8i color)
		{
			int f = 1 - (int)radius;
			int ddF_x = 0;
			int ddF_y = -2 * (int)radius;
			int x = 0;
			int y = (int)radius;

			SetPixel(x0, y0 + (int)radius, color);
			SetPixel(x0, y0 - (int)radius, color);
			SetPixel(x0 + (int)radius, y0, color);
			SetPixel(x0 - (int)radius, y0, color);

			while (x < y)
			{
				if (f >= 0)
				{
					y--;
					ddF_y += 2;
					f += ddF_y;
				}
				x++;
				ddF_x += 2;
				f += ddF_x + 1;

				SetPixel(x0 + x, y0 + y, color);
				SetPixel(x0 - x, y0 + y, color);
				SetPixel(x0 + x, y0 - y, color);
				SetPixel(x0 - x, y0 - y, color);
				SetPixel(x0 + y, y0 + x, color);
				SetPixel(x0 - y, y0 + x, color);
				SetPixel(x0 + y, y0 - x, color);
				SetPixel(x0 - y, y0 - x, color);
			}
		}
Ejemplo n.º 2
0
		public void DrawCircleFilled(int x0, int y0, uint radius, Color8i color)
		{
			for (int i = x0 - (int)radius; i <= x0 + radius; i++)
			{
				for (int k = y0 - (int)radius; k <= y0 + radius; k++)
				{
					// check if point in image
					if (i < 1 | i > Width - 1) continue;
					if (k < 1 | k > Height - 1) continue;
			
					// calc distance
					Tuple2is a = new Tuple2is(x0, y0);
					Tuple2is b = new Tuple2is(i, k);
					double distance = a % b;

					if (radius > distance)
					{
						SetPixel(i, k, color);
					}
				}
			}
		}
Ejemplo n.º 3
0
		public void DrawRectangle(int x, int y, uint w, uint h, Color8i color)
		{
			DrawLine(x, y, x + (int)w, y, color); // von 1 zu 2
			DrawLine(x + (int)w, y, x + (int)w, y + (int)h, color); // von 2 zu 3
			DrawLine(x + (int)w, y + (int)h, x, y + (int)h, color); // von 3 zu 4
			DrawLine(x, y + (int)h, x, y, color); // von 4 zu 1
		}
Ejemplo n.º 4
0
		public void DrawRectangleFilled(int x, int y, uint w, uint h, Color8i color)
		{
			if (x >= Width || y >= Height) throw new ArgumentOutOfRangeException("x or y", "Out of image.");

			uint bpp = GetBytePerPixelFromChannelFormat(ChannelFormat);

			uint start = (uint)System.Math.Max(-x, 0);
			uint end = (uint)System.Math.Min(w, Width - x);

			uint jstart = (uint)System.Math.Max(-y, 0);
			uint jend = (uint)System.Math.Min(h, Height - y);

			unsafe
			{
				fixed(byte* dst_=imageData)
				{
					byte* dst__ = dst_ + x * bpp + start;

					uint dw = Width * bpp;

					if (ChannelFormat == ChannelFormat.Gray)
					{
						byte g = color.R;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++) *dst++ = g;
						}
					}
					else if (ChannelFormat == ChannelFormat.RGB)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++)
							{
								*dst++ = b;
								*dst++ = g;
								*dst++ = r;
							}
						}
					}
					else if (ChannelFormat == ChannelFormat.BGR)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++)
							{
								*dst++ = r;
								*dst++ = g;
								*dst++ = b;
							}
						}
					}
					else if (ChannelFormat == ChannelFormat.RGBA)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						byte a = color.A;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++)
							{
								*dst++ = b;
								*dst++ = g;
								*dst++ = r;
								*dst++ = a;
							}
						}
					}
					else if (ChannelFormat == ChannelFormat.BGRA)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						byte a = color.A;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++)
							{
								*dst++ = r;
								*dst++ = g;
								*dst++ = b;
								*dst++ = a;
							}
						}
					}
					else if (ChannelFormat == ChannelFormat.GrayAlpha)
					{
						byte g = color.R;
						byte a = color.A;

						for (uint j = jstart; j < jend; j++)
						{
							byte* dst = dst__ + dw * (y + j);

							for (uint i = start; i < end; i++)
							{
								*dst++ = g;
								*dst++ = a;
							}
						}
					}
				}
			}
		}
Ejemplo n.º 5
0
		/// <summary>
		/// Sets the pixel.
		/// </summary>
		/// <param name="x">The x coordinate.</param>
		/// <param name="y">The y coordinate.</param>
		/// <param name="color">Color.</param>
		public void SetPixel(uint x, uint y, Color8i color)
		{
			if (x >= Width || y >= Height) return;

			ulong pos = y * Width + x;
			pos *= GetBytePerPixelFromChannelFormat(ChannelFormat);

			switch (ChannelFormat)
			{
				case ChannelFormat.Gray:
					{
						imageData[pos] = color.R;
						break;
					}
				case ChannelFormat.GrayAlpha:
					{
						imageData[pos] = color.R;
						imageData[pos + 1] = color.A;
						break;
					}
				case ChannelFormat.BGR:
					{
						imageData[pos] = color.R;
						imageData[pos + 1] = color.G;
						imageData[pos + 2] = color.B;
						break;
					}
				case ChannelFormat.BGRA:
					{
						imageData[pos] = color.R;
						imageData[pos + 1] = color.G;
						imageData[pos + 2] = color.B;
						imageData[pos + 3] = color.A;
						break;
					}
				case ChannelFormat.RGB:
					{
						imageData[pos] = color.B;
						imageData[pos + 1] = color.G;
						imageData[pos + 2] = color.R;
						break;
					}
				case ChannelFormat.RGBA:
					{
						imageData[pos] = color.B;
						imageData[pos + 1] = color.G;
						imageData[pos + 2] = color.R;
						imageData[pos + 3] = color.A;
						break;
					}
			}
		}
Ejemplo n.º 6
0
		public void SetPixel(int x, int y, Color8i color)
		{
			SetPixel((uint)x, (uint)y, color);
		}
Ejemplo n.º 7
0
		public void DrawLine(int xStart, int yStart, int xEnd, int yEnd, Color8i color)
		{
			// Init
			int x, y, t, dist, xerr, yerr, dx, dy, incx, incy;

			// Calculate distance
			dx = xEnd - xStart;
			dy = yEnd - yStart;

			// Estimate sign
			if (dx < 0)
			{
				incx = -1;
				dx = -dx;
			}
			else if (dx > 0) incx = 1;
			else incx = 0;

			if (dy < 0)
			{
				incy = -1;
				dy = -dy;
			}
			else if (dy > 0) incy = 1;
			else incy = 0;

			// which distance is greater?
			dist = (dx > dy) ? dx : dy;

			// Init
			x = xStart;
			y = yStart;
			xerr = dx;
			yerr = dy;

			// Calculate pixel
			for (t = 0; t < dist; ++t)
			{
				SetPixel(x, y, color);

				xerr += dx;
				yerr += dy;

				if (xerr > dist)
				{
					xerr -= dist;
					x += incx;
				}

				if (yerr > dist)
				{
					yerr -= dist;
					y += incy;
				}
			}

			SetPixel(xEnd, yEnd, color);
		}
Ejemplo n.º 8
0
		/// <summary>
		/// Fills the with mandelbrot.
		/// </summary>
		/// <param name="x1">Cut of graphic.</param>
		/// <param name="y1">Cut of graphic.</param>
		/// <param name="x2">Cut of graphic.</param>
		/// <param name="y2">Cut of graphic.</param>
		/// <param name="depth">Calc depth.</param>
		public void FillWithMandelbrot(double x1, double y1, double x2, double y2, byte depth)
		{
			int d; // Counter for depth
			double dx, dy; // Increment per pixel
			double px, py; // current word coordinate
			double u, v; // calculation variables
			double ax, ay; // calculation variables

			Color8i[] c = new Color8i[256];

			// create random colors
			Random randomColors = new Random();

			for (int i = 0; i < 256; i++)
			{
				c[i] = new Color8i((byte)randomColors.Next(255), (byte)randomColors.Next(255), (byte)randomColors.Next(255));
			}

			dx = (x2 - x1) / Width;
			dy = (y2 - y1) / Height;

			// create the image
			for (uint y = 0; y < Height; y++)
			{
				for (uint x = 0; x < Width; x++)
				{
					px = x1 + x * dx;
					py = y1 + y * dy;
					d = 0;
					ax = 0;
					ay = 0;

					do
					{
						u = ax * ax - ay * ay + px;
						v = 2 * ax * ay + py;
						ax = u;
						ay = v;
						d++;
					}
					while(!(ax * ax + ay * ay > 8 || d == depth));

					SetPixel(x, y, c[d]);
				}
			}
		}
Ejemplo n.º 9
0
		public void Fill(Color8i color)
		{
			unsafe
			{
				fixed(byte* dst_=imageData)
				{
					byte* dst = dst_;

					if (ChannelFormat == ChannelFormat.Gray)
					{
						byte g = color.R;
						int len = imageData.Length;
						for (int i = 0; i < len; i++) *dst++ = g;
					}
					else if (ChannelFormat == ChannelFormat.RGB)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						int len = imageData.Length / 3;

						for (int i = 0; i < len; i++)
						{
							*dst++ = b;
							*dst++ = g;
							*dst++ = r;
						}
					}
					else if (ChannelFormat == ChannelFormat.BGR)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						int len = imageData.Length / 3;

						for (int i = 0; i < len; i++)
						{
							*dst++ = r;
							*dst++ = g;
							*dst++ = b;
						}
					}
					else if (ChannelFormat == ChannelFormat.RGBA)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						byte a = color.A;
						int len = imageData.Length / 4;

						for (int i = 0; i < len; i++)
						{
							*dst++ = b;
							*dst++ = g;
							*dst++ = r;
							*dst++ = a;
						}
					}
					else if (ChannelFormat == ChannelFormat.BGRA)
					{
						byte r = color.R;
						byte g = color.G;
						byte b = color.B;
						byte a = color.A;
						int len = imageData.Length / 4;

						for (int i = 0; i < len; i++)
						{
							*dst++ = r;
							*dst++ = g;
							*dst++ = b;
							*dst++ = a;
						}
					}
					else if (ChannelFormat == ChannelFormat.GrayAlpha)
					{
						byte g = color.R;
						byte a = color.A;
						int len = imageData.Length / 2;

						for (int i = 0; i < len; i++)
						{
							*dst++ = g;
							*dst++ = a;
						}
					}
				}
			}
		}