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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
|
/* pnmcat.c - concatenate portable anymaps
**
** Copyright (C) 1989, 1991 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 "pnm.h"
#include "mallocvar.h"
#include "shhopt.h"
enum backcolor {BACK_WHITE, BACK_BLACK, BACK_AUTO};
enum orientation {TOPBOTTOM, LEFTRIGHT};
enum justification {JUST_CENTER, JUST_MIN, JUST_MAX};
struct imgInfo {
/* This obviously should be a struct pam. We should convert this
to 'pamcat'.
*/
FILE * ifP;
int cols;
int rows;
int format;
xelval maxval;
};
struct cmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
const char ** inputFilespec;
unsigned int nfiles;
enum backcolor backcolor;
enum orientation orientation;
enum justification justification;
};
static void
parseCommandLine(int argc, const char ** const 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 leftright, topbottom, black, white, jtop, jbottom,
jleft, jright, jcenter;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENTRY */
OPTENT3(0, "leftright", OPT_FLAG, NULL, &leftright, 0);
OPTENT3(0, "lr", OPT_FLAG, NULL, &leftright, 0);
OPTENT3(0, "topbottom", OPT_FLAG, NULL, &topbottom, 0);
OPTENT3(0, "tb", OPT_FLAG, NULL, &topbottom, 0);
OPTENT3(0, "black", OPT_FLAG, NULL, &black, 0);
OPTENT3(0, "white", OPT_FLAG, NULL, &white, 0);
OPTENT3(0, "jtop", OPT_FLAG, NULL, &jtop, 0);
OPTENT3(0, "jbottom", OPT_FLAG, NULL, &jbottom, 0);
OPTENT3(0, "jleft", OPT_FLAG, NULL, &jleft, 0);
OPTENT3(0, "jright", OPT_FLAG, NULL, &jright, 0);
OPTENT3(0, "jcenter", OPT_FLAG, NULL, &jcenter, 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 */
optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
if (leftright + topbottom > 1)
pm_error("You may specify only one of -topbottom (-tb) and "
"-leftright (-lr)");
else if (leftright)
cmdlineP->orientation = LEFTRIGHT;
else if (topbottom)
cmdlineP->orientation = TOPBOTTOM;
else
pm_error("You must specify either -leftright or -topbottom");
if (black + white > 1)
pm_error("You may specify only one of -black and -white");
else if (black)
cmdlineP->backcolor = BACK_BLACK;
else if (white)
cmdlineP->backcolor = BACK_WHITE;
else
cmdlineP->backcolor = BACK_AUTO;
if (jtop + jbottom + jleft + jright + jcenter > 1)
pm_error("You may specify onlyone of -jtop, -jbottom, "
"-jleft, and -jright");
else {
switch (cmdlineP->orientation) {
case LEFTRIGHT:
if (jleft)
pm_error("-jleft is invalid with -leftright");
if (jright)
pm_error("-jright is invalid with -leftright");
if (jtop)
cmdlineP->justification = JUST_MIN;
else if (jbottom)
cmdlineP->justification = JUST_MAX;
else if (jcenter)
cmdlineP->justification = JUST_CENTER;
else
cmdlineP->justification = JUST_CENTER;
break;
case TOPBOTTOM:
if (jtop)
pm_error("-jtop is invalid with -topbottom");
if (jbottom)
pm_error("-jbottom is invalid with -topbottom");
if (jleft)
cmdlineP->justification = JUST_MIN;
else if (jright)
cmdlineP->justification = JUST_MAX;
else if (jcenter)
cmdlineP->justification = JUST_CENTER;
else
cmdlineP->justification = JUST_CENTER;
break;
}
}
if (argc-1 < 1) {
MALLOCARRAY_NOFAIL(cmdlineP->inputFilespec, 1);
cmdlineP->inputFilespec[0] = "-";
cmdlineP->nfiles = 1;
} else {
unsigned int i;
MALLOCARRAY_NOFAIL(cmdlineP->inputFilespec, argc-1);
for (i = 0; i < argc-1; ++i)
cmdlineP->inputFilespec[i] = argv[1+i];
cmdlineP->nfiles = argc-1;
}
}
static void
computeOutputParms(unsigned int const nfiles,
enum orientation const orientation,
struct imgInfo const img[],
unsigned int * const newcolsP,
unsigned int * const newrowsP,
xelval * const newmaxvalP,
int * const newformatP) {
double newcols, newrows;
int newformat;
xelval newmaxval;
unsigned int i;
newcols = 0;
newrows = 0;
for (i = 0; i < nfiles; ++i) {
const struct imgInfo * const imgP = &img[i];
if (i == 0) {
newmaxval = imgP->maxval;
newformat = imgP->format;
} else {
if (PNM_FORMAT_TYPE(imgP->format) > PNM_FORMAT_TYPE(newformat))
newformat = imgP->format;
if (imgP->maxval > newmaxval)
newmaxval = imgP->maxval;
}
switch (orientation) {
case LEFTRIGHT:
newcols += imgP->cols;
if (imgP->rows > newrows)
newrows = imgP->rows;
break;
case TOPBOTTOM:
newrows += imgP->rows;
if (imgP->cols > newcols)
newcols = imgP->cols;
break;
}
}
/* Note that while 'double' is not in general a precise numerical type,
in the case of a sum of integers which is less than INT_MAX, it
is exact, because double's precision is greater than int's.
*/
if (newcols > INT_MAX)
pm_error("Output width too large: %.0f.", newcols);
if (newrows > INT_MAX)
pm_error("Output height too large: %.0f.", newrows);
*newrowsP = (unsigned int)newrows;
*newcolsP = (unsigned int)newcols;
*newmaxvalP = newmaxval;
*newformatP = newformat;
}
static unsigned char
leftBits(unsigned char const x,
unsigned int const n) {
/*----------------------------------------------------------------------------
Clear rightmost (8-n) bits, retain leftmost (=high) n bits.
-----------------------------------------------------------------------------*/
unsigned char retval;
assert(n < 8);
retval = x;
retval >>= (8-n);
retval <<= (8-n);
return retval;
}
static unsigned char
rightBits(unsigned char const x,
unsigned int const n){
/*----------------------------------------------------------------------------
Return rightmost (=low) n bits of x.
-----------------------------------------------------------------------------*/
unsigned char retval;
assert(n < 8);
retval = x;
retval <<= (8-n);
retval >>= (8-n);
return retval;
}
static void
copyBitrow(const unsigned char * const source,
unsigned char * const destBitrow,
unsigned int const cols,
unsigned int const offset) {
/*----------------------------------------------------------------------------
Copy from source to destBitrow, without shifting. Preserve
surrounding image data.
-----------------------------------------------------------------------------*/
unsigned char * const dest = & destBitrow[ offset/8 ];
/* Copy destination, with leading full bytes ignored. */
unsigned int const rs = offset % 8;
/* The "little offset", as measured from start of dest. Source
is already shifted by this value.
*/
unsigned int const trs = (cols + rs) % 8;
/* The number of partial bits in the final char. */
unsigned int const colByteCnt = pbm_packed_bytes(cols + rs);
/* # bytes to process, including partial ones on both ends. */
unsigned int const last = colByteCnt - 1;
unsigned char const origHead = dest[0];
unsigned char const origEnd = dest[last];
unsigned int i;
assert(colByteCnt >= 1);
for (i = 0; i < colByteCnt; ++i)
dest[i] = source[i];
if (rs > 0)
dest[0] = leftBits(origHead, rs) | rightBits(dest[0], 8-rs);
if (trs > 0)
dest[last] = leftBits(dest[last], trs) | rightBits(origEnd, 8-trs);
}
static void
padFillBitrow(unsigned char * const destBitrow,
char const padColor,
unsigned int const cols,
unsigned int const offset) {
/*----------------------------------------------------------------------------
Fill destBitrow, starting at offset, with padColor. padColor is a
byte -- 0x00 or 0xff -- not a single bit.
-----------------------------------------------------------------------------*/
unsigned char * const dest = &destBitrow[offset/8];
unsigned int const rs = offset % 8;
unsigned int const trs = (cols + rs) % 8;
unsigned int const colByteCnt = pbm_packed_bytes(cols + rs);
unsigned int const last = colByteCnt - 1;
unsigned char const origHead = dest[0];
unsigned char const origEnd = dest[last];
unsigned int i;
assert(colByteCnt > 0);
for (i = 0; i < colByteCnt; ++i)
dest[i] = padColor;
if (rs > 0)
dest[0] = leftBits(origHead, rs) | rightBits(dest[0], 8-rs);
if (trs > 0)
dest[last] = leftBits(dest[last], trs) | rightBits(origEnd, 8-trs);
}
/* concatenateLeftRightPBM() and concatenateLeftRightGen()
employ almost identical algorithms.
The difference is in the data types and functions.
Same for concatenateTopBottomPBM() and concatenateTopBottomGen().
*/
struct imgInfoPbm2 {
/* Information about one image */
unsigned char * proberow;
/* Top row of image, when background color is
auto-determined.
*/
unsigned int offset;
/* start position of image, in bits, counting from left
edge
*/
unsigned char background;
/* Background color. 0x00 means white; 0xff means black */
unsigned int padtop;
/* Top padding amount */
};
static void
getPbmImageInfo(struct imgInfo const img[],
unsigned int const nfiles,
unsigned int const newrows,
enum justification const justification,
enum backcolor const backcolor,
struct imgInfoPbm2 ** const img2P) {
/*----------------------------------------------------------------------------
Read the first row of each image in img[] and return that and additional
information about images as *img2P.
-----------------------------------------------------------------------------*/
struct imgInfoPbm2 * img2;
unsigned int i;
MALLOCARRAY_NOFAIL(img2, nfiles);
for (i = 0; i < nfiles; ++i) {
switch (justification) {
case JUST_MIN: img2[i].padtop = 0; break;
case JUST_MAX: img2[i].padtop = newrows - img[i].rows; break;
case JUST_CENTER: img2[i].padtop = (newrows - img[i].rows) / 2; break;
}
img2[i].offset = (i == 0) ? 0 : img2[i-1].offset + img[i-1].cols;
if (img[i].rows == newrows) /* no padding */
img2[i].proberow = NULL;
else { /* determine pad color for image i */
switch (backcolor) {
case BACK_AUTO: {
bit bgBit;
img2[i].proberow = pbm_allocrow_packed(img[i].cols+7);
pbm_readpbmrow_bitoffset(
img[i].ifP, img2[i].proberow,
img[i].cols, img[i].format, img2[i].offset % 8);
bgBit = pbm_backgroundbitrow(
img2[i].proberow, img[i].cols, img2[i].offset % 8);
img2[i].background = bgBit == PBM_BLACK ? 0xff : 0x00;
} break;
case BACK_BLACK:
img2[i].proberow = NULL;
img2[i].background = 0xff;
break;
case BACK_WHITE:
img2[i].proberow = NULL;
img2[i].background = 0x00;
break;
}
}
}
*img2P = img2;
}
static void
destroyPbmImg2(struct imgInfoPbm2 * const img2,
unsigned int const nfiles) {
unsigned int i;
for (i = 0; i < nfiles; ++i) {
if (img2[i].proberow)
free(img2[i].proberow);
}
free(img2);
}
static void
concatenateLeftRightPbm(FILE * const ofP,
unsigned int const nfiles,
unsigned int const newcols,
unsigned int const newrows,
enum justification const justification,
struct imgInfo const img[],
enum backcolor const backcolor) {
unsigned char * const outrow = pbm_allocrow_packed(newcols);
/* We use just one outrow. All padding and image data (with the
exeption of following img2.proberow) goes directly into this
packed PBM row.
*/
struct imgInfoPbm2 * img2;
/* malloc'ed array, one element per image. Shadows img[] */
unsigned int row;
getPbmImageInfo(img, nfiles, newrows, justification, backcolor, &img2);
for (row = 0; row < newrows; ++row) {
unsigned int i;
for (i = 0; i < nfiles; ++i) {
if ((row == 0 && img2[i].padtop > 0) ||
row == img2[i].padtop + img[i].rows) {
/* This row begins a run of padding, either above or below
file 'i', so set 'outrow' to padding.
*/
padFillBitrow(outrow, img2[i].background, img[i].cols,
img2[i].offset);
}
if (row == img2[i].padtop && img2[i].proberow != NULL) {
/* Top row has been read to proberow[] to determine
background. Copy it to outrow[].
*/
copyBitrow(img2[i].proberow, outrow,
img[i].cols, img2[i].offset);
} else if (row >= img2[i].padtop &&
row < img2[i].padtop + img[i].rows) {
pbm_readpbmrow_bitoffset(
img[i].ifP, outrow, img[i].cols, img[i].format,
img2[i].offset);
} else {
/* It's a row of padding, so outrow[] is already set
appropriately.
*/
}
}
pbm_writepbmrow_packed(ofP, outrow, newcols, 0);
}
destroyPbmImg2(img2, nfiles);
pbm_freerow_packed(outrow);
}
static void
concatenateTopBottomPbm(FILE * const ofP,
unsigned int const nfiles,
int const newcols,
int const newrows,
enum justification const justification,
struct imgInfo const img[],
enum backcolor const backcolor) {
unsigned char * const outrow = pbm_allocrow_packed(newcols);
/* Like the left-right PBM case, all padding and image data
goes directly into outrow. There is no proberow.
*/
unsigned char background, backgroundPrev;
/* 0x00 means white; 0xff means black */
unsigned int padleft;
bool backChange;
/* Background color is different from that of the previous
input image.
*/
unsigned int i;
unsigned int row, startRow;
outrow[pbm_packed_bytes(newcols)-1] = 0x00;
switch (backcolor){
case BACK_AUTO: /* do nothing */ break;
case BACK_BLACK: background = 0xff; break;
case BACK_WHITE: background = 0x00; break;
}
for (i = 0; i < nfiles; ++i) {
if (img[i].cols == newcols) {
/* No padding */
startRow = 0;
backChange = FALSE;
padleft = 0;
outrow[newcols-1] = 0x00;
} else {
/* Determine amount of padding and color */
switch (justification) {
case JUST_MIN: padleft = 0; break;
case JUST_MAX: padleft = newcols - img[i].cols; break;
case JUST_CENTER: padleft = (newcols - img[i].cols) / 2; break;
}
switch (backcolor) {
case BACK_AUTO: {
bit bgBit;
startRow = 1;
pbm_readpbmrow_bitoffset(img[i].ifP,
outrow, img[i].cols, img[i].format,
padleft);
bgBit = pbm_backgroundbitrow(outrow, img[i].cols, padleft);
background = bgBit == PBM_BLACK ? 0xff : 0x00;
backChange = (i == 0 || background != backgroundPrev);
} break;
case BACK_WHITE:
case BACK_BLACK:
startRow = 0;
backChange = (i==0);
break;
}
if (backChange || (i > 0 && img[i-1].cols > img[i].cols)) {
unsigned int const padright = newcols - padleft - img[i].cols;
if (padleft > 0)
padFillBitrow(outrow, background, padleft, 0);
if (padright > 0)
padFillBitrow(outrow, background, padright,
padleft + img[i].cols);
}
}
if (startRow == 1)
/* Top row already read for auto background color
determination. Write it out.
*/
pbm_writepbmrow_packed(ofP, outrow, newcols, 0);
for (row = startRow; row < img[i].rows; ++row) {
pbm_readpbmrow_bitoffset(img[i].ifP, outrow, img[i].cols,
img[i].format, padleft);
pbm_writepbmrow_packed(ofP, outrow, newcols, 0);
}
backgroundPrev = background;
}
free(outrow);
}
struct imgGen2 {
xel * xelrow;
xel * inrow;
xel background;
int padtop;
};
static void
getGenImgInfo(struct imgInfo const img[],
unsigned int const nfiles,
xel * const newxelrow,
unsigned int const newrows,
xelval const newmaxval,
int const newformat,
enum justification const justification,
enum backcolor const backcolor,
struct imgGen2 ** const img2P) {
struct imgGen2 * img2;
unsigned int i;
MALLOCARRAY_NOFAIL(img2, nfiles);
for (i = 0; i < nfiles; ++i) {
switch (justification) { /* Determine top padding */
case JUST_MIN:
img2[i].padtop = 0;
break;
case JUST_MAX:
img2[i].padtop = newrows - img[i].rows;
break;
case JUST_CENTER:
img2[i].padtop = (newrows - img[i].rows) / 2;
break;
}
img2[i].inrow =
(i == 0 ? &newxelrow[0] : img2[i-1].inrow + img[i-1].cols);
if (img[i].rows == newrows) /* no padding */
img2[i].xelrow = NULL;
else {
/* Determine pad color */
switch (backcolor){
case BACK_AUTO:
img2[i].xelrow = pnm_allocrow(img[i].cols);
pnm_readpnmrow(img[i].ifP, img2[i].xelrow,
img[i].cols, img[i].maxval, img[i].format);
pnm_promoteformatrow(img2[i].xelrow, img[i].cols,
img[i].maxval, img[i].format,
newmaxval, newformat);
img2[i].background = pnm_backgroundxelrow(
img2[i].xelrow, img[i].cols, newmaxval, newformat);
break;
case BACK_BLACK:
img2[i].xelrow = NULL;
img2[i].background = pnm_blackxel(newmaxval, newformat);
break;
case BACK_WHITE:
img2[i].xelrow = NULL;
img2[i].background = pnm_whitexel(newmaxval, newformat);
break;
}
}
}
*img2P = img2;
}
static void
concatenateLeftRightGen(FILE * const ofP,
unsigned int const nfiles,
unsigned int const newcols,
unsigned int const newrows,
xelval const newmaxval,
int const newformat,
enum justification const justification,
struct imgInfo const img[],
enum backcolor const backcolor) {
xel * const outrow = pnm_allocrow(newcols);
struct imgGen2 * img2;
unsigned int row;
getGenImgInfo(img, nfiles, outrow, newrows,
newmaxval, newformat, justification, backcolor, &img2);
for (row = 0; row < newrows; ++row) {
unsigned int i;
for (i = 0; i < nfiles; ++i) {
if ((row == 0 && img2[i].padtop > 0) ||
row == img2[i].padtop + img[i].rows) {
/* This row begins a run of padding, either above or below
file 'i', so set 'outrow' to padding.
*/
unsigned int col;
for (col = 0; col < img[i].cols; ++col)
img2[i].inrow[col] = img2[i].background;
}
if (row == img2[i].padtop && img2[i].xelrow) {
/* We're at the top row of file 'i', and that row
has already been read to xelrow[] to determine
background. Copy it to 'outrow'.
*/
unsigned int col;
for (col = 0; col < img[i].cols; ++col)
img2[i].inrow[col] = img2[i].xelrow[col];
free(img2[i].xelrow);
} else if (row >= img2[i].padtop &&
row < img2[i].padtop + img[i].rows) {
pnm_readpnmrow(
img[i].ifP, img2[i].inrow, img[i].cols, img[i].maxval,
img[i].format);
pnm_promoteformatrow(
img2[i].inrow, img[i].cols, img[i].maxval,
img[i].format, newmaxval, newformat);
} else {
/* It's a row of padding, so outrow[] is already set
appropriately.
*/
}
}
pnm_writepnmrow(ofP, outrow, newcols, newmaxval, newformat, 0);
}
pnm_freerow(outrow);
}
static void
concatenateTopBottomGen(FILE * const ofP,
unsigned int const nfiles,
int const newcols,
int const newrows,
xelval const newmaxval,
int const newformat,
enum justification const justification,
struct imgInfo const img[],
enum backcolor const backcolor) {
xel * const newxelrow = pnm_allocrow(newcols);
xel * inrow;
unsigned int padleft;
unsigned int i;
unsigned int row, startRow;
xel background, backgroundPrev;
bool backChange;
/* The background color is different from that of the previous
input image.
*/
switch (backcolor) {
case BACK_AUTO: /* do nothing now, determine at start of each image */
break;
case BACK_BLACK: background = pnm_blackxel(newmaxval, newformat);
break;
case BACK_WHITE: background = pnm_whitexel(newmaxval, newformat);
break;
}
for ( i = 0; i < nfiles; ++i, backgroundPrev = background) {
if (img[i].cols == newcols) {
/* no padding */
startRow = 0;
backChange = FALSE;
inrow = newxelrow;
} else { /* Calculate left padding amount */
switch (justification) {
case JUST_MIN: padleft = 0; break;
case JUST_MAX: padleft = newcols - img[i].cols; break;
case JUST_CENTER: padleft = (newcols - img[i].cols) / 2; break;
}
if (backcolor == BACK_AUTO) {
/* Determine background color */
startRow = 1;
inrow = &newxelrow[padleft];
pnm_readpnmrow(img[i].ifP, inrow,
img[i].cols, img[i].maxval, img[i].format);
pnm_promoteformatrow(inrow, img[i].cols, img[i].maxval,
img[i].format,
newmaxval, newformat);
background = pnm_backgroundxelrow(
inrow, img[i].cols, newmaxval, newformat);
backChange = i==0 || !PNM_EQUAL(background, backgroundPrev);
} else {
/* background color is constant: black or white */
startRow = 0;
inrow = &newxelrow[padleft];
backChange = (i==0);
}
if (backChange || (i > 0 && img[i-1].cols > img[i].cols)) {
unsigned int col;
for (col = 0; col < padleft; ++col)
newxelrow[col] = background;
for (col = padleft + img[i].cols; col < newcols; ++col)
newxelrow[col] = background;
}
}
if (startRow == 1)
/* Top row already read for auto background
color determination. Write it out. */
pnm_writepnmrow(ofP, newxelrow, newcols, newmaxval, newformat, 0);
for (row = startRow; row < img[i].rows; ++row) {
pnm_readpnmrow(img[i].ifP,
inrow, img[i].cols, img[i].maxval, img[i].format);
pnm_promoteformatrow(
inrow, img[i].cols, img[i].maxval, img[i].format,
newmaxval, newformat);
pnm_writepnmrow(ofP, newxelrow, newcols, newmaxval, newformat, 0);
}
}
pnm_freerow(newxelrow);
}
int
main(int argc,
const char ** argv) {
struct cmdlineInfo cmdline;
struct imgInfo * img; /* malloc'ed array */
xelval newmaxval;
int newformat;
unsigned int i;
unsigned int newrows, newcols;
pm_proginit(&argc, argv);
parseCommandLine(argc, argv, &cmdline);
MALLOCARRAY_NOFAIL(img, cmdline.nfiles);
for (i = 0; i < cmdline.nfiles; ++i) {
img[i].ifP = pm_openr(cmdline.inputFilespec[i]);
pnm_readpnminit(img[i].ifP, &img[i].cols, &img[i].rows,
&img[i].maxval, &img[i].format);
}
computeOutputParms(cmdline.nfiles, cmdline.orientation, img,
&newcols, &newrows, &newmaxval, &newformat);
pnm_writepnminit(stdout, newcols, newrows, newmaxval, newformat, 0);
if (PNM_FORMAT_TYPE(newformat) == PBM_TYPE) {
switch (cmdline.orientation) {
case LEFTRIGHT:
concatenateLeftRightPbm(stdout, cmdline.nfiles,
newcols, newrows, cmdline.justification,
img, cmdline.backcolor);
break;
case TOPBOTTOM:
concatenateTopBottomPbm(stdout, cmdline.nfiles,
newcols, newrows, cmdline.justification,
img, cmdline.backcolor);
break;
}
} else {
switch (cmdline.orientation) {
case LEFTRIGHT:
concatenateLeftRightGen(stdout, cmdline.nfiles,
newcols, newrows, newmaxval, newformat,
cmdline.justification, img,
cmdline.backcolor);
break;
case TOPBOTTOM:
concatenateTopBottomGen(stdout, cmdline.nfiles,
newcols, newrows, newmaxval, newformat,
cmdline.justification, img,
cmdline.backcolor);
break;
}
}
for (i = 0; i < cmdline.nfiles; ++i)
pm_close(img[i].ifP);
free(cmdline.inputFilespec);
pm_close(stdout);
return 0;
}
|