The SwappyItems Key-Values Store
Bmp.hpp
Go to the documentation of this file.
1 #ifndef __BMP_H__
2 #define __BMP_H__
3 
4 #include <cstdio>
5 #include <cstdlib>
6 
7 using namespace std;
8 
9 typedef unsigned char BMP_byte;
10 
11 struct Color {
15 };
16 
18 
19 struct Bmp {
20 private:
21  int pos(int x, int y) {
22  if (x<0) x=0;
23  if (x>=width) x=width-1;
24  if (y<0) y=0;
25  if (y>=height) y=height-1;
26  return ((height-y-1)*width +x);
27  }
28 
29 public:
31  int width;
32  int height;
33 
34  Color & get(int x, int y) {
35  return data[pos(x, y)];
36  }
37 
38  void copy(const Bmp & src) {
39  width = src.width;
40  height = src.height;
41  int size = 3 * (src.width * src.height);
42  data = (Color *) (new BMP_byte[size]);
43  for (int i=0; i<size; ++i) {
44  *(((BMP_byte *) data)+i) = *(((BMP_byte *) src.data)+i);
45  }
46  }
47 
48  BMP_byte & B(int x, int y) {
49  return *((BMP_byte *) &(data[pos(x, y)]));
50  }
51  BMP_byte & G(int x, int y) {
52  // get address from 3byte Color, cast to "bytes" address, add +1
53  // to Address (= 1byte step size) and get value of it
54  return *( ((BMP_byte *) &(data[pos(x, y)]))+1 );
55  }
56  BMP_byte & R(int x, int y) {
57  return *( ((BMP_byte *) &(data[pos(x, y)]))+2 );
58  }
59 
60  void init(int pwidth, int pheight) {
61  width = pwidth;
62  height = pheight;
63  data = new Color[width*height];
64  }
65 
66  void read(const char * filename) {
67  FILE* f = fopen(filename, "rb");
68  BMP_byte info[54];
69  BMP_byte * row;
70  int row_padded;
71 
72  fread(info, sizeof(BMP_byte), 54, f);
73 
74  width = *( (int*) &info[18]);
75  height = *( (int*) &info[22]);
76  int psize = *( (int*) &info[34]);
77 
78  data = new Color[width*height];
79 
80  row_padded = (psize/height);
81  row = new BMP_byte[row_padded];
82 
84  //fread(row, sizeof(byte), 68, f);
85 
86  for(int y = height-1; y >= 0; --y) {
87  fread(row, sizeof(BMP_byte), row_padded, f);
88  for(int x = 0; x < width; ++x) {
89  B(x, y) = row[(3*x)];
90  G(x, y) = row[(3*x)+1];
91  R(x, y) = row[(3*x)+2];
92  }
93  }
94  fclose(f);
95  }
96 
97  void write(const char * filename) {
98  FILE* f = fopen(filename, "wb");
99  BMP_byte info[54] = {
100  'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0,
101  40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0,
102  0,0,0,0, 0,0,0,0
103  };
104  int xbytes = 4 - ((width * 3) % 4);
105  if (xbytes == 4) xbytes = 0;
106  int psize = ((width * 3) + xbytes) * height;
107 
108  *( (int*) &info[2]) = 54 + psize;
109  *( (int*) &info[18]) = width;
110  *( (int*) &info[22]) = height;
111  *( (int*) &info[34]) = psize;
112 
113  fwrite(info, sizeof(BMP_byte), sizeof(info), f);
114 
115  int x,y,n;
116  for (y=height-1; y>=0; --y) {
117  for (x=0; x<width; ++x) {
118  fprintf(f, "%c", B(x, y));
119  fprintf(f, "%c", G(x, y));
120  fprintf(f, "%c", R(x, y));
121  }
122  // BMP lines must be of lengths divisible by 4
123  if (xbytes) {
124  for (n = 0; n < xbytes; ++n) fprintf(f, "%c", 0);
125  }
126  }
127 
128  fclose(f);
129  }
130 };
131 
132 #endif
unsigned char BMP_byte
Definition: Bmp.hpp:9
double pos
Definition: osm2graph_bitmap.cpp:125
Definition: Bmp.hpp:19
void read(const char *filename)
Definition: Bmp.hpp:66
void init(int pwidth, int pheight)
Definition: Bmp.hpp:60
Color & get(int x, int y)
Definition: Bmp.hpp:34
BMP_byte & B(int x, int y)
Definition: Bmp.hpp:48
void copy(const Bmp &src)
Definition: Bmp.hpp:38
Color * data
Definition: Bmp.hpp:30
void write(const char *filename)
Definition: Bmp.hpp:97
BMP_byte & R(int x, int y)
Definition: Bmp.hpp:56
int height
Definition: Bmp.hpp:32
BMP_byte & G(int x, int y)
Definition: Bmp.hpp:51
int width
Definition: Bmp.hpp:31
int pos(int x, int y)
Definition: Bmp.hpp:21
Definition: Bmp.hpp:11
BMP_byte blue
Definition: Bmp.hpp:12
BMP_byte red
Definition: Bmp.hpp:14
BMP_byte green
Definition: Bmp.hpp:13