PNGArena.h
Go to the documentation of this file.
1 #ifndef WEBEVD_PNGARENA_H
2 #define WEBEVD_PNGARENA_H
3 
4 #include <array>
5 #include <memory>
6 #include <mutex>
7 #include <string>
8 #include <vector>
9 
10 typedef unsigned char png_byte;
11 
12 namespace evd
13 {
14  class JSONFormatter;
15 
16  static constexpr int MipMapOffset(int dim, int maxdim)
17  {
18  if(dim >= maxdim) return 0;
19  return MipMapOffset(dim*2, maxdim) + (dim*2)*(dim*2)*4;
20  }
21 
22  class PNGArena
23  {
24  public:
25  friend class PNGView;
26  friend void AnalyzeArena(const PNGArena&);
27 
28  enum{
29  // Square because seems to be necessary for mipmapping. Larger than this
30  // doesn't seem to work in the browser.
31  kArenaSize = 4096,
32  // We have APAs with 480 and 1148 wires. This fits them in 1 and 3 blocks
33  // without being too wasteful.
34  kBlockSize = 512,
35  kTotBytes = MipMapOffset(1, kArenaSize)+4 // leave space for 1x1 as well
36  };
37 
38  PNGArena(const std::string& name);
39 
40  inline png_byte& operator()(int i, int x, int y, int c)
41  {
42  return (*data[i])[(y*kArenaSize+x)*4+c];
43  }
44 
45  inline const png_byte& operator()(int i, int x, int y, int c) const
46  {
47  return (*data[i])[(y*kArenaSize+x)*4+c];
48  }
49 
50  void WritePNGBytes(FILE* fout, int imgIdx, int dim);
51 
52  png_byte* NewBlock();
53 
54  // protected:
56  int elemx, elemy;
57  int nviews;
58 
59  std::vector<std::unique_ptr<std::array<png_byte, kTotBytes>>> data;
60 
61  protected:
62  void FillMipMaps(int d);
63 
64  std::vector<std::mutex*> fMIPLocks;
65  std::vector<bool> fHasMIP;
66  };
67 
68 
69  class PNGView
70  {
71  public:
72  explicit PNGView(PNGArena& a);
73 
74  inline png_byte& operator()(int x, int y, int c)
75  {
76  const unsigned int ix = x/PNGArena::kBlockSize;
77  const unsigned int iy = y/PNGArena::kBlockSize;
78  if(ix >= blocks.size()) blocks.resize(ix+1);
79  if(iy >= blocks[ix].size()) blocks[ix].resize(iy+1, 0);
80  if(!blocks[ix][iy]) blocks[ix][iy] = arena.NewBlock();
81  return blocks[ix][iy][((y-iy*PNGArena::kBlockSize)*PNGArena::kArenaSize+(x-ix*PNGArena::kBlockSize))*4+c];
82  }
83 
84  inline png_byte operator()(int x, int y, int c) const
85  {
86  const unsigned int ix = x/PNGArena::kBlockSize;
87  const unsigned int iy = y/PNGArena::kBlockSize;
88  if(ix >= blocks.size()) return 0;
89  if(iy >= blocks[ix].size()) return 0;
90  if(!blocks[ix][iy]) return 0;
91  return blocks[ix][iy][((y-iy*PNGArena::kBlockSize)*PNGArena::kArenaSize+(x-ix*PNGArena::kBlockSize))*4+c];
92  }
93 
94  protected:
96 
98 
99  std::vector<std::vector<png_byte*>> blocks;
100  };
101 
102  void AnalyzeArena(const PNGArena& bytes);
103 
104 } // namespace
105 
106 #endif
JSONFormatter & operator<<(JSONFormatter &json, const art::InputTag &t)
unsigned char png_byte
Definition: PNGArena.h:10
png_byte & operator()(int x, int y, int c)
Definition: PNGArena.h:74
std::string name
Definition: PNGArena.h:55
std::vector< std::unique_ptr< std::array< png_byte, kTotBytes > > > data
Definition: PNGArena.h:59
std::string string
Definition: nybbler.cc:12
static constexpr int MipMapOffset(int dim, int maxdim)
Definition: PNGArena.h:16
friend void AnalyzeArena(const PNGArena &)
Definition: PNGArena.cxx:169
friend class PNGView
Definition: PNGArena.h:25
png_byte & operator()(int i, int x, int y, int c)
Definition: PNGArena.h:40
void FillMipMaps(int d)
Definition: PNGArena.cxx:43
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
LArSoft includes.
Definition: InfoTransfer.h:33
const double a
PNGArena & arena
Definition: PNGArena.h:97
const png_byte & operator()(int i, int x, int y, int c) const
Definition: PNGArena.h:45
std::vector< std::vector< png_byte * > > blocks
Definition: PNGArena.h:99
png_byte * NewBlock()
Definition: PNGArena.cxx:19
std::vector< bool > fHasMIP
Definition: PNGArena.h:65
PNGArena(const std::string &name)
Definition: PNGArena.cxx:14
png_byte operator()(int x, int y, int c) const
Definition: PNGArena.h:84
list x
Definition: train.py:276
void WritePNGBytes(FILE *fout, int imgIdx, int dim)
Definition: PNGArena.cxx:108
byte bytes
Alias for common language habits.
Definition: datasize.h:101
std::vector< std::mutex * > fMIPLocks
Definition: PNGArena.h:64