|
|
1.1 root 1: #include <stdio.h>
2: #include <jstructs.h>
3: #include "pico.h"
4:
5: #define DITHSIZE 16
6: #define DITHMASK (DITHSIZE-1)
7: #define DITHMAX DITHSIZE*DITHSIZE-1
8:
9: int dith[DITHSIZE][DITHSIZE]={
10: #include "udither.data"
11: };
12: FILE *ofd;
13:
14: extern short CURSCRATCH, CUROLD;
15: extern struct SRC src[MANY];
16: extern int DEF_NL, DEF_LL;
17:
18: put0bitmap(name)
19: char *name;
20: {
21: register byte, mask, nout;
22: register unsigned char *s;
23: register int *dth;
24: unsigned char *zbuf;
25: int ll, len, ht, x, y, i;
26: short data;
27: Bitmap bp;
28:
29: if ((ofd = fopen(name, "w")) == NULL)
30: { fprintf(stderr, "pico: cannot open %s\n", name);
31: return;
32: }
33:
34: ll = DEF_LL; /* full size */
35: ht = DEF_NL;
36: /* len = 32*((ll+31)/32); */
37: len = 32*((2*ll+31)/32); /* 2*ll for 2bit/pel */
38: zbuf = (unsigned char *) Emalloc(ht*len/8);
39: nout = 0;
40: for (y = 0; y < ht; y++)
41: { byte = 0; mask = 0x80;
42: dth = dith[y&DITHMASK];
43: s = &(Old->pixred[2*y*DEF_LL]);
44: for (x = 0; x < ll; x++)
45: { data = *s++;
46: /* if ((data & DITHMAX) < dth[x&DITHMASK])
47: byte |= mask;
48: mask >>= 1;
49: if (mask == 0)
50: { zbuf[nout++] = byte;
51: byte = 0;
52: mask = 0x80;
53: }
54: */
55: data += dth[x&DITHMASK]; /* value between 0 - 511 */
56: data /= 128; /* value between 0 - 3 */
57: for (i = 2; i > 0; i = i/2)
58: { if (data&i)
59: byte |= mask;
60: mask >>= 1;
61: if (mask == 0)
62: { zbuf[nout++] = byte;
63: byte = 0;
64: mask = 0x80;
65: }
66: }
67: }
68: /* for ( ; x < len; x++)
69: */ for (x = 2*x ; x < len; x++)
70: { if (dth[x&DITHMASK])
71: byte |= mask;
72: mask >>= 1;
73: if (mask == 0)
74: { zbuf[nout++] = byte;
75: byte = 0;
76: mask = 0x80;
77: } }
78:
79: }
80: bp.base = (Word *)zbuf;
81: bp._null = (char *) 0;
82: bp.width = len/32; /* why not 16? */
83: bp.rect.origin.x = bp.rect.origin.y = 0;
84: bp.rect.corner.x = len; bp.rect.corner.y = ht;
85: outbitmap(&bp, bp.rect);
86: fflush(ofd);
87: fclose(ofd);
88: fprintf(stderr, "+: %s\n", name);
89: }
90:
91: putbitmap(name)
92: char *name;
93: {
94: register byte, mask, nout;
95: register unsigned char *s;
96: register int *dth;
97: unsigned char *zbuf;
98: int ll, len, ht, x, y;
99: short data;
100: Bitmap bp;
101:
102: if ((ofd = fopen(name, "w")) == NULL)
103: { fprintf(stderr, "pico: cannot open %s\n", name);
104: return;
105: }
106:
107: ll = DEF_LL/2; /* medium size */
108: ht = DEF_NL/2;
109: len = 32*((ll+31)/32);
110: zbuf = (unsigned char *) Emalloc(ht*len/8);
111: nout = 0;
112: for (y = 0; y < ht; y++)
113: { byte = 0; mask = 0x80;
114: dth = dith[y&DITHMASK];
115: s = &(Old->pixred[2*y*DEF_LL]);
116: for (x = 0; x < ll; x++)
117: { data = *s + *(s+DEF_LL); s++;
118: data += *s + *(s+DEF_LL); s++;
119: data >>= 2;
120: if ((data & DITHMAX) < dth[x&DITHMASK])
121: byte |= mask;
122: mask >>= 1;
123: if (mask == 0)
124: { zbuf[nout++] = byte;
125: byte = 0;
126: mask = 0x80;
127: }
128: }
129: for ( ; x < len; x++)
130: { if (dth[x&DITHMASK])
131: byte |= mask;
132: mask >>= 1;
133: if (mask == 0)
134: { zbuf[nout++] = byte;
135: byte = 0;
136: mask = 0x80;
137: } } }
138: bp.base = (Word *)zbuf;
139: bp._null = (char *) 0;
140: bp.width = len/32; /* why not 16? */
141: bp.rect.origin.x = bp.rect.origin.y = 0;
142: bp.rect.corner.x = len; bp.rect.corner.y = ht;
143: outbitmap(&bp, bp.rect);
144: fflush(ofd);
145: fclose(ofd);
146: fprintf(stderr, "+: %s\n", name);
147: }
148:
149: #define SHORTSIZE 16
150:
151: #define outword(w) (putc(w,ofd) < 0 ? -1 : putc((w)>>8,ofd))
152: #define outpoint(p) (outword((p).x) < 0 ? -1 : outword((p).y))
153: #define outrect(r) (outpoint((r).origin) < 0 ? -1 : outpoint((r).corner))
154:
155: static int ctype, count, rastwid; static short *p1;
156:
157: static short *buf; static bufwid;
158:
159: outbitmap(bp, r)
160: Bitmap *bp; Rectangle r;
161: {
162: register short *pr;
163: register y, i;
164:
165: rastwid = (r.corner.x-r.origin.x+SHORTSIZE-1)/SHORTSIZE;
166:
167: if (rastwid > bufwid) {
168: if (buf)
169: free(buf), bufwid = 0;
170: buf = (short *)Emalloc(rastwid*sizeof(short));
171: if (buf == 0)
172: return -1;
173: bufwid = rastwid;
174: }
175: if (outword(0) < 0 || outrect(r) < 0)
176: return -1;
177: pr = (short *)bp->base;
178: for (i=0; i<rastwid; i++)
179: buf[i] = 0;
180: for (y=r.origin.y; y<r.corner.y; y++)
181: { for (i=0; i<rastwid; i++)
182: buf[i] ^= pr[i];
183: if (outrast(buf) < 0)
184: return -1;
185: for (i=0; i<rastwid; i++)
186: buf[i] = pr[i];
187: pr = (short *)((Word *)pr + bp->width);
188: }
189: return 0;
190: }
191:
192: outrast(p2)
193: register short *p2;
194: {
195: short *endraster;
196: p1 = p2;
197: endraster = p2+rastwid-1;
198:
199: do {
200: if (p1 >= p2) {
201: p2=p1+1; count=2;
202: ctype=(*p1 == *p2);
203:
204: } else if ((*p2 == *(p2+1)) == ctype) {
205: if (++count >= 127) {
206: if (outbits() < 0)
207: return -1;
208: p1=p2+2;
209: } else p2++;
210:
211: } else if (ctype) {
212: if (outbits() < 0)
213: return -1;
214: p1=p2+1;
215: ctype=0;
216:
217: } else {
218: count--;
219: if (outbits() < 0)
220: return -1;
221: p1=p2;
222: ctype=1;
223: }
224: } while (p2 < endraster);
225:
226: if (p1 > endraster)
227: return 0;
228: if (p2 > endraster)
229: count--;
230: if (outbits() < 0)
231: return -1;
232: return 0;
233: }
234:
235: outbits()
236: { register c;
237: register char *pout;
238:
239: c = count;
240: if (ctype)
241: c += 128, count=1;
242: if (putc(c, ofd) < 0)
243: return -1;
244: c = sizeof(short)*count;
245: pout = (char *)p1;
246: while (--c >= 0)
247: if (putc(*pout++, ofd) < 0)
248: return -1;
249: return 0;
250: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.