Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

imageio.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #include <cmath>
00037 #include <cstdio>
00038 
00039 #ifdef _WIN32
00040         #include <cfloat>
00041 #endif  //_WIN32
00042 
00043 #include "imageio.h"
00044 #include "geometry.h"
00045 
00046 using namespace EMAN;
00047 
00048 ImageIO::~ImageIO()
00049 {
00050 }
00051 
00052 int ImageIO::read_ctf(Ctf &, int)
00053 {
00054         return 1;
00055 }
00056 
00057 void ImageIO::write_ctf(const Ctf &, int)
00058 {
00059 
00060 }
00061 
00062 void ImageIO::check_region(const Region * area, const FloatSize & max_size,
00063                                                    bool is_new_file,bool inbounds_only)
00064 {
00065         if (area) {
00066                 if (is_new_file) {
00067                         throw ImageReadException("", "file must exist before accessing its region");
00068                 }
00069                 int img_ndim = max_size.get_ndim();
00070                 int area_ndim = area->get_ndim();
00071 
00072                 if (area_ndim > img_ndim) {
00073                         char desc[256];
00074                         sprintf(desc, "Image is %dD. Cannot read %dD region", img_ndim, area_ndim);
00075                         throw ImageReadException("", desc);
00076                 }
00077 
00078                 // EMUtil::process_region_io handles regions that are outside the image. So some image types don't mind if the
00079                 // region is beyond the boundary. It would be ideal if they all could do this, but it would take some work.
00080                 if (inbounds_only ){
00081                         if (!area->is_region_in_box(max_size)) {
00082                                 char desc[1024];
00083                                 sprintf(desc, "Region box %s is outside image area (%d,%d,%d)",
00084                                                 area->get_string().c_str(), (int)max_size[0],
00085                                                 (int)max_size[1], (int)max_size[2]);
00086                                 throw ImageReadException("", desc);
00087                         }
00088                 }
00089         }
00090 }
00091 
00092 void ImageIO::check_region(const Region * area, const IntSize & max_size,
00093                                                    bool is_new_file, bool inbounds_only)
00094 {
00095         check_region(area, FloatSize(max_size[0], max_size[1], max_size[2]),
00096                                  is_new_file,inbounds_only);
00097 }
00098 
00099 void ImageIO::check_read_access(int image_index)
00100 {
00101         init();
00102 
00103         int nimg = get_nimg();
00104         if (image_index < 0 || image_index >= nimg) {
00105                 throw OutofRangeException(0, nimg-1, image_index, "image index");
00106         }
00107 }
00108 
00109 void ImageIO::check_read_access(int image_index, const float *data)
00110 {
00111         check_read_access(image_index);
00112         if (!data) {
00113                 throw NullPointerException("image data is NULL");
00114         }
00115 }
00116 
00117 void ImageIO::check_write_access(IOMode iomode, int image_index, int max_nimg)
00118 {
00119         init();
00120 
00121         if (iomode == READ_ONLY) {
00122                 throw ImageWriteException("", "File is not openned to write");
00123         }
00124 
00125         if ((image_index < -1) || (max_nimg > 0 && image_index >= max_nimg)) {
00126                 throw OutofRangeException(-1, max_nimg - 1, image_index, "image index");
00127         }
00128 }
00129 
00130 void ImageIO::check_write_access(IOMode iomode, int image_index,
00131                                                                  int max_nimg, const float *data)
00132 {
00133         check_write_access(iomode, image_index, max_nimg);
00134         if (!data) {
00135                 throw NullPointerException("image data is NULL");
00136         }
00137 }
00138 
00139 FILE *ImageIO::sfopen(const string & filename, IOMode mode,
00140                                           bool * is_new, bool overwrite)
00141 {
00142         FILE *f = 0;
00143         if (mode == READ_ONLY) {
00144                 f = fopen(filename.c_str(), "rb");
00145         }
00146         else if (mode == READ_WRITE) {
00147                 if (overwrite) {
00148                         f = fopen(filename.c_str(), "wb");
00149                         if (is_new) {
00150                                 *is_new = true;
00151                         }
00152                 }
00153                 else {
00154                         f = fopen(filename.c_str(), "r+b");
00155                         if (!f) {
00156                                 FILE *f1 = fopen(filename.c_str(), "wb");
00157                                 if (!f1) {
00158                                         throw FileAccessException(filename);
00159                                 }
00160                                 else {
00161                                         if (is_new) {
00162                                                 *is_new = true;
00163                                         }
00164                                         fclose(f1);
00165                                         f1 = 0;
00166                                         f = fopen(filename.c_str(), "r+b");
00167                                 }
00168                         }
00169                 }
00170         }
00171         else if (mode == WRITE_ONLY) {
00172                 f = fopen(filename.c_str(), "wb");
00173                 if (is_new) {
00174                         *is_new = true;
00175                 }
00176         }
00177 
00178         if (!f) {
00179                 throw FileAccessException(filename);
00180         }
00181         return f;
00182 }
00183 
00184 
00185 int ImageIO::get_nimg()
00186 {
00187         init();
00188         return 1;
00189 }

Generated on Tue Jun 11 13:40:38 2013 for EMAN2 by  doxygen 1.3.9.1