Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OImageBmp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "OImageBmp.h"
21 #include <cassert>
22 #include <fstream>
23 #include <cstring>
24 
26 
28 
29 bool OImageBmp::load(const std::string& filename)
30 {
31  // Open file to read
32  std::ifstream ifs;
33  ifs.open(filename.c_str(), std::ios::in | std::ios::binary);
34  if (!ifs)
35  {
36  return false;
37  }
38 
39  // Read width and height
40  ifs.seekg(18, std::ios::beg);
41  ifs.read((char*)&_sizeX, sizeof(unsigned int));
42  ifs.read((char*)&_sizeY, sizeof(unsigned int));
43 
44  // OImage size in BGR and RGBA
45  unsigned int size3 = _sizeX * _sizeY * 3;
46  unsigned int size4 = _sizeX * _sizeY * 4;
47 
48  // Check planes
49  unsigned short planes = 0;
50  ifs.read((char*)&planes, sizeof(unsigned short));
51  if (planes != 1)
52  {
53  return false;
54  }
55 
56  // Check bits per pixel
57  ifs.read((char*)&_depth, sizeof(unsigned int));
58  if (_depth != 24)
59  {
60  return false;
61  }
62 
63  // Allocate memory
64  _data = new unsigned char[size4];
65  unsigned char* tempData = new unsigned char[size3];
66 
67  // Read BGR image
68  ifs.seekg(22, std::ios::cur);
69  ifs.read((char*)tempData, size3);
70 
71  // Convert BGR to RGBA
72  unsigned int c1 = 0, c2 = 0;
73  for (c1 = 0, c2 = 0; c1 < size4; c1 += 4, c2 += 3)
74  {
75  _data[c1] = tempData[c2 + 2];
76  _data[c1 + 1] = tempData[c2 + 1];
77  _data[c1 + 2] = tempData[c2];
78  _data[c1 + 3] = 255;
79  }
80 
81  // Free temporary memory
82  delete[] tempData;
83 
84  _depth = 32;
85 
86  ifs.close();
87 
88  return true;
89 }
90 
92 {
93  const unsigned int stride = _depth;
94  const unsigned int w = _sizeX;
95  const unsigned int h = _sizeY;
96  const unsigned int m = h / 2;
97  unsigned char* temp = new unsigned char[stride];
98 
99  for (unsigned int y = 0; y < m; ++y)
100  {
101  for (unsigned int x = 0; x < w; ++x)
102  {
103  unsigned int offset = (y * w + x) * stride;
104  unsigned int offsetOpp = ((h - y - 1) * w + x) * stride;
105 
106  std::memcpy(temp, _data + offset, stride);
107  std::memcpy(_data + offset, _data + offsetOpp, stride);
108  std::memcpy(_data + offsetOpp, temp, stride);
109  }
110  }
111  delete[] temp;
112 }
113 
115 {
116  const unsigned int stride = _depth;
117  const unsigned int w = _sizeX;
118  const unsigned int h = _sizeY;
119  const unsigned int m = w / 2;
120  unsigned char* temp = new unsigned char[stride];
121 
122  for (unsigned int y = 0; y < h; ++y)
123  {
124  for (unsigned int x = 0; x < m; ++x)
125  {
126  unsigned int offset = (y * w + x) * stride;
127  unsigned int offsetOpp = (y * w + (w - x - 1)) * stride;
128 
129  std::memcpy(temp, _data + offset, stride);
130  std::memcpy(_data + offset, _data + offsetOpp, stride);
131  std::memcpy(_data + offsetOpp, temp, stride);
132  }
133  }
134  delete[] temp;
135 }
virtual bool load(const std::string &filename)
Definition: OImageBmp.cpp:29
void flipVertical()
Definition: OImageBmp.cpp:114
virtual ~OImageBmp()
Definition: OImageBmp.cpp:27
void flipHorizontal()
Definition: OImageBmp.cpp:91
Definition: OImage.h:34
unsigned int _sizeX
Definition: OImage.h:92
unsigned int _depth
Definition: OImage.h:89
unsigned int _sizeY
Definition: OImage.h:93
unsigned char * _data
Definition: OImage.h:86