34 lines
729 B
C
34 lines
729 B
C
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct _rgb {
|
|
unsigned char r,g,b;
|
|
} rgb;
|
|
|
|
typedef
|
|
struct _ppmimage {
|
|
int sizex,sizey;
|
|
rgb *color;
|
|
} ppmimage;
|
|
|
|
ppmimage read_ppmimage ( const char *name )
|
|
{
|
|
ppmimage result;
|
|
int range;
|
|
FILE *fp = fopen(name,"rb");
|
|
if (!fp)
|
|
{
|
|
fprintf(stderr,"No file\n");
|
|
exit(1);
|
|
}
|
|
assert(getc(fp)=='P');
|
|
assert(getc(fp)=='6');
|
|
fscanf(fp,"%d%d%d\n",&result.sizex,&result.sizey,&range);
|
|
assert(range==255); // we'll use only rgb range 0...255 here
|
|
result.color = (rgb*)malloc(result.sizex*result.sizey*sizeof(rgb));
|
|
fread(result.color,sizeof(rgb),result.sizex*result.sizey,fp);
|
|
return result;
|
|
}
|