about summary refs log tree commit diff
path: root/converter/other/pamtompfont.c
blob: dd92fc7c41cd975d33856812f65f7d1f9a9ce244 (plain) (blame)
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
/*----------------------------------------------------------------------------
                               pamtompfont
------------------------------------------------------------------------------
  Part of the Netpbm package.

  Convert a PAM image to an Mplayer bitmap font.

  It is obvious that this format was designed to be an image format and
  adopted by Mplayer for it's fonts (before Mplayer got the ability to
  use Freetype to read standard font formats such as TrueType).  But
  I have no idea what the format was originally.

  In the Mplayer font subset of the format, the image is always grayscale
  (one byte per pixel) with no palette.

  By Bryan Henderson, San Jose CA 2008.05.18

  Contributed to the public domain by its author.
-----------------------------------------------------------------------------*/

#include <string.h>
#include <assert.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "pm.h"
#include "pam.h"

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilename;
};


static void
parseCommandLine(int argc, char ** argv,
                 struct cmdlineInfo *cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */

    OPTENTINIT;

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */

    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (argc-1 == 0)
        cmdlineP->inputFilename = "-";
    else if (argc-1 != 1)
        pm_error("Program takes zero or one argument (filename).  You "
                 "specified %d", argc-1);
    else
        cmdlineP->inputFilename = argv[1];

    free(option_def);
}



static void
validateInput(struct pam * const inpamP) {

    /* The image format does provide for RGB images, but Mplayer doesn't
       understand that format (and doesn't even recognize it as something
       it doesn't understand)
    */

    if (inpamP->depth != 1)
        pm_error("Input must have depth 1.  This image's depth is %u",
                 inpamP->depth);
}



static void
writeMpFontHeader(FILE *       const ofP,
                  struct pam * const inpamP) {
/*----------------------------------------------------------------------------
   Write the 32 byte header.
-----------------------------------------------------------------------------*/
    fwrite("mhwanh", 1, 6, ofP);  /* Signature */

    fputc(0, ofP);  /* pad */
    fputc(0, ofP);  /* pad */

    /* Write the old 16 bit width field.  Zero means use the 32 bit one
       below instead.
    */
    pm_writebigshort(ofP, 0);

    /* Height */
    pm_writebigshort(ofP, inpamP->height);

    /* Number of colors in palette.  Zero means not paletted image */
    pm_writebigshort(ofP, 0);

    {
        unsigned int i;
        for (i = 0; i < 14; ++i)
            fputc(0, ofP);  /* pad */
    }
    /* Width */
    pm_writebiglong(ofP, inpamP->width);
}



static void
convertRaster(struct pam * const inpamP,
              FILE *       const ofP) {

    tuple * tuplerow;
    unsigned char * outrow;
    unsigned int row;

    assert(inpamP->depth == 1);

    tuplerow = pnm_allocpamrow(inpamP);

    MALLOCARRAY(outrow, inpamP->width);

    if (outrow == NULL)
        pm_error("Unable to allocate space for a %u-column output buffer",
                 inpamP->width);

    for (row = 0; row < inpamP->height; ++row) {
        unsigned int col;

        pnm_readpamrow(inpamP, tuplerow);

        for (col = 0; col < inpamP->width; ++col) {
            outrow[col] =
                pnm_scalesample(tuplerow[col][0], inpamP->maxval, 255);
        }

        fwrite(outrow, 1, inpamP->width, ofP);
    }
    free(outrow);
    pnm_freepamrow(tuplerow);
}



int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    struct pam inpam;   /* Input PAM image */

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilename);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    validateInput(&inpam);

    writeMpFontHeader(stdout, &inpam);

    convertRaster(&inpam, stdout);

    return 0;
}