#pragma once

namespace Image_Segmentation_on_GPU {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for MyFormHistogram
	/// </summary>
	public ref class MyFormHistogram : public System::Windows::Forms::Form
	{
	public:
		MyFormHistogram(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	private: System::Windows::Forms::TrackBar^  trackBar1;
	public:

	public:
		// setting description text
		property array<double>^ histogramValues
		{
			void set(array<double>^ histValues)
			{
				Values = histValues;
			}
		}

	private:
		double All = 0;
		array<double>^ Values;
		array<double>^ reducedValues;
	private: System::Windows::Forms::CheckBox^  checkBox2;
	private: System::Windows::Forms::Label^  label_trackbar_bar_count;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::GroupBox^  groupBox2;




	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog;



	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyFormHistogram()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::DataVisualization::Charting::Chart^  chart_histogram;
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma 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>
		void InitializeComponent(void)
		{
			System::Windows::Forms::DataVisualization::Charting::ChartArea^  chartArea1 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
			System::Windows::Forms::DataVisualization::Charting::Series^  series1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			this->chart_histogram = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
			this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
			this->checkBox2 = (gcnew System::Windows::Forms::CheckBox());
			this->label_trackbar_bar_count = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->saveFileDialog = (gcnew System::Windows::Forms::SaveFileDialog());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_histogram))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->BeginInit();
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->SuspendLayout();
			// 
			// chart_histogram
			// 
			this->chart_histogram->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
				| System::Windows::Forms::AnchorStyles::Left)
				| System::Windows::Forms::AnchorStyles::Right));
			this->chart_histogram->BackColor = System::Drawing::SystemColors::Control;
			this->chart_histogram->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
			chartArea1->AxisX->Interval = 1;
			chartArea1->AxisX->IsLabelAutoFit = false;
			chartArea1->AxisX->LabelStyle->Interval = 0.1;
			chartArea1->AxisX->MajorGrid->Interval = 0.1;
			chartArea1->AxisX->MajorGrid->LineColor = System::Drawing::Color::DarkGray;
			chartArea1->AxisX->MajorTickMark->Interval = 0.1;
			chartArea1->AxisX->Maximum = 1;
			chartArea1->AxisX->Minimum = 0;
			chartArea1->AxisX->MinorTickMark->Enabled = true;
			chartArea1->AxisX->ScaleView->Zoomable = false;
			chartArea1->AxisX->ScrollBar->Enabled = false;
			chartArea1->AxisX->Title = L"Intensity [-]";
			chartArea1->AxisX->TitleAlignment = System::Drawing::StringAlignment::Far;
			chartArea1->AxisY->MajorGrid->LineColor = System::Drawing::Color::DarkGray;
			chartArea1->AxisY->MinorTickMark->Enabled = true;
			chartArea1->AxisY->TextOrientation = System::Windows::Forms::DataVisualization::Charting::TextOrientation::Rotated270;
			chartArea1->AxisY->Title = L"Pixels in Image [-]";
			chartArea1->AxisY->TitleAlignment = System::Drawing::StringAlignment::Far;
			chartArea1->CursorX->Interval = 0.02;
			chartArea1->CursorX->IsUserEnabled = true;
			chartArea1->CursorX->IsUserSelectionEnabled = true;
			chartArea1->CursorX->SelectionColor = System::Drawing::Color::DarkGray;
			chartArea1->InnerPlotPosition->Auto = false;
			chartArea1->InnerPlotPosition->Height = 88;
			chartArea1->InnerPlotPosition->Width = 88;
			chartArea1->InnerPlotPosition->X = 10;
			chartArea1->InnerPlotPosition->Y = 2;
			chartArea1->Name = L"ChartArea1";
			this->chart_histogram->ChartAreas->Add(chartArea1);
			this->chart_histogram->Location = System::Drawing::Point(6, 19);
			this->chart_histogram->Name = L"chart_histogram";
			series1->ChartArea = L"ChartArea1";
			series1->CustomProperties = L"PointWidth=1";
			series1->Name = L"Histogram";
			this->chart_histogram->Series->Add(series1);
			this->chart_histogram->Size = System::Drawing::Size(765, 353);
			this->chart_histogram->SuppressExceptions = true;
			this->chart_histogram->TabIndex = 0;
			this->chart_histogram->Text = L"chart1";
			// 
			// trackBar1
			// 
			this->trackBar1->LargeChange = 8;
			this->trackBar1->Location = System::Drawing::Point(243, 19);
			this->trackBar1->Maximum = 256;
			this->trackBar1->Minimum = 8;
			this->trackBar1->Name = L"trackBar1";
			this->trackBar1->Size = System::Drawing::Size(129, 45);
			this->trackBar1->SmallChange = 4;
			this->trackBar1->TabIndex = 1;
			this->trackBar1->TickStyle = System::Windows::Forms::TickStyle::None;
			this->trackBar1->Value = 256;
			this->trackBar1->ValueChanged += gcnew System::EventHandler(this, &MyFormHistogram::trackBar1_ValueChanged);
			// 
			// checkBox2
			// 
			this->checkBox2->AutoSize = true;
			this->checkBox2->Location = System::Drawing::Point(378, 23);
			this->checkBox2->Name = L"checkBox2";
			this->checkBox2->Size = System::Drawing::Size(62, 17);
			this->checkBox2->TabIndex = 3;
			this->checkBox2->Text = L"Smooth";
			this->checkBox2->UseVisualStyleBackColor = true;
			this->checkBox2->CheckedChanged += gcnew System::EventHandler(this, &MyFormHistogram::checkBox2_CheckedChanged);
			// 
			// label_trackbar_bar_count
			// 
			this->label_trackbar_bar_count->AutoSize = true;
			this->label_trackbar_bar_count->Location = System::Drawing::Point(141, 24);
			this->label_trackbar_bar_count->Name = L"label_trackbar_bar_count";
			this->label_trackbar_bar_count->Size = System::Drawing::Size(103, 13);
			this->label_trackbar_bar_count->TabIndex = 5;
			this->label_trackbar_bar_count->Text = L"Number of bars: 128";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(6, 19);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(129, 23);
			this->button1->TabIndex = 6;
			this->button1->Text = L"Save Graph to Image";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyFormHistogram::button1_Click);
			// 
			// groupBox1
			// 
			this->groupBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->groupBox1->Controls->Add(this->button1);
			this->groupBox1->Controls->Add(this->checkBox2);
			this->groupBox1->Controls->Add(this->label_trackbar_bar_count);
			this->groupBox1->Controls->Add(this->trackBar1);
			this->groupBox1->Location = System::Drawing::Point(12, 396);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(777, 53);
			this->groupBox1->TabIndex = 7;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Histogram settings";
			// 
			// saveFileDialog
			// 
			this->saveFileDialog->DefaultExt = L"bmp";
			this->saveFileDialog->FileName = L"Histogram";
			this->saveFileDialog->Filter = L"BMP image(*.BMP)|*.bmp|JPG image(*.jpg)|*.jpg|PNG image(*.png)|*.png|All files (*"
				L".*)|*.*";
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->chart_histogram);
			this->groupBox2->Location = System::Drawing::Point(12, 12);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(777, 378);
			this->groupBox2->TabIndex = 8;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"Histogram";
			// 
			// MyFormHistogram
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(801, 461);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->MaximumSize = System::Drawing::Size(817, 500);
			this->MinimumSize = System::Drawing::Size(817, 500);
			this->Name = L"MyFormHistogram";
			this->ShowIcon = false;
			this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Show;
			this->Text = L"Histogram of input file";
			this->Load += gcnew System::EventHandler(this, &MyFormHistogram::MyFormHistogram_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_histogram))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->EndInit();
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void MyFormHistogram_Load(System::Object^  sender, System::EventArgs^  e)
	{
		int columns = System::Convert::ToInt16(this->trackBar1->Value);

		this->trackBar1->Value = 128;
	}

	private: System::Void trackBar1_ValueChanged(System::Object^  sender, System::EventArgs^  e)
	{
		int columns = System::Convert::ToInt16(this->trackBar1->Value);
		double value = 0;
		if (reducedValues)
		{
			delete reducedValues;
		}

		this->chart_histogram->Series["Histogram"]->Points->Clear();

		reducedValues = gcnew array<double>(columns);

		for (int i = 0; i < columns; i++)
		{
			value = 0;
			for (int j = 0; j < 256 / columns; j++)
			{
				value += Values[i * 256 / columns + j];
			}
			reducedValues[i] = value;
		}

		for (int i = 0; i < columns; i++)
		{
			this->chart_histogram->Series["Histogram"]->Points->AddXY(System::Convert::ToDouble(i) / (columns - 1), reducedValues[i]);
		}

		this->label_trackbar_bar_count->Text = "Number of Bars: " + columns;
	}

	private: System::Void checkBox2_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
	{
		if (this->checkBox2->Checked == true)
		{
			this->chart_histogram->Series["Histogram"]->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::SplineArea;
		}
		else
		{
			this->chart_histogram->Series["Histogram"]->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::Column;
		}
	}

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
	{
		if (this->saveFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
		{
			switch (this->saveFileDialog->FilterIndex)
			{
			case 1:
				this->chart_histogram->SaveImage(this->saveFileDialog->FileName, System::Windows::Forms::DataVisualization::Charting::ChartImageFormat::Bmp);
				break;
			case 2:
				this->chart_histogram->SaveImage(this->saveFileDialog->FileName, System::Windows::Forms::DataVisualization::Charting::ChartImageFormat::Jpeg);
				break;
			case 3:
				this->chart_histogram->SaveImage(this->saveFileDialog->FileName, System::Windows::Forms::DataVisualization::Charting::ChartImageFormat::Png);
				break;
			default:
				this->chart_histogram->SaveImage(this->saveFileDialog->FileName, System::Windows::Forms::DataVisualization::Charting::ChartImageFormat::Bmp);
				break;
			}
			this->saveFileDialog->FileName = "Histogram";
		}
	}
	};
}
