Debian Codegraph @

editor/specialty/pampop9.c

raw
1 /*
2 *
3 * (c) Robert Tinsley, 2003 (http://www.thepoacher.net/contact)
4 *
5 * Released under the GPL (http://www.fsf.org/licenses/gpl.txt)
6 *
7 *
8 * CHANGES
9 *
10 * v1.00 2003-02-28 Original version
11 *
12 * v1.10 2003-03-02
13 * + changed to use pam_* routines rather than ppm_*
14 * + changed to use pm_* rather than fopen()/fclose()
15 * + renamed from ppmgrid to pampup9
16 * + wrote a man-page (actually, html)
17 *
18 * Changes by Bryan Henderson for inclusion in the Netpbm package (fully
19 * exploiting Netpbm library). Renamed to Pampop9. March 2003.
20 *
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h> /* atoi() */
25
26 #include "pam.h"
27
28 static const char *usagestr = "pnmfile|- xtiles ytiles xdelta ydelta";
29
30 int main(int argc, char *argv[])
31 {
32 const char *filename = "-";
33 int xtiles, ytiles, xdelta, ydelta;
34 FILE *fp;
35
36 struct pam spam, dpam;
37 tuple **spix, *dpix;
38 int xtilesize, ytilesize, sx, sy, dx, dy, p;
39
40
41
42 pnm_init(&argc, argv);
43
44 if (argc-1 != 5) {
45 pm_error("Wrong number of arguments. Program requires 5 arguments; "
46 "you supplied %d. Usage: %s", argc-1, usagestr);
47 }
48
49 filename = argv[1];
50 xtiles = atoi(argv[2]);
51 ytiles = atoi(argv[3]);
52 xdelta = atoi(argv[4]);
53 ydelta = atoi(argv[5]);
54
55 if (filename == NULL || *filename == '\0'
56 || xtiles <= 0 || ytiles <= 0 || xdelta < 0 || ydelta < 0)
57 pm_error("invalid argument");
58
59 /* read src pam */
60
61 fp = pm_openr(filename);
62
63 spix = pnm_readpam(fp, &spam, PAM_STRUCT_SIZE(tuple_type));
64
65 pm_close(fp);
66
67 /* init dst pam */
68
69 xtilesize = spam.width - (xtiles - 1) * xdelta;
70 ytilesize = spam.height - (ytiles - 1) * ydelta;
71
72 if (xtilesize <= 0)
73 pm_error("xtilesize must be positive. You specified %d", xtilesize);
74 if (ytilesize <= 0)
75 pm_error("ytilesize must be positive. You specified %d", ytilesize);
76
77 dpam = spam;
78 dpam.file = stdout;
79 dpam.width = xtiles * xtilesize;
80 dpam.height = ytiles * ytilesize;
81
82 dpix = pnm_allocpamrow(&dpam);
83
84 pnm_writepaminit(&dpam);
85
86 /* generate dst pam */
87
88 for (dy = 0; dy < dpam.height; dy++) {
89 sy = ((int) (dy / ytilesize)) * ydelta + (dy % ytilesize);
90 for (dx = 0; dx < dpam.width; dx++) {
91 sx = ((int) (dx / xtilesize)) * xdelta + (dx % xtilesize);
92 for (p = 0; p < spam.depth; ++p) {
93 dpix[dx][p] = spix[sy][sx][p];
94 }
95 }
96 pnm_writepamrow(&dpam, dpix);
97 }
98
99 /* all done */
100
101 pnm_freepamarray(spix, &spam);
102 pnm_freepamrow(dpix);
103
104 exit(EXIT_SUCCESS);
105 }