i'm dying how do you convert a png image to a 1bpp bitmap

This commit is contained in:
Maff 2019-09-09 00:51:36 +01:00
parent 15ad9e30ce
commit d3b95505a7
2 changed files with 61 additions and 9 deletions

View File

@ -14,9 +14,11 @@
</Grid.ColumnDefinitions>
<GroupBox Header="Paperang-Sharp Test Library" Margin="10,10,9.6,0" Grid.ColumnSpan="3" Height="217" VerticalAlignment="Top">
<Grid Margin="10,10,10,10">
<Button x:Name="btClearLog" Content="Clear Log" HorizontalAlignment="Left" Width="75" Margin="0,2,0,152.2" Click="BtClearLog_Click"/>
<Button x:Name="btInitUSB" Content="Initialise USB" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,23,0,0" IsDefault="True" Height="21" Click="BtInitUSB_Click"/>
<Button x:Name="btTestLine" Content="Print test" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,44,0,0" IsDefault="True" Height="21" Click="BtTestLine_Click"/>
<Button x:Name="btClearLog" Content="Clear Log" HorizontalAlignment="Left" MinWidth="75" Margin="0,2,0,152.2" Click="BtClearLog_Click"/>
<Button x:Name="btInitUSB" Content="Initialise USB" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,23,0,0" IsDefault="True" Height="21" Click="BtInitUSB_Click"/>
<Button x:Name="btTestLine" Content="Single-line test" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,44,0,0" Height="21" Click="BtTestLine_Click"/>
<Button x:Name="btLoadImage" Content="Load test image" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,65,0,0" Height="21" Click="BtLoadImage_Click"/>
<Image x:Name="imPreview" HorizontalAlignment="Left" Height="154" Margin="363,10,0,0" VerticalAlignment="Top" Width="369"/>
</Grid>
</GroupBox>
<TextBox x:Name="tbLog" HorizontalAlignment="Left" Margin="10,232,0,10" TextWrapping="Wrap" Width="774" Grid.ColumnSpan="3" AllowDrop="False" IsReadOnly="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" Text="{Binding Path=LogBuffer}"/>

View File

@ -2,6 +2,12 @@
using System;
using System.Windows;
//testing
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;
namespace sharperang {
/// <summary>
/// Interaction logic for MainWindow.xaml
@ -9,6 +15,7 @@ namespace sharperang {
public partial class MainWindow : Window {
private LogBridge logger;
private USBPrinter printer=new USBPrinter();
private Bitmap bimg;
public MainWindow() {
InitializeComponent();
logger = new LogBridge();
@ -30,13 +37,56 @@ namespace sharperang {
logger.Debug("Printer initialised and ready");
}
private void BtTestLine_Click(object sender, RoutedEventArgs e) {
byte[] data = new byte[384];
byte[] data = new byte[72];
byte[] data2 = new byte[95];
//for each byte (0000 0000) every 4 bits is a "dot" with variable width.
//having spent some time examining this, each bit in a given byte corresponds to a dot on the thermal impression plate
//and each set of 192 bytes constitutes a single line
//giving a size of 3072 distinct dots per line
data[5] = 0x10; data[4]=0x10; data[3]=0x20; data[2]=0x20; data[1]=0x30; data[0]=0x30;
data[192] = 0x10; data[193]=0x10;data[194]=0x20;data[195]=0x20;data[196]=0x30;data[197]=0x30;
printer.PrintBytes(data);
// every 96 bytes is one line, and the first 48 bytes of each line is the actual dot data. it's unclear to me what the other half is for. P2 support maybe?
//giving a size of 768 distinct dots per line
//line length of P1 is 48 bytes
//line length of P2 is 72 bytes
//unsure why each line is stored in a buffer of 96 bytes, but okay.
//Paperang P1 prints bytes on one line from byte 0 to byte 47
//Paperang P2 prints bytes on one line from byte 0 to byte 71
data[0] = 0x55; data[8] = 0x77; data[16] = 0x77; data[24] = 0x77; data[32] = 0x77; data[40] = 0x77;
data[48] = 0x77; data[56] = 0x77; data[64] = 0x77; data[71] = 0x77; //data[72] = 0x77; data[80] = 0x77; data[88] = 0x77;
//data[5] = 0x10; data[4]=0x10; data[3]=0x20; data[2]=0x20; data[1]=0x30; data[0]=0x30;
/*data2[0] = 0x10; data2[1]=0x10;data2[2]=0x20;data2[3]=0x20;data2[4]=0x30;data2[5]=0x30;
data2[32] = 0xaa; data[32] = 0x55;
data2[50] = 0x55; data[50] = 0xaa;
data2[87] = 0xaa; data[87] = 0x55;*/
printer.PrintBytes(data, false);
printer.PrintBytes(data2, false);
}
private void BtLoadImage_Click(object sender, RoutedEventArgs e) {
bimg=new Bitmap("C:/Users/maff/Downloads/Sqrl - Kid Maff (Vectorised).png");
int w=bimg.Width; int h=bimg.Height;float rt=(float)w/h;
h=(int)(384*rt);
bimg=new Bitmap(bimg, 384, h);
Bitmap tmp = new Bitmap(bimg.Width, bimg.Height, PixelFormat.Format1bppIndexed);
int width = bimg.Width;
int height = bimg.Height;
Color p;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
p = bimg.GetPixel(x, y);
int a = p.A;
int r = p.R;
int g = p.G;
int b = p.B;
int avg = (int)(0.2989*r + 0.5870*g + 0.1140*b)/3;
avg = avg < 128 ? 0 : 255;
bimg.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
}
}
byte[] img;
using (MemoryStream s = new MemoryStream()) {
tmp.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
img=s.ToArray();
}
printer.PrintBytes(img, false);
}
}
}