Skip to content

Commit

Permalink
fix at leasst keys gif stuff is still bonkers
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Nov 17, 2024
1 parent 064ee0d commit 407f664
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 136 deletions.
61 changes: 0 additions & 61 deletions CommonControls/FocusExtension.cs

This file was deleted.

10 changes: 10 additions & 0 deletions CommonControls/GlobalKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CommonControls
Expand Down Expand Up @@ -87,6 +88,14 @@ private static void OnPreviewKeyDown(object sender, KeyEventArgs e)

if (sender is not UIElement element) return;

// Get the currently focused element using Keyboard.FocusedElement
var focusedElement = Keyboard.FocusedElement;

if (focusedElement is TextBox or RichTextBox)
{
return; // Skip key handling if focus is inside a TextBox or RichTextBox
}

// Retrieve the dictionary of key-command bindings for this element
var bindings = GetCommandBindings(element);

Expand All @@ -97,5 +106,6 @@ private static void OnPreviewKeyDown(object sender, KeyEventArgs e)
command.Execute(null);
e.Handled = true;
}

}
}
7 changes: 5 additions & 2 deletions Imaging/ImageGifHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using System.Windows.Media.Imaging;
using ExtendedSystemObjects;
using FileHandler;
using Size = System.Drawing.Size;

namespace Imaging
{
Expand All @@ -49,7 +48,11 @@ public static ImageGifInfo GetImageInfo(string path)
Trace.WriteLine(ex.Message);
//TODo fill up
}

catch (InvalidDataException ex)
{
Trace.WriteLine(ex.Message);
//TODo fill up
}

return info;
}
Expand Down
155 changes: 109 additions & 46 deletions Imaging/ImageGifMetadataExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,62 +55,121 @@ internal static ImageGifInfo ExtractGifMetadata(string filePath)
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
var blockId = reader.ReadByte();
switch (blockId)

// Skip padding or sub-block terminator
if (blockId == 0x00)
{
Console.WriteLine("Skipping padding or sub-block terminator (0x00)");
continue;
}

Console.WriteLine($"Processing block: 0x{blockId:X2}");

byte packed;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
case 0x21: // Extension Introducer
var extensionLabel = reader.ReadByte();
if (extensionLabel == 0xFF) // Application Extension
{
var blockSize = reader.ReadByte();
var appIdentifier = new string(reader.ReadChars(8));
var appAuthCode = new string(reader.ReadChars(3));

if (appIdentifier == "NETSCAPE")
blockId = reader.ReadByte();

// Skip padding or sub-block terminator
if (blockId == 0x00)
{
Console.WriteLine("Skipping padding or sub-block terminator (0x00)");
continue;
}

Console.WriteLine($"Processing block: 0x{blockId:X2}");

switch (blockId)
{
case 0x21: // Extension Introducer
var extensionLabel = reader.ReadByte();
if (extensionLabel == 0xFF) // Application Extension
{
var subBlockSize = reader.ReadByte();
var loopFlag = reader.ReadByte();
metadata.LoopCount = reader.ReadInt16();
var blockSize = reader.ReadByte(); // Block Size
var appIdentifier = new string(reader.ReadChars(8));
var appAuthCode = new string(reader.ReadChars(3));

if (appIdentifier == "NETSCAPE")
{
var subBlockSize = reader.ReadByte();
var loopFlag = reader.ReadByte();
metadata.LoopCount = reader.ReadInt16();
}
else
{
SkipExtensionBlocks(reader);
}
}
else if (extensionLabel == 0xF9) // Graphics Control Extension
{
reader.BaseStream.Seek(1, SeekOrigin.Current); // Skip Block Size
packed = reader.ReadByte();
var delay = reader.ReadInt16();
lastFrameDelay = delay / 100.0;
reader.BaseStream.Seek(1, SeekOrigin.Current); // Skip Transparent Color Index
}
else
{
SkipExtensionBlocks(reader);
}
}
else if (extensionLabel == 0xF9) // Graphics Control Extension
{
reader.BaseStream.Seek(1, SeekOrigin.Current); // Skip Block Size
var packed = reader.ReadByte(); // Packed Fields (ignored for now)
var delay = reader.ReadInt16(); // Delay time in hundredths of a second
lastFrameDelay = delay / 100.0; // Convert to seconds
reader.BaseStream.Seek(1, SeekOrigin.Current); // Skip Transparent Color Index
}
else // Other extensions
{
SkipExtensionBlocks(reader);
}

break;

case 0x2C: // Image Descriptor
metadata.Frames.Add(new FrameInfo
{
Description = "Image Frame",
DelayTime = lastFrameDelay
});
reader.BaseStream.Seek(9, SeekOrigin.Current); // Skip image descriptor
break;

case 0x3B: // Trailer
return metadata;

default:
throw new InvalidDataException($"Unknown block: 0x{blockId:X2}");
break;

case 0x2C: // Image Descriptor
metadata.Frames.Add(new FrameInfo
{
Description = "Image Frame",
DelayTime = lastFrameDelay
});

// Handle Image Descriptor and Local Color Table
reader.BaseStream.Seek(9, SeekOrigin.Current);
packed = reader.ReadByte();
if ((packed & 0x80) != 0) // Local Color Table present
{
var tableSize = 3 * (1 << ((packed & 0x07) + 1));
reader.BaseStream.Seek(tableSize, SeekOrigin.Current);
}

// Skip image data sub-blocks
while (true)
{
var subBlockSize = reader.ReadByte();
if (subBlockSize == 0x00) break;
reader.BaseStream.Seek(subBlockSize, SeekOrigin.Current);
}

break;

case 0x3B: // Trailer
Console.WriteLine("GIF Trailer found, parsing complete.");
return metadata;

default:
Console.WriteLine($"Unknown block encountered: 0x{blockId:X2}. Skipping.");
SkipUnknownBlock(reader, blockId);
break;
}
}


}


return metadata;
}

private static void SkipUnknownBlock(BinaryReader reader, byte blockId)
{
Console.WriteLine($"Skipping unknown block: 0x{blockId:X2}");
while (true)
{
var subBlockSize = reader.ReadByte();
if (subBlockSize == 0x00) break; // End of sub-blocks
reader.BaseStream.Seek(subBlockSize, SeekOrigin.Current);
}
}


/// <summary>
/// Skips the extension blocks.
/// </summary>
Expand All @@ -119,10 +178,14 @@ private static void SkipExtensionBlocks(BinaryReader reader)
{
while (true)
{
var subBlockSize = reader.ReadByte();
if (subBlockSize == 0) break; // Block terminator
reader.BaseStream.Seek(subBlockSize, SeekOrigin.Current); // Skip the sub-block
var blockSize = reader.ReadByte();
if (blockSize == 0x00) // Terminator
break;

Console.WriteLine($"Skipping extension block of size: {blockSize}");
reader.BaseStream.Seek(blockSize, SeekOrigin.Current);
}
}

}
}
1 change: 0 additions & 1 deletion SlimViewer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@
ScrollViewer.HorizontalScrollBarVisibility="Auto" />
<TextBox TextWrapping="Wrap"
Text="{Binding Path=FileName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2"
commonControls:FocusExtension.IsInputFocused="{Binding IsInputFocused}"
Grid.Column="2">
<TextBox.InputBindings>
<KeyBinding Command="{Binding RenameCommand}" Key="Enter" />
Expand Down
26 changes: 0 additions & 26 deletions SlimViews/ImageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ public sealed class ImageView : ViewModelBase
/// </summary>
private bool _isActive;

/// <summary>
/// The is input focused
/// </summary>
private bool _isInputFocused;

/// <summary>
/// The is image active
/// </summary>
Expand Down Expand Up @@ -721,19 +716,6 @@ public string FileName
set => SetProperty(ref _fileName, value, nameof(FileName));
}

/// <summary>
/// Gets or sets a value indicating whether this instance is input focused.
/// </summary>
/// <value>
/// <c>true</c> if this instance is input focused; otherwise, <c>false</c>.
/// </value>
public bool IsInputFocused
{
get => _isInputFocused;
set => SetProperty(ref _isInputFocused, value, nameof(IsInputFocused));
}


/// <summary>
/// Gets or sets a value indicating whether this instance is active.
/// </summary>
Expand Down Expand Up @@ -1492,8 +1474,6 @@ private void CloseAction(object obj)
/// <param name="obj">The object.</param>
private void OpenAction(object obj)
{
if (IsInputFocused) return;

var pathObj = FileIoHandler.HandleFileOpen(ViewResources.FileOpen, SlimViewerRegister.CurrentFolder);

if (string.IsNullOrEmpty(pathObj?.FilePath)) return;
Expand Down Expand Up @@ -1586,8 +1566,6 @@ private void ConvertCifAction(object obj)
/// <param name="obj">The object.</param>
private void SaveAction(object obj)
{
if (IsInputFocused) return;

if (Bmp == null) return;

var btm = Bmp.ToBitmap();
Expand Down Expand Up @@ -1617,8 +1595,6 @@ private void SaveAction(object obj)
/// <param name="obj">The object.</param>
private void NextAction(object obj)
{
if (IsInputFocused) return;

var lst = Observer.Keys.ToList();
if (lst.IsNullOrEmpty()) return;

Expand All @@ -1633,8 +1609,6 @@ private void NextAction(object obj)
/// <param name="obj">The object.</param>
private void PreviousAction(object obj)
{
if (IsInputFocused) return;

var lst = Observer.Keys.ToList();
if (lst.IsNullOrEmpty()) return;

Expand Down

0 comments on commit 407f664

Please sign in to comment.