Line data Source code
1 : #ifndef UTIL_BMP_H
2 : #define UTIL_BMP_H
3 :
4 : #include <string>
5 : #include <iostream>
6 : #include <vector>
7 : #include <array>
8 : #include "Util/Util.H"
9 :
10 : namespace Util
11 : {
12 : class BMP
13 : {
14 : public:
15 2 : BMP () {};
16 :
17 2 : void Define (std::string filename)
18 : {
19 : FILE* f;
20 2 : int retcode = -1;
21 2 : if ((f = fopen(filename.c_str(), "rb")))
22 : {
23 : unsigned char info[54];
24 2 : retcode = fread(info, sizeof(unsigned char), 54, f);
25 2 : if (retcode != 54) Util::Abort(INFO,"Error reading BMP file: 54 bytes expected but only ", retcode, " read.");
26 2 : nx = *(int*)&info[18];
27 2 : ny = *(int*)&info[22];
28 :
29 2 : data.resize(nx*ny);
30 :
31 2 : int row_padded = (nx*3 + 3) & (~3);
32 2 : unsigned char* data = new unsigned char[row_padded];
33 2002 : for (int j = 0; j < ny; j++)
34 : {
35 2000 : retcode = fread(data, sizeof(unsigned char), row_padded, f);
36 2000 : if (retcode != row_padded) Util::Abort(INFO,"Error reading BMP file: ",row_padded," bytes expected but only ", retcode, " read.");
37 2002000 : for(int i = 0; i < nx*3; i += 3)
38 : {
39 2000000 : (*this)(i/3,j)[0] = (int)data[i+2]; // R
40 2000000 : (*this)(i/3,j)[1] = (int)data[i+1]; // G
41 2000000 : (*this)(i/3,j)[2] = (int)data[i]; // B
42 : }
43 : }
44 2 : fclose(f);
45 : }
46 : else
47 : {
48 0 : Util::Abort(INFO,"File ", filename, " does not exist");
49 : }
50 2 : }
51 :
52 : AMREX_FORCE_INLINE
53 : std::array<int,3> & operator () (int i,int j)
54 : {
55 13933300 : Util::Assert(INFO,TEST(i < nx)," i = ",i," nx = ", nx);
56 13933300 : Util::Assert(INFO,TEST(j < ny)," j = ",j," ny = ", ny);
57 6966660 : return data[nx*j + i];
58 : }
59 2 : std::array<int,3> min()
60 : {
61 2 : std::array<int,3> _min = {std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};
62 2000000 : for (unsigned int i = 0; i < data.size(); i++)
63 : {
64 2000000 : if (_min[0] > data[i][0]) _min[0] = data[i][0];
65 2000000 : if (_min[1] > data[i][1]) _min[1] = data[i][1];
66 2000000 : if (_min[2] > data[i][2]) _min[2] = data[i][2];
67 : }
68 2 : return _min;
69 : }
70 2 : std::array<int,3> max()
71 : {
72 2 : std::array<int,3> _max = {0, 0, 0};
73 2000000 : for (unsigned int i = 0; i < data.size(); i++)
74 : {
75 2000000 : if (_max[0] < data[i][0]) _max[0] = data[i][0];
76 2000000 : if (_max[1] < data[i][1]) _max[1] = data[i][1];
77 2000000 : if (_max[2] < data[i][2]) _max[2] = data[i][2];
78 : }
79 2 : return _max;
80 : }
81 :
82 : public:
83 : int nx, ny;
84 : private:
85 : std::vector<std::array<int,3>> data;
86 : };
87 : }
88 :
89 :
90 : #endif
|