00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00079
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 }