Blame


1 1785490b 2012-12-01 rsc #include <u.h>
2 1785490b 2012-12-01 rsc #include <libc.h>
3 1785490b 2012-12-01 rsc #include <draw.h>
4 1785490b 2012-12-01 rsc #include <memdraw.h>
5 1785490b 2012-12-01 rsc #include "a.h"
6 1785490b 2012-12-01 rsc
7 1785490b 2012-12-01 rsc uchar pjw[] = {
8 1785490b 2012-12-01 rsc 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73,
9 1785490b 2012-12-01 rsc 0x65, 0x64, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
10 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x6b, 0x32, 0x20, 0x20,
11 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
12 1785490b 2012-12-01 rsc 0x20, 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
13 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x20, 0x20,
14 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
15 1785490b 2012-12-01 rsc 0x34, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
16 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x34, 0x38, 0x20, 0x20,
17 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
18 1785490b 2012-12-01 rsc 0x34, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
19 1785490b 2012-12-01 rsc 0x20, 0x20, 0x20, 0x35, 0x34, 0x31, 0x20, 0x80,
20 1785490b 2012-12-01 rsc 0xff, 0x20, 0x00, 0x08, 0x00, 0x82, 0xc0, 0x16,
21 1785490b 2012-12-01 rsc 0x7f, 0x04, 0x06, 0x04, 0x00, 0x83, 0xfc, 0x01,
22 1785490b 2012-12-01 rsc 0x04, 0x06, 0x04, 0x07, 0x04, 0x00, 0x83, 0xf1,
23 1785490b 2012-12-01 rsc 0x00, 0x50, 0x11, 0x04, 0x18, 0x04, 0x00, 0x84,
24 1785490b 2012-12-01 rsc 0xd4, 0x40, 0x48, 0x04, 0x05, 0x00, 0x07, 0x04,
25 1785490b 2012-12-01 rsc 0x00, 0x87, 0xc1, 0x44, 0x11, 0x04, 0x51, 0xbf,
26 1785490b 2012-12-01 rsc 0xff, 0xff, 0x04, 0x00, 0x87, 0x16, 0xfe, 0x81,
27 1785490b 2012-12-01 rsc 0x01, 0x40, 0x5f, 0xff, 0xff, 0x04, 0x00, 0x87,
28 1785490b 2012-12-01 rsc 0x5f, 0xff, 0xe4, 0x00, 0x40, 0x01, 0x1f, 0xff,
29 1785490b 2012-12-01 rsc 0x00, 0x00, 0x88, 0xfd, 0x7f, 0xff, 0xfd, 0x11,
30 1785490b 2012-12-01 rsc 0x11, 0x00, 0x3f, 0xff, 0x00, 0x00, 0x80, 0xe5,
31 1785490b 2012-12-01 rsc 0x00, 0x03, 0x84, 0x50, 0x04, 0x10, 0x42, 0xff,
32 1785490b 2012-12-01 rsc 0x00, 0x00, 0x80, 0x97, 0x00, 0x03, 0x84, 0x51,
33 1785490b 2012-12-01 rsc 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x80, 0x67,
34 1785490b 2012-12-01 rsc 0x00, 0x03, 0x84, 0xc4, 0x40, 0x41, 0x11, 0x3f,
35 1785490b 2012-12-01 rsc 0x00, 0x75, 0x80, 0x4f, 0x00, 0x0b, 0x84, 0xd0,
36 1785490b 2012-12-01 rsc 0x40, 0x00, 0x11, 0x1b, 0x82, 0xff, 0xff, 0xf5,
37 1785490b 2012-12-01 rsc 0x04, 0x40, 0x84, 0xf0, 0x00, 0x00, 0x04, 0x5f,
38 1785490b 2012-12-01 rsc 0x82, 0xff, 0xff, 0xe1, 0x04, 0x40, 0x84, 0xf4,
39 1785490b 2012-12-01 rsc 0x00, 0x00, 0x01, 0x5b, 0x00, 0x1e, 0x80, 0x2f,
40 1785490b 2012-12-01 rsc 0x04, 0x9e, 0x83, 0x00, 0x00, 0x00, 0x17, 0x82,
41 1785490b 2012-12-01 rsc 0xff, 0xff, 0x40, 0x04, 0x96, 0x84, 0xfe, 0x40,
42 1785490b 2012-12-01 rsc 0x00, 0x00, 0x0f, 0x82, 0xff, 0xfd, 0x00, 0x08,
43 1785490b 2012-12-01 rsc 0xa2, 0x00, 0x0b, 0x80, 0x5f, 0x8b, 0xff, 0xf9,
44 1785490b 2012-12-01 rsc 0x00, 0xb5, 0x11, 0x5f, 0xff, 0xa5, 0x64, 0x00,
45 1785490b 2012-12-01 rsc 0x00, 0x1f, 0x85, 0xff, 0xf4, 0x01, 0x0a, 0xf5,
46 1785490b 2012-12-01 rsc 0x42, 0x04, 0x2e, 0x81, 0x00, 0x2b, 0x87, 0xff,
47 1785490b 2012-12-01 rsc 0xf0, 0x01, 0x0f, 0xe4, 0x54, 0xf0, 0x3f, 0x00,
48 1785490b 2012-12-01 rsc 0x23, 0x80, 0x1f, 0x88, 0xff, 0xe0, 0x00, 0x4e,
49 1785490b 2012-12-01 rsc 0x00, 0x04, 0x00, 0x00, 0x14, 0x00, 0x0b, 0x85,
50 1785490b 2012-12-01 rsc 0xff, 0xd0, 0x00, 0x4c, 0x50, 0x44, 0x04, 0x22,
51 1785490b 2012-12-01 rsc 0x81, 0x00, 0x0b, 0x00, 0x0b, 0x82, 0x49, 0xa6,
52 1785490b 2012-12-01 rsc 0x6c, 0x00, 0x76, 0x00, 0x17, 0x00, 0x7d, 0x88,
53 1785490b 2012-12-01 rsc 0x5e, 0xd6, 0xf9, 0xf1, 0x51, 0x11, 0x00, 0x00,
54 1785490b 2012-12-01 rsc 0x3f, 0x88, 0xff, 0xf1, 0x02, 0x2e, 0xff, 0xf3,
55 1785490b 2012-12-01 rsc 0xf4, 0x6a, 0x98, 0x00, 0x0b, 0x8b, 0xff, 0xf3,
56 1785490b 2012-12-01 rsc 0x06, 0x2f, 0xff, 0xeb, 0xf8, 0xba, 0xa5, 0x04,
57 1785490b 2012-12-01 rsc 0x00, 0xbf, 0x8b, 0xff, 0xf5, 0x42, 0x9f, 0xff,
58 1785490b 2012-12-01 rsc 0xcf, 0xf9, 0x2b, 0xa5, 0xc0, 0x08, 0xff, 0x8b,
59 1785490b 2012-12-01 rsc 0xff, 0xfc, 0xc5, 0xfd, 0x40, 0x3f, 0xfe, 0x0b,
60 1785490b 2012-12-01 rsc 0xfb, 0x00, 0x0e, 0xff, 0x00, 0xae, 0x88, 0xfa,
61 1785490b 2012-12-01 rsc 0x96, 0xff, 0xfa, 0x81, 0x54, 0x40, 0x1b, 0xff,
62 1785490b 2012-12-01 rsc 0x00, 0x0b, 0x88, 0xfe, 0xaa, 0x96, 0x50, 0x59,
63 1785490b 2012-12-01 rsc 0x41, 0x00, 0x7b, 0xff, 0x88, 0xff, 0xff, 0x53,
64 1785490b 2012-12-01 rsc 0xff, 0xab, 0xc0, 0x00, 0x16, 0x54, 0x00, 0xba,
65 1785490b 2012-12-01 rsc 0x8b, 0xff, 0xff, 0x43, 0xfe, 0xaf, 0xe8, 0x00,
66 1785490b 2012-12-01 rsc 0x5a, 0x61, 0x41, 0xbf, 0xff, 0x8b, 0xff, 0xff,
67 1785490b 2012-12-01 rsc 0xc2, 0xfe, 0xbf, 0xfd, 0x01, 0x59, 0x94, 0x51,
68 1785490b 2012-12-01 rsc 0x1b, 0xff, 0x00, 0xef, 0x88, 0xfe, 0xeb, 0xed,
69 1785490b 2012-12-01 rsc 0x95, 0x56, 0x51, 0x40, 0x0f, 0xff, 0x8b, 0xff,
70 1785490b 2012-12-01 rsc 0xff, 0xe0, 0xfa, 0x92, 0xa6, 0x91, 0x11, 0x54,
71 1785490b 2012-12-01 rsc 0x54, 0x0f, 0xff, 0x00, 0x0b, 0x84, 0xff, 0xa9,
72 1785490b 2012-12-01 rsc 0xa4, 0x00, 0x01, 0x04, 0x17, 0x00, 0x6c, 0x88,
73 1785490b 2012-12-01 rsc 0xba, 0xaf, 0xff, 0xfd, 0x96, 0x45, 0x40, 0x3f,
74 1785490b 2012-12-01 rsc 0xff, 0x00, 0x00, 0x88, 0x2a, 0xbf, 0x6a, 0xa4,
75 1785490b 2012-12-01 rsc 0x15, 0x54, 0x60, 0xbf, 0xff, 0x00, 0x00, 0x88,
76 1785490b 2012-12-01 rsc 0xfa, 0xeb, 0x90, 0x00, 0x95, 0x41, 0xae, 0xff,
77 1785490b 2012-12-01 rsc 0xff, 0x04, 0x0b, 0x84, 0xaa, 0xa5, 0x11, 0x45,
78 1785490b 2012-12-01 rsc 0x05, 0x01, 0x49, 0x05, 0x27, 0x87, 0xae, 0xff,
79 1785490b 2012-12-01 rsc 0xf9, 0x54, 0x11, 0x6e, 0xff, 0xff, 0x0c, 0x0b,
80 1785490b 2012-12-01 rsc 0x85, 0xfa, 0x54, 0x56, 0xbb, 0xff, 0xff, 0x04,
81 1785490b 2012-12-01 rsc 0x0b, 0x87, 0xab, 0xbf, 0xf6, 0x54, 0x43, 0xaf,
82 1785490b 2012-12-01 rsc 0xff, 0xff, 0x04, 0x00, 0x87, 0x5a, 0xaa, 0x95,
83 1785490b 2012-12-01 rsc 0x11, 0x46, 0xfb, 0xff, 0xff, 0x04, 0x00, 0x84,
84 1785490b 2012-12-01 rsc 0xaa, 0x55, 0x10, 0x45, 0x07, 0x00, 0x17, 0x04,
85 1785490b 2012-12-01 rsc 0x00, 0x84, 0x95, 0x54, 0x45, 0x14, 0x06, 0x00,
86 1785490b 2012-12-01 rsc 0x5e, 0x04, 0x00, 0x87, 0xa5, 0x55, 0x50, 0x40,
87 1785490b 2012-12-01 rsc 0x12, 0xff, 0xbf, 0xff,
88 1785490b 2012-12-01 rsc };
89 1785490b 2012-12-01 rsc
90 1785490b 2012-12-01 rsc static Memimage* resample(int, int, Memimage*);
91 1785490b 2012-12-01 rsc
92 1785490b 2012-12-01 rsc void
93 1785490b 2012-12-01 rsc drawpjw(Memimage *m, Fontchar *fc, int x, int width, int height, int ascent)
94 1785490b 2012-12-01 rsc {
95 1785490b 2012-12-01 rsc int w, p[2];
96 1785490b 2012-12-01 rsc Memimage *pm, *pm1, *pm2;
97 1785490b 2012-12-01 rsc Rectangle r;
98 1785490b 2012-12-01 rsc int margin;
99 fa325e9b 2020-01-10 cross
100 1785490b 2012-12-01 rsc w = width;
101 1785490b 2012-12-01 rsc
102 1785490b 2012-12-01 rsc // assume margin below baseline is
103 1785490b 2012-12-01 rsc // approximately how much room we should preserve at top above peter too
104 1785490b 2012-12-01 rsc margin = height - ascent;
105 1785490b 2012-12-01 rsc if(w > ascent-margin)
106 1785490b 2012-12-01 rsc w = ascent-margin;
107 1785490b 2012-12-01 rsc
108 1785490b 2012-12-01 rsc if(w < 9)
109 1785490b 2012-12-01 rsc w = 9;
110 1785490b 2012-12-01 rsc fc->width = w;
111 1785490b 2012-12-01 rsc
112 1785490b 2012-12-01 rsc pipe(p);
113 1785490b 2012-12-01 rsc write(p[1], pjw, sizeof pjw);
114 1785490b 2012-12-01 rsc close(p[1]);
115 1785490b 2012-12-01 rsc pm = readmemimage(p[0]);
116 1785490b 2012-12-01 rsc close(p[0]);
117 1785490b 2012-12-01 rsc if(pm == nil)
118 1785490b 2012-12-01 rsc goto Error;
119 fa325e9b 2020-01-10 cross
120 1785490b 2012-12-01 rsc pm1 = allocmemimage(pm->r, GREY8);
121 1785490b 2012-12-01 rsc memimagedraw(pm1, pm1->r, pm, pm->r.min, nil, ZP, S);
122 1785490b 2012-12-01 rsc pm2 = resample(w, w, pm1);
123 1785490b 2012-12-01 rsc memimagedraw(m, Rect(x, ascent-w, x+w, ascent), memwhite, ZP, nil, ZP, SoverD);
124 1785490b 2012-12-01 rsc memimagedraw(m, Rect(x, ascent-w, x+w, ascent), memblack, ZP, pm2, pm2->r.min, SoverD);
125 1785490b 2012-12-01 rsc freememimage(pm2);
126 1785490b 2012-12-01 rsc freememimage(pm1);
127 1785490b 2012-12-01 rsc freememimage(pm);
128 1785490b 2012-12-01 rsc return;
129 1785490b 2012-12-01 rsc
130 1785490b 2012-12-01 rsc Error:
131 1785490b 2012-12-01 rsc // draw a box
132 1785490b 2012-12-01 rsc r = Rect(x, 0, x+width, height);
133 1785490b 2012-12-01 rsc memimagedraw(m, r, memwhite, ZP, nil, ZP, S);
134 1785490b 2012-12-01 rsc memimagedraw(m, insetrect(r, 3), memblack, ZP, nil, ZP, S);
135 1785490b 2012-12-01 rsc return;
136 1785490b 2012-12-01 rsc }
137 1785490b 2012-12-01 rsc
138 1785490b 2012-12-01 rsc #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
139 1785490b 2012-12-01 rsc #define NK (2*K2+1)
140 1785490b 2012-12-01 rsc static double K[NK];
141 1785490b 2012-12-01 rsc
142 1785490b 2012-12-01 rsc static void
143 1785490b 2012-12-01 rsc resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
144 1785490b 2012-12-01 rsc {
145 1785490b 2012-12-01 rsc int i, x, k;
146 1785490b 2012-12-01 rsc double X, xx, v, rat;
147 1785490b 2012-12-01 rsc
148 1785490b 2012-12-01 rsc
149 1785490b 2012-12-01 rsc rat = (double)inx/(double)outx;
150 1785490b 2012-12-01 rsc for(x=0; x<outx; x++){
151 1785490b 2012-12-01 rsc if(inx == outx){
152 1785490b 2012-12-01 rsc /* don't resample if size unchanged */
153 1785490b 2012-12-01 rsc out[off+x*d] = in[off+x*d];
154 1785490b 2012-12-01 rsc continue;
155 1785490b 2012-12-01 rsc }
156 1785490b 2012-12-01 rsc v = 0.0;
157 1785490b 2012-12-01 rsc X = x*rat;
158 1785490b 2012-12-01 rsc for(k=-K2; k<=K2; k++){
159 1785490b 2012-12-01 rsc xx = X + rat*k/10.;
160 1785490b 2012-12-01 rsc i = xx;
161 1785490b 2012-12-01 rsc if(i < 0)
162 1785490b 2012-12-01 rsc i = 0;
163 1785490b 2012-12-01 rsc if(i >= inx)
164 1785490b 2012-12-01 rsc i = inx-1;
165 1785490b 2012-12-01 rsc v += in[off+i*d] * K[K2+k];
166 1785490b 2012-12-01 rsc }
167 1785490b 2012-12-01 rsc out[off+x*d] = v;
168 1785490b 2012-12-01 rsc }
169 1785490b 2012-12-01 rsc }
170 1785490b 2012-12-01 rsc
171 1785490b 2012-12-01 rsc static void
172 1785490b 2012-12-01 rsc resampley(uchar **in, int off, int iny, uchar **out, int outy)
173 1785490b 2012-12-01 rsc {
174 1785490b 2012-12-01 rsc int y, i, k;
175 1785490b 2012-12-01 rsc double Y, yy, v, rat;
176 1785490b 2012-12-01 rsc
177 1785490b 2012-12-01 rsc rat = (double)iny/(double)outy;
178 1785490b 2012-12-01 rsc for(y=0; y<outy; y++){
179 1785490b 2012-12-01 rsc if(iny == outy){
180 1785490b 2012-12-01 rsc /* don't resample if size unchanged */
181 1785490b 2012-12-01 rsc out[y][off] = in[y][off];
182 1785490b 2012-12-01 rsc continue;
183 1785490b 2012-12-01 rsc }
184 1785490b 2012-12-01 rsc v = 0.0;
185 1785490b 2012-12-01 rsc Y = y*rat;
186 1785490b 2012-12-01 rsc for(k=-K2; k<=K2; k++){
187 1785490b 2012-12-01 rsc yy = Y + rat*k/10.;
188 1785490b 2012-12-01 rsc i = yy;
189 1785490b 2012-12-01 rsc if(i < 0)
190 1785490b 2012-12-01 rsc i = 0;
191 1785490b 2012-12-01 rsc if(i >= iny)
192 1785490b 2012-12-01 rsc i = iny-1;
193 1785490b 2012-12-01 rsc v += in[i][off] * K[K2+k];
194 1785490b 2012-12-01 rsc }
195 1785490b 2012-12-01 rsc out[y][off] = v;
196 1785490b 2012-12-01 rsc }
197 1785490b 2012-12-01 rsc
198 1785490b 2012-12-01 rsc }
199 1785490b 2012-12-01 rsc
200 1785490b 2012-12-01 rsc static int
201 1785490b 2012-12-01 rsc max(int a, int b)
202 1785490b 2012-12-01 rsc {
203 1785490b 2012-12-01 rsc if(a > b)
204 1785490b 2012-12-01 rsc return a;
205 1785490b 2012-12-01 rsc return b;
206 1785490b 2012-12-01 rsc }
207 1785490b 2012-12-01 rsc
208 1785490b 2012-12-01 rsc static double
209 1785490b 2012-12-01 rsc fac(int L)
210 1785490b 2012-12-01 rsc {
211 1785490b 2012-12-01 rsc int i, f;
212 1785490b 2012-12-01 rsc
213 1785490b 2012-12-01 rsc f = 1;
214 1785490b 2012-12-01 rsc for(i=L; i>1; --i)
215 1785490b 2012-12-01 rsc f *= i;
216 1785490b 2012-12-01 rsc return f;
217 1785490b 2012-12-01 rsc }
218 1785490b 2012-12-01 rsc
219 fa325e9b 2020-01-10 cross /*
220 1785490b 2012-12-01 rsc * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
221 1785490b 2012-12-01 rsc * There are faster ways to calculate this, but we precompute
222 1785490b 2012-12-01 rsc * into a table so let's keep it simple.
223 1785490b 2012-12-01 rsc */
224 1785490b 2012-12-01 rsc static double
225 1785490b 2012-12-01 rsc i0(double x)
226 1785490b 2012-12-01 rsc {
227 1785490b 2012-12-01 rsc double v;
228 1785490b 2012-12-01 rsc int L;
229 1785490b 2012-12-01 rsc
230 1785490b 2012-12-01 rsc v = 1.0;
231 1785490b 2012-12-01 rsc for(L=1; L<10; L++)
232 1785490b 2012-12-01 rsc v += pow(x/2., 2*L)/pow(fac(L), 2);
233 1785490b 2012-12-01 rsc return v;
234 1785490b 2012-12-01 rsc }
235 1785490b 2012-12-01 rsc
236 1785490b 2012-12-01 rsc static double
237 1785490b 2012-12-01 rsc kaiser(double x, double tau, double alpha)
238 1785490b 2012-12-01 rsc {
239 1785490b 2012-12-01 rsc if(fabs(x) > tau)
240 1785490b 2012-12-01 rsc return 0.;
241 1785490b 2012-12-01 rsc return i0(alpha*sqrt(1-(x*x/(tau*tau))))/i0(alpha);
242 1785490b 2012-12-01 rsc }
243 1785490b 2012-12-01 rsc
244 1785490b 2012-12-01 rsc
245 1785490b 2012-12-01 rsc static Memimage*
246 1785490b 2012-12-01 rsc resample(int xsize, int ysize, Memimage *m)
247 1785490b 2012-12-01 rsc {
248 1785490b 2012-12-01 rsc int i, j, bpl, nchan;
249 1785490b 2012-12-01 rsc Memimage *new;
250 1785490b 2012-12-01 rsc uchar **oscan, **nscan;
251 1785490b 2012-12-01 rsc double v;
252 1785490b 2012-12-01 rsc
253 1785490b 2012-12-01 rsc for(i=-K2; i<=K2; i++){
254 1785490b 2012-12-01 rsc K[K2+i] = kaiser(i/10., K2/10., 4.);
255 1785490b 2012-12-01 rsc /* print("%g %g\n", i/10., K[K2+i]); */
256 1785490b 2012-12-01 rsc }
257 1785490b 2012-12-01 rsc
258 1785490b 2012-12-01 rsc /* normalize */
259 1785490b 2012-12-01 rsc v = 0.0;
260 1785490b 2012-12-01 rsc for(i=0; i<NK; i++)
261 1785490b 2012-12-01 rsc v += K[i];
262 1785490b 2012-12-01 rsc for(i=0; i<NK; i++)
263 1785490b 2012-12-01 rsc K[i] /= v;
264 1785490b 2012-12-01 rsc
265 1785490b 2012-12-01 rsc new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
266 1785490b 2012-12-01 rsc if(new == nil)
267 1785490b 2012-12-01 rsc sysfatal("can't allocate new image: %r");
268 1785490b 2012-12-01 rsc
269 1785490b 2012-12-01 rsc oscan = malloc(Dy(m->r)*sizeof(uchar*));
270 1785490b 2012-12-01 rsc nscan = malloc(max(ysize, Dy(m->r))*sizeof(uchar*));
271 1785490b 2012-12-01 rsc if(oscan == nil || nscan == nil)
272 1785490b 2012-12-01 rsc sysfatal("can't allocate: %r");
273 1785490b 2012-12-01 rsc
274 1785490b 2012-12-01 rsc /* unload original image into scan lines */
275 1785490b 2012-12-01 rsc bpl = bytesperline(m->r, m->depth);
276 1785490b 2012-12-01 rsc for(i=0; i<Dy(m->r); i++){
277 1785490b 2012-12-01 rsc oscan[i] = malloc(bpl);
278 1785490b 2012-12-01 rsc if(oscan[i] == nil)
279 1785490b 2012-12-01 rsc sysfatal("can't allocate: %r");
280 1785490b 2012-12-01 rsc j = unloadmemimage(m, Rect(m->r.min.x, m->r.min.y+i, m->r.max.x, m->r.min.y+i+1), oscan[i], bpl);
281 1785490b 2012-12-01 rsc if(j != bpl)
282 1785490b 2012-12-01 rsc sysfatal("unloadmemimage");
283 1785490b 2012-12-01 rsc }
284 1785490b 2012-12-01 rsc
285 1785490b 2012-12-01 rsc /* allocate scan lines for destination. we do y first, so need at least Dy(m->r) lines */
286 1785490b 2012-12-01 rsc bpl = bytesperline(Rect(0, 0, xsize, Dy(m->r)), m->depth);
287 1785490b 2012-12-01 rsc for(i=0; i<max(ysize, Dy(m->r)); i++){
288 1785490b 2012-12-01 rsc nscan[i] = malloc(bpl);
289 1785490b 2012-12-01 rsc if(nscan[i] == nil)
290 1785490b 2012-12-01 rsc sysfatal("can't allocate: %r");
291 1785490b 2012-12-01 rsc }
292 1785490b 2012-12-01 rsc
293 1785490b 2012-12-01 rsc /* resample in X */
294 1785490b 2012-12-01 rsc nchan = m->depth/8;
295 1785490b 2012-12-01 rsc for(i=0; i<Dy(m->r); i++){
296 1785490b 2012-12-01 rsc for(j=0; j<nchan; j++){
297 1785490b 2012-12-01 rsc if(j==0 && m->chan==XRGB32)
298 1785490b 2012-12-01 rsc continue;
299 1785490b 2012-12-01 rsc resamplex(oscan[i], j, nchan, Dx(m->r), nscan[i], xsize);
300 1785490b 2012-12-01 rsc }
301 1785490b 2012-12-01 rsc free(oscan[i]);
302 1785490b 2012-12-01 rsc oscan[i] = nscan[i];
303 1785490b 2012-12-01 rsc nscan[i] = malloc(bpl);
304 1785490b 2012-12-01 rsc if(nscan[i] == nil)
305 1785490b 2012-12-01 rsc sysfatal("can't allocate: %r");
306 1785490b 2012-12-01 rsc }
307 1785490b 2012-12-01 rsc
308 1785490b 2012-12-01 rsc /* resample in Y */
309 1785490b 2012-12-01 rsc for(i=0; i<xsize; i++)
310 1785490b 2012-12-01 rsc for(j=0; j<nchan; j++)
311 1785490b 2012-12-01 rsc resampley(oscan, nchan*i+j, Dy(m->r), nscan, ysize);
312 1785490b 2012-12-01 rsc
313 1785490b 2012-12-01 rsc /* pack data into destination */
314 1785490b 2012-12-01 rsc bpl = bytesperline(new->r, m->depth);
315 1785490b 2012-12-01 rsc for(i=0; i<ysize; i++){
316 1785490b 2012-12-01 rsc j = loadmemimage(new, Rect(0, i, xsize, i+1), nscan[i], bpl);
317 1785490b 2012-12-01 rsc if(j != bpl)
318 1785490b 2012-12-01 rsc sysfatal("loadmemimage: %r");
319 1785490b 2012-12-01 rsc }
320 1785490b 2012-12-01 rsc return new;
321 1785490b 2012-12-01 rsc }