Ejemplo n.º 1
0
 public CompareItem(ManagedBitmap bmp, string name, bool isItalic, int expandCount, string text)
 {
     Bitmap = bmp;
     Name = name;
     Italic = isItalic;
     ExpandCount = expandCount;
     NumberOfForegroundColors = -1;
     Text = text;
 }
Ejemplo n.º 2
0
 internal void DrawImage(ManagedBitmap bmp, Point point)
 {
     for (int y = 0; y < bmp.Height; y++)
     {
         int newY = point.Y + y;
         if (newY >= 0 && newY < Height)
         {
             for (int x = 0; x < bmp.Width; x++)
             {
                 int newX = point.X + x;
                 if (newX >= 0 && newX < Width)
                 {
                     this.SetPixel(newX, newY, bmp.GetPixel(x, y));
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Copies a rectangle from the bitmap to a new bitmap
        /// </summary>
        /// <param name="section">Source rectangle</param>
        /// <returns>Rectangle from current image as new bitmap</returns>
        public ManagedBitmap GetRectangle(Rectangle section)
        {
            ManagedBitmap newRectangle = new ManagedBitmap(section.Width, section.Height);

            int recty = 0;

            for (int y = section.Top; y < section.Top + section.Height; y++)
            {
                int rectx = 0;
                for (int x = section.Left; x < section.Left + section.Width; x++)
                {
                    newRectangle.SetPixel(rectx, recty, this.GetPixel(x, y));
                    rectx++;
                }
                recty++;
            }
            return(newRectangle);
        }
Ejemplo n.º 4
0
        internal static int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap bmp2)
        {
            int different = 0;
            int maxDiff   = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                    {
                        different++;
                    }
                }
                if (different > maxDiff)
                {
                    return(different + 10);
                }
            }
            return(different);
        }
        internal static unsafe int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                        different++;
                }
                if (different > maxDiff)
                    return different + 10;
            }
            return different;
        }
Ejemplo n.º 6
0
        private void LoadOldCompareImages()
        {
            _compareBitmaps = new List<CompareItem>();
            string path = Configuration.VobSubCompareFolder + comboBoxCharacterDatabase.SelectedItem + Path.DirectorySeparatorChar;
            if (!File.Exists(path + "CompareDescription.xml"))
                _compareDoc.LoadXml("<OcrBitmaps></OcrBitmaps>");
            else
                _compareDoc.Load(path + "CompareDescription.xml");

            string databaseName = path + "Images.db";
            if (!File.Exists(databaseName))
            {
                labelStatus.Text = Configuration.Settings.Language.VobSubOcr.LoadingImageCompareDatabase;
                labelStatus.Refresh();
                using (var f = new FileStream(databaseName, FileMode.Create))
                {
                    foreach (string bmpFileName in Directory.GetFiles(path, "*.bmp"))
                    {
                        string name = Path.GetFileNameWithoutExtension(bmpFileName);

                        XmlNode node = _compareDoc.DocumentElement.SelectSingleNode("FileName[.='" + name + "']");
                        if (node != null)
                        {
                            node.InnerText = f.Position.ToString(CultureInfo.InvariantCulture);
                            var b = new Bitmap(bmpFileName);
                            var m = new ManagedBitmap(b);
                            b.Dispose();
                            m.AppendToStream(f);
                        }
                    }
                    f.Close();
                }
                _compareDoc.Save(path + "Images.xml");
                string text = File.ReadAllText(path + "Images.xml");
                File.WriteAllText(path + "Images.xml", text.Replace("<FileName", "<Item").Replace("</FileName>", "</Item>"));
                labelStatus.Text = string.Empty;
            }

            if (File.Exists(databaseName))
            {
                labelStatus.Text = Configuration.Settings.Language.VobSubOcr.LoadingImageCompareDatabase;
                labelStatus.Refresh();
                _compareDoc.Load(path + "Images.xml");
                using (var f = new FileStream(databaseName, FileMode.Open))
                {
                    foreach (XmlNode node in _compareDoc.DocumentElement.SelectNodes("Item"))
                    {
                        try
                        {
                            string name = node.InnerText;
                            int pos = Convert.ToInt32(name);
                            bool isItalic = node.Attributes["Italic"] != null;
                            string text = node.Attributes["Text"].InnerText;
                            int expandCount = 0;
                            if (node.Attributes["Expand"] != null)
                            {
                                if (!int.TryParse(node.Attributes["Expand"].InnerText, out expandCount))
                                    expandCount = 0;
                            }
                            f.Position = pos;
                            var mbmp = new ManagedBitmap(f);
                            _compareBitmaps.Add(new CompareItem(mbmp, name, isItalic, expandCount, text));
                        }
                        catch
                        {
                            //MessageBox.Show(node.OuterXml);
                        }
                    }
                }
                labelStatus.Text = string.Empty;
            }
        }
Ejemplo n.º 7
0
 private static int CalculateNumberOfForegroundColors(ManagedBitmap managedBitmap)
 {
     int count = 0;
     for (int y = 0; y < managedBitmap.Height; y++)
     {
         for (int x = 0; x < managedBitmap.Width; x++)
         {
             Color c = managedBitmap.GetPixel(x, y);
             if (c.A > 100 && c.R + c.G + c.B > 200)
                 count++;
         }
     }
     return count;
 }
        /// <summary>
        /// Copies a rectangle from the bitmap to a new bitmap
        /// </summary>
        /// <param name="section">Source rectangle</param>
        /// <returns>Rectangle from current image as new bitmap</returns>
        public ManagedBitmap GetRectangle(Rectangle section)
        {
            ManagedBitmap newRectangle = new ManagedBitmap(section.Width, section.Height);

            int recty = 0;
            for (int y = section.Top; y < section.Top + section.Height; y++)
            {
                int rectx = 0;
                for (int x = section.Left; x < section.Left + section.Width; x++)
                {
                    newRectangle.SetPixel(rectx, recty, this.GetPixel(x, y));
                    rectx++;
                }
                recty++;
            }
            return newRectangle;
        }
Ejemplo n.º 9
0
        private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e)
        {
            checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex];
            string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString();
            string databaseName = _directoryPath + "Images.db";
            string posAsString = GetSelectedFileName();
            Bitmap bmp = null;
            labelExpandCount.Text = string.Empty;
            if (_binOcrDb != null)
            {
                var bob = GetSelectedBinOcrBitmap();
                if (bob != null)
                {
                    bmp = bob.ToOldBitmap();
                    if (bob.ExpandCount > 0)
                    {
                        labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount);
                    }
                }
            }
            else  if (File.Exists(databaseName))
            {
                using (var f = new FileStream(databaseName, FileMode.Open))
                {
                    if (name.Contains("]"))
                        name = name.Substring(name.IndexOf("]") + 1).Trim();
                    f.Position = Convert.ToInt64(name);
                    bmp = new ManagedBitmap(f).ToOldBitmap();
                }
            }

            if (bmp == null)
            {
                bmp = new Bitmap(1,1);
                labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound;
            }
            pictureBox1.Image = bmp;
            pictureBox2.Width = bmp.Width * 2;
            pictureBox2.Height = bmp.Height * 2;
            pictureBox2.Image = bmp;

            if (Additions != null && Additions.Count > 0)
            {
                if (_binOcrDb != null)
                {
                    var bob = GetSelectedBinOcrBitmap();
                    foreach (var a in Additions)
                    {
                        if (bob != null && bob.Text != null && bob.Key == a.Name)
                        {
                            textBoxText.Text = a.Text;
                            checkBoxItalic.Checked = a.Italic;
                            break;
                        }
                    }
                }
                else
                {
                    string target = GetSelectedFileName();
                    foreach (var a in Additions)
                    {
                        if (target.StartsWith(a.Name))
                        {
                            textBoxText.Text = a.Text;
                            break;
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
 internal void DrawImage(ManagedBitmap bmp, Point point)
 {
     for (int y = 0; y < bmp.Height; y++)
     {
         int newY = point.Y + y;
         if (newY >= 0 && newY < Height)
         {
             for (int x = 0; x < bmp.Width; x++)
             {
                 int newX = point.X + x;
                 if (newX >= 0 && newX < Width)
                     this.SetPixel(newX, newY, bmp.GetPixel(x, y));
             }
         }
     }
 }
        private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelImageInfo.Text = string.Empty;
            labelExpandCount.Text = string.Empty;

            if (listBoxInspectItems.SelectedIndex < 0)
                return;

            _selectedCompareNode = null;
            _selectedCompareBinaryOcrBitmap = null;

            pictureBoxInspectItem.Image = _imageSources[listBoxInspectItems.SelectedIndex];
            pictureBoxCompareBitmap.Image = null;
            pictureBoxCompareBitmapDouble.Image = null;

            int index = (listBoxInspectItems.SelectedIndex);
            var match = _matches[index];
            _selectedMatch = match;
            if (!string.IsNullOrEmpty(match.Name))
            {
                Bitmap bitmap = new Bitmap(1, 1);

                if (_binOcrDb != null)
                {
                    bool bobFound = false;
                    foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages)
                    {
                        if (match.Name == bob.Key)
                        {
                            textBoxText.Text = bob.Text;
                            checkBoxItalic.Checked = bob.Italic;
                            _selectedCompareBinaryOcrBitmap = bob;

                            bitmap = bob.ToOldBitmap();
                            pictureBoxCompareBitmap.Image = bitmap;
                            pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                            pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                            pictureBoxCompareBitmapDouble.Image = bitmap;

                            var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex]));
                            if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels)
                            {
                                buttonAddBetterMatch.Enabled = false; // exact match
                            }
                            else
                            {
                                buttonAddBetterMatch.Enabled = true;
                            }

                            bobFound = true;
                            break;
                        }
                    }
                    if (!bobFound)
                    {
                        foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded)
                        {
                            if (match.Name == bob.Key)
                            {
                                textBoxText.Text = bob.Text;
                                checkBoxItalic.Checked = bob.Italic;
                                _selectedCompareBinaryOcrBitmap = bob;

                                bitmap = bob.ToOldBitmap();
                                pictureBoxCompareBitmap.Image = bitmap;
                                pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                                pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                                pictureBoxCompareBitmapDouble.Image = bitmap;
                                buttonAddBetterMatch.Enabled = false; // exact match
                                labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes)
                    {
                        if (node.Attributes["Text"] != null && node.InnerText == match.Name)
                        {
                            string text = node.Attributes["Text"].InnerText;
                            string imageFileName = node.InnerText;
                            imageFileName = Path.Combine(_directoryPath, imageFileName);
                            textBoxText.Text = text;
                            checkBoxItalic.Checked = node.Attributes["Italic"] != null;

                            string databaseName = Path.Combine(_directoryPath, "Images.db");
                            using (var f = new FileStream(databaseName, FileMode.Open))
                            {
                                try
                                {
                                    string name = node.InnerText;
                                    int pos = Convert.ToInt32(name);
                                    f.Position = pos;
                                    ManagedBitmap mbmp = new ManagedBitmap(f);
                                    bitmap = mbmp.ToOldBitmap();
                                    pictureBoxCompareBitmap.Image = bitmap;
                                    pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                                    pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                                    pictureBoxCompareBitmapDouble.Image = bitmap;
                                }
                                catch (Exception exception)
                                {
                                    MessageBox.Show(exception.Message);
                                }
                            }

                            try
                            {
                                labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height);
                            }
                            catch
                            {
                            }

                            _selectedCompareNode = node;
                            break;
                        }
                    }
                }
            }

            if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null)
            {
                buttonUpdate.Enabled = false;
                buttonDelete.Enabled = false;
                buttonAddBetterMatch.Enabled = false;
                textBoxText.Enabled = false;
                textBoxText.Text = string.Empty;
                checkBoxItalic.Enabled = false;
            }
            else
            {
                buttonUpdate.Enabled = true;
                buttonDelete.Enabled = true;
                if (_selectedCompareNode != null)
                    buttonAddBetterMatch.Enabled = true;
                textBoxText.Enabled = true;
                checkBoxItalic.Enabled = true;
            }
        }