using System; using System.IO; using System.Drawing; using System.ComponentModel; using System.Drawing.Imaging; using System.Windows.Forms; using System.Threading; using Frames; using ClassLibrary; using Projects; using ControlLibrary; namespace ChildForms { public partial class frmChildTreko : Form { #region Local Variables TrekoControl _trekoControl; ProjectControl _projectControl; private frmChildColorProfile childColorProfile; Treko.Treko _trekoObj; Project _projectObj; Layer _layerObj; //ColorPixel[,] _colorPixels; FrameGroup[,] _frameGroups; int _mmToPixelsCoeffPreview = 20; //!!!!Nick string _outDirName; private ProgressModel progressModel1; private ProgressModel progressModel2; int angPreview = 0; int deltaAng = 5; int parallaxAng = 0; Graphics gPreview; #endregion // Delegate declaration public delegate void ColorProfileClicked(frmChildTreko sender, EventArgs e); public event ColorProfileClicked OnColorProfileClicked; #region Get/Set public string OutDirName { get { return _outDirName; } set { _outDirName = value; } } public Treko.Treko TrekoObj { get { return _trekoObj; } set { _trekoObj = value; } } public Project ProjectObj { get { return _projectObj; } set { _projectObj = value; } } public Layer LayerObj { get { return _layerObj; } set { _layerObj = value; } } public frmChildTreko() { InitializeComponent(); this.progressModel1 = new ProgressModel(); this.progressModel2 = new ProgressModel(); this.OnColorProfileClicked += childColorProfileClicked; } public int FrameGroupWidth { get { return (int)numFrameGroupWidth.Value; } set { numFrameGroupWidth.Value = value; } } public int FrameGroupHeight { get { return (int)numFrameGroupHeight.Value; } set { numFrameGroupHeight.Value = value; } } public int RowStart { get { return (int)numRowStart.Value; } set { numRowStart.Value = value; } } public int RowEnd { get { return (int)numRowEnd.Value; } set { numRowEnd.Value = value; } } public TrekoControl TrekoControl { get { return _trekoControl; } set { _trekoControl = value; } } public ProjectControl ProjectControl { get { return _projectControl; } set { _projectControl = value; } } #endregion public void RefreshChildForm() { FrameGroupHeight = 1; FrameGroupWidth = 1; RowStart = 0; RowEnd = Convert.ToInt16(ProjectObj.HologramHeight / ProjectObj.FrameHeight); } void childColorProfileClicked(object sender, EventArgs e) { if (childColorProfile == null || childColorProfile.IsDisposed) { childColorProfile = new frmChildColorProfile(); //childColorProfile.FormClosed += new FormClosedEventHandler(Child_FormClosed); childColorProfile.Location = new Point(Location.X, Location.Y + Height + 20); } childColorProfile.ColorProfileControl.ObjSender = sender; // Sender - TrekoControl, Treko3DControl childColorProfile.Show(); } public void GenerateAndSaveStrips(Treko.Treko trekoObj, Project projectObj) //string[] rakurseFiles) { _trekoObj = trekoObj; _projectObj = projectObj; //Strip strip = new Strip(rakurseFiles, Width, Height); //string dirName = trekoObj.ImagePath; string outDirName = projectObj.OutPath; //dirName + "\\TEST\\"; if (!Directory.Exists(outDirName)) { Directory.CreateDirectory(outDirName); } _outDirName = outDirName; //trekoObj.OutDirName = outDirName; //txtBoxProgress.Text = "In progress..."; //trekoObj.GenVectorData(_projectObj.HologramWidth, _projectObj.HologramHeight); trekoObj.GenVectorData(_projectObj.HologramWidth, _projectObj.HologramHeight); //txtBoxProgress.Text = "Done"; //Strip strip = new Strip(treko3DObj); // rakurseFiles); //strip.GenerateAndSaveStrips(outDirName); //fSystemBitmap = strip.MergeImages(fOriginalSystemBitmap, 50, 0); } public void Draw_HologramBorder(decimal nx, decimal ny) { Graphics g = Graphics.FromImage(ddPanBoxTreko.Image); ddPanBoxTreko.ClearImage(g); Color col = Color.FromArgb(255, 255, 0, 0); var Pn = new Pen(col, 3); g.DrawRectangle(Pn, 0, 0, (float)_projectObj.HologramWidth * _mmToPixelsCoeffPreview, (float)_projectObj.HologramHeight * _mmToPixelsCoeffPreview); Color col1 = Color.FromArgb(255, 255, 255, 0); var Pn1 = new Pen(col1, 1); g.DrawRectangle(Pn1, 0, 0, (float)0.2 * _mmToPixelsCoeffPreview, (float)0.15 * _mmToPixelsCoeffPreview); Color col2 = Color.FromArgb(255, 0, 255, 0); var Pn2 = new Pen(col2, 1); g.DrawRectangle(Pn2, 0, 0, (float)0.2 * _mmToPixelsCoeffPreview * Convert.ToInt32(nx), (float)0.15 * _mmToPixelsCoeffPreview * Convert.ToInt32(ny)); g.Dispose(); } public void DrawImage() { if (ddPanBoxTreko.Image == null) { _trekoObj.CreateImageTreko(); ddPanBoxTreko.Image = _trekoObj.ImgDisplayTreko; gPreview = Graphics.FromImage(ddPanBoxTreko.Image); timer1.Interval = 100; timer1.Enabled = true; } else { Graphics g = Graphics.FromImage(ddPanBoxTreko.Image); ddPanBoxTreko.ClearImage(g); g.Dispose(); } Draw_HologramBorder(numFrameGroupWidth.Value, numFrameGroupHeight.Value); _trekoObj.DrawOnImage(_trekoObj.ImgDisplayTreko, _mmToPixelsCoeffPreview); ddPanBoxTreko.Refresh(); } private void btnDraw_Click(object sender, EventArgs e) { timer1.Enabled = false; DrawImage(); } private void btnSaveBitmapToFile_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { ddPanBoxTreko.Image.Save(dialog.FileName, ImageFormat.Jpeg); } } public void generateFrameGroups() { int nFramesHor = Convert.ToInt32(_projectObj.HologramWidth / _projectObj.FrameWidth); int nFramesVert = Convert.ToInt32(_projectObj.HologramHeight / _projectObj.FrameHeight); string labelInd = "Gen. Frames " + Convert.ToString(nFramesHor) + "x" + Convert.ToString(nFramesVert); _frameGroups = new FrameGroup[nFramesHor, nFramesVert]; for (int j = 0; j < _frameGroups.GetLength(1); j++) { for (int i = 0; i < _frameGroups.GetLength(0); i++) { // in group only one frame for now _frameGroups[i, j] = new FrameGroup(_projectObj.ResolutionX, _projectObj.ResolutionY, _projectObj.FrameWidth, _projectObj.FrameHeight, 1, 1, i, j); _frameGroups[i, j].FillByIntersectColorArcs(_trekoObj.ColorPixels); progressBar1.Maximum = _frameGroups.GetLength(0); progressBar1.Value = i; lblGenFrames.Text = labelInd + " - " + Convert.ToString(i) + "x" + Convert.ToString(j); lblGenFrames.Refresh(); } progressBar2.Maximum = _frameGroups.GetLength(1); progressBar2.Value = j; } } public void GenerateFrames() { generateFrameGroups(); } private void clearProgressInfo() { lblGenFrames.Text = ""; //lblSaveFrames.Text = ""; progressBar1.Value = 0; progressBar2.Value = 0; //progressBar3.Value = 0; //progressBar4.Value = 0; } private void clearErrors() { errorProvider1.SetError(numUpDownStep, ""); errorProvider1.SetError(numUpDownRadius, ""); } private void launchBackgroundWorker() { if (backgroundWorker1.IsBusy != true) { int nFramesHor = Convert.ToInt32(_projectObj.HologramWidth / _projectObj.FrameWidth); int nFramesVert = Convert.ToInt32(_projectObj.HologramHeight / _projectObj.FrameHeight); // Number of frames in group int nFramesInGroupHor = FrameGroupWidth; int nFramesInGroupVert = FrameGroupHeight; // ini values in file this.progressModel1.MaxValue = nFramesHor / nFramesInGroupHor + 1; // horizontal length is bigger because of briks this.progressModel1.MinValue = 0; this.progressModel1.CurValue = 0; this.progressModel2.MaxValue = RowEnd + 1; // nFramesVert / nFramesInGroupVert; this.progressModel2.MinValue = RowStart; this.progressModel2.CurValue = RowStart; // Start the asynchronous operation. backgroundWorker1.RunWorkerAsync(); } } private bool btnRunValidating() { bool success = true; clearErrors(); CancelEventArgs ee = null; numFrameGroupWidth_Validating(this, ee); numFrameGroupHeight_Validating(this, ee); numRowStart_Validating(this, ee); numRowEnd_Validating(this, ee); return success; } private void btnGenerateFrames_Click(object sender, EventArgs e) { clearProgressInfo(); if (btnRunValidating() == false) return; TrekoControl.SaveData(); _trekoObj = TrekoControl.trekoObj; launchBackgroundWorker(); //GenerateFrames(); } //public void SaveFramesToFiles() //{ // int n = 1; // if (_frameGroups != null) // { // string labelInd = "Gen. Frames " + Convert.ToString(_frameGroups.GetLength(0)) + "x" + Convert.ToString(_frameGroups.GetLength(1)); // for (int j = 0; j < _frameGroups.GetLength(1); j++) // { // for (int i = 0; i < _frameGroups.GetLength(0); i++) // { // FrameGroup fg = _frameGroups[i, j]; // // in group only one frame for now // if (fg.IntersectColorArcs != null && fg.IntersectColorArcs.Count > 0) // { // fg.DrawBitmapByColorArcs(_layerObj.TraceProfile); // fg.SaveGroupToFiles(_outDirName, n, i, j); // //fg.SaveGroupToFiles(_outDirName, n); // fg.FrameGroupBmp.Dispose(); // } // n++; // progressBar3.Maximum = _frameGroups.GetLength(0); // progressBar3.Value = i; // label4.Text = labelInd + " - " + Convert.ToString(i) + "x" + Convert.ToString(j); // label4.Refresh(); // } // progressBar4.Maximum = _frameGroups.GetLength(1); // progressBar4.Value = j; // } // } else // MessageBox.Show("No generated frames", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); //} //private void BtnSaveFrames_Click(object sender, EventArgs e) //{ // SaveFramesToFiles(); //} public int ProgressPercentage(int value, int minval, int maxval) { return value * 100 / maxval; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; while (progressModel2.CurValue < progressModel2.MaxValue) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { int nFramesHor = Convert.ToInt32(_projectObj.HologramWidth / _projectObj.FrameWidth); int nFramesVert = Convert.ToInt32(_projectObj.HologramHeight / _projectObj.FrameHeight); //string labelInd = "Gen. Frames " + Convert.ToString(nFramesHor) + "x" + Convert.ToString(nFramesVert); // Number of frames in group int nFramesInGroupHor = FrameGroupWidth; int nFramesInGroupVert = FrameGroupHeight; // add one more frame group for bricks construction _frameGroups = new FrameGroup[nFramesHor / nFramesInGroupHor + 1, nFramesVert / nFramesInGroupVert + 1]; //_frameGroups = new FrameGroup[nFramesHor, nFramesVert]; string outDirName = _projectObj.OutPath; // dirName + "\\TEST\\"; for (int j = 0; j < _frameGroups.GetLength(1); j++) { for (int i = 0; i < _frameGroups.GetLength(0); i++) { // in group only one frame for now //_frameGroups[i, j] = new FrameGroup(_projectObj.ResolutionX, _projectObj.ResolutionY, _projectObj.FrameWidth, _projectObj.FrameHeight, 1, 1, i, j); _frameGroups[i, j] = new FrameGroup(_projectObj.ResolutionX, _projectObj.ResolutionY, _projectObj.FrameWidth, _projectObj.FrameHeight, nFramesInGroupHor, nFramesInGroupVert, i, j); FrameGroup fg = _frameGroups[i, j]; fg.FillByIntersectColorArcs(_trekoObj.ColorPixels); //_frameGroups[i, j].FillByIntersectColorArcs(_treko); if (fg.IntersectColorArcs != null && fg.IntersectColorArcs.Count > 0) { fg.DrawBitmapByColorArcs(_layerObj.TraceProfile); fg.CropIntoImageArray(j); //fg.SaveGroupToFiles(_outDirName, n, i, j); fg.SaveGroupToFiles(outDirName, i, j, _frameGroups.GetLength(0), _frameGroups.GetLength(1)); fg.FrameGroupBmp.Dispose(); } else fg.MarkFrameGroupAsBlack(); this.progressModel1.CurValue = i + 1; int progress1 = ProgressPercentage(progressModel1.CurValue, progressModel1.MinValue, progressModel1.MaxValue); worker.ReportProgress(progress1); if (worker.CancellationPending == true) { e.Cancel = true; break; } // Perform a time consuming operation and report progress. Thread.Sleep(20); } this.progressModel2.CurValue = j + 1; int progress2 = ProgressPercentage(progressModel2.CurValue, progressModel2.MinValue, progressModel2.MaxValue); worker.ReportProgress(progress2); if (worker.CancellationPending == true) { e.Cancel = true; break; } Thread.Sleep(20); } } } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { int nFramesHor = Convert.ToInt32(_projectObj.HologramWidth / _projectObj.FrameWidth); int nFramesVert = Convert.ToInt32(_projectObj.HologramHeight / _projectObj.FrameHeight); int progress1 = ProgressPercentage(progressModel1.CurValue, progressModel1.MinValue, progressModel1.MaxValue); int progress2 = ProgressPercentage(progressModel2.CurValue, progressModel2.MinValue, progressModel2.MaxValue); // Change the value of the ProgressBar to the BackgroundWorker progress. progressBar1.Value = progress1; // e.ProgressPercentage; progressBar1.Maximum = 100; progressBar1.Minimum = 0; progressBar2.Value = progress2; progressBar2.Maximum = 100; progressBar2.Minimum = 0; // Number of frames in group int nFramesInGroupHor = FrameGroupWidth; int nFramesInGroupVert = FrameGroupHeight; string labelInd = "Gen. Frames " + Convert.ToString(nFramesHor / nFramesInGroupHor + 1) + "x" + Convert.ToString(nFramesVert / nFramesInGroupVert); lblGenFrames.Text = labelInd + " - " + Convert.ToString(this.progressModel1.CurValue) + "x" + Convert.ToString(this.progressModel2.CurValue); lblGenFrames.Refresh(); // Set the text. //this.Text = e.ProgressPercentage.ToString(); } private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { if (e.Cancelled == true) { lblGenFrames.Text = "Canceled!"; } else if (e.Error != null) { lblGenFrames.Text = "Error: " + e.Error.Message; } else { lblGenFrames.Text = "Done!"; GenerateIniFile(); } } private void vResizeImage_ValueChanged(object sender, EventArgs e) { txtMmToPixCoeff.Text = ((VScrollBar)sender).Value.ToString(); _mmToPixelsCoeffPreview = ((VScrollBar)sender).Value; if(timer1.Enabled == false) DrawImage(); } private Frame GetFrame(int nCol, int nRow) { int nFramesHor = _frameGroups[0, 0].NFramesHor; int nFramesVert = _frameGroups[0, 0].NFramesVert; int iGroup = nCol / nFramesHor; int jGroup = nRow / nFramesVert; int i = nCol % nFramesHor; int j = nRow % nFramesVert; FrameGroup fg = _frameGroups[iGroup, jGroup]; Frame fr = fg.FrameMap[i, j]; if (fg.IsBlack) fr.IsBlack = true; return fr; } public void GenerateIniFile() { string outDirName = _projectObj.OutPath; // dirName + "\\TEST\\"; string sLine = ""; int eCount = 0; int zCount = 0; StreamWriter sw = new StreamWriter(outDirName + "\\exp.ini"); int nGroupCols = _frameGroups.GetLength(0); int nGroupRows = _frameGroups.GetLength(1); int nCols = nGroupCols * _frameGroups[0, 0].NFramesHor; int nRows = nGroupRows * _frameGroups[0, 0].NFramesVert; sLine = nCols.ToString(); WriteIniFile(sw, sLine); sLine = nCols.ToString(); WriteIniFile(sw, sLine); sLine = nRows.ToString(); WriteIniFile(sw, sLine); sLine = ""; int n = 0; // for debug number of frame files for (int j = 0; j < nRows; j++) { for (int i = 0; i < nCols; i++) { Frame fr = GetFrame(i, j); //FrameGroup fg = _frameGroups[i, j]; if (fr.IsBlack == false) { checkZcount(ref zCount, ref sLine); eCount++; } else { checkEcount(ref eCount, ref n, ref sLine); zCount++; } //// in group only one frame for now //if (fg.IntersectRakurseArcs != null && fg.IntersectRakurseArcs.Count > 0) //{ // checkZcount(ref zCount, ref sLine); // eCount++; //} //else //{ // checkEcount(ref eCount, ref n, ref sLine); // zCount++; //} } checkEcount(ref eCount, ref n, ref sLine); checkZcount(ref zCount, ref sLine); WriteIniFile(sw, sLine); sLine = ""; } WriteIniFile(sw, "Number of files:" + n); sw.Close(); } void checkEcount(ref int eCount, ref int n, ref string sLine) { if (eCount > 0) { n += eCount; sLine = sLine + eCount + "e"; eCount = 0; } } void checkZcount(ref int zCount, ref string sLine) { if (zCount > 0) { sLine = sLine + zCount + "z"; zCount = 0; } } public void WriteIniFile(StreamWriter sw, string sLine) { try { sw.WriteLine(sLine); } catch (Exception e) { } finally { } } private void btnGenExpIni_Click(object sender, EventArgs e) { GenerateIniFile(); } private void btnAnimation_Click(object sender, EventArgs e) { if (ddPanBoxTreko.Image == null) { ddPanBoxTreko.Image = _trekoObj.ImgDisplayTreko; //ddPanBoxTreko3D.ClearImage(); } gPreview = Graphics.FromImage(ddPanBoxTreko.Image); ddPanBoxTreko.ClearImage(gPreview); ddPanBoxTreko.Refresh(); //!!!!Nick timer1.Interval = 100; //!!!!Nick timer1.Enabled = true; //!!!!Nick } public void AnimateImage(int parallaxAng) { ddPanBoxTreko.ClearImage(gPreview); Draw_HologramBorder(numFrameGroupWidth.Value, numFrameGroupHeight.Value); _trekoObj.DrawAnimation(_trekoObj.ImgDisplayTreko, angPreview, parallaxAng, _mmToPixelsCoeffPreview); ddPanBoxTreko.Refresh(); } private void timer1_Tick(object sender, EventArgs e) { angPreview += deltaAng; AnimateImage(parallaxAng); if (angPreview < 0) //!!!!Nick { angPreview = 0; deltaAng = -deltaAng; } if (angPreview > 180) { angPreview = 180; deltaAng = -deltaAng; } } private void chckTimer_CheckedChanged(object sender, EventArgs e) { timer1.Enabled = chckTimer.Checked; } private void btnTraceProfile_Click(object sender, EventArgs e) { if (OnColorProfileClicked != null) OnColorProfileClicked(this, EventArgs.Empty); } private void numUpDownSpeed_ValueChanged(object sender, EventArgs e) { if (deltaAng >= 0) deltaAng = Convert.ToInt32(numUpDownSpeed.Value); if (deltaAng < 0) deltaAng = Convert.ToInt32(numUpDownSpeed.Value) * -1; } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { parallaxAng = Convert.ToInt32(numericUpDown1.Value); } private void numUpDownStep_ValueChanged(object sender, EventArgs e) { //step = (float)numUpDownStep.Value / 1000; } private void numFrameGroupWidth_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (numFrameGroupWidth.Value < 1) numFrameGroupWidth.Value = 1; if (numFrameGroupWidth.Value > 20) numFrameGroupWidth.Value = 20; } private void numFrameGroupHeight_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (numFrameGroupHeight.Value < 1) numFrameGroupHeight.Value = 1; if (numFrameGroupHeight.Value > 20) numFrameGroupHeight.Value = 20; } private void numRowStart_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (numRowStart.Value < 0) numRowStart.Value = 0; } private void numRowEnd_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (numRowEnd.Value < 0) numRowEnd.Value = 0; int nRows = Convert.ToInt16(ProjectObj.HologramHeight / (ProjectObj.FrameHeight * FrameGroupHeight)); numRowEnd.Value = nRows; } private void numFrameGroupHeight_ValueChanged(object sender, EventArgs e) { CancelEventArgs ee = new CancelEventArgs(); numRowEnd_Validating(sender, ee); } private void btnCancel_Click(object sender, EventArgs e) { this.Visible = false; backgroundWorker1.CancelAsync(); } private void numFrameGroupWidth_ValueChanged(object sender, EventArgs e) { } } }