-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp16.c
164 lines (133 loc) · 4.34 KB
/
bmp16.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
#include <stdio.h>
#include <stdlib.h>
#include "bmp16.h"
#include "constants.h"
#include "file.h"
bmp16* bmp16_create_new(int width, int height)
{
bmp16* bmp = malloc(sizeof(bmp16));
bmp->header.id = BMP_ID;
bmp->header.size = 14;
bmp->header.non1 = 0;
bmp->header.non2 = 0;
bmp->header.pixel_array_offset = 54;
bmp->dib_size = 40;
bmp->dib.width = width;
bmp->dib.height = height;
bmp->dib.col_planes = 1;
bmp->dib.bits = 24;
bmp->dib.bi_rgb = 0;
bmp->dib.raw_size = width * height * BYTES_PER_PIXEL_24;
bmp->dib.dpi_horizontal = (int)(72 * 39.3701);
bmp->dib.dpi_vertical = (int)(72 * 39.3701);
bmp->dib.col_palette = 0;
bmp->dib.col_important = 0;
bmp->pixel_count = width * height;
bmp->pad = 0;
int row_bytes = width * BYTES_PER_PIXEL_24;
if ((row_bytes) % 4 != 0)
bmp->pad = 4 - (row_bytes % 4);
bmp->pixels = malloc(sizeof(bmp16_pixel) * bmp->pixel_count);
return bmp;
}
bmp16* bmp16_single_read(const char* file)
{
bmp16* bmp = malloc(sizeof(bmp16));
FILE* fd = fopen(file, "rb+");
long fsize = file_size(fd);
unsigned char* data = malloc(sizeof(unsigned char) * fsize);
int fres = fread(data, sizeof(unsigned char), fsize, fd);
fclose(fd);
if (fres != fsize)
return 0;
bmp->header.id = file_read_short(&data);
bmp->header.size = file_read_int(&data);
bmp->header.non1 = file_read_short(&data);
bmp->header.non2 = file_read_short(&data);
bmp->header.pixel_array_offset = file_read_int(&data);
bmp->dib_size = file_read_int(&data);
bmp->dib.width = file_read_int(&data);
bmp->dib.height = file_read_int(&data);
bmp->dib.col_planes = file_read_short(&data);
bmp->dib.bits = file_read_short(&data);
bmp->dib.bi_rgb = file_read_int(&data);
bmp->dib.raw_size = file_read_int(&data);
bmp->dib.dpi_horizontal = file_read_int(&data);
bmp->dib.dpi_vertical = file_read_int(&data);
bmp->dib.col_palette = file_read_int(&data);
bmp->dib.col_important = file_read_int(&data);
int i, j, ind = 0;
int width = bmp->dib.width;
int height = bmp->dib.height;
bmp->pixel_count = width * height;
bmp->pad = 0;
int row_bytes = width * BYTES_PER_PIXEL_24;
if ((row_bytes) % 4 != 0)
bmp->pad = 4 - (row_bytes % 4);
bmp->pixels = malloc(sizeof(bmp16_pixel) * bmp->pixel_count);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
bmp->pixels[ind++] = bmp16_get_pixel(&data);
}
file_skip_pad(&data, bmp->pad);
}
return bmp;
}
bmp16_pixel bmp16_get_pixel(unsigned char** data)
{
bmp16_pixel pixel;
char components[3];
components[0] = *((*data)++);
components[1] = *((*data)++);
components[2] = *((*data)++);
int col = *((int*)components);
pixel.b = (col & 0x00FF0000) >> 16;
pixel.g = (col & 0x0000FF00) >> 8;
pixel.r = (col & 0x000000FF);
return pixel;
}
void bmp16_save(bmp16* bmp, char* path)
{
FILE* fd = fopen(path, "wb+");
int file_size = sizeof(char) * 54 + sizeof(char)*((bmp->dib.width * bmp->dib.height * 3) + bmp->dib.width * bmp->pad);
unsigned char* file = malloc(file_size);
unsigned char* file_cpy = file;
unsigned char pad[2] = {0,0};
bmp16_pixel* pix_cpy = bmp->pixels;
file_write_data(&file_cpy, (unsigned char*)&bmp->header.id, sizeof(short));
file_write_data(&file_cpy, (unsigned char*)&bmp->header.size, sizeof(int));
file_write_data(&file_cpy, (unsigned char*)&bmp->header.non1, sizeof(short));
file_write_data(&file_cpy, (unsigned char*)&bmp->header.non2, sizeof(short));
file_write_data(&file_cpy, (unsigned char*)&bmp->header.pixel_array_offset, sizeof(int));
file_write_data(&file_cpy, (unsigned char*)&bmp->dib_size, sizeof(int));
file_write_data(&file_cpy, (unsigned char*)&bmp->dib, sizeof(bmp16_dib));
int width = bmp->dib.width;
int height = bmp->dib.height;
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
bmp16_pixel pixel = *pix_cpy++;
int i_pix = 0;
i_pix |= pixel.r << 16;
i_pix |= pixel.g << 8;
i_pix |= pixel.b;
file_write_data(&file_cpy, (unsigned char*)&i_pix, 3);
}
if (bmp->pad > 0)
file_write_data(&file_cpy, pad, sizeof(unsigned char) * bmp->pad);
}
fwrite(file, sizeof(unsigned char), file_size, fd);
perror("Err");
fclose(fd);
free(file);
}
bmp16_pixel bmp16_get_pixel_at(bmp16* bmp, int i, int j)
{
int index = (i * bmp->dib.width) + j;
return bmp->pixels[index];
}
void bmp16_set_pixel_at(bmp16* bmp, bmp16_pixel pixel, int i, int j)
{
int index = (i * bmp->dib.width) + j;
bmp->pixels[index] = pixel;
}