about summary refs log tree commit diff
path: root/converter/pbm/pbmtomda.c
blob: 3ad5149983edb9241a565461fe4884011ccefa78 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201

/***************************************************************************

    PBMTOMDA: Convert portable bitmap to Microdesign area
    Copyright (C) 1999,2004 John Elliott <jce@seasip.demon.co.uk>

    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 2 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

******************************************************************************/

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

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

/* I'm being somewhat conservative in the PBM -> MDA translation. I output 
 * only the MD2 format and don't allow RLE over the ends of lines.
 */

typedef unsigned char mdbyte;

static FILE *infile;
static mdbyte header[128];
static int bInvert = 0;
static int bScale  = 0;

/* Encode 8 pixels as a byte */

static mdbyte 
encode(bit ** const bits, int const row, int const col)
{
    int n;
    int mask;
    mdbyte b;

    mask = 0x80;   /* initial value */
    b = 0;  /* initial value */

    for (n = 0; n < 8; n++) {
        if (bits[row][col+n] == PBM_BLACK) b |= mask;
        mask = mask >> 1;
    }
    return b;
}

/* Translate a pbm to MD2 format, one row at a time */

static void 
do_translation(bit ** const bits, 
               int    const nOutCols, 
               int    const nOutRows,
               int    const nInRows)
{
    int row;
    mdbyte *mdrow;  /* malloc'ed */

    int const step = bScale ? 2 : 1;

    MALLOCARRAY(mdrow, nOutCols);

    if (mdrow == NULL)
        pm_error("Not enough memory for conversion.");

    for (row = 0; row < nOutRows; row+=step)
    {
        int col;
        int x1;

        /* Encode image into non-compressed bitmap */
        for (col = 0; col < nOutCols; ++col) {
            mdbyte b;

            if (row < nInRows)
                b = encode(bits, row, col*8);
            else
                b = 0xff;  /* All black */

            mdrow[col] = bInvert ? b : ~b;
        }

        /* Encoded. Now RLE it */
        for (col = 0; col < nOutCols; )
        {
            mdbyte const b = mdrow[col];

            if (b != 0xFF && b != 0) /* Normal byte */
            {
                putchar(b);
                ++col;
            }
            else    /* RLE a run of 0s or 0xFFs */
            {
                for (x1 = col; x1 < nOutCols; x1++)
                {
                    if (mdrow[x1] != b) break;
                    if (x1 - col > 256) break;
                }
                x1 -= col;    /* x1 = no. of repeats */
                if (x1 == 256) x1 = 0;
                putchar(b);
                putchar(x1);
                col += x1;        
            }   
        }
    }
    free(mdrow);
}


static void usage(char *s)
{        
    printf("pbmtomda v1.01, Copyright (C) 1999,2004 John Elliott <jce@seasip.demon.co.uk>\n"
         "This program is redistributable under the terms of the GNU General Public\n"
                 "License, version 2 or later.\n\n"
                 "Usage: %s [ -d ] [ -i ] [ -- ] [ infile ]\n\n"
                 "-d: Halve height (to compensate for the PCW aspect ratio)\n"
                 "-i: Invert colors\n"
                 "--: No more options (use if filename begins with a dash)\n",
        s);

    exit(0);
}

int main(int argc, char **argv)
{
    int nOutRowsUnrounded;  /* Before rounding up to multiple of 4 */
    int nOutCols, nOutRows;
    int nInCols, nInRows;
    bit **bits;
    int rc;

    int n, optstop = 0;
    char *fname = NULL;

    pbm_init(&argc, argv);

    /* Output v2-format MDA images. Simulate MDA header...
     * 2004-01-11: Hmm. Apparently some (but not all) MDA-reading 
     * programs insist on the program identifier being exactly 
     * 'MicroDesignPCW'. The spec does not make this clear. */
    strcpy((char*) header, ".MDAMicroDesignPCWv1.00\r\npbm2mda\r\n");

    for (n = 1; n < argc; n++)
    {
        if (argv[n][0] == '-' && !optstop)
        {   
            if (argv[n][1] == 'd' || argv[n][1] == 'D') bScale = 1;
            if (argv[n][1] == 'i' || argv[n][1] == 'I') bInvert = 1;
            if (argv[n][1] == 'h' || argv[n][1] == 'H') usage(argv[0]);
            if (argv[n][1] == '-' && argv[n][2] == 0 && !fname)     /* "--" */
            {
                optstop = 1;
            }
            if (argv[n][1] == '-' && (argv[n][2] == 'h' || argv[n][2] == 'H')) usage(argv[0]);
        }
        else if (argv[n][0] && !fname)  /* Filename */
        {
            fname = argv[n];
        }
    }

    if (fname) infile = pm_openr(fname);
    else       infile = stdin;

    bits = pbm_readpbm(infile, &nInCols, &nInRows);
    
    nOutRowsUnrounded = bScale ? nInRows/2 : nInRows;

    nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4;
        /* MDA wants rows a multiple of 4 */   
    nOutCols = nInCols / 8;

    rc = fwrite(header, 1, 128, stdout);
    if (rc < 128)
        pm_error("Unable to write header to output file.  errno=%d (%s)",
                 errno, strerror(errno));

    pm_writelittleshort(stdout, nOutRows);
    pm_writelittleshort(stdout, nOutCols);

    do_translation(bits, nOutCols, nOutRows, nInRows);

    pm_close(infile);
    fflush(stdout);
    pbm_freearray(bits, nInRows);
    
    return 0;
}