-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp.c
336 lines (278 loc) · 8.65 KB
/
bmp.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bmp.h"
// Correct values for the header
#define MAGIC_VALUE 0x4D43
#define NUM_PLANE 1
#define COMPRESSION 0
#define NUM_COLORS 0
#define IMPORTANT_COLORS 0
#define BITS_PER_PIXEL 24
#define BITS_PER_BYTE 8
BMPImage * read_bmp (FILE *fp, char **error);
bool write_bmp (FILE *fp, BMPImage *image, char **error);
bool check_bmp_header (BMPHeader *bmp_header, FILE *fp);
void free_bmp (BMPImage *image);
long _get_file_size(FILE *fp);
int _get_image_size_bytes (BMPHeader *bmp_header);
int _get_image_row_size_bytes (BMPHeader *bmp_header);
int _get_bytes_per_pixel (BMPHeader *bmp_header);
int _get_padding (BMPHeader *bmp_header);
int _get_position_x_row (int x, BMPHeader *bmp_header);
bool _check (bool condition, char **error, const char *error_message);
char *_string_duplicate (const char *string);
BMPImage *read_bmp(FILE *fp, char **error)
{
BMPImage *image = malloc(sizeof(*image));
if (!_check(image != NULL, error, "Not enough memory"))
{
return NULL;
}
///read header
rewind(fp);
int num_read = fread(&image->header, sizeof(image->header), 1, fp);
if (!_check(num_read == 1, error, "Cannot read header"))
{
return NULL;
}
///check header
bool is_valid_header = check_bmp_header(&image->header, fp);
if (_check(is_valid_header, error, "Invalid BMP file"))
{
return NULL;
}
///allocate memory for image data
image->data = malloc (sizeof(*image->data) * image->header.image_size_bytes);
if (!_check(image->data != NULL, error, "Not enough memory"))
{
return NULL;
}
///read image data
num_read = fread(image->data, image->header.image_size_bytes, 1, fp);
if (!_check(num_read == 1, error, "Cannot read image"))
{
return NULL;
}
return image;
}
bool write_bmp (FILE *fp, BMPImage *image, char **error)
{
///write the header
rewind(fp);
int num_read = fwrite(&image->header, sizeof(image->header), 1, fp);
if (!_check(num_read == 1, error, "Cannot write image"))
{
return false;
}
///write the data
num_read = fwrite (image->data, image->header.image_size_bytes, 1, fp);
if (!_check(num_read == 1, error, "Cannot wrire image"))
{
return false;
}
return true;
}
/**
*
* Test if the BMPHeader is consistent with itself and the already open file
*
* Return true if the given BMPHeader is valid
*/
bool check_bmp_header (BMPHeader * bmp_header, FILE *fp)
{
/**
A header is valid if:
1. its magic number is 0x4D42,
2. image data begins immediatly after the header data (header->offset == BMP_HEADER_SIZE),
3. the DIB header is the correct size (DIB_HEADER_SIZE),
4. there is only one image plane,
5. there is no compression (header->compression == 0),
6. num_colors and important colors_colors are both 0,
7. the image has 24 bits per pixel,
8. the size and image_size_bytes fields are correct in relation to the bits,
width, and height fields and in relation to the field size.
*/
return (
bmp_header->type == MAGIC_VALUE
&& bmp_header->offset == BMP_HEADER_SIZE
&& bmp_header->dib_header_size == DIB_HEADER_SIZE
&& bmp_header->num_planes == NUM_PLANE
&& bmp_header->compression == COMPRESSION
&& bmp_header->num_colors == NUM_COLORS && bmp_header->important_colors == IMPORTANT_COLORS
&& bmp_header->bits_per_pixel == BITS_PER_PIXEL
&& bmp_header->fsize == _get_file_size(fp) && bmp_header->image_size_bytes == _get_image_size_bytes(bmp_header)
);
}
/**
* Free all memory referred to by the given BMPImage
*/
void free_bmp(BMPImage *image)
{
free (image->data);
free (image);
}
/**
* Create a new image containing the cropped of the given image
* - Params:
* x - the start index, from the left edge of the input image.
* y - the start index, from the top edge of the input image.
* w - the width of the new image.
* h - the height of the new image.
* - Postconditions: it is the caller's responsibility to free the memory
* for the error message and the returned image.
* - Return: the cropped image as a BMPImage on the heap
*/
BMPImage *crop_bmp (BMPImage *image, int x, int y, int w, int h, char **error)
{
BMPImage *new_image = malloc(sizeof(*new_image));
/// Check size of cropedd image is less or equal than the size of original image
if (!_check
(
x + w <= image->header.width_px && y + h <= image->header.height_px,
error,
"The size of the new image should be equal or less than the size of the original")
)
{
return NULL;
}
/// Update new image header
new_image->header = image->header;
new_image->header.width_px = w;
new_image->header.height_px = h;
new_image->header.image_size_bytes = _get_image_size_bytes (&new_image->header);
new_image->header.fsize = BMP_HEADER_SIZE + new_image->header.image_size_bytes;
/// Allocate memory for image data
new_image->data = malloc(sizeof(*new_image->data) * new_image->header.image_size_bytes);
if(!_check(new_image->data != NULL, error, "Not enough memory"))
{
return NULL;
}
int position_y = y * _get_image_row_size_bytes (&image->header);
int position_x_row = _get_position_x_row(x, &image->header);
int new_index = 0;
// Iterate image's columns
for (int i = 0; i < h; i++)
{
// Iterate image's rows
for (int j = 0; j < w; j++)
{
// Iterate image's pixels
for(int k = 0; k < 3; k++)
{
new_image->data[new_index] = image->data[position_y + position_x_row];
new_index++;
position_x_row++;
}
}
/// Add padding to new_image
int padding = _get_padding(&new_image->header);
for (int l = 0; l < padding; l++)
{
new_image->data[new_index] = 0x00;
new_index++;
}
position_y += _get_image_row_size_bytes(&image->header);
position_x_row = _get_position_x_row(x, &image->header);
}
return new_image;
}
/**
* Return the size of the file.
*/
long _get_file_size(FILE *fp)
{
//Get current file position
long current_position = ftell(fp);
if (current_position == -1)
{
return -1;
}
// Set file position to the end
if (fseek(fp, 0, SEEK_END) != 0)
{
return -2;
}
// Get current file position (now at the end)
long file_size = ftell(fp);
if (file_size == -1)
{
return -3;
}
// Restore previous file position
if (fseek(fp, current_position, SEEK_SET) != 0)
{
return -4;
}
return file_size;
}
/**
* Return the size of the image in bytes.
*/
int _get_image_size_bytes (BMPHeader *bmp_header)
{
return _get_image_row_size_bytes (bmp_header) * bmp_header->height_px;
}
/**
* Return the size of an image row in bytes.
* . Precondition: the header must have the width of the image in pixels.
*/
int _get_image_row_size_bytes (BMPHeader *bmp_header)
{
int bytes_per_row_without_padding = bmp_header ->width_px * _get_bytes_per_pixel (bmp_header);
return bytes_per_row_without_padding + _get_padding (bmp_header);
}
/**
* Return size of padding in bytes.
*/
int _get_padding (BMPHeader *bmp_header)
{
return (4 - (bmp_header ->width_px * _get_bytes_per_pixel (bmp_header)) % 4) % 4;
}
/**
* Return the number of bytes per pixel.
* Precondition:
* - the header must have the number of bits per pixel.
*/
int _get_bytes_per_pixel (BMPHeader *bmp_header)
{
return bmp_header->bits_per_pixel / BITS_PER_BYTE;
}
/**
* Return the position of the pixel x from the beginning of a row.
*/
int _get_position_x_row(int x, BMPHeader *bmp_header)
{
return x * _get_bytes_per_pixel (bmp_header);
}
/**
* Check condition and set error message.
*/
bool _check (bool condition, char **error, const char *error_message)
{
bool is_valid = true;
if(!condition)
{
is_valid = false;
if (*error == NULL)// to avoid memory leaks
{
*error = _string_duplicate (error_message);
}
}
return is_valid;
}
/**
* Make a copy of a string on the heap.
* . Postcondition: the caller is responsible to free
* the memory for the string.
*/
char *_string_duplicate(const char *string)
{
char *copy = malloc(sizeof(*copy) * (strlen(string) + 1));
if (copy == NULL)
{
return "Not enough memory for error message";
}
strcpy(copy, string);
return copy;
}