39const char *PgmIO::MAGIC_BINARY =
"P5";
40const char *PgmIO::MAGIC_ASCII =
"P2";
42PgmIO::PgmIO(
const string & fname,
IOMode rw)
43:
ImageIO(fname, rw), is_big_endian(true),
44 nx(0), ny(0), maxval(0), minval(0),
45 file_offset(0), rendermin(0), rendermax(0), renderbits(16)
59 int read_int_and_space(FILE * in)
65 while (!isspace(c = getc(in))) {
66 buf[i] =
static_cast < char >(c);
84 bool is_new_file =
false;
88 const int bufsz = 1024;
91 buf[0] =
static_cast < char >(getc(
file));
92 buf[1] =
static_cast < char >(getc(
file));
102 while ((c =
static_cast < char >(getc(
file))) ==
'#') {
103 fgets(buf, bufsz,
file);
107 nx = read_int_and_space(
file);
108 ny = read_int_and_space(
file);
111 if (
nx <= 0 ||
ny <= 0) {
136 if(image_index == -1) {
140 if(image_index != 0) {
141 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().");
148 int xlen = 0, ylen = 0;
155 dict[
"PGM.max_gray"] =
maxval;
156 dict[
"PGM.min_gray"] =
minval;
169 if(image_index == -1) {
172 if(image_index != 0) {
179 LOGERR(
"Cannot write 3D image as PGM. Your image nz = %d", nz);
216 unsigned char *cdata = (
unsigned char *) (data);
217 size_t mode_size =
sizeof(
unsigned char);
220 mode_size,
nx,
ny, 1, area,
true);
222 int xlen = 0, ylen = 0;
225 for (
int k = xlen * ylen - 1; k >= 0; k--) {
226 data[k] =
static_cast < float >(cdata[k]);
253 unsigned char *cdata=(
unsigned char *)malloc(
nx*
ny);
257 for(
int j=0; j<
ny; ++j ) {
258 for(
int i=0; i<
nx; ++i) {
260 new_add = (
ny-1-j)*
nx + i;
266 cdata[new_add] = 255;
274 size_t mode_size =
sizeof(
unsigned char);
277 mode_size,
nx,
ny, 1, area);
Dict is a dictionary to store <string, EMObject> pair.
bool has_key(const string &key) const
Ask the Dictionary if it as a particular key.
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 getRenderMinMax(float *data, const int nx, const int ny, float &rendermin, float &rendermax, int &renderbits, const int nz=1)
Calculate the min and max pixel value accepted for image nomalization, if we did not get them from im...
static void getRenderLimits(const Dict &dict, float &rendermin, float &rendermax, int &renderbits)
Get the min and max pixel value accepted for image nomalization from image attribute dictionary,...
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 check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
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.
static bool is_valid(const void *first_block)
static const char * MAGIC_BINARY
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static bool is_nan(const float number)
tell whether a float value is a NaN
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)
off_t portable_ftell(FILE *fp)
int portable_fseek(FILE *fp, off_t offset, int whence)