-
Notifications
You must be signed in to change notification settings - Fork 5
/
tc_catpgm.c
executable file
·117 lines (103 loc) · 2.86 KB
/
tc_catpgm.c
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
/**
* \file tc_catpgm.h
* \brief Concatenate channels of a PGM into a multi-channel PGM image
* \author David Ray Thompson
*
* Copyright 2012, by the California Institute of Technology. ALL RIGHTS
* RESERVED. United States Government Sponsorship acknowledged. Any
* commercial use must be negotiated with the Office of Technology
* Transfer at the California Institute of Technology.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "tc_image.h"
int main(int argc, char **argv)
{
int i, j, b, r, c, curchan, nimages, arg=1;
int totchans = 0;
char *outname = NULL;
pixel_t val;
tc_image **in = NULL, *out = NULL;
/* Parse the command line */
while ((arg < argc) && (argv[arg][0] == '-'))
{
if (strcmp(argv[arg],"-o") == 0)
{
arg++;
outname = argv[arg];
fprintf(stderr,"output to: %s\n", argv[arg]);
}
else
{
fprintf(stderr,"Unrecognized switch: %s\n", argv[arg]);
}
arg++;
}
nimages = argc - arg;
fprintf(stderr,"%i input images.\n", nimages);
if ((nimages < 2) || (outname == NULL))
{
fprintf(stderr,
"Usage: catpgm -o <output.pgm> <input1.pgm> <input2.pgm> ...\n");
exit(-1);
}
in = malloc(sizeof(tc_image *) * nimages);
/* Read each image sequentially, checking integrity as we go */
for (i=0; i<nimages; i++)
{
tc_read_image(&(in[i]), argv[arg+i]);
totchans += in[i]->chans;
if ((&(in[i]) == NULL) ||
((i>0) && (in[i]->rows != in[0]->rows)) ||
((i>0) && (in[i]->cols != in[0]->cols)))
{
fprintf(stderr,"Out of memory, or dimensions don't match!\n");
for (j=0; j<=i; j++)
{
tc_free_image((in[i])); /* Free old images */
}
free(in);
exit(-1);
}
}
/* Allocate the output image */
tc_alloc_image(&out, in[0]->rows, in[0]->cols, totchans);
if (out == NULL)
{
fprintf(stderr,"Out of memory!\n");
for (i=0; i<nimages; i++)
{
tc_free_image((in[i])); /* Free old images */
}
free(in);
exit(-1);
}
/* Copy each pixel into the new channel stack */
curchan = 0;
for (i=0; i<nimages; i++)
{
for (b=0; b<in[i]->chans; b++)
{
for (r=0; r<in[0]->rows; r++)
{
for (c=0; c<in[0]->cols; c++)
{
val = tc_get(in[i],r,c,b);
tc_set(out,r,c,curchan,val);
}
}
}
curchan++;
}
/* Write output */
tc_write_image(out, outname);
/* Clean up */
for (i=0; i<nimages; i++)
{
tc_free_image(in[i]);
}
free(in);
tc_free_image(out);
return 0;
}