using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ToGrayscale
{
    public partial class frm36ToGrayscale : Form
    {
        public frm36ToGrayscale()
        {
            InitializeComponent();
        }

        // Make the grayscale images.
        private void frm36ToGrayscale_Load(object sender, EventArgs e)
        {
            BitmapToGrayscale(averagePictureBox.Image as Bitmap, true);
            BitmapToGrayscale(grayscalePictureBox.Image as Bitmap, false);
        }

        // Convert the Bitmap to grayscale.
        private void BitmapToGrayscale(Bitmap bm, bool useAverage)
        {
            // Make a Bitmap24 object.
            Bitmap32 bm32 = new Bitmap32(bm);

            // Lock the bitmap.
            bm32.LockBitmap();

            // Get color weights.
            float wr, wg, wb;
            if (useAverage)
            {
                wr = 1 / 3f;
                wg = 1 / 3f;
                wb = 1 / 3f;
            }
            else
            {
                wr = 0.3f;
                wg = 0.5f;
                wb = 0.2f;
            }

            // Process the pixels.
            for (int x = 0; x < bm.Width; x++)
            {
                for (int y = 0; y < bm.Height; y++)
                {
                    byte r, g, b, a;
                    bm32.GetPixel(x, y, out r, out g, out b, out a);
                    byte gray = (byte)(wr * r + wg * g + wb * b);
                    bm32.SetPixel(x, y, gray, gray, gray, a);
                }
            }

            // Unlock the bitmap.
            bm32.UnlockBitmap();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm36ToGrayscale));
            this.grayscalePictureBox = new System.Windows.Forms.PictureBox();
            this.averagePictureBox = new System.Windows.Forms.PictureBox();
            this.originalPictureBox = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.grayscalePictureBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.averagePictureBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.originalPictureBox)).BeginInit();
            this.SuspendLayout();
            // 
            // grayscalePictureBox
            // 
            this.grayscalePictureBox.Image = ((System.Drawing.Image)(resources.GetObject("grayscalePictureBox.Image")));
            this.grayscalePictureBox.Location = new System.Drawing.Point(372, 37);
            this.grayscalePictureBox.Name = "grayscalePictureBox";
            this.grayscalePictureBox.Size = new System.Drawing.Size(175, 217);
            this.grayscalePictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.grayscalePictureBox.TabIndex = 7;
            this.grayscalePictureBox.TabStop = false;
            // 
            // averagePictureBox
            // 
            this.averagePictureBox.Image = ((System.Drawing.Image)(resources.GetObject("averagePictureBox.Image")));
            this.averagePictureBox.Location = new System.Drawing.Point(191, 37);
            this.averagePictureBox.Name = "averagePictureBox";
            this.averagePictureBox.Size = new System.Drawing.Size(175, 217);
            this.averagePictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.averagePictureBox.TabIndex = 6;
            this.averagePictureBox.TabStop = false;
            // 
            // originalPictureBox
            // 
            this.originalPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("originalPictureBox.Image")));
            this.originalPictureBox.Location = new System.Drawing.Point(10, 37);
            this.originalPictureBox.Name = "originalPictureBox";
            this.originalPictureBox.Size = new System.Drawing.Size(175, 217);
            this.originalPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.originalPictureBox.TabIndex = 5;
            this.originalPictureBox.TabStop = false;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(10, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(175, 25);
            this.label1.TabIndex = 8;
            this.label1.Text = "Original";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(191, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(175, 25);
            this.label2.TabIndex = 9;
            this.label2.Text = "Average";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(372, 9);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(175, 25);
            this.label3.TabIndex = 10;
            this.label3.Text = "Weighted Average";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frm36ToGrayscale
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(557, 264);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.grayscalePictureBox);
            this.Controls.Add(this.averagePictureBox);
            this.Controls.Add(this.originalPictureBox);
            this.Name = "frm36ToGrayscale";
            this.Text = "ToGrayscale";
            this.Load += new System.EventHandler(this.frm36ToGrayscale_Load);
            ((System.ComponentModel.ISupportInitialize)(this.grayscalePictureBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.averagePictureBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.originalPictureBox)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        internal System.Windows.Forms.PictureBox grayscalePictureBox;
        internal System.Windows.Forms.PictureBox averagePictureBox;
        internal System.Windows.Forms.PictureBox originalPictureBox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
    }
}