forked from andrewawni/Qompressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageDecoder.cpp
123 lines (90 loc) · 2.85 KB
/
ImageDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "ImageDecoder.h"
#include "stringCompressor.cpp"
ImageDecoder::ImageDecoder()
{
HuffmanTree = new MinHeap();
}
void ImageDecoder::Decode(string inputFileName, string outputFileName)
{
std::ifstream InputFile(inputFileName);
int height, width, mapSize;
if (InputFile.is_open())
{
InputFile >> height >> width >> mapSize;
std::cout << "Height: " << height << " " << "Width: " << width << " " << "Number of values: "<< mapSize << '\n';
while (mapSize--)
{
int key;
string code;
InputFile >> key >> code;
// std::cout << key << " " << code << std::endl;
keyCodeMap[key] = code;
}
std::cout << "Map generated\n";
/*******************************************/
this->HuffmanTree->GenerateMinHeap(this->keyCodeMap);
MinHeapNode *root = HuffmanTree->GetRoot();
char c;
int temp = height;
int k = 0;
while (InputFile >> c)
{
string row = "";
int numChars;
string s = "";
while (c != ' ')
{
s.push_back(c);
InputFile.get(c);
}
numChars = std::stoi(s);
for (int j = 0; j < numChars; j++)
{
InputFile.get(c);
row.push_back(c);
}
StringCompressor con;
row = con.asciitoBinaryStream(row);
int t = row.size(), i = 0;
while (t--)
{
if (row[i++] == '0')
root = root->left;
else
root = root->right;
if (root->isLeaf)
{
colorStream.push_back(root->m_data);
root = HuffmanTree->GetRoot();
}
}
}
InputFile.close();
}
/**************************************************/
unsigned char image[height][width][3];
char *imageFileName = new char[outputFileName.size()+5];
for (int i = 0; i < outputFileName.length(); i++)
{
imageFileName[i] = outputFileName[i];
}
int x = 0;
for (int i = height - 1; i >= 0; i--)
{
for (int j = 0; j < width; j++)
{
image[i][j][2] = (unsigned char)this->colorStream[x++]; ///red
image[i][j][1] = (unsigned char)this->colorStream[x++]; ///green
image[i][j][0] = (unsigned char)this->colorStream[x++]; ///blue
}
}
BitmapImageGenerator gen;
gen.generateBitmapImage((unsigned char *)image, height, width, imageFileName);
std::cout << "Image decoded!\n";
}
ImageDecoder::~ImageDecoder()
{
}