-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscbitmap.cpp
75 lines (57 loc) · 1.97 KB
/
cscbitmap.cpp
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
// Code by Bidur Bohara (LSU) in collaboration with Brygg Ullmer
#include "cscbitmap.h"
#include <stdio.h>
Bitmap::Bitmap()
{
bmpWidth = 0;
bmpHeight = 0;
bmpSize = 0;
offset = 0;
bitsPerPixel = 0;
}
///
unsigned char* Bitmap::readGrayBitmap(const char *file)
{
// Open bitmap file to read
FILE *fp = fopen(file, "rb");
if(!fp)
{
printf("Error! Cannot open input file.\n");
return 0;
}
unsigned int status = 0;
status = fseek(fp, 10, SEEK_SET); // Seek to width
status = fread((void*)&offset, sizeof(unsigned int), 1, fp);
status = fseek(fp, 18, SEEK_SET); // Seek to width
status = fread((void*)&bmpWidth, sizeof(int), 1, fp);
status = fseek(fp, 22, SEEK_SET); // Seek to height
status = fread((void*)&bmpHeight, sizeof(int), 1, fp);
status = fseek(fp, 28, SEEK_SET); // Seek to bits per pixel
status = fread((void*)&bitsPerPixel, sizeof(unsigned short), 1, fp);
status = fseek(fp, 34, SEEK_SET); // Seek to bitmap image size
status = fread((void*)&bmpSize, sizeof(unsigned int), 1, fp);
/// Read the Bitmap Header info.
bmpHeader = new unsigned char[offset];
status = fseek(fp, 0, SEEK_SET);
status = fread((void*)bmpHeader, sizeof(unsigned char), offset, fp);
/// Read the Bitmap image data.
unsigned char* bmpData = new unsigned char[bmpSize];
/// Seek to the position of image data.
status = fseek(fp, offset, SEEK_SET);
status = fread(bmpData, sizeof(unsigned char), bmpSize, fp);
//bmpSize = status > 0 ? status : bmpSize;
fclose(fp);
return bmpData;
}
///
void Bitmap::writeGrayBmp(unsigned char* data, const char* filename)
{
FILE *wp = fopen(filename, "wb");
if(!data)
printf("No data to be written!!!");
unsigned int status = 0;
status = fwrite((const void*)bmpHeader, sizeof(unsigned char),
offset, wp);
status = fwrite((const void*)data, sizeof(unsigned char), bmpSize, wp);
fclose(wp);
}