Debian Codegraph @

editor/specialty/ppmtv.c

raw
1 /*********************************************************************/
2 /* ppmtv - make a 'look-alike ntsc' picture from a PPM file */
3 /* Frank Neumann, October 1993 */
4 /* V1.1 16.11.1993 */
5 /* */
6 /* version history: */
7 /* V1.0 12.10.1993 first version */
8 /* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */
9 /*********************************************************************/
10
11 #include "ppm.h"
12
13 /**************************/
14 /* start of main function */
15 /**************************/
16 int
17 main(int argc, char * argv[]) {
18
19 FILE* ifp;
20 int argn, rows, cols, format, i = 0, j = 0;
21 pixel *srcrow, *destrow;
22 pixel *pP = NULL, *pP2 = NULL;
23 pixval maxval;
24 double dimfactor;
25 long int longfactor;
26 const char * const usage = "dimfactor [ppmfile]\n dimfactor: 0.0 = total blackness, 1.0 = original picture\n";
27
28 /* parse in 'default' parameters */
29 ppm_init(&argc, argv);
30
31 argn = 1;
32
33 /* parse in dim factor */
34 if (argn == argc)
35 pm_usage(usage);
36 if (sscanf(argv[argn], "%lf", &dimfactor) != 1)
37 pm_usage(usage);
38 if (dimfactor < 0.0 || dimfactor > 1.0)
39 pm_error("dim factor must be in the range from 0.0 to 1.0 ");
40 ++argn;
41
42 /* parse in filename (if present, stdin otherwise) */
43 if (argn != argc) {
44 ifp = pm_openr(argv[argn]);
45 ++argn;
46 }
47 else
48 ifp = stdin;
49
50 if (argn != argc)
51 pm_usage(usage);
52
53 /* read first data from file */
54 ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
55
56 /* no error checking required here, ppmlib does it all for us */
57 srcrow = ppm_allocrow(cols);
58
59 longfactor = (long int) (dimfactor * 65536);
60
61 /* allocate a row of pixel data for the new pixels */
62 destrow = ppm_allocrow(cols);
63
64 ppm_writeppminit(stdout, cols, rows, maxval, 0);
65
66 /** now do the ntsc'ing (actually very similar to ppmdim) **/
67 for (i = 0; i < rows; i++) {
68 ppm_readppmrow(ifp, srcrow, cols, maxval, format);
69
70 pP = srcrow;
71 pP2 = destrow;
72
73 for (j = 0; j < cols; j++) {
74 /* every alternating row is left in unchanged condition */
75 if (i & 1) {
76 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
77 }
78 /* and the other lines are dimmed to the specified factor */
79 else {
80 PPM_ASSIGN(*pP2, (PPM_GETR(*pP) * longfactor) >> 16,
81 (PPM_GETG(*pP) * longfactor) >> 16,
82 (PPM_GETB(*pP) * longfactor) >> 16);
83 }
84 pP++;
85 pP2++;
86 }
87
88 /* write out one line of graphic data */
89 ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
90 }
91
92 pm_close(ifp);
93 ppm_freerow(srcrow);
94 ppm_freerow(destrow);
95
96 exit(0);
97 }
98
99
100