-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp.c
More file actions
218 lines (209 loc) · 4.58 KB
/
bmp.c
File metadata and controls
218 lines (209 loc) · 4.58 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
#include "bmp.h"
#include <string.h>
#include <stdlib.h>
long int _get_file_size(FILE*);
BMPImage* crop_bmp(BMPImage* image, int x, int y, int w, int h, char** error)
{
if(w > image->header.width_px)
{
char* msg = "w is out of bound";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
if(h > image->header.height_px)
{
char* msg = "h is out of bound";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
BMPImage* new = malloc(sizeof(BMPImage));
new->header = image->header;
new->header.width_px = w;
new->header.height_px = h;
int pixel = (new->header.bits_per_pixel / 8);
int pad = w * pixel;
while(pad % 4 != 0)
{
pad++;
}
int pad_ori = image->header.width_px * pixel;
while(pad_ori % 4 != 0)
{
pad_ori++;
}
new->header.image_size_bytes = pad * h;
int size = new->header.image_size_bytes;
new->data = malloc(sizeof(unsigned char)* (size + 1));
new->data[size] = '\0';
int target = (pad_ori * (image->header.height_px - (y + h))) + (x * pixel);
int temp = 0;
for(int j = 0; j < h; j++)
{
for(int k = 0; k < (w*pixel); k++)
{
new->data[temp] = image->data[target + k];
temp++;
}
for(int k1 = 0; k1 < (pad - (w*pixel)); k1++)
{
new->data[temp] = 0;
temp++;
}
target = pad_ori + target;
}
return new;
}
BMPImage* read_bmp(FILE* fp, char**error)
{
BMPImage* image;
image = malloc(sizeof(*image) * 1);
if(fp == NULL)
{
free(image);
char* msg = "fail to read 6x6.bmp";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
if(fread(&(image->header), sizeof(BMPHeader), 1, fp) != 1)
{
free(image);
char* msg = "fread image.header fail";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
if(!check_bmp_header(&(image->header), fp))
{
free(image);
char* msg = "check_bmp_hdr invalid";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
int image_size = image->header.size - sizeof(BMPHeader);
image->data = malloc(sizeof(*(image->data)) * (image_size));
if(image->data == NULL)
{
free(image->data);
free(image);
char* msg = "malloc fail";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
fseek(fp, sizeof(BMPHeader),SEEK_SET);
if(fread(image->data, image_size, sizeof(char),fp) != 1)
{
free(image->data);
free(image);
char* msg = "fread image->data fail";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
char lastbyte;
if(fread(&lastbyte, sizeof(char), 1, fp) != 0)
{
free(image->data);
free(image);
char* msg = "lastbyte fail";
*error = malloc(sizeof(**error) * (strlen(msg)+1));
strcpy(*error, msg);
return NULL;
}
return image;
}
bool check_bmp_header(BMPHeader* bmp_hdr, FILE* fp)
{
if(bmp_hdr -> type != 0x4d42)
{
return false;
}
if(bmp_hdr->offset != BMP_HEADER_SIZE)
{
return false;
}
if(bmp_hdr->dib_header_size != DIB_HEADER_SIZE)
{
return false;
}
if(bmp_hdr->num_planes != 1)
{
return false;
}
if(bmp_hdr->compression != 0)
{
return false;
}
if(bmp_hdr->num_colors != 0 || bmp_hdr->important_colors != 0)
{
return false;
}
if(bmp_hdr->bits_per_pixel != 16 && bmp_hdr->bits_per_pixel != 24)
{
return false;
}
long int actualFileSize = _get_file_size(fp);
if(actualFileSize != bmp_hdr->size)
{
return false;
}
if(bmp_hdr->image_size_bytes != bmp_hdr->width_px * (bmp_hdr->bits_per_pixel / 8) * bmp_hdr->height_px)
{
int pad = bmp_hdr->width_px * (bmp_hdr->bits_per_pixel / 8);
while(pad % 4 != 0)
{
pad++;
}
if(bmp_hdr->image_size_bytes != pad * bmp_hdr->height_px)
{
return false;
}
}
return true;
}
bool write_bmp(FILE* fp, BMPImage* image, char** error)
{
if(fp == NULL)
{
char* msg = "fopen fail";
*error = malloc(sizeof(**error) * (strlen(msg) + 1));
strcpy(*error, msg);
return false;
}
if(fwrite(&(image->header), sizeof(BMPHeader), 1, fp) != 1)
{
char* msg = "fwrite image fail";
*error = malloc(sizeof(**error) * (strlen(msg) + 1));
strcpy(*error, msg);
return false;
}
if(fwrite(image->data, image->header.image_size_bytes, sizeof(unsigned char), fp) != 1)
{
char* msg = "fwrite imagedata fail";
*error = malloc(sizeof(**error) * (strlen(msg) + 1));
strcpy(*error, msg);
return false;
}
return true;
}
long int _get_file_size(FILE* fp)
{
fseek(fp, 0, SEEK_END);
long int size = ftell(fp);
return size;
}
void free_bmp(BMPImage* image)
{
if(image != NULL)
{
if(image->data != NULL)
{
free(image->data);
}
free(image);
}
}