2
* program epmarrange - arrange several epm files on a canvas
4
* (C) by Oliver Vogel (e-mail: vogel@ikp.uni-koeln.de)
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public Licence as by published
9
* by the Free Software Foundation; either version 2; or (at your option)
12
* This program is distributed in the hope that it will entertaining,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15
* Public License for more details.
17
* $Id: epmarrange.c,v 1.1 1999/04/04 11:44:46 xblast Exp $
18
* $Log: epmarrange.c,v $
19
* Revision 1.1 1999/04/04 11:44:46 xblast
29
* some type defintions
31
typedef struct _epm_file {
33
unsigned width, height;
34
unsigned maxval, depth;
39
struct _epm_file * next;
42
typedef struct _epm_data {
43
unsigned width, height;
44
unsigned maxval, depth;
52
static char *progname;
55
* parse_input: parse the input file for epmfile names and positions
59
parse_input (char *filename)
62
parse_input (filename)
67
static char line[1024];
68
static char epmname[1024];
70
EpmFile *new, *last = NULL, *root = NULL;
72
if ( (NULL == filename) || (0 == strcmp (filename, "-") ) ) {
76
if (NULL == (fp = fopen(filename, "r") ) ) {
82
while (NULL != fgets(line, sizeof(line), fp) ) {
83
if (3 == sscanf(line, "%u %u %s", &x, &y, epmname) ) {
84
if ( (NULL == (new = (EpmFile *) malloc(sizeof(EpmFile) ) ) ) ||
85
(NULL == (new->fname = (char *) malloc(strlen(epmname)+1) ) ) ) {
86
fprintf(stderr, "%s: alloc failed\n", progname);
92
strcpy(new->fname, epmname);
94
fprintf(stderr, "EPM %s at (%u,%u)\n", new->fname, new->x, new->y);
109
check_epm (EpmFile *epm)
119
for (; epm != NULL; epm = epm->next) {
121
if (NULL == (fp = fopen(epm->fname, "r") ) ) {
126
if (5 != fscanf(fp, "%s%d%d%d%d%*c", magic, &(epm->width), &(epm->height),
127
&(epm->maxval), &(epm->depth) ) ) {
128
fprintf(stderr, "%s: Failed to read epm header\n",progname);
132
if (0 != strcmp(magic,"PX")) {
133
fprintf(stderr, "%s: Wrong magic word \"%s\".\n", progname, magic);
136
/* get offset of pixel data */
137
epm->offset = ftell(fp);
140
/* alloc line buffer */
141
if (NULL == (epm->line = malloc(epm->width) ) ) {
142
fprintf(stderr, "%s: alloc failed\n", progname);
145
/* set total number of pixel */
146
epm->npixel = epm->width * epm->height;
155
init_epm (EpmFile *epm)
165
if (NULL == (new = calloc(1, sizeof(EpmData) ) ) ) {
166
fprintf(stderr, "%s: alloc failed\n", progname);
170
for (; epm != NULL; epm = epm->next) {
172
w = epm->x + epm->width;
173
if (w > new->width) {
177
h = epm->y + epm->height;
178
if (h > new->height) {
182
if (epm->maxval > new->maxval) {
183
new->maxval = epm->maxval;
186
if (epm->depth > new->depth) {
187
new->depth = epm->depth;
190
new->npixel = new->width * new->height;
192
/* alloc one layer */
193
if (NULL == (new->layer = malloc(new->npixel) ) ) {
194
fprintf(stderr, "%s: alloc failed\n", progname);
203
draw_one_layer (EpmData *dst,
208
draw_one_layer (dst, src, layer)
219
memset(dst->layer, 0, src->npixel);
221
for (; src != NULL; src = src->next) {
222
if (layer < src->depth) {
224
if (NULL == (fp = fopen(src->fname, "r") ) ) {
228
/* skip to layer data */
229
if (0 > fseek(fp, src->offset + layer * src->npixel, SEEK_SET) ) {
234
ptr = dst->layer + src->x + src->y*dst->width;
235
for (y=0; y<src->height; y++) {
237
if (src->width != fread (src->line, 1, src->width, fp) ) {
239
fprintf(stderr, "%s: premature eof in file %s\n",
240
progname, src->fname);
246
/* copy to dest epm */
247
memcpy (ptr, src->line, src->width);
259
epm_open (EpmData *epm,
263
epm_open (epm, filename)
270
if ( (NULL == filename) || (0 == strcmp (filename, "-") ) ){
273
if (NULL == (fp = fopen(filename, "w") ) ) {
281
fprintf(fp,"%u %u %u %u\n", epm->width,epm->height,epm->maxval,epm->depth);
289
write_layer (FILE *fp,
293
write_layer (fp, epm)
298
if (epm->npixel != fwrite(epm->layer, 1, epm->npixel, fp) ) {
330
/* parse commandline args */
342
/* print usage message */
343
fprintf(stderr, "usage: %s [datafile [outputfile]]\n", argv[0]);
347
if (NULL == (epm_list = parse_input(input) ) ) {
350
if (check_epm (epm_list) ){
353
if (NULL == (new_epm = init_epm(epm_list) ) ) {
356
/* open output file */
357
if (NULL == (fp = epm_open(new_epm, output) ) ) {
360
for (layer = 0; layer < new_epm->depth; layer++) {
361
if (draw_one_layer(new_epm, epm_list, layer) ) {
364
if (write_layer(fp, new_epm) ) {