Rafactoring CClipper.
[xy_vsfilter.git] / src / subtitles / Rasterizer.cpp
blob347d127b3c05c758ce810c16aae1311d56f1788a
1 /*
2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "stdafx.h"
23 #include <string.h>
24 #include <math.h>
25 #include <vector>
26 #include <algorithm>
27 #include "Rasterizer.h"
28 #include "SeparableFilter.h"
29 #include "xy_logger.h"
30 #include <boost/flyweight/key_value.hpp>
32 #ifndef _MAX /* avoid collision with common (nonconforming) macros */
33 #define _MAX (max)
34 #define _MIN (min)
35 #define _IMPL_MAX max
36 #define _IMPL_MIN min
37 #else
38 #define _IMPL_MAX _MAX
39 #define _IMPL_MIN _MIN
40 #endif
43 //NOTE: signed or unsigned affects the result seriously
44 #define COMBINE_AYUV(a, y, u, v) ((((((((int)(a))<<8)|y)<<8)|u)<<8)|v)
46 #define SPLIT_AYUV(color, a, y, u, v) do { \
47 *(v)=(color)&0xff; \
48 *(u)=((color)>>8) &0xff; \
49 *(y)=((color)>>16)&0xff;\
50 *(a)=((color)>>24)&0xff;\
51 } while(0)
53 class ass_synth_priv
55 public:
56 static const int VOLUME_BITS = 22;//should not exceed 32-8, and better not exceed 31-8
58 ass_synth_priv(const double sigma);
59 ass_synth_priv(const ass_synth_priv& priv);
61 ~ass_synth_priv();
62 int generate_tables(double sigma);
64 int g_r;
65 int g_w;
67 unsigned *g;
68 unsigned *gt2;
70 double sigma;
73 struct ass_synth_priv_key
75 const double& operator()(const ass_synth_priv& x)const
77 return x.sigma;
81 struct ass_tmp_buf
83 public:
84 ass_tmp_buf(size_t size);
85 ass_tmp_buf(const ass_tmp_buf& buf);
86 ~ass_tmp_buf();
87 size_t size;
88 unsigned *tmp;
91 struct ass_tmp_buf_get_size
93 const size_t& operator()(const ass_tmp_buf& buf)const
95 return buf.size;
99 static const unsigned int maxcolor = 255;
100 static const unsigned base = 256;
102 ass_synth_priv::ass_synth_priv(const double sigma)
104 g_r = 0;
105 g_w = 0;
107 g = NULL;
108 gt2 = NULL;
110 this->sigma = 0;
111 generate_tables(sigma);
114 ass_synth_priv::ass_synth_priv(const ass_synth_priv& priv):g_r(priv.g_r),g_w(priv.g_w),sigma(priv.sigma)
116 if (this->g_w > 0 && this != &priv) {
117 this->g = (unsigned*)realloc(this->g, this->g_w * sizeof(unsigned));
118 this->gt2 = (unsigned*)realloc(this->gt2, 256 * this->g_w * sizeof(unsigned));
119 //if (this->g == null || this->gt2 == null) {
120 // return -1;
122 memcpy(g, priv.g, this->g_w * sizeof(unsigned));
123 memcpy(gt2, priv.gt2, 256 * this->g_w * sizeof(unsigned));
127 ass_synth_priv::~ass_synth_priv()
129 free(g); g=NULL;
130 free(gt2); gt2=NULL;
133 int ass_synth_priv::generate_tables(double sigma)
135 const int TARGET_VOLUME = 1<<VOLUME_BITS;
136 const int MAX_VOLUME_ERROR = VOLUME_BITS>=22 ? 16 : 1;
138 double a = -1 / (sigma * sigma * 2);
139 double exp_a = exp(a);
141 double volume_factor = 0;
142 double volume_start = 0, volume_end = 0;
143 unsigned volume;
145 if (this->sigma == sigma)
146 return 0;
147 else
148 this->sigma = sigma;
150 this->g_w = (int)ceil(sigma*3) | 1;
151 this->g_r = this->g_w / 2;
153 if (this->g_w > 0) {
154 this->g = (unsigned*)realloc(this->g, this->g_w * sizeof(unsigned));
155 this->gt2 = (unsigned*)realloc(this->gt2, 256 * this->g_w * sizeof(unsigned));
156 if (this->g == NULL || this->gt2 == NULL) {
157 return -1;
161 if (this->g_w > 0) {
162 volume_start = 0;
164 double exp_0 = 1.0;
165 double exp_1 = exp_a;
166 double exp_2 = exp_1 * exp_1;
167 volume_start += exp_0;
168 for(int i=0;i<this->g_r;++i)
170 exp_0 *= exp_1;
171 exp_1 *= exp_2;
172 volume_start += exp_0;
173 volume_start += exp_0;
175 //euqivalent:
176 // for (i = 0; i < this->g_w; ++i) {
177 // volume_start += exp(a * (i - this->g_r) * (i - this->g_r));
178 // }
180 volume_end = (TARGET_VOLUME+g_w)/volume_start;
181 volume_start = (TARGET_VOLUME-g_w)/volume_start;
183 volume = 0;
184 while( volume_start+0.000001<volume_end )
186 volume_factor = (volume_start+volume_end)*0.5;
187 volume = 0;
189 exp_0 = volume_factor;
190 exp_1 = exp_a;
191 exp_2 = exp_1 * exp_1;
193 volume = static_cast<int>(exp_0+.5);
194 this->g[this->g_r] = volume;
196 unsigned* p_left = this->g+this->g_r-1;
197 unsigned* p_right= this->g+this->g_r+1;
198 for(int i=0; i<this->g_r;++i,p_left--,p_right++)
200 exp_0 *= exp_1;
201 exp_1 *= exp_2;
202 *p_left = static_cast<int>(exp_0+.5);
203 *p_right = *p_left;
204 volume += (*p_left<<1);
206 //equivalent:
207 // for (i = 0; i < this->g_w; ++i) {
208 // this->g[i] = (unsigned) ( exp(a * (i - this->g_r) * (i - this->g_r))* volume_factor + .5 );
209 // volume += this->g[i];
210 // }
212 // volume don't have to be equal to TARGET_VOLUME,
213 // even if volume=TARGET_VOLUME+MAX_VOLUME_ERROR,
214 // max error introducing in later blur operation,
215 // which is (dot_product(g_w, pixel))/TARGET_VOLUME with pixel<256,
216 // would not exceed (MAX_VOLUME_ERROR*256)/TARGET_VOLUME,
217 // as long as MAX_VOLUME_ERROR/TARGET_VOLUME is small enough, error introduced would be kept in safe range
219 // NOTE: when it comes to rounding, no matter how small the error is,
220 // it may result a different rounding output
221 if( volume>=TARGET_VOLUME && volume< (TARGET_VOLUME+MAX_VOLUME_ERROR) )
222 break;
223 else if(volume < TARGET_VOLUME)
225 volume_start = volume_factor;
227 else if(volume >= TARGET_VOLUME+MAX_VOLUME_ERROR)
229 volume_end = volume_factor;
232 if(volume==0)
234 volume_factor = volume_end;
236 exp_0 = volume_factor;
237 exp_1 = exp_a;
238 exp_2 = exp_1 * exp_1;
240 volume = static_cast<int>(exp_0+.5);
241 this->g[this->g_r] = volume;
243 unsigned* p_left = this->g+this->g_r-1;
244 unsigned* p_right= this->g+this->g_r+1;
245 for(int i=0; i<this->g_r;++i,p_left--,p_right++)
247 exp_0 *= exp_1;
248 exp_1 *= exp_2;
249 *p_left = static_cast<int>(exp_0+.5);
250 *p_right = *p_left;
251 volume += (*p_left<<1);
253 //equivalent:
254 // for (i = 0; i < this->g_w; ++i) {
255 // this->g[i] = (unsigned) ( exp(a * (i - this->g_r) * (i - this->g_r))* volume_factor + .5 );
256 // volume += this->g[i];
257 // }
260 // gauss table:
261 for (int mx = 0; mx < this->g_w; mx++) {
262 int last_mul = 0;
263 unsigned *p_gt2 = this->gt2 + mx;
264 *p_gt2 = 0;
265 for (int i = 1; i < 256; i++) {
266 last_mul = last_mul+this->g[mx];
267 p_gt2 += this->g_w;
268 *p_gt2 = last_mul;
269 //equivalent:
270 // this->gt2[this->g_w * i+ mx] = this->g[mx] * i;
274 return 0;
277 ass_tmp_buf::ass_tmp_buf(size_t size)
279 tmp = (unsigned *)malloc(size * sizeof(unsigned));
280 this->size = size;
283 ass_tmp_buf::ass_tmp_buf(const ass_tmp_buf& buf)
284 :size(buf.size)
286 tmp = (unsigned *)malloc(size * sizeof(unsigned));
289 ass_tmp_buf::~ass_tmp_buf()
291 free(tmp);
295 * \brief gaussian blur. an fast pure c implementation from libass.
297 static void ass_gauss_blur(unsigned char *buffer, unsigned *tmp2,
298 int width, int height, int stride, const unsigned *m2,
299 int r, int mwidth)
302 int x, y;
304 unsigned char *s = buffer;
305 unsigned *t = tmp2 + 1;
306 for (y = 0; y < height; y++) {
307 memset(t - 1, 0, (width + 1) * sizeof(*t));
308 x = 0;
309 if(x < r)//in case that r < 0
311 const int src = s[x];
312 if (src) {
313 register unsigned *dstp = t + x - r;
314 int mx;
315 const unsigned *m3 = m2 + src * mwidth;
316 unsigned sum = 0;
317 for (mx = mwidth-1; mx >= r - x ; mx--) {
318 sum += m3[mx];
319 dstp[mx] += sum;
324 for (x = 1; x < r; x++) {
325 const int src = s[x];
326 if (src) {
327 register unsigned *dstp = t + x - r;
328 int mx;
329 const unsigned *m3 = m2 + src * mwidth;
330 for (mx = r - x; mx < mwidth; mx++) {
331 dstp[mx] += m3[mx];
336 for (; x < width - r; x++) {
337 const int src = s[x];
338 if (src) {
339 register unsigned *dstp = t + x - r;
340 int mx;
341 const unsigned *m3 = m2 + src * mwidth;
342 for (mx = 0; mx < mwidth; mx++) {
343 dstp[mx] += m3[mx];
348 for (; x < width-1; x++) {
349 const int src = s[x];
350 if (src) {
351 register unsigned *dstp = t + x - r;
352 int mx;
353 const int x2 = r + width - x;
354 const unsigned *m3 = m2 + src * mwidth;
355 for (mx = 0; mx < x2; mx++) {
356 dstp[mx] += m3[mx];
360 if(x==width-1) //important: x==width-1 failed, if r==0
362 const int src = s[x];
363 if (src) {
364 register unsigned *dstp = t + x - r;
365 int mx;
366 const int x2 = r + width - x;
367 const unsigned *m3 = m2 + src * mwidth;
368 unsigned sum = 0;
369 for (mx = 0; mx < x2; mx++) {
370 sum += m3[mx];
371 dstp[mx] += sum;
376 s += stride;
377 t += width + 1;
380 t = tmp2;
381 for (x = 0; x < width; x++) {
382 y = 0;
383 if(y < r)//in case that r<0
385 unsigned *srcp = t + y * (width + 1) + 1;
386 int src = *srcp;
387 if (src) {
388 register unsigned *dstp = srcp - 1 + (mwidth -r +y)*(width + 1);
389 const int src2 = (src + (1<<(ass_synth_priv::VOLUME_BITS-1))) >> ass_synth_priv::VOLUME_BITS;
390 const unsigned *m3 = m2 + src2 * mwidth;
391 unsigned sum = 0;
392 int mx;
393 *srcp = (1<<(ass_synth_priv::VOLUME_BITS-1));
394 for (mx = mwidth-1; mx >=r - y ; mx--) {
395 sum += m3[mx];
396 *dstp += sum;
397 dstp -= width + 1;
401 for (y = 1; y < r; y++) {
402 unsigned *srcp = t + y * (width + 1) + 1;
403 int src = *srcp;
404 if (src) {
405 register unsigned *dstp = srcp - 1 + width + 1;
406 const int src2 = (src + (1<<(ass_synth_priv::VOLUME_BITS-1))) >> ass_synth_priv::VOLUME_BITS;
407 const unsigned *m3 = m2 + src2 * mwidth;
409 int mx;
410 *srcp = (1<<(ass_synth_priv::VOLUME_BITS-1));
411 for (mx = r - y; mx < mwidth; mx++) {
412 *dstp += m3[mx];
413 dstp += width + 1;
417 for (; y < height - r; y++) {
418 unsigned *srcp = t + y * (width + 1) + 1;
419 int src = *srcp;
420 if (src) {
421 register unsigned *dstp = srcp - 1 - r * (width + 1);
422 const int src2 = (src + (1<<(ass_synth_priv::VOLUME_BITS-1))) >> ass_synth_priv::VOLUME_BITS;
423 const unsigned *m3 = m2 + src2 * mwidth;
425 int mx;
426 *srcp = (1<<(ass_synth_priv::VOLUME_BITS-1));
427 for (mx = 0; mx < mwidth; mx++) {
428 *dstp += m3[mx];
429 dstp += width + 1;
433 for (; y < height-1; y++) {
434 unsigned *srcp = t + y * (width + 1) + 1;
435 int src = *srcp;
436 if (src) {
437 const int y2 = r + height - y;
438 register unsigned *dstp = srcp - 1 - r * (width + 1);
439 const int src2 = (src + (1<<(ass_synth_priv::VOLUME_BITS-1))) >> ass_synth_priv::VOLUME_BITS;
440 const unsigned *m3 = m2 + src2 * mwidth;
442 int mx;
443 *srcp = (1<<(ass_synth_priv::VOLUME_BITS-1));
444 for (mx = 0; mx < y2; mx++) {
445 *dstp += m3[mx];
446 dstp += width + 1;
450 if(y == height - 1)//important: y == height - 1 failed if r==0
452 unsigned *srcp = t + y * (width + 1) + 1;
453 int src = *srcp;
454 if (src) {
455 const int y2 = r + height - y;
456 register unsigned *dstp = srcp - 1 - r * (width + 1);
457 const int src2 = (src + (1<<(ass_synth_priv::VOLUME_BITS-1))) >> ass_synth_priv::VOLUME_BITS;
458 const unsigned *m3 = m2 + src2 * mwidth;
459 unsigned sum = 0;
460 int mx;
461 *srcp = (1<<(ass_synth_priv::VOLUME_BITS-1));
462 for (mx = 0; mx < y2; mx++) {
463 sum += m3[mx];
464 *dstp += sum;
465 dstp += width + 1;
469 t++;
472 t = tmp2;
473 s = buffer;
474 for (y = 0; y < height; y++) {
475 for (x = 0; x < width; x++) {
476 s[x] = t[x] >> ass_synth_priv::VOLUME_BITS;
478 s += stride;
479 t += width + 1;
484 * \brief blur with [[1,2,1]. [2,4,2], [1,2,1]] kernel.
486 static void be_blur(unsigned char *buf, unsigned *tmp_base, int w, int h, int stride)
488 WORD *col_pix_buf_base = reinterpret_cast<WORD*>(xy_malloc(w*sizeof(WORD)));
489 WORD *col_sum_buf_base = reinterpret_cast<WORD*>(xy_malloc(w*sizeof(WORD)));
490 if(!col_sum_buf_base || !col_pix_buf_base)
492 //ToDo: error handling
493 return;
495 memset(col_pix_buf_base, 0, w*sizeof(WORD));
496 memset(col_sum_buf_base, 0, w*sizeof(WORD));
497 WORD *col_pix_buf = col_pix_buf_base-2;//for aligment;
498 WORD *col_sum_buf = col_sum_buf_base-2;//for aligment;
500 int y = 0;
501 unsigned char *src=buf+y*stride;
503 int x = 2;
504 int old_pix = src[x-1];
505 int old_sum = old_pix + src[x-2];
506 for ( ; x < w; x++) {
507 int temp1 = src[x];
508 int temp2 = old_pix + temp1;
509 old_pix = temp1;
510 temp1 = old_sum + temp2;
511 old_sum = temp2;
512 col_pix_buf[x] = temp1;
516 int y = 1;
517 unsigned char *src=buf+y*stride;
520 int x = 2;
521 int old_pix = src[x-1];
522 int old_sum = old_pix + src[x-2];
523 for ( ; x < w; x++) {
524 int temp1 = src[x];
525 int temp2 = old_pix + temp1;
526 old_pix = temp1;
527 temp1 = old_sum + temp2;
528 old_sum = temp2;
530 temp2 = col_pix_buf[x] + temp1;
531 col_pix_buf[x] = temp1;
532 //dst[x-1] = (col_sum_buf[x] + temp2) >> 4;
533 col_sum_buf[x] = temp2;
537 //__m128i round = _mm_set1_epi16(8);
538 for (int y = 2; y < h; y++) {
539 unsigned char *src=buf+y*stride;
540 unsigned char *dst=buf+(y-1)*stride;
543 int x = 2;
544 __m128i old_pix_128 = _mm_cvtsi32_si128(src[1]);
545 __m128i old_sum_128 = _mm_cvtsi32_si128(src[0]+src[1]);
546 for ( ; x < ((w-2)&(~7)); x+=8) {
547 __m128i new_pix = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(src+x));
548 new_pix = _mm_unpacklo_epi8(new_pix, _mm_setzero_si128());
549 __m128i temp = _mm_slli_si128(new_pix,2);
550 temp = _mm_add_epi16(temp, old_pix_128);
551 temp = _mm_add_epi16(temp, new_pix);
552 old_pix_128 = _mm_srli_si128(new_pix,14);
554 new_pix = _mm_slli_si128(temp,2);
555 new_pix = _mm_add_epi16(new_pix, old_sum_128);
556 new_pix = _mm_add_epi16(new_pix, temp);
557 old_sum_128 = _mm_srli_si128(temp, 14);
559 __m128i old_col_pix = _mm_loadu_si128( reinterpret_cast<const __m128i*>(col_pix_buf+x) );
560 __m128i old_col_sum = _mm_loadu_si128( reinterpret_cast<const __m128i*>(col_sum_buf+x) );
561 _mm_storeu_si128( reinterpret_cast<__m128i*>(col_pix_buf+x), new_pix );
562 temp = _mm_add_epi16(new_pix, old_col_pix);
563 _mm_storeu_si128( reinterpret_cast<__m128i*>(col_sum_buf+x), temp );
565 old_col_sum = _mm_add_epi16(old_col_sum, temp);
566 //old_col_sum = _mm_add_epi16(old_col_sum, round);
567 old_col_sum = _mm_srli_epi16(old_col_sum, 4);
568 old_col_sum = _mm_packus_epi16(old_col_sum, old_col_sum);
569 _mm_storel_epi64( reinterpret_cast<__m128i*>(dst+x-1), old_col_sum );
571 int old_pix = src[x-1];
572 int old_sum = old_pix + src[x-2];
573 for ( ; x < w; x++) {
574 int temp1 = src[x];
575 int temp2 = old_pix + temp1;
576 old_pix = temp1;
577 temp1 = old_sum + temp2;
578 old_sum = temp2;
580 temp2 = col_pix_buf[x] + temp1;
581 col_pix_buf[x] = temp1;
582 dst[x-1] = (col_sum_buf[x] + temp2) >> 4;
583 col_sum_buf[x] = temp2;
587 xy_free(col_sum_buf_base);
588 xy_free(col_pix_buf_base);
591 static void Bilinear(unsigned char *buf, int w, int h, int stride, int x_factor, int y_factor)
593 WORD *col_pix_buf_base = reinterpret_cast<WORD*>(xy_malloc(w*sizeof(WORD)));
594 if(!col_pix_buf_base)
596 //ToDo: error handling
597 return;
599 memset(col_pix_buf_base, 0, w*sizeof(WORD));
601 for (int y = 0; y < h; y++){
602 unsigned char *src=buf+y*stride;
604 WORD *col_pix_buf = col_pix_buf_base;
605 int last=0;
606 for(int x = 0; x < w; x++)
608 int temp1 = src[x];
609 int temp2 = temp1*x_factor;
610 temp1 <<= 3;
611 temp1 -= temp2;
612 temp1 += last;
613 last = temp2;
615 temp2 = temp1*y_factor;
616 temp1 <<= 3;
617 temp1 -= temp2;
618 temp1 += col_pix_buf[x];
619 src[x] = ((temp1+32)>>6);
620 col_pix_buf[x] = temp2;
623 xy_free(col_pix_buf_base);
626 bool Rasterizer::Rasterize(const ScanLineData2& scan_line_data2, int xsub, int ysub, SharedPtrOverlay overlay)
628 using namespace ::boost::flyweights;
630 if(!overlay)
632 return false;
634 overlay->CleanUp();
635 const ScanLineData& scan_line_data = *scan_line_data2.m_scan_line_data;
636 if(!scan_line_data.mWidth || !scan_line_data.mHeight)
638 return true;
640 xsub &= 7;
641 ysub &= 7;
642 //xsub = ysub = 0;
643 int width = scan_line_data.mWidth + xsub;
644 int height = scan_line_data.mHeight + ysub;
645 overlay->mOffsetX = scan_line_data2.mPathOffsetX - xsub;
646 overlay->mOffsetY = scan_line_data2.mPathOffsetY - ysub;
647 int wide_border = (scan_line_data2.mWideBorder+7)&~7;
648 overlay->mfWideOutlineEmpty = scan_line_data2.mWideOutline.empty();
649 if(!overlay->mfWideOutlineEmpty)
651 width += 2*wide_border ;
652 height += 2*wide_border ;
653 xsub += wide_border ;
654 ysub += wide_border ;
655 overlay->mOffsetX -= wide_border;
656 overlay->mOffsetY -= wide_border;
659 overlay->mWidth = width;
660 overlay->mHeight = height;
661 overlay->mOverlayWidth = ((width+7)>>3) + 1;
662 overlay->mOverlayHeight = ((height+7)>>3) + 1;
663 overlay->mOverlayPitch = (overlay->mOverlayWidth+15)&~15;
665 overlay->mpOverlayBuffer.base = (byte*)xy_malloc(2 * overlay->mOverlayPitch * overlay->mOverlayHeight);
666 memset(overlay->mpOverlayBuffer.base, 0, 2 * overlay->mOverlayPitch * overlay->mOverlayHeight);
667 overlay->mpOverlayBuffer.body = overlay->mpOverlayBuffer.base;
668 overlay->mpOverlayBuffer.border = overlay->mpOverlayBuffer.base + overlay->mOverlayPitch * overlay->mOverlayHeight;
670 // Are we doing a border?
671 const ScanLineData::tSpanBuffer* pOutline[2] = {&(scan_line_data.mOutline), &(scan_line_data2.mWideOutline)};
672 for(int i = countof(pOutline)-1; i >= 0; i--)
674 ScanLineData::tSpanBuffer::const_iterator it = pOutline[i]->begin();
675 ScanLineData::tSpanBuffer::const_iterator itEnd = pOutline[i]->end();
676 byte* plan_selected = i==0 ? overlay->mpOverlayBuffer.body : overlay->mpOverlayBuffer.border;
677 int pitch = overlay->mOverlayPitch;
678 for(; it!=itEnd; ++it)
680 int y = (int)(((*it).first >> 32) - 0x40000000 + ysub);
681 int x1 = (int)(((*it).first & 0xffffffff) - 0x40000000 + xsub);
682 int x2 = (int)(((*it).second & 0xffffffff) - 0x40000000 + xsub);
683 if(x2 > x1)
685 int first = x1>>3;
686 int last = (x2-1)>>3;
687 byte* dst = plan_selected + (pitch*(y>>3) + first);
688 if(first == last)
689 *dst += x2-x1;
690 else
692 *dst += ((first+1)<<3) - x1;
693 dst += 1;
694 while(++first < last)
696 *dst += 0x08;
697 dst += 1;
699 *dst += x2 - (last<<3);
705 return true;
708 // @return: true if actually a blur operation has done, or else false and output is leave unset.
709 bool Rasterizer::Blur(const Overlay& input_overlay, int fBlur, double fGaussianBlur,
710 SharedPtrOverlay output_overlay)
712 using namespace ::boost::flyweights;
714 if(!output_overlay)
716 return false;
718 output_overlay->CleanUp();
720 output_overlay->mOffsetX = input_overlay.mOffsetX;
721 output_overlay->mOffsetY = input_overlay.mOffsetY;
722 output_overlay->mWidth = input_overlay.mWidth;
723 output_overlay->mHeight = input_overlay.mHeight;
724 output_overlay->mOverlayWidth = input_overlay.mOverlayWidth;
725 output_overlay->mOverlayHeight = input_overlay.mOverlayHeight;
726 output_overlay->mfWideOutlineEmpty = input_overlay.mfWideOutlineEmpty;
728 int bluradjust = 0;
729 if(fBlur || fGaussianBlur > 0.1)
731 if (fGaussianBlur > 0)
732 bluradjust += (int)(fGaussianBlur*3*8 + 0.5) | 1;
733 if (fBlur)
734 bluradjust += 8;
735 // Expand the buffer a bit when we're blurring, since that can also widen the borders a bit
736 bluradjust = (bluradjust+7)&~7;
738 output_overlay->mOffsetX -= bluradjust;
739 output_overlay->mOffsetY -= bluradjust;
740 output_overlay->mWidth += (bluradjust<<1);
741 output_overlay->mHeight += (bluradjust<<1);
742 output_overlay->mOverlayWidth += (bluradjust>>2);
743 output_overlay->mOverlayHeight += (bluradjust>>2);
745 else
747 return false;
750 output_overlay->mOverlayPitch = (output_overlay->mOverlayWidth+15)&~15;
752 output_overlay->mpOverlayBuffer.base = (byte*)xy_malloc(2 * output_overlay->mOverlayPitch * output_overlay->mOverlayHeight);
753 memset(output_overlay->mpOverlayBuffer.base, 0, 2 * output_overlay->mOverlayPitch * output_overlay->mOverlayHeight);
754 output_overlay->mpOverlayBuffer.body = output_overlay->mpOverlayBuffer.base;
755 output_overlay->mpOverlayBuffer.border = output_overlay->mpOverlayBuffer.base + output_overlay->mOverlayPitch * output_overlay->mOverlayHeight;
757 //copy buffer
758 for(int i = 1; i >= 0; i--)
760 byte* plan_selected = i==0 ? output_overlay->mpOverlayBuffer.body : output_overlay->mpOverlayBuffer.border;
761 const byte* plan_input = i==0 ? input_overlay.mpOverlayBuffer.body : input_overlay.mpOverlayBuffer.border;
763 plan_selected += (bluradjust>>3) + (bluradjust>>3)*output_overlay->mOverlayPitch;
764 for (int j=0;j<input_overlay.mOverlayHeight;j++)
766 memcpy(plan_selected, plan_input, input_overlay.mOverlayPitch);
767 plan_selected += output_overlay->mOverlayPitch;
768 plan_input += input_overlay.mOverlayPitch;
772 ass_tmp_buf tmp_buf( max((output_overlay->mOverlayPitch+1)*(output_overlay->mOverlayHeight+1),0) );
773 //flyweight<key_value<int, ass_tmp_buf, ass_tmp_buf_get_size>, no_locking> tmp_buf((overlay->mOverlayWidth+1)*(overlay->mOverlayPitch+1));
774 // Do some gaussian blur magic
775 if (fGaussianBlur > 0.1)//(fGaussianBlur > 0) return true even if fGaussianBlur very small
777 byte* plan_selected= output_overlay->mfWideOutlineEmpty ? output_overlay->mpOverlayBuffer.body : output_overlay->mpOverlayBuffer.border;
778 flyweight<key_value<double, ass_synth_priv, ass_synth_priv_key>, no_locking> fw_priv_blur(fGaussianBlur);
779 const ass_synth_priv& priv_blur = fw_priv_blur.get();
780 if (output_overlay->mOverlayWidth>=priv_blur.g_w && output_overlay->mOverlayHeight>=priv_blur.g_w)
782 ass_gauss_blur(plan_selected, tmp_buf.tmp, output_overlay->mOverlayWidth, output_overlay->mOverlayHeight, output_overlay->mOverlayPitch,
783 priv_blur.gt2, priv_blur.g_r, priv_blur.g_w);
787 for (int pass = 0; pass < fBlur; pass++)
789 if(output_overlay->mOverlayWidth >= 3 && output_overlay->mOverlayHeight >= 3)
791 int pitch = output_overlay->mOverlayPitch;
792 byte* plan_selected= output_overlay->mfWideOutlineEmpty ? output_overlay->mpOverlayBuffer.body : output_overlay->mpOverlayBuffer.border;
793 be_blur(plan_selected, tmp_buf.tmp, output_overlay->mOverlayWidth, output_overlay->mOverlayHeight, pitch);
796 return true;
799 ///////////////////////////////////////////////////////////////////////////
801 static __forceinline void pixmix(DWORD *dst, DWORD color, DWORD alpha)
803 int a = alpha;
804 // Make sure both a and ia are in range 1..256 for the >>8 operations below to be correct
805 int ia = 256-a;
806 a+=1;
807 *dst = ((((*dst&0x00ff00ff)*ia + (color&0x00ff00ff)*a)&0xff00ff00)>>8)
808 | ((((*dst&0x0000ff00)*ia + (color&0x0000ff00)*a)&0x00ff0000)>>8)
809 | ((((*dst>>8)&0x00ff0000)*ia)&0xff000000);
812 static __forceinline void pixmix2(DWORD *dst, DWORD color, DWORD shapealpha, DWORD clipalpha)
814 int a = (((shapealpha)*(clipalpha)*(color>>24))>>12)&0xff;
815 int ia = 256-a;
816 a+=1;
817 *dst = ((((*dst&0x00ff00ff)*ia + (color&0x00ff00ff)*a)&0xff00ff00)>>8)
818 | ((((*dst&0x0000ff00)*ia + (color&0x0000ff00)*a)&0x00ff0000)>>8)
819 | ((((*dst>>8)&0x00ff0000)*ia)&0xff000000);
822 #include <xmmintrin.h>
823 #include <emmintrin.h>
825 static __forceinline void pixmix_sse2(DWORD* dst, DWORD color, DWORD alpha)
827 // alpha = (((alpha) * (color>>24)) >> 6) & 0xff;
828 color &= 0xffffff;
829 __m128i zero = _mm_setzero_si128();
830 __m128i a = _mm_set1_epi32(((alpha+1) << 16) | (0x100 - alpha));
831 __m128i d = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*dst), zero);
832 __m128i s = _mm_unpacklo_epi8(_mm_cvtsi32_si128(color), zero);
833 __m128i r = _mm_unpacklo_epi16(d, s);
834 r = _mm_madd_epi16(r, a);
835 r = _mm_srli_epi32(r, 8);
836 r = _mm_packs_epi32(r, r);
837 r = _mm_packus_epi16(r, r);
838 *dst = (DWORD)_mm_cvtsi128_si32(r);
841 static __forceinline void pixmix2_sse2(DWORD* dst, DWORD color, DWORD shapealpha, DWORD clipalpha)
843 int alpha = (((shapealpha)*(clipalpha)*(color>>24))>>12)&0xff;
844 color &= 0xffffff;
845 __m128i zero = _mm_setzero_si128();
846 __m128i a = _mm_set1_epi32(((alpha+1) << 16) | (0x100 - alpha));
847 __m128i d = _mm_unpacklo_epi8(_mm_cvtsi32_si128(*dst), zero);
848 __m128i s = _mm_unpacklo_epi8(_mm_cvtsi32_si128(color), zero);
849 __m128i r = _mm_unpacklo_epi16(d, s);
850 r = _mm_madd_epi16(r, a);
851 r = _mm_srli_epi32(r, 8);
852 r = _mm_packs_epi32(r, r);
853 r = _mm_packus_epi16(r, r);
854 *dst = (DWORD)_mm_cvtsi128_si32(r);
857 #include <mmintrin.h>
859 // Calculate a - b clamping to 0 instead of underflowing
860 static __forceinline DWORD safe_subtract(DWORD a, DWORD b)
862 __m64 ap = _mm_cvtsi32_si64(a);
863 __m64 bp = _mm_cvtsi32_si64(b);
864 __m64 rp = _mm_subs_pu16(ap, bp);
865 DWORD r = (DWORD)_mm_cvtsi64_si32(rp);
866 _mm_empty();
867 return r;
868 //return (b > a) ? 0 : a - b;
871 /***
872 * No aligned requirement
875 void AlphaBlt(byte* pY,
876 const byte* pAlphaMask,
877 const byte Y,
878 int h, int w, int src_stride, int dst_stride)
880 __m128i zero = _mm_setzero_si128();
881 __m128i s = _mm_set1_epi16(Y); //s = c 0 c 0 c 0 c 0 c 0 c 0 c 0 c 0
883 if( w>16 )//IMPORTANT! The result of the following code is undefined with w<15.
885 for( ; h>0; h--, pAlphaMask += src_stride, pY += dst_stride )
887 const BYTE* sa = pAlphaMask;
888 BYTE* dy = pY;
889 const BYTE* dy_first_mod16 = reinterpret_cast<BYTE*>((reinterpret_cast<int>(pY)+15)&~15); //IMPORTANT! w must >= 15
890 const BYTE* dy_end_mod16 = reinterpret_cast<BYTE*>(reinterpret_cast<int>(pY+w)&~15);
891 const BYTE* dy_end = pY + w;
893 for(;dy < dy_first_mod16; sa++, dy++)
895 *dy = (*dy * (256 - *sa)+ Y*(*sa+1))>>8;
897 for(; dy < dy_end_mod16; sa+=8, dy+=16)
899 __m128i a = _mm_loadl_epi64((__m128i*)sa);
902 __m128i d = _mm_load_si128((__m128i*)dy);
904 //__m128i ones = _mm_cmpeq_epi32(zero,zero); //ones = ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
905 //__m128i ia = _mm_xor_si128(a,ones); //ia = ~a
906 //ia = _mm_unpacklo_epi8(ia,zero); //ia = ~a0 0 ~a1 0 ~a2 0 ~a3 0 ~a4 0 ~a5 0 ~a6 0 ~a7 0
907 a = _mm_unpacklo_epi8(a,zero); //a= a0 0 a1 0 a2 0 a3 0 a4 0 a5 0 a6 0 a7 0
908 __m128i ones = _mm_set1_epi16(256); //ones = 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
909 __m128i ia = _mm_sub_epi16(ones, a); //ia = 256-a0 ... 256-a7
910 ones = _mm_srli_epi16(ones, 8);
911 a = _mm_add_epi16(a, ones); //a= 1+a0 ... 1+a7
913 __m128i dl = _mm_unpacklo_epi8(d,zero); //d = b0 0 b1 0 b2 0 b3 0 b4 0 b5 0 b6 0 b7 0
914 __m128i sl = _mm_mullo_epi16(s,a); //sl = c0*a0 c1*a1 ... c7*a7
916 dl = _mm_mullo_epi16(dl,ia); //d = b0*~a0 b1*~a1 ... b7*~a7
918 dl = _mm_add_epi16(dl,sl); //d = d + sl
919 dl = _mm_srli_epi16(dl, 8); //d = d>>8
921 sa += 8;
922 a = _mm_loadl_epi64((__m128i*)sa);
924 a = _mm_unpacklo_epi8(a,zero);
925 ones = _mm_slli_epi16(ones, 8);
926 ia = _mm_sub_epi16(ones, a);
927 ones = _mm_srli_epi16(ones, 8);
928 a = _mm_add_epi16(a,ones);
930 d = _mm_unpackhi_epi8(d,zero);
931 sl = _mm_mullo_epi16(s,a);
932 d = _mm_mullo_epi16(d,ia);
933 d = _mm_add_epi16(d,sl);
934 d = _mm_srli_epi16(d, 8);
936 dl = _mm_packus_epi16(dl,d);
938 _mm_store_si128((__m128i*)dy, dl);
940 for(;dy < dy_end; sa++, dy++)
942 *dy = (*dy * (256 - *sa)+ Y*(*sa+1))>>8;
946 else
948 for( ; h>0; h--, pAlphaMask += src_stride, pY += dst_stride )
950 const BYTE* sa = pAlphaMask;
951 BYTE* dy = pY;
952 const BYTE* dy_end = pY + w;
954 for(;dy < dy_end; sa++, dy++)
956 *dy = (*dy * (256 - *sa)+ Y*(*sa+1))>>8;
960 //__asm emms;
963 /***
964 * No aligned requirement
967 void AlphaBlt(byte* pY,
968 const byte alpha,
969 const byte Y,
970 int h, int w, int dst_stride)
972 int yPremul = Y*(alpha+1);
973 int dstAlpha = 0x100 - alpha;
974 if( w>32 )//IMPORTANT! The result of the following code is undefined with w<15.
976 __m128i zero = _mm_setzero_si128();
977 __m128i s = _mm_set1_epi16(yPremul); //s = c 0 c 0 c 0 c 0 c 0 c 0 c 0 c 0
978 __m128i ia = _mm_set1_epi16(dstAlpha);
979 for( ; h>0; h--, pY += dst_stride )
981 BYTE* dy = pY;
982 const BYTE* dy_first_mod16 = reinterpret_cast<BYTE*>((reinterpret_cast<int>(pY)+15)&~15); //IMPORTANT! w must >= 15
983 const BYTE* dy_end_mod16 = reinterpret_cast<BYTE*>(reinterpret_cast<int>(pY+w)&~15);
984 const BYTE* dy_end = pY + w;
986 for(;dy < dy_first_mod16; dy++)
988 *dy = (*dy * dstAlpha + yPremul)>>8;
990 for(; dy < dy_end_mod16; dy+=16)
993 __m128i d = _mm_load_si128(reinterpret_cast<const __m128i*>(dy));
994 __m128i dl = _mm_unpacklo_epi8(d,zero); //d = b0 0 b1 0 b2 0 b3 0 b4 0 b5 0 b6 0 b7 0
996 dl = _mm_mullo_epi16(dl,ia); //d = b0*~a0 b1*~a1 ... b7*~a7
997 dl = _mm_adds_epu16(dl,s); //d = d + s
998 dl = _mm_srli_epi16(dl, 8); //d = d>>8
1000 d = _mm_unpackhi_epi8(d,zero);
1001 d = _mm_mullo_epi16(d,ia);
1002 d = _mm_adds_epu16(d,s);
1003 d = _mm_srli_epi16(d, 8);
1005 dl = _mm_packus_epi16(dl,d);
1007 _mm_store_si128(reinterpret_cast<__m128i*>(dy), dl);
1009 for(;dy < dy_end; dy++)
1011 *dy = (*dy * dstAlpha + yPremul)>>8;
1015 else
1017 for( ; h>0; h--, pY += dst_stride )
1019 BYTE* dy = pY;
1020 const BYTE* dy_end = pY + w;
1022 for(;dy < dy_end; dy++)
1024 *dy = (*dy * dstAlpha + yPremul)>>8;
1028 //__asm emms;
1031 /***
1032 * No aligned requirement
1035 void AlphaBltC(byte* pY,
1036 const byte alpha,
1037 const byte Y,
1038 int h, int w, int dst_stride)
1040 int yPremul = Y*(alpha+1);
1041 int dstAlpha = 0x100 - alpha;
1043 for( ; h>0; h--, pY += dst_stride )
1045 BYTE* dy = pY;
1046 const BYTE* dy_end = pY + w;
1048 for(;dy < dy_end; dy++)
1050 *dy = (*dy * dstAlpha + yPremul)>>8;
1055 // For CPUID usage in Rasterizer::Draw
1056 #include "../dsutil/vd.h"
1058 static const __int64 _00ff00ff00ff00ff = 0x00ff00ff00ff00ffi64;
1060 // Render a subpicture onto a surface.
1061 // spd is the surface to render on.
1062 // clipRect is a rectangular clip region to render inside.
1063 // pAlphaMask is an alpha clipping mask.
1064 // xsub and ysub ???
1065 // switchpts seems to be an array of fill colours interlaced with coordinates.
1066 // switchpts[i*2] contains a colour and switchpts[i*2+1] contains the coordinate to use that colour from
1067 // fBody tells whether to render the body of the subs.
1068 // fBorder tells whether to render the border of the subs.
1069 SharedPtrByte Rasterizer::CompositeAlphaMask(SubPicDesc& spd, const SharedPtrOverlay& overlay, const CRect& clipRect,
1070 const GrayImage2* alpha_mask,
1071 int xsub, int ysub, const DWORD* switchpts, bool fBody, bool fBorder,
1072 CRect *outputDirtyRect)
1074 //fix me: check and log error
1075 SharedPtrByte result;
1076 *outputDirtyRect = CRect(0, 0, 0, 0);
1077 if(!switchpts || !fBody && !fBorder) return(result);
1079 // clip
1080 // Limit drawn area to intersection of rendering surface and rectangular clip area
1081 CRect r(0, 0, spd.w, spd.h);
1082 r &= clipRect;
1083 if (alpha_mask!=NULL)
1085 r &= CRect(alpha_mask->left_top, alpha_mask->size);
1088 // Remember that all subtitle coordinates are specified in 1/8 pixels
1089 // (x+4)>>3 rounds to nearest whole pixel.
1090 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
1091 int x = (xsub + overlay->mOffsetX + 4)>>3;
1092 int y = (ysub + overlay->mOffsetY + 4)>>3;
1093 int w = overlay->mOverlayWidth;
1094 int h = overlay->mOverlayHeight;
1095 int xo = 0, yo = 0;
1096 // Again, limiting?
1097 if(x < r.left) {xo = r.left-x; w -= r.left-x; x = r.left;}
1098 if(y < r.top) {yo = r.top-y; h -= r.top-y; y = r.top;}
1099 if(x+w > r.right) w = r.right-x;
1100 if(y+h > r.bottom) h = r.bottom-y;
1101 // Check if there's actually anything to render
1102 if(w <= 0 || h <= 0) return(result);
1103 outputDirtyRect->SetRect(x, y, x+w, y+h);
1105 bool fSingleColor = (switchpts[1]==0xffffffff);
1107 // draw
1108 // Grab the first colour
1109 DWORD color = switchpts[0];
1110 byte* s_base = (byte*)xy_malloc(overlay->mOverlayPitch * overlay->mOverlayHeight);
1111 const byte* alpha_mask_data = alpha_mask != NULL ? alpha_mask->data.get() : NULL;
1112 const int alpha_mask_pitch = alpha_mask != NULL ? alpha_mask->pitch : 0;
1113 if(alpha_mask_data!=NULL )
1114 alpha_mask_data += alpha_mask->pitch * y + x - alpha_mask->left_top.y*alpha_mask->pitch - alpha_mask->left_top.x;
1116 if(fSingleColor)
1118 overlay->FillAlphaMash(s_base, fBody, fBorder, xo, yo, w, h,
1119 alpha_mask_data, alpha_mask_pitch,
1120 color>>24 );
1122 else
1124 int last_x = xo;
1125 const DWORD *sw = switchpts;
1126 while( last_x<w+xo )
1128 byte alpha = sw[0]>>24;
1129 while( sw[3]<w+xo && (sw[2]>>24)==alpha )
1131 sw += 2;
1133 int new_x = sw[3] < w+xo ? sw[3] : w+xo;
1134 overlay->FillAlphaMash(s_base, fBody, fBorder,
1135 last_x, yo, new_x-last_x, h,
1136 alpha_mask_data, alpha_mask_pitch,
1137 alpha );
1138 last_x = new_x;
1139 sw += 2;
1142 result.reset( s_base, xy_free );
1143 return result;
1146 void Rasterizer::Draw(SubPicDesc& spd, SharedPtrOverlay overlay, const CRect& clipRect, byte* s_base,
1147 int xsub, int ysub, const DWORD* switchpts, bool fBody, bool fBorder)
1149 if(!switchpts || !fBody && !fBorder) return;
1151 // clip
1152 // Limit drawn area to intersection of rendering surface and rectangular clip area
1153 CRect r(0, 0, spd.w, spd.h);
1154 r &= clipRect;
1155 // Remember that all subtitle coordinates are specified in 1/8 pixels
1156 // (x+4)>>3 rounds to nearest whole pixel.
1157 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
1158 int overlayPitch = overlay->mOverlayPitch;
1159 int x = (xsub + overlay->mOffsetX + 4)>>3;
1160 int y = (ysub + overlay->mOffsetY + 4)>>3;
1161 int w = overlay->mOverlayWidth;
1162 int h = overlay->mOverlayHeight;
1163 int xo = 0, yo = 0;
1164 // Again, limiting?
1165 if(x < r.left) {xo = r.left-x; w -= r.left-x; x = r.left;}
1166 if(y < r.top) {yo = r.top-y; h -= r.top-y; y = r.top;}
1167 if(x+w > r.right) w = r.right-x;
1168 if(y+h > r.bottom) h = r.bottom-y;
1169 // Check if there's actually anything to render
1170 if(w <= 0 || h <= 0) return;
1172 // CPUID from VDub
1173 bool fSSE2 = !!(g_cpuid.m_flags & CCpuID::sse2);
1174 bool fSingleColor = (switchpts[1]==0xffffffff);
1175 bool AYUV_PLANAR = (spd.type==MSP_AYUV_PLANAR);
1176 int draw_method = 0;
1177 if(fSingleColor)
1178 draw_method |= DM::SINGLE_COLOR;
1179 if(fSSE2)
1180 draw_method |= DM::SSE2;
1181 if(AYUV_PLANAR)
1182 draw_method |= DM::AYUV_PLANAR;
1184 // draw
1185 // Grab the first colour
1186 DWORD color = switchpts[0];
1187 const byte* s = s_base + overlay->mOverlayPitch*yo + xo;
1189 // How would this differ from src?
1190 unsigned long* dst = (unsigned long *)(((char *)spd.bits + spd.pitch * y) + ((x*spd.bpp)>>3));
1192 // Every remaining line in the bitmap to be rendered...
1193 switch(draw_method)
1195 case DM::SINGLE_COLOR | DM::SSE2 | 0*DM::AYUV_PLANAR :
1197 while(h--)
1199 for(int wt=0; wt<w; ++wt)
1200 // The <<6 is due to pixmix expecting the alpha parameter to be
1201 // the multiplication of two 6-bit unsigned numbers but we
1202 // only have one here. (No alpha mask.)
1203 pixmix_sse2(&dst[wt], color, s[wt]);
1204 s += overlayPitch;
1205 dst = (unsigned long *)((char *)dst + spd.pitch);
1208 break;
1209 case DM::SINGLE_COLOR | 0*DM::SSE2 | 0*DM::AYUV_PLANAR :
1211 while(h--)
1213 for(int wt=0; wt<w; ++wt)
1214 pixmix(&dst[wt], color, s[wt]);
1215 s += overlayPitch;
1216 dst = (unsigned long *)((char *)dst + spd.pitch);
1219 break;
1220 case 0*DM::SINGLE_COLOR | DM::SSE2 | 0*DM::AYUV_PLANAR :
1222 while(h--)
1224 const DWORD *sw = switchpts;
1225 for(int wt=0; wt<w; ++wt)
1227 // xo is the offset (usually negative) we have moved into the image
1228 // So if we have passed the switchpoint (?) switch to another colour
1229 // (So switchpts stores both colours *and* coordinates?)
1230 if(wt+xo >= sw[1]) {while(wt+xo >= sw[1]) sw += 2; color = sw[-2];}
1231 pixmix_sse2(&dst[wt], color, s[wt]);
1233 s += overlayPitch;
1234 dst = (unsigned long *)((char *)dst + spd.pitch);
1237 break;
1238 case 0*DM::SINGLE_COLOR | 0*DM::SSE2 | 0*DM::AYUV_PLANAR :
1240 while(h--)
1242 const DWORD *sw = switchpts;
1243 for(int wt=0; wt<w; ++wt)
1245 if(wt+xo >= sw[1]) {while(wt+xo >= sw[1]) sw += 2; color = sw[-2];}
1246 pixmix(&dst[wt], color, s[wt]);
1248 s += overlayPitch;
1249 dst = (unsigned long *)((char *)dst + spd.pitch);
1252 break;
1253 case DM::SINGLE_COLOR | DM::SSE2 | DM::AYUV_PLANAR :
1255 unsigned char* dst_A = (unsigned char*)dst;
1256 unsigned char* dst_Y = dst_A + spd.pitch*spd.h;
1257 unsigned char* dst_U = dst_Y + spd.pitch*spd.h;
1258 unsigned char* dst_V = dst_U + spd.pitch*spd.h;
1260 AlphaBlt(dst_Y, s, ((color)>>16)&0xff, h, w, overlayPitch, spd.pitch);
1261 AlphaBlt(dst_U, s, ((color)>>8)&0xff, h, w, overlayPitch, spd.pitch);
1262 AlphaBlt(dst_V, s, ((color))&0xff, h, w, overlayPitch, spd.pitch);
1263 AlphaBlt(dst_A, s, 0, h, w, overlayPitch, spd.pitch);
1265 break;
1266 case 0*DM::SINGLE_COLOR | DM::SSE2 | DM::AYUV_PLANAR :
1268 unsigned char* dst_A = (unsigned char*)dst;
1269 unsigned char* dst_Y = dst_A + spd.pitch*spd.h;
1270 unsigned char* dst_U = dst_Y + spd.pitch*spd.h;
1271 unsigned char* dst_V = dst_U + spd.pitch*spd.h;
1273 const DWORD *sw = switchpts;
1274 int last_x = xo;
1275 color = sw[0];
1276 while(last_x<w+xo)
1278 int new_x = sw[3] < w+xo ? sw[3] : w+xo;
1279 color = sw[0];
1280 sw += 2;
1281 if( new_x < last_x )
1282 continue;
1283 AlphaBlt(dst_Y, s + last_x - xo, (color>>16)&0xff, h, new_x-last_x, overlayPitch, spd.pitch);
1284 AlphaBlt(dst_U, s + last_x - xo, (color>>8)&0xff, h, new_x-last_x, overlayPitch, spd.pitch);
1285 AlphaBlt(dst_V, s + last_x - xo, (color)&0xff, h, new_x-last_x, overlayPitch, spd.pitch);
1286 AlphaBlt(dst_A, s + last_x - xo, 0, h, new_x-last_x, overlayPitch, spd.pitch);
1288 dst_A += new_x - last_x;
1289 dst_Y += new_x - last_x;
1290 dst_U += new_x - last_x;
1291 dst_V += new_x - last_x;
1292 last_x = new_x;
1295 break;
1296 case DM::SINGLE_COLOR | 0*DM::SSE2 | DM::AYUV_PLANAR :
1298 // char * debug_dst=(char*)dst;int h2 = h;
1299 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", (char*)&color, sizeof(color)) );
1300 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1301 // debug_dst += spd.pitch*spd.h;
1302 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1303 // debug_dst += spd.pitch*spd.h;
1304 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1305 // debug_dst += spd.pitch*spd.h;
1306 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1307 // debug_dst=(char*)dst;
1309 unsigned char* dst_A = (unsigned char*)dst;
1310 unsigned char* dst_Y = dst_A + spd.pitch*spd.h;
1311 unsigned char* dst_U = dst_Y + spd.pitch*spd.h;
1312 unsigned char* dst_V = dst_U + spd.pitch*spd.h;
1313 while(h--)
1315 for(int wt=0; wt<w; ++wt)
1317 DWORD temp = COMBINE_AYUV(dst_A[wt], dst_Y[wt], dst_U[wt], dst_V[wt]);
1318 pixmix(&temp, color, s[wt]);
1319 SPLIT_AYUV(temp, dst_A+wt, dst_Y+wt, dst_U+wt, dst_V+wt);
1321 s += overlayPitch;
1322 dst_A += spd.pitch;
1323 dst_Y += spd.pitch;
1324 dst_U += spd.pitch;
1325 dst_V += spd.pitch;
1327 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1328 // debug_dst += spd.pitch*spd.h;
1329 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1330 // debug_dst += spd.pitch*spd.h;
1331 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1332 // debug_dst += spd.pitch*spd.h;
1333 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1335 break;
1336 case 0*DM::SINGLE_COLOR | 0*DM::SSE2 | DM::AYUV_PLANAR :
1338 unsigned char* dst_A = (unsigned char*)dst;
1339 unsigned char* dst_Y = dst_A + spd.pitch*spd.h;
1340 unsigned char* dst_U = dst_Y + spd.pitch*spd.h;
1341 unsigned char* dst_V = dst_U + spd.pitch*spd.h;
1342 while(h--)
1344 const DWORD *sw = switchpts;
1345 for(int wt=0; wt<w; ++wt)
1347 if(wt+xo >= sw[1]) {while(wt+xo >= sw[1]) sw += 2; color = sw[-2];}
1348 DWORD temp = COMBINE_AYUV(dst_A[wt], dst_Y[wt], dst_U[wt], dst_V[wt]);
1349 pixmix(&temp, color, (s[wt]*(color>>24))>>8);
1350 SPLIT_AYUV(temp, dst_A+wt, dst_Y+wt, dst_U+wt, dst_V+wt);
1352 s += overlayPitch;
1353 dst_A += spd.pitch;
1354 dst_Y += spd.pitch;
1355 dst_U += spd.pitch;
1356 dst_V += spd.pitch;
1359 break;
1361 // Remember to EMMS!
1362 // Rendering fails in funny ways if we don't do this.
1363 _mm_empty();
1364 return;
1367 CRect Rasterizer::DryDraw( SubPicDesc& spd, SharedPtrOverlay overlay, const CRect& clipRect, byte* pAlphaMask, int xsub, int ysub, const DWORD* switchpts, bool fBody, bool fBorder )
1369 CRect bbox(0, 0, 0, 0);
1370 if(!switchpts || !fBody && !fBorder) return(bbox);
1372 // clip
1373 // Limit drawn area to intersection of rendering surface and rectangular clip area
1374 CRect r(0, 0, spd.w, spd.h);
1375 r &= clipRect;
1376 // Remember that all subtitle coordinates are specified in 1/8 pixels
1377 // (x+4)>>3 rounds to nearest whole pixel.
1378 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
1379 int overlayPitch = overlay->mOverlayPitch;
1380 int x = (xsub + overlay->mOffsetX + 4)>>3;
1381 int y = (ysub + overlay->mOffsetY + 4)>>3;
1382 int w = overlay->mOverlayWidth;
1383 int h = overlay->mOverlayHeight;
1384 int xo = 0, yo = 0;
1385 // Again, limiting?
1386 if(x < r.left) {xo = r.left-x; w -= r.left-x; x = r.left;}
1387 if(y < r.top) {yo = r.top-y; h -= r.top-y; y = r.top;}
1388 if(x+w > r.right) w = r.right-x;
1389 if(y+h > r.bottom) h = r.bottom-y;
1390 // Check if there's actually anything to render
1391 if(w <= 0 || h <= 0) return(bbox);
1392 bbox.SetRect(x, y, x+w, y+h);
1393 bbox &= CRect(0, 0, spd.w, spd.h);
1395 return bbox;
1398 void Rasterizer::FillSolidRect(SubPicDesc& spd, int x, int y, int nWidth, int nHeight, DWORD argb)
1400 bool fSSE2 = !!(g_cpuid.m_flags & CCpuID::sse2);
1401 bool AYUV_PLANAR = (spd.type==MSP_AYUV_PLANAR);
1402 int draw_method = 0;
1403 if(fSSE2)
1404 draw_method |= DM::SSE2;
1405 if(AYUV_PLANAR)
1406 draw_method |= DM::AYUV_PLANAR;
1408 switch (draw_method)
1410 case DM::SSE2 | 0*DM::AYUV_PLANAR :
1412 for (int wy=y; wy<y+nHeight; wy++) {
1413 DWORD* dst = (DWORD*)((BYTE*)spd.bits + spd.pitch * wy) + x;
1414 for(int wt=0; wt<nWidth; ++wt) {
1415 pixmix_sse2(&dst[wt], argb, argb>>24);
1419 break;
1420 case 0*DM::SSE2 | 0*DM::AYUV_PLANAR :
1422 for (int wy=y; wy<y+nHeight; wy++) {
1423 DWORD* dst = (DWORD*)((BYTE*)spd.bits + spd.pitch * wy) + x;
1424 for(int wt=0; wt<nWidth; ++wt) {
1425 pixmix(&dst[wt], argb, argb>>24);
1429 break;
1430 case DM::SSE2 | DM::AYUV_PLANAR :
1432 BYTE* dst = reinterpret_cast<BYTE*>(spd.bits) + spd.pitch * y + x;
1433 BYTE* dst_A = dst;
1434 BYTE* dst_Y = dst_A + spd.pitch*spd.h;
1435 BYTE* dst_U = dst_Y + spd.pitch*spd.h;
1436 BYTE* dst_V = dst_U + spd.pitch*spd.h;
1437 AlphaBlt(dst_Y, argb>>24, ((argb)>>16)&0xff, nHeight, nWidth, spd.pitch);
1438 AlphaBlt(dst_U, argb>>24, ((argb)>>8)&0xff, nHeight, nWidth, spd.pitch);
1439 AlphaBlt(dst_V, argb>>24, ((argb))&0xff, nHeight, nWidth, spd.pitch);
1440 AlphaBlt(dst_A, argb>>24, 0, nHeight, nWidth, spd.pitch);
1442 break;
1443 case 0*DM::SSE2 | DM::AYUV_PLANAR :
1445 BYTE* dst = reinterpret_cast<BYTE*>(spd.bits) + spd.pitch * y + x;
1446 BYTE* dst_A = dst;
1447 BYTE* dst_Y = dst_A + spd.pitch*spd.h;
1448 BYTE* dst_U = dst_Y + spd.pitch*spd.h;
1449 BYTE* dst_V = dst_U + spd.pitch*spd.h;
1450 AlphaBltC(dst_Y, argb>>24, ((argb)>>16)&0xff, nHeight, nWidth, spd.pitch);
1451 AlphaBltC(dst_U, argb>>24, ((argb)>>8)&0xff, nHeight, nWidth, spd.pitch);
1452 AlphaBltC(dst_V, argb>>24, ((argb))&0xff, nHeight, nWidth, spd.pitch);
1453 AlphaBltC(dst_A, argb>>24, 0, nHeight, nWidth, spd.pitch);
1455 break;
1457 _mm_empty();
1460 ///////////////////////////////////////////////////////////////
1462 // Overlay
1464 void Overlay::_DoFillAlphaMash(byte* outputAlphaMask, const byte* pBody, const byte* pBorder, int x, int y, int w, int h,
1465 const byte* pAlphaMask, int pitch, DWORD color_alpha )
1467 pBody = pBody!=NULL ? pBody + y*mOverlayPitch + x: NULL;
1468 pBorder = pBorder!=NULL ? pBorder + y*mOverlayPitch + x: NULL;
1469 byte* dst = outputAlphaMask + y*mOverlayPitch + x;
1471 const int x0 = ((reinterpret_cast<int>(dst)+3)&~3) - reinterpret_cast<int>(dst) < w ?
1472 ((reinterpret_cast<int>(dst)+3)&~3) - reinterpret_cast<int>(dst) : w; //IMPORTANT! Should not exceed w.
1473 const int x00 = ((reinterpret_cast<int>(dst)+15)&~15) - reinterpret_cast<int>(dst) < w ?
1474 ((reinterpret_cast<int>(dst)+15)&~15) - reinterpret_cast<int>(dst) : w;//IMPORTANT! Should not exceed w.
1475 const int x_end00 = ((reinterpret_cast<int>(dst)+w)&~15) - reinterpret_cast<int>(dst);
1476 const int x_end0 = ((reinterpret_cast<int>(dst)+w)&~3) - reinterpret_cast<int>(dst);
1477 const int x_end = w;
1479 __m64 color_alpha_64 = _mm_set1_pi16(color_alpha);
1480 __m128i color_alpha_128 = _mm_set1_epi16(color_alpha);
1482 if(pAlphaMask==NULL && pBody!=NULL && pBorder!=NULL)
1485 __asm
1487 mov eax, color_alpha
1488 movd XMM3, eax
1489 punpcklwd XMM3, XMM3
1490 pshufd XMM3, XMM3, 0
1493 while(h--)
1495 int j=0;
1496 for( ; j<x0; j++ )
1498 int temp = pBorder[j]-pBody[j];
1499 temp = temp<0 ? 0 : temp;
1500 dst[j] = (temp * color_alpha)>>6;
1502 for( ;j<x00;j+=4 )
1504 __m64 border = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder+j));
1505 __m64 body = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody+j));
1506 border = _mm_subs_pu8(border, body);
1507 __m64 zero = _mm_setzero_si64();
1508 border = _mm_unpacklo_pi8(border, zero);
1509 border = _mm_mullo_pi16(border, color_alpha_64);
1510 border = _mm_srli_pi16(border, 6);
1511 border = _mm_packs_pu16(border,border);
1512 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(border);
1514 __m128i zero = _mm_setzero_si128();
1515 for( ;j<x_end00;j+=16)
1517 __m128i border = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pBorder+j));
1518 __m128i body = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pBody+j));
1519 border = _mm_subs_epu8(border,body);
1520 __m128i srchi = border;
1521 border = _mm_unpacklo_epi8(border, zero);
1522 srchi = _mm_unpackhi_epi8(srchi, zero);
1523 border = _mm_mullo_epi16(border, color_alpha_128);
1524 srchi = _mm_mullo_epi16(srchi, color_alpha_128);
1525 border = _mm_srli_epi16(border, 6);
1526 srchi = _mm_srli_epi16(srchi, 6);
1527 border = _mm_packus_epi16(border, srchi);
1528 _mm_storeu_si128(reinterpret_cast<__m128i*>(dst+j), border);
1530 for( ;j<x_end0;j+=4)
1532 __m64 border = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder+j));
1533 __m64 body = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody+j));
1534 border = _mm_subs_pu8(border, body);
1535 __m64 zero = _mm_setzero_si64();
1536 border = _mm_unpacklo_pi8(border, zero);
1537 border = _mm_mullo_pi16(border, color_alpha_64);
1538 border = _mm_srli_pi16(border, 6);
1539 border = _mm_packs_pu16(border,border);
1540 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(border);
1542 for( ;j<x_end;j++)
1544 int temp = pBorder[j]-pBody[j];
1545 temp = temp<0 ? 0 : temp;
1546 dst[j] = (temp * color_alpha)>>6;
1548 pBody += mOverlayPitch;
1549 pBorder += mOverlayPitch;
1550 //pAlphaMask += pitch;
1551 dst += mOverlayPitch;
1554 else if( ((pBody==NULL) + (pBorder==NULL))==1 && pAlphaMask==NULL)
1556 const BYTE* src1 = pBody!=NULL ? pBody : pBorder;
1557 while(h--)
1559 int j=0;
1560 for( ; j<x0; j++ )
1562 dst[j] = (src1[j] * color_alpha)>>6;
1564 for( ;j<x00;j+=4 )
1566 __m64 src = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1+j));
1567 __m64 zero = _mm_setzero_si64();
1568 src = _mm_unpacklo_pi8(src, zero);
1569 src = _mm_mullo_pi16(src, color_alpha_64);
1570 src = _mm_srli_pi16(src, 6);
1571 src = _mm_packs_pu16(src,src);
1572 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(src);
1574 __m128i zero = _mm_setzero_si128();
1575 for( ;j<x_end00;j+=16)
1577 __m128i src = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src1+j));
1578 __m128i srchi = src;
1579 src = _mm_unpacklo_epi8(src, zero);
1580 srchi = _mm_unpackhi_epi8(srchi, zero);
1581 src = _mm_mullo_epi16(src, color_alpha_128);
1582 srchi = _mm_mullo_epi16(srchi, color_alpha_128);
1583 src = _mm_srli_epi16(src, 6);
1584 srchi = _mm_srli_epi16(srchi, 6);
1585 src = _mm_packus_epi16(src, srchi);
1586 _mm_storeu_si128(reinterpret_cast<__m128i*>(dst+j), src);
1588 for( ;j<x_end0;j+=4)
1590 __m64 src = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1+j));
1591 __m64 zero = _mm_setzero_si64();
1592 src = _mm_unpacklo_pi8(src, zero);
1593 src = _mm_mullo_pi16(src, color_alpha_64);
1594 src = _mm_srli_pi16(src, 6);
1595 src = _mm_packs_pu16(src,src);
1596 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(src);
1598 for( ;j<x_end;j++)
1600 dst[j] = (src1[j] * color_alpha)>>6;
1602 src1 += mOverlayPitch;
1603 //pAlphaMask += pitch;
1604 dst += mOverlayPitch;
1607 else if( ((pBody==NULL) + (pBorder==NULL))==1 && pAlphaMask!=NULL)
1609 const BYTE* src1 = pBody!=NULL ? pBody : pBorder;
1610 while(h--)
1612 int j=0;
1613 for( ; j<x0; j++ )
1615 dst[j] = (src1[j] * pAlphaMask[j] * color_alpha)>>12;
1617 for( ;j<x00;j+=4 )
1619 __m64 src = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1+j));
1620 __m64 mask = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask+j));
1621 __m64 zero = _mm_setzero_si64();
1622 src = _mm_unpacklo_pi8(src, zero);
1623 src = _mm_mullo_pi16(src, color_alpha_64);
1624 mask = _mm_unpacklo_pi8(zero, mask); //important!
1625 src = _mm_mulhi_pi16(src, mask); //important!
1626 src = _mm_srli_pi16(src, 12+8-16); //important!
1627 src = _mm_packs_pu16(src,src);
1628 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(src);
1630 __m128i zero = _mm_setzero_si128();
1631 for( ;j<x_end00;j+=16)
1633 __m128i src = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src1+j));
1634 __m128i mask = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pAlphaMask+j));
1635 __m128i srchi = src;
1636 __m128i maskhi = mask;
1637 src = _mm_unpacklo_epi8(src, zero);
1638 srchi = _mm_unpackhi_epi8(srchi, zero);
1639 mask = _mm_unpacklo_epi8(zero, mask); //important!
1640 maskhi = _mm_unpackhi_epi8(zero, maskhi);
1641 src = _mm_mullo_epi16(src, color_alpha_128);
1642 srchi = _mm_mullo_epi16(srchi, color_alpha_128);
1643 src = _mm_mulhi_epu16(src, mask); //important!
1644 srchi = _mm_mulhi_epu16(srchi, maskhi);
1645 src = _mm_srli_epi16(src, 12+8-16); //important!
1646 srchi = _mm_srli_epi16(srchi, 12+8-16);
1647 src = _mm_packus_epi16(src, srchi);
1648 _mm_storeu_si128(reinterpret_cast<__m128i*>(dst+j), src);
1650 for( ;j<x_end0;j+=4)
1652 __m64 src = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1+j));
1653 __m64 mask = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask+j));
1654 __m64 zero = _mm_setzero_si64();
1655 src = _mm_unpacklo_pi8(src, zero);
1656 src = _mm_mullo_pi16(src, color_alpha_64);
1657 mask = _mm_unpacklo_pi8(zero, mask); //important!
1658 src = _mm_mulhi_pi16(src, mask); //important!
1659 src = _mm_srli_pi16(src, 12+8-16); //important!
1660 src = _mm_packs_pu16(src,src);
1661 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(src);
1663 for( ;j<x_end;j++)
1665 dst[j] = (src1[j] * pAlphaMask[j] * color_alpha)>>12;
1667 src1 += mOverlayPitch;
1668 pAlphaMask += pitch;
1669 dst += mOverlayPitch;
1672 else if( pAlphaMask!=NULL && pBody!=NULL && pBorder!=NULL )
1674 while(h--)
1676 int j=0;
1677 for( ; j<x0; j++ )
1679 int temp = pBorder[j]-pBody[j];
1680 temp = temp<0 ? 0 : temp;
1681 dst[j] = (temp * pAlphaMask[j] * color_alpha)>>12;
1683 for( ;j<x00;j+=4 )
1685 __m64 border = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder+j));
1686 __m64 body = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody+j));
1687 border = _mm_subs_pu8(border, body);
1688 __m64 mask = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask+j));
1689 __m64 zero = _mm_setzero_si64();
1690 border = _mm_unpacklo_pi8(border, zero);
1691 border = _mm_mullo_pi16(border, color_alpha_64);
1692 mask = _mm_unpacklo_pi8(zero, mask); //important!
1693 border = _mm_mulhi_pi16(border, mask); //important!
1694 border = _mm_srli_pi16(border, 12+8-16); //important!
1695 border = _mm_packs_pu16(border,border);
1696 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(border);
1698 __m128i zero = _mm_setzero_si128();
1699 for( ;j<x_end00;j+=16)
1701 __m128i border = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pBorder+j));
1702 __m128i body = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pBody+j));
1703 border = _mm_subs_epu8(border,body);
1705 __m128i mask = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pAlphaMask+j));
1706 __m128i srchi = border;
1707 __m128i maskhi = mask;
1708 border = _mm_unpacklo_epi8(border, zero);
1709 srchi = _mm_unpackhi_epi8(srchi, zero);
1710 mask = _mm_unpacklo_epi8(zero, mask); //important!
1711 maskhi = _mm_unpackhi_epi8(zero, maskhi);
1712 border = _mm_mullo_epi16(border, color_alpha_128);
1713 srchi = _mm_mullo_epi16(srchi, color_alpha_128);
1714 border = _mm_mulhi_epu16(border, mask); //important!
1715 srchi = _mm_mulhi_epu16(srchi, maskhi);
1716 border = _mm_srli_epi16(border, 12+8-16); //important!
1717 srchi = _mm_srli_epi16(srchi, 12+8-16);
1718 border = _mm_packus_epi16(border, srchi);
1719 _mm_storeu_si128(reinterpret_cast<__m128i*>(dst+j), border);
1721 for( ;j<x_end0;j+=4)
1723 __m64 border = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder+j));
1724 __m64 body = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody+j));
1725 border = _mm_subs_pu8(border, body);
1726 __m64 mask = _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask+j));
1727 __m64 zero = _mm_setzero_si64();
1728 border = _mm_unpacklo_pi8(border, zero);
1729 border = _mm_mullo_pi16(border, color_alpha_64);
1730 mask = _mm_unpacklo_pi8(zero, mask); //important!
1731 border = _mm_mulhi_pi16(border, mask); //important!
1732 border = _mm_srli_pi16(border, 12+8-16); //important!
1733 border = _mm_packs_pu16(border,border);
1734 *reinterpret_cast<int*>(dst+j) = _mm_cvtsi64_si32(border);
1736 for( ;j<x_end;j++)
1738 int temp = pBorder[j]-pBody[j];
1739 temp = temp<0 ? 0 : temp;
1740 dst[j] = (temp * pAlphaMask[j] * color_alpha)>>12;
1742 pBody += mOverlayPitch;
1743 pBorder += mOverlayPitch;
1744 pAlphaMask += pitch;
1745 dst += mOverlayPitch;
1748 else
1750 //should NOT happen!
1751 ASSERT(0);
1755 void Overlay::FillAlphaMash( byte* outputAlphaMask, bool fBody, bool fBorder, int x, int y, int w, int h, const byte* pAlphaMask, int pitch, DWORD color_alpha)
1757 if(!fBorder && fBody && pAlphaMask==NULL)
1759 _DoFillAlphaMash(outputAlphaMask, mpOverlayBuffer.body, NULL, x, y, w, h, pAlphaMask, pitch, color_alpha);
1761 else if(/*fBorder &&*/ fBody && pAlphaMask==NULL)
1763 _DoFillAlphaMash(outputAlphaMask, NULL, mpOverlayBuffer.border, x, y, w, h, pAlphaMask, pitch, color_alpha);
1765 else if(!fBody && fBorder /* pAlphaMask==NULL or not*/)
1767 _DoFillAlphaMash(outputAlphaMask, mpOverlayBuffer.body, mpOverlayBuffer.border, x, y, w, h, pAlphaMask, pitch, color_alpha);
1769 else if(!fBorder && fBody && pAlphaMask!=NULL)
1771 _DoFillAlphaMash(outputAlphaMask, mpOverlayBuffer.body, NULL, x, y, w, h, pAlphaMask, pitch, color_alpha);
1773 else if(fBorder && fBody && pAlphaMask!=NULL)
1775 _DoFillAlphaMash(outputAlphaMask, NULL, mpOverlayBuffer.border, x, y, w, h, pAlphaMask, pitch, color_alpha);
1777 else
1779 //should NOT happen
1780 ASSERT(0);
1784 Overlay* Overlay::GetSubpixelVariance(unsigned int xshift, unsigned int yshift)
1786 Overlay* overlay = new Overlay();
1787 if(!overlay)
1789 return NULL;
1791 xshift &= 7;
1792 yshift &= 7;
1794 overlay->mOffsetX = mOffsetX - xshift;
1795 overlay->mOffsetY = mOffsetY - yshift;
1796 overlay->mWidth = mWidth + xshift;
1797 overlay->mHeight = mHeight + yshift;
1799 overlay->mOverlayWidth = ((overlay->mWidth+7)>>3) + 1;
1800 overlay->mOverlayHeight = ((overlay->mHeight + 7)>>3) + 1;
1801 overlay->mOverlayPitch = (overlay->mOverlayWidth+15)&~15;
1803 overlay->mpOverlayBuffer.base = reinterpret_cast<byte*>(xy_malloc(2 * overlay->mOverlayPitch * overlay->mOverlayHeight));
1804 overlay->mpOverlayBuffer.body = overlay->mpOverlayBuffer.base;
1805 overlay->mpOverlayBuffer.border = overlay->mpOverlayBuffer.base + overlay->mOverlayPitch * overlay->mOverlayHeight;
1807 overlay->mfWideOutlineEmpty = mfWideOutlineEmpty;
1809 if(overlay->mOverlayWidth==mOverlayWidth && overlay->mOverlayHeight==mOverlayHeight)
1810 memcpy(overlay->mpOverlayBuffer.base, mpOverlayBuffer.base, 2 * mOverlayPitch * mOverlayHeight);
1811 else
1813 memset(overlay->mpOverlayBuffer.base, 0, 2 * overlay->mOverlayPitch * overlay->mOverlayHeight);
1814 byte* dst = overlay->mpOverlayBuffer.body;
1815 const byte* src = mpOverlayBuffer.body;
1816 for (int i=0;i<mOverlayHeight;i++)
1818 memcpy(dst, src, mOverlayPitch);
1819 dst += overlay->mOverlayPitch;
1820 src += mOverlayPitch;
1822 dst = overlay->mpOverlayBuffer.border;
1823 src = mpOverlayBuffer.border;
1824 for (int i=0;i<mOverlayHeight;i++)
1826 memcpy(dst, src, mOverlayPitch);
1827 dst += overlay->mOverlayPitch;
1828 src += mOverlayPitch;
1831 //not equal
1832 // Bilinear(overlay->mpOverlayBuffer.base, overlay->mOverlayWidth, 2*overlay->mOverlayHeight, overlay->mOverlayPitch, xshift, yshift);
1833 Bilinear(overlay->mpOverlayBuffer.body, overlay->mOverlayWidth, overlay->mOverlayHeight, overlay->mOverlayPitch, xshift, yshift);
1834 Bilinear(overlay->mpOverlayBuffer.border, overlay->mOverlayWidth, overlay->mOverlayHeight, overlay->mOverlayPitch, xshift, yshift);
1835 return overlay;
1838 ///////////////////////////////////////////////////////////////
1840 // PathData
1842 PathData::PathData():mpPathTypes(NULL), mpPathPoints(NULL), mPathPoints(0)
1846 PathData::PathData( const PathData& src ):mpPathTypes(NULL), mpPathPoints(NULL), mPathPoints(src.mPathPoints)
1848 //TODO: deal with the case that src.mPathPoints<0
1849 if(mPathPoints>0)
1851 mpPathTypes = static_cast<BYTE*>(malloc(mPathPoints * sizeof(BYTE)));
1852 mpPathPoints = static_cast<POINT*>(malloc(mPathPoints * sizeof(POINT)));
1854 if(mPathPoints>0)
1856 memcpy(mpPathTypes, src.mpPathTypes, mPathPoints*sizeof(BYTE));
1857 memcpy(mpPathPoints, src.mpPathPoints, mPathPoints*sizeof(POINT));
1861 const PathData& PathData::operator=( const PathData& src )
1863 if(this!=&src)
1865 if(mPathPoints!=src.mPathPoints && src.mPathPoints>0)
1867 _TrashPath();
1868 mPathPoints = src.mPathPoints;
1869 mpPathTypes = static_cast<BYTE*>(malloc(mPathPoints * sizeof(BYTE)));
1870 mpPathPoints = static_cast<POINT*>(malloc(mPathPoints * sizeof(POINT)));//better than realloc
1872 if(src.mPathPoints>0)
1874 memcpy(mpPathTypes, src.mpPathTypes, mPathPoints*sizeof(BYTE));
1875 memcpy(mpPathPoints, src.mpPathPoints, mPathPoints*sizeof(POINT));
1878 return *this;
1881 PathData::~PathData()
1883 _TrashPath();
1886 bool PathData::operator==( const PathData& rhs ) const
1888 return (this==&rhs) || (
1889 mPathPoints==rhs.mPathPoints
1890 && !memcmp(mpPathTypes, rhs.mpPathTypes, mPathPoints * sizeof(BYTE) )
1891 && !memcmp(mpPathPoints, rhs.mpPathPoints, mPathPoints * sizeof(POINT) )
1895 void PathData::_TrashPath()
1897 if (mpPathTypes)
1899 free(mpPathTypes);
1900 mpPathTypes = NULL;
1902 if (mpPathPoints)
1904 free(mpPathPoints);
1905 mpPathPoints = NULL;
1907 mPathPoints = 0;
1910 bool PathData::BeginPath(HDC hdc)
1912 _TrashPath();
1913 return !!::BeginPath(hdc);
1916 bool PathData::EndPath(HDC hdc)
1918 ::CloseFigure(hdc);
1919 if(::EndPath(hdc))
1921 mPathPoints = GetPath(hdc, NULL, NULL, 0);
1922 if(!mPathPoints)
1923 return true;
1924 mpPathTypes = (BYTE*)malloc(sizeof(BYTE) * mPathPoints);
1925 mpPathPoints = (POINT*)malloc(sizeof(POINT) * mPathPoints);
1926 if(mPathPoints == GetPath(hdc, mpPathPoints, mpPathTypes, mPathPoints))
1927 return true;
1929 ::AbortPath(hdc);
1930 return false;
1933 bool PathData::PartialBeginPath(HDC hdc, bool bClearPath)
1935 if(bClearPath)
1936 _TrashPath();
1937 return !!::BeginPath(hdc);
1940 bool PathData::PartialEndPath(HDC hdc, long dx, long dy)
1942 ::CloseFigure(hdc);
1943 if(::EndPath(hdc))
1945 int nPoints;
1946 BYTE* pNewTypes;
1947 POINT* pNewPoints;
1948 nPoints = GetPath(hdc, NULL, NULL, 0);
1949 if(!nPoints)
1950 return true;
1951 pNewTypes = (BYTE*)realloc(mpPathTypes, (mPathPoints + nPoints) * sizeof(BYTE));
1952 pNewPoints = (POINT*)realloc(mpPathPoints, (mPathPoints + nPoints) * sizeof(POINT));
1953 if(pNewTypes)
1954 mpPathTypes = pNewTypes;
1955 if(pNewPoints)
1956 mpPathPoints = pNewPoints;
1957 BYTE* pTypes = new BYTE[nPoints];
1958 POINT* pPoints = new POINT[nPoints];
1959 if(pNewTypes && pNewPoints && nPoints == GetPath(hdc, pPoints, pTypes, nPoints))
1961 for(int i = 0; i < nPoints; ++i)
1963 mpPathPoints[mPathPoints + i].x = pPoints[i].x + dx;
1964 mpPathPoints[mPathPoints + i].y = pPoints[i].y + dy;
1965 mpPathTypes[mPathPoints + i] = pTypes[i];
1967 mPathPoints += nPoints;
1968 delete[] pTypes;
1969 delete[] pPoints;
1970 return true;
1972 else
1973 DebugBreak();
1974 delete[] pTypes;
1975 delete[] pPoints;
1977 ::AbortPath(hdc);
1978 return false;
1981 void PathData::AlignLeftTop(CPoint *left_top, CSize *size)
1983 int minx = INT_MAX;
1984 int miny = INT_MAX;
1985 int maxx = INT_MIN;
1986 int maxy = INT_MIN;
1987 for(int i=0; i<mPathPoints; ++i)
1989 int ix = mpPathPoints[i].x;
1990 int iy = mpPathPoints[i].y;
1991 if(ix < minx) minx = ix;
1992 if(ix > maxx) maxx = ix;
1993 if(iy < miny) miny = iy;
1994 if(iy > maxy) maxy = iy;
1996 if(minx > maxx || miny > maxy)
1998 _TrashPath();
1999 *left_top = CPoint(0, 0);
2000 *size = CSize(0, 0);
2001 return;
2003 minx = (minx >> 3) & ~7;
2004 miny = (miny >> 3) & ~7;
2005 maxx = (maxx + 7) >> 3;
2006 maxy = (maxy + 7) >> 3;
2007 for(int i=0; i<mPathPoints; ++i)
2009 mpPathPoints[i].x -= minx*8;
2010 mpPathPoints[i].y -= miny*8;
2012 *left_top = CPoint(minx, miny);
2013 *size = CSize(maxx+1-minx, maxy+1-miny);
2014 return;
2017 //////////////////////////////////////////////////////////////////////////
2019 // ScanLineData
2021 ScanLineData::ScanLineData()
2025 ScanLineData::~ScanLineData()
2029 void ScanLineData::_ReallocEdgeBuffer(int edges)
2031 mEdgeHeapSize = edges;
2032 mpEdgeBuffer = (Edge*)realloc(mpEdgeBuffer, sizeof(Edge)*edges);
2035 void ScanLineData::_EvaluateBezier(const PathData& path_data, int ptbase, bool fBSpline)
2037 const POINT* pt0 = path_data.mpPathPoints + ptbase;
2038 const POINT* pt1 = path_data.mpPathPoints + ptbase + 1;
2039 const POINT* pt2 = path_data.mpPathPoints + ptbase + 2;
2040 const POINT* pt3 = path_data.mpPathPoints + ptbase + 3;
2041 double x0 = pt0->x;
2042 double x1 = pt1->x;
2043 double x2 = pt2->x;
2044 double x3 = pt3->x;
2045 double y0 = pt0->y;
2046 double y1 = pt1->y;
2047 double y2 = pt2->y;
2048 double y3 = pt3->y;
2049 double cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
2050 if(fBSpline)
2052 // 1 [-1 +3 -3 +1]
2053 // - * [+3 -6 +3 0]
2054 // 6 [-3 0 +3 0]
2055 // [+1 +4 +1 0]
2056 double _1div6 = 1.0/6.0;
2057 cx3 = _1div6*(- x0+3*x1-3*x2+x3);
2058 cx2 = _1div6*( 3*x0-6*x1+3*x2);
2059 cx1 = _1div6*(-3*x0 +3*x2);
2060 cx0 = _1div6*( x0+4*x1+1*x2);
2061 cy3 = _1div6*(- y0+3*y1-3*y2+y3);
2062 cy2 = _1div6*( 3*y0-6*y1+3*y2);
2063 cy1 = _1div6*(-3*y0 +3*y2);
2064 cy0 = _1div6*( y0+4*y1+1*y2);
2066 else // bezier
2068 // [-1 +3 -3 +1]
2069 // [+3 -6 +3 0]
2070 // [-3 +3 0 0]
2071 // [+1 0 0 0]
2072 cx3 = - x0+3*x1-3*x2+x3;
2073 cx2 = 3*x0-6*x1+3*x2;
2074 cx1 = -3*x0+3*x1;
2075 cx0 = x0;
2076 cy3 = - y0+3*y1-3*y2+y3;
2077 cy2 = 3*y0-6*y1+3*y2;
2078 cy1 = -3*y0+3*y1;
2079 cy0 = y0;
2082 // This equation is from Graphics Gems I.
2084 // The idea is that since we're approximating a cubic curve with lines,
2085 // any error we incur is due to the curvature of the line, which we can
2086 // estimate by calculating the maximum acceleration of the curve. For
2087 // a cubic, the acceleration (second derivative) is a line, meaning that
2088 // the absolute maximum acceleration must occur at either the beginning
2089 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
2090 // conservative than that, but that's okay.
2092 // If the acceleration of the parametric formula is zero (c2 = c3 = 0),
2093 // that component of the curve is linear and does not incur any error.
2094 // If a=0 for both X and Y, the curve is a line segment and we can
2095 // use a step size of 1.
2096 double maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
2097 double maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
2098 double maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
2099 double h = 1.0;
2100 if(maxaccel > 8.0) h = sqrt(8.0 / maxaccel);
2101 if(!fFirstSet) {firstp.x = (LONG)cx0; firstp.y = (LONG)cy0; lastp = firstp; fFirstSet = true;}
2102 for(double t = 0; t < 1.0; t += h)
2104 double x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
2105 double y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
2106 _EvaluateLine(lastp.x, lastp.y, (int)x, (int)y);
2108 double x = cx0 + cx1 + cx2 + cx3;
2109 double y = cy0 + cy1 + cy2 + cy3;
2110 _EvaluateLine(lastp.x, lastp.y, (int)x, (int)y);
2113 void ScanLineData::_EvaluateLine(const PathData& path_data, int pt1idx, int pt2idx)
2115 const POINT* pt1 = path_data.mpPathPoints + pt1idx;
2116 const POINT* pt2 = path_data.mpPathPoints + pt2idx;
2117 _EvaluateLine(pt1->x, pt1->y, pt2->x, pt2->y);
2120 void ScanLineData::_EvaluateLine(int x0, int y0, int x1, int y1)
2122 if(lastp.x != x0 || lastp.y != y0)
2124 _EvaluateLine(lastp.x, lastp.y, x0, y0);
2126 if(!fFirstSet) {firstp.x = x0; firstp.y = y0; fFirstSet = true;}
2127 lastp.x = x1;
2128 lastp.y = y1;
2129 if(y1 > y0) // down
2131 __int64 xacc = (__int64)x0 << 13;
2132 // prestep y0 down
2133 int dy = y1 - y0;
2134 int y = ((y0 + 3)&~7) + 4;
2135 int iy = y >> 3;
2136 y1 = (y1 - 5) >> 3;
2137 if(iy <= y1)
2139 __int64 invslope = (__int64(x1 - x0) << 16) / dy;
2140 while(mEdgeNext + y1 + 1 - iy > mEdgeHeapSize)
2141 _ReallocEdgeBuffer(mEdgeHeapSize*2);
2142 xacc += (invslope * (y - y0)) >> 3;
2143 while(iy <= y1)
2145 int ix = (int)((xacc + 32768) >> 16);
2146 mpEdgeBuffer[mEdgeNext].next = mpScanBuffer[iy];
2147 mpEdgeBuffer[mEdgeNext].posandflag = ix*2 + 1;
2148 mpScanBuffer[iy] = mEdgeNext++;
2149 ++iy;
2150 xacc += invslope;
2154 else if(y1 < y0) // up
2156 __int64 xacc = (__int64)x1 << 13;
2157 // prestep y1 down
2158 int dy = y0 - y1;
2159 int y = ((y1 + 3)&~7) + 4;
2160 int iy = y >> 3;
2161 y0 = (y0 - 5) >> 3;
2162 if(iy <= y0)
2164 __int64 invslope = (__int64(x0 - x1) << 16) / dy;
2165 while(mEdgeNext + y0 + 1 - iy > mEdgeHeapSize)
2166 _ReallocEdgeBuffer(mEdgeHeapSize*2);
2167 xacc += (invslope * (y - y1)) >> 3;
2168 while(iy <= y0)
2170 int ix = (int)((xacc + 32768) >> 16);
2171 mpEdgeBuffer[mEdgeNext].next = mpScanBuffer[iy];
2172 mpEdgeBuffer[mEdgeNext].posandflag = ix*2;
2173 mpScanBuffer[iy] = mEdgeNext++;
2174 ++iy;
2175 xacc += invslope;
2181 bool ScanLineData::ScanConvert(const PathData& path_data, const CSize& size)
2183 int lastmoveto = -1;
2184 int i;
2185 // Drop any outlines we may have.
2186 mOutline.clear();
2187 // Determine bounding box
2188 if(!path_data.mPathPoints)
2190 mWidth = mHeight = 0;
2191 return false;
2193 mWidth = size.cx;
2194 mHeight = size.cy;
2195 // Initialize edge buffer. We use edge 0 as a sentinel.
2196 mEdgeNext = 1;
2197 mEdgeHeapSize = 2048;
2198 mpEdgeBuffer = (Edge*)malloc(sizeof(Edge)*mEdgeHeapSize);
2199 // Initialize scanline list.
2200 mpScanBuffer = new unsigned int[mHeight];
2201 memset(mpScanBuffer, 0, mHeight*sizeof(unsigned int));
2202 // Scan convert the outline. Yuck, Bezier curves....
2203 // Unfortunately, Windows 95/98 GDI has a bad habit of giving us text
2204 // paths with all but the first figure left open, so we can't rely
2205 // on the PT_CLOSEFIGURE flag being used appropriately.
2206 fFirstSet = false;
2207 firstp.x = firstp.y = 0;
2208 lastp.x = lastp.y = 0;
2209 for(i=0; i<path_data.mPathPoints; ++i)
2211 BYTE t = path_data.mpPathTypes[i] & ~PT_CLOSEFIGURE;
2212 switch(t)
2214 case PT_MOVETO:
2215 if(lastmoveto >= 0 && firstp != lastp)
2216 _EvaluateLine(lastp.x, lastp.y, firstp.x, firstp.y);
2217 lastmoveto = i;
2218 fFirstSet = false;
2219 lastp = path_data.mpPathPoints[i];
2220 break;
2221 case PT_MOVETONC:
2222 break;
2223 case PT_LINETO:
2224 if(path_data.mPathPoints - (i-1) >= 2) _EvaluateLine(path_data, i-1, i);
2225 break;
2226 case PT_BEZIERTO:
2227 if(path_data.mPathPoints - (i-1) >= 4) _EvaluateBezier(path_data, i-1, false);
2228 i += 2;
2229 break;
2230 case PT_BSPLINETO:
2231 if(path_data.mPathPoints - (i-1) >= 4) _EvaluateBezier(path_data, i-1, true);
2232 i += 2;
2233 break;
2234 case PT_BSPLINEPATCHTO:
2235 if(path_data.mPathPoints - (i-3) >= 4) _EvaluateBezier(path_data, i-3, true);
2236 break;
2239 if(lastmoveto >= 0 && firstp != lastp)
2240 _EvaluateLine(lastp.x, lastp.y, firstp.x, firstp.y);
2241 // Convert the edges to spans. We couldn't do this before because some of
2242 // the regions may have winding numbers >+1 and it would have been a pain
2243 // to try to adjust the spans on the fly. We use one heap to detangle
2244 // a scanline's worth of edges from the singly-linked lists, and another
2245 // to collect the actual scans.
2246 std::vector<int> heap;
2247 mOutline.reserve(mEdgeNext / 2);
2248 __int64 y = 0;
2249 for(y=0; y<mHeight; ++y)
2251 int count = 0;
2252 // Detangle scanline into edge heap.
2253 for(unsigned ptr = (unsigned)(mpScanBuffer[y]&0xffffffff); ptr; ptr = mpEdgeBuffer[ptr].next)
2255 heap.push_back(mpEdgeBuffer[ptr].posandflag);
2257 // Sort edge heap. Note that we conveniently made the opening edges
2258 // one more than closing edges at the same spot, so we won't have any
2259 // problems with abutting spans.
2260 std::sort(heap.begin(), heap.end()/*begin() + heap.size()*/);
2261 // Process edges and add spans. Since we only check for a non-zero
2262 // winding number, it doesn't matter which way the outlines go!
2263 std::vector<int>::iterator itX1 = heap.begin();
2264 std::vector<int>::iterator itX2 = heap.end(); // begin() + heap.size();
2265 int x1, x2;
2266 for(; itX1 != itX2; ++itX1)
2268 int x = *itX1;
2269 if(!count)
2270 x1 = (x>>1);
2271 if(x&1)
2272 ++count;
2273 else
2274 --count;
2275 if(!count)
2277 x2 = (x>>1);
2278 if(x2>x1)
2279 mOutline.push_back(std::pair<__int64,__int64>((y<<32)+x1+0x4000000040000000i64, (y<<32)+x2+0x4000000040000000i64)); // G: damn Avery, this is evil! :)
2282 heap.clear();
2284 // Dump the edge and scan buffers, since we no longer need them.
2285 free(mpEdgeBuffer);
2286 delete [] mpScanBuffer;
2287 // All done!
2288 return true;
2291 using namespace std;
2293 void ScanLineData::DeleteOutlines()
2295 mOutline.clear();
2298 void ScanLineData2::_OverlapRegion(tSpanBuffer& dst, const tSpanBuffer& src, int dx, int dy)
2300 tSpanBuffer temp;
2301 temp.reserve(dst.size() + src.size());
2302 dst.swap(temp);
2303 tSpanBuffer::iterator itA = temp.begin();
2304 tSpanBuffer::iterator itAE = temp.end();
2305 tSpanBuffer::const_iterator itB = src.begin();
2306 tSpanBuffer::const_iterator itBE = src.end();
2307 // Don't worry -- even if dy<0 this will still work! // G: hehe, the evil twin :)
2308 unsigned __int64 offset1 = (((__int64)dy)<<32) - dx;
2309 unsigned __int64 offset2 = (((__int64)dy)<<32) + dx;
2310 while(itA != itAE && itB != itBE)
2312 if((*itB).first + offset1 < (*itA).first)
2314 // B span is earlier. Use it.
2315 unsigned __int64 x1 = (*itB).first + offset1;
2316 unsigned __int64 x2 = (*itB).second + offset2;
2317 ++itB;
2318 // B spans don't overlap, so begin merge loop with A first.
2319 for(;;)
2321 // If we run out of A spans or the A span doesn't overlap,
2322 // then the next B span can't either (because B spans don't
2323 // overlap) and we exit.
2324 if(itA == itAE || (*itA).first > x2)
2325 break;
2326 do {x2 = _MAX(x2, (*itA++).second);}
2327 while(itA != itAE && (*itA).first <= x2);
2328 // If we run out of B spans or the B span doesn't overlap,
2329 // then the next A span can't either (because A spans don't
2330 // overlap) and we exit.
2331 if(itB == itBE || (*itB).first + offset1 > x2)
2332 break;
2333 do {x2 = _MAX(x2, (*itB++).second + offset2);}
2334 while(itB != itBE && (*itB).first + offset1 <= x2);
2336 // Flush span.
2337 dst.push_back(tSpan(x1, x2));
2339 else
2341 // A span is earlier. Use it.
2342 unsigned __int64 x1 = (*itA).first;
2343 unsigned __int64 x2 = (*itA).second;
2344 ++itA;
2345 // A spans don't overlap, so begin merge loop with B first.
2346 for(;;)
2348 // If we run out of B spans or the B span doesn't overlap,
2349 // then the next A span can't either (because A spans don't
2350 // overlap) and we exit.
2351 if(itB == itBE || (*itB).first + offset1 > x2)
2352 break;
2353 do {x2 = _MAX(x2, (*itB++).second + offset2);}
2354 while(itB != itBE && (*itB).first + offset1 <= x2);
2355 // If we run out of A spans or the A span doesn't overlap,
2356 // then the next B span can't either (because B spans don't
2357 // overlap) and we exit.
2358 if(itA == itAE || (*itA).first > x2)
2359 break;
2360 do {x2 = _MAX(x2, (*itA++).second);}
2361 while(itA != itAE && (*itA).first <= x2);
2363 // Flush span.
2364 dst.push_back(tSpan(x1, x2));
2367 // Copy over leftover spans.
2368 while(itA != itAE)
2369 dst.push_back(*itA++);
2370 while(itB != itBE)
2372 dst.push_back(tSpan((*itB).first + offset1, (*itB).second + offset2));
2373 ++itB;
2377 bool ScanLineData2::CreateWidenedRegion(int rx, int ry)
2379 if(rx < 0) rx = 0;
2380 if(ry < 0) ry = 0;
2381 mWideBorder = max(rx,ry);
2382 mWideOutline.clear();
2384 const tSpanBuffer& out_line = m_scan_line_data->mOutline;
2385 if (ry > 0)
2387 // Do a half circle.
2388 // _OverlapRegion mirrors this so both halves are done.
2389 for(int y = -ry; y <= ry; ++y)
2391 int x = (int)(0.5 + sqrt(float(ry*ry - y*y)) * float(rx)/float(ry));
2392 _OverlapRegion(mWideOutline, out_line, x, y);
2395 else if (ry == 0 && rx > 0)
2397 // There are artifacts if we don't make at least two overlaps of the line, even at same Y coord
2398 _OverlapRegion(mWideOutline, out_line, rx, 0);
2399 _OverlapRegion(mWideOutline, out_line, rx, 0);
2401 return true;