Skip to content

Instantly share code, notes, and snippets.

@klemenzarn
Created November 9, 2014 15:25
Show Gist options
  • Save klemenzarn/3c8b4a5b9b5817eacae9 to your computer and use it in GitHub Desktop.
Save klemenzarn/3c8b4a5b9b5817eacae9 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());
//string test1 = prirediBite(-2222);
//MessageBox.Show(test1);
//int beck = prirediBiteInverz(test1);
//MessageBox.Show(beck.ToString());
}
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;
}
//za kodiranje CMP 01
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) {
}
#region kodiranje CMP
private string KodiranjeCMP(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;
}
#endregion
//nalaganje več slik
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();
}
//odpri sliko
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);
KodiranjeCMP(row, pot);
pictureBox1.Image = slika;
}
}
//Prikaz slike trackbarr
private void trackBar1_Scroll(object sender, EventArgs e) {
int st = trackBar1.Value;
pictureBox1.Image = slike[st - 1];
}
#region paleta barv
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;
}
}
#endregion
#region dekodiranje CMP
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(false);
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) {
if (mojIndex < 262143) {
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();
}
//naloži več slik-neuporabno trenutno
private void naloziSlikeDecodeCM3(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(false);
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(bool cm3) {
while (index < decoded.Length) {
if (decodedValues.Count > 262142) {
MessageBox.Show("haha");
break;
} else {
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;
}
#endregion
//gzip dekodiranje
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();
}
}
}
//Nalaganje območja
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() + "%";
}
#region KodirajVse
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]);
}
}
KodiranjeCMP(row, pot);
//slike[a - 1] = slika;
//MessageBox.Show(a.ToString());
}
});
myThread.Start();
}
#endregion
#region CM3kodiranje/dekodiranje
List<short[,]> rezine = new List<short[,]>();
List<short[,]> razlike = new List<short[,]>();
List<string> kodirano = new List<string>();
private void GetDataCM3(int x, int y) {
napredek = 0.00;
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;
short[,] row1 = new short[512, 512];
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;
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 = KodiranjeCMP(m0, null);
kodirano.Add(kodiranom0);
//računamo matriko razlik
int stevecRezin = 1;
for (int i = a; i < b; i++) {
short[,] trenutna = rezine[stevecRezin];
short[,] prejsnja = rezine[stevecRezin - 1];
stevecRezin++;
short[,] razlika = new short[512, 512];
for (int j = 0; j < 512; j++) {
for (int k = 0; k < 512; k++) {
razlika[j, k] = (short)(trenutna[j, k] - prejsnja[j, k]);
}
}
razlike.Add(razlika);
}
//pričnemo z kodiranjem razlik
for (int i = 0; i < razlike.Count; i++) {
kodirano.Add(cm3Encoding(razlike[i]));
}
StringBuilder koda = new StringBuilder();
List<byte[]> bytesToCompressList = new List<byte[]>();
UnicodeEncoding uniEncode = new UnicodeEncoding();
int celotnaVelikost = 0;
for (int i = 0; i < kodirano.Count; i++) {
bytesToCompressList.Add(uniEncode.GetBytes(kodirano[i]));
celotnaVelikost += bytesToCompressList[i].Length;
}
byte[] bytesToCompress = new byte[celotnaVelikost];
int offset = 0;
for (int i = 0; i < bytesToCompressList.Count; i++) {
byte[] test2 = bytesToCompressList[i];
for (int j = 0; j < test2.Length; j++) {
bytesToCompress[offset] = test2[j];
offset++;
}
}
byte[] compress = Compress(bytesToCompress);
File.WriteAllBytes("celota.cm3", compress);
}
private string prirediBite(int stevilo) {
string output = "";
int max = 0;
int steviloBitov = 0;
if (stevilo >= -4 && stevilo <= -1) {
output += "00";
max = 5;
steviloBitov = 3;
} else if (stevilo >= 1 && stevilo <= 4) {
output += "00";
max = 5;
steviloBitov = 3;
} else if (stevilo >= -20 && stevilo <= -5) {
output += "01";
max = 21;
steviloBitov = 5;
} else if (stevilo >= 5 && stevilo <= 20) {
output += "01";
max = 21;
steviloBitov = 5;
} else if (stevilo >= -276 && stevilo <= -21) {
output += "10";
max = 277;
steviloBitov = 9;
} else if (stevilo >= 21 && stevilo <= 276) {
output += "10";
max = 277;
steviloBitov = 9;
} else if (stevilo >= -4095 && stevilo <= -277) {
output += "11";
max = 4096;
steviloBitov = 13;
} else if (stevilo >= 277 && stevilo <= 4095) {
max = 4096;
output += "11";
steviloBitov = 13;
}
int mesto = dobiIndex(stevilo, max);
output += pretvoriVBinarno(mesto, steviloBitov);
return output;
}
private string cm3Encoding(short[,] polje) {
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 == 0) {
koda.Append("11");
} else {
koda.Append("10").Append(prirediBite(vrednost));
}
prejsnaVrednost = vrednost;
} else if (i == 511 && j == 511) {
//ZA ZADNJI ZNAK!!
if (jeIsto) {
if (prejsnaVrednost == 0) {
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 == 0) {
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 == 0) {
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(prirediBite(vrednost));
}
}
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 == 0) {
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 == 0) {
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(prirediBite(vrednost));
}
}
prejsnaVrednost = vrednost;
}
} else {
if (vrednost == prejsnaVrednost) {
enakaVrednost++;
jeIsto = true;
} else {
if (vrednost == 0) {
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(prirediBite(vrednost));
}
}
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);
return koda.ToString();
}
string decoded2 = "";
int index2 = 0;
List<int> decodedValues2 = new List<int>();
int indexGlobal2 = 0;
int previousValue2 = 0;
private void DekodirajCM3() {
while (index2 < decoded2.Length) {
string type = decoded2.Substring(index2, 2);
index2 += 2;
switch (type) {
case "11":
decodedValues2.Add(0);
previousValue2 = 0;
break;
case "01":
try {
string what = decoded2.Substring(index2, 6);
int howManyTimes = Convert.ToInt32(what, 2);
for (int i = 0; i < howManyTimes; i++) {
decodedValues2.Add(previousValue2);
}
} catch (Exception ex) {
Console.WriteLine("dobr ok");
}
index2 += 6;
break;
case "10":
string dolzina = decoded2.Substring(index2, 2);
string razlika = "";
//int deltaValue = 0;
index2 += 2;
switch (dolzina) {
case "00":
razlika = decoded2.Substring(index2, 3);
//deltaValue = decodeDeltaToInt(razlika, 5);
index2 += 3;
break;
case "01":
razlika = decoded2.Substring(index2, 5);
//deltaValue = decodeDeltaToInt(razlika, 21);
index2 += 5;
break;
case "10":
razlika = decoded2.Substring(index2, 9);
//deltaValue = decodeDeltaToInt(razlika, 277);
index2 += 9;
break;
case "11":
razlika = decoded2.Substring(index2, 13);
//deltaValue = decodeDeltaToInt(razlika, 4096);
index2 += 13;
break;
}
int[] bitsString = new int[razlika.Length];
for (int i = 0; i < razlika.Length; i++) {
bitsString[i] = Convert.ToInt32(razlika.Substring(i, 1));
}
int absoluteValue = convertBinToDec(bitsString);
previousValue2 = absoluteValue;
decodedValues2.Add(absoluteValue);
break;
case "00":
string dolzina2 = decoded2.Substring(index2, 2);
string razlika2 = "";
int deltaValue2 = 0;
index2 += 2;
switch (dolzina2) {
case "00":
razlika2 = decoded2.Substring(index2, 2);
deltaValue2 = decodeDeltaToInt(razlika2, 3);
index2 += 2;
break;
case "01":
razlika2 = decoded2.Substring(index2, 3);
deltaValue2 = decodeDeltaToInt(razlika2, 7);
index2 += 3;
break;
case "10":
razlika2 = decoded2.Substring(index2, 4);
deltaValue2 = decodeDeltaToInt(razlika2, 15);
index2 += 4;
break;
case "11":
razlika2 = decoded2.Substring(index2, 5);
deltaValue2 = decodeDeltaToInt(razlika2, 31);
index2 += 5;
break;
}
previousValue2 = previousValue2 - deltaValue2;
decodedValues2.Add(previousValue2);
int b = 5;
break;
}
}
}
private void button2_Click(object sender, EventArgs e) {
List<int[,]> razlike = new List<int[,]>();
List<int[,]> rezine = new List<int[,]>();
int[,] temp = new int[512, 512];
int[,] temp2 = new int[512, 512];
//to je skopirano iz tvojega dekoranja-skoraj isto..prvo plast naredi vredu
#region kodiranje prve rezine
decoded = "";
decodedValues = new List<int>();
indexGlobal = 0;
previousValue = 0;
index = 0;
string pot = "celota.cm3";
UnicodeEncoding uniEncode = new UnicodeEncoding();
byte[] file = File.ReadAllBytes(pot);
byte[] decompressed = Decompress(file);
decoded = uniEncode.GetString(decompressed);
decodeIt(true);
int mojIndex = 0;
Bitmap prva = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
if (mojIndex < decodedValues.Count) {
if (mojIndex < 262143) {
int myShort = decodedValues[mojIndex];
mojIndex++;
temp[i, j] = myShort;
prva.SetPixel(i, j, barva[myShort + 2048]);
} else { MessageBox.Show("Premali cm3"); }
}
}
}
rezine.Add(temp);
int stevilorezin = 1;
pictureBox1.Image = prva;
#endregion
//pol druga plast kodira dalje od 512*512-tea indexa dalje po dekodirajCM3.. skoraj ista dekodirna funkcija ko tvoja
#region OdkodirajOstanek
decoded2 = "";
decodedValues2 = new List<int>();
indexGlobal2 = 0;
previousValue2 = 0;
index2 = 262144;
UnicodeEncoding uniEncode2 = new UnicodeEncoding();
byte[] file2 = File.ReadAllBytes(pot);
byte[] decompressed2 = Decompress(file2);
decoded2 = uniEncode.GetString(decompressed2);
DekodirajCM3();
for (int rez = 0; rez <= (decodedValues2.Count / (512 * 512)); rez++) {
int[,] temp3 = new int[512, 512];
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
if (index2 < decodedValues2.Count) {
int myShort2 = decodedValues2[index2];
temp3[i, j] = myShort2;
index2++;
}
}
}
razlike.Add(temp3);
stevilorezin++;
}
#endregion
//pol dobim razlike pa probam računati iz razlik rezine------TU JE PROBLEM KER REZINE[0]--rezine od 0 bi morala biti
//dekorina prava prednost
#region PridobiPlasti
int[,] prejsnja = razlike[0];
//int c = 0;
int[,] trenutna = rezine.ElementAt(0);
for (int i = 0; i < stevilorezin - 1; i++) {
trenutna = razlike[i];
int[,] rezultat = new int[512, 512];
for (int j = 0; j < 512; j++) {
for (int k = 0; k < 512; k++) {
short tempRezultaT = (short)(trenutna[j, k] + prejsnja[j, k]);
rezultat[j, k] = tempRezultaT;
}
}
prejsnja = rezultat;
rezine.Add(rezultat);
}
#endregion
//tu pa se riše zaenkrat črnobelo, dokler ni pravih vrednosti
#region Narisi
trackBar1.Minimum = 1;
trackBar1.Maximum = stevilorezin;
napredek = 0.00;
// Thread myThread = new System.Threading.Thread(delegate() {
for (int a = 0; a < stevilorezin; a++) {
int stevec = 0;
Bitmap slika = new Bitmap(pictureBox1.Width, pictureBox1.Height);
int[,] temp5 = rezine.ElementAt(a);
for (int i = 0; i < 512; i++) {
for (int j = 0; j < 512; j++) {
if (temp5[i, j] == -2048) {
slika.SetPixel(i, j, Color.White);
} else {
slika.SetPixel(i, j, Color.Black);
}
//slika.SetPixel(i, j, barva[temp[i, j] + 2048]);
}
}
napredek++;
slike[a] = slika;
//MessageBox.Show(a.ToString());
pictureBox1.Image = slika;
}
// });
// myThread.Start();
#endregion
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment