about summary refs log tree commit diff
path: root/editor/pnmpaste.c
blob: 3fd9d521dd93b4e52c142f620df4244ce6d71913 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* pnmpaste.c - paste a rectangle into a portable anymap
**
** 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 <assert.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "shhopt.h"
#include "pnm.h"


enum boolOp {REPLACE, AND, OR, XOR /*, NAND, NOR, NXOR */ };

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * baseFilename;
    const char * insetFilename;
    int insertCol;  /* Negative means from right edge */
    int insertRow;  /* Negative means from bottom edge */
    enum boolOp operation;
};



static void
parseCommandLine(int argc, const char ** argv,
                 struct cmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry *option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;
    unsigned int replaceOpt, andOpt, orOpt, xorOpt;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "replace",     OPT_FLAG,    NULL,
            &replaceOpt,           0);
    OPTENT3(0,   "and",         OPT_FLAG,    NULL,
            &andOpt,               0);
    OPTENT3(0,   "or",          OPT_FLAG,    NULL,
            &orOpt,                0);
    OPTENT3(0,   "xor",         OPT_FLAG,    NULL,
            &xorOpt,               0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = TRUE;  /* We have parms that are negative numbers */

    optParseOptions3(&argc, (char **)argv, opt, sizeof opt, 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (replaceOpt + andOpt + orOpt + xorOpt > 1)
        pm_error("You may specify only one of -replace, -and, -or, and -xor");

    cmdlineP->operation =
        replaceOpt ? REPLACE :
        andOpt     ? AND     :
        orOpt      ? OR      :
        xorOpt     ? XOR     :
        replaceOpt;
        

    if (argc-1 >= 3) {
        cmdlineP->insetFilename = argv[1];
        cmdlineP->insertCol     = atoi(argv[2]);
        cmdlineP->insertRow     = atoi(argv[3]);

        if (argc-1 >= 4) {
            cmdlineP->baseFilename = argv[4];

            if (argc-1 > 4)
                pm_error("Too many arguments: %u.  This program takes "
                         "at most 4", argc-1);
        } else
            cmdlineP->baseFilename = "-";
    } else
        pm_error("You must specify at least 3 arguments: \"from\" file "
                 "name, insert-at column, and insert-at row.  "
                 "You specified %u", argc-1);

    if (streq(cmdlineP->baseFilename, "-") &&
        streq(cmdlineP->insetFilename, "-"))
        pm_error("You can't use Standard Input for both the input images");
}



static unsigned char
leftBits(unsigned char const x,
         unsigned int  const n){
/*----------------------------------------------------------------------------
  Clear rightmost (8-n) bits, retain leftmost (=high) n bits.
-----------------------------------------------------------------------------*/
    assert(n <= 8);

    return (x >> (8-n)) << (8-n);
}



static unsigned char
rightBits(unsigned char const x,
          unsigned int  const n){
/*----------------------------------------------------------------------------
  Return rightmost (=low) n bits of x. (Clear the rest).
-----------------------------------------------------------------------------*/
    assert(n <= 8);

    return (x << (8-n)) >> (8-n);
}



static void
insertDirect(FILE *          const ifP,
             unsigned char * const destrow,
             unsigned int    const cols,
             int             const format,
             enum boolOp     const operation,
             unsigned char * const buffer) {
/*----------------------------------------------------------------------------
   Read the next row from PBM file 'ifP' and merge it according to
   'operation' into 'destrow', flush left in packed PBM format.

   'cols' and 'format' describe the 'ifP' image.

   'buffer' is a scratch buffer for our use, at least wide enough to hold
   a packed PBM row of 'ifP'.
-----------------------------------------------------------------------------*/
    unsigned int  const colBytes  = pbm_packed_bytes(cols);
    unsigned int  const last      = colBytes - 1;
    unsigned char const origRight = destrow[last];

    if (operation == REPLACE)
        pbm_readpbmrow_packed(ifP, destrow, cols, format);
    else {
        unsigned int i;

        pbm_readpbmrow_packed(ifP, buffer, cols, format);

        for (i = 0; i < colBytes; ++i) {
            switch (operation) {
            case AND: destrow[i] |= buffer[i]; break;
            case OR : destrow[i] &= buffer[i]; break;
            case XOR: destrow[i]  = ~( destrow[i] ^ buffer[i] ) ; break;
            /*
            case NAND: destrow[i] = ~( destrow[i] | buffer[i] ) ; break;
            case NOR : destrow[i] = ~( destrow[i] & buffer[i] ) ; break;
            case NXOR: destrow[i] ^= buffer[i]  ; break;
            */
            case REPLACE: assert(false); break;
            }
        }
    }

    if (cols % 8 > 0)
        destrow[last] = leftBits(destrow[last], cols % 8)
            | rightBits(origRight, 8 - cols % 8);
}



static void
insertShift(FILE *          const ifP,
            unsigned char * const destrow,
            unsigned int    const cols,
            unsigned int    const format,
            unsigned int    const offset,
            enum boolOp     const operation,
            unsigned char * const buffer) {
/*----------------------------------------------------------------------------
   Same as insertDirect(), but start merging 'offset' bits from the left
   end of 'destrow'.  'offset' is less than 8.

   buffer[] is wide enough to hold a packed PBM row of *ifP plus one
   byte of margin.
-----------------------------------------------------------------------------*/
    unsigned int const shiftBytes = pbm_packed_bytes(cols + offset);
    unsigned int const last = shiftBytes - 1;
    unsigned char const origLeft  = destrow[0];
    unsigned char const origRight = destrow[last];

    unsigned int const padOffset = (cols + offset) % 8;

    unsigned int i;

    assert(offset < 8);

    pbm_readpbmrow_packed(ifP, &buffer[1], cols, format);

    /* Note that buffer[0] is undefined. */

    for (i = 0; i < shiftBytes; ++i) {
        unsigned int  const rsh = offset;
        unsigned int  const lsh = 8-rsh;
        unsigned char const t = buffer[i] << lsh | buffer[i+1] >> rsh;

        switch (operation) {
        case REPLACE: destrow[i] = t; break;
        case AND:     destrow[i] |= t; break;
        case OR :     destrow[i] &= t; break;
        case XOR:     destrow[i] = ~ (destrow[i] ^ t); break;
        /*
        case NAND:    destrow[i] = ~ (destrow[i] | t); break;
        case NOR :    destrow[i] = ~ (destrow[i] & t); break;
        case NXOR:    destrow[i] ^= t; break;
        */
        }
    }

    /* destrow[] now contains garbage in the 'offset' leftmost bits
       and 8-offset rightmost bits.  Those are supposed to be unchanged
       from the input, so we restore them now.
    */

    destrow[0] = leftBits(origLeft, offset) |
        rightBits(destrow[0], 8-offset);
   
    if (padOffset % 8 > 0)
        destrow[last] = leftBits(destrow[last], padOffset) |
            rightBits(origRight , 8-padOffset);
}



static void
pastePbm(FILE *       const fpInset,
         FILE *       const fpBase,
         int          const insetFormat,
         int          const baseFormat,
         unsigned int const insetRows,
         unsigned int const baseRows,
         unsigned int const insetCols,
         unsigned int const baseCols,
         unsigned int const insertCol,
         unsigned int const insertRow,
         enum boolOp  const operation) {
/*----------------------------------------------------------------------------
  Fast paste for PBM
-----------------------------------------------------------------------------*/
    unsigned char * const baserow = pbm_allocrow_packed(baseCols);
    unsigned char * const buffer = pbm_allocrow_packed(insetCols+8);
    int const shiftBytes = insertCol / 8;
    unsigned int const shiftOffset = insertCol % 8;
    int const baseColBytes = pbm_packed_bytes(baseCols);

    unsigned int row;

    pbm_writepbminit(stdout, baseCols, baseRows, 0);

    for (row = 0; row < baseRows; ++row) {
        pbm_readpbmrow_packed(fpBase, baserow, baseCols, baseFormat);
        
        if (row >= insertRow && row < insertRow + insetRows) {
            if (shiftOffset == 0)
                insertDirect(fpInset, &baserow[shiftBytes], insetCols,
                             insetFormat, operation, buffer);
            else
                insertShift(fpInset, &baserow[shiftBytes], insetCols,
                            insetFormat, shiftOffset, operation, buffer);
        }

        if (baseCols % 8 > 0)
            baserow[baseColBytes-1]
                = leftBits(baserow[baseColBytes-1] , baseCols % 8);

        pbm_writepbmrow_packed(stdout, baserow, baseCols, 0);
    }
    pbm_freerow_packed(buffer);
    pbm_freerow_packed(baserow);
}



static void
pasteNonPbm(FILE *       const fpInset,
            FILE *       const fpBase,
            int          const formatInset,
            int          const formatBase,
            int          const newformat,
            xelval       const maxvalInset,
            xelval       const maxvalBase,
            unsigned int const rowsInset,
            unsigned int const rowsBase,
            unsigned int const colsInset,
            unsigned int const colsBase,
            unsigned int const insertCol,
            unsigned int const insertRow) {

    /* Logic works for PBM, but cannot do bitwise operations */             

    xelval const newmaxval = MAX(maxvalInset, maxvalBase);

    xel * const xelrowInset = pnm_allocrow(colsInset);
    xel * const xelrowBase  = pnm_allocrow(colsBase);

    unsigned int row;

    pnm_writepnminit(stdout, colsBase, rowsBase, newmaxval, newformat, 0);

    for (row = 0; row < rowsBase; ++row) {
        pnm_readpnmrow(fpBase, xelrowBase, colsBase, maxvalBase, formatBase);
        pnm_promoteformatrow(xelrowBase, colsBase, maxvalBase, formatBase,
                             newmaxval, newformat);

        if (row >= insertRow && row < insertRow + rowsInset) {
            unsigned int colInset;

            pnm_readpnmrow(fpInset, xelrowInset, colsInset, maxvalInset,
                           formatInset);
            pnm_promoteformatrow(xelrowInset, colsInset, maxvalInset,
                                 formatInset, newmaxval, newformat );
            for (colInset = 0; colInset < colsInset; ++colInset)
                xelrowBase[insertCol + colInset] = xelrowInset[colInset];
        }
        pnm_writepnmrow(stdout, xelrowBase, colsBase, newmaxval, newformat, 0);
    }
    
    pnm_freerow(xelrowBase);
    pnm_freerow(xelrowInset);
}



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

    struct cmdlineInfo cmdline;
    FILE * fpInset;
    FILE * fpBase;
    xelval maxvalInset, maxvalBase;
    int rowsInset, colsInset;
    int formatInset;
    int rowsBase, colsBase;
    int formatBase;
    int newformat;
    unsigned int insertRow, insertCol;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    fpInset = pm_openr(cmdline.insetFilename);
    fpBase  = pm_openr(cmdline.baseFilename);

    pnm_readpnminit(fpInset, &colsInset, &rowsInset,
                    &maxvalInset, &formatInset);
    pnm_readpnminit(fpBase, &colsBase, &rowsBase, &maxvalBase, &formatBase);

    if (colsBase < colsInset)
        pm_error(
            "Image to paste is wider than base image by %u cols",
            colsInset - colsBase);
    else if (cmdline.insertCol <= -colsBase)
        pm_error(
            "x is too negative -- the second image has only %u cols",
            colsBase);
    else if (cmdline.insertCol >= colsBase)
        pm_error(
            "x is too large -- the second image has only %u cols",
            colsBase);

    if (rowsBase < rowsInset)
        pm_error(
            "Image to paste is taller than base image by %u rows",
            rowsInset - rowsBase);
    else if (cmdline.insertRow <= -rowsBase)
        pm_error(
            "y is too negative -- the second image has only %u rows",
            rowsBase);
    else if (cmdline.insertRow >= rowsBase)
        pm_error(
            "y is too large -- the second image has only %d rows",
            rowsBase);

    insertCol = cmdline.insertCol < 0 ?
        colsBase + cmdline.insertCol : cmdline.insertCol;
    insertRow = cmdline.insertRow < 0 ?
        rowsBase + cmdline.insertRow : cmdline.insertRow;

    if (insertCol + colsInset > colsBase)
        pm_error("Extends over right edge by %u pixels",
                 (insertCol + colsInset) - colsBase);
    if (insertRow + rowsInset > rowsBase)
        pm_error("Extends over bottom edge by %u pixels",
                 (insertRow + rowsInset) - rowsBase);

    newformat = MAX(PNM_FORMAT_TYPE(formatInset), PNM_FORMAT_TYPE(formatBase));

    if (cmdline.operation != REPLACE && newformat != PBM_TYPE)
        pm_error("no logical operations allowed for a non-PBM image");

    if (newformat == PBM_TYPE)
        pastePbm(fpInset, fpBase, formatInset, formatBase,
                 rowsInset, rowsBase, colsInset, colsBase,
                 insertCol, insertRow, cmdline.operation);
    else
        pasteNonPbm(fpInset, fpBase,
                    formatInset, formatBase, newformat,
                    maxvalInset, maxvalBase,
                    rowsInset, rowsBase, colsInset, colsBase,
                    insertCol, insertRow);

    pm_close(fpInset);
    pm_close(fpBase);
    pm_close(stdout);

    return 0;
}