1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * mgfelics.c -- Program to compress/decompress grey scale images
|
---|
4 | * Copyright (C) 1994 Stuart Inglis
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: mgfelics.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 | #include "utils.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | #define MAXKVALUE 32
|
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 | void encode (unsigned int *line, int width, int height, int maxval, int maxk,
|
---|
33 | FILE * fp_in, FILE * fp_out, int (*get) (FILE *));
|
---|
34 |
|
---|
35 | void decode (unsigned int *line, int width, int height, int maxval, int maxk,
|
---|
36 | FILE * fp_in, FILE * fp_out);
|
---|
37 |
|
---|
38 |
|
---|
39 | void
|
---|
40 | usage (char *prog)
|
---|
41 | {
|
---|
42 | fprintf (stderr, "usage:\n");
|
---|
43 | fprintf (stderr, "\t%s -e [-k n] infile [>compressed]\n"
|
---|
44 | "or\n"
|
---|
45 | "\t%s -d compressed-infile [>outfile]\n", prog, prog);
|
---|
46 | exit (1);
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | int
|
---|
53 | main (int argc, char **argv)
|
---|
54 | {
|
---|
55 | FILE *fp_in = (stdin), *fp_out = (stdout);
|
---|
56 | int width, height, maxval;
|
---|
57 | u_long magic;
|
---|
58 | char *filename = NULL;
|
---|
59 | unsigned int *line;
|
---|
60 | int maxk = 0;
|
---|
61 | int i;
|
---|
62 | int encode_flag = 0, decode_flag = 0;
|
---|
63 |
|
---|
64 | if (argc < 2)
|
---|
65 | usage (argv[0]);
|
---|
66 |
|
---|
67 | for (i = 1; i < argc; i++)
|
---|
68 | {
|
---|
69 | if (!strcmp (argv[i], "-h"))
|
---|
70 | usage (argv[0]);
|
---|
71 | else if (!strcmp (argv[i], "-e"))
|
---|
72 | encode_flag = 1;
|
---|
73 | else if (!strcmp (argv[i], "-d"))
|
---|
74 | decode_flag = 1;
|
---|
75 | else if (!strcmp (argv[i], "-k"))
|
---|
76 | {
|
---|
77 | maxk = atoi (argv[++i]);
|
---|
78 | }
|
---|
79 | else if (argv[i][0] == '-')
|
---|
80 | error_msg (argv[0], "unknown switch:", argv[i]);
|
---|
81 | else if (!filename)
|
---|
82 | filename = argv[i];
|
---|
83 | else
|
---|
84 | error_msg (argv[0], "too many filenames specified.", "");
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (encode_flag == decode_flag)
|
---|
88 | error_msg (argv[0], "please specify either encode XOR decode.", "");
|
---|
89 |
|
---|
90 | if (!filename)
|
---|
91 | error_msg (argv[0], "please specify a filename.", "");
|
---|
92 |
|
---|
93 | if ((encode_flag) && (maxk >= MAXKVALUE))
|
---|
94 | { /* to fit into 32 bit integers */
|
---|
95 | fprintf (stderr, "%s: k value of %d is too large; it must be less than %d\n",
|
---|
96 | argv[0], maxk, MAXKVALUE);
|
---|
97 | exit (1);
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | if ((fp_in = fopen (filename, "rb")) == NULL)
|
---|
102 | error_msg (argv[0], "unable to open file:", filename);
|
---|
103 |
|
---|
104 | /* read file header to determine the type and dimensions */
|
---|
105 |
|
---|
106 |
|
---|
107 | magic = magic_read (fp_in);
|
---|
108 | if (magic != MAGIC_FELICS)
|
---|
109 | {
|
---|
110 | rewind (fp_in);
|
---|
111 | magic = getmagicno_short_pop (fp_in);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if ((decode_flag && (magic != MAGIC_FELICS)) ||
|
---|
115 | (encode_flag && ((magic != MAGIC_P2) && (magic != MAGIC_P5))))
|
---|
116 | {
|
---|
117 | fclose (fp_in);
|
---|
118 | error_msg (argv[0], encode_flag ? "only PGM files can be used." : "only felics compressed files can be used.", "");
|
---|
119 | }
|
---|
120 |
|
---|
121 | width = gethint (fp_in);
|
---|
122 | height = gethint (fp_in);
|
---|
123 | maxval = gethint (fp_in);
|
---|
124 |
|
---|
125 | if ((line = (unsigned int *) malloc (width * sizeof (int *))) == NULL)
|
---|
126 | {
|
---|
127 | fclose (fp_in);
|
---|
128 | error_msg (argv[0], "unable to allocate memory,", "");
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | if (encode_flag)
|
---|
133 | {
|
---|
134 | magic_write (fp_out, MAGIC_FELICS);
|
---|
135 | fprintf (fp_out, "%d\n%d\n%d\n%d\n", width, height, maxval, maxk);
|
---|
136 | encode (line, width, height, maxval, maxk, fp_in, fp_out,
|
---|
137 | magic == MAGIC_P2 ? getint : fgetc);
|
---|
138 | }
|
---|
139 | else if (decode_flag)
|
---|
140 | {
|
---|
141 | if (maxk)
|
---|
142 | warn (argv[0], "k value not required for decompression, ignored.", "");
|
---|
143 |
|
---|
144 | maxk = gethint (fp_in);
|
---|
145 | fprintf (fp_out, "P%d\n%d %d\n%d\n", maxval < 256 ? 5 : 2,
|
---|
146 | width, height, maxval);
|
---|
147 | decode (line, width, height, maxval, maxk, fp_in, fp_out);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | fclose (fp_in);
|
---|
152 | fclose (fp_out);
|
---|
153 |
|
---|
154 | return 0;
|
---|
155 | }
|
---|