blob: a0770a5608564f1536b243815d9d4ec0cdf1fc8b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27/*
28 * DESCRIPTION
29 * Calculates cliping boundary for Affine functions.
30 *
31 */
32
33#include "mlib_image.h"
34#include "mlib_SysMath.h"
35#include "mlib_ImageAffine.h"
36
37/***************************************************************/
38mlib_status mlib_AffineEdges(mlib_affine_param *param,
39 const mlib_image *dst,
40 const mlib_image *src,
41 void *buff_lcl,
42 mlib_s32 buff_size,
43 mlib_s32 kw,
44 mlib_s32 kh,
45 mlib_s32 kw1,
46 mlib_s32 kh1,
47 mlib_edge edge,
48 const mlib_d64 *mtx,
49 mlib_s32 shiftx,
50 mlib_s32 shifty)
51{
52 mlib_u8 *buff = buff_lcl;
53 mlib_u8 **lineAddr = param->lineAddr;
54 mlib_s32 srcWidth, dstWidth, srcHeight, dstHeight, srcYStride, dstYStride;
55 mlib_s32 *leftEdges, *rightEdges, *xStarts, *yStarts, bsize0, bsize1 = 0;
56 mlib_u8 *srcData, *dstData;
57 mlib_u8 *paddings;
58 void *warp_tbl = NULL;
59 mlib_s32 yStart = 0, yFinish = -1, dX, dY;
60
61 mlib_d64 xClip, yClip, wClip, hClip;
62 mlib_d64 delta = 0.;
63 mlib_d64 minX, minY, maxX, maxY;
64
65 mlib_d64 coords[4][2];
66 mlib_d64 a = mtx[0], b = mtx[1], tx = mtx[2], c = mtx[3], d = mtx[4], ty = mtx[5];
67 mlib_d64 a2, b2, tx2, c2, d2, ty2;
68 mlib_d64 dx, dy, div;
69 mlib_s32 sdx, sdy;
70 mlib_d64 dTop;
71 mlib_d64 val0;
72 mlib_s32 top, bot;
73 mlib_s32 topIdx, max_xsize = 0;
74 mlib_s32 i, j, t;
75
76 srcData = mlib_ImageGetData(src);
77 dstData = mlib_ImageGetData(dst);
78 srcWidth = mlib_ImageGetWidth(src);
79 srcHeight = mlib_ImageGetHeight(src);
80 dstWidth = mlib_ImageGetWidth(dst);
81 dstHeight = mlib_ImageGetHeight(dst);
82 srcYStride = mlib_ImageGetStride(src);
83 dstYStride = mlib_ImageGetStride(dst);
84 paddings = mlib_ImageGetPaddings(src);
85
86 if (srcWidth >= (1 << 15) || srcHeight >= (1 << 15)) {
87 return MLIB_FAILURE;
88 }
89
90 div = a * d - b * c;
91
92 if (div == 0.0) {
93 return MLIB_FAILURE;
94 }
95
96 bsize0 = (dstHeight * sizeof(mlib_s32) + 7) & ~7;
97
98 if (lineAddr == NULL) {
99 bsize1 = ((srcHeight + 4 * kh) * sizeof(mlib_u8 *) + 7) & ~7;
100 }
101
102 param->buff_malloc = NULL;
103
104 if ((4 * bsize0 + bsize1) > buff_size) {
105 buff = param->buff_malloc = mlib_malloc(4 * bsize0 + bsize1);
106
107 if (buff == NULL)
108 return MLIB_FAILURE;
109 }
110
111 leftEdges = (mlib_s32 *) (buff);
112 rightEdges = (mlib_s32 *) (buff += bsize0);
113 xStarts = (mlib_s32 *) (buff += bsize0);
114 yStarts = (mlib_s32 *) (buff += bsize0);
115
116 if (lineAddr == NULL) {
117 mlib_u8 *srcLinePtr = srcData;
118 lineAddr = (mlib_u8 **) (buff += bsize0);
119 for (i = 0; i < 2 * kh; i++)
120 lineAddr[i] = srcLinePtr;
121 lineAddr += 2 * kh;
122 for (i = 0; i < srcHeight - 1; i++) {
123 lineAddr[i] = srcLinePtr;
124 srcLinePtr += srcYStride;
125 }
126
127 for (i = srcHeight - 1; i < srcHeight + 2 * kh; i++)
128 lineAddr[i] = srcLinePtr;
129 }
130
131 if ((mlib_s32) edge < 0) { /* process edges */
132 minX = 0;
133 minY = 0;
134 maxX = srcWidth;
135 maxY = srcHeight;
136 }
137 else {
138
139 if (kw > 1)
140 delta = -0.5; /* for MLIB_NEAREST filter delta = 0. */
141
142 minX = (kw1 - delta);
143 minY = (kh1 - delta);
144 maxX = srcWidth - ((kw - 1) - (kw1 - delta));
145 maxY = srcHeight - ((kh - 1) - (kh1 - delta));
146
147 if (edge == MLIB_EDGE_SRC_PADDED) {
148 if (minX < paddings[0])
149 minX = paddings[0];
150
151 if (minY < paddings[1])
152 minY = paddings[1];
153
154 if (maxX > (srcWidth - paddings[2]))
155 maxX = srcWidth - paddings[2];
156
157 if (maxY > (srcHeight - paddings[3]))
158 maxY = srcHeight - paddings[3];
159 }
160 }
161
162 xClip = minX;
163 yClip = minY;
164 wClip = maxX;
165 hClip = maxY;
166
167/*
168 * STORE_PARAM(param, src);
169 * STORE_PARAM(param, dst);
170 */
171 param->src = (void *)src;
172 param->dst = (void *)dst;
173 STORE_PARAM(param, lineAddr);
174 STORE_PARAM(param, dstData);
175 STORE_PARAM(param, srcYStride);
176 STORE_PARAM(param, dstYStride);
177 STORE_PARAM(param, leftEdges);
178 STORE_PARAM(param, rightEdges);
179 STORE_PARAM(param, xStarts);
180 STORE_PARAM(param, yStarts);
181 STORE_PARAM(param, max_xsize);
182 STORE_PARAM(param, yStart);
183 STORE_PARAM(param, yFinish);
184 STORE_PARAM(param, warp_tbl);
185
186 if ((xClip >= wClip) || (yClip >= hClip)) {
187 return MLIB_SUCCESS;
188 }
189
190 a2 = d;
191 b2 = -b;
192 tx2 = (-d * tx + b * ty);
193 c2 = -c;
194 d2 = a;
195 ty2 = (c * tx - a * ty);
196
197 dx = a2;
198 dy = c2;
199
200 tx -= 0.5;
201 ty -= 0.5;
202
203 coords[0][0] = xClip * a + yClip * b + tx;
204 coords[0][1] = xClip * c + yClip * d + ty;
205
206 coords[2][0] = wClip * a + hClip * b + tx;
207 coords[2][1] = wClip * c + hClip * d + ty;
208
209 if (div > 0) {
210 coords[1][0] = wClip * a + yClip * b + tx;
211 coords[1][1] = wClip * c + yClip * d + ty;
212
213 coords[3][0] = xClip * a + hClip * b + tx;
214 coords[3][1] = xClip * c + hClip * d + ty;
215 }
216 else {
217 coords[3][0] = wClip * a + yClip * b + tx;
218 coords[3][1] = wClip * c + yClip * d + ty;
219
220 coords[1][0] = xClip * a + hClip * b + tx;
221 coords[1][1] = xClip * c + hClip * d + ty;
222 }
223
224 topIdx = 0;
225 for (i = 1; i < 4; i++) {
226
227 if (coords[i][1] < coords[topIdx][1])
228 topIdx = i;
229 }
230
231 dTop = coords[topIdx][1];
232 val0 = dTop;
233 SAT32(top);
234 bot = -1;
235
236 if (top >= dstHeight) {
237 return MLIB_SUCCESS;
238 }
239
240 if (dTop >= 0.0) {
241 mlib_d64 xLeft, xRight, x;
242 mlib_s32 nextIdx;
243
244 if (dTop == top) {
245 xLeft = coords[topIdx][0];
246 xRight = coords[topIdx][0];
247 nextIdx = (topIdx + 1) & 0x3;
248
249 if (dTop == coords[nextIdx][1]) {
250 x = coords[nextIdx][0];
251 xLeft = (xLeft <= x) ? xLeft : x;
252 xRight = (xRight >= x) ? xRight : x;
253 }
254
255 nextIdx = (topIdx - 1) & 0x3;
256
257 if (dTop == coords[nextIdx][1]) {
258 x = coords[nextIdx][0];
259 xLeft = (xLeft <= x) ? xLeft : x;
260 xRight = (xRight >= x) ? xRight : x;
261 }
262
263 val0 = xLeft;
264 SAT32(t);
265 leftEdges[top] = (t >= xLeft) ? t : ++t;
266
267 if (xLeft >= MLIB_S32_MAX)
268 leftEdges[top] = MLIB_S32_MAX;
269
270 val0 = xRight;
271 SAT32(rightEdges[top]);
272 }
273 else
274 top++;
275 }
276 else
277 top = 0;
278
279 for (i = 0; i < 2; i++) {
280 mlib_d64 dY1 = coords[(topIdx - i) & 0x3][1];
281 mlib_d64 dX1 = coords[(topIdx - i) & 0x3][0];
282 mlib_d64 dY2 = coords[(topIdx - i - 1) & 0x3][1];
283 mlib_d64 dX2 = coords[(topIdx - i - 1) & 0x3][0];
284 mlib_d64 x = dX1, slope = (dX2 - dX1) / (dY2 - dY1);
285 mlib_s32 y1;
286 mlib_s32 y2;
287
288 if (dY1 == dY2)
289 continue;
290
291 if (dY1 < 0.0)
292 y1 = 0;
293 else {
294 val0 = dY1 + 1;
295 SAT32(y1);
296 }
297
298 val0 = dY2;
299 SAT32(y2);
300
301 if (y2 >= dstHeight)
302 y2 = (mlib_s32) (dstHeight - 1);
303
304 x += slope * (y1 - dY1);
305#ifdef __SUNPRO_C
306#pragma pipeloop(0)
307#endif /* __SUNPRO_C */
308 for (j = y1; j <= y2; j++) {
309 val0 = x;
310 SAT32(t);
311 leftEdges[j] = (t >= x) ? t : ++t;
312
313 if (x >= MLIB_S32_MAX)
314 leftEdges[j] = MLIB_S32_MAX;
315 x += slope;
316 }
317 }
318
319 for (i = 0; i < 2; i++) {
320 mlib_d64 dY1 = coords[(topIdx + i) & 0x3][1];
321 mlib_d64 dX1 = coords[(topIdx + i) & 0x3][0];
322 mlib_d64 dY2 = coords[(topIdx + i + 1) & 0x3][1];
323 mlib_d64 dX2 = coords[(topIdx + i + 1) & 0x3][0];
324 mlib_d64 x = dX1, slope = (dX2 - dX1) / (dY2 - dY1);
325 mlib_s32 y1;
326 mlib_s32 y2;
327
328 if (dY1 == dY2)
329 continue;
330
331 if (dY1 < 0.0)
332 y1 = 0;
333 else {
334 val0 = dY1 + 1;
335 SAT32(y1);
336 }
337
338 val0 = dY2;
339 SAT32(y2);
340
341 if (y2 >= dstHeight)
342 y2 = (mlib_s32) (dstHeight - 1);
343
344 x += slope * (y1 - dY1);
345#ifdef __SUNPRO_C
346#pragma pipeloop(0)
347#endif /* __SUNPRO_C */
348 for (j = y1; j <= y2; j++) {
349 val0 = x;
350 SAT32(rightEdges[j]);
351 x += slope;
352 }
353
354 bot = y2;
355 }
356
357 {
358 mlib_d64 dxCl = xClip * div;
359 mlib_d64 dyCl = yClip * div;
360 mlib_d64 dwCl = wClip * div;
361 mlib_d64 dhCl = hClip * div;
362
363 mlib_s32 xCl = (mlib_s32) (xClip + delta);
364 mlib_s32 yCl = (mlib_s32) (yClip + delta);
365 mlib_s32 wCl = (mlib_s32) (wClip + delta);
366 mlib_s32 hCl = (mlib_s32) (hClip + delta);
367
368 /*
369 * mlib_s32 xCl = (mlib_s32)(xClip + delta);
370 * mlib_s32 yCl = (mlib_s32)(yClip + delta);
371 * mlib_s32 wCl = (mlib_s32)(wClip);
372 * mlib_s32 hCl = (mlib_s32)(hClip);
373 */
374
375 if (edge == MLIB_EDGE_SRC_PADDED) {
376 xCl = kw1;
377 yCl = kh1;
378 wCl = (mlib_s32) (srcWidth - ((kw - 1) - kw1));
379 hCl = (mlib_s32) (srcHeight - ((kh - 1) - kh1));
380 }
381
382 div = 1.0 / div;
383
384 sdx = (mlib_s32) (a2 * div * (1 << shiftx));
385 sdy = (mlib_s32) (c2 * div * (1 << shifty));
386
387 if (div > 0) {
388
389#ifdef __SUNPRO_C
390#pragma pipeloop(0)
391#endif /* __SUNPRO_C */
392 for (i = top; i <= bot; i++) {
393 mlib_s32 xLeft = leftEdges[i];
394 mlib_s32 xRight = rightEdges[i];
395 mlib_s32 xs, ys, x_e, y_e, x_s, y_s;
396 mlib_d64 dxs, dys, dxe, dye;
397 mlib_d64 xl, ii, xr;
398
399 xLeft = (xLeft < 0) ? 0 : xLeft;
400 xRight = (xRight >= dstWidth) ? (mlib_s32) (dstWidth - 1) : xRight;
401
402 xl = xLeft + 0.5;
403 ii = i + 0.5;
404 xr = xRight + 0.5;
405 dxs = xl * a2 + ii * b2 + tx2;
406 dys = xl * c2 + ii * d2 + ty2;
407
408 if ((dxs < dxCl) || (dxs >= dwCl) || (dys < dyCl) || (dys >= dhCl)) {
409 dxs += dx;
410 dys += dy;
411 xLeft++;
412
413 if ((dxs < dxCl) || (dxs >= dwCl) || (dys < dyCl) || (dys >= dhCl))
414 xRight = -1;
415 }
416
417 dxe = xr * a2 + ii * b2 + tx2;
418 dye = xr * c2 + ii * d2 + ty2;
419
420 if ((dxe < dxCl) || (dxe >= dwCl) || (dye < dyCl) || (dye >= dhCl)) {
421 dxe -= dx;
422 dye -= dy;
423 xRight--;
424
425 if ((dxe < dxCl) || (dxe >= dwCl) || (dye < dyCl) || (dye >= dhCl))
426 xRight = -1;
427 }
428
429 xs = (mlib_s32) ((dxs * div + delta) * (1 << shiftx));
430 x_s = xs >> shiftx;
431
432 ys = (mlib_s32) ((dys * div + delta) * (1 << shifty));
433 y_s = ys >> shifty;
434
435 if (x_s < xCl)
436 xs = (xCl << shiftx);
437 else if (x_s >= wCl)
438 xs = ((wCl << shiftx) - 1);
439
440 if (y_s < yCl)
441 ys = (yCl << shifty);
442 else if (y_s >= hCl)
443 ys = ((hCl << shifty) - 1);
444
445 if (xRight >= xLeft) {
446 x_e = ((xRight - xLeft) * sdx + xs) >> shiftx;
447 y_e = ((xRight - xLeft) * sdy + ys) >> shifty;
448
449 if ((x_e < xCl) || (x_e >= wCl)) {
450 if (sdx > 0)
451 sdx -= 1;
452 else
453 sdx += 1;
454 }
455
456 if ((y_e < yCl) || (y_e >= hCl)) {
457 if (sdy > 0)
458 sdy -= 1;
459 else
460 sdy += 1;
461 }
462 }
463
464 leftEdges[i] = xLeft;
465 rightEdges[i] = xRight;
466 xStarts[i] = xs;
467 yStarts[i] = ys;
468
469 if ((xRight - xLeft + 1) > max_xsize)
470 max_xsize = (xRight - xLeft + 1);
471 }
472 }
473 else {
474
475#ifdef __SUNPRO_C
476#pragma pipeloop(0)
477#endif /* __SUNPRO_C */
478 for (i = top; i <= bot; i++) {
479 mlib_s32 xLeft = leftEdges[i];
480 mlib_s32 xRight = rightEdges[i];
481 mlib_s32 xs, ys, x_e, y_e, x_s, y_s;
482 mlib_d64 dxs, dys, dxe, dye;
483 mlib_d64 xl, ii, xr;
484
485 xLeft = (xLeft < 0) ? 0 : xLeft;
486 xRight = (xRight >= dstWidth) ? (mlib_s32) (dstWidth - 1) : xRight;
487
488 xl = xLeft + 0.5;
489 ii = i + 0.5;
490 xr = xRight + 0.5;
491 dxs = xl * a2 + ii * b2 + tx2;
492 dys = xl * c2 + ii * d2 + ty2;
493
494 if ((dxs > dxCl) || (dxs <= dwCl) || (dys > dyCl) || (dys <= dhCl)) {
495 dxs += dx;
496 dys += dy;
497 xLeft++;
498
499 if ((dxs > dxCl) || (dxs <= dwCl) || (dys > dyCl) || (dys <= dhCl))
500 xRight = -1;
501 }
502
503 dxe = xr * a2 + ii * b2 + tx2;
504 dye = xr * c2 + ii * d2 + ty2;
505
506 if ((dxe > dxCl) || (dxe <= dwCl) || (dye > dyCl) || (dye <= dhCl)) {
507 dxe -= dx;
508 dye -= dy;
509 xRight--;
510
511 if ((dxe > dxCl) || (dxe <= dwCl) || (dye > dyCl) || (dye <= dhCl))
512 xRight = -1;
513 }
514
515 xs = (mlib_s32) ((dxs * div + delta) * (1 << shiftx));
516 x_s = xs >> shiftx;
517
518 if (x_s < xCl)
519 xs = (xCl << shiftx);
520 else if (x_s >= wCl)
521 xs = ((wCl << shiftx) - 1);
522
523 ys = (mlib_s32) ((dys * div + delta) * (1 << shifty));
524 y_s = ys >> shifty;
525
526 if (y_s < yCl)
527 ys = (yCl << shifty);
528 else if (y_s >= hCl)
529 ys = ((hCl << shifty) - 1);
530
531 if (xRight >= xLeft) {
532 x_e = ((xRight - xLeft) * sdx + xs) >> shiftx;
533 y_e = ((xRight - xLeft) * sdy + ys) >> shifty;
534
535 if ((x_e < xCl) || (x_e >= wCl)) {
536 if (sdx > 0)
537 sdx -= 1;
538 else
539 sdx += 1;
540 }
541
542 if ((y_e < yCl) || (y_e >= hCl)) {
543 if (sdy > 0)
544 sdy -= 1;
545 else
546 sdy += 1;
547 }
548 }
549
550 leftEdges[i] = xLeft;
551 rightEdges[i] = xRight;
552 xStarts[i] = xs;
553 yStarts[i] = ys;
554
555 if ((xRight - xLeft + 1) > max_xsize)
556 max_xsize = (xRight - xLeft + 1);
557 }
558 }
559 }
560
561 while (leftEdges[top] > rightEdges[top] && top <= bot)
562 top++;
563
564 if (top < bot)
565 while (leftEdges[bot] > rightEdges[bot])
566 bot--;
567
568 yStart = top;
569 yFinish = bot;
570 dX = sdx;
571 dY = sdy;
572
573 dstData += (yStart - 1) * dstYStride;
574
575 STORE_PARAM(param, dstData);
576 STORE_PARAM(param, yStart);
577 STORE_PARAM(param, yFinish);
578 STORE_PARAM(param, max_xsize);
579 STORE_PARAM(param, dX);
580 STORE_PARAM(param, dY);
581
582 return MLIB_SUCCESS;
583}
584
585/***************************************************************/