static char *skip_white(char *s)
{
- while(*s)
+ if (s != NULL)
{
- if(*s == '\n' || *s == '\r') return NULL;
- if(isspace(*s)) { ++s; continue; }
- return s;
+ while(*s)
+ {
+ if(*s == '\n' || *s == '\r') return NULL;
+ if(isspace(*s)) { ++s; continue; }
+ return s;
+ }
}
return NULL;
}
static void read_pnm_header(FILE *reader, struct pnm_header *ph)
{
- int format, have_wh, end, ttype;
+ int format, end, ttype;
char idf[256], type[256];
char line[256];
return;
}
ph->format = format;
- ttype = end = have_wh = 0;
+ ttype = end = 0;
while(fgets(line, 250, reader))
{
char *s;
+ int allow_null = 0;
if(*line == '#') continue;
return;
} /* if(format == 7) */
- if( !have_wh)
- {
+ /* Here format is in range [1,6] */
+ if (ph->width == 0) {
s = skip_int(s, &ph->width);
-
+ if ((s == NULL) || (*s == 0) || (ph->width < 1)) return;
+ allow_null = 1;
+ }
+ if (ph->height == 0) {
s = skip_int(s, &ph->height);
-
- have_wh = 1;
-
- if(format == 1 || format == 4) break;
-
- if(format == 2 || format == 3 || format == 5 || format == 6)
- {
- if (skip_int(s, &ph->maxval) != NULL) {
- if(ph->maxval > 65535) {
- return;
- }
- else {
- break;
- }
- }
+ if ((s == NULL) && allow_null) continue;
+ if ((s == NULL) || (*s == 0) || (ph->height < 1)) return;
+ if(format == 1 || format == 4) {
+ break;
}
- continue;
- }
- if(format == 2 || format == 3 || format == 5 || format == 6)
- {
- /* P2, P3, P5, P6: */
- s = skip_int(s, &ph->maxval);
-
- if(ph->maxval > 65535) return;
+ allow_null = 1;
}
+ /* here, format is in P2, P3, P5, P6 */
+ s = skip_int(s, &ph->maxval);
+ if ((s == NULL) && allow_null) continue;
+ if ((s == NULL) || (*s == 0)) return;
break;
}/* while(fgets( ) */
if(format == 2 || format == 3 || format > 4)
}
if(ph->depth < 1 || ph->depth > 4) return;
- if(ph->width && ph->height && ph->depth && ph->maxval && ttype)
+ if (ttype)
ph->ok = 1;
}
else
{
- if(format != 1 && format != 4)
- {
- if(ph->width && ph->height && ph->maxval) ph->ok = 1;
- }
- else
+ ph->ok = 1;
+ if(format == 1 || format == 4)
{
- if(ph->width && ph->height) ph->ok = 1;
ph->maxval = 255;
}
}