- •На тему: «представлення і обробка статичних зображень. Стеганографія»
- •1. Мета роботи
- •Короткі теоретичні відомості
- •3. Лабораторне завдання Написати програму для відкриття 24-бітного bmp файлу, яка перетворює кольорове зображення в відтінки сірого. Реалізувати стеганографічне кодування та декодування тексту.
- •4.Код програми
- •Файл Program.Cs
- •Файл Text.Cs
- •Файл TextExtracter.Cs
- •Файл TextHider.Cs
- •5.Результат виконання програми
- •6.Висновок
3. Лабораторне завдання Написати програму для відкриття 24-бітного bmp файлу, яка перетворює кольорове зображення в відтінки сірого. Реалізувати стеганографічне кодування та декодування тексту.
4.Код програми
Файл Program.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace HideMe
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Файл Text.Cs
using System.Collections;
using System.Text;
namespace HideMe
{
class Text{
private BitArray _bits;
private int _bitsCount;
private readonly string _text;
/// Формує масив бітів на основі стрічки
public Text(string text)
{
_text = text;
ExtractBits(text + "\0");
}
/// Формує стіччку на основі масива бітів
public Text(bool[] bits)
{
_text = GetText(GetBytes(bits)).TrimEnd('\0');
}
public string Value{
get { return _text; }
}
public int BitsCount
{
get { return _bitsCount; }
}
/// Повертає вибірку з масива бітів
/// index початок вибірки
/// count довжина вибірки
public bool[] GetBits(int index, int count)
{
var result = new bool[count];
for (int i = 0; i < count; i++)
{
result[i] = (index + i < _bits.Length) ? _bits[index + i] : false;
}
return result;
}
/// Вибирає біти з тексту
private void ExtractBits(string text)
{
byte[] bytes = GetBytes(text);
_bitsCount = bytes.Length*8;
_bits = new BitArray(bytes);
}
private static byte[] GetBytes(string text)
{
return Encoding.Default.GetBytes(text);
}
private static byte[] GetBytes(bool[] bits)
{
var result = new byte[bits.Length/8];
for (int i = 0; i < result.Length; i++)
{
byte b = 0;
for (int j = 0; j < 8; j++)
{
if (bits[i * 8 + j])
b |= (byte) (1 << j);
}
result[i] = b;
}
return result;
}
private static string GetText(byte[] bytes)
{
return Encoding.Default.GetString(bytes);
}
}
}
Файл TextExtracter.Cs
using System.Drawing;
namespace HideMe
{
static class TextExtracter
{
public static string ExtractText(string pathToBmp, int bitsPerByte)
{
var bmp = new Bitmap(pathToBmp);
var bits = new bool[bmp.Width*bmp.Height*3*bitsPerByte];
int index = 0;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
Color pixel = bmp.GetPixel(x, y);
ExtractBits(bits, index, bitsPerByte, pixel);
index += 3*bitsPerByte;
}
}
var text = new Text(bits);
return text.Value;
}
private static void ExtractBits(bool[] bits, int index, int bitsPerByte, Color c)
{
byte[] rgb = new[] {c.R, c.G, c.B};
for (int i = 0; i < rgb.Length; i++)
{
for (int j = 0; j < bitsPerByte; j++)
{
bits[index++] = GetBit(rgb[i], j);
}
}
}
private static bool GetBit(byte b, int index)
{
byte mask = 1;
mask <<= index;
return (b & mask) > 0;
}
}
}
