EMAN2
imageio.cpp
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#include <cmath>
33#include <cstdio>
34
35#include "imageio.h"
36#include "geometry.h"
37
38using namespace EMAN;
39
40ImageIO::ImageIO(const string & fname, IOMode rw)
41 : filename(fname), rw_mode(rw)
42{}
43
45{
46}
47
49{
50 return 1;
51}
52
53void ImageIO::write_ctf(const Ctf &, int)
54{
55
56}
57
58void ImageIO::check_region(const Region * area, const FloatSize & max_size,
59 bool is_new_file,bool inbounds_only)
60{
61 if (area) {
62 if (is_new_file) {
63 throw ImageReadException("", "file must exist before accessing its region");
64 }
65 int img_ndim = max_size.get_ndim();
66 int area_ndim = area->get_ndim();
67
68 if (area_ndim > img_ndim) {
69 char desc[256];
70 sprintf(desc, "Image is %dD. Cannot read %dD region", img_ndim, area_ndim);
71 throw ImageReadException("", desc);
72 }
73
74 // EMUtil::process_region_io handles regions that are outside the image. So some image types don't mind if the
75 // region is beyond the boundary. It would be ideal if they all could do this, but it would take some work.
76 if (inbounds_only ){
77 if (!area->is_region_in_box(max_size)) {
78 char desc[1024];
79 sprintf(desc, "Region box %s is outside image area (%d,%d,%d)",
80 area->get_string().c_str(), (int)max_size[0],
81 (int)max_size[1], (int)max_size[2]);
82 throw ImageReadException("", desc);
83 }
84 }
85 }
86}
87
88void ImageIO::check_region(const Region * area, const IntSize & max_size,
89 bool is_new_file, bool inbounds_only)
90{
91 check_region(area, FloatSize(max_size[0], max_size[1], max_size[2]),
92 is_new_file,inbounds_only);
93}
94
95void ImageIO::check_read_access(int image_index)
96{
97 init();
98
99 int nimg = get_nimg();
100 if (image_index < 0 || image_index >= nimg) {
101 throw OutofRangeException(0, nimg-1, image_index, "image index");
102 }
103}
104
105void ImageIO::check_read_access(int image_index, const float *data)
106{
107 check_read_access(image_index);
108 if (!data) {
109 throw NullPointerException("image data is NULL");
110 }
111}
112
113void ImageIO::check_write_access(IOMode iomode, int image_index, int max_nimg)
114{
115 init();
116
117 if (iomode == READ_ONLY) {
118 throw ImageWriteException("", "File is not opened to write");
119 }
120
121 if ((image_index < -1) || (max_nimg > 0 && image_index >= max_nimg)) {
122 throw OutofRangeException(-1, max_nimg - 1, image_index, "image index");
123 }
124}
125
126void ImageIO::check_write_access(IOMode iomode, int image_index,
127 int max_nimg, const float *data)
128{
129 check_write_access(iomode, image_index, max_nimg);
130 if (!data) {
131 throw NullPointerException("image data is NULL");
132 }
133}
134
135FILE *ImageIO::sfopen(const string & filename, IOMode mode,
136 bool * is_new, bool overwrite)
137{
138 FILE *f = 0;
139 if (mode == READ_ONLY)
140 f = fopen(filename.c_str(), "rb");
141 else if (mode == READ_WRITE) {
142 if (overwrite) {
143 f = fopen(filename.c_str(), "wb");
144 if (is_new)
145 *is_new = true;
146 }
147 else {
148 f = fopen(filename.c_str(), "r+b");
149 if (!f) {
150 FILE *f1 = fopen(filename.c_str(), "wb");
151 if (!f1)
153 else {
154 if (is_new)
155 *is_new = true;
156 fclose(f1);
157 f1 = 0;
158 f = fopen(filename.c_str(), "r+b");
159 }
160 }
161 }
162 }
163 else if (mode == WRITE_ONLY) {
164 f = fopen(filename.c_str(), "wb");
165 if (is_new)
166 *is_new = true;
167 }
168
169 if (!f)
171
172 return f;
173}
174
175
177{
178 init();
179 return 1;
180}
Ctf is the base class for all CTF model.
Definition: ctf.h:60
FloatSize is used to describe a 1D, 2D or 3D rectangular size in floating numbers.
Definition: geometry.h:105
int get_ndim() const
Get its dimension, 1D, 2D, or 3D.
Definition: geometry.h:147
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
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 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
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
int get_ndim() const
Get the region's dimension.
Definition: geometry.h:644
string get_string() const
Get the description of this region in a string.
Definition: geometry.cpp:138
bool is_region_in_box(const FloatSize &box) const
To check whether 'this' region is inside a given box assuming the box's origins are (0,...
Definition: geometry.cpp:123
#define ImageReadException(filename, desc)
Definition: exception.h:204
#define OutofRangeException(low, high, input, objname)
Definition: exception.h:334
#define FileAccessException(filename)
Definition: exception.h:187
#define ImageWriteException(imagename, desc)
Definition: exception.h:223
#define NullPointerException(desc)
Definition: exception.h:241
E2Exception class.
Definition: aligner.h:40