home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
BarlaA
/
sw
/
OLD
/
Simo
/
SVM_mono
/
pnmio.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-06-25
|
4KB
|
246 lines
#include <stdio.h>
#include <stdlib.h>
#include "image2d.h"
/*char *getnext(FILE * p);*/
/*int number(const char *s);*/
/* pnm header reader
* based upon John Burns ppm reader
*
* give newly oped file pointer returns relavent information
* through pointers to width, height, depth and type
*
* width is the number of columns in the image
* height is the number of rows in the image
* depth is the maximum value represented in the image (e.g 255 for 8
bit)
* and type is the character that differentiates the image type ('1' to
'6')
*/
static char *getnext(FILE * p)
{
static char buf[8192];
char *bp = buf;
int c;
buf[0] = 0;
start:
while (c = getc(p), c != EOF && isspace(c));
if (feof(p)) return NULL;
/* Was start of a string so get the first character back */
if (c == '#') {
while (c = getc(p), c != EOF && c != '\n');
if (feof(p)) return NULL;
goto start;
} else
ungetc(c, p);
bp = buf;
do {
c = getc(p);
if (c == EOF)
break;
if (isspace(c))
break;
*bp++ = c;
} while (bp < buf + sizeof(buf) - 1);
if (isspace(c)) ungetc(c, p);
*bp = '\0';
return buf;
}
static int number(const char *s)
{
while (*s)
{
if (!isdigit(*s)) return 0;
s++;
}
return 1;
}
int ReadHeaderPN(FILE *fp, int *width, int *height, int *depth, char *type)
{
char *h;
int c;
h = getnext(fp);
if (h[0]!='P') return 0;
*type = h[1];
h = getnext(fp);
if (!number(h)) return 0;
*width = atoi(h);
h = getnext(fp);
if (!number(h)) return 0;
*height = atoi(h);
h = getnext(fp);
if (!number(h)) return 0;
*depth = atoi(h);
while (c = getc(fp))
{
if (c != '\n' && c != EOF) continue;
if (feof(fp))
{
/*fprintf(stderr, "Bad PN file\n");*/
return 0;
}
break;
}
return 1;
}
static Image2D *ReadImageRaw(FILE *fp, int w, int h, int spp)
{
Image2D *im;
unsigned char **data;
int byteWidth = w*spp;
int i;
im = ImageAlloc(w, h);
if (im==NULL)
return NULL;
data = (unsigned char **)im->data;
for (i=0; i<h; ++i)
fread(data[i], sizeof(unsigned char), byteWidth, fp);
return im;
}
static Image2D *ReadImagePN(FILE *fp)
{
int w, h, depth;
char type;
if (!ReadHeaderPN(fp, &w, &h, &depth, &type))
return NULL;
switch (type)
{
case '5': return ReadImageRaw(fp, w, h, 1);
case '6': return ReadImageRaw(fp, w, h, 3);
default : return(NULL);
}
}
static int WriteImagePN(FILE *fp, Image2D *im)
{
unsigned char **data;
int byteWidth;
int i;
if (im==NULL)
return 0;
fprintf(fp, "P%s\n %d %d 255\n", (im->samplesPerPixel==3) ? "6" : "5",
im->width, im->height);
data = (unsigned char **)im->data;
byteWidth = im->width*im->samplesPerPixel;
for (i=0; i<im->height; ++i)
fwrite(data[i], sizeof(unsigned char), byteWidth, fp);
return 1;
}
static int read_pnm_line(FILE *fp, unsigned char *p, int *control)
{
static int w, h, depth;
static char type;
static int byteWidth;
if (*control == 0)
{
if (!ReadHeaderPN(fp, &w, &h, &depth, &type))
return 0;
switch (type)
{
case '5':
byteWidth = w;
break;
case '6':
byteWidth = 3*w;
break;
}
*control = 1;
}
fread(p, sizeof(unsigned char), byteWidth, fp);
return 1;
}
int read_size(char *filename, int *width, int *height)
{
FILE *fp;
int depth;
char type;
fp = fopen(filename, "r");
if (fp==NULL) return 0;
ReadHeaderPN(fp, width, height, &depth, &type);
return 1;
}
Image2D *open_pnm_file(char *filename)
{
FILE *fp;
Image2D *im;
if ((fp=fopen(filename, "rb"))==NULL)
{
return NULL;
}
im = ReadImagePN(fp);
fclose(fp);
strcpy(im->name,filename);
return im;
}
int write_file_pnm(char *name, Image2D *im)
{
FILE *fp;
int out;
if ((fp=fopen(name, "wb"))==NULL)
{
return 0;
}
out = WriteImagePN(fp, im);
fclose(fp);
return out;
}