-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgmlO.c
175 lines (167 loc) · 4.27 KB
/
pgmlO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* pgmlO.c
*
* Created on: Dec 20, 2011
* Author: jamie
*/
#include "pgmIO.h"
FILE *_INFP = NULL;
FILE *_OUTFP = NULL;
/////////////////////////////////////////////////////////////////////////////////////////////
//
// Line-wise pgm input routines: open file, read a line, close the file
//
/////////////////////////////////////////////////////////////////////////////////////////////
int _openinpgm(char fname[], int width, int height)
{
char str[ 64 ];
int inwidth, inheight;
_INFP = fopen( fname, "rb" );
if( _INFP == NULL )
{
printf( "Could not open %s.\n", fname );
return -1;
}
//Strip off header
fgets( str, 64, _INFP ); //Version: P5
fgets( str, 64, _INFP ); //width and height
sscanf( str, "%d%d", &inwidth, &inheight );
if( inwidth != width || inheight != height )
{
printf( "Input image size(%dx%d) does not = %dx%d or trouble reading header\n", inwidth,
inheight, width, height );
return -1;
}
fgets( str, 64, _INFP ); //bit depth, must be 255
return 0;
}
int _readinline(unsigned char line[], int width)
{
int nb;
if( _INFP == NULL )
{
return -1;
}
nb = fread( line, 1, width, _INFP );
if( nb != width )
{
//printf( "Error reading line, nb = %d\n", nb );
//Error or end of file
return -1;
}
return 0;
}
int _closeinpgm()
{
int ret = fclose( _INFP );
if( ret == 0 )
{
_INFP = NULL;
}
else
{
printf( "Error closing file _INFP.\n" );
}
return ret; //return zero for succes and EOF for fail
}
/////////////////////////////////////////////////////////////////////////////////////////////
//
// Line-wise pgm output routines: open file, read a line, close the file
//
/////////////////////////////////////////////////////////////////////////////////////////////
int _openoutpgm(char fname[], int width, int height)
{
char hdr[ 64 ];
_OUTFP = fopen( fname, "wb" );
if( _OUTFP == NULL )
{
printf( "Could not open %s.\n", fname );
return -1;
}
sprintf( hdr, "P5\n%d %d\n255\n", width, height );
fprintf( _OUTFP, "%s", hdr );
return 0;
}
int _writeoutline(unsigned char line[], int width)
{
int nb;
if( _OUTFP == NULL )
{
return -1;
}
nb = fwrite( line, 1, width, _OUTFP );
if( nb != width )
{
//printf( "Error writing line, nb = %d\n", nb );
//Error or end of file
return -1;
}
return 0;
}
int _closeoutpgm()
{
int ret = fclose( _OUTFP );
if( ret == 0 )
{
_OUTFP = NULL;
}
else
{
printf( "Error closing file _OUTFP.\n" );
}
return ret; //return zero for succes and EOF for fail
}
/////////////////////////////////////////////////////////////////////////////////////////////
//
// standard pgm input and output routines
//
// Input is a referenced array of unsigned chars of width and height and a
// referenced char array of the system path to destination, e.g.
// "/home/user/xmos/project/" on Linux or "C:\\user\\xmos\\project\\" on Windows
//
/////////////////////////////////////////////////////////////////////////////////////////////
int _writepgm(unsigned char x[], int height, int width, char fname[])
{
FILE *fp;
int hlen;
char hdr[ 64 ];
sprintf( hdr, "P5\n%d %d\n255\n", width, height );
hlen = strlen( hdr );
printf( "In writepgm function, writing: %s\n", fname );
fp = fopen( fname, "wb" );
if( fp == NULL)
{
printf( "Could not open %s for writing.\n", fname );
return -1;
}
fprintf( fp, "%s", hdr );
fwrite( x, width * height, 1, fp );
fclose( fp );
return 0;
}
int _readpgm(unsigned char x[], int height, int width, char fname[])
{
FILE *fp;
unsigned int inwidth, inheight;
char str[ 64 ];
printf( "In readpgm function, reading: %s\n", fname );
fp = fopen( fname, "rb" );
if( fp == NULL)
{
printf( "Could not open %s for reading.\n", fname );
return -1;
}
fgets( str, 64, fp ); //Version: P5
fgets( str, 64, fp ); //width and height
sscanf( str, "%d%d", &inwidth, &inheight );
fgets( str, 64, fp ); //bit depth, must be 255
if( inwidth != width || inheight != height )
{
printf( "Input image size(%dx%d) does not = %dx%d or trouble reading header\n", inwidth,
inheight, width, height );
return -1;
}
fread( x, inwidth * inheight, 1, fp );
fclose( fp );
return 0;
}