1 /* rgb3toppm - combine three portable graymaps into one portable pixmap
2 **
3 ** Copyright (C) 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include "ppm.h"
14
15 int
main(argc,argv)16 main( argc, argv )
17 int argc;
18 char* argv[];
19 {
20 FILE* rfd;
21 FILE* gfd;
22 FILE* bfd;
23 gray* rrow;
24 gray* rP;
25 gray* grow;
26 gray* gP;
27 gray* brow;
28 gray* bP;
29 pixel* pixelrow;
30 register pixel* pP;
31 int rows, cols, trows, tcols, row, col;
32 gray rmaxval, gmaxval, bmaxval;
33 int rformat, gformat, bformat;
34 pixval pmaxval;
35
36
37 ppm_init( &argc, argv );
38
39 if ( argc != 4 )
40 pm_usage( "<red pgmfile> <green pgmfile> <blue pgmfile> " );
41
42 rfd = pm_openr( argv[1] );
43 gfd = pm_openr( argv[2] );
44 bfd = pm_openr( argv[3] );
45
46 pgm_readpgminit( rfd, &cols, &rows, &rmaxval, &rformat );
47 pgm_readpgminit( gfd, &tcols, &trows, &gmaxval, &gformat );
48 if ( tcols != cols || trows != rows )
49 pm_error( "all three graymaps must be the same size" );
50 pgm_readpgminit( bfd, &tcols, &trows, &bmaxval, &bformat );
51 if ( tcols != cols || trows != rows )
52 pm_error( "all three graymaps must be the same size" );
53
54 pmaxval = rmaxval;
55 if ( gmaxval > pmaxval ) pmaxval = gmaxval;
56 if ( bmaxval > pmaxval ) pmaxval = bmaxval;
57 rrow = pgm_allocrow( cols );
58 grow = pgm_allocrow( cols );
59 brow = pgm_allocrow( cols );
60
61 ppm_writeppminit( stdout, cols, rows, pmaxval, 0 );
62 pixelrow = ppm_allocrow( cols );
63
64 for ( row = 0; row < rows; row++ )
65 {
66 pgm_readpgmrow( rfd, rrow, cols, rmaxval, rformat );
67 pgm_readpgmrow( gfd, grow, cols, gmaxval, gformat );
68 pgm_readpgmrow( bfd, brow, cols, bmaxval, bformat );
69
70 for ( col = 0, rP = rrow, gP = grow, bP = brow, pP = pixelrow;
71 col < cols;
72 ++col, ++rP, ++gP, ++bP, ++pP )
73 {
74 if ( rmaxval != pmaxval ) *rP = (int) *rP * pmaxval / rmaxval;
75 if ( gmaxval != pmaxval ) *gP = (int) *gP * pmaxval / gmaxval;
76 if ( bmaxval != pmaxval ) *bP = (int) *bP * pmaxval / bmaxval;
77 PPM_ASSIGN( *pP, *rP, *gP, *bP );
78 }
79 ppm_writeppmrow( stdout, pixelrow, cols, pmaxval, 0 );
80 }
81
82 pm_close( rfd );
83 pm_close( gfd );
84 pm_close( bfd );
85 pm_close( stdout );
86
87 exit( 0 );
88 }
89