OpenCores
URL https://opencores.org/ocsvn/linkruncca/linkruncca/trunk

Subversion Repositories linkruncca

[/] [linkruncca/] [trunk/] [sim/] [ppm.h] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 jaytang
#ifndef ppm_h
2
#define ppm_h
3
 
4
int writePPM(char* filename,unsigned char* im,unsigned long width,unsigned long height,int colour)
5
{
6
        int i,j;
7
        FILE *output;
8
        output=fopen(filename,"w");
9
        if(output==NULL){printf("File not found!");return 1;}
10
        if(colour)
11
        {
12
                fprintf(output,"P6\n%lu %lu\n255\n",width,height);
13
                for(i=0;i<height*width*3;i++)
14
                        fputc(im[i],output);
15
        }
16
        else
17
        {
18
                fprintf(output,"P5\n%lu %lu\n255\n",width,height);
19
                for(i=0;i<height*width;i++)
20
                        fputc(im[i],output);
21
        }
22
        fclose(output);
23
        return 0;
24
}
25
 
26
int readPPM(char* filename,unsigned char** im,unsigned int *width,unsigned int *height) //support P5 and P6 file type
27
{
28
        unsigned int i,j,maxPixValue,colour;
29
        FILE *input;
30
        char buffer;
31
        input=fopen(filename,"r");if(input==NULL){printf("Image not found!\n");return 1;}
32
 
33
        if(buffer=fgetc(input)!='P')
34
                {printf("Wrong image format!\n");return 1;}
35
        colour=fgetc(input)-'0'-5;
36
 
37
        while(!isdigit(buffer=fgetc(input)))
38
                if(buffer=='#') //skip comment
39
                        while(fgetc(input)!='\n');
40
        *width=buffer-'0';
41
        while(isdigit(buffer=fgetc(input)))
42
                *width=*width*10+buffer-'0'; //width
43
 
44
        while(!isdigit(buffer=fgetc(input)))
45
                if(buffer=='#') //skip comment
46
                        while(fgetc(input)!='\n');
47
        *height=buffer-'0';
48
        while(isdigit(buffer=fgetc(input)))
49
                *height=*height*10+buffer-'0'; //height
50
 
51
        while(!isdigit(buffer=fgetc(input)))
52
                if(buffer=='#') //skip comment
53
                {
54
                        while(fgetc(input)!='\n');
55
                }
56
        maxPixValue=buffer-'0';
57
        while(isdigit(buffer=fgetc(input)))
58
                maxPixValue=maxPixValue*10+buffer-'0';
59
//      printf("%u\n",maxPixValue);
60
 
61
        //read image
62
        unsigned int w=*width;unsigned int h=*height;
63
        unsigned char* imread=(unsigned char*) malloc(w*h*3);if(imread==NULL){printf("malloc error\n");return 1;}
64
 
65
        if(colour)
66
                for(i=0;i<h*w*3;i++)imread[i]=fgetc(input);
67
        else
68
                for(i=0;i<h*w;i++)imread[i]=fgetc(input);
69
 
70
        fclose(input);
71
        *im=imread;
72
        return 0;
73
}
74
 
75
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.