39#define MAXPATHLEN (MAX_PATH * 4)
48const char *SalIO::HDR_EXT =
"hdr";
49const char *SalIO::IMG_EXT =
"img";
50const char *SalIO::MAGIC =
" IDENTIFICATION";
53SalIO::SalIO(
const string & fname,
IOMode rw)
84 bool is_new_file =
false;
87 char scan_type[MAXPATHLEN];
92 if (fgets(buf, MAXPATHLEN,
file)) {
98 while (fgets(buf, MAXPATHLEN,
file)) {
99 const char *buf1 = buf + 1;
102 sscanf(strchr(buf,
'=') + 1,
" %d", &
nx);
105 sscanf(strchr(buf,
'=') + 1,
" %d", &
ny);
108 char *t = strrchr(buf,
'\'');
109 if (t && t[-1] ==
'Y') {
117 sscanf(strchr(buf,
'\'') + 1,
" %s", scan_type);
118 if (scan_type[0] ==
'R') {
123 sscanf(strchr(buf,
'=') + 1,
" %f", &
pixel);
159 if(image_index == -1) {
163 if(image_index != 0) {
164 throw ImageReadException(
filename,
"no stack allowed for MRC image. For take 2D slice out of 3D image, read the 3D image first, then use get_clip().");
171 int xlen = 0, ylen = 0;
178 dict[
"SAL.pixel"] =
pixel;
187 LOGWARN(
"SAL write is not supported.");
202 LOGERR(
"only NON_RASTER_SCAN scan mode is supported in a SAL image");
208 int mode_size = (int)
sizeof(
short);
209 unsigned char *cdata = (
unsigned char *) data;
210 short *sdata = (
short *) data;
211 size_t row_size =
nx * mode_size;
213 size_t post_row = block_size - row_size;
216 mode_size,
nx,
ny, 1, area,
false,
220 int row_size =
nx * mode_size;
223 for (
int j = 0; j <
ny; j++) {
224 if (fread(&cdata[j * row_size], block_size, 1,
file) != 1) {
225 LOGERR(
"Incomplete SAL data read %d/%d blocks", j,
ny);
231 int xlen = 0, ylen = 0;
237 for (
int i = 0; i < ylen; i += 2) {
238 for (
int j = 0; j < xlen / 2; j++) {
239 short sw = sdata[i * xlen + j];
240 sdata[i * xlen + j] = sdata[i * xlen + xlen - j - 1];
241 sdata[i * xlen + xlen - j - 1] = sw;
246 for (
int i = xlen * ylen - 1; i >= 0; i--) {
247 data[i] =
static_cast < float >((cdata[i * 2 + 1] * UCHAR_MAX) + cdata[i * 2]);
256 LOGWARN(
"SAL write is not supported.");
Dict is a dictionary to store <string, EMObject> pair.
static void process_region_io(void *cdata, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz=1, const Region *area=0, bool need_flip=false, ImageType imgtype=IMAGE_UNKNOWN, int pre_row=0, int post_row=0)
Process image region IO.
EMDataType
Image pixel data type used in EMAN.
static void get_region_dims(const Region *area, int nx, int *area_x, int ny, int *area_y, int nz=1, int *area_z=0)
Get a region's dimensions.
ImageIO classes are designed for reading/writing various electron micrography image formats,...
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
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.
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.
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.
void check_read_access(int image_index)
Validate 'image_index' in file reading.
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.
virtual void init()=0
Do some initialization before doing the read/write.
void become_host_endian(T *data, size_t n=1)
Convert data of this image into host endian format.
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.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static const char * MAGIC
static const char * IMG_EXT
static bool is_valid(const void *first_block)
static const char * HDR_EXT
static bool sstrncmp(const char *s1, const char *s2)
Safe string compare.
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
#define ImageReadException(filename, desc)