// Convert to a blue scale. private void btnBlue_Click(object sender, EventArgs e) { Bitmap bm = new Bitmap(picVisible.Image); this.Cursor = Cursors.WaitCursor; DateTime start_time = DateTime.Now; // Make a Bitmap32 object. Bitmap32 bm32 = new Bitmap32(bm); // Convert to blue. bm32.ClearRed(); bm32.ClearGreen(); // Display the result. picVisible.Image = bm; DateTime stop_time = DateTime.Now; this.Cursor = Cursors.Default; TimeSpan elapsed_time = stop_time - start_time; lblElapsed.Text = elapsed_time.TotalSeconds.ToString("0.000000"); }
// Warp an image and return a new Bitmap32 holding the result. public Bitmap32 Warp(WarpOperations warp_op, bool lock_result) { // Make a copy of this Bitmap32. Bitmap32 result = this.Clone(); // Lock both bitmaps. bool was_locked = this.IsLocked; this.LockBitmap(); result.LockBitmap(); // Warp the image. WarpImage(this, result, warp_op); // Unlock the bitmaps. if (!lock_result) { result.UnlockBitmap(); } if (!was_locked) { this.UnlockBitmap(); } // Return the result. return(result); }
// Apply a filter. private void ApplyFilter(Bitmap32.Filter filter) { Bitmap bm = new Bitmap(picVisible.Image); this.Cursor = Cursors.WaitCursor; DateTime start_time = DateTime.Now; // Make a Bitmap32 object. Bitmap32 bm32 = new Bitmap32(bm); // Apply the filter. Bitmap32 new_bm32 = bm32.ApplyFilter(filter, false); // Display the result. picVisible.Image = new_bm32.Bitmap; DateTime stop_time = DateTime.Now; this.Cursor = Cursors.Default; TimeSpan elapsed_time = stop_time - start_time; lblElapsed.Text = elapsed_time.TotalSeconds.ToString("0.000000"); }
private void DisplayWarpedImage(Bitmap32.WarpOperations warp_op) { Bitmap bm = new Bitmap(picVisible.Image); this.Cursor = Cursors.WaitCursor; DateTime start_time = DateTime.Now; // Make a Bitmap32 object. Bitmap32 bm32 = new Bitmap32(bm); // Apply the warping operation. Bitmap32 new_bm32 = bm32.Warp(warp_op, false); // Display the result. picVisible.Image = new_bm32.Bitmap; DateTime stop_time = DateTime.Now; this.Cursor = Cursors.Default; TimeSpan elapsed_time = stop_time - start_time; lblElapsed.Text = elapsed_time.TotalSeconds.ToString("0.000000"); }
// Make a deep copy of this object. public Bitmap32 Clone() { // See if we are locked. bool was_locked = this.IsLocked; // Lock this bitmap. this.LockBitmap(); // Perform a shallow copy. Bitmap32 result = (Bitmap32)this.MemberwiseClone(); // Copy the Bitmap. result.Bitmap = new Bitmap(this.Bitmap.Width, this.Bitmap.Height); result.m_IsLocked = false; // Unlock if appropriate. if (!was_locked) { this.UnlockBitmap(); } // Return the result. return(result); }
// Transform the image. private static void WarpImage(Bitmap32 bm_src, Bitmap32 bm_dest, WarpOperations warp_op) { // Calculate some image information. double xmid = bm_dest.Width / 2.0; double ymid = bm_dest.Height / 2.0; double rmax = bm_dest.Width * 0.75; int ix_max = bm_src.Width - 2; int iy_max = bm_src.Height - 2; // Generate a result for each output pixel. double x0, y0; for (int y1 = 0; y1 < bm_dest.Height; y1++) { for (int x1 = 0; x1 < bm_dest.Width; x1++) { // Map back to the source image. MapPixel(warp_op, xmid, ymid, rmax, x1, y1, out x0, out y0); // Interpolate to get the result pixel's value. // Find the next smaller integral position. int ix0 = (int)x0; int iy0 = (int)y0; // See if this is out of bounds. if ((ix0 < 0) || (ix0 > ix_max) || (iy0 < 0) || (iy0 > iy_max)) { // The point is outside the image. Use white. bm_dest.SetPixel(x1, y1, 255, 255, 255, 255); } else { // The point lies within the image. // Calculate its value. double dx0 = x0 - ix0; double dy0 = y0 - iy0; double dx1 = 1 - dx0; double dy1 = 1 - dy0; // Get the colors of the surrounding pixels. byte r00, g00, b00, a00, r01, g01, b01, a01, r10, g10, b10, a10, r11, g11, b11, a11; bm_src.GetPixel(ix0, iy0, out r00, out g00, out b00, out a00); bm_src.GetPixel(ix0, iy0 + 1, out r01, out g01, out b01, out a01); bm_src.GetPixel(ix0 + 1, iy0, out r10, out g10, out b10, out a10); bm_src.GetPixel(ix0 + 1, iy0 + 1, out r11, out g11, out b11, out a11); // Compute the weighted average. int r = (int)( r00 * dx1 * dy1 + r01 * dx1 * dy0 + r10 * dx0 * dy1 + r11 * dx0 * dy0); int g = (int)( g00 * dx1 * dy1 + g01 * dx1 * dy0 + g10 * dx0 * dy1 + g11 * dx0 * dy0); int b = (int)( b00 * dx1 * dy1 + b01 * dx1 * dy0 + b10 * dx0 * dy1 + b11 * dx0 * dy0); int a = (int)( a00 * dx1 * dy1 + a01 * dx1 * dy0 + a10 * dx0 * dy1 + a11 * dx0 * dy0); bm_dest.SetPixel(x1, y1, (byte)r, (byte)g, (byte)b, (byte)a); } } } }
// Apply a filter to the image. public Bitmap32 ApplyFilter(Filter filter, bool lock_result) { // Make a copy of this Bitmap32. Bitmap32 result = this.Clone(); // Lock both bitmaps. bool was_locked = this.IsLocked; this.LockBitmap(); result.LockBitmap(); // Apply the filter. int xoffset = -(int)(filter.Kernel.GetUpperBound(1) / 2); int yoffset = -(int)(filter.Kernel.GetUpperBound(0) / 2); int xmin = -xoffset; int xmax = Bitmap.Width - filter.Kernel.GetUpperBound(1); int ymin = -yoffset; int ymax = Bitmap.Height - filter.Kernel.GetUpperBound(0); int row_max = filter.Kernel.GetUpperBound(0); int col_max = filter.Kernel.GetUpperBound(1); for (int x = xmin; x <= xmax; x++) { for (int y = ymin; y <= ymax; y++) { // Skip the pixel if any under the kernel // is completely transparent. bool skip_pixel = false; // Apply the filter to pixel (x, y). float red = 0, green = 0, blue = 0; for (int row = 0; row <= row_max; row++) { for (int col = 0; col <= col_max; col++) { int ix = x + col + xoffset; int iy = y + row + yoffset; byte new_red, new_green, new_blue, new_alpha; this.GetPixel(ix, iy, out new_red, out new_green, out new_blue, out new_alpha); // See if we should skip this pixel. if (new_alpha == 0) { skip_pixel = true; break; } red += new_red * filter.Kernel[row, col]; green += new_green * filter.Kernel[row, col]; blue += new_blue * filter.Kernel[row, col]; } if (skip_pixel) { break; } } if (!skip_pixel) { // Divide by the weight, add the offset, and // make sure the result is between 0 and 255. red = filter.offset + red / filter.Weight; if (red < 0) { red = 0; } if (red > 255) { red = 255; } green = filter.offset + green / filter.Weight; if (green < 0) { green = 0; } if (green > 255) { green = 255; } blue = filter.offset + blue / filter.Weight; if (blue < 0) { blue = 0; } if (blue > 255) { blue = 255; } // Set the new pixel's value. result.SetPixel(x, y, (byte)red, (byte)green, (byte)blue, this.GetAlpha(x, y)); } } } // Unlock the bitmaps. if (!lock_result) { result.UnlockBitmap(); } if (!was_locked) { this.UnlockBitmap(); } // Return the result. return(result); }