Projeto ITP - Esteganografia 1.0
Loading...
Searching...
No Matches
Image Class Reference

Defines a dinamically allocated PPM image and provides various methods to manipulate and access the image data. More...

#include <Image.h>

Public Member Functions

 Image ()
 Default constructor for the Image class.
 
 Image (int w, int h)
 Constructor to initialize an image with given width and height.
 
int GetWidth () const
 Gets the Width object.

 
int GetHeight () const
 Gets the Height object.
 
void AllocatePixels ()
 Memory allocation of the matrix of pixels, which is fundamental in defining any Image object.
 
void LiberatePixels ()
 Deallocates the memory previously allocated by the AllocatePixels() function, as well as the memory alocated by the array of the pixels' 'R', 'G' and 'B' values.
 
void GetPixels ()
 Function that fills in the matrix of pixels.
 
bool ExtensionCheck (const string &input_text, const string &file_type)
 Function that checks if a string ends with the valid substring.
 
void PrintPPM () const
 Function that prints out the parameters of a PPM image. Mostly used for testing purposes.
 
string CreatePPM () const
 Function that creates a string containing all the data of a PPM image.
 
bool ReadPPM (const string &file_name)
 Reads and dynamically stores the content of an image from a valid PPM file.
 
void ShowArray () const
 Function that outputs a previously defined array of 'R', 'G' and 'B' values. Mostly used for testing purposes.
 
string IntToBin (int num, int bits=8)
 Function that receives an integer value and converts it to binary.
 
void CodeMsg (string msg)
 Main function used in encrypting and hiding an ASCII message inside the least significant bits (LSB) of a PPM image's pixels.
 
int BinToInt (string bin_num, int index=0)
 Recursive function used to convert a binary value into an integer value.
 
void DecodeMsg ()
 Main function used in uncovering and decrypting an ASCII message inside the least significant bits (LSB) of a PPM image's pixels.
 

Detailed Description

Defines a dinamically allocated PPM image and provides various methods to manipulate and access the image data.

Constructor & Destructor Documentation

◆ Image() [1/2]

Image::Image ( )
inline

Default constructor for the Image class.

43: width(0), height(0), pixel(nullptr), pixel_array(nullptr) {}

◆ Image() [2/2]

Image::Image ( int w,
int h )
inline

Constructor to initialize an image with given width and height.

< This variable defines the size of the array of pixels, counting each R, G and B value as a separate unit of the array.

48 : width(w), height(h)
49 {
50 size = (width * height) * 3;
51 }

Member Function Documentation

◆ AllocatePixels()

void Image::AllocatePixels ( )
inline

Memory allocation of the matrix of pixels, which is fundamental in defining any Image object.

73 {
74 pixel = new Pixel *[height];
75 for (int i = 0; i < height; i++)
76 {
77 pixel[i] = new Pixel[width];
78 }
79 }
Struct that defines the pixel of a PPM image.
Definition Image.h:22

◆ BinToInt()

int Image::BinToInt ( string bin_num,
int index = 0 )
inline

Recursive function used to convert a binary value into an integer value.

Parameters
bin_numString containing the binary value that will be converted into an integer.
indexThe index of the string.
Returns
the sum of the current and subsequent values acquired recursively.

Base case: if the index reaches the end of the binary string.

Recursive call to calculate the value of the current and subsequent binary digits.

357 {
359 if (index == bin_num.length())
360 {
361 return 0;
362 }
363
365 int value_current = (bin_num[index] - '0') * pow(2, bin_num.length() - 1 - index);
366 int value_subsequent = BinToInt(bin_num, index + 1);
367
368 return value_current + value_subsequent;
369 }
int BinToInt(string bin_num, int index=0)
Recursive function used to convert a binary value into an integer value.
Definition Image.h:356

◆ CodeMsg()

void Image::CodeMsg ( string msg)
inline

Main function used in encrypting and hiding an ASCII message inside the least significant bits (LSB) of a PPM image's pixels.

Parameters
msgMessage that will be stored inside the image.

‍The number of characters in the message.

‍Array that stores the ASCII values of each of the message's characters.

Fills in the chars[] array.

Stores the binary value of the number of characters in the message.

Modification of the first 8 bits of the array of 'R', 'G' and 'B' values, which store the message's size.

Modifies the bits of the array of 'R', 'G' and 'B' values which will store the actual message.

‍Index for each of the message's bits.

289 {
290 int t = msg.size();
291 int chars[t];
294 for (int i = 0; i < t; ++i)
295 {
296 chars[i] = (int)msg[i];
297 }
298
300 string bin = IntToBin(t);
301
303 for (int i = 0; i < 8 && i < size; ++i)
304 {
305 if (bin[i] == '1')
306 {
307 if (pixel_array[i] % 2 == 0)
308 {
309 pixel_array[i]++;
310 }
311 }
312 else
313 {
314 if (pixel_array[i] % 2 != 0)
315 {
316 pixel_array[i]++;
317 }
318 }
319 }
320
322 int bit_index = 0;
323 for (int j = 0; j < t; ++j)
324 {
325 bin = IntToBin(chars[j]);
326 for (int i = 0; i < 8 && (bit_index + 8) < size; ++i, ++bit_index)
327 {
328 if (bin[i] == '1')
329 {
330 if (pixel_array[bit_index + 8] % 2 == 0)
331 {
332 pixel_array[bit_index + 8]++;
333 }
334 }
335 else
336 {
337 if (pixel_array[bit_index + 8] % 2 != 0)
338 {
339 pixel_array[bit_index + 8]++;
340 if (pixel_array[bit_index + 8] == 255)
341 {
342 pixel_array[bit_index + 8]--;
343 }
344 }
345 }
346 }
347 }
348 }
string IntToBin(int num, int bits=8)
Function that receives an integer value and converts it to binary.
Definition Image.h:267

◆ CreatePPM()

string Image::CreatePPM ( ) const
inline

Function that creates a string containing all the data of a PPM image.

Returns
all the information needed to define a PPM image.

Gets the pixels from a previously defined array of pixels.

170 {
171 stringstream img_content;
172
173 img_content << "P3" << endl;
174 img_content << width << " " << height << endl;
175 img_content << max_color << endl;
176
178 for (int i = 0; i < size; i += 3)
179 {
180 img_content << pixel_array[i] << " " << pixel_array[i + 1] << " " << pixel_array[i + 2] << " ";
181 if ((i / 3 + 1) % width == 0)
182 {
183 img_content << endl;
184 }
185 }
186 return img_content.str();
187 }

◆ DecodeMsg()

void Image::DecodeMsg ( )
inline

Main function used in uncovering and decrypting an ASCII message inside the least significant bits (LSB) of a PPM image's pixels.

‍String that stores the size of the message in binary.

Gets (in binary) the size of the message stored in the image from the first 8 bits of it's pixels' 'R', 'G' and 'B' values, previously stored in an array.

Adds the character '0' to bin_size.

Adds the character '1' to bin_size.

‍String that stores the ASCII message in binary.

‍Stores the size of the image as an integer value.

Gets (in binary) the message stored in the image, using the message's size and using the array of the pixels' 'R', 'G' and 'B' values.

Adds the character '0' to bin_msg.

Adds the character '1' to bin_msg.

‍String that stores the completely decoded message.

Repeats the process below until every character of the message has been decoded and stored.

‍Stores a binary value of a single character.

Gets 8 bits (or a single character) from the message in binary, then adds up so it does the same to the next bit (if there is a next bit).

‍Stores the decoded ASCII character as a char.

Pushes a new decoded character into the string that stores the message.

375 {
376 string bin_size;
379 for (int i = 0; i < 8; ++i)
380 {
381 if (pixel_array[i] % 2 == 0)
382 {
383 bin_size.push_back('0');
384 }
385 else
386 {
387 bin_size.push_back('1');
388 }
389 }
390
391 string bin_msg;
392 int msg_size = BinToInt(bin_size);
395 for (int i = 8; i < 8 + (msg_size * 8); ++i)
396 {
397 if (pixel_array[i] % 2 == 0)
398 {
399 bin_msg.push_back('0');
400 }
401 else
402 {
403 bin_msg.push_back('1');
404 }
405 }
406
407 string decoded_msg;
410 for(int j = 0; j < msg_size; ++j)
411 {
412 string bin_char;
415 for(int i = 0; i < 8; ++i)
416 {
417 bin_char.push_back(bin_msg[i + (8 * j)]);
418 }
419
420 char single_char = BinToInt(bin_char);
423 decoded_msg.push_back(single_char);
424 }
425 cout << "\033[94mA mensagem escondida é: \033[0m" + decoded_msg << "\n";
426 }

◆ ExtensionCheck()

bool Image::ExtensionCheck ( const string & input_text,
const string & file_type )
inline

Function that checks if a string ends with the valid substring.

Parameters
input_textText inputed into the function, which is to be checked.
file_typeThe substring which you wish to check if exists in the end of a given string.
Returns
true if the substring exists in the end of the given string, false otherwise.
128 {
129 if (input_text.length() >= file_type.length())
130 {
131 return (input_text.compare(input_text.length() - file_type.length(), file_type.length(), file_type) == 0);
132 }
133 else
134 {
135 return false;
136 }
137 }

◆ GetHeight()

int Image::GetHeight ( ) const
inline

Gets the Height object.

65 {
66 return height;
67 }

◆ GetPixels()

void Image::GetPixels ( )
inline

Function that fills in the matrix of pixels.

107 {
108 for (int h = 0; h < height; h++)
109 {
110 for (int w = 0; w < width; w++)
111 {
112 int r, g, b;
113 cin >> r >> g >> b;
114 pixel[h][w].r = r;
115 pixel[h][w].g = g;
116 pixel[h][w].b = b;
117 }
118 }
119 }
unsigned char r
Definition Image.h:23
unsigned char b
Definition Image.h:23
unsigned char g
Definition Image.h:23

◆ GetWidth()

int Image::GetWidth ( ) const
inline

Gets the Width object.

57 {
58 return width;
59 }

◆ IntToBin()

string Image::IntToBin ( int num,
int bits = 8 )
inline

Function that receives an integer value and converts it to binary.

Parameters
numInteger value to be converted into binary.
bitsThe max size of the returned binary value.
Returns
a string containing the binary representation of the number received.

Base case: when there are no more bits to convert.

Determines the current bit in binary.

Recursive call to calculate the subsequent bits in binary.

268 {
270 if (bits == 0)
271 {
272 return "";
273 }
274
276 int bit_current = num % 2;
277
279 string bit_subsequent = IntToBin(num / 2, bits - 1);
280
281 return bit_subsequent + (bit_current == 0 ? '0' : '1');
282 }

◆ LiberatePixels()

void Image::LiberatePixels ( )
inline

Deallocates the memory previously allocated by the AllocatePixels() function, as well as the memory alocated by the array of the pixels' 'R', 'G' and 'B' values.

Deallocates the memory utilized by the array of the pixels' 'R', 'G' and 'B' values, used in the CreatePPM(), ReadPPM(), ShowArray(), CodeMsg() and DecodeMsg() functions.

85 {
86 if (pixel != nullptr)
87 {
88 for (int i = 0; i < height; i++)
89 {
90 delete[] pixel[i];
91 }
92 delete[] pixel;
93 pixel = nullptr;
94 }
96 if (pixel_array != nullptr)
97 {
98 delete[] pixel_array;
99 pixel_array = nullptr;
100 }
101 }

◆ PrintPPM()

void Image::PrintPPM ( ) const
inline

Function that prints out the parameters of a PPM image. Mostly used for testing purposes.

143 {
144 cout << "\033[94mDados da imagem: \033[0m\n";
145 cout << "\033[33m- Comprimento: \033[0m\n" << width << endl;
146 cout << "\033[33m- Largura: \033[0m\n" << height << endl;
147 cout << "\033[33m- Tipo de imagem: \033[0m\n" << img_type << endl;
148 cout << "\033[33m- Valor máximo de cor: \033[0m\n" << max_color << endl;
149 cout << "\033[33m- Valor RGB de cada pixel:\033[0m\n";
150
151 for (int h = 0; h < height; h++)
152 {
153 for (int w = 0; w < width; w++)
154 {
155 Pixel p = pixel[h][w];
156 cout << (int)p.r << " ";
157 cout << (int)p.g << " ";
158 cout << (int)p.b << " ";
159 }
160 cout << endl;
161 }
162 cout << endl;
163 }

◆ ReadPPM()

bool Image::ReadPPM ( const string & file_name)
inline

Reads and dynamically stores the content of an image from a valid PPM file.

Parameters
file_nameThe name of a PPM file previously stored inside the source folder, which will be read by the function.
Returns
true if the image was successfully read, false otherwise.

Tests if the file does exist inside the source folder.

Tests if the file is in fact a P3/PPM file by looking at it's content.

Memory allocation of the array of pixels, used in decodification and codification.

Reads and stores in an array the pixels' 'R', 'G' and 'B' values.

195 {
197 ifstream file(file_name);
198 if (!file)
199 {
200 cerr << "\033[31mErro de leitura. O arquivo" << file_name << " não existe.\033[0m\n";
201 return false;
202 }
203
204 string file_type;
205 file >> file_type;
206
207
208
210 if (file_type != "P3")
211 {
212 cerr << "\033[31mErro de leitura. O arquivo " << file_name << " não é uma imagem PPM no formato P3.\033[0m\n" << endl;
213 return false;
214 }
215
216 file >> width >> height >> max_color;
217 size = width * height * 3;
218
220
222 pixel_array = new int[size];
223 int index = 0;
224
225 for (int h = 0; h < height; h++)
226 {
227 for (int w = 0; w < width; w++)
228 {
229 int r, g, b;
230 file >> r >> g >> b;
231 pixel[h][w].r = r;
232 pixel[h][w].g = g;
233 pixel[h][w].b = b;
235 pixel_array[index++] = r;
236 pixel_array[index++] = g;
237 pixel_array[index++] = b;
238 }
239 }
240 file.close();
241 return true;
242 }
void AllocatePixels()
Memory allocation of the matrix of pixels, which is fundamental in defining any Image object.
Definition Image.h:72

◆ ShowArray()

void Image::ShowArray ( ) const
inline

Function that outputs a previously defined array of 'R', 'G' and 'B' values. Mostly used for testing purposes.

248 {
249 if (pixel_array == nullptr)
250 {
251 cout << "Array não inicializado." << endl;
252 return;
253 }
254 for (int i = 0; i < size; i += 3)
255 {
256 cout << "Pixel [" << i / 3 << "]: R = " << pixel_array[i] << ", G = " << pixel_array[i + 1]
257 << ", B = " << pixel_array[i + 2] << endl;
258 }
259 }

The documentation for this class was generated from the following file: