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
|
/*
* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include <stdio.h>
#include "bitmap.h"
Bitmap::Bitmap() : data(NULL)
{
}
Bitmap::~Bitmap()
{
delete[] data;
data = NULL;
}
int Bitmap::create(int w, int h, int bpp)
{
const int imagesize = w*h*(bpp / 8);
headerField0 = 'B';
headerField1 = 'M';
size = 54 + imagesize;
reserved1 = 0;
reserved2 = 0;
offset = 54;
headerSize = 40;
width = w;
height = h;
colorPlanes = 1;
bitsPerPixel = bpp;
compressionMethod = 0;
imageSize = imagesize;
hRes = 2000;
vRes = 2000;
numColors = 0;
numImportantColors = 0;
data = new unsigned char[imagesize];
return 0;
}
int Bitmap::write(FILE* stream)
{
if (stream == NULL || data == NULL) return -1;
fwrite(&headerField0, 1, 1, stream);
fwrite(&headerField1, 1, 1, stream);
fwrite(&size, 4, 1, stream);
fwrite(&reserved1, 2, 1, stream);
fwrite(&reserved2, 2, 1, stream);
fwrite(&offset, 4, 1, stream);
fwrite(&headerSize, 4, 1, stream);
fwrite(&width, 4, 1, stream);
fwrite(&height, 4, 1, stream);
fwrite(&colorPlanes, 2, 1, stream);
fwrite(&bitsPerPixel, 2, 1, stream);
fwrite(&compressionMethod, 4, 1, stream);
fwrite(&imageSize, 4, 1, stream);
fwrite(&hRes, 4, 1, stream);
fwrite(&vRes, 4, 1, stream);
fwrite(&numColors, 4, 1, stream);
fwrite(&numImportantColors, 4, 1, stream);
if (compressionMethod == 0)
{
fwrite(data, 1, imageSize, stream);
}
else
{
printf("Write format not supported\n");
return -1;
}
return 0;
}
int Bitmap::read(FILE* stream)
{
if (stream == NULL) return -1;
size_t readCount = 0;
readCount += fread(&headerField0, 1, 1, stream);
readCount += fread(&headerField1, 1, 1, stream);
readCount += fread(&size, 4, 1, stream);
readCount += fread(&reserved1, 2, 1, stream);
readCount += fread(&reserved2, 2, 1, stream);
readCount += fread(&offset, 4, 1, stream);
readCount += fread(&headerSize, 4, 1, stream);
readCount += fread(&width, 4, 1, stream);
readCount += fread(&height, 4, 1, stream);
readCount += fread(&colorPlanes, 2, 1, stream);
readCount += fread(&bitsPerPixel, 2, 1, stream);
readCount += fread(&compressionMethod, 4, 1, stream);
readCount += fread(&imageSize, 4, 1, stream);
readCount += fread(&hRes, 4, 1, stream);
readCount += fread(&vRes, 4, 1, stream);
readCount += fread(&numColors, 4, 1, stream);
readCount += fread(&numImportantColors, 4, 1, stream);
if (compressionMethod == 0)
{
delete[] data;
data = new unsigned char[imageSize];
readCount += fread(data, 1, imageSize, stream);
}
else
{
printf("Write format not supported\n");
return -1;
}
return 0;
}
|