Skip to content

Commit

Permalink
Merge pull request #4 from JimBobSquarePants/main
Browse files Browse the repository at this point in the history
More efficient frame copying.
  • Loading branch information
khalidabuhakmeh committed Jul 22, 2021
2 parents 5012682 + da8aa37 commit c6d2c21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions Program.fs
Expand Up @@ -9,17 +9,15 @@ type options = {
[<Option('r', "recursive", HelpText = "Recursively scan directories from root directory", Default=false)>] recursive: bool
}

let getFrameFromGif (path: string) : byte[] =
let gif = Image.Load(path)
let frame = gif.Frames.RootFrame
use image = new Image<Rgba32>(frame.Width, frame.Height)
let source = frame :?> ImageFrame<Rgba32>
for y in 0..frame.Height-1 do
for x in 0..frame.Width-1 do
image.[x,y] <- source.[x,y]
use ms = new MemoryStream()
let getFrameFromGif (path: string) =
use gif = Image.Load(path)
use image = new Image<Rgba32>(gif.Width, gif.Height)
image.Frames.AddFrame(gif.Frames.RootFrame)|> ignore
image.Frames.RemoveFrame(0)
let ms = new MemoryStream()
image.SaveAsPng(ms)
ms.ToArray()
ms.Seek(0L, SeekOrigin.Begin) |> ignore
ms

let processFiles options : Unit =
match Directory.Exists options.directory with
Expand All @@ -29,11 +27,12 @@ let processFiles options : Unit =
let progress = AnsiConsole.Status()
let processEachFile (ctx:StatusContext) =
for path in files do
let bytes = getFrameFromGif path
let filename = Path.GetFileNameWithoutExtension path
let target = $"%s{filename}.png"
use destinationStream = File.Create target
use frameStream = getFrameFromGif path
AnsiConsole.MarkupLine $"[green]:check_mark: Processing %s{Path.GetFileName path} -> %s{target}[/]"
File.WriteAllBytes(target, bytes)
frameStream.CopyTo(destinationStream)
progress.Start($"processing {files.Length} GIFs", processEachFile)
AnsiConsole.MarkupLine($"[green]:glowing_star: { files.Length} GIFs processed in {options.directory}[/]")
| false ->
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -28,7 +28,7 @@ Copyright (C) 2021 FreezeFrame

## Licenses For ImageSharp

- This tool uses [ImageSharp](https://sixlabors.com/products/imagesharp/), which requires a license for commercial use.
- This tool uses [ImageSharp](https://sixlabors.com/products/imagesharp/), which requires a license for commercial support.

## License

Expand All @@ -39,4 +39,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit c6d2c21

Please sign in to comment.