about summary refs log tree commit diff
path: root/other/pamx/pamx.c
blob: 35900f8227109e4b8d79ddeb1ccd7418b89a07ef (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* By Bryan Henderson 2006.03.25 

   Copyright information is in the file COPYRIGHT
*/

#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
#define _BSD_SOURCE  /* Make sure strdup() is in <string.h> */
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "pm_c_util.h"
#include "pam.h"
#include "shhopt.h"
#include "mallocvar.h"
#include "nstring.h"
#include "filename.h"

#include "ximageinfo.h"
#include "image.h"
#include "fill.h"
#include "window.h"


struct geometry {
    bool specified;
    const char * string;
    unsigned int width;
    unsigned int height;
};

struct cmdlineInfo {
    const char * inputFileName;
    const char * displayName;  /* NULL if none */
    const char * display;
    const char * title;
    unsigned int fullscreen;
    unsigned int verbose;
    struct geometry geometry;
    const char * foreground;
    const char * background;
    const char * border;
    unsigned int install;
    unsigned int private;
    unsigned int pixmap;
    unsigned int fit;
    unsigned int visualSpec;
    unsigned int visual;
};



static void 
parseCommandLine(int argc, 
                 char ** argv, 
                 struct cmdlineInfo  * const cmdlineP) {
/* --------------------------------------------------------------------------
   Parse program command line described in Unix standard form by argc
   and argv.  Return the information in the options as *cmdlineP.  

   If command line is internally inconsistent (invalid options, etc.),
   issue error message to stderr and abort program.

   Note that the strings we return are stored in the storage that
   was passed to us as the argv array.  We also trash *argv.
--------------------------------------------------------------------------*/
    optEntry *option_def;
        /* Instructions to pm_optParseOptions3 on how to parse our options. */
    optStruct3 opt;
  
    unsigned int option_def_index;

    unsigned int displaySpec, titleSpec, foregroundSpec, backgroundSpec,
        borderSpec, geometrySpec;

    const char * geometryOpt;
    const char * visualOpt;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0, "fullscreen", OPT_FLAG,
            NULL, &cmdlineP->fullscreen,     0);
    OPTENT3(0, "install",    OPT_FLAG,
            NULL, &cmdlineP->install,     0);
    OPTENT3(0, "private",    OPT_FLAG,
            NULL, &cmdlineP->private,     0);
    OPTENT3(0, "fit",        OPT_FLAG,
            NULL, &cmdlineP->fit,     0);
    OPTENT3(0, "pixmap",     OPT_FLAG,
            NULL, &cmdlineP->pixmap,     0);
    OPTENT3(0, "verbose",    OPT_FLAG,
            NULL, &cmdlineP->verbose,     0);
    OPTENT3(0, "display",    OPT_STRING,
            &cmdlineP->display, &displaySpec, 0);
    OPTENT3(0, "title",      OPT_STRING,
            &cmdlineP->title, &titleSpec,     0);
    OPTENT3(0, "foreground", OPT_STRING,
            &cmdlineP->foreground, &foregroundSpec,     0);
    OPTENT3(0, "background", OPT_STRING,
            &cmdlineP->background, &backgroundSpec,     0);
    OPTENT3(0, "border",     OPT_STRING,
            &cmdlineP->border, &borderSpec,     0);
    OPTENT3(0, "geometry",   OPT_STRING,
            &geometryOpt, &geometrySpec,     0);
    OPTENT3(0, "visual",     OPT_STRING,
            &visualOpt, &cmdlineP->visualSpec,     0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;   /* We have no parms that are negative numbers */
    
    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (geometrySpec) {
        int rc;

        cmdlineP->geometry.specified = true;
        cmdlineP->geometry.string    = geometryOpt;

        rc = sscanf(geometryOpt, "%ux%u",
                    &cmdlineP->geometry.width, &cmdlineP->geometry.height);

        if (rc != 2)
            pm_error("Geometry value '%s' does not have the form WxH, "
                     "where W and H are natural numbers", geometryOpt);

        if (cmdlineP->geometry.width == 0)
            pm_error("Width in -geometry option is zero");
        if (cmdlineP->geometry.height == 0)
            pm_error("Height in -geometry option is zero");
    } else
        cmdlineP->geometry.specified = false;

    if (!displaySpec)
        cmdlineP->display = NULL;

    if (!titleSpec)
        cmdlineP->title = NULL;

    if (!foregroundSpec)
        cmdlineP->foreground = NULL;

    if (!backgroundSpec)
        cmdlineP->background = NULL;

    if (!borderSpec)
        cmdlineP->border = NULL;

    if (cmdlineP->visualSpec)
        cmdlineP->visual = visualClassFromName(visualOpt);

    if (argc-1 < 1)
        cmdlineP->inputFileName = "-";
    else if (argc-1 == 1)
        cmdlineP->inputFileName = argv[1];
    else
        pm_error("Program takes at most one argument:  input file name");
}



static int
errorHandler(Display *     const disp,
             XErrorEvent * const error) {
/*----------------------------------------------------------------------------
   This is an X error handler
-----------------------------------------------------------------------------*/
    char errortext[BUFSIZ];

    XGetErrorText(disp, error->error_code, errortext, sizeof(errortext));
    pm_error("X Error: %s on resource 0x%x",
             errortext, (unsigned)error->resourceid);
    return 0;
}



enum usableDepth {DEPTH_1, DEPTH_3};

static void
fillRow(struct pam *     const pamP,
        tuple *          const tuplerow,
        enum usableDepth const depth,
        unsigned char ** const pP) {
/*----------------------------------------------------------------------------
   Add one row to the 24-bit truecolor image data at *pP, and advance
   *pP just past that row.

   Use either the first plane or the first 3 planes of tuplerow[]
   for its contents, according to 'depth'.
-----------------------------------------------------------------------------*/
    unsigned int col;
    
    for (col = 0; col < pamP->width; ++col) {
        /* Truecolor image data has 3 bytes per pixel, one each for
           red, green, and blue.
        */
        unsigned int plane;
        for (plane = 0; plane < 3; ++plane) {
            unsigned int const tuplePlane = depth == DEPTH_3 ? plane : 0;
            *(*pP)++ =
                pnm_scalesample(tuplerow[col][tuplePlane], pamP->maxval, 255);
        }
    }
}



static void
loadPamImage(FILE *   const ifP,
             Image ** const imagePP) {

    struct pam pam;
    Image * imageP;
    unsigned int row;
    tuple * tuplerow;
    unsigned char * p;
    enum usableDepth depth;

    pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));

    if (strncmp(pam.tuple_type, "RGB", 3) == 0) {
        depth = DEPTH_3;
        if (pam.depth < 3)
            pm_error("Invalid depth %u for RGB tuple type.", pam.depth);
    } else
        depth = DEPTH_1;

    imageP = newTrueImage(pam.width, pam.height);

    p = &imageP->data[0];  /* initial value */

    tuplerow = pnm_allocpamrow(&pam);

    for (row = 0; row < pam.height; ++row) {
        pnm_readpamrow(&pam, tuplerow);
        
        /* This semantically wasteful code allows a dumb compiler
           optimizer to recognize that the depth is constant and
           therefore not generate code that checks the depth every
           time it processes a sample.
        */
        switch (depth) {
        case DEPTH_3:
            fillRow(&pam, tuplerow, DEPTH_3, &p);
            break;
        case DEPTH_1:
            fillRow(&pam, tuplerow, DEPTH_1, &p);
            break;
        }
    }
    pnm_freepamrow(tuplerow);

    *imagePP = imageP;
}


#define BACKGROUND_IDX 0
#define FOREGROUND_IDX 1


static void
processImage(Image *            const imageP,
             struct cmdlineInfo const cmdline,
             Display *          const dispP,
             int                const scrn) {
/*----------------------------------------------------------------------------
   Modify image *imageP according to various command line options.
-----------------------------------------------------------------------------*/
    if (imageP->depth <= 1) {
        if (cmdline.background) {
            XColor color;
            XParseColor(dispP, DefaultColormap(dispP, scrn),
                        cmdline.background, &color);
            imageP->rgb.red[BACKGROUND_IDX] = color.red;
            imageP->rgb.grn[BACKGROUND_IDX] = color.green;
            imageP->rgb.blu[BACKGROUND_IDX] = color.blue;
        }
        if (cmdline.foreground) {
            XColor color;
            XParseColor(dispP, DefaultColormap(dispP, scrn),
                        cmdline.foreground, &color);
            imageP->rgb.red[FOREGROUND_IDX] = color.red;
            imageP->rgb.grn[FOREGROUND_IDX] = color.green;
            imageP->rgb.blu[FOREGROUND_IDX] = color.blue;
        }
    }    
}



static void
determineTitle(struct cmdlineInfo const cmdline,
               const char **      const titleP) {
    
    const char * title;
    
    if (cmdline.title)
        title = strdup(cmdline.title);
    else {
        if (streq(cmdline.inputFileName, "-"))
            title = NULL;
        else {
            title = pm_basename(cmdline.inputFileName);
        }
    }
    *titleP = title;
}



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

    struct cmdlineInfo cmdline;
    FILE *       ifP;
    Image *      imageP;
    Display *    dispP;           /* display we're sending to */
    int          scrn;           /* screen we're sending to */
    int          retval;
    const char * geometryString;
    const char * title;
    viewer * viewerP;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);
    
    dispP = XOpenDisplay(cmdline.display);
    if (!dispP)
        pm_error("Cannot open display '%s'", XDisplayName(cmdline.display));

    scrn = DefaultScreen(dispP);
    XSetErrorHandler(errorHandler);

    geometryString =
        cmdline.geometry.specified ? cmdline.geometry.string : NULL;

    createViewer(&viewerP, dispP, scrn, geometryString, cmdline.fullscreen);

    loadPamImage(ifP, &imageP);

    processImage(imageP, cmdline, dispP, scrn);

    determineTitle(cmdline, &title);

    displayInViewer(viewerP, imageP,
                    cmdline.install, cmdline.private, cmdline.fit,
                    cmdline.pixmap, cmdline.visualSpec, cmdline.visual,
                    title, cmdline.verbose,
                    &retval);

    freeImage(imageP);

    destroyViewer(viewerP);

    if (title)
        pm_strfree(title);

    XCloseDisplay(dispP);

    return retval;
}