EMAN2
imageio.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 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__imageio_h__
33#define eman__imageio_h__ 1
34
35#include "byteorder.h"
36#include "emutil.h"
37
38using std::vector;
39using std::string;
40
41namespace EMAN
42{
43 class Region;
44 class FloatSize;
45 class IntSize;
46 class Ctf;
47
127 {
128 public:
130 { READ_ONLY = 1, READ_WRITE = 2, WRITE_ONLY = 3 };
131 public:
132 ImageIO(const string & fname, IOMode rw);
133
134 virtual ~ImageIO();
135
146 virtual int read_header(Dict & dict, int image_index = 0,
147 const Region * area = 0, bool is_3d = false) = 0;
148
160 virtual int write_header(const Dict & dict,
161 int image_index = 0,
162 const Region * area = 0,
163 EMUtil::EMDataType filestoragetype = EMUtil::EM_FLOAT,
164 bool use_host_endian = true) = 0;
165
177 virtual int read_data(float *data, int image_index = 0,
178 const Region * area = 0, bool is_3d = false) = 0;
179
193 virtual int read_data_8bit(unsigned char *data, int image_index = 0,
194 const Region * area = 0, bool is_3d = false, float minval = 0.0f, float maxval = 0.0f) {
195 throw ImageFormatException("8 bit reading not supported for this format");
196 }
197
209 virtual int write_data(float *data,
210 int image_index = 0,
211 const Region * area = 0,
212 EMUtil::EMDataType filestoragetype = EMUtil::EM_FLOAT,
213 bool use_host_endian = true) = 0;
214
221 virtual int read_ctf(Ctf & ctf, int image_index = 0);
222
229 virtual void write_ctf(const Ctf & ctf, int image_index = 0);
230
233 virtual void flush() = 0;
234
236 virtual int get_nimg();
237
239 virtual bool is_complex_mode() = 0;
240
242 virtual bool is_image_big_endian() = 0;
243
250 virtual bool is_single_image_format() const
251 {
252 return true;
253 }
254
261 template < class T > void become_host_endian(T * data, size_t n = 1)
262 {
264 ByteOrder::swap_bytes(data, n);
265 }
266 }
267
268 string get_filename() const {
269 return filename;
270 }
271
272 protected:
275 virtual void init() = 0;
276
282 void check_read_access(int image_index);
283
291 void check_read_access(int image_index, const float *data);
292
302 void check_write_access(IOMode rw_mode, int image_index, int max_nimg = 0);
303
314 void check_write_access(IOMode rw_mode, int image_index, int max_nimg,
315 const float *data);
316
325 void check_region(const Region * area, const FloatSize & max_size,
326 bool is_new_file = false, bool inbounds_only=true);
327
337 void check_region(const Region * area, const IntSize & max_size,
338 bool is_new_file = false, bool inbounds_only=true);
339
349 FILE *sfopen(const string & filename, IOMode mode,
350 bool * is_new = 0, bool overwrite = false);
351
352 string filename;
354 FILE *file = nullptr;
355 bool initialized = false;
356 };
357
361#define DEFINE_IMAGEIO_FUNC \
362 int read_header(Dict & dict, int image_index = 0, const Region* area = 0, bool is_3d = false); \
363 int write_header(const Dict & dict, int image_index = 0, const Region * area = 0, EMUtil::EMDataType filestoragetype = EMUtil::EM_FLOAT, bool use_host_endian = true); \
364 int read_data(float* data, int image_index = 0, const Region* area = 0, bool is_3d = false); \
365 int write_data(float* data, int image_index = 0, const Region * area = 0, EMUtil::EMDataType filestoragetype = EMUtil::EM_FLOAT, bool use_host_endian = true); \
366 void flush(); \
367 bool is_complex_mode(); \
368 bool is_image_big_endian(); \
369 void init()
370
371}
372
373
374#endif //eman__imageio_h__
static bool is_host_big_endian()
Definition: byteorder.cpp:40
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
Definition: byteorder.h:131
Ctf is the base class for all CTF model.
Definition: ctf.h:60
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
FloatSize is used to describe a 1D, 2D or 3D rectangular size in floating numbers.
Definition: geometry.h:105
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
IOMode rw_mode
Definition: imageio.h:353
string filename
Definition: imageio.h:352
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
Definition: imageio.cpp:58
virtual int get_nimg()
Return the number of images in this image file.
Definition: imageio.cpp:176
virtual void flush()=0
Flush the IO buffer.
virtual int write_header(const Dict &dict, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write a header to an image.
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
bool initialized
Definition: imageio.h:355
virtual int write_data(float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write data to an image.
string get_filename() const
Definition: imageio.h:268
virtual bool is_complex_mode()=0
Is this an complex image or not.
virtual int read_header(Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the header from an image.
FILE * sfopen(const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
Run fopen safely.
Definition: imageio.cpp:135
virtual void write_ctf(const Ctf &ctf, int image_index=0)
Write CTF data to this image.
Definition: imageio.cpp:53
void check_read_access(int image_index)
Validate 'image_index' in file reading.
Definition: imageio.cpp:95
virtual int read_ctf(Ctf &ctf, int image_index=0)
Read CTF data from this image.
Definition: imageio.cpp:48
virtual bool is_single_image_format() const
Is this image format only storing 1 image or not.
Definition: imageio.h:250
virtual int read_data(float *data, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the data from an image.
FILE * file
Definition: imageio.h:354
virtual void init()=0
Do some initialization before doing the read/write.
void check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
Definition: imageio.cpp:113
virtual ~ImageIO()
Definition: imageio.cpp:44
virtual int read_data_8bit(unsigned char *data, int image_index=0, const Region *area=0, bool is_3d=false, float minval=0.0f, float maxval=0.0f)
Read the data from an image as an 8 bit array, regardless of format.
Definition: imageio.h:193
void become_host_endian(T *data, size_t n=1)
Convert data of this image into host endian format.
Definition: imageio.h:261
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
IntSize is used to describe a 1D, 2D or 3D rectangular size in integers.
Definition: geometry.h:49
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
#define ImageFormatException(desc)
Definition: exception.h:147
E2Exception class.
Definition: aligner.h:40