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)
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
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 */
38 #define _IMPL_MAX _MAX
39 #define _IMPL_MIN _MIN
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 { \
48 *(u)=((color)>>8) &0xff; \
49 *(y)=((color)>>16)&0xff;\
50 *(a)=((color)>>24)&0xff;\
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
);
62 int generate_tables(double sigma
);
73 struct ass_synth_priv_key
75 const double& operator()(const ass_synth_priv
& x
)const
84 ass_tmp_buf(size_t size
);
85 ass_tmp_buf(const ass_tmp_buf
& buf
);
91 struct ass_tmp_buf_get_size
93 const size_t& operator()(const ass_tmp_buf
& buf
)const
99 static const unsigned int maxcolor
= 255;
100 static const unsigned base
= 256;
102 ass_synth_priv::ass_synth_priv(const double sigma
)
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) {
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()
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;
145 if (this->sigma
== sigma
)
150 this->g_w
= (int)ceil(sigma
*3) | 1;
151 this->g_r
= this->g_w
/ 2;
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
) {
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
)
172 volume_start
+= exp_0
;
173 volume_start
+= exp_0
;
176 // for (i = 0; i < this->g_w; ++i) {
177 // volume_start += exp(a * (i - this->g_r) * (i - this->g_r));
180 volume_end
= (TARGET_VOLUME
+g_w
)/volume_start
;
181 volume_start
= (TARGET_VOLUME
-g_w
)/volume_start
;
184 while( volume_start
+0.000001<volume_end
)
186 volume_factor
= (volume_start
+volume_end
)*0.5;
189 exp_0
= volume_factor
;
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
++)
202 *p_left
= static_cast<int>(exp_0
+.5);
204 volume
+= (*p_left
<<1);
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];
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
) )
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
;
234 volume_factor
= volume_end
;
236 exp_0
= volume_factor
;
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
++)
249 *p_left
= static_cast<int>(exp_0
+.5);
251 volume
+= (*p_left
<<1);
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];
261 for (int mx
= 0; mx
< this->g_w
; mx
++) {
263 unsigned *p_gt2
= this->gt2
+ mx
;
265 for (int i
= 1; i
< 256; i
++) {
266 last_mul
= last_mul
+this->g
[mx
];
270 // this->gt2[this->g_w * i+ mx] = this->g[mx] * i;
277 ass_tmp_buf::ass_tmp_buf(size_t size
)
279 tmp
= (unsigned *)malloc(size
* sizeof(unsigned));
283 ass_tmp_buf::ass_tmp_buf(const ass_tmp_buf
& buf
)
286 tmp
= (unsigned *)malloc(size
* sizeof(unsigned));
289 ass_tmp_buf::~ass_tmp_buf()
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
,
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
));
309 if(x
< r
)//in case that r < 0
311 const int src
= s
[x
];
313 register unsigned *dstp
= t
+ x
- r
;
315 const unsigned *m3
= m2
+ src
* mwidth
;
317 for (mx
= mwidth
-1; mx
>= r
- x
; mx
--) {
324 for (x
= 1; x
< r
; x
++) {
325 const int src
= s
[x
];
327 register unsigned *dstp
= t
+ x
- r
;
329 const unsigned *m3
= m2
+ src
* mwidth
;
330 for (mx
= r
- x
; mx
< mwidth
; mx
++) {
336 for (; x
< width
- r
; x
++) {
337 const int src
= s
[x
];
339 register unsigned *dstp
= t
+ x
- r
;
341 const unsigned *m3
= m2
+ src
* mwidth
;
342 for (mx
= 0; mx
< mwidth
; mx
++) {
348 for (; x
< width
-1; x
++) {
349 const int src
= s
[x
];
351 register unsigned *dstp
= t
+ x
- r
;
353 const int x2
= r
+ width
- x
;
354 const unsigned *m3
= m2
+ src
* mwidth
;
355 for (mx
= 0; mx
< x2
; mx
++) {
360 if(x
==width
-1) //important: x==width-1 failed, if r==0
362 const int src
= s
[x
];
364 register unsigned *dstp
= t
+ x
- r
;
366 const int x2
= r
+ width
- x
;
367 const unsigned *m3
= m2
+ src
* mwidth
;
369 for (mx
= 0; mx
< x2
; mx
++) {
381 for (x
= 0; x
< width
; x
++) {
383 if(y
< r
)//in case that r<0
385 unsigned *srcp
= t
+ y
* (width
+ 1) + 1;
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
;
393 *srcp
= (1<<(ass_synth_priv::VOLUME_BITS
-1));
394 for (mx
= mwidth
-1; mx
>=r
- y
; mx
--) {
401 for (y
= 1; y
< r
; y
++) {
402 unsigned *srcp
= t
+ y
* (width
+ 1) + 1;
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
;
410 *srcp
= (1<<(ass_synth_priv::VOLUME_BITS
-1));
411 for (mx
= r
- y
; mx
< mwidth
; mx
++) {
417 for (; y
< height
- r
; y
++) {
418 unsigned *srcp
= t
+ y
* (width
+ 1) + 1;
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
;
426 *srcp
= (1<<(ass_synth_priv::VOLUME_BITS
-1));
427 for (mx
= 0; mx
< mwidth
; mx
++) {
433 for (; y
< height
-1; y
++) {
434 unsigned *srcp
= t
+ y
* (width
+ 1) + 1;
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
;
443 *srcp
= (1<<(ass_synth_priv::VOLUME_BITS
-1));
444 for (mx
= 0; mx
< y2
; mx
++) {
450 if(y
== height
- 1)//important: y == height - 1 failed if r==0
452 unsigned *srcp
= t
+ y
* (width
+ 1) + 1;
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
;
461 *srcp
= (1<<(ass_synth_priv::VOLUME_BITS
-1));
462 for (mx
= 0; mx
< y2
; mx
++) {
474 for (y
= 0; y
< height
; y
++) {
475 for (x
= 0; x
< width
; x
++) {
476 s
[x
] = t
[x
] >> ass_synth_priv::VOLUME_BITS
;
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
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;
501 unsigned char *src
=buf
+y
*stride
;
504 int old_pix
= src
[x
-1];
505 int old_sum
= old_pix
+ src
[x
-2];
506 for ( ; x
< w
; x
++) {
508 int temp2
= old_pix
+ temp1
;
510 temp1
= old_sum
+ temp2
;
512 col_pix_buf
[x
] = temp1
;
517 unsigned char *src
=buf
+y
*stride
;
521 int old_pix
= src
[x
-1];
522 int old_sum
= old_pix
+ src
[x
-2];
523 for ( ; x
< w
; x
++) {
525 int temp2
= old_pix
+ temp1
;
527 temp1
= 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
;
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
++) {
575 int temp2
= old_pix
+ temp1
;
577 temp1
= 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
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
;
606 for(int x
= 0; x
< w
; x
++)
609 int temp2
= temp1
*x_factor
;
615 temp2
= temp1
*y_factor
;
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 ScanLineData
& scan_line_data
, int xsub
, int ysub
, SharedPtrOverlay overlay
)
628 using namespace ::boost::flyweights
;
636 if(!scan_line_data
.mWidth
|| !scan_line_data
.mHeight
)
643 int width
= scan_line_data
.mWidth
+ xsub
;
644 int height
= scan_line_data
.mHeight
+ ysub
;
645 overlay
->mOffsetX
= scan_line_data
.mPathOffsetX
- xsub
;
646 overlay
->mOffsetY
= scan_line_data
.mPathOffsetY
- ysub
;
647 int wide_border
= (scan_line_data
.mWideBorder
+7)&~7;
648 overlay
->mfWideOutlineEmpty
= scan_line_data
.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_data
.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
);
686 int last
= (x2
-1)>>3;
687 byte
* dst
= plan_selected
+ (pitch
*(y
>>3) + first
);
692 *dst
+= ((first
+1)<<3) - x1
;
694 while(++first
< last
)
699 *dst
+= x2
- (last
<<3);
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
;
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
;
729 if(fBlur
|| fGaussianBlur
> 0.1)
731 if (fGaussianBlur
> 0)
732 bluradjust
+= (int)(fGaussianBlur
*3*8 + 0.5) | 1;
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);
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
;
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
);
799 ///////////////////////////////////////////////////////////////////////////
801 static __forceinline
void pixmix(DWORD
*dst
, DWORD color
, DWORD alpha
)
803 int a
= (((alpha
)*(color
>>24))>>6)&0xff;
804 // Make sure both a and ia are in range 1..256 for the >>8 operations below to be correct
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;
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;
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;
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
);
868 //return (b > a) ? 0 : a - b;
872 * No aligned requirement
875 void AlphaBlt(byte
* pY
,
876 const byte
* pAlphaMask
,
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
;
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
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;
948 for( ; h
>0; h
--, pAlphaMask
+= src_stride
, pY
+= dst_stride
)
950 const BYTE
* sa
= pAlphaMask
;
952 const BYTE
* dy_end
= pY
+ w
;
954 for(;dy
< dy_end
; sa
++, dy
++)
956 *dy
= (*dy
* (256 - *sa
)+ Y
*(*sa
+1))>>8;
963 // For CPUID usage in Rasterizer::Draw
964 #include "../dsutil/vd.h"
966 static const __int64 _00ff00ff00ff00ff
= 0x00ff00ff00ff00ffi
64;
968 // Render a subpicture onto a surface.
969 // spd is the surface to render on.
970 // clipRect is a rectangular clip region to render inside.
971 // pAlphaMask is an alpha clipping mask.
973 // switchpts seems to be an array of fill colours interlaced with coordinates.
974 // switchpts[i*2] contains a colour and switchpts[i*2+1] contains the coordinate to use that colour from
975 // fBody tells whether to render the body of the subs.
976 // fBorder tells whether to render the border of the subs.
977 SharedPtrByte
Rasterizer::CompositeAlphaMask(SubPicDesc
& spd
, SharedPtrOverlay overlay
, const CRect
& clipRect
, byte
* pAlphaMask
,
978 int xsub
, int ysub
, const DWORD
* switchpts
, bool fBody
, bool fBorder
,
979 CRect
*outputDirtyRect
)
981 //fix me: check and log error
982 SharedPtrByte result
;
983 *outputDirtyRect
= CRect(0, 0, 0, 0);
984 if(!switchpts
|| !fBody
&& !fBorder
) return(result
);
987 // Limit drawn area to intersection of rendering surface and rectangular clip area
988 CRect
r(0, 0, spd
.w
, spd
.h
);
990 // Remember that all subtitle coordinates are specified in 1/8 pixels
991 // (x+4)>>3 rounds to nearest whole pixel.
992 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
993 int x
= (xsub
+ overlay
->mOffsetX
+ 4)>>3;
994 int y
= (ysub
+ overlay
->mOffsetY
+ 4)>>3;
995 int w
= overlay
->mOverlayWidth
;
996 int h
= overlay
->mOverlayHeight
;
999 if(x
< r
.left
) {xo
= r
.left
-x
; w
-= r
.left
-x
; x
= r
.left
;}
1000 if(y
< r
.top
) {yo
= r
.top
-y
; h
-= r
.top
-y
; y
= r
.top
;}
1001 if(x
+w
> r
.right
) w
= r
.right
-x
;
1002 if(y
+h
> r
.bottom
) h
= r
.bottom
-y
;
1003 // Check if there's actually anything to render
1004 if(w
<= 0 || h
<= 0) return(result
);
1005 outputDirtyRect
->SetRect(x
, y
, x
+w
, y
+h
);
1006 *outputDirtyRect
&= CRect(0, 0, spd
.w
, spd
.h
);
1008 bool fSingleColor
= (switchpts
[1]==0xffffffff);
1011 // Grab the first colour
1012 DWORD color
= switchpts
[0];
1013 byte
* s_base
= (byte
*)xy_malloc(overlay
->mOverlayPitch
* overlay
->mOverlayHeight
);
1017 overlay
->FillAlphaMash(s_base
, fBody
, fBorder
, xo
, yo
, w
, h
,
1018 pAlphaMask
==NULL
? NULL
: pAlphaMask
+ spd
.w
* y
+ x
, spd
.w
,
1024 const DWORD
*sw
= switchpts
;
1025 while( last_x
<w
+xo
)
1027 byte alpha
= sw
[0]>>24;
1028 while( sw
[3]<w
+xo
&& (sw
[2]>>24)==alpha
)
1032 int new_x
= sw
[3] < w
+xo
? sw
[3] : w
+xo
;
1033 overlay
->FillAlphaMash(s_base
, fBody
, fBorder
,
1034 last_x
, yo
, new_x
-last_x
, h
,
1035 pAlphaMask
==NULL
? NULL
: pAlphaMask
+ spd
.w
* y
+ x
+ last_x
- xo
, spd
.w
,
1041 result
.reset( s_base
, xy_free
);
1045 CRect
Rasterizer::Draw(SubPicDesc
& spd
, SharedPtrOverlay overlay
, const CRect
& clipRect
, byte
* pAlphaMask
,
1046 int xsub
, int ysub
, const DWORD
* switchpts
, bool fBody
, bool fBorder
)
1048 CRect
bbox(0,0,0,0);
1049 if(!switchpts
|| !fBody
&& !fBorder
) return(bbox
);
1052 // Limit drawn area to intersection of rendering surface and rectangular clip area
1053 CRect
r(0, 0, spd
.w
, spd
.h
);
1055 // Remember that all subtitle coordinates are specified in 1/8 pixels
1056 // (x+4)>>3 rounds to nearest whole pixel.
1057 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
1058 int overlayPitch
= overlay
->mOverlayPitch
;
1059 int x
= (xsub
+ overlay
->mOffsetX
+ 4)>>3;
1060 int y
= (ysub
+ overlay
->mOffsetY
+ 4)>>3;
1061 int w
= overlay
->mOverlayWidth
;
1062 int h
= overlay
->mOverlayHeight
;
1065 if(x
< r
.left
) {xo
= r
.left
-x
; w
-= r
.left
-x
; x
= r
.left
;}
1066 if(y
< r
.top
) {yo
= r
.top
-y
; h
-= r
.top
-y
; y
= r
.top
;}
1067 if(x
+w
> r
.right
) w
= r
.right
-x
;
1068 if(y
+h
> r
.bottom
) h
= r
.bottom
-y
;
1069 // Check if there's actually anything to render
1070 if(w
<= 0 || h
<= 0) return(bbox
);
1078 SINGLE_COLOR
= 1<<2,
1084 bool fSSE2
= !!(g_cpuid
.m_flags
& CCpuID::sse2
);
1085 bool fSingleColor
= (switchpts
[1]==0xffffffff);
1086 bool AYUV_PLANAR
= (spd
.type
==MSP_AYUV_PLANAR
);
1087 int draw_method
= 0;
1089 draw_method
|= DM::SINGLE_COLOR
;
1091 draw_method
|= DM::SSE2
;
1093 draw_method
|= DM::AYUV_PLANAR
;
1096 // Grab the first colour
1097 DWORD color
= switchpts
[0];
1098 SharedPtrByte s_base
= CompositeAlphaMask(spd
, overlay
, clipRect
, pAlphaMask
, xsub
, ysub
, switchpts
,
1099 fBody
, fBorder
, &bbox
);
1100 const byte
* s
= s_base
.get() + overlay
->mOverlayPitch
*yo
+ xo
;
1102 // How would this differ from src?
1103 unsigned long* dst
= (unsigned long *)(((char *)spd
.bits
+ spd
.pitch
* y
) + ((x
*spd
.bpp
)>>3));
1105 // Every remaining line in the bitmap to be rendered...
1108 case DM::SINGLE_COLOR
| DM::SSE2
| 0*DM::AYUV_PLANAR
:
1112 for(int wt
=0; wt
<w
; ++wt
)
1113 // The <<6 is due to pixmix expecting the alpha parameter to be
1114 // the multiplication of two 6-bit unsigned numbers but we
1115 // only have one here. (No alpha mask.)
1116 pixmix_sse2(&dst
[wt
], color
, s
[wt
]);
1118 dst
= (unsigned long *)((char *)dst
+ spd
.pitch
);
1122 case DM::SINGLE_COLOR
| 0*DM::SSE2
| 0*DM::AYUV_PLANAR
:
1126 for(int wt
=0; wt
<w
; ++wt
)
1127 pixmix(&dst
[wt
], color
, s
[wt
]);
1129 dst
= (unsigned long *)((char *)dst
+ spd
.pitch
);
1133 case 0*DM::SINGLE_COLOR
| DM::SSE2
| 0*DM::AYUV_PLANAR
:
1137 const DWORD
*sw
= switchpts
;
1138 for(int wt
=0; wt
<w
; ++wt
)
1140 // xo is the offset (usually negative) we have moved into the image
1141 // So if we have passed the switchpoint (?) switch to another colour
1142 // (So switchpts stores both colours *and* coordinates?)
1143 if(wt
+xo
>= sw
[1]) {while(wt
+xo
>= sw
[1]) sw
+= 2; color
= sw
[-2];}
1144 pixmix_sse2(&dst
[wt
], color
, s
[wt
]);
1147 dst
= (unsigned long *)((char *)dst
+ spd
.pitch
);
1151 case 0*DM::SINGLE_COLOR
| 0*DM::SSE2
| 0*DM::AYUV_PLANAR
:
1155 const DWORD
*sw
= switchpts
;
1156 for(int wt
=0; wt
<w
; ++wt
)
1158 if(wt
+xo
>= sw
[1]) {while(wt
+xo
>= sw
[1]) sw
+= 2; color
= sw
[-2];}
1159 pixmix(&dst
[wt
], color
, s
[wt
]);
1162 dst
= (unsigned long *)((char *)dst
+ spd
.pitch
);
1166 case DM::SINGLE_COLOR
| DM::SSE2
| DM::AYUV_PLANAR
:
1168 unsigned char* dst_A
= (unsigned char*)dst
;
1169 unsigned char* dst_Y
= dst_A
+ spd
.pitch
*spd
.h
;
1170 unsigned char* dst_U
= dst_Y
+ spd
.pitch
*spd
.h
;
1171 unsigned char* dst_V
= dst_U
+ spd
.pitch
*spd
.h
;
1173 AlphaBlt(dst_Y
, s
, ((color
)>>16)&0xff, h
, w
, overlayPitch
, spd
.pitch
);
1174 AlphaBlt(dst_U
, s
, ((color
)>>8)&0xff, h
, w
, overlayPitch
, spd
.pitch
);
1175 AlphaBlt(dst_V
, s
, ((color
))&0xff, h
, w
, overlayPitch
, spd
.pitch
);
1176 AlphaBlt(dst_A
, s
, 0, h
, w
, overlayPitch
, spd
.pitch
);
1179 case 0*DM::SINGLE_COLOR
| DM::SSE2
| DM::AYUV_PLANAR
:
1181 unsigned char* dst_A
= (unsigned char*)dst
;
1182 unsigned char* dst_Y
= dst_A
+ spd
.pitch
*spd
.h
;
1183 unsigned char* dst_U
= dst_Y
+ spd
.pitch
*spd
.h
;
1184 unsigned char* dst_V
= dst_U
+ spd
.pitch
*spd
.h
;
1186 const DWORD
*sw
= switchpts
;
1191 int new_x
= sw
[3] < w
+xo
? sw
[3] : w
+xo
;
1194 AlphaBlt(dst_Y
, s
+ last_x
- xo
, (color
>>16)&0xff, h
, new_x
-last_x
, overlayPitch
, spd
.pitch
);
1195 AlphaBlt(dst_U
, s
+ last_x
- xo
, (color
>>8)&0xff, h
, new_x
-last_x
, overlayPitch
, spd
.pitch
);
1196 AlphaBlt(dst_V
, s
+ last_x
- xo
, (color
)&0xff, h
, new_x
-last_x
, overlayPitch
, spd
.pitch
);
1197 AlphaBlt(dst_A
, s
+ last_x
- xo
, 0, h
, new_x
-last_x
, overlayPitch
, spd
.pitch
);
1199 dst_A
+= new_x
- last_x
;
1200 dst_Y
+= new_x
- last_x
;
1201 dst_U
+= new_x
- last_x
;
1202 dst_V
+= new_x
- last_x
;
1207 case DM::SINGLE_COLOR
| 0*DM::SSE2
| DM::AYUV_PLANAR
:
1209 // char * debug_dst=(char*)dst;int h2 = h;
1210 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", (char*)&color, sizeof(color)) );
1211 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1212 // debug_dst += spd.pitch*spd.h;
1213 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1214 // debug_dst += spd.pitch*spd.h;
1215 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1216 // debug_dst += spd.pitch*spd.h;
1217 // XY_DO_ONCE( xy_logger::write_file("G:\\b2_rt", debug_dst, (h2-1)*spd.pitch) );
1218 // debug_dst=(char*)dst;
1220 unsigned char* dst_A
= (unsigned char*)dst
;
1221 unsigned char* dst_Y
= dst_A
+ spd
.pitch
*spd
.h
;
1222 unsigned char* dst_U
= dst_Y
+ spd
.pitch
*spd
.h
;
1223 unsigned char* dst_V
= dst_U
+ spd
.pitch
*spd
.h
;
1226 for(int wt
=0; wt
<w
; ++wt
)
1228 DWORD temp
= COMBINE_AYUV(dst_A
[wt
], dst_Y
[wt
], dst_U
[wt
], dst_V
[wt
]);
1229 pixmix(&temp
, color
, s
[wt
]);
1230 SPLIT_AYUV(temp
, dst_A
+wt
, dst_Y
+wt
, dst_U
+wt
, dst_V
+wt
);
1238 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1239 // debug_dst += spd.pitch*spd.h;
1240 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1241 // debug_dst += spd.pitch*spd.h;
1242 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1243 // debug_dst += spd.pitch*spd.h;
1244 // XY_DO_ONCE( xy_logger::write_file("G:\\a2_rt", debug_dst, (h2-1)*spd.pitch) );
1247 case 0*DM::SINGLE_COLOR
| 0*DM::SSE2
| DM::AYUV_PLANAR
:
1249 unsigned char* dst_A
= (unsigned char*)dst
;
1250 unsigned char* dst_Y
= dst_A
+ spd
.pitch
*spd
.h
;
1251 unsigned char* dst_U
= dst_Y
+ spd
.pitch
*spd
.h
;
1252 unsigned char* dst_V
= dst_U
+ spd
.pitch
*spd
.h
;
1255 const DWORD
*sw
= switchpts
;
1256 for(int wt
=0; wt
<w
; ++wt
)
1258 if(wt
+xo
>= sw
[1]) {while(wt
+xo
>= sw
[1]) sw
+= 2; color
= sw
[-2];}
1259 DWORD temp
= COMBINE_AYUV(dst_A
[wt
], dst_Y
[wt
], dst_U
[wt
], dst_V
[wt
]);
1260 pixmix(&temp
, color
, (s
[wt
]*(color
>>24))>>8);
1261 SPLIT_AYUV(temp
, dst_A
+wt
, dst_Y
+wt
, dst_U
+wt
, dst_V
+wt
);
1272 // Remember to EMMS!
1273 // Rendering fails in funny ways if we don't do this.
1278 CRect
Rasterizer::Draw( SubPicDesc
& spd
, DrawItem
& draw_item
)
1280 return Draw(spd
, draw_item
.overlay
, draw_item
.clip_rect
, draw_item
.alpha_mask
.get(),
1281 draw_item
.xsub
, draw_item
.ysub
, draw_item
.switchpts
, draw_item
.fBody
, draw_item
.fBorder
);
1284 DrawItem
* Rasterizer::CreateDrawItem( SubPicDesc
& spd
, SharedPtrOverlay overlay
, const CRect
& clipRect
, SharedArrayByte pAlphaMask
, int xsub
, int ysub
, const DWORD
* switchpts
, bool fBody
, bool fBorder
)
1286 DrawItem
* result
= new DrawItem();
1287 result
->overlay
= overlay
;
1288 result
->clip_rect
= clipRect
;
1289 result
->alpha_mask
= pAlphaMask
;
1290 result
->xsub
= xsub
;
1291 result
->ysub
= ysub
;
1293 memcpy(result
->switchpts
, switchpts
, sizeof(result
->switchpts
));
1294 result
->fBody
= fBody
;
1295 result
->fBorder
= fBorder
;
1299 CRect
Rasterizer::DryDraw( SubPicDesc
& spd
, SharedPtrOverlay overlay
, const CRect
& clipRect
, byte
* pAlphaMask
, int xsub
, int ysub
, const DWORD
* switchpts
, bool fBody
, bool fBorder
)
1301 CRect
bbox(0, 0, 0, 0);
1302 if(!switchpts
|| !fBody
&& !fBorder
) return(bbox
);
1305 // Limit drawn area to intersection of rendering surface and rectangular clip area
1306 CRect
r(0, 0, spd
.w
, spd
.h
);
1308 // Remember that all subtitle coordinates are specified in 1/8 pixels
1309 // (x+4)>>3 rounds to nearest whole pixel.
1310 // ??? What is xsub, ysub, mOffsetX and mOffsetY ?
1311 int overlayPitch
= overlay
->mOverlayPitch
;
1312 int x
= (xsub
+ overlay
->mOffsetX
+ 4)>>3;
1313 int y
= (ysub
+ overlay
->mOffsetY
+ 4)>>3;
1314 int w
= overlay
->mOverlayWidth
;
1315 int h
= overlay
->mOverlayHeight
;
1318 if(x
< r
.left
) {xo
= r
.left
-x
; w
-= r
.left
-x
; x
= r
.left
;}
1319 if(y
< r
.top
) {yo
= r
.top
-y
; h
-= r
.top
-y
; y
= r
.top
;}
1320 if(x
+w
> r
.right
) w
= r
.right
-x
;
1321 if(y
+h
> r
.bottom
) h
= r
.bottom
-y
;
1322 // Check if there's actually anything to render
1323 if(w
<= 0 || h
<= 0) return(bbox
);
1324 bbox
.SetRect(x
, y
, x
+w
, y
+h
);
1325 bbox
&= CRect(0, 0, spd
.w
, spd
.h
);
1330 CRect
Rasterizer::DryDraw( SubPicDesc
& spd
, DrawItem
& draw_item
)
1332 return DryDraw(spd
, draw_item
.overlay
, draw_item
.clip_rect
, draw_item
.alpha_mask
.get(),
1333 draw_item
.xsub
, draw_item
.ysub
, draw_item
.switchpts
, draw_item
.fBody
, draw_item
.fBorder
);
1335 ///////////////////////////////////////////////////////////////
1339 void Overlay::_DoFillAlphaMash(byte
* outputAlphaMask
, const byte
* pBody
, const byte
* pBorder
, int x
, int y
, int w
, int h
, const byte
* pAlphaMask
, int pitch
, DWORD color_alpha
)
1341 pBody
= pBody
!=NULL
? pBody
+ y
*mOverlayPitch
+ x
: NULL
;
1342 pBorder
= pBorder
!=NULL
? pBorder
+ y
*mOverlayPitch
+ x
: NULL
;
1343 byte
* dst
= outputAlphaMask
+ y
*mOverlayPitch
+ x
;
1345 const int x0
= ((reinterpret_cast<int>(dst
)+3)&~3) - reinterpret_cast<int>(dst
) < w
?
1346 ((reinterpret_cast<int>(dst
)+3)&~3) - reinterpret_cast<int>(dst
) : w
; //IMPORTANT! Should not exceed w.
1347 const int x00
= ((reinterpret_cast<int>(dst
)+15)&~15) - reinterpret_cast<int>(dst
) < w
?
1348 ((reinterpret_cast<int>(dst
)+15)&~15) - reinterpret_cast<int>(dst
) : w
;//IMPORTANT! Should not exceed w.
1349 const int x_end00
= ((reinterpret_cast<int>(dst
)+w
)&~15) - reinterpret_cast<int>(dst
);
1350 const int x_end0
= ((reinterpret_cast<int>(dst
)+w
)&~3) - reinterpret_cast<int>(dst
);
1351 const int x_end
= w
;
1353 __m64 color_alpha_64
= _mm_set1_pi16(color_alpha
);
1354 __m128i color_alpha_128
= _mm_set1_epi16(color_alpha
);
1356 if(pAlphaMask
==NULL
&& pBody
!=NULL
&& pBorder
!=NULL
)
1361 mov eax, color_alpha
1363 punpcklwd XMM3, XMM3
1364 pshufd XMM3, XMM3, 0
1372 int temp
= pBorder
[j
]-pBody
[j
];
1373 temp
= temp
<0 ? 0 : temp
;
1374 dst
[j
] = (temp
* color_alpha
)>>6;
1378 __m64 border
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder
+j
));
1379 __m64 body
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody
+j
));
1380 border
= _mm_subs_pu8(border
, body
);
1381 __m64 zero
= _mm_setzero_si64();
1382 border
= _mm_unpacklo_pi8(border
, zero
);
1383 border
= _mm_mullo_pi16(border
, color_alpha_64
);
1384 border
= _mm_srli_pi16(border
, 6);
1385 border
= _mm_packs_pu16(border
,border
);
1386 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(border
);
1388 __m128i zero
= _mm_setzero_si128();
1389 for( ;j
<x_end00
;j
+=16)
1391 __m128i border
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pBorder
+j
));
1392 __m128i body
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pBody
+j
));
1393 border
= _mm_subs_epu8(border
,body
);
1394 __m128i srchi
= border
;
1395 border
= _mm_unpacklo_epi8(border
, zero
);
1396 srchi
= _mm_unpackhi_epi8(srchi
, zero
);
1397 border
= _mm_mullo_epi16(border
, color_alpha_128
);
1398 srchi
= _mm_mullo_epi16(srchi
, color_alpha_128
);
1399 border
= _mm_srli_epi16(border
, 6);
1400 srchi
= _mm_srli_epi16(srchi
, 6);
1401 border
= _mm_packus_epi16(border
, srchi
);
1402 _mm_storeu_si128(reinterpret_cast<__m128i
*>(dst
+j
), border
);
1404 for( ;j
<x_end0
;j
+=4)
1406 __m64 border
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder
+j
));
1407 __m64 body
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody
+j
));
1408 border
= _mm_subs_pu8(border
, body
);
1409 __m64 zero
= _mm_setzero_si64();
1410 border
= _mm_unpacklo_pi8(border
, zero
);
1411 border
= _mm_mullo_pi16(border
, color_alpha_64
);
1412 border
= _mm_srli_pi16(border
, 6);
1413 border
= _mm_packs_pu16(border
,border
);
1414 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(border
);
1418 int temp
= pBorder
[j
]-pBody
[j
];
1419 temp
= temp
<0 ? 0 : temp
;
1420 dst
[j
] = (temp
* color_alpha
)>>6;
1422 pBody
+= mOverlayPitch
;
1423 pBorder
+= mOverlayPitch
;
1424 //pAlphaMask += pitch;
1425 dst
+= mOverlayPitch
;
1428 else if( ((pBody
==NULL
) + (pBorder
==NULL
))==1 && pAlphaMask
==NULL
)
1430 const BYTE
* src1
= pBody
!=NULL
? pBody
: pBorder
;
1436 dst
[j
] = (src1
[j
] * color_alpha
)>>6;
1440 __m64 src
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1
+j
));
1441 __m64 zero
= _mm_setzero_si64();
1442 src
= _mm_unpacklo_pi8(src
, zero
);
1443 src
= _mm_mullo_pi16(src
, color_alpha_64
);
1444 src
= _mm_srli_pi16(src
, 6);
1445 src
= _mm_packs_pu16(src
,src
);
1446 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(src
);
1448 __m128i zero
= _mm_setzero_si128();
1449 for( ;j
<x_end00
;j
+=16)
1451 __m128i src
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(src1
+j
));
1452 __m128i srchi
= src
;
1453 src
= _mm_unpacklo_epi8(src
, zero
);
1454 srchi
= _mm_unpackhi_epi8(srchi
, zero
);
1455 src
= _mm_mullo_epi16(src
, color_alpha_128
);
1456 srchi
= _mm_mullo_epi16(srchi
, color_alpha_128
);
1457 src
= _mm_srli_epi16(src
, 6);
1458 srchi
= _mm_srli_epi16(srchi
, 6);
1459 src
= _mm_packus_epi16(src
, srchi
);
1460 _mm_storeu_si128(reinterpret_cast<__m128i
*>(dst
+j
), src
);
1462 for( ;j
<x_end0
;j
+=4)
1464 __m64 src
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1
+j
));
1465 __m64 zero
= _mm_setzero_si64();
1466 src
= _mm_unpacklo_pi8(src
, zero
);
1467 src
= _mm_mullo_pi16(src
, color_alpha_64
);
1468 src
= _mm_srli_pi16(src
, 6);
1469 src
= _mm_packs_pu16(src
,src
);
1470 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(src
);
1474 dst
[j
] = (src1
[j
] * color_alpha
)>>6;
1476 src1
+= mOverlayPitch
;
1477 //pAlphaMask += pitch;
1478 dst
+= mOverlayPitch
;
1481 else if( ((pBody
==NULL
) + (pBorder
==NULL
))==1 && pAlphaMask
!=NULL
)
1483 const BYTE
* src1
= pBody
!=NULL
? pBody
: pBorder
;
1489 dst
[j
] = (src1
[j
] * pAlphaMask
[j
] * color_alpha
)>>12;
1493 __m64 src
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1
+j
));
1494 __m64 mask
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask
+j
));
1495 __m64 zero
= _mm_setzero_si64();
1496 src
= _mm_unpacklo_pi8(src
, zero
);
1497 src
= _mm_mullo_pi16(src
, color_alpha_64
);
1498 mask
= _mm_unpacklo_pi8(zero
, mask
); //important!
1499 src
= _mm_mulhi_pi16(src
, mask
); //important!
1500 src
= _mm_srli_pi16(src
, 12+8-16); //important!
1501 src
= _mm_packs_pu16(src
,src
);
1502 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(src
);
1504 __m128i zero
= _mm_setzero_si128();
1505 for( ;j
<x_end00
;j
+=16)
1507 __m128i src
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(src1
+j
));
1508 __m128i mask
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pAlphaMask
+j
));
1509 __m128i srchi
= src
;
1510 __m128i maskhi
= mask
;
1511 src
= _mm_unpacklo_epi8(src
, zero
);
1512 srchi
= _mm_unpackhi_epi8(srchi
, zero
);
1513 mask
= _mm_unpacklo_epi8(zero
, mask
); //important!
1514 maskhi
= _mm_unpackhi_epi8(zero
, maskhi
);
1515 src
= _mm_mullo_epi16(src
, color_alpha_128
);
1516 srchi
= _mm_mullo_epi16(srchi
, color_alpha_128
);
1517 src
= _mm_mulhi_epu16(src
, mask
); //important!
1518 srchi
= _mm_mulhi_epu16(srchi
, maskhi
);
1519 src
= _mm_srli_epi16(src
, 12+8-16); //important!
1520 srchi
= _mm_srli_epi16(srchi
, 12+8-16);
1521 src
= _mm_packus_epi16(src
, srchi
);
1522 _mm_storeu_si128(reinterpret_cast<__m128i
*>(dst
+j
), src
);
1524 for( ;j
<x_end0
;j
+=4)
1526 __m64 src
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(src1
+j
));
1527 __m64 mask
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask
+j
));
1528 __m64 zero
= _mm_setzero_si64();
1529 src
= _mm_unpacklo_pi8(src
, zero
);
1530 src
= _mm_mullo_pi16(src
, color_alpha_64
);
1531 mask
= _mm_unpacklo_pi8(zero
, mask
); //important!
1532 src
= _mm_mulhi_pi16(src
, mask
); //important!
1533 src
= _mm_srli_pi16(src
, 12+8-16); //important!
1534 src
= _mm_packs_pu16(src
,src
);
1535 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(src
);
1539 dst
[j
] = (src1
[j
] * pAlphaMask
[j
] * color_alpha
)>>12;
1541 src1
+= mOverlayPitch
;
1542 pAlphaMask
+= pitch
;
1543 dst
+= mOverlayPitch
;
1546 else if( pAlphaMask
!=NULL
&& pBody
!=NULL
&& pBorder
!=NULL
)
1553 int temp
= pBorder
[j
]-pBody
[j
];
1554 temp
= temp
<0 ? 0 : temp
;
1555 dst
[j
] = (temp
* pAlphaMask
[j
] * color_alpha
)>>12;
1559 __m64 border
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder
+j
));
1560 __m64 body
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody
+j
));
1561 border
= _mm_subs_pu8(border
, body
);
1562 __m64 mask
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask
+j
));
1563 __m64 zero
= _mm_setzero_si64();
1564 border
= _mm_unpacklo_pi8(border
, zero
);
1565 border
= _mm_mullo_pi16(border
, color_alpha_64
);
1566 mask
= _mm_unpacklo_pi8(zero
, mask
); //important!
1567 border
= _mm_mulhi_pi16(border
, mask
); //important!
1568 border
= _mm_srli_pi16(border
, 12+8-16); //important!
1569 border
= _mm_packs_pu16(border
,border
);
1570 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(border
);
1572 __m128i zero
= _mm_setzero_si128();
1573 for( ;j
<x_end00
;j
+=16)
1575 __m128i border
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pBorder
+j
));
1576 __m128i body
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pBody
+j
));
1577 border
= _mm_subs_epu8(border
,body
);
1579 __m128i mask
= _mm_loadu_si128(reinterpret_cast<const __m128i
*>(pAlphaMask
+j
));
1580 __m128i srchi
= border
;
1581 __m128i maskhi
= mask
;
1582 border
= _mm_unpacklo_epi8(border
, zero
);
1583 srchi
= _mm_unpackhi_epi8(srchi
, zero
);
1584 mask
= _mm_unpacklo_epi8(zero
, mask
); //important!
1585 maskhi
= _mm_unpackhi_epi8(zero
, maskhi
);
1586 border
= _mm_mullo_epi16(border
, color_alpha_128
);
1587 srchi
= _mm_mullo_epi16(srchi
, color_alpha_128
);
1588 border
= _mm_mulhi_epu16(border
, mask
); //important!
1589 srchi
= _mm_mulhi_epu16(srchi
, maskhi
);
1590 border
= _mm_srli_epi16(border
, 12+8-16); //important!
1591 srchi
= _mm_srli_epi16(srchi
, 12+8-16);
1592 border
= _mm_packus_epi16(border
, srchi
);
1593 _mm_storeu_si128(reinterpret_cast<__m128i
*>(dst
+j
), border
);
1595 for( ;j
<x_end0
;j
+=4)
1597 __m64 border
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBorder
+j
));
1598 __m64 body
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pBody
+j
));
1599 border
= _mm_subs_pu8(border
, body
);
1600 __m64 mask
= _mm_cvtsi32_si64(*reinterpret_cast<const int*>(pAlphaMask
+j
));
1601 __m64 zero
= _mm_setzero_si64();
1602 border
= _mm_unpacklo_pi8(border
, zero
);
1603 border
= _mm_mullo_pi16(border
, color_alpha_64
);
1604 mask
= _mm_unpacklo_pi8(zero
, mask
); //important!
1605 border
= _mm_mulhi_pi16(border
, mask
); //important!
1606 border
= _mm_srli_pi16(border
, 12+8-16); //important!
1607 border
= _mm_packs_pu16(border
,border
);
1608 *reinterpret_cast<int*>(dst
+j
) = _mm_cvtsi64_si32(border
);
1612 int temp
= pBorder
[j
]-pBody
[j
];
1613 temp
= temp
<0 ? 0 : temp
;
1614 dst
[j
] = (temp
* pAlphaMask
[j
] * color_alpha
)>>12;
1616 pBody
+= mOverlayPitch
;
1617 pBorder
+= mOverlayPitch
;
1618 pAlphaMask
+= pitch
;
1619 dst
+= mOverlayPitch
;
1624 //should NOT happen!
1629 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
)
1631 if(!fBorder
&& fBody
&& pAlphaMask
==NULL
)
1633 _DoFillAlphaMash(outputAlphaMask
, mpOverlayBuffer
.body
, NULL
, x
, y
, w
, h
, pAlphaMask
, pitch
, color_alpha
);
1635 else if(/*fBorder &&*/ fBody
&& pAlphaMask
==NULL
)
1637 _DoFillAlphaMash(outputAlphaMask
, NULL
, mpOverlayBuffer
.border
, x
, y
, w
, h
, pAlphaMask
, pitch
, color_alpha
);
1639 else if(!fBody
&& fBorder
/* pAlphaMask==NULL or not*/)
1641 _DoFillAlphaMash(outputAlphaMask
, mpOverlayBuffer
.body
, mpOverlayBuffer
.border
, x
, y
, w
, h
, pAlphaMask
, pitch
, color_alpha
);
1643 else if(!fBorder
&& fBody
&& pAlphaMask
!=NULL
)
1645 _DoFillAlphaMash(outputAlphaMask
, mpOverlayBuffer
.body
, NULL
, x
, y
, w
, h
, pAlphaMask
, pitch
, color_alpha
);
1647 else if(fBorder
&& fBody
&& pAlphaMask
!=NULL
)
1649 _DoFillAlphaMash(outputAlphaMask
, NULL
, mpOverlayBuffer
.border
, x
, y
, w
, h
, pAlphaMask
, pitch
, color_alpha
);
1658 Overlay
* Overlay::GetSubpixelVariance(unsigned int xshift
, unsigned int yshift
)
1660 Overlay
* overlay
= new Overlay();
1668 overlay
->mOffsetX
= mOffsetX
- xshift
;
1669 overlay
->mOffsetY
= mOffsetY
- yshift
;
1670 overlay
->mWidth
= mWidth
+ xshift
;
1671 overlay
->mHeight
= mHeight
+ yshift
;
1673 overlay
->mOverlayWidth
= ((overlay
->mWidth
+7)>>3) + 1;
1674 overlay
->mOverlayHeight
= ((overlay
->mHeight
+ 7)>>3) + 1;
1675 overlay
->mOverlayPitch
= (overlay
->mOverlayWidth
+15)&~15;
1677 overlay
->mpOverlayBuffer
.base
= reinterpret_cast<byte
*>(xy_malloc(2 * overlay
->mOverlayPitch
* overlay
->mOverlayHeight
));
1678 overlay
->mpOverlayBuffer
.body
= overlay
->mpOverlayBuffer
.base
;
1679 overlay
->mpOverlayBuffer
.border
= overlay
->mpOverlayBuffer
.base
+ overlay
->mOverlayPitch
* overlay
->mOverlayHeight
;
1681 overlay
->mfWideOutlineEmpty
= mfWideOutlineEmpty
;
1683 if(overlay
->mOverlayWidth
==mOverlayWidth
&& overlay
->mOverlayHeight
==mOverlayHeight
)
1684 memcpy(overlay
->mpOverlayBuffer
.base
, mpOverlayBuffer
.base
, 2 * mOverlayPitch
* mOverlayHeight
);
1687 memset(overlay
->mpOverlayBuffer
.base
, 0, 2 * overlay
->mOverlayPitch
* overlay
->mOverlayHeight
);
1688 byte
* dst
= overlay
->mpOverlayBuffer
.body
;
1689 const byte
* src
= mpOverlayBuffer
.body
;
1690 for (int i
=0;i
<mOverlayHeight
;i
++)
1692 memcpy(dst
, src
, mOverlayPitch
);
1693 dst
+= overlay
->mOverlayPitch
;
1694 src
+= mOverlayPitch
;
1696 dst
= overlay
->mpOverlayBuffer
.border
;
1697 src
= mpOverlayBuffer
.border
;
1698 for (int i
=0;i
<mOverlayHeight
;i
++)
1700 memcpy(dst
, src
, mOverlayPitch
);
1701 dst
+= overlay
->mOverlayPitch
;
1702 src
+= mOverlayPitch
;
1706 // Bilinear(overlay->mpOverlayBuffer.base, overlay->mOverlayWidth, 2*overlay->mOverlayHeight, overlay->mOverlayPitch, xshift, yshift);
1707 Bilinear(overlay
->mpOverlayBuffer
.body
, overlay
->mOverlayWidth
, overlay
->mOverlayHeight
, overlay
->mOverlayPitch
, xshift
, yshift
);
1708 Bilinear(overlay
->mpOverlayBuffer
.border
, overlay
->mOverlayWidth
, overlay
->mOverlayHeight
, overlay
->mOverlayPitch
, xshift
, yshift
);
1712 ///////////////////////////////////////////////////////////////
1716 PathData::PathData():mpPathTypes(NULL
), mpPathPoints(NULL
), mPathPoints(0)
1720 PathData::PathData( const PathData
& src
):mPathPoints(src
.mPathPoints
)
1722 //TODO: deal with the case that src.mPathPoints<0
1725 mpPathTypes
= static_cast<BYTE
*>(malloc(mPathPoints
* sizeof(BYTE
)));
1726 mpPathPoints
= static_cast<POINT
*>(malloc(mPathPoints
* sizeof(POINT
)));
1730 memcpy(mpPathTypes
, src
.mpPathTypes
, mPathPoints
*sizeof(BYTE
));
1731 memcpy(mpPathPoints
, src
.mpPathPoints
, mPathPoints
*sizeof(POINT
));
1735 const PathData
& PathData::operator=( const PathData
& src
)
1739 if(mPathPoints
!=src
.mPathPoints
&& src
.mPathPoints
>0)
1741 mPathPoints
= src
.mPathPoints
;
1742 delete[] mpPathTypes
;
1743 delete[] mpPathPoints
;
1744 mpPathTypes
= static_cast<BYTE
*>(malloc(mPathPoints
* sizeof(BYTE
)));
1745 mpPathPoints
= static_cast<POINT
*>(malloc(mPathPoints
* sizeof(POINT
)));//better than realloc
1747 if(src
.mPathPoints
>0)
1749 memcpy(mpPathTypes
, src
.mpPathTypes
, mPathPoints
*sizeof(BYTE
));
1750 memcpy(mpPathPoints
, src
.mpPathPoints
, mPathPoints
*sizeof(POINT
));
1756 PathData::~PathData()
1761 void PathData::_TrashPath()
1763 delete [] mpPathTypes
;
1764 delete [] mpPathPoints
;
1766 mpPathPoints
= NULL
;
1770 bool PathData::BeginPath(HDC hdc
)
1773 return !!::BeginPath(hdc
);
1776 bool PathData::EndPath(HDC hdc
)
1781 mPathPoints
= GetPath(hdc
, NULL
, NULL
, 0);
1784 mpPathTypes
= (BYTE
*)malloc(sizeof(BYTE
) * mPathPoints
);
1785 mpPathPoints
= (POINT
*)malloc(sizeof(POINT
) * mPathPoints
);
1786 if(mPathPoints
== GetPath(hdc
, mpPathPoints
, mpPathTypes
, mPathPoints
))
1793 bool PathData::PartialBeginPath(HDC hdc
, bool bClearPath
)
1797 return !!::BeginPath(hdc
);
1800 bool PathData::PartialEndPath(HDC hdc
, long dx
, long dy
)
1808 nPoints
= GetPath(hdc
, NULL
, NULL
, 0);
1811 pNewTypes
= (BYTE
*)realloc(mpPathTypes
, (mPathPoints
+ nPoints
) * sizeof(BYTE
));
1812 pNewPoints
= (POINT
*)realloc(mpPathPoints
, (mPathPoints
+ nPoints
) * sizeof(POINT
));
1814 mpPathTypes
= pNewTypes
;
1816 mpPathPoints
= pNewPoints
;
1817 BYTE
* pTypes
= new BYTE
[nPoints
];
1818 POINT
* pPoints
= new POINT
[nPoints
];
1819 if(pNewTypes
&& pNewPoints
&& nPoints
== GetPath(hdc
, pPoints
, pTypes
, nPoints
))
1821 for(int i
= 0; i
< nPoints
; ++i
)
1823 mpPathPoints
[mPathPoints
+ i
].x
= pPoints
[i
].x
+ dx
;
1824 mpPathPoints
[mPathPoints
+ i
].y
= pPoints
[i
].y
+ dy
;
1825 mpPathTypes
[mPathPoints
+ i
] = pTypes
[i
];
1827 mPathPoints
+= nPoints
;
1841 //////////////////////////////////////////////////////////////////////////
1845 ScanLineData::ScanLineData():mPathOffsetX(0),mPathOffsetY(0)
1849 ScanLineData::~ScanLineData()
1853 void ScanLineData::_ReallocEdgeBuffer(int edges
)
1855 mEdgeHeapSize
= edges
;
1856 mpEdgeBuffer
= (Edge
*)realloc(mpEdgeBuffer
, sizeof(Edge
)*edges
);
1859 void ScanLineData::_EvaluateBezier(const PathData
& path_data
, int ptbase
, bool fBSpline
)
1861 const POINT
* pt0
= path_data
.mpPathPoints
+ ptbase
;
1862 const POINT
* pt1
= path_data
.mpPathPoints
+ ptbase
+ 1;
1863 const POINT
* pt2
= path_data
.mpPathPoints
+ ptbase
+ 2;
1864 const POINT
* pt3
= path_data
.mpPathPoints
+ ptbase
+ 3;
1873 double cx3
, cx2
, cx1
, cx0
, cy3
, cy2
, cy1
, cy0
;
1880 double _1div6
= 1.0/6.0;
1881 cx3
= _1div6
*(- x0
+3*x1
-3*x2
+x3
);
1882 cx2
= _1div6
*( 3*x0
-6*x1
+3*x2
);
1883 cx1
= _1div6
*(-3*x0
+3*x2
);
1884 cx0
= _1div6
*( x0
+4*x1
+1*x2
);
1885 cy3
= _1div6
*(- y0
+3*y1
-3*y2
+y3
);
1886 cy2
= _1div6
*( 3*y0
-6*y1
+3*y2
);
1887 cy1
= _1div6
*(-3*y0
+3*y2
);
1888 cy0
= _1div6
*( y0
+4*y1
+1*y2
);
1896 cx3
= - x0
+3*x1
-3*x2
+x3
;
1897 cx2
= 3*x0
-6*x1
+3*x2
;
1900 cy3
= - y0
+3*y1
-3*y2
+y3
;
1901 cy2
= 3*y0
-6*y1
+3*y2
;
1906 // This equation is from Graphics Gems I.
1908 // The idea is that since we're approximating a cubic curve with lines,
1909 // any error we incur is due to the curvature of the line, which we can
1910 // estimate by calculating the maximum acceleration of the curve. For
1911 // a cubic, the acceleration (second derivative) is a line, meaning that
1912 // the absolute maximum acceleration must occur at either the beginning
1913 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
1914 // conservative than that, but that's okay.
1916 // If the acceleration of the parametric formula is zero (c2 = c3 = 0),
1917 // that component of the curve is linear and does not incur any error.
1918 // If a=0 for both X and Y, the curve is a line segment and we can
1919 // use a step size of 1.
1920 double maxaccel1
= fabs(2*cy2
) + fabs(6*cy3
);
1921 double maxaccel2
= fabs(2*cx2
) + fabs(6*cx3
);
1922 double maxaccel
= maxaccel1
> maxaccel2
? maxaccel1
: maxaccel2
;
1924 if(maxaccel
> 8.0) h
= sqrt(8.0 / maxaccel
);
1925 if(!fFirstSet
) {firstp
.x
= (LONG
)cx0
; firstp
.y
= (LONG
)cy0
; lastp
= firstp
; fFirstSet
= true;}
1926 for(double t
= 0; t
< 1.0; t
+= h
)
1928 double x
= cx0
+ t
*(cx1
+ t
*(cx2
+ t
*cx3
));
1929 double y
= cy0
+ t
*(cy1
+ t
*(cy2
+ t
*cy3
));
1930 _EvaluateLine(lastp
.x
, lastp
.y
, (int)x
, (int)y
);
1932 double x
= cx0
+ cx1
+ cx2
+ cx3
;
1933 double y
= cy0
+ cy1
+ cy2
+ cy3
;
1934 _EvaluateLine(lastp
.x
, lastp
.y
, (int)x
, (int)y
);
1937 void ScanLineData::_EvaluateLine(const PathData
& path_data
, int pt1idx
, int pt2idx
)
1939 const POINT
* pt1
= path_data
.mpPathPoints
+ pt1idx
;
1940 const POINT
* pt2
= path_data
.mpPathPoints
+ pt2idx
;
1941 _EvaluateLine(pt1
->x
, pt1
->y
, pt2
->x
, pt2
->y
);
1944 void ScanLineData::_EvaluateLine(int x0
, int y0
, int x1
, int y1
)
1946 if(lastp
.x
!= x0
|| lastp
.y
!= y0
)
1948 _EvaluateLine(lastp
.x
, lastp
.y
, x0
, y0
);
1950 if(!fFirstSet
) {firstp
.x
= x0
; firstp
.y
= y0
; fFirstSet
= true;}
1955 __int64 xacc
= (__int64
)x0
<< 13;
1958 int y
= ((y0
+ 3)&~7) + 4;
1963 __int64 invslope
= (__int64(x1
- x0
) << 16) / dy
;
1964 while(mEdgeNext
+ y1
+ 1 - iy
> mEdgeHeapSize
)
1965 _ReallocEdgeBuffer(mEdgeHeapSize
*2);
1966 xacc
+= (invslope
* (y
- y0
)) >> 3;
1969 int ix
= (int)((xacc
+ 32768) >> 16);
1970 mpEdgeBuffer
[mEdgeNext
].next
= mpScanBuffer
[iy
];
1971 mpEdgeBuffer
[mEdgeNext
].posandflag
= ix
*2 + 1;
1972 mpScanBuffer
[iy
] = mEdgeNext
++;
1978 else if(y1
< y0
) // up
1980 __int64 xacc
= (__int64
)x1
<< 13;
1983 int y
= ((y1
+ 3)&~7) + 4;
1988 __int64 invslope
= (__int64(x0
- x1
) << 16) / dy
;
1989 while(mEdgeNext
+ y0
+ 1 - iy
> mEdgeHeapSize
)
1990 _ReallocEdgeBuffer(mEdgeHeapSize
*2);
1991 xacc
+= (invslope
* (y
- y1
)) >> 3;
1994 int ix
= (int)((xacc
+ 32768) >> 16);
1995 mpEdgeBuffer
[mEdgeNext
].next
= mpScanBuffer
[iy
];
1996 mpEdgeBuffer
[mEdgeNext
].posandflag
= ix
*2;
1997 mpScanBuffer
[iy
] = mEdgeNext
++;
2005 bool ScanLineData::ScanConvert(SharedPtrPathData path_data
)
2007 int lastmoveto
= -1;
2009 // Drop any outlines we may have.
2011 mWideOutline
.clear();
2013 // Determine bounding box
2014 if(!path_data
->mPathPoints
)
2016 mPathOffsetX
= mPathOffsetY
= 0;
2017 mWidth
= mHeight
= 0;
2024 for(i
=0; i
<path_data
->mPathPoints
; ++i
)
2026 int ix
= path_data
->mpPathPoints
[i
].x
;
2027 int iy
= path_data
->mpPathPoints
[i
].y
;
2028 if(ix
< minx
) minx
= ix
;
2029 if(ix
> maxx
) maxx
= ix
;
2030 if(iy
< miny
) miny
= iy
;
2031 if(iy
> maxy
) maxy
= iy
;
2033 minx
= (minx
>> 3) & ~7;
2034 miny
= (miny
>> 3) & ~7;
2035 maxx
= (maxx
+ 7) >> 3;
2036 maxy
= (maxy
+ 7) >> 3;
2037 for(i
=0; i
<path_data
->mPathPoints
; ++i
)
2039 path_data
->mpPathPoints
[i
].x
-= minx
*8;
2040 path_data
->mpPathPoints
[i
].y
-= miny
*8;
2042 if(minx
> maxx
|| miny
> maxy
)
2044 mWidth
= mHeight
= 0;
2045 mPathOffsetX
= mPathOffsetY
= 0;
2046 path_data
->_TrashPath();
2049 mWidth
= maxx
+ 1 - minx
;
2050 mHeight
= maxy
+ 1 - miny
;
2051 mPathOffsetX
= minx
;
2052 mPathOffsetY
= miny
;
2053 // Initialize edge buffer. We use edge 0 as a sentinel.
2055 mEdgeHeapSize
= 2048;
2056 mpEdgeBuffer
= (Edge
*)malloc(sizeof(Edge
)*mEdgeHeapSize
);
2057 // Initialize scanline list.
2058 mpScanBuffer
= new unsigned int[mHeight
];
2059 memset(mpScanBuffer
, 0, mHeight
*sizeof(unsigned int));
2060 // Scan convert the outline. Yuck, Bezier curves....
2061 // Unfortunately, Windows 95/98 GDI has a bad habit of giving us text
2062 // paths with all but the first figure left open, so we can't rely
2063 // on the PT_CLOSEFIGURE flag being used appropriately.
2065 firstp
.x
= firstp
.y
= 0;
2066 lastp
.x
= lastp
.y
= 0;
2067 for(i
=0; i
<path_data
->mPathPoints
; ++i
)
2069 BYTE t
= path_data
->mpPathTypes
[i
] & ~PT_CLOSEFIGURE
;
2073 if(lastmoveto
>= 0 && firstp
!= lastp
)
2074 _EvaluateLine(lastp
.x
, lastp
.y
, firstp
.x
, firstp
.y
);
2077 lastp
= path_data
->mpPathPoints
[i
];
2082 if(path_data
->mPathPoints
- (i
-1) >= 2) _EvaluateLine(*path_data
, i
-1, i
);
2085 if(path_data
->mPathPoints
- (i
-1) >= 4) _EvaluateBezier(*path_data
, i
-1, false);
2089 if(path_data
->mPathPoints
- (i
-1) >= 4) _EvaluateBezier(*path_data
, i
-1, true);
2092 case PT_BSPLINEPATCHTO
:
2093 if(path_data
->mPathPoints
- (i
-3) >= 4) _EvaluateBezier(*path_data
, i
-3, true);
2097 if(lastmoveto
>= 0 && firstp
!= lastp
)
2098 _EvaluateLine(lastp
.x
, lastp
.y
, firstp
.x
, firstp
.y
);
2099 // Free the path since we don't need it anymore.
2100 path_data
->_TrashPath();
2101 // Convert the edges to spans. We couldn't do this before because some of
2102 // the regions may have winding numbers >+1 and it would have been a pain
2103 // to try to adjust the spans on the fly. We use one heap to detangle
2104 // a scanline's worth of edges from the singly-linked lists, and another
2105 // to collect the actual scans.
2106 std::vector
<int> heap
;
2107 mOutline
.reserve(mEdgeNext
/ 2);
2109 for(y
=0; y
<mHeight
; ++y
)
2112 // Detangle scanline into edge heap.
2113 for(unsigned ptr
= (unsigned)(mpScanBuffer
[y
]&0xffffffff); ptr
; ptr
= mpEdgeBuffer
[ptr
].next
)
2115 heap
.push_back(mpEdgeBuffer
[ptr
].posandflag
);
2117 // Sort edge heap. Note that we conveniently made the opening edges
2118 // one more than closing edges at the same spot, so we won't have any
2119 // problems with abutting spans.
2120 std::sort(heap
.begin(), heap
.end()/*begin() + heap.size()*/);
2121 // Process edges and add spans. Since we only check for a non-zero
2122 // winding number, it doesn't matter which way the outlines go!
2123 std::vector
<int>::iterator itX1
= heap
.begin();
2124 std::vector
<int>::iterator itX2
= heap
.end(); // begin() + heap.size();
2126 for(; itX1
!= itX2
; ++itX1
)
2139 mOutline
.push_back(std::pair
<__int64
,__int64
>((y
<<32)+x1
+0x4000000040000000i
64, (y
<<32)+x2
+0x4000000040000000i
64)); // G: damn Avery, this is evil! :)
2144 // Dump the edge and scan buffers, since we no longer need them.
2146 delete [] mpScanBuffer
;
2151 using namespace std
;
2153 void ScanLineData::_OverlapRegion(tSpanBuffer
& dst
, tSpanBuffer
& src
, int dx
, int dy
)
2156 temp
.reserve(dst
.size() + src
.size());
2158 tSpanBuffer::iterator itA
= temp
.begin();
2159 tSpanBuffer::iterator itAE
= temp
.end();
2160 tSpanBuffer::iterator itB
= src
.begin();
2161 tSpanBuffer::iterator itBE
= src
.end();
2162 // Don't worry -- even if dy<0 this will still work! // G: hehe, the evil twin :)
2163 unsigned __int64 offset1
= (((__int64
)dy
)<<32) - dx
;
2164 unsigned __int64 offset2
= (((__int64
)dy
)<<32) + dx
;
2165 while(itA
!= itAE
&& itB
!= itBE
)
2167 if((*itB
).first
+ offset1
< (*itA
).first
)
2169 // B span is earlier. Use it.
2170 unsigned __int64 x1
= (*itB
).first
+ offset1
;
2171 unsigned __int64 x2
= (*itB
).second
+ offset2
;
2173 // B spans don't overlap, so begin merge loop with A first.
2176 // If we run out of A spans or the A span doesn't overlap,
2177 // then the next B span can't either (because B spans don't
2178 // overlap) and we exit.
2179 if(itA
== itAE
|| (*itA
).first
> x2
)
2181 do {x2
= _MAX(x2
, (*itA
++).second
);}
2182 while(itA
!= itAE
&& (*itA
).first
<= x2
);
2183 // If we run out of B spans or the B span doesn't overlap,
2184 // then the next A span can't either (because A spans don't
2185 // overlap) and we exit.
2186 if(itB
== itBE
|| (*itB
).first
+ offset1
> x2
)
2188 do {x2
= _MAX(x2
, (*itB
++).second
+ offset2
);}
2189 while(itB
!= itBE
&& (*itB
).first
+ offset1
<= x2
);
2192 dst
.push_back(tSpan(x1
, x2
));
2196 // A span is earlier. Use it.
2197 unsigned __int64 x1
= (*itA
).first
;
2198 unsigned __int64 x2
= (*itA
).second
;
2200 // A spans don't overlap, so begin merge loop with B first.
2203 // If we run out of B spans or the B span doesn't overlap,
2204 // then the next A span can't either (because A spans don't
2205 // overlap) and we exit.
2206 if(itB
== itBE
|| (*itB
).first
+ offset1
> x2
)
2208 do {x2
= _MAX(x2
, (*itB
++).second
+ offset2
);}
2209 while(itB
!= itBE
&& (*itB
).first
+ offset1
<= x2
);
2210 // If we run out of A spans or the A span doesn't overlap,
2211 // then the next B span can't either (because B spans don't
2212 // overlap) and we exit.
2213 if(itA
== itAE
|| (*itA
).first
> x2
)
2215 do {x2
= _MAX(x2
, (*itA
++).second
);}
2216 while(itA
!= itAE
&& (*itA
).first
<= x2
);
2219 dst
.push_back(tSpan(x1
, x2
));
2222 // Copy over leftover spans.
2224 dst
.push_back(*itA
++);
2227 dst
.push_back(tSpan((*itB
).first
+ offset1
, (*itB
).second
+ offset2
));
2232 bool ScanLineData::CreateWidenedRegion(int rx
, int ry
)
2236 mWideBorder
= max(rx
,ry
);
2239 // Do a half circle.
2240 // _OverlapRegion mirrors this so both halves are done.
2241 for(int y
= -ry
; y
<= ry
; ++y
)
2243 int x
= (int)(0.5 + sqrt(float(ry
*ry
- y
*y
)) * float(rx
)/float(ry
));
2244 _OverlapRegion(mWideOutline
, mOutline
, x
, y
);
2247 else if (ry
== 0 && rx
> 0)
2249 // There are artifacts if we don't make at least two overlaps of the line, even at same Y coord
2250 _OverlapRegion(mWideOutline
, mOutline
, rx
, 0);
2251 _OverlapRegion(mWideOutline
, mOutline
, rx
, 0);
2256 void ScanLineData::DeleteOutlines()
2258 mWideOutline
.clear();
2262 void Rasterizer::FillSolidRect(SubPicDesc
& spd
, int x
, int y
, int nWidth
, int nHeight
, DWORD lColor
)
2264 bool fSSE2
= !!(g_cpuid
.m_flags
& CCpuID::sse2
);
2267 for (int wy
=y
; wy
<y
+nHeight
; wy
++) {
2268 DWORD
* dst
= (DWORD
*)((BYTE
*)spd
.bits
+ spd
.pitch
* wy
) + x
;
2269 for(int wt
=0; wt
<nWidth
; ++wt
) {
2270 pixmix_sse2(&dst
[wt
], lColor
, lColor
>>24);
2274 for (int wy
=y
; wy
<y
+nHeight
; wy
++) {
2275 DWORD
* dst
= (DWORD
*)((BYTE
*)spd
.bits
+ spd
.pitch
* wy
) + x
;
2276 for(int wt
=0; wt
<nWidth
; ++wt
) {
2277 pixmix(&dst
[wt
], lColor
, 0x40);