-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbmp.c
More file actions
94 lines (83 loc) · 2.8 KB
/
bmp.c
File metadata and controls
94 lines (83 loc) · 2.8 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
//
// Created by Dreamer on 13/12/2017.
//
#include <malloc.h>
#include <stdlib.h>
#include "bmp.h"
read_status from_bmp(FILE* in,image_t *read) {
bmp_header header;
fread(&header, sizeof(header), 1, in);
if(header.bfType!= 19778)
return READ_INVALID_SIGNATURE;
else if(header.biBitCount!= 24)
return READ_INVALID_BITS;
else if(header.bOffBits!= 54)
return READ_INVALID_HEADER;
read->width = header.biWidth;
read->height = header.biHeight;
read->data = malloc(read-> width * abs(read->height)*sizeof(pixel_t));
int padding = (4 - (read->width * sizeof(pixel_t)) % 4) % 4;
pixel_t *temp = read->data;
for (int i = 0; i < abs(read->height) ; ++i) {
for (int j = 0; j < read->width ; ++j) {
fread(temp,sizeof(pixel_t),1,in);
temp++;
}
fseek(in,padding,SEEK_CUR);
}
return READ_OK;
}
write_status to_bmp(FILE* out, image_t* img) {
bmp_header header;
header.bfType = 19778;
header.biWidth = (uint32_t) abs(img->width);
header.biHeight = img->height;
int padding = (4 - (header.biWidth * sizeof(pixel_t)) % 4) % 4;
header.bOffBits = 54;
header.biBitCount = 24;
header.biSizeImage = abs(header.biHeight) * header.biWidth * sizeof(pixel_t) + abs(header.biHeight)*padding;
header.bfileSize = header.bOffBits + header.biSizeImage;
header.bfReserved = 0;
header.biCompression = 0;
header.biSize = 40;
header.biPlanes = 1;
header.biClrUsed = 0;
header.biClrImportant = 0;
header.biXPelsPerMeter = 3780;
header.biYPelsPerMeter = 3780;
fwrite(&header,sizeof(bmp_header),1,out);
for (int i = 0; i < abs(header.biHeight); i++) {
for (int j = 0; j < header.biWidth; j++) {
pixel_t triple;
triple = img->data[header.biWidth*i + j];
fwrite(&triple,sizeof(pixel_t),1,out);
}
for (int j = 0; j < padding; j++)
{
fputc(0x00, out);
}
}
return WRITE_OK;
}
image_t rotate_90(image_t source, char side) {
image_t* rotated = malloc(sizeof(image_t));
rotated->width = source.height;
rotated->height = source.width;
pixel_t *rotate = malloc(abs(rotated->width)*rotated->height*sizeof(pixel_t));
for (int i = 0; i < abs(source.height); ++i) {
for (int j = 0; j < source.width ; ++j) {
pixel_t temp = source.data[source.width*i + j];
if (side == 'r') {
rotate[abs(rotated->width) * (j + 1) + i - 1] = temp;
puts("right");
}
else {
rotate[abs(rotated->width) * (j + 1) - i - 1] = temp;
puts("left");
}
// rotate[abs(rotated->width) * (j + 1) - i - 1] = temp;
}
}
rotated->data = rotate;
return *rotated;
}