about summary refs log tree commit diff
path: root/editor/ppm3d.c
blob: c37ceeb1a3b2b880586a33e18e87261ce668d7be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ppmto3d.c - convert a portable pixmap to a portable graymap
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include "ppm.h"
#include "lum.h"

static void
computeGrayscaleRow(const pixel * const inputRow,
                    gray *        const outputRow,
                    pixval        const maxval,
                    unsigned int  const cols) {

    if (maxval <= 255) {
        unsigned int col;
        /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b. */
        for (col = 0; col < cols; ++col)
            outputRow[col] = ppm_fastlumin(inputRow[col]);
    } else {
        unsigned int col;
        /* Can't use fast approximation, so fall back on floats. */
        for (col = 0; col < cols; ++col)
            outputRow[col] = PPM_LUMIN(inputRow[col]) + 0.5;
    }
}



int
main (int argc, char *argv[]) {

    int offset; 
    int cols, rows, row;
    pixel* pixelrow;
    pixval maxval;

    FILE* Lifp;
    pixel* Lpixelrow;
    gray* Lgrayrow;
    int Lrows, Lcols, Lformat;
    pixval Lmaxval;
   
    FILE* Rifp;
    pixel* Rpixelrow;
    gray* Rgrayrow;
    int Rrows, Rcols, Rformat;
    pixval Rmaxval;
   
    ppm_init (&argc, argv);

    if (argc-1 > 3 || argc-1 < 2) 
        pm_error("Wrong number of arguments (%d).  Arguments are "
                 "leftppmfile rightppmfile [horizontal_offset]", argc-1);

    Lifp = pm_openr (argv[1]);
    Rifp = pm_openr (argv[2]);

    if (argc-1 >= 3) 
        offset = atoi (argv[3]);
    else
        offset = 30;

    ppm_readppminit (Lifp, &Lcols, &Lrows, &Lmaxval, &Lformat);
    ppm_readppminit (Rifp, &Rcols, &Rrows, &Rmaxval, &Rformat);
    
    if ((Lcols != Rcols) || (Lrows != Rrows) || 
        (Lmaxval != Rmaxval) || 
        (PPM_FORMAT_TYPE(Lformat) != PPM_FORMAT_TYPE(Rformat)))
        pm_error ("Pictures are not of same size and format");
    
    cols = Lcols;
    rows = Lrows;
    maxval = Lmaxval;
   
    ppm_writeppminit (stdout, cols, rows, maxval, 0);
    Lpixelrow = ppm_allocrow (cols);
    Lgrayrow = pgm_allocrow (cols);
    Rpixelrow = ppm_allocrow (cols);
    Rgrayrow = pgm_allocrow (cols);
    pixelrow = ppm_allocrow (cols);

    for (row = 0; row < rows; ++row) {
        ppm_readppmrow(Lifp, Lpixelrow, cols, maxval, Lformat);
        ppm_readppmrow(Rifp, Rpixelrow, cols, maxval, Rformat);

        computeGrayscaleRow(Lpixelrow, Lgrayrow, maxval, cols);
        computeGrayscaleRow(Rpixelrow, Rgrayrow, maxval, cols);
        {
            int col;
            gray* LgP;
            gray* RgP;
            pixel* pP;
            for (col = 0, pP = pixelrow, LgP = Lgrayrow, RgP = Rgrayrow;
                 col < cols + offset;
                 ++col) {
            
                if (col < offset/2)
                    ++LgP;
                else if (col >= offset/2 && col < offset) {
                    const pixval Blue = (pixval) (float) *LgP;
                    const pixval Red = (pixval) 0;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++LgP;
                    ++pP;
                } else if (col >= offset && col < cols) {
                    const pixval Red = (pixval) (float) *RgP;
                    const pixval Blue = (pixval) (float) *LgP;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++LgP;
                    ++RgP;
                    ++pP;
                } else if (col >= cols && col < cols + offset/2) {
                    const pixval Blue = (pixval) 0;
                    const pixval Red = (pixval) (float) *RgP;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++RgP;
                    ++pP;
                } else
                    ++RgP;
            }
        }    
        ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0);
    }

    pm_close(Lifp);
    pm_close(Rifp);
    pm_close(stdout);

    return 0;
}