Skip to content

Commit

Permalink
Updated to the beta version of ImageSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewEC committed Nov 18, 2017
1 parent e9c30c3 commit 591091c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 67 deletions.
2 changes: 1 addition & 1 deletion SteganographyApp.Calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ImageSharp;
using SixLabors.ImageSharp;
using SteganographyApp.Common;
using SteganographyApp.Common.Data;
using SteganographyApp.Common.IO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.11" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
114 changes: 53 additions & 61 deletions SteganographyApp.Common/IO/ImageStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Helpers;
using SteganographyApp.Common.IO.Content;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -139,20 +140,17 @@ public void CleanAll(ProgressReport report)
currentImageWidth = image.Width;
currentImageHeight = image.Height;

using (var pixels = image.Lock())
while (true)
{
while (true)
{
byte newRed = (byte)(pixels[x, y].R & ~1);
byte newGreen = (byte)(pixels[x, y].G & ~1);
byte newBlue = (byte)(pixels[x, y].B & ~1);
byte newRed = (byte)(image[x, y].R & ~1);
byte newGreen = (byte)(image[x, y].G & ~1);
byte newBlue = (byte)(image[x, y].B & ~1);

pixels[x, y] = new Rgba32(newRed, newGreen, newBlue, pixels[x, y].A);
image[x, y] = new Rgba32(newRed, newGreen, newBlue, image[x, y].A);

if(!TryMove())
{
break;
}
if (!TryMove())
{
break;
}
}
image.Save(imagePath);
Expand All @@ -176,38 +174,35 @@ public int Write(string binary)
int written = 0;
using (var image = Image.Load(args.CoverImages[currentImageIndex]))
{
using (var pixels = image.Lock())
for (int i = 0; i < binary.Length; i += 3)
{
for (int i = 0; i < binary.Length; i += 3)
{
Rgba32 pixel = pixels[x, y];
Rgba32 pixel = image[x, y];

pixel.R = (binary[i] == '0') ? (byte)(pixel.R & ~1) : (byte)(pixel.R | 1);
written++;

pixel.R = (binary[i] == '0') ? (byte)(pixel.R & ~1) : (byte)(pixel.R | 1);
if (written < binary.Length)
{
pixel.G = (binary[i + 1] == '0') ? (byte)(pixel.G & ~1) : (byte)(pixel.G | 1);
written++;

if (written < binary.Length)
{
pixel.G = (binary[i + 1] == '0') ? (byte)(pixel.G & ~1) : (byte)(pixel.G | 1);
pixel.B = (binary[i + 2] == '0') ? (byte)(pixel.B & ~1) : (byte)(pixel.B | 1);
written++;

if (written < binary.Length)
{
pixel.B = (binary[i + 2] == '0') ? (byte)(pixel.B & ~1) : (byte)(pixel.B | 1);
written++;
}
}
}

pixels[x, y] = new Rgba32(
pixel.R,
pixel.G,
pixel.B,
pixel.A
);
image[x, y] = new Rgba32(
pixel.R,
pixel.G,
pixel.B,
pixel.A
);

if(!TryMove())
{
break;
}
if (!TryMove())
{
break;
}
}
image.Save(args.CoverImages[currentImageIndex]);
Expand All @@ -234,34 +229,31 @@ public string Read(int length)
using (var image = Image.Load(args.CoverImages[currentImageIndex]))
{
int read = 0;
using (var pixels = image.Lock())
while (read < length)
{
while (read < length)
{
Rgba32 pixel = pixels[x, y];
Rgba32 pixel = image[x, y];

string redBit = Convert.ToString(pixel.R, 2);
binary.Append(redBit.Substring(redBit.Length - 1));
read++;

string redBit = Convert.ToString(pixel.R, 2);
binary.Append(redBit.Substring(redBit.Length - 1));
if (read < length)
{
string greenBit = Convert.ToString(pixel.G, 2);
binary.Append(greenBit.Substring(greenBit.Length - 1));
read++;

if (read < length)
{
string greenBit = Convert.ToString(pixel.G, 2);
binary.Append(greenBit.Substring(greenBit.Length - 1));
string blueBit = Convert.ToString(pixel.B, 2);
binary.Append(blueBit.Substring(blueBit.Length - 1));
read++;

if (read < length)
{
string blueBit = Convert.ToString(pixel.B, 2);
binary.Append(blueBit.Substring(blueBit.Length - 1));
read++;
}
}
}

if(!TryMove())
{
break;
}
if (!TryMove())
{
break;
}
}
}
Expand Down Expand Up @@ -319,13 +311,13 @@ public void WriteContentChunkTable(List<int> table)
// 11 pixels. Since 11 pixels can actually store 33 bits we pad the 32 bit table entry with an additional
// zero at the end which will be ignored when reading the table.
binary.Append(Convert.ToString(table.Count, 2).PadLeft(ChunkDefinitionBitSize, '0')).Append('0');

foreach (int chunkLength in table)
{
binary.Append(Convert.ToString(chunkLength, 2).PadLeft(ChunkDefinitionBitSize, '0')).Append('0');
}
int written = Write(binary.ToString());
if(binary.Length < written)
if (binary.Length < written)
{
throw new ImageProcessingException("There is not enough space in the current image to write the entire content chunk table.");
}
Expand Down Expand Up @@ -365,7 +357,7 @@ public void Next()
x = 0;
y = 0;
currentImageIndex++;
if(currentImageIndex == args.CoverImages.Length)
if (currentImageIndex == args.CoverImages.Length)
{
throw new ImageProcessingException("There are not enough available store space in the provided images to process this request.");
}
Expand All @@ -387,15 +379,15 @@ public void Seek(int length)
x = 0;
y = 0;
int count = 0;
while(count < length)
while (count < length)
{
count += 3;
x++;
if(x == currentImageWidth)
if (x == currentImageWidth)
{
x = 0;
y++;
if(y == currentImageHeight)
if (y == currentImageHeight)
{
throw new ImageProcessingException(String.Format("There are not enough available bits in this image to seek to the specified length of {0}", length));
}
Expand All @@ -412,11 +404,11 @@ public void Seek(int length)
public bool TryMove()
{
x++;
if(x == currentImageWidth)
if (x == currentImageWidth)
{
x = 0;
y++;
if(y == currentImageHeight)
if (y == currentImageHeight)
{
x = 0;
y = 0;
Expand Down
5 changes: 3 additions & 2 deletions SteganographyApp.Common/SteganographyApp.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImageSharp" Version="1.0.0-alpha9-00021" />
<PackageReference Include="Rijndael256" Version="3.2.0" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0002" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0002" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>

</Project>

0 comments on commit 591091c

Please sign in to comment.