-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteBMP.c
79 lines (65 loc) · 1.92 KB
/
writeBMP.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
#include "writeBMP.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void writeBMP(Image *image, const char* originalImgFileName, const char* fileName) {
// open the file to be written
FILE * bmpfile;
bmpfile = fopen(fileName, "wb");
if (bmpfile == NULL) {
printf("Error opening output file\n");
// close all open files and free any allocated memory
exit (1);
}
// open BMP file of original image
FILE * srcFile;
if ((srcFile = fopen(originalImgFileName, "rb")) == NULL) {
printf("File Not Found : %s\n", originalImgFileName);
exit (1);
}
// read header of original image
char originalHeader[54];
fread(&originalHeader, 1, 54, srcFile);
// write the BMP file header
fwrite(&originalHeader, 1, 54, bmpfile);
// close BMP file of original image
fclose(srcFile);
// calculate number of bytes per each line
int bytesPerLine;
bytesPerLine = image->sizeX * 3; // for 24 bit images)
// round up to a dword boundary
if (bytesPerLine & 0x0003) {
bytesPerLine |= 0x0003;
++bytesPerLine;
}
// allocate buffer to hold one line of the image
char *linebuf;
linebuf = (char *) calloc(1, bytesPerLine);
if (linebuf == NULL) {
printf ("Error allocating memory\n");
// close all open files and free any allocated memory
exit (1);
}
// write the image line by line - start with the lowest line
int line;
int i;
char* iData = image->data;
for (line = 0; line <= image->sizeY; ++line) {
/*
* fill line linebuf with the image data for that line
* remember that the order is BGR
*/
for (i = 0 ; i < bytesPerLine ; i += 3) {
linebuf[i] = iData[line*bytesPerLine + i + 2];
linebuf[i + 1] = iData[line*bytesPerLine + i + 1];
linebuf[i + 2] = iData[line*bytesPerLine + i];
}
/*
* remember that the order is BGR and if width is not a multiple
* of 4 then the last few bytes may be unused
*/
fwrite(linebuf, 1, bytesPerLine, bmpfile);
}
// close the image file
fclose(bmpfile);
}