about summary refs log tree commit diff
path: root/converter/pbm/pbmtozinc.c
blob: a89b8c9fa6f698efd5230c0b1183d72e340634d6 (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
/* pbmtozinc.c - read a PBM image and produce a bitmap file
**               in the format used by the Zinc Interface Library (v1.0)
**               November 1990.
**
** Author: James Darrell McCauley
**         Department of Agricultural Engineering
**         Texas A&M University
**         College Station, Texas 77843-2117 USA
**
** Copyright (C) 1988 by James Darrell McCauley (jdm5548@diamond.tamu.edu)
**                    and Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdio.h>
#include <string.h>

#include "mallocvar.h"
#include "nstring.h"
#include "pbm.h"

static void
parseCommandLine(int           const argc,
                 const char ** const argv,
                 const char ** const inputFileNameP) {

    if (argc-1 > 0) {
        *inputFileNameP = argv[1];

        if (argc-1 > 1)
            pm_error("To many arguments: %u.  "
                     "The only possible argument is the "
                     "name of the input file", argc-1);
    } else
        *inputFileNameP = "-";
}



static const char *
imageName(const char * const inputFileName) {
/*----------------------------------------------------------------------------
   The image name to put in the Zinc file, based on the input file name
   'inputFileName' ("-" to indicate Standard Input).

   Result is newly malloc'ed space that Caller must free.
-----------------------------------------------------------------------------*/
    const char * retval;

    if (streq(inputFileName, "-"))
        pm_asprintf(&retval, "noname");
    else {
        char * nameBuf;
        char * cp;

        MALLOCARRAY_NOFAIL(nameBuf, strlen(inputFileName) + 1);

        strcpy(nameBuf, inputFileName);

        cp = strchr(nameBuf, '.' );
        if (cp)
            *cp = '\0';

        retval = nameBuf;
    }
    return retval;
}



typedef struct {
    unsigned int itemsperline;
    uint16_t     item;
    unsigned int firstitem;
} Packer;



static void
packer_init(Packer * const packerP) {

    packerP->itemsperline = 0;
    packerP->firstitem = 1;
}



static void
packer_putitem(Packer *      const packerP,
               unsigned char const hi,
               unsigned char const lo) {

    if (packerP->firstitem)
        packerP->firstitem = 0;
    else
        putchar(',');

    if (packerP->itemsperline == 11) {
        putchar('\n');
        packerP->itemsperline = 0;
    }
    if (packerP->itemsperline == 0)
        putchar(' ');

    ++packerP->itemsperline;

    printf ("0x%02x%02x", hi, lo);

}



static void
writeRaster(FILE *       const ifP,
            unsigned int const rows,
            unsigned int const cols,
            int          const format) {

    unsigned char * const bitrow = pbm_allocrow_packed(cols + 8);

    Packer packer;
    unsigned int row;

    packer_init(&packer);

    bitrow[pbm_packed_bytes(cols+8) -1 ] = 0x00;

    for (row = 0; row < rows; ++row) {
        unsigned int const itemCt = (cols + 15 ) / 16;

        unsigned int i;

        pbm_readpbmrow_packed(ifP, bitrow, cols, format);

        pbm_cleanrowend_packed(bitrow, cols);

        for (i = 0; i < itemCt; ++i) {
            packer_putitem(&packer, bitrow[2*i+0], bitrow[2*i+1]);
        }
    }
    pbm_freerow_packed(bitrow);
}



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

    const char * inputFileName;
    FILE * ifP;
    int rows, cols;
    int format;
    const char * name;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &inputFileName);

    ifP = pm_openr(inputFileName);

    name = imageName(inputFileName);

    pbm_readpbminit(ifP, &cols, &rows, &format);

    printf("USHORT %s[] = {\n", name);
    printf("  %d\n", cols);
    printf("  %d\n", rows);

    writeRaster(ifP, rows, cols, format);

    printf("};\n");

    pm_close(ifP);

    pm_strfree(name);

    return 0;
}