Commit aadbb553 authored by Amir Mohammadi's avatar Amir Mohammadi
Browse files

fixes errors and adds more test cases

parent 6bed680f
...@@ -104,20 +104,74 @@ struct pam { ...@@ -104,20 +104,74 @@ struct pam {
// } // }
/* File open/close that handles "-" as stdin/stdout and checks errors. */
FILE*
pm_openr(const char * const name) {
FILE* f;
if (strcmp(name, "-") == 0)
f = stdin;
else {
#ifndef VMS
f = fopen(name, "rb");
#else
f = fopen (name, "r", "ctx=stm");
#endif
if (f == NULL) {
fprintf(stderr, name);
// pm_perror(name);
exit(1);
}
}
return f;
}
FILE*
pm_openw(const char * const name) {
FILE* f;
if (strcmp(name, "-") == 0)
f = stdout;
else {
#ifndef VMS
f = fopen(name, "wb");
#else
f = fopen (name, "w", "mbc=32", "mbf=2"); /* set buffer factors */
#endif
if (f == NULL) {
fprintf(stderr, name);
// pm_perror(name);
exit(1);
}
}
return f;
}
void
pm_close(FILE * const f) {
// fprintf(stderr, "I am closing a file\n");
fflush( f );
if ( ferror( f ) )
fprintf(stderr, "a file read or write error occurred at some point" );
if ( f != stdin )
if ( fclose( f ) != 0 )
fprintf(stderr, "fclose");
}
static boost::shared_ptr<std::FILE> make_cfile(const char *filename, const char *flags) static boost::shared_ptr<std::FILE> make_cfile(const char *filename, const char *flags)
{ {
std::FILE* fp; std::FILE* fp;
if(strcmp(flags, "r") == 0) if(strcmp(flags, "r") == 0)
fp = fopen(filename, "r"); fp = pm_openr(filename);
else // write else // write
fp = fopen(filename, "w"); fp = pm_openw(filename);
if(fp == 0) { if(fp == 0) {
boost::format m("cannot open file `%s'"); boost::format m("cannot open file `%s'");
m % filename; m % filename;
throw std::runtime_error(m.str()); throw std::runtime_error(m.str());
} }
return boost::shared_ptr<std::FILE>(fp, fclose); return boost::shared_ptr<std::FILE>(fp, pm_close);
} }
static void pnm_readpaminit(FILE *file, struct pam * const pamP, const int size) { static void pnm_readpaminit(FILE *file, struct pam * const pamP, const int size) {
...@@ -163,6 +217,7 @@ static void pnm_readpaminit(FILE *file, struct pam * const pamP, const int size) ...@@ -163,6 +217,7 @@ static void pnm_readpaminit(FILE *file, struct pam * const pamP, const int size)
static int * pnm_allocpam(struct pam * const pamP) { static int * pnm_allocpam(struct pam * const pamP) {
int *img_data; int *img_data;
/* Perform operations. */ /* Perform operations. */
// fprintf(stderr, "%d, %d, %d\n", pamP->width, pamP->height, sizeof(int));
if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) { if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) {
img_data = (int *) malloc((3 * pamP->width * pamP->height) * sizeof(int)); img_data = (int *) malloc((3 * pamP->width * pamP->height) * sizeof(int));
} else { } else {
...@@ -174,27 +229,28 @@ static void pnm_readpam(struct pam * const pamP, int *img_data) { ...@@ -174,27 +229,28 @@ static void pnm_readpam(struct pam * const pamP, int *img_data) {
/* Read the image data. */ /* Read the image data. */
if ((pamP->format == PBM_ASCII) || (pamP->format == PBM_BINARY)) { if ((pamP->format == PBM_ASCII) || (pamP->format == PBM_BINARY)) {
read_pbm_data(pamP->file, img_data, pamP->plainformat); read_pbm_data(pamP->file, img_data, pamP->width * pamP->height, pamP->plainformat, pamP->width);
} else if ((pamP->format == PGM_ASCII) || (pamP->format == PGM_BINARY)) { } else if ((pamP->format == PGM_ASCII) || (pamP->format == PGM_BINARY)) {
read_pgm_data(pamP->file, img_data, pamP->plainformat); read_pgm_data(pamP->file, img_data, pamP->width * pamP->height, pamP->plainformat, pamP->bytes_per_sample);
} else if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) { } else if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) {
read_ppm_data(pamP->file, img_data, pamP->plainformat); read_ppm_data(pamP->file, img_data, 3 * pamP->width * pamP->height, pamP->plainformat, pamP->bytes_per_sample);
} }
} }
static void pnm_writepam(struct pam * const pamP, int *img_data) { static void pnm_writepam(struct pam * const pamP, int *img_data) {
char * file_name="";
/* Write the output image file. */ /* Write the output image file. */
if ((pamP->format == PBM_ASCII) || (pamP->format == PBM_BINARY)) { if ((pamP->format == PBM_ASCII) || (pamP->format == PBM_BINARY)) {
write_pbm_file(pamP->file, img_data, file_name, write_pbm_file(pamP->file, img_data,
pamP->width, pamP->height, 1, 1, 32, pamP->plainformat pamP->width, pamP->height, 1, 1, 32, pamP->plainformat
); );
} else if ((pamP->format == PGM_ASCII) || (pamP->format == PGM_BINARY)) { } else if ((pamP->format == PGM_ASCII) || (pamP->format == PGM_BINARY)) {
write_pgm_file(pamP->file, img_data, file_name, write_pgm_file(pamP->file, img_data,
pamP->width, pamP->height, 1, 1, pamP->maxval, 16, pamP->plainformat pamP->width, pamP->height, 1, 1, pamP->maxval, 16, pamP->plainformat,
pamP->bytes_per_sample
); );
} else if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) { } else if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) {
write_ppm_file(pamP->file, img_data, file_name, write_ppm_file(pamP->file, img_data,
pamP->width, pamP->height, 1, 1, pamP->maxval, pamP->plainformat pamP->width, pamP->height, 1, 1, pamP->maxval, pamP->plainformat,
pamP->bytes_per_sample
); );
} }
} }
...@@ -246,7 +302,9 @@ void im_load_gray(struct pam *in_pam, bob::io::base::array::interface& b) { ...@@ -246,7 +302,9 @@ void im_load_gray(struct pam *in_pam, bob::io::base::array::interface& b) {
T *element = static_cast<T*>(b.ptr()); T *element = static_cast<T*>(b.ptr());
int *img_data = pnm_allocpam(in_pam); int *img_data = pnm_allocpam(in_pam);
// fprintf(stderr, "img_data: %x\n", img_data);
pnm_readpam(in_pam, img_data); pnm_readpam(in_pam, img_data);
// fprintf(stderr, "img_data after read: %x\n", img_data);
for(size_t y=0; y<info.shape[0]; ++y) for(size_t y=0; y<info.shape[0]; ++y)
{ {
for(size_t x=0; x<info.shape[1]; ++x) for(size_t x=0; x<info.shape[1]; ++x)
...@@ -256,7 +314,9 @@ void im_load_gray(struct pam *in_pam, bob::io::base::array::interface& b) { ...@@ -256,7 +314,9 @@ void im_load_gray(struct pam *in_pam, bob::io::base::array::interface& b) {
c++; c++;
} }
} }
// fprintf(stderr, "freeing img_data: %x\n", img_data);
free(img_data); free(img_data);
// fprintf(stderr, "freed img_data\n");
} }
template <typename T> static template <typename T> static
...@@ -271,16 +331,13 @@ void im_load_color(struct pam *in_pam, bob::io::base::array::interface& b) { ...@@ -271,16 +331,13 @@ void im_load_color(struct pam *in_pam, bob::io::base::array::interface& b) {
int *img_data = pnm_allocpam(in_pam); int *img_data = pnm_allocpam(in_pam);
pnm_readpam(in_pam, img_data); pnm_readpam(in_pam, img_data);
for(size_t y=0; y<info.shape[0]; ++y) for(size_t y=0; y<info.shape[1]; ++y)
{ {
for(size_t x=0; x<info.shape[1]; ++x) for(size_t x=0; x<info.shape[2]; ++x)
{ {
element_r[x] = img_data[c+0]; element_r[y*info.shape[2] + x] = img_data[c+0];
element_g[x] = img_data[c+1]; element_g[y*info.shape[2] + x] = img_data[c+1];
element_b[x] = img_data[c+2]; element_b[y*info.shape[2] + x] = img_data[c+2];
++element_r;
++element_g;
++element_b;
c = c + 3; c = c + 3;
} }
} }
...@@ -358,16 +415,13 @@ static void im_save_color(const bob::io::base::array::interface& b, struct pam * ...@@ -358,16 +415,13 @@ static void im_save_color(const bob::io::base::array::interface& b, struct pam *
const T *element_b = element_g + frame_size; const T *element_b = element_g + frame_size;
int *img_data = pnm_allocpam(out_pam); int *img_data = pnm_allocpam(out_pam);
for(size_t y=0; y<info.shape[0]; ++y) for(size_t y=0; y<info.shape[1]; ++y)
{ {
for(size_t x=0; x<info.shape[1]; ++x) for(size_t x=0; x<info.shape[2]; ++x)
{ {
img_data[c+0] = element_r[x]; img_data[c+0] = element_r[y*info.shape[2] + x];
img_data[c+1] = element_g[x]; img_data[c+1] = element_g[y*info.shape[2] + x];
img_data[c+2] = element_b[x]; img_data[c+2] = element_b[y*info.shape[2] + x];
++element_r;
++element_g;
++element_b;
c = c + 3; c = c + 3;
} }
} }
...@@ -393,7 +447,7 @@ static void im_save (const std::string& filename, const bob::io::base::array::in ...@@ -393,7 +447,7 @@ static void im_save (const std::string& filename, const bob::io::base::array::in
out_pam.height = (info.nd == 2 ? info.shape[0] : info.shape[1]); out_pam.height = (info.nd == 2 ? info.shape[0] : info.shape[1]);
out_pam.width = (info.nd == 2 ? info.shape[1] : info.shape[2]); out_pam.width = (info.nd == 2 ? info.shape[1] : info.shape[2]);
out_pam.depth = (info.nd == 2 ? 1 : 3); out_pam.depth = (info.nd == 2 ? 1 : 3);
out_pam.maxval = (bob::io::base::array::t_uint8 ? 255 : 65535); out_pam.maxval = (info.dtype == bob::io::base::array::t_uint8 ? 255 : 65535);
out_pam.bytes_per_sample = (info.dtype == bob::io::base::array::t_uint8 ? 1 : 2); out_pam.bytes_per_sample = (info.dtype == bob::io::base::array::t_uint8 ? 1 : 2);
if( ext.compare(".pbm") == 0) if( ext.compare(".pbm") == 0)
{ {
......
...@@ -28,8 +28,8 @@ ...@@ -28,8 +28,8 @@
#include "pnmio.h" #include "pnmio.h"
#define MAXLINE 1024 #define MAXLINE 1024
#define LITTLE_ENDIAN -1 // #define LITTLE_ENDIAN -1
#define BIG_ENDIAN 1 // #define BIG_ENDIAN 1
#define GREYSCALE_TYPE 0 /* used for PFM */ #define GREYSCALE_TYPE 0 /* used for PFM */
#define RGB_TYPE 1 /* used for PFM */ #define RGB_TYPE 1 /* used for PFM */
...@@ -155,8 +155,8 @@ void read_pbm_header(FILE *f, int *img_xdim, int *img_ydim, int *is_ascii) ...@@ -155,8 +155,8 @@ void read_pbm_header(FILE *f, int *img_xdim, int *img_ydim, int *is_ascii)
exit(1); exit(1);
} }
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d\n", // fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d\n",
magic, x_val, y_val); // magic, x_val, y_val);
*img_xdim = x_val; *img_xdim = x_val;
*img_ydim = y_val; *img_ydim = y_val;
} }
...@@ -217,8 +217,8 @@ void read_pgm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_colors, int ...@@ -217,8 +217,8 @@ void read_pgm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_colors, int
exit(1); exit(1);
} }
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n", // fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
magic, x_val, y_val, maxcolors_val); // magic, x_val, y_val, maxcolors_val);
*img_xdim = x_val; *img_xdim = x_val;
*img_ydim = y_val; *img_ydim = y_val;
*img_colors = maxcolors_val; *img_colors = maxcolors_val;
...@@ -280,8 +280,8 @@ void read_ppm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_colors, int ...@@ -280,8 +280,8 @@ void read_ppm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_colors, int
exit(1); exit(1);
} }
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n", // fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
magic, x_val, y_val, maxcolors_val); // magic, x_val, y_val, maxcolors_val);
*img_xdim = x_val; *img_xdim = x_val;
*img_ydim = y_val; *img_ydim = y_val;
*img_colors = maxcolors_val; *img_colors = maxcolors_val;
...@@ -356,8 +356,8 @@ void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type, int * ...@@ -356,8 +356,8 @@ void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type, int *
exit(1); exit(1);
} }
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, aspect_ratio.f=%f\n", // fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, aspect_ratio.f=%f\n",
magic, x_val, y_val, aspect_ratio.f); // magic, x_val, y_val, aspect_ratio.f);
/* FIXME: Aspect ratio different to 1.0 is not yet supported. */ /* FIXME: Aspect ratio different to 1.0 is not yet supported. */
if (!floatEqualComparison(aspect_ratio.f, -1.0, 1E-06) && if (!floatEqualComparison(aspect_ratio.f, -1.0, 1E-06) &&
...@@ -379,37 +379,47 @@ void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type, int * ...@@ -379,37 +379,47 @@ void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type, int *
/* read_pbm_data: /* read_pbm_data:
* Read the data contents of a PBM (portable bit map) file. * Read the data contents of a PBM (portable bit map) file.
*/ */
void read_pbm_data(FILE *f, int *img_in, int is_ascii) void read_pbm_data(FILE *f, int *img_in, int img_size, int is_ascii, int img_width)
{ {
int i=0, c; int i=0, c;
int lum_val; int lum_val;
int k; int k;
int row_position = 0;
/* Read the rest of the PPM file. */ /* Read the rest of the PPM file. */
while ((c = fgetc(f)) != EOF) { while ((c = fgetc(f)) != EOF) {
ungetc(c, f); ungetc(c, f);
if (is_ascii == 1) { if (is_ascii == 1) {
fscanf(f, "%d", &lum_val); fscanf(f, "%d", &lum_val);
if (i >= img_size) return;
img_in[i++] = lum_val; img_in[i++] = lum_val;
} else { } else {
lum_val = fgetc(f); lum_val = fgetc(f);
/* Decode the image contents byte-by-byte. */ /* Decode the image contents byte-by-byte. */
for (k = 0; k < 8; k++) { for (k = 0; k < 8; k++) {
img_in[i++] = (lum_val >> (7-k)) & 0x1; if (i >= img_size) return;
img_in[i++] = (lum_val >> (7-k)) & 0x1;
// fprintf(stderr, "i: %d, %d\n", i, img_in[i]);
row_position++;
if (row_position >= img_width) {
row_position = 0;
break;
}
} }
} }
} }
fclose(f); // fclose(f);
} }
/* read_pgm_data: /* read_pgm_data:
* Read the data contents of a PGM (portable grey map) file. * Read the data contents of a PGM (portable grey map) file.
*/ */
void read_pgm_data(FILE *f, int *img_in, int is_ascii) void read_pgm_data(FILE *f, int *img_in, int img_size, int is_ascii,
unsigned int bytes_per_sample)
{ {
int i=0, c; int i=0, c;
int lum_val; int lum_val;
int k; // int k;
/* Read the rest of the PPM file. */ /* Read the rest of the PPM file. */
while ((c = fgetc(f)) != EOF) { while ((c = fgetc(f)) != EOF) {
...@@ -417,17 +427,25 @@ void read_pgm_data(FILE *f, int *img_in, int is_ascii) ...@@ -417,17 +427,25 @@ void read_pgm_data(FILE *f, int *img_in, int is_ascii)
if (is_ascii == 1) { if (is_ascii == 1) {
fscanf(f, "%d", &lum_val); fscanf(f, "%d", &lum_val);
} else { } else {
lum_val = fgetc(f); if (bytes_per_sample == 1) {
lum_val = fgetc(f);
} else {
lum_val = fgetc(f);
lum_val = lum_val << 8;
lum_val |= fgetc(f);
}
} }
if (i >= img_size) return;
img_in[i++] = lum_val; img_in[i++] = lum_val;
} }
fclose(f); // fclose(f);
} }
/* read_ppm_data: /* read_ppm_data:
* Read the data contents of a PPM (portable pix map) file. * Read the data contents of a PPM (portable pix map) file.
*/ */
void read_ppm_data(FILE *f, int *img_in, int is_ascii) void read_ppm_data(FILE *f, int *img_in, int img_size, int is_ascii,
unsigned int bytes_per_sample)
{ {
int i=0, c; int i=0, c;
int r_val, g_val, b_val; int r_val, g_val, b_val;
...@@ -438,15 +456,30 @@ void read_ppm_data(FILE *f, int *img_in, int is_ascii) ...@@ -438,15 +456,30 @@ void read_ppm_data(FILE *f, int *img_in, int is_ascii)
if (is_ascii == 1) { if (is_ascii == 1) {
fscanf(f, "%d %d %d", &r_val, &g_val, &b_val); fscanf(f, "%d %d %d", &r_val, &g_val, &b_val);
} else { } else {
r_val = fgetc(f); if (bytes_per_sample == 1) {
g_val = fgetc(f); r_val = fgetc(f);
b_val = fgetc(f); g_val = fgetc(f);
b_val = fgetc(f);
} else {
r_val = fgetc(f);
r_val = r_val << 8;
r_val |= fgetc(f);
g_val = fgetc(f);
g_val = g_val << 8;
g_val |= fgetc(f);
b_val = fgetc(f);
b_val = b_val << 8;
b_val |= fgetc(f);
}
} }
if (i >= img_size) return;
img_in[i++] = r_val; img_in[i++] = r_val;
img_in[i++] = g_val; img_in[i++] = g_val;
img_in[i++] = b_val; img_in[i++] = b_val;
} }
fclose(f); // fclose(f);
} }
/* read_pfm_data: /* read_pfm_data:
...@@ -474,18 +507,19 @@ void read_pfm_data(FILE *f, float *img_in, int img_type, int endianess) ...@@ -474,18 +507,19 @@ void read_pfm_data(FILE *f, float *img_in, int img_type, int endianess)
img_in[i++] = g_val; img_in[i++] = g_val;
} }
} }
fclose(f); // fclose(f);
} }
/* write_pbm_file: /* write_pbm_file:
* Write the contents of a PBM (portable bit map) file. * Write the contents of a PBM (portable bit map) file.
*/ */
void write_pbm_file(FILE *f, int *img_out, char *img_out_fname, void write_pbm_file(FILE *f, int *img_out,
int x_size, int y_size, int x_scale_val, int y_scale_val, int linevals, int x_size, int y_size, int x_scale_val, int y_scale_val, int linevals,
int is_ascii) int is_ascii)
{ {
int i, j, x_scaled_size, y_scaled_size; int i, j, x_scaled_size, y_scaled_size;
int k, v, temp, step; int k, v, temp, step;
int row_position = 0;
x_scaled_size = x_size * x_scale_val; x_scaled_size = x_size * x_scale_val;
y_scaled_size = y_size * y_scale_val; y_scaled_size = y_size * y_scale_val;
...@@ -512,6 +546,11 @@ void write_pbm_file(FILE *f, int *img_out, char *img_out_fname, ...@@ -512,6 +546,11 @@ void write_pbm_file(FILE *f, int *img_out, char *img_out_fname,
for (k = 0; k < 8; k++) { for (k = 0; k < 8; k++) {
v = img_out[i*x_scaled_size+j+k]; v = img_out[i*x_scaled_size+j+k];
temp |= (v << (7-k)); temp |= (v << (7-k));
row_position++;
if (row_position >= x_size) {
row_position = 0;
break;
}
} }
fprintf(f, "%c", temp); fprintf(f, "%c", temp);
} }
...@@ -520,15 +559,16 @@ void write_pbm_file(FILE *f, int *img_out, char *img_out_fname, ...@@ -520,15 +559,16 @@ void write_pbm_file(FILE *f, int *img_out, char *img_out_fname,
} }
} }
} }
fclose(f); // fclose(f);
} }
/* write_pgm_file: /* write_pgm_file:
* Write the contents of a PGM (portable grey map) file. * Write the contents of a PGM (portable grey map) file.
*/ */
void write_pgm_file(FILE *f, int *img_out, char *img_out_fname, void write_pgm_file(FILE *f, int *img_out,
int x_size, int y_size, int x_scale_val, int y_scale_val, int x_size, int y_size, int x_scale_val, int y_scale_val,
int img_colors, int linevals, int is_ascii) int img_colors, int linevals, int is_ascii,
unsigned int bytes_per_sample)
{ {
int i, j, x_scaled_size, y_scaled_size; int i, j, x_scaled_size, y_scaled_size;
...@@ -556,19 +596,24 @@ void write_pgm_file(FILE *f, int *img_out, char *img_out_fname, ...@@ -556,19 +596,24 @@ void write_pgm_file(FILE *f, int *img_out, char *img_out_fname,
fprintf(f, "\n"); fprintf(f, "\n");
} }
} else { } else {
fprintf(f, "%c", img_out[i*x_scaled_size+j]); if (bytes_per_sample == 1) {
fprintf(f, "%c", img_out[i*x_scaled_size+j]);
} else {
fprintf(f, "%c", img_out[i*x_scaled_size+j]);
fprintf(f, "%c", (img_out[i*x_scaled_size+j] >> 8));
}
} }
} }
} }
fclose(f); // fclose(f);
} }
/* write_ppm_file: /* write_ppm_file:
* Write the contents of a PPM (portable pix map) file. * Write the contents of a PPM (portable pix map) file.
*/ */
void write_ppm_file(FILE *f, int *img_out, char *img_out_fname, void write_ppm_file(FILE *f, int *img_out,
int x_size, int y_size, int x_scale_val, int y_scale_val, int x_size, int y_size, int x_scale_val, int y_scale_val,
int img_colors, int is_ascii) int img_colors, int is_ascii, unsigned int bytes_per_sample)
{ {
int i, j, x_scaled_size, y_scaled_size; int i, j, x_scaled_size, y_scaled_size;
...@@ -599,14 +644,25 @@ void write_ppm_file(FILE *f, int *img_out, char *img_out_fname, ...@@ -599,14 +644,25 @@ void write_ppm_file(FILE *f, int *img_out, char *img_out_fname,