Skip to content

Commit

Permalink
Linux support added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed Jan 17, 2017
1 parent ebe330d commit 7d06e57
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
16 changes: 0 additions & 16 deletions Config.cs
@@ -1,42 +1,26 @@
using Newtonsoft.Json;
using System.IO;
using System.Runtime.Serialization;

namespace RiverTrace
{
[DataContract]
class ConfigData
{
[DataMember]
public int zoom;

[DataMember]
public double lat1;
[DataMember]
public double lon1;
[DataMember]
public double lat2;
[DataMember]
public double lon2;

[DataMember]
public int iterationCount;
[DataMember]
public double shoreContrast;
[DataMember]
public double scanRadiusScale;
[DataMember]
public double angleRange;
[DataMember]
public double advanceRate;
[DataMember]
public double noiseReduction;
[DataMember]
public double resamplingFactor;
[DataMember]
public double simplificationStrength;

[DataMember]
public bool debug;
}

Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.5.0")]
[assembly: AssemblyFileVersion("0.0.5.0")]
[assembly: AssemblyVersion("0.0.6.0")]
[assembly: AssemblyFileVersion("0.0.6.0")]
8 changes: 1 addition & 7 deletions RiverTrace.csproj
Expand Up @@ -41,14 +41,8 @@
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
Expand Down
55 changes: 48 additions & 7 deletions SimpleBitmap.cs
@@ -1,5 +1,8 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows.Media.Imaging;

Expand All @@ -8,21 +11,42 @@ namespace RiverTrace
class SimpleBitmap
{
public byte[] Data;
public readonly int Width;
public readonly int Height;
public int Width;
public int Height;

public SimpleBitmap(byte[] fileData)
{
MemoryStream byteStream = new MemoryStream(fileData);
if (Type.GetType("Mono.Runtime") == null)
InitWin(byteStream);
else
InitMono(byteStream);
}

private void InitWin(MemoryStream byteStream)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(fileData);
bitmapImage.StreamSource = byteStream;
bitmapImage.EndInit();
Width = bitmapImage.PixelWidth;
Height = bitmapImage.PixelHeight;
Data = new byte[Width * Height * 4];
bitmapImage.CopyPixels(Data, Width * 4, 0);
}

private void InitMono(MemoryStream byteStream)
{
Bitmap bmp = (Bitmap)Bitmap.FromStream(byteStream);
Width = bmp.Width;
Height = bmp.Height;
Data = new byte[Width * Height * 4];
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Marshal.Copy(bmpData.Scan0, Data, 0, Data.Length);
bmp.UnlockBits(bmpData);
}

public SimpleBitmap(int width, int height)
{
Data = new byte[width * height * 4];
Expand All @@ -31,17 +55,34 @@ public SimpleBitmap(int width, int height)
}

public void WriteTo(string fileName)
{
if (Type.GetType("Mono.Runtime") == null)
WriteToWin(fileName);
else
WriteToMono(fileName);
}

private void WriteToWin(string fileName)
{
var encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Rle;
encoder.Frames.Add(BitmapFrame.Create(GetBitmap()));
encoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(
Width, Height, 96, 96, PixelFormats.Bgr32, null, Data, Width * 4)));
encoder.Save(File.Create(fileName));
}

public BitmapSource GetBitmap()
private void WriteToMono(string fileName)
{
return BitmapSource.Create(Width, Height,
96, 96, PixelFormats.Bgr32, null, Data, Width * 4);
GCHandle pinnedArray = GCHandle.Alloc(Data, GCHandleType.Pinned);
Bitmap bmp32 = new Bitmap(Width, Height, Width * 4,
System.Drawing.Imaging.PixelFormat.Format32bppRgb,
pinnedArray.AddrOfPinnedObject());
Bitmap bmp24 = new Bitmap(Width, Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gr = Graphics.FromImage(bmp24))
gr.DrawImage(bmp32, new Rectangle(0, 0, Width, Height));
bmp24.Save(fileName, ImageFormat.Tiff);
pinnedArray.Free();
}

public Color GetPixel(int x, int y)
Expand Down

0 comments on commit 7d06e57

Please sign in to comment.