summaryrefslogtreecommitdiff
path: root/imageto/main.c
blob: 15b3906370229c710cb5a5325154d2af9d4c6f47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* imageto -- convert a scanned image.

Copyright (C) 1992, 2004, 2011 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "config.h"
#include "lib.h"

#include "concatn.h"

#define CMDLINE_NO_DPI /* It's not in the input filename.  */
#include "cmdline.h"
#include "encoding.h" /* For `DEFAULT_ENCODING'.  */
#include "getopt.h"
#include "gf.h"
#include "report.h"

#include "ifi.h" /* For `encoding_name'.  */
#include "image-header.h"
#include "input-img.h"
#include "input-pbm.h"
#include "main.h"
#include "out-chars.h"
#include "out-epsf.h"
#include "out-strips.h"


/* General information about the image.  Set by the input routines.  */
image_header_type image_header;

/* Pointers to functions based on input format.  (-input-format)  */
void (*image_open_input_file) (string) = NULL;
void (*image_close_input_file) (void) = NULL;
void (*image_get_header) (void) = NULL;
boolean (*image_get_scanline) (one_byte *) = NULL;

/* The filename for the input image.  */
string input_name;

/* Show every scanline on the terminal as we read it?  (-trace-scanlines) */
boolean trace_scanlines = false;

/* Private variables.  */

/* The design size of the font we're creating.  (-designsize)  */
static real design_size = 10.0;

/* The suffix for the image file.  */
static string input_extension;

/* If set, write an EPSF file.  (-epsf)  */
static boolean output_epsf = false;

/* The name of the file we're going to write.  (-output-file) */
string output_name;

/* If set, make each ``character'' a (more or less) constant number of
   scanlines.  (-strips) */
static boolean do_strips = false;


static string read_command_line (int, string[]);
static void set_img_input_format (void);
static void set_input_format (string);
static void set_pbm_input_format (void);

/* We have three different strategies for processing the image:
     1)  (normal) analyze the image and write out the ``true'' characters,
     2) (-strips) takes a constant number of scanlines as each character,
     3)   (-epsf) write out the image as an Encapsulated PostScript file.

   The output name is, correspondingly, constructed differently:
     1) if `output_name' was supplied, and has a suffix, that's it.
     2) if `output_name' was supplied, but has no suffix, append `.eps'
        if we're doing EPSF, else append `<suffix>.<dpi>gf', where
        <suffix> is
          a) `sp', if we are doing strips;
          b) the design size, if not.
     3) if `output_name' was not supplied, use the basename of the input
        filename extended as in #2.
   */

int
main (int argc, string argv[])
{
  boolean writing_gf;
  
  /* kpse_set_progname (argv[0]); */

  input_name = read_command_line (argc, argv);
  writing_gf = !output_epsf;

  set_input_format (input_name);

  /* If the input format is PBM, then they must also tell us the dpi.  */
  if (image_open_input_file == pbm_open_input_file
      && image_header.hres == 0)
    {
      fprintf (stderr, "If you use PBM format, you must supply the dpi.\n");
      fprintf (stderr, "For more information, use ``-help''.\n");
      exit (1);
    }

  /* Open the main input file.  */
  (*image_open_input_file) (extend_filename (input_name, input_extension));

  /* We need the horizontal resolution before we can make the GF name,
     so, at least for IMG input, have to read the header.  */
  (*image_get_header) ();

  /* If the user didn't give an output name, use the input name.  */
  if (output_name == NULL)
    output_name =  remove_suffix ( (string) basename (input_name));
  
  /* If they didn't give an output name with a suffix, use ours.  */
  if (find_suffix (output_name) == NULL)
    {
      if (writing_gf)
        {
          char dpi[MAX_INT_LENGTH + 1];
          string suffix = do_strips ? "sp" : itoa ((unsigned) design_size);

          sprintf (dpi, "%u", image_header.hres);
          output_name = concatn (output_name, suffix, ".", dpi, "gf", NULL);
	}
      else if (output_epsf)
        output_name = concat (output_name, ".eps");
      else
        abort (); /* Should never happen.  */
    }

  /* If necessary, open the GF file.  */
  if (writing_gf)
    {
      if (!gf_open_output_file (output_name))
        FATAL_PERROR (output_name);

      /* Identify ourselves in the GF comment.  */
      gf_put_preamble (concat ("imageto output ", now () + 4));
    }

  /* Do the real work, whichever the user wants.  */
  if (do_strips)
    { /* The design size is irrelevant when we're creating strips,
         but it should be pretty large, lest the relative dimensions in
         the TFM file get too big.  */
      design_size = 100.0;
      write_chars_as_strips (image_header, design_size);
    }
  else if (output_epsf)
    write_epsf (output_name, image_header);
  else
    write_image_chars (image_header, design_size);

  /* Clean up.  */
  if (verbose)
    REPORT ("\n");

  if (writing_gf)
    {
      /* We've read all the characters we're supposed to (or else the whole
         image).  Finish up the font.  */
      gf_put_postamble (real_to_fix (design_size),
                        (real) image_header.hres, (real) image_header.vres);
      gf_close_output_file ();
    }

  (*image_close_input_file) ();

  return 0;
}

/* We are semi-clever about printing this, for the sake of huge images.  */

void
print_scanline (one_byte line[], unsigned width)
{
  static unsigned scanline_count = 0;

  if (trace_scanlines)
    {
      printf ("%7d:", scanline_count++);

      /* If the line is entirely white, don't print anything.  */
      if (memchr (line, BLACK, width))
        {
          unsigned c;

          for (c = 0; c < width; c++)
            { /* Compress eight consecutive spaces to a tab, if we're at
                 the beginning of a tab mark.  This handles the usual
                 case, although we could do still better.  */
              if (c % 8 == 0 && c + 7 < width
	          && memchr (line + c, BLACK, 8) == NULL)
                {
                  putchar ('\t');
                  c += 7;
		}
              else
                putchar (line[c] ? '*' : ' ');
	    }
	}

      putchar ('\n');
    }
}

/* Reading the options.  */

/* This is defined in version.c.  */
extern string version_string;

#define USAGE "Options:\n\
<font_name> should be a base filename, e.g., `ggmr'.  (More properly, it\n\
is an <image_name>, not a <font_name>.)" 				\
  GETOPT_USAGE								\
"baselines <row1>,<row2>,...: define the baselines for each image row.\n\
  The baseline of the first image row is taken to be scanline <row1>, etc.\n\
designsize <real>: set the designsize of the font to <real>; default is 10.0.\n\
dpi <unsigned>: resolution (required for pbm input).\n\
encoding <filename>: read ligature and other encoding information\n\
  from `<filename>.enc'; the default is to assign successive character codes.\n\
epsf: write the image as an Encapsulated PostScript file, instead of a\n\
  bitmap font.\n\
help: print this message.\n\
ifi-file <filename>: use <filename>.ifi (if <filename doesn't have a\n\
  suffix; otherwise use <filename>) for the IFI filename; default is\n\
  `<font_name>.ifi'.\n\
input-format <format>: specify format of input image; <format> must be\n\
  one of `pbm' or `img'.\n\
nchars <unsigned>: only write the first <unsigned> (approximately)\n\
  characters to the font; default is infinity.\n\
output-file <filename>: write to <filename> if <filename> has a suffix.\n\
  If <filename> doesn't have a suffix, then if writing strips, write to\n\
  <filename>sp.<dpi>gf and to <filename>.<dpi>gf if not.  By default,\n\
  use <font_name> for <filename>.\n\
print-clean-info: print gray values for the bounding boxes that are\n\
  considered for cleaning.  This implies `-verbose'.\n\
print-guidelines: print the numbers of the top and bottom rows (in that\n\
  order) of each character.  This implies `-verbose'.\n\
range <char1>-<char2>: only output characters between <char1> and\n\
  <char2>, inclusive.\n\
strips: take a constant number of scanlines as each character,\n\
  instead of using an IFI file to analyze the image.\n\
trace-scanlines: show every scanline as we read it.\n\
verbose: output progress reports.\n\
version: print the version number of this program.\n\
"

static string
read_command_line (int argc, string argv[])
{
  int g;  /* `getopt' return code.  */
  int option_index;
  boolean printed_version = false;
  struct option long_options[]
    = { { "baselines",		1, 0, 0 },
        { "designsize",		1, 0, 0 },
        { "encoding",		1, 0, 0 },
        { "epsf",		0, (int *) &output_epsf, 1 },
        { "help",               0, 0, 0 },
	{ "dpi",		1, 0, 0 },
        { "ifi-file",		1, 0, 0 },
        { "nchars",		1, 0, 0 },
        { "input-format",	1, 0, 0 },
        { "output-file",	1, 0, 0 },
        { "print-clean-info",	0, (int *) &print_clean_info, 1 },
        { "print-guidelines",	0, (int *) &print_guidelines, 1 },
        { "range",              1, 0, 0 },
        { "strips",		0, (int *) &do_strips, 1 },
        { "trace-scanlines",	0, (int *) &trace_scanlines, 1 },
        { "verbose",		0, (int *) &verbose, 1 },
        { "version",            0, (int *) &printed_version, 1 },
        { 0, 0, 0, 0 } };

  while (true)
    {
      g = getopt_long_only (argc, argv, "", long_options, &option_index);

      if (g == EOF)
        break;

      if (g == '?')
        exit (1);  /* Unknown option.  */

      assert (g == 0); /* We have no short option names.  */

      if (ARGUMENT_IS ("baselines"))
        baseline_list = scan_unsigned_list (optarg);

      else if (ARGUMENT_IS ("designsize"))
        design_size = atof (optarg);

      else if (ARGUMENT_IS ("dpi"))
	{
          image_header.hres = (two_bytes) atou (optarg);
          image_header.vres = image_header.hres;
        }

      else if (ARGUMENT_IS ("encoding"))
        encoding_name = optarg;

      else if (ARGUMENT_IS ("help"))
        {
          fprintf (stderr, "Usage: %s [options] <font_name>.\n", argv[0]);
          fprintf (stderr, USAGE);
          exit (0);
        }

      else if (ARGUMENT_IS ("ifi-file"))
        ifi_filename = optarg;

      else if (ARGUMENT_IS ("input-format"))
	{
	  if (STREQ ("pbm", optarg))
            set_pbm_input_format ();
	  else if (STREQ ("img", optarg))
            set_img_input_format ();
          else
            FATAL1 ("imageto: Unknown input format `%s'; expected one of \
`img' or `pbm'", optarg);
        }

      else if (ARGUMENT_IS ("nchars"))
        nchars_wanted = atou (optarg);

      else if (ARGUMENT_IS ("output-file"))
        output_name = optarg;

      else if (ARGUMENT_IS ("print-clean-info")
               || ARGUMENT_IS ("print-guidelines"))
        verbose = true;

      else if (ARGUMENT_IS ("range"))
        GET_RANGE (optarg, starting_char, ending_char);

      else if (ARGUMENT_IS ("version"))
        printf ("%s.\n", version_string);

      /* Else it was a flag; getopt has already done the assignment.  */
    }

  if (do_strips && output_epsf)
    FATAL ("imageto: Sorry, -strips and -epsf are mutually exclusive");

  FINISH_COMMAND_LINE ();
}

/* If the input format wasn't explicitly set on the command line,
   attempt to intuit it from FILENAME, and set the necessary variables.
   If we can't tell what the format should be, quit.  */

static void
set_input_format (string filename)
{
  string input_extension;

  /* If it's already set, just return.  */
  if (image_open_input_file != NULL)
    return;

  /* Try to guess based on FILENAME.  */
  input_extension = find_suffix (filename) ? : "";

  if (STREQ (input_extension, "img"))
    set_img_input_format ();

  else if (STREQ (input_extension, "pbm"))
    set_pbm_input_format ();

  else /* Can't guess it; quit.  */
    {
      fprintf (stderr, "You must supply an input format.\n");
      fprintf (stderr, "(I can't guess from the filename `%s'.)\n", filename);
      fprintf (stderr, "For more information, use ``-help''.\n");
      exit (1);
    }
}


/* Set up for reading a PBM file.  */

static void
set_pbm_input_format ()
{
  image_open_input_file = pbm_open_input_file;
  image_close_input_file = pbm_close_input_file;
  image_get_header = pbm_get_header;
  image_get_scanline = pbm_get_scanline;
  input_extension = "pbm";
}


/* Set up for reading an IMG file.  */

static void
set_img_input_format ()
{
  image_open_input_file = img_open_input_file;
  image_close_input_file = img_close_input_file;
  image_get_header = img_get_header;
  image_get_scanline = img_get_scanline;
  input_extension = "img";
}