1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * mgbilevel.c -- Program to compress/decompress bilevel 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: mgbilevel.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "utils.h"
|
---|
27 | #include "pbmtools.h"
|
---|
28 | #include "arithcode.h"
|
---|
29 | #include "bilevel.h"
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | static char default_template[] =
|
---|
34 | {
|
---|
35 | ".ppppp.;"
|
---|
36 | "pp222pp;"
|
---|
37 | "p22222p;"
|
---|
38 | "p22*...;"
|
---|
39 | };
|
---|
40 |
|
---|
41 | void
|
---|
42 | Encode_bitmap (marktype b, char template[])
|
---|
43 | {
|
---|
44 | InitArithEncoding ();
|
---|
45 |
|
---|
46 | bl_compress (b, template);
|
---|
47 |
|
---|
48 | CloseDownArithEncoding ();
|
---|
49 | }
|
---|
50 |
|
---|
51 | void
|
---|
52 | Decode_bitmap (marktype * b)
|
---|
53 | {
|
---|
54 | InitArithDecoding ();
|
---|
55 |
|
---|
56 | bl_decompress (b);
|
---|
57 |
|
---|
58 | CloseDownArithDecoding ();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | void
|
---|
63 | usage ()
|
---|
64 | {
|
---|
65 | fprintf (stderr,
|
---|
66 | "usage:\n"
|
---|
67 | " mgbilevel -e [-t \"string\"] infile >compressed\n"
|
---|
68 | "or\n"
|
---|
69 | " mgbilevel -d compressed >outfile\n");
|
---|
70 | exit (1);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #define BUF_SIZE 65536
|
---|
74 |
|
---|
75 | void
|
---|
76 | main (int argc, char *args[])
|
---|
77 | {
|
---|
78 | marktype d;
|
---|
79 | char *filename = NULL, *template = NULL;
|
---|
80 | Pixel **bitmap;
|
---|
81 | int encode = 0, decode = 0, i;
|
---|
82 | char buffer[BUF_SIZE];
|
---|
83 |
|
---|
84 | if (argc < 2)
|
---|
85 | usage ();
|
---|
86 | template = default_template;
|
---|
87 | for (i = 1; i < argc; i++)
|
---|
88 | {
|
---|
89 | if (strcmp (args[i], "-e") == 0)
|
---|
90 | encode = 1;
|
---|
91 | else if (strcmp (args[i], "-d") == 0)
|
---|
92 | decode = 1;
|
---|
93 | else if (strcmp (args[i], "-h") == 0)
|
---|
94 | usage ();
|
---|
95 | else if (strcmp (args[i], "-t") == 0)
|
---|
96 | {
|
---|
97 | template = args[i + 1];
|
---|
98 | i++;
|
---|
99 | }
|
---|
100 | else if (args[i][0] == '-')
|
---|
101 | error_msg (args[0], "unknown switch:", args[i]);
|
---|
102 | else if (!filename)
|
---|
103 | filename = args[i];
|
---|
104 | else
|
---|
105 | error_msg (args[0], "only specify 1 filename", "");
|
---|
106 | }
|
---|
107 | if (encode == decode)
|
---|
108 | {
|
---|
109 | fprintf (stderr, "You must specify either encode XOR decode.\n");
|
---|
110 | exit (1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (!filename)
|
---|
114 | {
|
---|
115 | fprintf (stderr, "You must specify a filename.\n");
|
---|
116 | exit (1);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (encode)
|
---|
120 | {
|
---|
121 | bitmap = pbm_readnamedfile (filename, &d.w, &d.h);
|
---|
122 | if (bitmap == NULL)
|
---|
123 | error_msg (args[0], "problem reading bitmap", "");
|
---|
124 |
|
---|
125 | arith_out = stdout;
|
---|
126 | setbuffer (stdout, buffer, BUF_SIZE);
|
---|
127 | magic_write (stdout, MAGIC_BILEVEL);
|
---|
128 | if (bitmap)
|
---|
129 | {
|
---|
130 | d.bitmap = bitmap;
|
---|
131 | Encode_bitmap (d, template);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else if (decode)
|
---|
135 | {
|
---|
136 | FILE *tpin;
|
---|
137 | tpin = fopen (filename, "rb");
|
---|
138 | if (!tpin)
|
---|
139 | error_msg ("mgbilevel", "Can't open file:", filename);
|
---|
140 | arith_in = tpin;
|
---|
141 | setbuffer (tpin, buffer, BUF_SIZE);
|
---|
142 | magic_check (tpin, MAGIC_BILEVEL);
|
---|
143 | Decode_bitmap (&d);
|
---|
144 | fclose (tpin);
|
---|
145 |
|
---|
146 | setbuffer (stdout, buffer, BUF_SIZE);
|
---|
147 | pbm_writefile (stdout, d.bitmap, d.w, d.h);
|
---|
148 | }
|
---|
149 | }
|
---|