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