[brlcad-commits] SF.net SVN: brlcad:[57682] brlcad/trunk/src/libicv/ppm.c
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <br...@us...> - 2013-09-17 04:15:46
|
Revision: 57682 http://sourceforge.net/p/brlcad/code/57682 Author: brlcad Date: 2013-09-17 04:15:44 +0000 (Tue, 17 Sep 2013) Log Message: ----------- handle ppm files a little more gracefully, supporting mac, unix, windows line ending headers just in case. since we don't do anything with them, just add a function that skips to the next line. Modified Paths: -------------- brlcad/trunk/src/libicv/ppm.c Modified: brlcad/trunk/src/libicv/ppm.c =================================================================== --- brlcad/trunk/src/libicv/ppm.c 2013-09-17 04:03:05 UTC (rev 57681) +++ brlcad/trunk/src/libicv/ppm.c 2013-09-17 04:15:44 UTC (rev 57682) @@ -84,6 +84,28 @@ return 0; } + +HIDDEN void +ppm_nextline(FILE *fp) +{ + int c; + + /* skip to the binary data section, starting on next line. + * supports mac, unix, windows line endings. + */ + do { + c = fgetc(fp); + if (c == '\r') { + c = fgetc(fp); + if (c != '\n') { + ungetc(c, fp); + c = '\n'; /* pretend we're not an old mac file */ + } + } + } while (c != '\n'); +} + + icv_image_t* ppm_read(const char *filename) { @@ -116,11 +138,11 @@ BU_ALLOC(bif, struct icv_image); ICV_IMAGE_INIT(bif); - /* check for comments in PPM image*/ + /* check for comments lines in PPM image header */ c = getc(fp); while (c == '#') { - while (getc(fp) != '\n') - /* do nothing */; + /* encountered comment, skip to next line */ + ppm_nextline(fp); c = getc(fp); } @@ -147,8 +169,10 @@ return NULL; } - while (fgetc(fp) != '\n') - /* do nothing */; + /* skip to the binary data section, starting on next line. + * supports mac, unix, windows line endings. + */ + ppm_nextline(fp); /* memory allocation for pixel data */ data = (unsigned char*) bu_malloc(bif->width * bif->height * 3, "image data"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |