-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageConverter.cpp
177 lines (152 loc) · 3.86 KB
/
ImageConverter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Created by root on 2/9/18.
//
#include "ImageConverter.h"
#include <iostream>
#include <time.h>
#include <map>
ImageConverter::ImageConverter()
{ }
void ImageConverter::mapCharToValue()
{
bool conv = true;
int count = 0;
int tcount = 0;
int setLoc = 0;
int lktLoc = 0;
while(conv)
{
if(count == 4)
{
++setLoc;
count = 0;
}
intToCharMap[lktLoc] = charSet[setLoc];
++lktLoc;
++count;
if(tcount == maxColorValue)
conv = false;
++tcount;
}
for (int i = 0; i < 256; ++i)
{
charmap.insert(std::pair<char, int>(intToCharMap[i], i));
i += 3;
}
}
void ImageConverter::stringsToImage(std::string msg)
{
char tempchar = ' ';
char pasttempchar = ' ';
std::string tempstring = "";
int row = 0;
int spec = 0;
int value = 0;
for (int i = 0; i < msg.length(); ++i)
{
tempstring = "";
tempchar = msg[i];
if (tempchar == '|')
{
i++;
tempchar = msg[i];
while(tempchar != '|')
{
tempstring += tempchar;
i++;
tempchar = msg[i];
}
i++;
std::string token = tempstring.substr(0,1);
std::string token2 = tempstring.substr(1,tempstring.size());
spec = std::atoi(token.c_str());
row = std::atoi(token2.c_str());
}
tempchar = msg[i];
if(tempchar < '0' || tempchar > '9')
{
imageMatrix[spec][col[spec][row]][row] = charmap.find(tempchar)->second;
col[spec][row]++;
pasttempchar = tempchar;
}
else
{
i++;
tempstring = "";
tempstring += tempchar;
tempchar = msg[i];
while(tempchar >= '0' && tempchar <= '9' )
{
tempstring += tempchar;
i++;
}
value = std::atoi(tempstring.c_str());
for (int j = 0; j < value-1; ++j)
{
imageMatrix[spec][col[spec][row]][row] = charmap.find(pasttempchar)->second;
col[spec][row]++;
}
i--;
}
}
}
void ImageConverter::imageToString(int image[3][mwidth][mwidth])
{
// Holds one portion of the image up to 210 characters
std::string msg;
// Number of repeating pixel values
int compressionCount;
// Pointer as we traverse the image
char currentChar;
// Pointer as we traverse the image
char nextChar;
// Temporary string for checking message length before committing
std::string tempMsg = "";
// For each channel(RGB)
for(int rgb = 0; rgb < 3; rgb++){
compressionCount = 0;
//For each row
for(int row = 0; row < mwidth; row++){
compressionCount = 0;
// Denote spectrum and row value at the beginning of each row
tempMsg = '|' + std::to_string(rgb) + std::to_string(row) + '|'; // "|[rgb][0-99]|"
if( msg.size() + tempMsg.size() > maxstring-2){ //If adding to message exceeds limit
//TODO push out message
stringBuffer[size_stringBuffer++] = msg;
msg = "";
}
msg += tempMsg;
tempMsg = "";
// For each column
for(int column = 0; column < mwidth; column++){
//get a char for each pixel value( [0-3] -> a )
currentChar = intToCharMap[image[rgb][column][row]];
if(column == 99){ // If at end of row
nextChar = '/';
}
else{
nextChar = intToCharMap[image[rgb][column+1][row]];
}
// If another repeated character
if(currentChar == nextChar){
compressionCount++; // Increment repeated count
}
else{
compressionCount++; // Current char is the same so add to repeated count
tempMsg += currentChar; // Push the current char to msg string
tempMsg += std::to_string(compressionCount); // Append the count of that character
if(tempMsg.size() + msg.size() > maxstring){ //If adding to the msg will exceed limit
//TODO push out message
stringBuffer[size_stringBuffer++] = msg;
msg = '|' + std::to_string(rgb) + std::to_string(row) + '|'; // "|[rgb][0-99]|"
}
msg += tempMsg;
tempMsg = "";
compressionCount = 0; // Reset count
}
} // end for imageLength
}// end for imageWidth
} // end for rgb
//TODO push out message
stringBuffer[size_stringBuffer++] = msg;
} // end imageToString