Scale. [PART 3]
[xy_vsfilter.git] / src / subtitles / Rasterizer.h
blob9b35336d8ab3cc1beb07d0aab2c0ff4b1b9f3271
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 #pragma once
24 #include <boost/smart_ptr.hpp>
25 #include <vector>
26 #include "../SubPic/ISubPic.h"
27 #include "xy_malloc.h"
29 #ifdef _OPENMP
30 #include <omp.h>
31 #endif
33 #define PT_MOVETONC 0xfe
34 #define PT_BSPLINETO 0xfc
35 #define PT_BSPLINEPATCHTO 0xfa
37 typedef struct {
38 int left, top;
39 int w, h; // width, height
40 unsigned char *buffer; // w x h buffer
41 } Bitmap;
43 struct PathData
45 public:
46 PathData();
47 ~PathData();
49 PathData(const PathData&);//important! disable default copy constructor
50 const PathData& operator=(const PathData&);
51 bool operator==(const PathData& rhs)const;
53 void _TrashPath();
54 bool BeginPath(HDC hdc);
55 bool EndPath(HDC hdc);
56 bool PartialBeginPath(HDC hdc, bool bClearPath);
57 bool PartialEndPath(HDC hdc, long dx, long dy);
59 void AlignLeftTop(CPoint *left_top, CSize *size);
61 BYTE* mpPathTypes;
62 POINT* mpPathPoints;
63 int mPathPoints;
66 typedef ::boost::shared_ptr<const PathData> SharedPtrConstPathData;
67 typedef ::boost::shared_ptr<PathData> SharedPtrPathData;
70 typedef std::pair<unsigned __int64, unsigned __int64> tSpan;
71 typedef std::vector<tSpan> tSpanBuffer;
73 class ScanLineData
75 bool fFirstSet;
76 CPoint firstp, lastp;
78 private:
79 int mWidth, mHeight;
81 tSpanBuffer mOutline;
83 struct Edge {
84 int next;
85 int posandflag;
86 } *mpEdgeBuffer;
87 unsigned mEdgeHeapSize;
88 unsigned mEdgeNext;
90 unsigned int* mpScanBuffer;
92 typedef unsigned char byte;
94 private:
95 void _ReallocEdgeBuffer(int edges);
96 void _EvaluateBezier(const PathData& path_data, int ptbase, bool fBSpline);
97 void _EvaluateLine(const PathData& path_data, int pt1idx, int pt2idx);
98 void _EvaluateLine(int x0, int y0, int x1, int y1);
100 public:
101 ScanLineData();
102 virtual ~ScanLineData();
104 bool ScanConvert(const PathData& path_data, const CSize& size);
105 void DeleteOutlines();
107 friend class Rasterizer;
108 friend class ScanLineData2;
111 typedef ::boost::shared_ptr<const ScanLineData> SharedPtrConstScanLineData;
112 typedef ::boost::shared_ptr<ScanLineData> SharedPtrScanLineData;
114 class ScanLineData2
116 public:
117 ScanLineData2():mPathOffsetX(0),mPathOffsetY(0),mWideBorder(0){}
118 ScanLineData2(const CPoint& left_top, const SharedPtrConstScanLineData& scan_line_data)
119 :m_scan_line_data(scan_line_data)
120 ,mPathOffsetX(left_top.x)
121 ,mPathOffsetY(left_top.y)
124 void SetOffset(const CPoint& offset)
126 mPathOffsetX = offset.x;
127 mPathOffsetY = offset.y;
129 bool CreateWidenedRegion(int borderX, int borderY);
130 private:
131 SharedPtrConstScanLineData m_scan_line_data;
132 int mPathOffsetX, mPathOffsetY;
133 tSpanBuffer mWideOutline;
134 int mWideBorder;
136 friend class Rasterizer;
139 typedef ::boost::shared_ptr<const ScanLineData2> SharedPtrConstScanLineData2;
140 typedef ::boost::shared_ptr<ScanLineData2> SharedPtrScanLineData2;
142 typedef ::boost::shared_ptr<BYTE> SharedPtrByte;
144 struct Overlay
146 public:
147 Overlay()
149 mOffsetX=mOffsetY=mWidth=mHeight=0;
150 mOverlayWidth=mOverlayHeight=mOverlayPitch=0;
151 mfWideOutlineEmpty = false;
153 ~Overlay()
155 CleanUp();
158 void CleanUp()
160 mBody.reset((BYTE*)NULL);
161 mBorder.reset((BYTE*)NULL);
162 mOffsetX=mOffsetY=mWidth=mHeight=0;
163 mOverlayWidth=mOverlayHeight=mOverlayPitch=0;
164 mfWideOutlineEmpty = false;
167 void FillAlphaMash(byte* outputAlphaMask, bool fBody, bool fBorder,
168 int x, int y, int w, int h,
169 const byte* pAlphaMask, int pitch, DWORD color_alpha);
171 Overlay* GetSubpixelVariance(unsigned int xshift, unsigned int yshift);
172 public:
173 SharedPtrByte mBody;
174 SharedPtrByte mBorder;
175 int mOffsetX, mOffsetY;
176 int mWidth, mHeight;
178 int mOverlayWidth, mOverlayHeight, mOverlayPitch;
180 bool mfWideOutlineEmpty;//specially for blur
181 private:
182 void _DoFillAlphaMash(byte* outputAlphaMask, const byte* pBody, const byte* pBorder,
183 int x, int y, int w, int h,
184 const byte* pAlphaMask, int pitch, DWORD color_alpha);
185 void _DoFillAlphaMash_c(byte* outputAlphaMask, const byte* pBody, const byte* pBorder, int x, int y, int w, int h, const byte* pAlphaMask, int pitch, DWORD color_alpha );
188 typedef ::boost::shared_ptr<Overlay> SharedPtrOverlay;
190 struct GrayImage2
192 public:
193 CPoint left_top;
194 CSize size;
195 int pitch;
196 SharedPtrByte data;
199 typedef ::boost::shared_ptr<GrayImage2> SharedPtrGrayImage2;
201 class XyBitmap;
202 class Rasterizer
204 private:
205 typedef unsigned char byte;
207 struct DM
209 enum
211 SSE2 = 1,
212 ALPHA_MASK = 1<<1,
213 SINGLE_COLOR = 1<<2,
214 BODY = 1<<3,
215 AYUV_PLANAR = 1<<4
218 public:
219 static const float GAUSSIAN_BLUR_THREHOLD;
220 public:
222 static bool Rasterize(const ScanLineData2& scan_line_data2, int xsub, int ysub, SharedPtrOverlay overlay);
224 static bool IsItReallyBlur(int be_strength, double gaussian_blur_strength);
225 static bool OldFixedPointBlur(const Overlay& input_overlay, int be_strength, double gaussian_blur_strength,
226 SharedPtrOverlay output_overlay);
227 static bool Blur(const Overlay& input_overlay, int be_strength, double gaussian_blur_strength, SharedPtrOverlay output_overlay);
228 static bool BeBlur(const Overlay& input_overlay, int be_strength, SharedPtrOverlay output_overlay);
229 static bool GaussianBlur(const Overlay& input_overlay, double gaussian_blur_strength, SharedPtrOverlay output_overlay);
231 static SharedPtrByte CompositeAlphaMask(const SharedPtrOverlay& overlay, const CRect& clipRect,
232 const GrayImage2* alpha_mask,
233 int xsub, int ysub, const DWORD* switchpts, bool fBody, bool fBorder,
234 CRect *outputDirtyRect);
236 static void Draw(XyBitmap* bitmap,
237 SharedPtrOverlay overlay,
238 const CRect& clipRect,
239 byte* s_base,
240 int xsub, int ysub,
241 const DWORD* switchpts, bool fBody, bool fBorder);
243 static void FillSolidRect(SubPicDesc& spd, int x, int y, int nWidth, int nHeight, DWORD lColor);