2
* program epmcompress - compress Extented Pixmap Format epm using
3
* simple Runlength Encoding
5
* (C) by Oliver Vogel (e-mail: vogel@ikp.uni-koeln.de)
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public Licence as by published
10
* by the Free Software Foundation; either version 2; or (at your option)
13
* This program is distributed in the hope that it will entertaining,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16
* Public License for more details.
18
* $Id: epmuncompress.c,v 1.1 1999/04/04 11:43:54 xblast Exp $
19
* $Log: epmuncompress.c,v $
20
* Revision 1.1 1999/04/04 11:43:54 xblast
42
int width, height, maxval, depth;
43
int arg, n_pixel, n_bytes;
44
unsigned char *inbuf, *outbuf;
45
unsigned char *src, *dst;
50
/* check args (part one) */
51
if ( (argc < 2) || (0 == strcmp(argv[1],"-?") ) ) {
52
fprintf(stderr, "usage: %s epmfile ...\n", argv[0]);
56
for (arg = 1; arg < argc; arg++) {
59
if (NULL == (fp = fopen(argv[arg],"r") ) ) {
60
fprintf(stderr, "%s: failed to open file %s for reading\n",
66
if (5 != fscanf(fp, "%s%d%d%d%d%*c", magic,&width,&height,&maxval,&depth) ) {
67
fprintf(stderr, "%s: Failed to read epm header\n",argv[0]);
71
if (0 != strcmp(magic,"PZ")) {
72
if (0 == strcmp(magic, "PX") ) {
73
fprintf(stderr, "File \"%s\" is already uncompressed.\n", argv[arg]);
77
fprintf(stderr, "%s: Wrong magic word \"%s\".\n",argv[0],magic);
81
/* calc number of pixel per layer */
82
n_pixel = width*height;
84
fprintf (stderr, "%d pixels\n", n_pixel);
88
/* alloc input and color buffer */
89
if ( (NULL == (inbuf = malloc(2*depth*n_pixel*sizeof(char) ) ) ) ||
90
(NULL == (outbuf = calloc(depth*n_pixel,sizeof(char) ) ) ) ) {
91
fprintf(stderr, "%s: Failed to alloc buffer\n",argv[0]);
96
if (0 == (n_bytes = fread(inbuf, sizeof(char), 2*depth*n_pixel, fp) ) ) {
100
fprintf (stderr, "File \"%s\": %6.1f%%.\n", argv[arg],
101
100. * (1. - (double)n_bytes/(depth*n_pixel) ) );
104
for (src = inbuf, dst=outbuf; (src < inbuf + n_bytes) && (dst < outbuf + depth*n_pixel); src++) {
113
} while (*src == 255);
114
memset (dst, 0, zero_count);
120
/* rename old file */
121
sprintf (file_name, "%s~", argv[arg]);
122
if (rename (argv[arg], file_name)) {
123
fprintf (stderr, "Failed to create backup file \"%s\".\n",
128
if (NULL == (fp = fopen (argv[arg], "w") ) ) {
129
fprintf(stderr, "%s: failed to open file %s for writing\n",
134
fprintf (fp, "PX\n");
135
fprintf (fp, "%d %d %d %d\n", width, height, maxval, depth);
136
fwrite (outbuf, sizeof(char), dst - outbuf, fp);