public static Image ExportMask(IMask mask) { int width = TerrainGlobals.getTerrain().getNumXVerts(); int height = TerrainGlobals.getTerrain().getNumZVerts(); Rectangle r = new Rectangle(0, 0, width, height); Bitmap destination = new Bitmap(width, height, PixelFormat.Format24bppRgb); unsafe { BitmapData outputData = destination.LockBits(r, ImageLockMode.WriteOnly, destination.PixelFormat); if (outputData.PixelFormat == PixelFormat.Format24bppRgb) { PixelData24 *outputBase = (PixelData24 *)outputData.Scan0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData24 *pPixel = outputBase + y * width + x; long index = y * width + x; float value = mask.GetMaskWeight(index); byte outputValue = (byte)(value * byte.MaxValue); pPixel->blue = outputValue; pPixel->red = outputValue; pPixel->green = outputValue; } } destination.UnlockBits(outputData); } } return((Image)destination); }
//This sucks. static public Bitmap generateAlpha(Bitmap source) { Bitmap output = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb); Rectangle r = new Rectangle(0, 0, source.Width, source.Height); unsafe { BitmapData sourceData = source.LockBits(r, ImageLockMode.ReadOnly, source.PixelFormat); BitmapData outputData = output.LockBits(r, ImageLockMode.WriteOnly, output.PixelFormat); if (sourceData.PixelFormat == PixelFormat.Format24bppRgb) { PixelData24 *sourceBase = (PixelData24 *)sourceData.Scan0; PixelData32 *outputBase = (PixelData32 *)outputData.Scan0; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData24 *pSourcePixel = sourceBase + y * width + x; PixelData32 *pOututPixel = outputBase + y * width + x; byte blue = pSourcePixel->blue; pOututPixel->alpha = (byte)(blue); pOututPixel->red = (byte)(blue); pOututPixel->blue = (byte)(blue); pOututPixel->green = (byte)(blue); } } } else if (sourceData.PixelFormat == PixelFormat.Format32bppArgb) { PixelData32 *sourceBase = (PixelData32 *)sourceData.Scan0; PixelData32 *outputBase = (PixelData32 *)outputData.Scan0; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData32 *pSourcePixel = sourceBase + y * width + x; PixelData32 *pOututPixel = outputBase + y * width + x; byte alpha = pSourcePixel->alpha; pOututPixel->alpha = (byte)(alpha); pOututPixel->red = (byte)(alpha); pOututPixel->blue = (byte)(alpha); pOututPixel->green = (byte)(alpha); } } } source.UnlockBits(sourceData); output.UnlockBits(outputData); } return(output); }
public void render(Bitmap source) { float max = float.MinValue; float min = float.MaxValue; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float v = mFunction.compute(x, y); if (v > max) { max = v; } if (v < min) { min = v; } mFloats[x, y] = v; } } //adjust range float scale = 1 / (max - min); float offset = -min * scale; Rectangle r = new Rectangle(0, 0, source.Width, source.Height); unsafe { BitmapData sourceData = source.LockBits(r, ImageLockMode.ReadOnly, source.PixelFormat); PixelData24 *sourceBase = (PixelData24 *)sourceData.Scan0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData24 *pSourcePixel = sourceBase + y * width + x; float v = offset + scale * mFloats[x, y]; byte val = (byte)(v * 255); pSourcePixel->red = val; pSourcePixel->green = val; pSourcePixel->blue = val; } } source.UnlockBits(sourceData); } }
public void SetPixel(int x, int y, byte alpha, byte red, byte green, byte blue) { switch (bitmap.PixelFormat) { case PixelFormat.Format24bppRgb: PixelData24 *pixel24 = PixelAt24(x, y); * pixel24 = new PixelData24(red, green, blue); break; case PixelFormat.Format32bppArgb: PixelData32 *pixel32 = PixelAt32(x, y); * pixel32 = new PixelData32(alpha, red, green, blue); break; default: throw new NotSupportedException(); } }
public static IMask CreateMask(Image source1) { if (myCallback == null) { myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); } IMask mask = MaskFactory.GetNewMask(); Bitmap source = (Bitmap)(source1.Clone()); Rectangle r = new Rectangle(0, 0, source.Width, source.Height); int formatSize = Image.GetPixelFormatSize(source.PixelFormat); unsafe { BitmapData sourceData = source.LockBits(r, ImageLockMode.ReadWrite, source.PixelFormat); if (sourceData.PixelFormat == PixelFormat.Format24bppRgb) { PixelData24 *sourceBase = (PixelData24 *)sourceData.Scan0; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData24 *pSourcePixel = sourceBase + y * width + x; byte blue = pSourcePixel->blue; //pOututPixel->alpha = (byte)(blue); //pOututPixel->red = (byte)(blue); //pOututPixel->blue = (byte)(blue); //pOututPixel->green = (byte)(blue); long index = y * width + x; float value = blue / (float)byte.MaxValue; //mask.Add(index, value); mask.SetMaskWeight(index, value); } } source.UnlockBits(sourceData); } else if (sourceData.PixelFormat == PixelFormat.Format32bppRgb) { PixelData32Rgb *sourceBase = (PixelData32Rgb *)sourceData.Scan0; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData32Rgb *pSourcePixel = sourceBase + y * width + x; byte blue = pSourcePixel->blue; //pOututPixel->alpha = (byte)(blue); //pOututPixel->red = (byte)(blue); //pOututPixel->blue = (byte)(blue); //pOututPixel->green = (byte)(blue); long index = y * width + x; float value = blue / (float)byte.MaxValue; //mask.Add(index, value); mask.SetMaskWeight(index, value); } } source.UnlockBits(sourceData); } else if (sourceData.PixelFormat == PixelFormat.Format32bppPArgb) { PixelData32PArgb *sourceBase = (PixelData32PArgb *)sourceData.Scan0; int width = source.Width; int height = source.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { PixelData32PArgb *pSourcePixel = sourceBase + y * width + x; byte blue = pSourcePixel->blue; //pOututPixel->alpha = (byte)(blue); //pOututPixel->red = (byte)(blue); //pOututPixel->blue = (byte)(blue); //pOututPixel->green = (byte)(blue); long index = y * width + x; float value = blue / (float)byte.MaxValue; //mask.Add(index, value); mask.SetMaskWeight(index, value); } } source.UnlockBits(sourceData); } } return(mask); }