EMAN2
eerio.h
Go to the documentation of this file.
1/*
2 * Author: tunay.durmaz@bcm.edu, 08/20/2020
3 * Copyright (c) 2020- Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__eerio_h__
33#define eman__eerio_h__ 1
34
35#include "imageio.h"
36
37#include <tiffio.h>
38#include <bitset>
39
40
41namespace EMAN
42{
43 template <class T>
44 class BitStream {
45 public:
46 BitStream(T *buf)
47 : buffer(buf), cur(*buffer)
48 {}
49
50 T get_bits(int N) {
51 auto result = cur & ((1 << N) - 1);
52
53 if(N < bit_counter) {
54 cur >>= N;
55 bit_counter -= N;
56 }
57 else {
58 auto remaining_bits = N - bit_counter;
59
60 cur = *(++buffer);
61 result |= ((cur & ((1 << remaining_bits) - 1)) << bit_counter);
62
63 cur >>= remaining_bits;
64 bit_counter = max_num_bits - remaining_bits;
65 }
66
67 return result;
68 }
69
70 private:
72 T cur;
73 const size_t max_num_bits = 8*sizeof(T);
75
76 friend std::ostream &operator<<(std::ostream &out, const BitStream &obj) {
77 return out<<"cur: "<<std::bitset<8*sizeof(T)>(obj.cur)
78 <<endl
79 <<"bit_counter: "<<obj.bit_counter<<endl
80 <<"max_num_bits: "<<obj.max_num_bits<<endl;
81 }
82 };
83
84 template<unsigned int T, bool BIT_OVERFLOW, class U>
85 class BitReader {
86 private:
87 const decltype(T) num_bits = T;
88 const decltype(T) max_val = (1 << num_bits) - 1;
89 uintmax_t val = 0;
90
91 public:
92 operator decltype(val) const () {
93 return val;
94 }
95
97 decltype(val) count;
98 obj.val = 0;
99 do {
100 count = in.get_bits(obj.num_bits);
101 obj.val += count;
102 } while(BIT_OVERFLOW && count == obj.max_val);
103
104 return in;
105 }
106 };
107
108 template<unsigned int T, class U>
110
111 template<unsigned int T, class U>
113
114 using EerWord = uint64_t;
118
119
120 class Decoder {
121 public:
122 virtual unsigned int num_pix() const =0;
123 auto operator()(unsigned int count, unsigned int sub_pix) const;
124
125 const unsigned int camera_size_bits = 12;
126 const unsigned int camera_size = 1 << camera_size_bits; // 2^12 = 4096
127
128 private:
129 virtual unsigned int x(unsigned int count, unsigned int sub_pix) const =0;
130 virtual unsigned int y(unsigned int count, unsigned int sub_pix) const =0;
131 };
132
133
134 template <unsigned int I>
135 struct DecoderIx : public Decoder {
136 unsigned int num_pix() const override;
137 unsigned int x(unsigned int count, unsigned int sub_pix) const override;
138 unsigned int y(unsigned int count, unsigned int sub_pix) const override;
139 };
140
141 template <unsigned int I>
142 unsigned int DecoderIx<I>::num_pix() const {
143// 4096 * 1 // 4k
144// 4096 * 2 // 8k
145// 4096 * 4 // 16k
146 return camera_size * (1 << I);
147 }
148
149 template <unsigned int I>
150 unsigned int DecoderIx<I>::x(unsigned int count, unsigned int sub_pix) const {
151// (((count & 4095) << 2) | ((sub_pix & 3) ^ 2)) // 16k
152// (((count & 4095) << 1) | ((sub_pix & 3) ^ 2) >> 1) // 8k
153 return (DecoderIx<0>().x(count, sub_pix) << I) | (((sub_pix & 3) ^ 2) >> (2 - I));
154 }
155
156 template <unsigned int I>
157 unsigned int DecoderIx<I>::y(unsigned int count, unsigned int sub_pix) const {
158// (((count >> 12) << 2) | ((sub_pix >> 2) ^ 2)) // 16k
159// (((count >> 12) << 1) | ((sub_pix >> 2) ^ 2) >> 1) // 8k
160 return (DecoderIx<0>().y(count, sub_pix) << I) | (((sub_pix >> 2) ^ 2) >> (2 - I));
161 }
162
163 template <>
164 inline unsigned int DecoderIx<0>::x(unsigned int count, unsigned int sub_pix) const {
165// count & ((1 << 12) - 1)
166 return count & (camera_size - 1);
167 }
168
169 template <>
170 inline unsigned int DecoderIx<0>::y(unsigned int count, unsigned int sub_pix) const {
171// count >> 12
172 return count >> camera_size_bits;
173 }
174
178
179
180 class EerIO : public ImageIO
181 {
182 public:
183 EerIO(const string & fname, IOMode rw_mode = READ_ONLY, Decoder &dec=decoder0x);
184 ~EerIO();
185
186 int get_nimg();
187 bool is_single_image_format() const override;
188
189
191
192 private:
195 size_t num_frames = 0;
197
199 };
200}
201
202#endif //eman__eerio_h__
const decltype(T) num_bits
Definition: eerio.h:87
const decltype(T) max_val
Definition: eerio.h:88
uintmax_t val
Definition: eerio.h:89
friend BitStream< U > & operator>>(BitStream< U > &in, BitReader< T, BIT_OVERFLOW, U > &obj)
Definition: eerio.h:96
T * buffer
Definition: eerio.h:71
size_t bit_counter
Definition: eerio.h:74
T get_bits(int N)
Definition: eerio.h:50
friend std::ostream & operator<<(std::ostream &out, const BitStream &obj)
Definition: eerio.h:76
BitStream(T *buf)
Definition: eerio.h:46
const size_t max_num_bits
Definition: eerio.h:73
auto operator()(unsigned int count, unsigned int sub_pix) const
Definition: eerio.cpp:42
virtual unsigned int x(unsigned int count, unsigned int sub_pix) const =0
virtual unsigned int num_pix() const =0
const unsigned int camera_size
Definition: eerio.h:126
virtual unsigned int y(unsigned int count, unsigned int sub_pix) const =0
const unsigned int camera_size_bits
Definition: eerio.h:125
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
bool is_big_endian
Definition: eerio.h:193
bool is_single_image_format() const override
Is this image format only storing 1 image or not.
Definition: eerio.cpp:257
Decoder & decoder
Definition: eerio.h:198
DEFINE_IMAGEIO_FUNC
Definition: eerio.h:190
size_t num_frames
Definition: eerio.h:195
TIFF * tiff_file
Definition: eerio.h:194
int get_nimg()
Return the number of images in this image file.
Definition: eerio.cpp:177
EerIO(const string &fname, IOMode rw_mode=READ_ONLY, Decoder &dec=decoder0x)
Definition: eerio.cpp:150
Dict acquisition_data_dict
Definition: eerio.h:196
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
IOMode rw_mode
Definition: imageio.h:353
E2Exception class.
Definition: aligner.h:40
static DecoderIx< 2 > decoder2x
Definition: eerio.h:177
static DecoderIx< 1 > decoder1x
Definition: eerio.h:176
uint64_t EerWord
Definition: eerio.h:114
static DecoderIx< 0 > decoder0x
Definition: eerio.h:175
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517
unsigned int x(unsigned int count, unsigned int sub_pix) const override
Definition: eerio.h:150
unsigned int num_pix() const override
Definition: eerio.h:142
unsigned int y(unsigned int count, unsigned int sub_pix) const override
Definition: eerio.h:157