Commit aadbb553 authored by Amir Mohammadi's avatar Amir Mohammadi

fixes errors and adds more test cases

parent 6bed680f
......@@ -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)
{
std::FILE* fp;
if(strcmp(flags, "r") == 0)
fp = fopen(filename, "r");
fp = pm_openr(filename);
else // write
fp = fopen(filename, "w");
fp = pm_openw(filename);
if(fp == 0) {
boost::format m("cannot open file `%s'");
m % filename;
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) {
......@@ -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) {
int *img_data;
/* Perform operations. */
// fprintf(stderr, "%d, %d, %d\n", pamP->width, pamP->height, sizeof(int));
if ((pamP->format == PPM_ASCII) || (pamP->format == PPM_BINARY)) {
img_data = (int *) malloc((3 * pamP->width * pamP->height) * sizeof(int));
} else {
......@@ -174,27 +229,28 @@ static void pnm_readpam(struct pam * const pamP, int *img_data) {
/* Read the image data. */
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)) {
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)) {
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) {
char * file_name="";
/* Write the output image file. */
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
);
} else if ((pamP->format == PGM_ASCII) || (pamP->format == PGM_BINARY)) {
write_pgm_file(pamP->file, img_data, file_name,
pamP->width, pamP->height, 1, 1, pamP->maxval, 16, pamP->plainformat
write_pgm_file(pamP->file, img_data,
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)) {
write_ppm_file(pamP->file, img_data, file_name,
pamP->width, pamP->height, 1, 1, pamP->maxval, pamP->plainformat
write_ppm_file(pamP->file, img_data,
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) {
T *element = static_cast<T*>(b.ptr());
int *img_data = pnm_allocpam(in_pam);
// fprintf(stderr, "img_data: %x\n", 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 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) {
c++;
}
}
// fprintf(stderr, "freeing img_data: %x\n", img_data);
free(img_data);
// fprintf(stderr, "freed img_data\n");
}
template <typename T> static
......@@ -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);
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_g[x] = img_data[c+1];
element_b[x] = img_data[c+2];
++element_r;
++element_g;
++element_b;
element_r[y*info.shape[2] + x] = img_data[c+0];
element_g[y*info.shape[2] + x] = img_data[c+1];
element_b[y*info.shape[2] + x] = img_data[c+2];
c = c + 3;
}
}
......@@ -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;
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+1] = element_g[x];
img_data[c+2] = element_b[x];
++element_r;
++element_g;
++element_b;
img_data[c+0] = element_r[y*info.shape[2] + x];
img_data[c+1] = element_g[y*info.shape[2] + x];
img_data[c+2] = element_b[y*info.shape[2] + x];
c = c + 3;
}
}
......@@ -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.width = (info.nd == 2 ? info.shape[1] : info.shape[2]);
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);
if( ext.compare(".pbm") == 0)
{
......
......@@ -28,8 +28,8 @@
#include "pnmio.h"
#define MAXLINE 1024
#define LITTLE_ENDIAN -1
#define BIG_ENDIAN 1
// #define LITTLE_ENDIAN -1
// #define BIG_ENDIAN 1
#define GREYSCALE_TYPE 0 /* 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)
exit(1);
}
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d\n",
magic, x_val, y_val);
// fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d\n",
// magic, x_val, y_val);
*img_xdim = x_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
exit(1);
}
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
magic, x_val, y_val, maxcolors_val);
// fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
// magic, x_val, y_val, maxcolors_val);
*img_xdim = x_val;
*img_ydim = y_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
exit(1);
}
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
magic, x_val, y_val, maxcolors_val);
// fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, maxcolors_val=%d\n",
// magic, x_val, y_val, maxcolors_val);
*img_xdim = x_val;
*img_ydim = y_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 *
exit(1);
}
fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, aspect_ratio.f=%f\n",
magic, x_val, y_val, aspect_ratio.f);
// fprintf(stderr, "Info: magic=%s, x_val=%d, y_val=%d, aspect_ratio.f=%f\n",
// magic, x_val, y_val, aspect_ratio.f);
/* FIXME: Aspect ratio different to 1.0 is not yet supported. */
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 *
/* read_pbm_data:
* 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 lum_val;
int k;
int row_position = 0;
/* Read the rest of the PPM file. */
while ((c = fgetc(f)) != EOF) {
ungetc(c, f);
if (is_ascii == 1) {
fscanf(f, "%d", &lum_val);
if (i >= img_size) return;
img_in[i++] = lum_val;
} else {
lum_val = fgetc(f);
/* Decode the image contents byte-by-byte. */
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 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 lum_val;
int k;
// int k;
/* Read the rest of the PPM file. */
while ((c = fgetc(f)) != EOF) {
......@@ -417,17 +427,25 @@ void read_pgm_data(FILE *f, int *img_in, int is_ascii)
if (is_ascii == 1) {
fscanf(f, "%d", &lum_val);
} 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;
}
fclose(f);
// fclose(f);
}
/* read_ppm_data:
* 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 r_val, g_val, b_val;
......@@ -438,15 +456,30 @@ void read_ppm_data(FILE *f, int *img_in, int is_ascii)
if (is_ascii == 1) {
fscanf(f, "%d %d %d", &r_val, &g_val, &b_val);
} else {
r_val = fgetc(f);
g_val = fgetc(f);
b_val = fgetc(f);
if (bytes_per_sample == 1) {
r_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++] = g_val;
img_in[i++] = b_val;
}
fclose(f);
// fclose(f);
}
/* read_pfm_data:
......@@ -474,18 +507,19 @@ void read_pfm_data(FILE *f, float *img_in, int img_type, int endianess)
img_in[i++] = g_val;
}
}
fclose(f);
// fclose(f);
}
/* write_pbm_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 is_ascii)
{
int i, j, x_scaled_size, y_scaled_size;
int k, v, temp, step;
int row_position = 0;
x_scaled_size = x_size * x_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,
for (k = 0; k < 8; k++) {
v = img_out[i*x_scaled_size+j+k];
temp |= (v << (7-k));
row_position++;
if (row_position >= x_size) {
row_position = 0;
break;
}
}
fprintf(f, "%c", temp);
}
......@@ -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 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 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;
......@@ -556,19 +596,24 @@ void write_pgm_file(FILE *f, int *img_out, char *img_out_fname,
fprintf(f, "\n");
}
} 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 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 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;
......@@ -599,14 +644,25 @@ void write_ppm_file(FILE *f, int *img_out, char *img_out_fname,
fprintf(f, "\n");
}
} else {
fprintf(f, "%c%c%c",
img_out[3*(i*x_scaled_size+j)+0],
img_out[3*(i*x_scaled_size+j)+1],
img_out[3*(i*x_scaled_size+j)+2]);
if (bytes_per_sample == 1) {
fprintf(f, "%c%c%c",
img_out[3*(i*x_scaled_size+j)+0],
img_out[3*(i*x_scaled_size+j)+1],
img_out[3*(i*x_scaled_size+j)+2]);
} else {
fprintf(f, "%c%c%c",
img_out[3*(i*x_scaled_size+j)+0],
img_out[3*(i*x_scaled_size+j)+1],
img_out[3*(i*x_scaled_size+j)+2]);
fprintf(f, "%c%c%c",
img_out[3*(i*x_scaled_size+j)+0] >> 8,
img_out[3*(i*x_scaled_size+j)+1] >> 8,
img_out[3*(i*x_scaled_size+j)+2] >> 8);
}
}
}
}
fclose(f);
// fclose(f);
}
/* write_pfm_file:
......@@ -648,7 +704,7 @@ void write_pfm_file(FILE *f, float *img_out, char *img_out_fname,
}
}
}
fclose(f);
// fclose(f);
}
/* ReadFloat:
......
......@@ -56,19 +56,19 @@ void read_ppm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_colors,
int *is_ascii);
void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type,
int *endianess);
void read_pbm_data(FILE *f, int *img_in, int is_ascii);
void read_pgm_data(FILE *f, int *img_in, int is_ascii);
void read_ppm_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);
void read_pgm_data(FILE *f, int *img_in, int img_size, int is_ascii, unsigned int bytes_per_sample);
void read_ppm_data(FILE *f, int *img_in, int img_size, int is_ascii, unsigned int bytes_per_sample);
void read_pfm_data(FILE *f, float *img_in, int img_type, int endianess);
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 is_ascii);
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 img_colors, int linevals, int is_ascii);
void write_ppm_file(FILE *f, int *img_out, char *img_out_fname,
int img_colors, int linevals, int is_ascii, unsigned int bytes_per_sample);
void write_ppm_file(FILE *f, int *img_out,
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);
void write_pfm_file(FILE *f, float *img_out, char *img_out_fname,
int x_size, int y_size, int img_type, int endianess);
......
......@@ -16,21 +16,23 @@ from bob.io.base import load, write, test_utils
PNG_INDEXED_COLOR = test_utils.datafile('img_indexed_color.png', __name__)
PNG_RGBA_COLOR = test_utils.datafile('img_rgba_color.png', __name__)
def test_png_indexed_color():
# Read an indexed color PNG image, and compared with hardcoded values
img = load(PNG_INDEXED_COLOR)
assert img.shape == (3,22,32)
assert img[0,0,0] == 255
assert img[0,17,17] == 117
assert img.shape == (3, 22, 32)
assert img[0, 0, 0] == 255
assert img[0, 17, 17] == 117
def test_png_rgba_color():
# Read an indexed color PNG image, and compared with hardcoded values
img = load(PNG_RGBA_COLOR)
assert img.shape == (3,22,32)
assert img[0,0,0] == 255
assert img[0,17,17] == 117
assert img.shape == (3, 22, 32)
assert img[0, 0, 0] == 255
assert img[0, 17, 17] == 117
def transcode(filename):
......@@ -50,11 +52,16 @@ def transcode(filename):
assert numpy.array_equal(image, image2)
finally:
if os.path.exists(tmpname): os.unlink(tmpname)
if os.path.exists(tmpname):
os.unlink(tmpname)
def test_netpbm():
transcode(test_utils.datafile('test.pgm', __name__)) #indexed, works fine
transcode(test_utils.datafile('test.pbm', __name__)) #indexed, works fine
transcode(test_utils.datafile('test.ppm', __name__)) #indexed, works fine
#transcode(test_utils.datafile('test.jpg', __name__)) #does not work because of re-compression
transcode(test_utils.datafile('test.pbm', __name__)) # indexed, works fine
transcode(test_utils.datafile('test.pgm', __name__)) # indexed, works fine
transcode(test_utils.datafile('test.ppm', __name__)) # indexed, works fine
transcode(test_utils.datafile('test_2.pgm', __name__)) # indexed, works fine
transcode(test_utils.datafile('test_2.ppm', __name__)) # indexed, works fine
# transcode(test_utils.datafile('test.jpg', __name__)) #does not work
# because of re-compression
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment