-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask 5- Image Processing Tool
152 lines (126 loc) · 4.18 KB
/
Task 5- Image Processing Tool
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
// Image Processing Tool
#include <iostream>
#include <fstream>
// Define a structure to store the bitmap header
struct BitmapHeader
{
char signature[2]; // Signature "BM"
int fileSize; // Size of the file in bytes
int reserved; // Reserved for future use
int offset; // Offset to the pixel data
int headerSize; // Size of the header in bytes
int width; // Width of the image in pixels
int height; // Height of the image in pixels
short planes; // Number of color planes
short bitsPerPixel; // Number of bits per pixel
int compression; // Compression method
int imageSize; // Size of the pixel data in bytes
int xResolution; // Horizontal resolution in pixels per meter
int yResolution; // Vertical resolution in pixels per meter
int colorsUsed; // Number of colors used
int colorsImportant; // Number of important colors
};
// Define a function to read a bitmap image from a file
bool readBitmap(const char* fileName, BitmapHeader& header, char*& data)
{
// Open the file in binary mode
std::ifstream file(fileName, std::ios::binary);
// Check if the file is opened successfully
if (!file.is_open())
{
std::cout << "Error: Could not open the file " << fileName << std::endl;
return false;
}
// Read the bitmap header
file.read((char*)&header, sizeof(BitmapHeader));
// Check if the file is a valid bitmap
if (header.signature[0] != 'B' || header.signature[1] != 'M')
{
std::cout << "Error: The file " << fileName << " is not a bitmap" << std::endl;
file.close();
return false;
}
// Check if the bitmap is uncompressed and has 24 bits per pixel
if (header.compression != 0 || header.bitsPerPixel != 24)
{
std::cout << "Error: The bitmap " << fileName << " is not supported" << std::endl;
file.close();
return false;
}
// Allocate memory for the pixel data
data = new char[header.imageSize];
// Check if the memory allocation is successful
if (data == nullptr)
{
std::cout << "Error: Could not allocate memory for the pixel data" << std::endl;
file.close();
return false;
}
// Seek to the pixel data
file.seekg(header.offset, std::ios::beg);
// Read the pixel data
file.read(data, header.imageSize);
// Close the file
file.close();
// Return true on successful reading
return true;
}
// Define a function to write a bitmap image to a file
bool writeBitmap(const char* fileName, const BitmapHeader& header, const char* data)
{
// Open the file in binary mode
std::ofstream file(fileName, std::ios::binary);
// Check if the file is opened successfully
if (!file.is_open())
{
std::cout << "Error: Could not open the file " << fileName << std::endl;
return false;
}
// Write the bitmap header
file.write((char*)&header, sizeof(BitmapHeader));
// Write the pixel data
file.write(data, header.imageSize);
// Close the file
file.close();
// Return true on successful writing
return true;
}
// Define a function to invert the colors of a bitmap image
void invertColors(char* data, int size)
{
// Loop through the pixel data
for (int i = 0; i < size; i++)
{
// Invert each color component by subtracting it from 255
data[i] = 255 - data[i];
}
}
// Define the main function
int main(int argc, char** argv)
{
// Check if the input and output file names are provided as arguments
if (argc != 3)
{
std::cout << "Usage: " << argv[0] << " <input_file> <output_file>" << std::endl;
return -1;
}
// Declare a bitmap header and a pointer to the pixel data
BitmapHeader header;
char* data = nullptr;
// Read the bitmap image from the input file
if (!readBitmap(argv[1], header, data))
{
return -1;
}
// Invert the colors of the bitmap image
invertColors(data, header.imageSize);
// Write the bitmap image to the output file
if (!writeBitmap(argv[2], header, data))
{
return -1;
}
// Free the memory allocated for the pixel data
delete[] data;
// Return 0 on successful execution
return 0;
}