Minor refactor.
[xy_vsfilter.git] / src / subtitles / Rasterizer.h
blobb522bd27334e02f2eb31ee9c13a315315a71553d
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);
187 typedef ::boost::shared_ptr<Overlay> SharedPtrOverlay;
189 struct GrayImage2
191 public:
192 CPoint left_top;
193 CSize size;
194 int pitch;
195 SharedPtrByte data;
198 typedef ::boost::shared_ptr<GrayImage2> SharedPtrGrayImage2;
200 class XyBitmap;
201 class Rasterizer
203 private:
204 typedef unsigned char byte;
206 struct DM
208 enum
210 SSE2 = 1,
211 ALPHA_MASK = 1<<1,
212 SINGLE_COLOR = 1<<2,
213 BODY = 1<<3,
214 AYUV_PLANAR = 1<<4
217 public:
218 static bool Rasterize(const ScanLineData2& scan_line_data2, int xsub, int ysub, SharedPtrOverlay overlay);
219 static bool Blur(const Overlay& input_overlay, int fBlur, double fGaussianBlur, SharedPtrOverlay output_overlay);
221 static SharedPtrByte CompositeAlphaMask(const SharedPtrOverlay& overlay, const CRect& clipRect,
222 const GrayImage2* alpha_mask,
223 int xsub, int ysub, const DWORD* switchpts, bool fBody, bool fBorder,
224 CRect *outputDirtyRect);
226 static void Draw(XyBitmap* bitmap,
227 SharedPtrOverlay overlay,
228 const CRect& clipRect,
229 byte* s_base,
230 int xsub, int ysub,
231 const DWORD* switchpts, bool fBody, bool fBorder);
233 static void FillSolidRect(SubPicDesc& spd, int x, int y, int nWidth, int nHeight, DWORD lColor);