Skip to content

Commit

Permalink
Adding ability to flip over the image
Browse files Browse the repository at this point in the history
  • Loading branch information
Effix committed Nov 19, 2015
1 parent eec46e5 commit e4553db
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
4 changes: 2 additions & 2 deletions LedBadge/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void OnFrameReady(object sender, LedBadgeLib.BadgeFrameEventArgs args)
{
Dispatcher.InvokeAsync(() =>
{
LedBadgeLib.WPF.ImageFromPackedBuffer(FrameBuffer, args.Frame.PackedBuffer, 0, args.Frame.Width, args.Frame.Height);
LedBadgeLib.WPF.ImageFromPackedBuffer(FrameBuffer, args.Frame.PackedBuffer, 0, RotateFrame, args.Frame.Width, args.Frame.Height);
if(fpsUpdate)
{
Expand Down Expand Up @@ -270,7 +270,7 @@ void LogMessage(LedBadgeLib.ResponseCodes code, byte[] response)
int offset = LedBadgeLib.BadgeResponses.DecodePixRect(response, 0, out width, out height, out length);
var img = new System.Windows.Controls.Image()
{
Source = LedBadgeLib.WPF.ImageFromPackedBuffer(response, offset, width, height),
Source = LedBadgeLib.WPF.ImageFromPackedBuffer(response, offset, RotateFrame, width, height),
Width = width * scale,
Height = height * scale,
SnapsToDevicePixels = true,
Expand Down
29 changes: 18 additions & 11 deletions LedBadge/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<StackPanel DockPanel.Dock="Left" Width="150">
<GroupBox Header="Connection">
<StackPanel>
<ComboBox Name="ComPortDropDown" ItemsSource="{Binding ComPorts}" SelectedValue="{Binding SelectedComPort}" DropDownOpened="ComPortsDropDownOpened"/>
<ComboBox ToolTip="Choose the serial communication port that the badge is connected to." Name="ComPortDropDown" ItemsSource="{Binding ComPorts}" SelectedValue="{Binding SelectedComPort}" DropDownOpened="ComPortsDropDownOpened"/>
<Button Content="Connect" Click="ToggleConnection">
<Button.Style>
<Style TargetType="{x:Type Button}">
Expand All @@ -66,13 +66,13 @@
</GroupBox>
<GroupBox Header="Output">
<StackPanel>
<DockPanel Margin="0,-1,0,0">
<DockPanel Margin="0,-1,0,0" ToolTip="Are the LEDs blinding you? Turn down the brightness a little.">
<TextBlock DockPanel.Dock="Left" Text="Brightness" Margin="0,0,5,0"/>
<Slider Minimum="0" Maximum="255" Value="{Binding Brightness}"/>
</DockPanel>
<CheckBox Margin="0,-1,0,0" Content="Dither Frame" IsChecked="{Binding DitherFrame}"/>
<CheckBox Margin="0,-1,0,0" Content="Dither Images" IsChecked="{Binding DitherImages}"/>
<CheckBox Margin="0,-1,0,-1" Content="Rotate Frame" IsChecked="{Binding RotateFrame}"/>
<CheckBox Margin="0,-1,0,0" ToolTip="Dithers the frame buffer before packing. The dots will crawl as the frame changes." Content="Dither Frame" IsChecked="{Binding DitherFrame}"/>
<CheckBox Margin="0,-1,0,0" ToolTip="Dithers images before copying to the frame buffer. Done once at image load and stable as they scroll." Content="Dither Images" IsChecked="{Binding DitherImages}"/>
<CheckBox Margin="0,-1,0,-1" ToolTip="Rotate image 180° if it is easier to have the cable com out of the other side when mounting." Content="Rotate Frame" IsChecked="{Binding RotateFrame}"/>
</StackPanel>
</GroupBox>
</StackPanel>
Expand All @@ -92,7 +92,7 @@
</GroupBox>
</StackPanel>
<DockPanel>
<DockPanel Margin="5,5,5,0" DockPanel.Dock="Top">
<DockPanel ToolTip="Pick one of the sample sources or diagnostic modes to display." Margin="5,5,5,0" DockPanel.Dock="Top">
<TextBlock DockPanel.Dock="Left" Text="Display" VerticalAlignment="Center" Margin="0,0,5,0"/>
<ComboBox ItemsSource="{Binding Source={l:Enumeration {x:Type l:DisplayMode}}}" SelectedValue="{Binding DisplayMode}"/>
</DockPanel>
Expand All @@ -111,13 +111,14 @@
<DockPanel>
<DockPanel DockPanel.Dock="Bottom">
<Button Width="64" Content="Send" DockPanel.Dock="Right" Click="SendText"/>
<TextBlock DockPanel.Dock="Right" Text="Add one message per line above "/>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left">
<RadioButton GroupName="Layout" Name="SingleLineLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton ToolTip="Single line layout" GroupName="Layout" Name="SingleLineLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton.Content>
<Rectangle Width="40" Height="16" Stroke="Black" StrokeThickness="1" Fill="Transparent"/>
</RadioButton.Content>
</RadioButton>
<RadioButton GroupName="Layout" Name="DoubleLineLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton ToolTip="Double line layout" GroupName="Layout" Name="DoubleLineLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton.Content>
<Grid Background="Transparent">
<Grid.RowDefinitions>
Expand All @@ -129,7 +130,7 @@
</Grid>
</RadioButton.Content>
</RadioButton>
<RadioButton GroupName="Layout" Name="SplitLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton ToolTip="Split layout" GroupName="Layout" Name="SplitLayout" Style="{StaticResource RadioButtonButton}">
<RadioButton.Content>
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
Expand All @@ -153,7 +154,10 @@
</TabItem>
<TabItem Header="Image">
<DockPanel>
<Button Width="64" Content="Send" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Click="SendImage"/>
<DockPanel DockPanel.Dock="Bottom">
<Button Width="64" Content="Send" DockPanel.Dock="Right" Click="SendImage"/>
<TextBlock HorizontalAlignment="Right" Text="Add one image path per line above "/>
</DockPanel>
<TextBox Name="ImageInput" AcceptsReturn="True" Margin="0,0,0,5"/>
</DockPanel>
</TabItem>
Expand All @@ -180,7 +184,10 @@
<TextBlock Grid.Column="0" Grid.Row="3" Text="Access Token Secret"/>
<TextBox Grid.Column="2" Grid.Row="3" Text="{Binding TwitterProvider.AccessTokenSecret}"/>
</Grid>
<Button Name="TwitterButton" Width="64" Content="Start" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Click="ToggleTwitter"/>
<DockPanel DockPanel.Dock="Bottom">
<Button Name="TwitterButton" Width="64" Content="Start" DockPanel.Dock="Right" Click="ToggleTwitter"/>
<TextBox HorizontalAlignment="Right" Text="Add one search term per line above "/>
</DockPanel>
<TextBox Name="Hashtags" AcceptsReturn="True" Margin="0,0,0,5"/>
</DockPanel>
</TabItem>
Expand Down
41 changes: 32 additions & 9 deletions LedBadgeUtilLib/Image/BadgeImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,25 @@ public static byte Pack4GrayPix(byte p0, byte p1, byte p2, byte p3)
return dst;
}

public static void IntermediateImagetoPackedBuffer(byte[] intermediateImage, byte[] packedBuffer, int width, int height, int offset, bool rotate)
public static void IntermediateImagetoPackedBuffer(byte[] intermediateImage, byte[] packedBuffer, int offset, bool rotate)
{
int packedI = offset;
if(rotate)
{
for(int p = intermediateImage.Length - 4; p >= 0; ++packedI, p -= 4)
{
packedBuffer[packedI] = BadgeImage.Pack4GrayPix(
intermediateImage[p + 3],
intermediateImage[p + 2],
intermediateImage[p + 1],
intermediateImage[p + 0]);
}
}
else
{
for(int i = offset, p = 0, n = intermediateImage.Length / 4; i < n; ++i, p += 4)
for(int p = 0; p < intermediateImage.Length; ++packedI, p += 4)
{
packedBuffer[i] = BadgeImage.Pack4GrayPix(
packedBuffer[packedI] = BadgeImage.Pack4GrayPix(
intermediateImage[p + 0],
intermediateImage[p + 1],
intermediateImage[p + 2],
Expand All @@ -71,14 +80,28 @@ public static void IntermediateImagetoPackedBuffer(byte[] intermediateImage, byt
}
}

public static void PackedBufferToIntermediateImage(byte[] packedBuffer, byte[] intermediateImage, int width, int height, int offset)
public static void PackedBufferToIntermediateImage(byte[] packedBuffer, byte[] intermediateImage, int offset, bool rotate)
{
for(int i = offset, p = 0, n = intermediateImage.Length / 4; i < n; ++i, p += 4)
int packedI = offset;
if(rotate)
{
for(int p = intermediateImage.Length - 4; p >= 0; ++packedI, p -= 4)
{
intermediateImage[p + 3] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 0) & 0x3));
intermediateImage[p + 2] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 2) & 0x3));
intermediateImage[p + 1] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 4) & 0x3));
intermediateImage[p + 0] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 6) & 0x3));
}
}
else
{
intermediateImage[p + 0] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[i] >> 0) & 0x3));
intermediateImage[p + 1] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[i] >> 2) & 0x3));
intermediateImage[p + 2] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[i] >> 4) & 0x3));
intermediateImage[p + 3] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[i] >> 6) & 0x3));
for(int p = 0; p < intermediateImage.Length; ++packedI, p += 4)
{
intermediateImage[p + 0] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 0) & 0x3));
intermediateImage[p + 1] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 2) & 0x3));
intermediateImage[p + 2] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 4) & 0x3));
intermediateImage[p + 3] = BadgeImage.PixToSrgbGray((byte)((packedBuffer[packedI] >> 6) & 0x3));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion LedBadgeUtilLib/Image/RenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void PackBuffer(bool rotate)
System.Diagnostics.Debug.Assert((Width & 0x3) == 0);
PackedBuffer = new byte[(Width * BadgeCaps.BitsPerPixel / 8) * Height];
}
BadgeImage.IntermediateImagetoPackedBuffer(IntermediateImage, PackedBuffer, Width, Height, 0, rotate);
BadgeImage.IntermediateImagetoPackedBuffer(IntermediateImage, PackedBuffer, 0, rotate);
}

public int Width { get; private set; }
Expand Down
8 changes: 4 additions & 4 deletions LedBadgeUtilLib/Image/WPF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ public static void ImageFromIntermediate(WriteableBitmap target, byte[] intermed
target.WritePixels(new Int32Rect(0, 0, width, height), intermediateImage, width, 0);
}

public static BitmapSource ImageFromPackedBuffer(byte[] packedBuffer, int offset, int width, int height)
public static BitmapSource ImageFromPackedBuffer(byte[] packedBuffer, int offset, bool rotate, int width, int height)
{
var image = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, null);
ImageFromPackedBuffer(image, packedBuffer, offset, width, height);
ImageFromPackedBuffer(image, packedBuffer, offset, rotate, width, height);
return image;
}

public static void ImageFromPackedBuffer(WriteableBitmap target, byte[] packedBuffer, int offset, int width, int height, byte[] tempIntermediate = null)
public static void ImageFromPackedBuffer(WriteableBitmap target, byte[] packedBuffer, int offset, bool rotate, int width, int height, byte[] tempIntermediate = null)
{
var intermediateImage = tempIntermediate ?? new byte[width * height];
BadgeImage.PackedBufferToIntermediateImage(packedBuffer, intermediateImage, width, height, offset);
BadgeImage.PackedBufferToIntermediateImage(packedBuffer, intermediateImage, offset, rotate);
ImageFromIntermediate(target, intermediateImage, width, height);
}

Expand Down

0 comments on commit e4553db

Please sign in to comment.