The SwappyItems Key-Values Store
Public Member Functions | Public Attributes | Private Member Functions | List of all members
Bmp Struct Reference

#include <Bmp.hpp>

Public Member Functions

Colorget (int x, int y)
 
void copy (const Bmp &src)
 
BMP_byteB (int x, int y)
 
BMP_byteG (int x, int y)
 
BMP_byteR (int x, int y)
 
void init (int pwidth, int pheight)
 
void read (const char *filename)
 
void write (const char *filename)
 

Public Attributes

Colordata
 
int width
 
int height
 

Private Member Functions

int pos (int x, int y)
 

Detailed Description

Todo:
destructor !!!

Member Function Documentation

◆ B()

BMP_byte& Bmp::B ( int  x,
int  y 
)
inline
48  {
49  return *((BMP_byte *) &(data[pos(x, y)]));
50  }
unsigned char BMP_byte
Definition: Bmp.hpp:9
Color * data
Definition: Bmp.hpp:30
int pos(int x, int y)
Definition: Bmp.hpp:21

◆ copy()

void Bmp::copy ( const Bmp src)
inline
38  {
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  }
int height
Definition: Bmp.hpp:32
int width
Definition: Bmp.hpp:31
Definition: Bmp.hpp:11

◆ G()

BMP_byte& Bmp::G ( int  x,
int  y 
)
inline
51  {
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  }

◆ get()

Color& Bmp::get ( int  x,
int  y 
)
inline
34  {
35  return data[pos(x, y)];
36  }

◆ init()

void Bmp::init ( int  pwidth,
int  pheight 
)
inline
60  {
61  width = pwidth;
62  height = pheight;
63  data = new Color[width*height];
64  }

◆ pos()

int Bmp::pos ( int  x,
int  y 
)
inlineprivate
21  {
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  }

◆ R()

BMP_byte& Bmp::R ( int  x,
int  y 
)
inline
56  {
57  return *( ((BMP_byte *) &(data[pos(x, y)]))+2 );
58  }

◆ read()

void Bmp::read ( const char *  filename)
inline

sometimes the fileheader is bigger?!?!

66  {
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  }
BMP_byte & B(int x, int y)
Definition: Bmp.hpp:48
BMP_byte & R(int x, int y)
Definition: Bmp.hpp:56
BMP_byte & G(int x, int y)
Definition: Bmp.hpp:51

◆ write()

void Bmp::write ( const char *  filename)
inline
97  {
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  }

Member Data Documentation

◆ data

Color* Bmp::data

◆ height

int Bmp::height

◆ width

int Bmp::width

The documentation for this struct was generated from the following file: