-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbitmap.cpp
More file actions
291 lines (257 loc) · 7.52 KB
/
bitmap.cpp
File metadata and controls
291 lines (257 loc) · 7.52 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <iostream>
#include <fstream>
#include "bitmap.h"
#include <cstdlib>
typedef unsigned char uchar_t;
typedef unsigned int uint32_t;
typedef unsigned short int uint16_t;
typedef signed int int32_t;
typedef signed short int int16_t;
const int MIN_RGB=0;
const int MAX_RGB=255;
const int BMP_MAGIC_ID=2;
// --------------------------------------------------------------
// Windows BMP-specific format data
struct bmpfile_magic
{
uchar_t magic[BMP_MAGIC_ID];
};
struct bmpfile_header
{
uint32_t file_size;
uint16_t creator1;
uint16_t creator2;
uint32_t bmp_offset;
};
struct bmpfile_dib_info
{
uint32_t header_size;
int32_t width;
int32_t height;
uint16_t num_planes;
uint16_t bits_per_pixel;
uint32_t compression;
uint32_t bmp_byte_size;
int32_t hres;
int32_t vres;
uint32_t num_colors;
uint32_t num_important_colors;
};
// --------------------------------------------------------------
/**
* Opens a file as its name is provided and reads pixel-by-pixel the colors
* into a matrix of RGB pixels. Any errors will cout but will result in an
* empty matrix (with no rows and no columns).
*
* @param name of the filename to be opened and read as a matrix of pixels
**/
void Bitmap::open(std::string filename)
{
std::ifstream file(filename.c_str(), std::ios::in | std::ios::binary);
//clear data if already holds information
for(int i=0; i<pixels.size(); i++)
{
pixels[i].clear();
}
pixels.clear();
if (file.fail())
{
std::cerr<<filename<<" could not be opened. Does it exist? "
<<"Is it already open by another program?\n";
}
else
{
bmpfile_magic magic;
file.read((char*)(&magic), sizeof(magic));
// Check to make sure that the first two bytes of the file are the "BM"
// identifier that identifies a bitmap image.
if (magic.magic[0] != 'B' || magic.magic[1] != 'M')
{
std::cerr<<filename<<" is not in proper BMP format.\n";
}
else
{
bmpfile_header header;
file.read((char*)(&header), sizeof(header));
bmpfile_dib_info dib_info;
file.read((char*)(&dib_info), sizeof(dib_info));
// Check for this here and so that we know later whether we need to insert
// each row at the bottom or top of the image.
bool flip = true;
if (dib_info.height < 0)
{
flip = false;
dib_info.height = -dib_info.height;
}
// Only support for 24-bit images
if (dib_info.bits_per_pixel != 24)
{
std::cerr<<filename<<" uses "<<dib_info.bits_per_pixel
<<"bits per pixel (bit depth). Bitmap only supports 24bit.\n";
}
// No support for compressed images
if (dib_info.compression != 0)
{
std::cerr<<filename<<" is compressed. "
<<"Bitmap only supports uncompressed images.\n";
}
file.seekg(header.bmp_offset);
// Read the pixels for each row and column of Pixels in the image.
for (int row = 0; row < dib_info.height; row++)
{
std::vector <Pixel> row_data;
for (int col = 0; col < dib_info.width; col++)
{
int blue = file.get();
int green = file.get();
int red = file.get();
row_data.push_back( Pixel(red, green, blue) );
}
// Rows are padded so that they're always a multiple of 4
// bytes. This line skips the padding at the end of each row.
file.seekg(dib_info.width % 4, std::ios::cur);
if (flip)
{
pixels.insert(pixels.begin(), row_data);
}
else
{
pixels.push_back(row_data);
}
}
file.close();
}//end else (is an image)
}//end else (can open file)
}
// ----------------------------------------------------------------------------
/**
* Saves the current image, represented by the matrix of pixels, as a
* Windows BMP file with the name provided by the parameter. File extension
* is not forced but should be .bmp. Any errors will print to cerr and will NOT
* attempt to save the file.
*
* @param name of the filename to be written as a bmp image
**/
void Bitmap::save(std::string filename)
{
std::ofstream file(filename.c_str(), std::ios::out | std::ios::binary);
if (file.fail())
{
std::cerr<<filename<<" could not be opened for editing. "
<<"Is it already open by another program or is it read-only?\n";
}
else if( !isImage() )
{
std::cerr<<"Bitmap cannot be saved. It is not a valid image.\n";
}
else
{
// Write all the header information that the BMP file format requires.
bmpfile_magic magic;
magic.magic[0] = 'B';
magic.magic[1] = 'M';
file.write((char*)(&magic), sizeof(magic));
bmpfile_header header = { 0 };
header.bmp_offset = sizeof(bmpfile_magic)
+ sizeof(bmpfile_header) + sizeof(bmpfile_dib_info);
header.file_size = header.bmp_offset
+ (pixels.size() * 3 + pixels[0].size() % 4) * pixels.size();
file.write((char*)(&header), sizeof(header));
bmpfile_dib_info dib_info = { 0 };
dib_info.header_size = sizeof(bmpfile_dib_info);
dib_info.width = pixels[0].size();
dib_info.height = pixels.size();
dib_info.num_planes = 1;
dib_info.bits_per_pixel = 24;
dib_info.compression = 0;
dib_info.bmp_byte_size = 0;
dib_info.hres = 2835;
dib_info.vres = 2835;
dib_info.num_colors = 0;
dib_info.num_important_colors = 0;
file.write((char*)(&dib_info), sizeof(dib_info));
// Write each row and column of Pixels into the image file -- we write
// the rows upside-down to satisfy the easiest BMP format.
for (int row = pixels.size() - 1; row >= 0; row--)
{
const std::vector <Pixel> & row_data = pixels[row];
for (int col = 0; col < row_data.size(); col++)
{
const Pixel& pix = row_data[col];
file.put((uchar_t)(pix.blue));
file.put((uchar_t)(pix.green));
file.put((uchar_t)(pix.red));
}
// Rows are padded so that they're always a multiple of 4
// bytes. This line skips the padding at the end of each row.
for (int i = 0; i < row_data.size() % 4; i++)
{
file.put(0);
}
}
file.close();
}
}
// ----------------------------------------------------------------------------
/**
* Validates whether or not the current matrix of pixels represents a
* proper image with non-zero-size rows and consistent non-zero-size
* columns for each row. In addition, each pixel in the matrix is validated
* to have red, green, and blue components with values between 0 and 255
*
* @return boolean value of whether or not the matrix is a valid image
**/
bool Bitmap::isImage()
{
const int height = pixels.size();
if( height == 0 || pixels[0].size() == 0)
{
return false;
}
const int width = pixels[0].size();
for(int row=0; row < height; row++)
{
if( pixels[row].size() != width )
{
return false;
}
for(int column=0; column < width; column++)
{
Pixel current = pixels[row][column];
if( current.red > MAX_RGB || current.red < MIN_RGB ||
current.green > MAX_RGB || current.green < MIN_RGB ||
current.blue > MAX_RGB || current.blue < MIN_RGB )
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------
/**
* Provides a vector of vector of pixels representing the bitmap
*
* @return the bitmap image, represented by a matrix of RGB pixels
**/
PixelMatrix Bitmap::toPixelMatrix()
{
if( isImage() )
{
return pixels;
}
else
{
return PixelMatrix();
}
}
// ----------------------------------------------------------------------------
/**
* Overwrites the current bitmap with that represented by a matrix of
* pixels. Does not validate that the new matrix of pixels is a proper
* image.
*
* @param a matrix of pixels to represent a bitmap
**/
void Bitmap::fromPixelMatrix(const PixelMatrix & values)
{
pixels = values;
}