MCPcopy Create free account
hub / github.com/HiLab-git/SimpleCRF / readPPM

Function readPPM

dependency/densecrf/examples/ppm.cpp:45–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43 fclose ( fp );
44}
45unsigned char* readPPM ( const char* filename, int& W, int& H )
46{
47 FILE* fp = fopen ( filename, "rb" );
48 if ( !fp )
49 {
50 printf ( "Failed to open file '%s'!\n", filename );
51 }
52 char hdr[256]={};
53 int l=0;
54 // Read the header
55 char p,n;
56 int D;
57 while ( sscanf ( hdr, "%c%c %d %d %d", &p, &n, &W, &H, &D ) < 5 )
58 {
59 fgets ( hdr+l, 256-l, fp );
60 char * comment = strchr ( hdr, 'p' );
61 if ( comment ) l = hdr - comment;
62 else l = strlen ( hdr );
63 if ( l>=255 )
64 {
65 W=H=0;
66 fclose ( fp );
67 return NULL;
68 }
69 }
70 if ( p != 'P' )
71 {
72 W=H=0;
73 fclose ( fp );
74 return NULL;
75 }
76 unsigned char * r = new unsigned char[W*H*3];
77 if ( n=='6' )
78 fread ( r, 1, W*H*3, fp );
79 else if ( n=='3' )
80 {
81 int c;
82 for ( int i=0; i<W*H*3; i++ )
83 {
84 fscanf ( fp, "%d", &c );
85 r[i] = 255*c / D;
86 }
87 }
88 else
89 {
90 W=H=0;
91 fclose ( fp );
92 return NULL;
93 }
94 fclose ( fp );
95 return r;
96}
97
98void writePPM ( const char* filename, int W, int H, unsigned char* data )
99{

Callers 2

mainFunction · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected