Exemplo n.º 1
0
        private static BooruImage FromStream(Stream Stream) //, Action<float> ProgessCallback = null)
        {
            BooruImage img = new BooruImage();

            //if (ProgessCallback != null)
            //    ProgessCallback(0f);
            if (!(Stream is MemoryStream))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = new byte[16 * 1024];
                    while (true)
                    {
                        int read = Stream.Read(buffer, 0, buffer.Length);
                        if (read > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        else
                        {
                            break;
                        }
                    }
                    img._Bytes = ms.ToArray();
                }
            }
            else
            {
                img._Bytes = (Stream as MemoryStream).ToArray();
            }
            //if (ProgessCallback != null)
            //    ProgessCallback(1f);
            return(img);
        }
Exemplo n.º 2
0
        public BooruImage CreateThumbnail(int SideLength, bool Square)
        {
            Size   size       = Bitmap.Size;
            Size   th_size    = new Size(SideLength, SideLength);
            double num        = Math.Min((double)th_size.Width / size.Width, (double)th_size.Height / size.Height);
            Size   resultSize = new Size((int)(size.Width * num), (int)(size.Height * num));

            if (Square)
            {
                //TODO Maybe use floats instead of int division?
                Point resultPoint = new Point((th_size.Width - resultSize.Width) / 2, (th_size.Height - resultSize.Height) / 2);
                using (Bitmap th = new Bitmap(th_size.Width, th_size.Height))
                {
                    using (Graphics g = CreateAAGraphics(th))
                        g.DrawImage(Bitmap, resultPoint.X, resultPoint.Y, resultSize.Width, resultSize.Height);
                    return(BooruImage.FromBitmap(th));
                }
            }
            else
            {
                using (Bitmap th = new Bitmap(resultSize.Width, resultSize.Height))
                {
                    using (Graphics g = CreateAAGraphics(th))
                        g.DrawImage(Bitmap, 0f, 0f, resultSize.Width, resultSize.Height);
                    return(BooruImage.FromBitmap(th));
                }
            }
        }
Exemplo n.º 3
0
 public bool Equals(BooruImage bImg, bool CheckPixels)
 {
     if (bImg != null)
     {
         if (CheckPixels)
         {
             Bitmap B1 = Bitmap;
             Bitmap B2 = bImg.Bitmap;
             if (B1.Size.Equals(B2.Size))
             {
                 BitmapData bd1          = B1.LockBits(new Rectangle(0, 0, B1.Width, B1.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                 BitmapData bd2          = B2.LockBits(new Rectangle(0, 0, B2.Width, B2.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                 bool       is_different = Helper.MemoryCompare(bd1.Scan0, bd2.Scan0, B1.Width * B1.Height * 4);
                 B1.UnlockBits(bd1);
                 B2.UnlockBits(bd2);
                 return(!is_different);
             }
             else
             {
                 using (MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider())
                     if (md5Provider.ComputeHash(Bytes).Equals(md5Provider.ComputeHash(bImg.Bytes)))
                     {
                         return(true);
                     }
             }
         }
     }
     return(false);
 }
Exemplo n.º 4
0
        public bool NotificatePostAdded(ulong ID, BooruPost Post, BooruImage Thumb)
        {
            StringBuilder mailBody = new StringBuilder();

            mailBody.AppendLine("User " + Post.User + " added post " + ID);
            mailBody.AppendLine("https://eggy.hopto.org/booru/post.php?id=" + ID);
            using (var stream = new MemoryStream(Thumb.Bytes))
                using (var att = new Attachment(stream, "thumb.jpg", "image/jpeg"))
                    return(SendMail("Post " + ID + " added", mailBody.ToString(), att));
        }
Exemplo n.º 5
0
        private static ulong AddPost(Stream str, BooruPost Post, BooruTagList Tags, BooruImage Image)
        {
            ulong postID = 0;

            Request(str, RequestCode.Add_Post, (rw) =>
            {
                Post.ToWriter(rw);
                Tags.ToWriter(rw);
                Image.ToWriter(rw);
            }, (rw) => { postID = rw.ReadULong(); });
            return(postID);
        }
Exemplo n.º 6
0
 public static BooruImage FromURL(string URL)
 {
     if (Helper.CheckURL(URL))
     {
         try
         {
             using (WebClient client = new WebClient())
                 return(BooruImage.FromBytes(client.DownloadData(URL)));
         }
         catch { }
     }
     return(null);
 }
Exemplo n.º 7
0
 public void Dispose()
 {
     if (Image != null)
     {
         Image.Dispose();
         Image = null;
     }
     if (Thumbnail != null)
     {
         Thumbnail.Dispose();
         Thumbnail = null;
     }
 }
Exemplo n.º 8
0
        /*
         * public void ToWriter(BinaryWriter Writer, Action<float> ProgressCallback)
         * {
         *  Writer.Write(Bytes.Length);
         *  if (ProgressCallback != null)
         *  {
         *      int chunkSize = 1024 * 5;
         *      int chunkCount = Bytes.Length / chunkSize;
         *      for (int i = 0; i < chunkCount; i++)
         *      {
         *          Writer.Write(Bytes, i * chunkSize, chunkSize);
         *          ProgressCallback(i * (float)chunkSize / Bytes.Length);
         *      }
         *      Writer.Write(Bytes, chunkCount * chunkSize, Bytes.Length % chunkSize);
         *      ProgressCallback(1f);
         *  }
         *  else Writer.Write(Bytes);
         * }
         */

        public static BooruImage FromReader(ReaderWriter Reader)
        {
            return(BooruImage.FromBytes(Reader.ReadBytes()));
        }
Exemplo n.º 9
0
        private static void Process(Stream str, Options commonOptions)
        {
            if (!string.IsNullOrWhiteSpace(commonOptions.Username))
            {
                Request(str, RequestCode.Login, (rw) =>
                {
                    rw.Write(commonOptions.Username, true);
                    rw.Write(commonOptions.Password ?? string.Empty, true);
                }, (rw) => { });
            }

            Type oType = commonOptions.GetType();

            if (oType == typeof(AddOptions))
            {
                var options = (AddOptions)commonOptions;
                Console.Write("Loading image... ");
                using (var post = new BooruPost())
                    using (var image = BooruImage.FromFile(options.ImagePath))
                    {
                        Console.WriteLine("OK");
                        if (options.Tags != null)
                        {
                            foreach (var tag in options.Tags.Split(new char[1] {
                                ' '
                            }, StringSplitOptions.RemoveEmptyEntries))
                            {
                                post.Tags.Add(new BooruTag(tag));
                            }
                        }
                        if (options.Source != null)
                        {
                            post.Source = options.Source;
                        }
                        if (options.Description != null)
                        {
                            post.Description = options.Description;
                        }
                        post.Rating = (byte)options.Rating;
                        if (options.Private.HasValue)
                        {
                            post.Private = options.Private.Value;
                        }
                        Console.Write("Adding post... ");
                        ulong id = AddPost(str, post, post.Tags, image);
                        Console.WriteLine(id);
                    }
            }
            else if (oType == typeof(AddUrlOptions))
            {
                var options  = (AddUrlOptions)commonOptions;
                var apiPosts = BooruAPI.SearchPostsPerURL(options.URL);
                if (apiPosts.Count < 1)
                {
                    throw new Exception("No post to import detected");
                }
                else if (apiPosts.Count > 1)
                {
                    Console.WriteLine("Multiple posts found, importing only the first one");
                    for (int i = 1; i < apiPosts.Count; i++)
                    {
                        apiPosts[i].Dispose();
                    }
                }
                var apiPost = apiPosts[0];
                if (options.CustomImagePath == null)
                {
                    Console.Write("Downloading image... ");
                    apiPost.DownloadImage();
                }
                else
                {
                    Console.Write("Loading image... ");
                    apiPost.Image = BooruImage.FromFile(options.CustomImagePath);
                }
                Console.WriteLine("OK");
                if (!options.AllTags)
                {
                    string[] allTags = null;
                    Request(str, RequestCode.Get_AllTags, (rw) => { }, (rw) =>
                    {
                        uint count = rw.ReadUInt();
                        allTags    = new string[count];
                        for (int a = 0; a < count; a++)
                        {
                            allTags[a] = rw.ReadString();
                        }
                    });
                    for (int a = apiPost.Tags.Count - 1; !(a < 0); a--)
                    {
                        if (!allTags.Contains(apiPost.Tags[a].Tag))
                        {
                            apiPost.Tags.RemoveAt(a);
                        }
                    }
                }
                if (options.Tags != null)
                {
                    options.Tags = options.Tags.ToLower();
                    if (options.TagsNoDelta)
                    {
                        string[] parts = options.Tags.Split(new char[1] {
                            ' '
                        }, StringSplitOptions.RemoveEmptyEntries);
                        apiPost.Tags.Clear();
                        foreach (string part in parts)
                        {
                            apiPost.Tags.Add(new BooruTag(part));
                        }
                    }
                    else
                    {
                        TagDelta(ref apiPost.Tags, options.Tags);
                    }
                }
                //apiPost.Description = "Imported from " + apiPost.APIName;
                if (options.Description != null)
                {
                    apiPost.Description = options.Description;
                }
                else
                {
                    apiPost.Description = string.Empty;   //needed?
                }
                apiPost.Rating = (byte)options.Rating;
                if (options.Private.HasValue)
                {
                    apiPost.Private = options.Private.Value;
                }
                Console.Write("Importing post... ");
                ulong id = AddPost(str, apiPost, apiPost.Tags, apiPost.Image);
                Console.WriteLine(id);
            }
            else if (oType == typeof(DelOptions))
            {
                var options = (DelOptions)commonOptions;
                Request(str, RequestCode.Delete_Post, (rw) => rw.Write(options.ID), (rw) => { });
            }
            else if (oType == typeof(GetOptions))
            {
                var options = (GetOptions)commonOptions;
                using (var post = GetPost(str, options.ID))
                {
                    Console.WriteLine("User        " + post.User);
                    Console.WriteLine("Private     " + (post.Private ? "yes" : "no"));
                    Console.WriteLine("Source      " + post.Source ?? string.Empty);
                    Console.WriteLine("Description " + post.Description ?? string.Empty);
                    Console.WriteLine("Rating      " + post.Rating);
                    Console.WriteLine("Size        {0}x{1}", post.Width, post.Height);
                    Console.WriteLine("Date        {0}", post.CreationDate);
                    Console.WriteLine("ViewCount   " + post.ViewCount);
                    Console.WriteLine("EditCount   " + post.EditCount);
                    Console.WriteLine("Score       " + post.Score);
                    Console.WriteLine();
                    Console.WriteLine(BooruTagListToString(post.Tags, !options.NoColor));
                }
            }
            else if (oType == typeof(EditOptions))
            {
                var options = (EditOptions)commonOptions;
                using (var post = GetPost(str, options.ID))
                {
                    post.EditCount += 1;
                    if (options.Description != null)
                    {
                        post.Description = options.Description;
                    }
                    if (options.Private.HasValue)
                    {
                        post.Private = options.Private.Value;
                    }
                    if (!(options.Rating < 0) && options.Rating < byte.MaxValue)
                    {
                        post.Rating = (byte)options.Rating;
                    }
                    if (options.Source != null)
                    {
                        post.Source = options.Source;
                    }
                    if (options.Tags != null)
                    {
                        options.Tags = options.Tags.ToLower();
                        if (options.TagsNoDelta)
                        {
                            string[] parts = options.Tags.Split(new char[1] {
                                ' '
                            }, StringSplitOptions.RemoveEmptyEntries);
                            post.Tags.Clear();
                            foreach (string part in parts)
                            {
                                post.Tags.Add(new BooruTag(part));
                            }
                        }
                        else
                        {
                            TagDelta(ref post.Tags, options.Tags);
                        }
                    }
                    Request(str, RequestCode.Edit_Post, (rw) =>
                    {
                        post.ToWriter(rw);
                        post.Tags.ToWriter(rw);
                    }, (rw) => { });
                }
            }
            else if (oType == typeof(EditImgOptions))
            {
                EditImgOptions options = (EditImgOptions)commonOptions;
                string         path    = Helper.GetTempFile();
                BooruImage     img     = null;
                try
                {
                    Request(str, RequestCode.Get_Image, (rw) => rw.Write(options.ID), (rw) => { img = BooruImage.FromReader(rw); });
                    img.Save(ref path, true);
                }
                finally { img.Dispose(); }
                var     psi  = new ProcessStartInfo(options.Editor, path);
                Process tool = new Process()
                {
                    StartInfo = psi
                };
                tool.Start();
                Console.Write("Waiting for image editor to exit...");
                tool.WaitForExit();
                using (BooruImage eImg = BooruImage.FromFile(path))
                    Request(str, RequestCode.Edit_Image, (rw) =>
                    {
                        rw.Write(options.ID);
                        eImg.ToWriter(rw);
                    }, (rw) => { });
            }
            else if (oType == typeof(GetImgOptions))
            {
                GetImgOptions options = (GetImgOptions)commonOptions;
                BooruImage    img     = null;
                try
                {
                    Request(str, RequestCode.Get_Image, (rw) => rw.Write(options.ID), (rw) => { img = BooruImage.FromReader(rw); });
                    string path = options.Path;
                    img.Save(ref path, true);
                }
                finally { img.Dispose(); }
            }
            else if (oType == typeof(SetImgOptions))
            {
                SetImgOptions options = (SetImgOptions)commonOptions;
                using (BooruImage img = BooruImage.FromFile(options.Path))
                    Request(str, RequestCode.Edit_Image, (rw) =>
                    {
                        rw.Write(options.ID);
                        img.ToWriter(rw);
                    }, (rw) => { });
            }
            else if (oType == typeof(SetImgUrlOptions))
            {
                SetImgUrlOptions options = (SetImgUrlOptions)commonOptions;
                Console.Write("Downloading image... ");
                using (BooruImage img = BooruImage.FromURL(options.URL))
                {
                    Console.WriteLine("OK");
                    Request(str, RequestCode.Edit_Image, (rw) =>
                    {
                        rw.Write(options.ID);
                        img.ToWriter(rw);
                    }, (rw) => { });
                }
            }
            else if (oType == typeof(GCOptions))
            {
                Request(str, RequestCode.Start_GC, (rw) => { }, (rw) => { });
            }
            else
            {
                throw new Exception("Unknown options type");
            }
            //Logout
            str.WriteByte(0xFF);
            str.WriteByte(0xFF);
        }