示例#1
0
 /// <summary>
 /// Gets the CDN URI of the given achievement's icon.
 /// </summary>
 /// <param name="application">The application.</param>
 /// <param name="achievementID">The ID of the achievement.</param>
 /// <param name="iconHash">The image hash of the icon.</param>
 /// <param name="imageFormat">The requested image format.</param>
 /// <param name="imageSize">The requested image size. May be any power of two between 16 and 4096.</param>
 /// <returns>A result which may or may not have succeeded.</returns>
 public static Result <Uri> GetAchievementIconUrl
 (
     IApplication application,
     Snowflake achievementID,
     IImageHash iconHash,
     Optional <CDNImageFormat> imageFormat = default,
     Optional <ushort> imageSize           = default
 ) => GetAchievementIconUrl(application.ID, achievementID, iconHash, imageFormat, imageSize);
示例#2
0
 public DuplicateMediaItemPipeline(DirectoryInfo inputDirectoryInfo, DirectoryInfo outputDirectoryInfo,
                                   DirectoryInfo recycleBin, IImageHash imageHasher)
 {
     this.inputDirectoryInfo  = inputDirectoryInfo;
     this.outputDirectoryInfo = outputDirectoryInfo;
     this.recycleBin          = recycleBin;
     this.imageHasher         = imageHasher;
 }
示例#3
0
 public static ulong CalculateHash(IImageHash imageHasher, string fullName)
 {
     using (var stream = File.OpenRead(fullName))
         using (var image = Image.Load(stream))
         {
             return(imageHasher.Hash(image));
         }
 }
        public void NullArgumentShouldThrowArgumentNullExceptionTest(bool hashImplIsNull, bool streamIsNull)
        {
            // arrange
            IImageHash imageHashImplementation = hashImplIsNull ? null : A.Dummy <IImageHash>();
            Stream     stream = streamIsNull ? null : new MemoryStream();

            // act
            Action act = () => Sut.Hash(imageHashImplementation, stream);

            // assert
            act.Should().Throw <ArgumentNullException>();
        }
        public FullMediaItemPipeline(DirectoryInfo inputDirectoryInfo, DirectoryInfo outputDirectoryInfo,
                                     DirectoryInfo recycleBin)
        {
            this.db = new LiteDatabase(Path.Combine(outputDirectoryInfo.FullName, "mediaItems.db"));
            this.mediaItemCollection = this.db.GetCollection <MediaItemQuickScanInfo>();
            this.mediaItemCollection.EnsureIndex(info => info.FullName);
            this.mediaItemCollection.EnsureIndex(info => info.Hash);

            this.inputDirectoryInfo  = inputDirectoryInfo;
            this.outputDirectoryInfo = outputDirectoryInfo;
            this.recycleBin          = recycleBin;
            this.imageHasher         = new PerceptualHash();
        }
        /// <summary>Calculate the hash of the image (stream) using the hashImplementation.</summary>
        /// <param name="hashImplementation">HashImplementation to calculate the hash.</param>
        /// <param name="stream">Stream should 'contain' raw image data.</param>
        /// <returns>hash value.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="hashImplementation"/> or <paramref name="stream"/> is <c>null</c>.</exception>
        /// <exception cref="SixLabors.ImageSharp.UnknownImageFormatException">Thrown when stream content cannot be loaded as an image.</exception>
        public static ulong Hash(this IImageHash hashImplementation, Stream stream)
        {
            if (hashImplementation == null)
            {
                throw new ArgumentNullException(nameof(hashImplementation));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var image = Image.Load <Rgba32>(stream))
                return(hashImplementation.Hash(image));
        }
        private void ComputeAndSaveImageHash(IImageHash hashAlgorithm)
        {
            ulong imageHash  = ComputeHashOfImageFile(_options.ImageFile, hashAlgorithm);
            var   hashString = $"{imageHash:X16}";

            if (String.IsNullOrEmpty(_options.OutputFile))
            {
                Console.Out.WriteLine(hashString);
            }
            else
            {
                File.WriteAllText(_options.OutputFile, hashString, Encoding.ASCII);
            }
        }
示例#8
0
 private static void ComputeHashesIfNeeded(IImageHash hashAlgorithm, string file1, string file2, ref ulong hash1, ref ulong hash2)
 {
     if (file1 != null)
     {
         using (var stream = File.OpenRead(file1))
         {
             hash1 = hashAlgorithm.Hash(stream);
         }
     }
     if (file2 != null)
     {
         using (var stream = File.OpenRead(file2))
         {
             hash2 = hashAlgorithm.Hash(stream);
         }
     }
 }
示例#9
0
        /// <summary>
        /// Gets the CDN URI of the given achievement's icon.
        /// </summary>
        /// <param name="applicationID">The ID of the application.</param>
        /// <param name="achievementID">The ID of the achievement.</param>
        /// <param name="iconHash">The image hash of the achievement's icon.</param>
        /// <param name="imageFormat">The requested image format.</param>
        /// <param name="imageSize">The requested image size. May be any power of two between 16 and 4096.</param>
        /// <returns>A result which may or may not have succeeded.</returns>
        public static Result <Uri> GetAchievementIconUrl
        (
            Snowflake applicationID,
            Snowflake achievementID,
            IImageHash iconHash,
            Optional <CDNImageFormat> imageFormat = default,
            Optional <ushort> imageSize           = default
        )
        {
            var formatValidation = ValidateOrDefaultImageFormat
                                   (
                imageFormat,
                CDNImageFormat.PNG,
                CDNImageFormat.JPEG,
                CDNImageFormat.WebP
                                   );

            if (!formatValidation.IsSuccess)
            {
                return(Result <Uri> .FromError(formatValidation));
            }

            imageFormat = formatValidation.Entity;

            var checkImageSize = CheckImageSize(imageSize);

            if (!checkImageSize.IsSuccess)
            {
                return(Result <Uri> .FromError(checkImageSize));
            }

            var ub = new UriBuilder(Constants.CDNBaseURL)
            {
                Path = $"app-assets/{applicationID}/achievements/{achievementID}/icons/{iconHash.Value}.{imageFormat.Value.ToString().ToLowerInvariant()}"
            };

            if (imageSize.HasValue)
            {
                ub.Query = $"size={imageSize.Value}";
            }

            return(ub.Uri);
        }
示例#10
0
        /// <summary>
        /// Gets the CDN URI of the given user's avatar.
        /// </summary>
        /// <param name="userID">The ID of the team.</param>
        /// <param name="avatarHash">The image hash of the user's avatar.</param>
        /// <param name="imageFormat">The requested image format.</param>
        /// <param name="imageSize">The requested image size. May be any power of two between 16 and 4096.</param>
        /// <returns>A result which may or may not have succeeded.</returns>
        public static Result <Uri> GetUserAvatarUrl
        (
            Snowflake userID,
            IImageHash avatarHash,
            Optional <CDNImageFormat> imageFormat = default,
            Optional <ushort> imageSize           = default
        )
        {
            var formatValidation = ValidateOrDefaultImageFormat
                                   (
                imageFormat,
                CDNImageFormat.PNG,
                CDNImageFormat.JPEG,
                CDNImageFormat.WebP,
                CDNImageFormat.GIF
                                   );

            if (!formatValidation.IsSuccess)
            {
                return(Result <Uri> .FromError(formatValidation));
            }

            imageFormat = formatValidation.Entity;

            var checkImageSize = CheckImageSize(imageSize);

            if (!checkImageSize.IsSuccess)
            {
                return(Result <Uri> .FromError(checkImageSize));
            }

            var ub = new UriBuilder(Constants.CDNBaseURL)
            {
                Path = $"avatars/{userID}/{avatarHash.Value}.{imageFormat.Value.ToString().ToLowerInvariant()}"
            };

            if (imageSize.HasValue)
            {
                ub.Query = $"size={imageSize.Value}";
            }

            return(ub.Uri);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageSticker"/> class.
 /// </summary>
 /// <param name="id">The ID of the sticker.</param>
 /// <param name="packID">The ID of the sticker pack.</param>
 /// <param name="name">The name of the sticker.</param>
 /// <param name="description">The description of the sticker.</param>
 /// <param name="tags">The tags associated with the sticker.</param>
 /// <param name="asset">The asset hash of the sticker.</param>
 /// <param name="previewAsset">The preview asset hash of the sticker.</param>
 /// <param name="formatType">The format type of the sticker.</param>
 public MessageSticker
 (
     Snowflake id,
     Snowflake packID,
     string name,
     string description,
     Optional <IReadOnlyList <string> > tags,
     IImageHash asset,
     IImageHash previewAsset,
     MessageStickerFormatType formatType
 )
 {
     this.ID           = id;
     this.PackID       = packID;
     this.Name         = name;
     this.Description  = description;
     this.Tags         = tags;
     this.Asset        = asset;
     this.PreviewAsset = previewAsset;
     this.FormatType   = formatType;
 }
示例#12
0
 private ulong ComputeHashOfImageFile(string path, IImageHash hashAlgorithm)
 {
     using (var image = (Image <Rgba32>)Image.Load(path))
     {
         // check whether we have R=G=B=0 (ie, black) for all pixels, presumably with A varying.
         var allBlack = true;
         for (int x = 0; allBlack && x < image.Width; ++x)
         {
             for (int y = 0; allBlack && y < image.Height; ++y)
             {
                 var pixel = image[x, y];
                 if (pixel.R != 0 || pixel.G != 0 || pixel.B != 0)
                 {
                     allBlack = false;
                 }
             }
         }
         if (allBlack)
         {
             // If the pixels all end up the same because A never changes, we're no worse off
             // because the hash result will still be all zero bits.
             for (int x = 0; x < image.Width; ++x)
             {
                 for (int y = 0; y < image.Height; ++y)
                 {
                     var pixel = image[x, y];
                     pixel.R     = pixel.A;
                     pixel.G     = pixel.A;
                     pixel.B     = pixel.A;
                     image[x, y] = pixel;
                 }
             }
         }
         return(hashAlgorithm.Hash(image));
     }
 }
示例#13
0
 public FileRenamerPipeline(DirectoryInfo inputDirectoryInfo, DirectoryInfo outputDirectoryInfo)
 {
     this.inputDirectoryInfo  = inputDirectoryInfo;
     this.outputDirectoryInfo = outputDirectoryInfo;
     this.imageHasher         = new AverageHash();
 }
 public ImageHashExtensionsTest()
 {
     hashAlgorithm = A.Fake <IImageHash>();
 }
示例#15
0
        public static IPropagatorBlock <PhotoContext, PhotoContext> CreateHashCalculator(IImageHash imageHasher)
        {
            var output = new BufferBlock <PhotoContext>();

            var input = new ActionBlock <PhotoContext>(
                context =>
            {
                try
                {
                    context.Hash = CalculateHash(imageHasher, context.Source.FullName);
                    Log.Information($"Hash: {context.Source.Name} = {context.Hash}");
                    output.Post(context);
                }
                catch (Exception)
                {
                    // ignore for the moment: 80/20
                }
            },
                new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 8
            });

            input.Completion.ContinueWith(task => output.Complete());

            return(DataflowBlock.Encapsulate(input, output));
        }