2 * Copyright (c) 2014, Mathieu Malaterre <mathieu.malaterre@voxxl.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
28 * Technically on UNIX, one can simply call `ppmtorgb3`, but on my system it
29 * did not work. So I had to write my own.
32 #include <stdio.h> /* fprintf */
33 #include <string.h> /* strcmp */
34 #include <stdlib.h> /* malloc */
36 static const char magic[] = "P6";
38 static int readheader( FILE *ppm, int *X, int *Y, int *bpp )
47 line = fgets(buffer, sizeof(buffer), ppm);
49 n = sscanf(buffer, "%255[^\r\n]", strbuffer);
50 if( n != 1 ) return 0;
51 if( strcmp(strbuffer, magic ) != 0 ) return 0;
54 while( fgets(buffer, sizeof(buffer), ppm) && *buffer == '#' )
57 n = sscanf(buffer, "%d %d", X,Y);
58 if( n != 2 ) return 0;
59 line = fgets(buffer, sizeof(buffer), ppm);
61 n = sscanf(buffer, "%d", bpp);
62 if( n != 1 ) return 0;
63 if( *bpp != 255 ) return 0;
68 static int writeoutput( const char *fn, FILE *ppm, int X, int Y, int bpp )
70 FILE *outf[] = {NULL, NULL, NULL};
73 static const char *exts[3] = {
78 char *image_line = NULL;
81 /* write single comp as PGM: P5 */
82 for( i = 0; i < 3; ++i )
85 #define snprintf _snprintf /* Visual Studio */
87 snprintf( outfn, sizeof(outfn), "%s.%s.pgm", fn, exts[i] );
88 outf[i] = fopen( outfn, "wb" );
89 if( !outf[i] ) goto cleanup;
91 fprintf( outf[i], "P5\n" );
92 fprintf( outf[i], "%d %d\n", X, Y );
93 fprintf( outf[i], "%d\n", bpp );
96 /* write pixel data */
97 image_line = (char*)malloc( (size_t)X * 3 * sizeof(char) );
98 if( !image_line ) goto cleanup;
99 while( fread(image_line, sizeof(char), (size_t)X * 3, ppm) == (size_t)X * 3 )
101 for( x = 0; x < X; ++x )
102 for( i = 0; i < 3; ++i )
103 if( fputc( image_line[3*x+i], outf[i] ) == EOF ) goto cleanup;
110 for( i = 0; i < 3; ++i )
111 if( outf[i] ) fclose( outf[i] );
116 int main(int argc, char *argv[])
125 fprintf( stderr, "%s input.ppm\n", argv[0] );
129 ppm = fopen( fn, "rb" );
131 if( !readheader( ppm, &X, &Y, &bpp ) ) goto cleanup;
133 if( !writeoutput(fn, ppm, X, Y, bpp ) ) goto cleanup;
138 return ok ? EXIT_SUCCESS : EXIT_FAILURE;