-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
78 lines (63 loc) · 2.3 KB
/
Main.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
/*
** Main.cpp - a simple program to load an image, compress + decompress it and show the results
*/
#include "Compressor.h"
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <conio.h> // wait until any key is pressed
#define IMAGE_SIZE 512
using namespace cv;
using namespace std;
void distortImage(const uchar* original, uchar* distorted, int rows, int columns)
{
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
int index = row * columns + column;
uchar difference = (column % 32);
if (row % 2)
distorted[index] = difference + original[index] > 255 ? 255 : original[index] + difference;
else
distorted[index] = difference > original[index] ? 0 : original[index] - difference;
}
}
}
int main()
{
Mat image;
image = imread("../../Beach.jpeg", CV_LOAD_IMAGE_GRAYSCALE); // path should be relative to VS project file
if (image.data == NULL)
{
cout << "Failed to load image" << endl;
return 1;
}
uchar* distorted = new uchar[IMAGE_SIZE * IMAGE_SIZE];
uchar* uncompressed = new uchar[IMAGE_SIZE*IMAGE_SIZE];
distortImage(image.data, distorted, IMAGE_SIZE, IMAGE_SIZE);
Compressor compressor(IMAGE_SIZE, IMAGE_SIZE);
/*char* compressed = NULL;
int compessedLength = compressor.CompressImage(distorted, image.data, compressed);
compressor.DecompressImage(distorted, compressed, uncompressed);*/
uchar* compressed = NULL;
int compessedLength = compressor.CompressImagePNG(image.data, (compressed));
compressor.DecompressImagePNG(compressed, uncompressed);
cout << "Compressed image has " << compessedLength << " bytes" << endl;
bool imagesAreEqual = true;
for (int row = 0; row < IMAGE_SIZE; row++)
{
for (int column = 0; column < IMAGE_SIZE; column++)
{
int index = row * IMAGE_SIZE + column;
imagesAreEqual = imagesAreEqual && (uncompressed[index] == image.data[index]);
if (uncompressed[index] != image.data[index])
cout << "orig: " << image.data[index] << ", uncompressed: " << uncompressed[index] << endl;
}
}
cout << "Decompressed is " << (imagesAreEqual ? "" : "not ") << "the same as original" << endl;
Mat uncompressedImage = Mat(IMAGE_SIZE, IMAGE_SIZE, CV_8UC1, uncompressed);
imshow("uncompressed", uncompressedImage);
waitKey(1);
cout << "Press any key to continue" << endl;
while (!_kbhit());
}