about summary refs log tree commit diff
path: root/converter/pbm/pbmtobbnbg.c
blob: f9191a4973bf234e0598d00a0607c25bc1bc9425 (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
/* pbmtobg.c - read a portable bitmap and produce BitGraph graphics
**
** Copyright 1989 by Mike Parker.
**
** 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.
*/

/*
** Changed to take advantage of negative Packed Pixed Data values and
** supply ANSI-standard string terminator.  Paul Milazzo, 28 May 1990.
*/

#include "pbm.h"

static void write16 ARGS(( unsigned int ));

static int nco;

int
main(argc,argv)
int argc;
char **argv;
{
 int rows;
 int cols;
 int format;
 bit *bitrow;
 int row;
 unsigned int sixteen;
 int i;
 unsigned int mask;
 int op;
 int x;
 int y;


 pbm_init( &argc, argv );

 op = 3;
 switch (argc)
  { case 1:
       break;
    case 2:
       op = atoi(argv[1]);
       break;
    case 3:
       x = atoi(argv[1]);
       y = atoi(argv[2]);
       printf("\33:%d;%dm",x,y);
       break;
    case 4:
       op = atoi(argv[1]);
       x = atoi(argv[2]);
       y = atoi(argv[3]);
       printf("\33:%d;%dm",x,y);
       break;
  }
 nco = 0;
 pbm_readpbminit(stdin,&cols,&rows,&format);
 printf("\33P:%d;%d;%ds\n",op,cols,rows);
 bitrow = pbm_allocrow(cols);
 for (row=0;row<rows;row++)
  { pbm_readpbmrow(stdin,bitrow,cols,format);
    sixteen = 0;
    mask = 0x8000;
    for (i=0;i<cols;i++)
     { if (bitrow[i]==PBM_BLACK) sixteen |= mask;
       mask >>= 1;
       if (mask == 0)
        { mask = 0x8000;
          write16(sixteen);
          sixteen = 0;
        }
     }
    if (mask != 0x8000)
     { write16(sixteen);
     }
  }
 puts("\033\\");
 exit(0);
}



#ifdef POSITIVE_VALUES_ONLY
static void
write16(sixteen)
unsigned int sixteen;
{
 if (nco > 75)
  { putchar('\n');
    nco = 0;
  }
 if (sixteen & 0xfc00)
  { putchar(0100+(sixteen>>10));
    nco ++;
  }
 if (sixteen & 0xfff0)
  { putchar(0100+((sixteen>>4)&0x3f));
    nco ++;
  }
 putchar(060+(sixteen&0xf));
 nco ++;
}
#else
/*
 *  This version of "write16" uses negative Packed Pixel Data values to
 *  represent numbers in the range 0x7fff--0xffff; negative values will
 *  require fewer characters as they approach the upper end of that range.
 */
static void
write16 (word)
unsigned int    word;
{
    int         high;
    int         mid;
    int         low;
    int         signChar;

    if (nco > 75) {
        putchar ('\n');
        nco = 0;
    }

    if (word > 0x7fff) {
        word = (unsigned int) (0x10000L - (long) word);
        signChar = ' ';
    }
    else
        signChar = '0';

    high = (word >> 10) + '@';
    mid = ((word & 0x3f0) >> 4) + '@';
    low = (word & 0xf) + signChar;

    if (high != '@') {
        printf ("%c%c%c", high, mid, low);
        nco += 3;
    }
    else if (mid != '@') {
        printf ("%c%c", mid, low);
        nco += 2;
    }
    else {
        putchar (low);
        nco++;
    }
}
#endif