- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 888
 
Pixel remapping/Swizzling #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ecbe356
              
                wip - Add basic structure for the swizzler
              
              
                Evangelink 3dc2209
              
                Merge branch 'master' into pixel-swizzle
              
              
                JimBobSquarePants 6077f96
              
                Merge branch 'master' into pixel-swizzle
              
              
                Evangelink 2e5a86a
              
                More progress toward a working swizzler
              
              
                Evangelink d16149a
              
                Fix unit test
              
              
                Evangelink 5f3bb97
              
                Update integration tests
              
              
                Evangelink f1a799e
              
                Apply suggested change for test
              
              
                 407fc07
              
                Address review comments
              
              
                Evangelink 7c5f39d
              
                Forgot to commit tests
              
              
                Evangelink File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  src/ImageSharp/Processing/Extensions/Transforms/SwizzleExtensions.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
| 
     | 
||
| using SixLabors.ImageSharp.Processing.Processors.Transforms; | ||
| 
     | 
||
| namespace SixLabors.ImageSharp.Processing.Extensions.Transforms | ||
| { | ||
| /// <summary> | ||
| /// Defines extensions that allow the application of swizzle operations on an <see cref="Image"/> | ||
| /// </summary> | ||
| public static class SwizzleExtensions | ||
| { | ||
| /// <summary> | ||
| /// Swizzles an image. | ||
| /// </summary> | ||
| /// <param name="source">The image to swizzle.</param> | ||
| /// <param name="swizzler">The swizzler function.</param> | ||
| /// <typeparam name="TSwizzler">The swizzler function type.</typeparam> | ||
| /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns> | ||
| public static IImageProcessingContext Swizzle<TSwizzler>(this IImageProcessingContext source, TSwizzler swizzler) | ||
| where TSwizzler : struct, ISwizzler | ||
| => source.ApplyProcessor(new SwizzleProcessor<TSwizzler>(swizzler)); | ||
| } | ||
| } | 
        
          
          
            18 changes: 18 additions & 0 deletions
          
          18 
        
  src/ImageSharp/Processing/Processors/Transforms/ISwizzler.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
| 
     | 
||
| namespace SixLabors.ImageSharp.Processing.Processors.Transforms | ||
| { | ||
| /// <summary> | ||
| /// Encapsulate an algorithm to swizzle pixels in an image. | ||
| /// </summary> | ||
| public interface ISwizzler | ||
| { | ||
| /// <summary> | ||
| /// Applies the swizzle transformation to a given point. | ||
| /// </summary> | ||
| /// <param name="point">Point to transform.</param> | ||
| /// <returns>Transformed point.</returns> | ||
| Point Transform(Point point); | ||
| } | ||
| } | 
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler,TPixel}.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
| 
     | 
||
| using SixLabors.ImageSharp.PixelFormats; | ||
| 
     | 
||
| namespace SixLabors.ImageSharp.Processing.Processors.Transforms | ||
| { | ||
| internal class SwizzleProcessor<TSwizzler, TPixel> : ImageProcessor<TPixel> | ||
| where TSwizzler : struct, ISwizzler | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| private readonly TSwizzler swizzler; | ||
| 
     | 
||
| public SwizzleProcessor(Configuration configuration, TSwizzler swizzler, Image<TPixel> source, Rectangle sourceRectangle) | ||
| : base(configuration, source, sourceRectangle) | ||
| { | ||
| this.swizzler = swizzler; | ||
| } | ||
| 
     | 
||
| /// <inheritdoc/> | ||
| protected override void OnFrameApply(ImageFrame<TPixel> source) | ||
| { | ||
| for (int y = 0; y < source.Height; y++) | ||
| { | ||
| var pixelRowSpan = source.GetPixelRowSpan(y); | ||
| for (int x = 0; x < source.Width; x++) | ||
| { | ||
| var newPoint = this.swizzler.Transform(new Point(x, y)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
        
          
          
            32 changes: 32 additions & 0 deletions
          
          32 
        
  src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler}.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
| 
     | 
||
| using SixLabors.ImageSharp.PixelFormats; | ||
| 
     | 
||
| namespace SixLabors.ImageSharp.Processing.Processors.Transforms | ||
| { | ||
| /// <summary> | ||
| /// Defines a swizzle operation on an image. | ||
| /// </summary> | ||
| /// <typeparam name="TSwizzler">The swizzle function type.</typeparam> | ||
| public sealed class SwizzleProcessor<TSwizzler> : IImageProcessor | ||
| where TSwizzler : struct, ISwizzler | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="SwizzleProcessor{TSwizzler}"/> class. | ||
| /// </summary> | ||
| /// <param name="swizzler">The swizzler operation.</param> | ||
| public SwizzleProcessor(TSwizzler swizzler) | ||
| => this.Swizzler = swizzler; | ||
| 
     | 
||
| /// <summary> | ||
| /// Gets the swizzler operation. | ||
| /// </summary> | ||
| public TSwizzler Swizzler { get; } | ||
| 
     | 
||
| /// <inheritdoc /> | ||
| public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle) | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| => new SwizzleProcessor<TSwizzler, TPixel>(configuration, this, source, sourceRectangle); | ||
| } | ||
| } | 
        
          
          
            31 changes: 31 additions & 0 deletions
          
          31 
        
  tests/ImageSharp.Tests/Processing/Transforms/SwizzleTests.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
| 
     | 
||
| using SixLabors.ImageSharp.Processing.Extensions.Transforms; | ||
| using SixLabors.ImageSharp.Processing.Processors.Transforms; | ||
| using Xunit; | ||
| 
     | 
||
| namespace SixLabors.ImageSharp.Tests.Processing.Transforms | ||
| { | ||
| public class SwizzleTests : BaseImageOperationsExtensionTest | ||
| { | ||
| private struct InvertXAndYSwizzler : ISwizzler | ||
| { | ||
| public Point Transform(Point point) => new Point(point.Y, point.X); | ||
| } | ||
| 
     | 
||
| [Fact] | ||
| public void RotateDegreesFloatRotateProcessorWithAnglesSet() | ||
| { | ||
| this.operations.Swizzle(default(InvertXAndYSwizzler)); | ||
| SwizzleProcessor<InvertXAndYSwizzler> processor = this.Verify<SwizzleProcessor<InvertXAndYSwizzler>>(); | ||
| 
     | 
||
| // assert that pixels have been changed | ||
| 
     | 
||
| this.operations.Swizzle(default(InvertXAndYSwizzler)); | ||
| SwizzleProcessor<InvertXAndYSwizzler> processor2 = this.Verify<SwizzleProcessor<InvertXAndYSwizzler>>(); | ||
| 
     | 
||
| // assert that pixels have been changed (i.e. back to original) | ||
| } | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.