-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (122 loc) · 6.95 KB
/
main.cpp
File metadata and controls
141 lines (122 loc) · 6.95 KB
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
#include "../../include/bitmap.hpp" // Using BmpTool API
#include <iostream> // For std::cout, std::cerr
#include <vector> // For std::vector (used by BmpTool::Bitmap)
#include <string> // For std::string
#include <cstdint> // For uint8_t etc. (used by BmpTool::Bitmap)
// Helper to print BmpTool errors
void printError(BmpTool::BitmapError error, const std::string& operation_name) {
std::cerr << "Error during " << operation_name << ": " << static_cast<int>(error) << std::endl;
}
int main() {
const std::string inputFilename = "test.bmp";
std::cout << "Bitmap Processing Demo with BmpTool API" << std::endl;
std::cout << "---------------------------------------" << std::endl;
std::cout << "Attempting to load image: " << inputFilename << std::endl;
BmpTool::Result<BmpTool::Bitmap, BmpTool::BitmapError> load_result = BmpTool::load(inputFilename);
if (!load_result.isSuccess()) {
std::cerr << "********************************************************************************" << std::endl;
std::cerr << "Error: Could not load '" << inputFilename << "' using BmpTool::load." << std::endl;
std::cerr << "Please ensure this file exists in the same directory as the executable and is a valid BMP." << std::endl;
std::cerr << "You can copy any BMP image (e.g., from Windows, or download one) and rename it to 'test.bmp'." << std::endl;
std::cerr << "A common source of BMP files is MS Paint (save as BMP)." << std::endl;
std::cerr << "********************************************************************************" << std::endl;
printError(load_result.error(), "loading " + inputFilename);
return 1;
}
BmpTool::Bitmap originalBitmap = load_result.value();
std::cout << "'" << inputFilename << "' loaded successfully. Dimensions: "
<< originalBitmap.w << "x" << originalBitmap.h << std::endl << std::endl;
BmpTool::Bitmap currentBitmap; // To hold results of operations
// --- Example 1: Invert Colors ---
std::cout << "Processing: Invert Colors..." << std::endl;
auto invert_result = BmpTool::invertColors(originalBitmap);
if (invert_result.isSuccess()) {
currentBitmap = invert_result.value();
auto save_res_file = BmpTool::save(currentBitmap, "output_inverted.bmp");
if (save_res_file.isSuccess()) {
std::cout << "Saved: output_inverted.bmp" << std::endl;
} else {
std::cerr << "Error writing output_inverted.bmp directly to file." << std::endl;
printError(save_res_file.error(), "saving inverted image");
}
} else {
printError(invert_result.error(), "inverting colors");
}
std::cout << std::endl;
// --- Example 2: Apply Sepia Tone to the original ---
std::cout << "Processing: Apply Sepia Tone..." << std::endl;
auto sepia_result = BmpTool::applySepiaTone(originalBitmap);
if (sepia_result.isSuccess()) {
currentBitmap = sepia_result.value();
auto save_res_file = BmpTool::save(currentBitmap, "output_sepia.bmp");
if (save_res_file.isSuccess()) {
std::cout << "Saved: output_sepia.bmp" << std::endl;
} else {
std::cerr << "Error writing output_sepia.bmp directly to file." << std::endl;
printError(save_res_file.error(), "saving sepia image");
}
} else {
printError(sepia_result.error(), "applying sepia tone");
}
std::cout << std::endl;
// --- Example 3: Apply Box Blur and then change contrast ---
std::cout << "Processing: Box Blur (Radius 2) then Contrast (Factor 1.5)..." << std::endl;
// currentBitmap = originalBitmap; // This would make a shallow copy if Bitmap struct is not careful.
// For safety, let's reload or re-assign from originalBitmap if we need a pristine copy.
// Or ensure Bitmap struct has proper copy semantics if it manages its own data deeply.
// Given BmpTool::Bitmap is a struct with std::vector, it has deep copy semantics.
BmpTool::Bitmap tempProcessedBitmap = originalBitmap;
auto blur_result = BmpTool::applyBoxBlur(tempProcessedBitmap, 2);
if (!blur_result.isSuccess()) {
printError(blur_result.error(), "applying box blur");
} else {
tempProcessedBitmap = blur_result.value();
std::cout << "Step 1: Box blur applied." << std::endl;
auto contrast_result = BmpTool::changeContrast(tempProcessedBitmap, 1.5f);
if (!contrast_result.isSuccess()) {
printError(contrast_result.error(), "changing contrast after blur");
} else {
tempProcessedBitmap = contrast_result.value();
std::cout << "Step 2: Contrast increased." << std::endl;
auto save_res_file = BmpTool::save(tempProcessedBitmap, "output_blurred_contrasted.bmp");
if (save_res_file.isSuccess()) {
std::cout << "Saved: output_blurred_contrasted.bmp" << std::endl;
} else {
std::cerr << "Error writing output_blurred_contrasted.bmp directly to file." << std::endl;
printError(save_res_file.error(), "saving blurred_contrasted image");
}
}
}
std::cout << std::endl;
// --- Example 4: Demonstrate Shrink Image & Grayscale ---
std::cout << "Processing: Shrink Image (Factor 2) then Grayscale..." << std::endl;
tempProcessedBitmap = originalBitmap; // Start with a fresh copy
auto shrink_result = BmpTool::shrink(tempProcessedBitmap, 2);
if (!shrink_result.isSuccess()) {
printError(shrink_result.error(), "shrinking image");
} else {
tempProcessedBitmap = shrink_result.value();
std::cout << "Step 1: Image shrunk." << std::endl;
if (tempProcessedBitmap.w == 0 || tempProcessedBitmap.h == 0) {
std::cout << "Image shrunk to zero dimensions, skipping further processing and save." << std::endl;
} else {
auto greyscale_result = BmpTool::greyscale(tempProcessedBitmap);
if(!greyscale_result.isSuccess()){
printError(greyscale_result.error(), "applying greyscale after shrinking");
} else {
tempProcessedBitmap = greyscale_result.value();
std::cout << "Step 2: Greyscale applied." << std::endl;
auto save_res_file = BmpTool::save(tempProcessedBitmap, "output_shrunk_greyscale.bmp");
if (save_res_file.isSuccess()) {
std::cout << "Saved: output_shrunk_greyscale.bmp" << std::endl;
} else {
std::cerr << "Error writing output_shrunk_greyscale.bmp directly to file." << std::endl;
printError(save_res_file.error(), "saving shrunk_greyscale image");
}
}
}
}
std::cout << std::endl;
std::cout << "Main example processing complete. Check for output_*.bmp files." << std::endl;
return 0;
}