-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsecretebmp.c
87 lines (82 loc) · 3.14 KB
/
libsecretebmp.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
#include <stdio.h>
#include "libsecretebmp.h"
void generateBitmapImageByRow(
unsigned char* (*cb_row)(void *cbdata),
void (*cb_writer)(
unsigned char *data, int size, int nmemb, void *cbdata),
void (*cb_done)(void *cbdata),
void *cbdata,
int height,
int width) {
int widthInBytes = width * BYTES_PER_PIXEL;
unsigned char padding[3] = {0, 0, 0};
int paddingSize = (4 - (widthInBytes) % 4) % 4;
int stride = (widthInBytes) + paddingSize;
unsigned char* fileHeader = createBitmapFileHeader(height, stride);
(*cb_writer)(fileHeader, 1, FILE_HEADER_SIZE, cbdata);
unsigned char* infoHeader = createBitmapInfoHeader(height, width);
(*cb_writer)(infoHeader, 1, INFO_HEADER_SIZE, cbdata);
int i;
for (i = 0; i < height; i++) {
unsigned char *rowd;
rowd = (*cb_row)(cbdata); // request row of data
#ifdef SECRETE_BMP_DEBUG
if (rowd == NULL) {
fprintf(stderr, "cb_row() returned NULL\n");
exit(1);
}
#endif
(*cb_writer)(rowd, BYTES_PER_PIXEL, width, cbdata);
(*cb_writer)(padding, 1, paddingSize, cbdata);
}
if (cb_done) (*cb_done)(cbdata);
}
unsigned char* createBitmapFileHeader (int height, int stride) {
int fileSize = FILE_HEADER_SIZE + INFO_HEADER_SIZE + (stride * height);
printf("Filesize: %d\n", fileSize);
static unsigned char fileHeader[] = {
0,0, /// signature
0,0,0,0, /// image file size in bytes
0,0,0,0, /// reserved
0,0,0,0, /// start of pixel array
};
fileHeader[ 0] = (unsigned char)('B');
fileHeader[ 1] = (unsigned char)('M');
fileHeader[ 2] = (unsigned char)(fileSize );
fileHeader[ 3] = (unsigned char)(fileSize >> 8);
fileHeader[ 4] = (unsigned char)(fileSize >> 16);
fileHeader[ 5] = (unsigned char)(fileSize >> 24);
fileHeader[10] = (unsigned char)(FILE_HEADER_SIZE + INFO_HEADER_SIZE);
//fileHeader[0] = 0xAA; // was debugging headers
return fileHeader;
}
unsigned char* createBitmapInfoHeader (int height, int width) {
static unsigned char infoHeader[] = {
0,0,0,0, /// header size
0,0,0,0, /// image width
0,0,0,0, /// image height
0,0, /// number of color planes
0,0, /// bits per pixel
0,0,0,0, /// compression
0,0,0,0, /// image size
0,0,0,0, /// horizontal resolution
0,0,0,0, /// vertical resolution
0,0,0,0, /// colors in color table
0,0,0,0, /// important color count
};
infoHeader[ 0] = (unsigned char)(INFO_HEADER_SIZE);
infoHeader[ 4] = (unsigned char)(width );
infoHeader[ 5] = (unsigned char)(width >> 8);
infoHeader[ 6] = (unsigned char)(width >> 16);
infoHeader[ 7] = (unsigned char)(width >> 24);
infoHeader[ 8] = (unsigned char)(height );
infoHeader[ 9] = (unsigned char)(height >> 8);
infoHeader[10] = (unsigned char)(height >> 16);
infoHeader[11] = (unsigned char)(height >> 24);
infoHeader[12] = (unsigned char)(1);
infoHeader[14] = (unsigned char)(BYTES_PER_PIXEL*8);
//infoHeader[0] = 0xBB; // was debugging headers
//infoHeader[30-14] = 0x88;
//infoHeader[31-14] = 0x99;
return infoHeader;
}