color_apply_icc_profile: add checks on the number of components (#1236)
[openjpeg.git] / tests / ppm2rgb3.c
1 /*
2  * Copyright (c) 2014, Mathieu Malaterre <mathieu.malaterre@voxxl.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 /*
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.
30  */
31
32 #include <stdio.h>  /* fprintf */
33 #include <string.h> /* strcmp */
34 #include <stdlib.h> /* malloc */
35
36 static const char magic[] = "P6";
37
38 static int readheader( FILE *ppm, int *X, int *Y, int *bpp )
39 {
40   char buffer[256];
41   char strbuffer[256];
42   char *line;
43   int n;
44
45   *X = *Y = *bpp = 0;
46
47   line = fgets(buffer, sizeof(buffer), ppm);
48   if( !line ) return 0;
49   n = sscanf(buffer, "%255[^\r\n]", strbuffer);
50   if( n != 1 ) return 0;
51   if( strcmp(strbuffer, magic ) != 0 ) return 0;
52
53   /* skip comments */
54   while( fgets(buffer, sizeof(buffer), ppm) && *buffer == '#' )
55     {
56     }
57   n = sscanf(buffer, "%d %d", X,Y);
58   if( n != 2 ) return 0;
59   line = fgets(buffer, sizeof(buffer), ppm);
60   if( !line ) return 0;
61   n = sscanf(buffer, "%d", bpp);
62   if( n != 1 ) return 0;
63   if( *bpp != 255 ) return 0;
64
65   return 1;
66 }
67
68 static int writeoutput( const char *fn, FILE *ppm, int X, int Y, int bpp )
69 {
70   FILE *outf[] = {NULL, NULL, NULL};
71   int i, x, y = 0;
72   char outfn[256];
73   static const char *exts[3] = {
74     "red",
75     "grn",
76     "blu"
77   };
78   char *image_line = NULL;
79   int ok = 0;
80
81   /* write single comp as PGM: P5 */
82   for( i = 0; i < 3; ++i )
83     {
84 #ifdef _MSC_VER
85 #define snprintf _snprintf /* Visual Studio */
86 #endif
87     snprintf( outfn, sizeof(outfn), "%s.%s.pgm", fn, exts[i] );
88     outf[i] = fopen( outfn, "wb" );
89     if( !outf[i] ) goto cleanup;
90     /* write header */
91     fprintf( outf[i], "P5\n" );
92     fprintf( outf[i], "%d %d\n", X, Y );
93     fprintf( outf[i], "%d\n", bpp );
94     }
95
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 )
100     {
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;
104     ++y;
105     }
106   if( y == Y )
107     ok = 1;
108 cleanup:
109   free(image_line);
110   for( i = 0; i < 3; ++i )
111     if( outf[i] ) fclose( outf[i] );
112
113   return ok;
114 }
115
116 int main(int argc, char *argv[])
117 {
118   const char *fn;
119   FILE *ppm = NULL;
120   int X, Y, bpp;
121   int ok = 0;
122
123   if( argc < 2 )
124     {
125     fprintf( stderr, "%s input.ppm\n", argv[0] );
126     goto cleanup;
127     }
128   fn = argv[1];
129   ppm = fopen( fn, "rb" );
130
131   if( !readheader( ppm, &X, &Y, &bpp ) ) goto cleanup;
132
133   if( !writeoutput(fn, ppm, X, Y, bpp ) ) goto cleanup;
134
135   ok = 1;
136 cleanup:
137   if(ppm) fclose(ppm);
138   return ok ? EXIT_SUCCESS : EXIT_FAILURE;
139 }