11#include " ../../include/bitmap.hpp" // Using BmpTool API
2- #include " ../../src/bitmapfile/bitmap_file.h" // For BITMAPFILEHEADER for writeFile logic
32
43#include < iostream> // For std::cout, std::cerr
5- #include < vector> // For std::vector
4+ #include < vector> // For std::vector (used by BmpTool::Bitmap)
65#include < string> // For std::string
7- #include < fstream> // For std::ifstream, std::ofstream
8- #include < cstdint> // For uint8_t etc.
9- #include < span> // For std::span
10- #include < cstring> // For std::memcpy
11-
12- // File I/O Utilities
13- std::vector<uint8_t > readFile (const std::string& filename) {
14- std::ifstream file (filename, std::ios::binary | std::ios::ate);
15- if (!file.is_open ()) {
16- std::cerr << " Error: Could not open file for reading: " << filename << std::endl;
17- return {};
18- }
19- std::streamsize size = file.tellg ();
20- file.seekg (0 , std::ios::beg);
21- std::vector<uint8_t > buffer (static_cast <size_t >(size)); // Ensure size_t for vector constructor
22- if (file.read (reinterpret_cast <char *>(buffer.data ()), size)) {
23- return buffer;
24- }
25- std::cerr << " Error: Could not read file: " << filename << std::endl;
26- return {};
27- }
28-
29- bool writeFile (const std::string& filename, const std::vector<uint8_t >& data) {
30- std::ofstream file (filename, std::ios::binary);
31- if (!file.is_open ()) {
32- std::cerr << " Error: Could not open file for writing: " << filename << std::endl;
33- return false ;
34- }
35- file.write (reinterpret_cast <const char *>(data.data ()), data.size ());
36- if (!file.good ()) {
37- std::cerr << " Error: Could not write all data to file: " << filename << std::endl;
38- return false ;
39- }
40- return true ;
41- }
6+ #include < cstdint> // For uint8_t etc. (used by BmpTool::Bitmap)
427
438// Helper to print BmpTool errors
449void printError (BmpTool::BitmapError error, const std::string& operation_name) {
@@ -53,19 +18,14 @@ int main() {
5318 std::cout << " ---------------------------------------" << std::endl;
5419 std::cout << " Attempting to load image: " << inputFilename << std::endl;
5520
56- std::vector< uint8_t > file_data = readFile (inputFilename);
57- if (file_data. empty ()) {
21+ BmpTool::Result<BmpTool::Bitmap, BmpTool::BitmapError> load_result = BmpTool::load (inputFilename);
22+ if (!load_result. isSuccess ()) {
5823 std::cerr << " ********************************************************************************" << std::endl;
59- std::cerr << " Error: Could not read '" << inputFilename << " '." << std::endl;
60- std::cerr << " Please ensure this file exists in the same directory as the executable." << std::endl;
24+ std::cerr << " Error: Could not load '" << inputFilename << " ' using BmpTool::load ." << std::endl;
25+ std::cerr << " Please ensure this file exists in the same directory as the executable and is a valid BMP ." << std::endl;
6126 std::cerr << " You can copy any BMP image (e.g., from Windows, or download one) and rename it to 'test.bmp'." << std::endl;
6227 std::cerr << " A common source of BMP files is MS Paint (save as BMP)." << std::endl;
6328 std::cerr << " ********************************************************************************" << std::endl;
64- return 1 ;
65- }
66-
67- BmpTool::Result<BmpTool::Bitmap, BmpTool::BitmapError> load_result = BmpTool::load (file_data);
68- if (!load_result.isSuccess ()) {
6929 printError (load_result.error (), " loading " + inputFilename);
7030 return 1 ;
7131 }
@@ -80,19 +40,12 @@ int main() {
8040 auto invert_result = BmpTool::invertColors (originalBitmap);
8141 if (invert_result.isSuccess ()) {
8242 currentBitmap = invert_result.value ();
83- std::vector<uint8_t > output_buffer (54 + currentBitmap.w * currentBitmap.h * 4 ); // Estimate
84- auto save_res = BmpTool::save (currentBitmap, output_buffer);
85- if (save_res.isSuccess ()) {
86- BITMAPFILEHEADER fh_out;
87- std::memcpy (&fh_out, output_buffer.data (), sizeof (BITMAPFILEHEADER));
88- std::vector<uint8_t > actual_data (output_buffer.begin (), output_buffer.begin () + fh_out.bfSize );
89- if (writeFile (" output_inverted.bmp" , actual_data)) {
90- std::cout << " Saved: output_inverted.bmp" << std::endl;
91- } else {
92- std::cerr << " Error writing output_inverted.bmp" << std::endl;
93- }
43+ auto save_res_file = BmpTool::save (currentBitmap, " output_inverted.bmp" );
44+ if (save_res_file.isSuccess ()) {
45+ std::cout << " Saved: output_inverted.bmp" << std::endl;
9446 } else {
95- printError (save_res.error (), " saving inverted image" );
47+ std::cerr << " Error writing output_inverted.bmp directly to file." << std::endl;
48+ printError (save_res_file.error (), " saving inverted image" );
9649 }
9750 } else {
9851 printError (invert_result.error (), " inverting colors" );
@@ -104,19 +57,12 @@ int main() {
10457 auto sepia_result = BmpTool::applySepiaTone (originalBitmap);
10558 if (sepia_result.isSuccess ()) {
10659 currentBitmap = sepia_result.value ();
107- std::vector<uint8_t > output_buffer (54 + currentBitmap.w * currentBitmap.h * 4 );
108- auto save_res = BmpTool::save (currentBitmap, output_buffer);
109- if (save_res.isSuccess ()) {
110- BITMAPFILEHEADER fh_out;
111- std::memcpy (&fh_out, output_buffer.data (), sizeof (BITMAPFILEHEADER));
112- std::vector<uint8_t > actual_data (output_buffer.begin (), output_buffer.begin () + fh_out.bfSize );
113- if (writeFile (" output_sepia.bmp" , actual_data)) {
114- std::cout << " Saved: output_sepia.bmp" << std::endl;
115- } else {
116- std::cerr << " Error writing output_sepia.bmp" << std::endl;
117- }
60+ auto save_res_file = BmpTool::save (currentBitmap, " output_sepia.bmp" );
61+ if (save_res_file.isSuccess ()) {
62+ std::cout << " Saved: output_sepia.bmp" << std::endl;
11863 } else {
119- printError (save_res.error (), " saving sepia image" );
64+ std::cerr << " Error writing output_sepia.bmp directly to file." << std::endl;
65+ printError (save_res_file.error (), " saving sepia image" );
12066 }
12167 } else {
12268 printError (sepia_result.error (), " applying sepia tone" );
@@ -146,19 +92,12 @@ int main() {
14692 tempProcessedBitmap = contrast_result.value ();
14793 std::cout << " Step 2: Contrast increased." << std::endl;
14894
149- std::vector<uint8_t > output_buffer (54 + tempProcessedBitmap.w * tempProcessedBitmap.h * 4 );
150- auto save_res = BmpTool::save (tempProcessedBitmap, output_buffer);
151- if (save_res.isSuccess ()) {
152- BITMAPFILEHEADER fh_out;
153- std::memcpy (&fh_out, output_buffer.data (), sizeof (BITMAPFILEHEADER));
154- std::vector<uint8_t > actual_data (output_buffer.begin (), output_buffer.begin () + fh_out.bfSize );
155- if (writeFile (" output_blurred_contrasted.bmp" , actual_data)) {
156- std::cout << " Saved: output_blurred_contrasted.bmp" << std::endl;
157- } else {
158- std::cerr << " Error writing output_blurred_contrasted.bmp" << std::endl;
159- }
95+ auto save_res_file = BmpTool::save (tempProcessedBitmap, " output_blurred_contrasted.bmp" );
96+ if (save_res_file.isSuccess ()) {
97+ std::cout << " Saved: output_blurred_contrasted.bmp" << std::endl;
16098 } else {
161- printError (save_res.error (), " saving blurred_contrasted image" );
99+ std::cerr << " Error writing output_blurred_contrasted.bmp directly to file." << std::endl;
100+ printError (save_res_file.error (), " saving blurred_contrasted image" );
162101 }
163102 }
164103 }
@@ -185,19 +124,12 @@ int main() {
185124 tempProcessedBitmap = greyscale_result.value ();
186125 std::cout << " Step 2: Greyscale applied." << std::endl;
187126
188- std::vector<uint8_t > output_buffer (54 + tempProcessedBitmap.w * tempProcessedBitmap.h * 4 );
189- auto save_res = BmpTool::save (tempProcessedBitmap, output_buffer);
190- if (save_res.isSuccess ()) {
191- BITMAPFILEHEADER fh_out;
192- std::memcpy (&fh_out, output_buffer.data (), sizeof (BITMAPFILEHEADER));
193- std::vector<uint8_t > actual_data (output_buffer.begin (), output_buffer.begin () + fh_out.bfSize );
194- if (writeFile (" output_shrunk_greyscale.bmp" , actual_data)){
195- std::cout << " Saved: output_shrunk_greyscale.bmp" << std::endl;
196- } else {
197- std::cerr << " Error writing output_shrunk_greyscale.bmp" << std::endl;
198- }
127+ auto save_res_file = BmpTool::save (tempProcessedBitmap, " output_shrunk_greyscale.bmp" );
128+ if (save_res_file.isSuccess ()) {
129+ std::cout << " Saved: output_shrunk_greyscale.bmp" << std::endl;
199130 } else {
200- printError (save_res.error (), " saving shrunk_greyscale image" );
131+ std::cerr << " Error writing output_shrunk_greyscale.bmp directly to file." << std::endl;
132+ printError (save_res_file.error (), " saving shrunk_greyscale image" );
201133 }
202134 }
203135 }
0 commit comments