Skip to content

Instantly share code, notes, and snippets.

@klemenzarn
Created November 5, 2014 17:01
Show Gist options
  • Save klemenzarn/8bd2de6fdf72432ea825 to your computer and use it in GitHub Desktop.
Save klemenzarn/8bd2de6fdf72432ea825 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO.Compression;
using System.Diagnostics;
namespace Multimedija {
public partial class Form1 : Form {
Image[] slike = new Image[219];
short[,] row = new short[512, 512];
Color[] barva = new Color[4096];
double napredek;
double tmp;
public Form1() {
InitializeComponent();
//MessageBox.Show(pretvoriVBinarno(-2, 12));
//MessageBox.Show(pretvorba());
}
private int decodeDeltaToInt(string p, int max) {
int delta = Convert.ToInt32(p, 2);
int test = max / 2 + 1;
if (delta < test) {
delta = -max + delta + 1;
} else {
delta = delta - 1;
}
return delta;
}
private string pretvorba(int stevilo) {
string output = "";
int max = 0;
int steviloBitov = 0;
if (stevilo >= -2 && stevilo <= 2) {
output += "00";
max = 3;
steviloBitov = 2;
} else if (stevilo >= -6 && stevilo <= -3) {
output += "01";
max = 7;
steviloBitov = 3;
} else if (stevilo >= 3 && stevilo <= 6) {
output += "01";
max = 7;
steviloBitov = 3;
} else if (stevilo >= -14 && stevilo <= -7) {
output += "10";
max = 15;
steviloBitov = 4;
} else if (stevilo >= 7 && stevilo <= 14) {
output += "10";
max = 15;
steviloBitov = 4;
} else if (stevilo >= -30 && stevilo <= -15) {
output += "11";
max = 31;
steviloBitov = 5;
} else if (stevilo >= 15 && stevilo <= 30) {
max = 31;
output += "11";
steviloBitov = 5;
}
int mesto = dobiIndex(stevilo, max);
output += pretvoriVBinarno(mesto, steviloBitov);
return output;
}
private int dobiIndex(int stevilo, int max) {
int output = 0;
if (stevilo < 0) {
output = max - Math.Abs(stevilo) - 1;
} else {
output = stevilo + 1;
}
return output;
}
private void Form1_Load(object sender, EventArgs e) {
}
private string stisniKoIzPicke(short[,] polje, string pot) {
//Stopwatch stopWatch = new Stopwatch();
//stopWatch.Start();
StringBuilder koda = new StringBuilder();
bool jeIsto = false;
int prejsnaVrednost = 0;
int enakaVrednost = 0;
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
int vrednost = polje[i, j];
if (i == 0 && j == 0) {
//preverimo prvo vrednost ali je -2048, če ni jo kodiramo po absolutnem, torej 10 in 12 bitov za število
if (vrednost == -2048) {
koda.Append("11");
} else {
koda.Append("10").Append(pretvoriVBinarno(vrednost, 12));
}
prejsnaVrednost = vrednost;
} else if (i == 511 && j == 511) {
//ZA ZADNJI ZNAK!!
if (jeIsto) {
if (prejsnaVrednost == -2048) {
if (enakaVrednost >= 5) {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //ZRAK + RLE
} else {
for (int k = 0; k < enakaVrednost; k++) {
koda.Append("11"); //ZRAK
}
}
} else {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //DRUGAČE po RLE
}
} else {
if (vrednost == prejsnaVrednost) {
enakaVrednost++;
if (prejsnaVrednost == -2048) {
if (enakaVrednost >= 5) {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //ZRAK + RLE
} else {
for (int k = 0; k < enakaVrednost; k++) {
koda.Append("11"); //ZRAK
}
}
} else {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //DRUGAČE po RLE
}
} else {
if (vrednost == -2048) {
koda.Append("11");
jeIsto = true;
} else {
int delta = prejsnaVrednost - vrednost;
if (Math.Abs(delta) < 30) {
koda.Append("00").Append(pretvorba(delta));
//Console.WriteLine("pride do 00");
//int c = 45;
} else {
koda.Append("10").Append(pretvoriVBinarno(vrednost, 12));
}
}
prejsnaVrednost = vrednost;
}
}
} else {
//če ni prvi stolpec in prva vrstica
if (jeIsto) {
//če je isto ko prej, seštevamo iste vrednosti...
if (vrednost == prejsnaVrednost) {
enakaVrednost++; //seštevamo vse vrednosti
if (enakaVrednost == 63) {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //ZRAK + RLE
prejsnaVrednost = vrednost;
enakaVrednost = 0;
}
} else {
//če ni ista vrednost kot prejšna preverimo če je -2048. ČE je -2048 gremo
//kodirati po zraku, če je več k 5 potem zrak + RLE
if (prejsnaVrednost == -2048) {
if (enakaVrednost >= 5) {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //ZRAK + RLE
} else {
for (int k = 0; k < enakaVrednost; k++) {
koda.Append("11"); //ZRAK
}
}
// steviloZrakov = 0;
} else {
koda.Append("01").Append(pretvoriVBinarno(enakaVrednost, 6)); //DRUGAČE po RLE
}
jeIsto = false;
enakaVrednost = 0;
//kodiramo nasledni znakc
if (vrednost == -2048) {
koda.Append("11");
} else {
int delta = prejsnaVrednost - vrednost;
if (Math.Abs(delta) < 30) {
string tempPretvorba = pretvorba(delta);
koda.Append("00").Append(tempPretvorba);
//Console.WriteLine("pride do 00");
// int c = 45;
} else {
koda.Append("10").Append(pretvoriVBinarno(vrednost, 12));
}
}
prejsnaVrednost = vrednost;
}
} else {
if (vrednost == prejsnaVrednost) {
enakaVrednost++;
jeIsto = true;
} else {
if (vrednost == -2048) {
koda.Append("11");
jeIsto = true;
} else {
int delta = prejsnaVrednost - vrednost;
if (Math.Abs(delta) < 30) {
koda.Append("00").Append(pretvorba(delta));
//Console.WriteLine("pride do 00");
//int c = 45;
} else {
koda.Append("10").Append(pretvoriVBinarno(vrednost, 12));
}
}
prejsnaVrednost = vrednost;
}
}
}
}
}
//stopWatch.Stop();
//// Get the elapsed time as a TimeSpan value.
//TimeSpan ts = stopWatch.Elapsed;
//// Format and display the TimeSpan value.
//string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
// ts.Hours, ts.Minutes, ts.Seconds,
// ts.Milliseconds / 10);
//Console.WriteLine("RunTime " + elapsedTime);
if (pot != null) {
UnicodeEncoding uniEncode = new UnicodeEncoding();
byte[] bytesToCompress = uniEncode.GetBytes(koda.ToString());
byte[] compress = Compress(bytesToCompress);
File.WriteAllBytes("compressed\\" + pot + ".cmp", compress);
}
return koda.ToString();
}
public static byte[] Compress(byte[] raw) {
using (MemoryStream memory = new MemoryStream()) {
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true)) {
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
private string pretvoriVBinarno(int enakaVrednost, int interval) {
string binarno = Convert.ToString(enakaVrednost, 2);
//int binranoInt = Convert.ToInt32(binarno, 10);
string formatk = "";
for (int i = 0; i < interval - binarno.Length; i++) {
formatk += "0";
}
string binarno2 = formatk + binarno;
if (binarno2.Length > interval) {
binarno2 = binarno2.Substring(binarno2.Length - interval);
}
return binarno2;
}
private void nalozislike(int x, int y) {
napredek = 0.00;
Thread myThread = new System.Threading.Thread(delegate() {
for (int a = x; a <= y; a++) {
string pot = String.Format(String.Format("{0:0000}", a));
byte[] bytes = File.ReadAllBytes("glava/" + pot + ".img");
int stevec = 0;
Bitmap slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
short myShort = (short)(bytes[stevec + 1] << 8 | bytes[stevec] << 0);
stevec = stevec + 2;
row[i, j] = myShort;
// MessageBox.Show(row[i, j].ToString());
slika.SetPixel(i, j, barva[row[i, j] + 2048]);
}
}
//stisniKoIzPicke(row, pot);
napredek++;
slike[a - 1] = slika;
//MessageBox.Show(a.ToString());
napredek = (double)a / (double)y;
pictureBox1.Image = slika;
}
});
myThread.Start();
}
private void slikaIMGToolStripMenuItem_Click(object sender, EventArgs e) {
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
byte[] bytes = File.ReadAllBytes(file);
int stevec = 0;
Bitmap slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
short myShort = (short)(bytes[stevec + 1] << 8 | bytes[stevec] << 0);
stevec = stevec + 2;
row[i, j] = myShort;
// MessageBox.Show(row[i, j].ToString());
slika.SetPixel(i, j, barva[row[i, j] + 2048]);
}
}
string pot = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
stisniKoIzPicke(row, pot);
pictureBox1.Image = slika;
}
}
private void trackBar1_Scroll(object sender, EventArgs e) {
int st = trackBar1.Value;
pictureBox1.Image = slike[st - 1];
}
private void barvnaPaletaToolStripMenuItem_Click(object sender, EventArgs e) {
// Set to details view.c
listView1.View = View.Details;
// Add a column with width 20 and left alignment.
int j = 9;
byte[] bytes = File.ReadAllBytes("paleta/1.pal");
for (int i = 0; i < 4096; i++) {
int stevec = i - 2048;
ListViewItem a = new ListViewItem(stevec.ToString(), 0);
a.SubItems.Add("");
listView1.Items.Add(a);
listView1.Items[i].SubItems[1].BackColor = Color.FromArgb(bytes[j], bytes[j + 1], bytes[j + 2]);
barva[i] = listView1.Items[i].SubItems[1].BackColor;
listView1.Items[i].UseItemStyleForSubItems = false;
j = j + 4;
}
}
private void naloziSlikeDecode() {
Bitmap slika = null;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file2 = openFileDialog1.FileName;
decoded = "";
decodedValues = new List<int>();
indexGlobal = 0;
previousValue = 0;
index = 0;
UnicodeEncoding uniEncode = new UnicodeEncoding();
byte[] file = File.ReadAllBytes(file2);
byte[] decompressed = Decompress(file);
decoded = uniEncode.GetString(decompressed);
decodeIt();
int mojIndex = 0;
slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
if (mojIndex < decodedValues.Count) {
int myShort = decodedValues[mojIndex];
mojIndex++;
slika.SetPixel(i, j, barva[myShort + 2048]);
}
}
}
//MessageBox.Show(a.ToString());
pictureBox1.Image = slika;
}
}
private void dekodirajToolStripMenuItem_Click(object sender, EventArgs e) {
naloziSlikeDecode(1, 219);
}
private void naloziSlikeDecode(int x, int y) {
Bitmap slika = null;
for (int a = x; a < y; a++) {
decoded = "";
decodedValues = new List<int>();
indexGlobal = 0;
previousValue = 0;
index = 0;
string pot = String.Format(String.Format("{0:0000}", a));
UnicodeEncoding uniEncode = new UnicodeEncoding();
byte[] file = File.ReadAllBytes("compressed/" + pot + ".cmp");
byte[] decompressed = Decompress(file);
decoded = uniEncode.GetString(decompressed);
decodeIt();
int mojIndex = 0;
slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
if (mojIndex < decodedValues.Count) {
int myShort = decodedValues[mojIndex];
mojIndex++;
slika.SetPixel(i, j, barva[myShort + 2048]);
}
}
}
napredek++;
slike[a - 1] = slika;
//MessageBox.Show(a.ToString());
}
if (napredek < y) {
//progressBar1.Value = napredek;
}
pictureBox1.Image = slika;
}
string decoded = "";
int index = 0;
List<int> decodedValues = new List<int>();
int indexGlobal = 0;
int previousValue = 0;
private void decodeIt() {
while (index < decoded.Length) {
string type = decoded.Substring(index, 2);
index += 2;
switch (type) {
case "11":
decodedValues.Add(-2048);
previousValue = -2048;
break;
case "01":
try {
string what = decoded.Substring(index, 6);
int howManyTimes = Convert.ToInt32(what, 2);
for (int i = 0; i < howManyTimes; i++) {
decodedValues.Add(previousValue);
}
} catch (Exception ex) {
Console.WriteLine("dobr ok");
}
index += 6;
break;
case "10":
string temp = decoded.Substring(index, 12);
int[] bitsString = new int[temp.Length];
for (int i = 0; i < temp.Length; i++) {
bitsString[i] = Convert.ToInt32(temp.Substring(i, 1));
}
int absoluteValue = convertBinToDec(bitsString);
index += 12;
decodedValues.Add(absoluteValue);
previousValue = absoluteValue;
break;
case "00":
string dolzina = decoded.Substring(index, 2);
string razlika = "";
int deltaValue = 0;
index += 2;
switch (dolzina) {
case "00":
razlika = decoded.Substring(index, 2);
deltaValue = decodeDeltaToInt(razlika, 3);
index += 2;
break;
case "01":
razlika = decoded.Substring(index, 3);
deltaValue = decodeDeltaToInt(razlika, 7);
index += 3;
break;
case "10":
razlika = decoded.Substring(index, 4);
deltaValue = decodeDeltaToInt(razlika, 15);
index += 4;
break;
case "11":
razlika = decoded.Substring(index, 5);
deltaValue = decodeDeltaToInt(razlika, 31);
index += 5;
break;
}
previousValue = previousValue - deltaValue;
decodedValues.Add(previousValue);
int b = 5;
break;
}
}
}
private int convertBinToDec(int[] bits) {
int vrednost = 0;
if (bits[0] == 1) //če je negativno število
{
for (int i = 0; i < bits.Length; i++) {
if (bits[i] == 1) bits[i] = 0;
else bits[i] = 1;
}
int rezultat = convertBinToDec(bits) + 1;
return -rezultat;
} else {
int index = 0;
for (int i = bits.Length - 1; i > 0; i--) {
if (bits[i] == 1) {
vrednost += (int)Math.Pow(2, index);
}
index++;
}
}
return vrednost;
}
static byte[] Decompress(byte[] gzip) {
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) {
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream()) {
int count = 0;
do {
count = stream.Read(buffer, 0, size);
if (count > 0) {
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
private void button1_Click(object sender, EventArgs e) {
int a, b;
try {
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
//napredek = 0;
nalozislike(a, b);
trackBar1.Minimum = a;
trackBar1.Maximum = b;
label5.Text = a.ToString();
label6.Text = b.ToString();
} catch {
MessageBox.Show("V območje vpiši številke");
textBox1.Text = "";
textBox2.Text = "";
}
}
private void timer1_Tick(object sender, EventArgs e) {
tmp = napredek * 100;
tmp = Math.Round(tmp, 2);
label3.Text = tmp.ToString() + "%";
}
private void kodirajVSEToolStripMenuItem_Click(object sender, EventArgs e) {
Thread myThread = new System.Threading.Thread(delegate() {
for (int a = 1; a < 219; a++) {
string pot = String.Format(String.Format("{0:0000}", a));
byte[] bytes = File.ReadAllBytes("glava/" + pot + ".img");
int stevec = 0;
//Bitmap slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
short myShort = (short)(bytes[stevec + 1] << 8 | bytes[stevec] << 0);
stevec = stevec + 2;
row[i, j] = myShort;
// MessageBox.Show(row[i, j].ToString());
//slika.SetPixel(i, j, barva[row[i, j] + 2048]);
}
}
stisniKoIzPicke(row, pot);
//slike[a - 1] = slika;
//MessageBox.Show(a.ToString());
}
});
myThread.Start();
}
List<int[,]> rezine = new List<int[,]>();
List<int[,]> razlike = new List<int[,]>();
List<string> kodirano = new List<string>();
private void GetDataCM3(int x, int y) {
napredek = 0.00;
int [,]row1 = new int[512, 512];
for (int a = x; a <= y; a++) {
string pot = String.Format(String.Format("{0:0000}", a));
Console.WriteLine(pot);
byte[] bytes = File.ReadAllBytes("glava/" + pot + ".img");
int stevec = 0;
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
int myShort = (bytes[stevec + 1] << 8 | bytes[stevec] << 0);
stevec = stevec + 2;
row1[i, j] = myShort;
}
}
rezine.Add(row1);
}
}
private void button3_Click(object sender, EventArgs e) {
int a, b;
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
GetDataCM3(a, b);
// short[,] m0 = rezine[0];
// string kodiranom0 = stisniKoIzPicke(m0, null);
// kodirano.Add(kodiranom0);
//računamo matriko razlik
for (int i = a; i < b; i++) {
int[,] trenutna = rezine[i];
int[,] prejsnja = rezine[i - 1];
int[,] razlika = new int[512, 512];
for (int j = 0; j < 512; j++) {
for (int k = 0; k < 512; k++) {
razlika[j, k] = trenutna[j, k] - prejsnja[j, k];
if (razlika[j, k] != 0) {
MessageBox.Show(razlika[j, k].ToString());
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment