-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMP.cpp
More file actions
74 lines (64 loc) · 2.02 KB
/
BMP.cpp
File metadata and controls
74 lines (64 loc) · 2.02 KB
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
#include "BMP.h"
namespace Compression{
BMP* BMP::readImage(string fileName){
//BMP *ret = new BMP();
long imSize, width, height;
unsigned short bitCount;
unsigned long compr, clrUsed, clrImp;
long xRes, yRes;
/*Buffers for reading file.*/
ifstream in;
in.open(fileName.c_str(), ifstream::binary);//Opening of the file.
BMPInfo *inf = new BMPInfo();
in>>(*inf);
if(imSize == 0) imSize = inf->width * inf->height * inf->bitCount;
RGBPixelSet *rgb;
if(inf->bitCount == 24) rgb = read24BitImage(&in, inf->height, inf->width);
BMP *bmp = new BMP();
in.close();
bmp->inf = inf;
bmp->pixels = rgb;
return bmp;
}
void BMP::writeBMP(string fileName){
ofstream out;
out.open(fileName.c_str(), std::ios::binary);
out<<hex;
out<<*(this->inf);
if(this->inf->bitCount == 24){
write24BitImage(out);
}
out.close();
}
/**
Reading pixels of image. Must be called after reading the header.
@input:
in — input stream.
line — number of lines in the image.
columns - number of columns in the image.
@output:
RGBPixelSet structure appropriate with image.
*/
RGBPixelSet* BMP::read24BitImage(istream* in, int lines, int columns){
RGBPixelSet *rgb = new RGBPixelSet(lines, columns);
for(int i = 0; i < lines; i++){
for(int j = 0; j < columns; j++){
RGBPixel px;
(*in)>>px;
rgb->addPixel(px);
}
}
return rgb;
}
void BMP::write24BitImage(ostream& out){
for(int i = 0; i < (this->pixels)->getHeight(); i++){
for(int j = 0; j < (this->pixels)->getWidth(); j++){
RGBPixel *px = pixels->getPixel(i,j);
out<<(*px);
}
}
}
RGBPixelSet BMP::getRGBPixelSet(){
return (*pixels);
}
}