Alexander Burt - Inside of 2D-graphics
45. 弌仂仂仆仂亠仆亳亠 仂仂仆
仂仆亳仂 1.0
PAL 1.09
PAL Widescreen 1.46
NTSC 0.91
NTSC Widescreen 1.21
Anamorphic 2.0
Cinemascope 2.39
89. byte b = 0xFF; // 8 弍亳仂于
ushort w = 0xFFFF; // 16 弍亳仂于
90. 0 1 2 3 4 5 6 7 8 9 A B C D E F ?
00 10
10 20
20 30
30 40
40 50
50 6060 70 70 80 80 90 90 A0 A0 B0 B0C0 C0D0 E0
D0 F0
E0 F0
FF FF
91. 0 1 2 3 4 5 6 7 8 9 A B C D E F
00 10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0 FF
00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
000 111 222 333 444 555 #fff
666 777 #ffffff
888 999 AAA BBB CCC DDD EEE FFF
0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 AAAA BBBB CCCC DDDD EEEE FFFF
93. byte b = 0x98;
ushort w = b << 8; // 0x9800
ushort w = (b << 8) | b; // 0x9898
95. ushort w = 0x98FF;
byte b = w >> 8; // 0x98
byte b = (w + 0x80) >> 8; // 0x997F >> 8 = 0x99
96. 96 97 98 99 9A
9666 96EE 9777 97FF 9888 9911 9999 9A22 9AAA
98FF
97. ushort w = 0x98FF;
byte b = w >> 8; // 0x98
byte b = (w + 0x80) >> 8; // 0x997F >> 8 = 0x99
98. ushort w = 0x9900;
byte b = w >> 8; // 0x99
byte b = (w + 0x80) >> 8; // 0x9980 >> 8 = 0x99
99. 96 97 98 99 9A
9666 96EE 9777 97FF 9888 9911 9999 9A22 9AAA
9900
100. ushort w = 0x9900;
byte b = w >> 8; // 0x99
byte b = (w + 0x80) >> 8; // 0x9980 >> 8 = 0x99
101. 96 97 98 99 9A
111
88
9666 96EE 9777 97FF 9888 9911 9999 9A22 9AAA
102. ushort w = 0x9900;
byte b =
((w & 0xF000) >> 8)
| ((w & 0x0FFF) + 0x88) / 0x111; // 0x98
103. ushort w = 0x9900;
byte b = w >> 8;
byte b = (w + 0x80) >> 8;
00 01 FF
0000..00FF
... FF00..FFFF
104. Image
class Image
{
public:
Image(uint width, uint height)
: m_width(width)
, m_height(height)
{
m_buffer = (pix8*)calloc(m_width * m_height, sizeof(pix8));
}
~Image() { free(m_buffer); }
uint GetWidth() { return m_width; }
uint GetHeight() { return m_height; }
pix8* GetBuffer() const { return m_buffer; }
private:
uint m_width;
uint m_height;
pix8* m_buffer;
};
105. Graphics
class Graphics
{
public:
Graphics(Image* image) : m_image(image) {}
void FillRect(int x, int y, int width, int height, const Brush& brush);
void DrawImage(int x, int y, Image* image, float opacity);
/*
*
*
*
* ...etc...
*
*
*
*/
private:
Image* m_image;
};
137. float a = ; // [0..1]
float b = ; // [0..1]
float c = a * b;
138. int a = ; // [0..255]
int b = ; // [0..255]
int c = a * b;
139. float fa = (float)a / 255.0;
float fb = (float)b / 255.0;
int c = (int)((fa * ft) * 255.0);
143. int MultInt(int a, int b)
{
int temp = a * b + 0x80;
return (temp + (temp >> 8)) >> 8;
}
145. int a = ; // [0..255]
int b = ; // [0..255]
int t = ; // [0..255]
int c = a + IntMult(b a, t);
146. int p1 = ; // 0xAARRGGBB
int p2 = ; // 0xAARRGGBB
int t = ; // [0..255]
int r1 = (p1 >> 16) & 0xFF;
int g1 = (p1 >> 8) & 0xFF;
int b1 = (p1 ) & 0xFF;
int a1 = (p1 >> 24) & 0xFF;
int r2 = (p2 >> 16) & 0xFF;
int g2 = (p2 >> 8) & 0xFF;
int b2 = (p2 ) & 0xFF;
int a2 = (p2 >> 24) & 0xFF;
int r3 = r1 + MultInt(r2 r1, t);
int g3 = g1 + MultInt(g2 g1, t);
int b3 = b1 + MultInt(b2 b1, t);
int a3 = a1 + MultInt(a2 a1, t);
int p3 =
(r3 << 16)
| (g3 << 8)
| (b3 )
| (a3 << 24);
147. union pix8
{
int RGBA;
struct { byte R, G, B, A };
};
//
// ...
//
pix8& p1 = (pix8&) ; // 0xAARRGGBB
pix8& p2 = (pix8&) ; // 0xAARRGGBB
int t = ; // [0..255]
pix8 p3;
p3.R = p1.R + MultInt(p2.R p1.R, t);
p3.G = p1.G + MultInt(p2.G p1.G, t);
p3.B = p1.B + MultInt(p2.B p1.B, t);
p3.A = p1.A + MultInt(p2.A p1.A, t);
157. 舒仄
1
0.9
仗亠从舒仍仆亶
仍仂从
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
159. 舒仄
1
0.9
0.8 仍仂从
0.7 仆仂亞仂 亠仍舒
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
168. 亠亠仄仆仂亢亠仆亳亠
丶于亠舒
仆亠仗亠亠仄仆仂亢亠仆仆亶
(unpremultiplied)
仗亠亠仄仆仂亢亠仆仆亶
(premultiplied)
205. class ColorBrush : Brush
{
public:
ColorBrush(const Color& color) : m_color(color) {}
// virtual
void FillScanline(Image& img, int y, int[] edges)
{
assert(edges.Count % 2 == 0);
for (int i = 0; i < edges.Count; i += 2)
{
for (int x = edges[i]; x < edges[i + 1]; x++)
{
pix8& pix = img.Buffer[y * img.Width + x];
pix = Over(pix8(m_color), pix);
}
}
{
private:
Color m_color;
};
206. Brush
Color Brush
Gradient Brush
Linear Gradient Brush
Radial Gradient Brush
Angular Gradient Brush
Pattern Brush
Noise Brush
...
230. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
p1 += FPoint(0.5, 0.5);
p2 += FPoint(0.5, 0.5);
// ...
}
DrawLine(FPoint(3, 2), FPoint(12, 2), 1);
231. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
p1 += FPoint(0.5, 0.5);
p2 += FPoint(0.5, 0.5);
// ...
}
DrawLine(FPoint(3, 2), FPoint(12, 2), 2);
232. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
p1 += FPoint(0.5, 0.5);
p2 += FPoint(0.5, 0.5);
// ...
}
DrawLine(FPoint(3.5, 1.5), FPoint(11.5, 1.5), 2);
233. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
// ...
}
DrawLine(FPoint(3, 2), FPoint(13, 2), 2);
234. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
// ...
}
DrawLine(FPoint(3, 2), FPoint(13, 2), 1);
235. 仗 从仂仂亟亳仆舒
void DrawLine(FPoint p1, FPoint p2, float width)
{
// ...
}
DrawLine(FPoint(3, 2.5), FPoint(13, 2.5), 1);
255. 丐 仂 仆 从 亳 亠 仍 亳 仆 亳 亳
261. float _width = max(1, width);
float _opacity =
width < 1
? opacity * width
: opacity;
DrawLine(..., _width, _opacity);
276. // float t
float _1mt = 1 t;
p01.X = _1mt * p0.X + t * p1.X;
p01.Y = _1mt * p0.Y + t * p1.Y;
282. // float t
float tm1 = t 1;
float t2 = t * t;
float tm12 = tm1 * tm1;
float _2ttm1 = 2 * t * tm1;
p012.X = tm12 * p0.X _2ttm1 * p1.X + t2 * p2.X;
p012.Y = tm12 * p0.Y _2ttm1 * p1.Y + t2 * p2.Y;
288. // float t
float t2 = t * 2;
float t3 = t2 * t;
float _1mt = 1 t;
float _1mt2 = _1mt * _1mt;
float _1mt3 = _1mt2 * _1mt;
float _3t_1mt2 = 3 * t * _1mt2;
float _3t2_1mt = 3 * t2 * _1mt;
p0123.X = _1mt3 * p0.X _3t_1mt2 * p1.X + _3t2_1mt * p2.X + t3 * p3.X;
p0123.Y = _1mt3 * p0.Y _3t_1mt2 * p1.Y + _3t2_1mt * p2.Y + t3 * p3.Y;
335. 仂仆舒仆仂 亰亠仆亳
100:1*
* 仂仂弍亠-仂 1000:1, 仆仂 亳仗亳仆亠 仍仂于亳 仆亠 仗仂亰于仂仍ム
339. 仂弍仍亠仄舒 从仂亟舒 100
0 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 揃 255
仍亳仆亳亠
仆亠 于舒舒亠
0.01
342. 于亠舒
亞舒仄仄舒 亞舒仄仄舒 束亞舒仄仄舒損
从舒仄亠 亟亠亞舒仄仄舒 亞舒仄仄舒 仄仂仆亳仂舒 亞仍舒亰
?
于亠 8-bit 16-bit 16-bit 8-bit 于亠 亰亠仆亳亠
RGB RGB RGB RGB
358. 1
0
R G B R G B R G B
378. GetPixel(int x, int y);
SetPixel(int x, int y, int color);
pix8* buffer = image->GetBuffer();
int imageWidth = image->GetWidth();
...buffer[y * imageWidth + x]...
379. if (...)
{
}
else
{
}
int c = min(a, b); // c = a < b ? a : b
int c = a + (((b - a) >> 31) & (b - a));
380. int b = a / 3;
int b = (int)(((__int64)a * 0x55555556) >> 32);
Editor's Notes 亊 弌舒舒 . 仂 仗仂仍亠亟仆亠亠 仄亠仂 舒弍仂 Parallels, 亞亟亠 弍仍 亞仍舒于仆仄 亟亳亰舒亶仆亠仂仄 UX. 仂仍亠亟仆亳亠 亟于舒 仗仂仍仂于亳仆仂亶 亞仂亟舒 亟亠仍舒 于仂亶 仗仂亠从, 于 舒仄从舒 从仂仂仂亞仂 仆舒仗亳舒仍 亟于仄亠仆 亞舒亳亠从 弍亳弍仍亳仂亠从. 亅舒 仗亠亰亠仆舒亳 仂 仂仄, 亳亰 亠亞仂 仂仂亳 舒从舒 弍亳弍仍亳仂亠从舒 亳 从舒从亳亠 亠 仆ミ夷術 亳 仗仂亟于仂亟仆亠 从舒仄仆亳. 亳从亠仍亳 仆舒 仂从亢舒仍亳 于亠亞亟舒. 亳 仆亠 仂仍从仂 仆舒 亰亠仄仍亠 仆亠从仂仂亠 仗亳从亠仍亳 仄仂亢仆仂 亟于亳亞舒 仗亳从亠仍亳 仄仂亢仆仂 亞仗仗亳仂于舒 仗仂 仄亠亠 仂亞仂, 从舒从 仄 于 仂 仗仂仆亳仄舒仍亳, 仆舒亠 于仂仂弍舒亢亠仆亳亠 仆舒弍亳舒仍仂 从仂仂 (仂仂舒 舒) 仄 亰舒仄亠亳仍亳, 仂 仗亳从亠仍亳 仄仂亞 弍 舒亰仆仂亶 仂仄 亳 舒亰仆 舒亰仄亠仂于 于 1860 亞亞 仄 仗仂舒于亳仍亳 仂 仗亳仆亳仗 仆舒 从仂仆于亠亶亠 亠仄仆亠亠 舒仄, 亞亟亠 仂从亳 亢亳仆亠亠亠仍亳 亠亠 于亠舒, 仂 从舒仆 仗仂于仆, 仂弍 仄亠仆亳 仗仂于仂亠仆亳 亰仂舒 亠仄仆亠亠 舒仄, 亞亟亠 仂从亳 亢亳仆亠亠亠仍亳 亠亠 于亠舒, 仂 从舒仆 仗仂于仆, 仂弍 仄亠仆亳 仗仂于仂亠仆亳 亰仂舒 亠亰仍舒 仗仂仍舒亠 于仂 舒从仂亶 仗舒仗亳 仗礆仂亞仂仍仆亳从亳 亠仂弍舒亰仂于舒仆亳亠 从仂仂亟亳仆舒 亠仂弍舒亰仂于舒仆亳亠 从仂仂亟亳仆舒 亠仂弍舒亰仂于舒仆亳亠 从仂仂亟亳仆舒 亠仗亠从亳于仆仂亶 仗仂亠从亳亳 于舒亳 亟仍 仗仂仂亞仂 束舒亶仗舒亟仆仂亞仂損 3D 从舒仆仍亳仆亳亳 仗仂亰于仂仍ム 仆舒仍舒亟亳 亠从亳于仆仂亠 仂弍亠仆亳亠 仄亠亢亟 仍仂亞亳亠从亳仄 从仂亟仂仄 (仄亠亟仍亠仆仆仄) 亳 舒仆仄 从仂亟仂仄 (弍仄) 舒亠亳亰舒亳 于 仗仂仍仂舒 舒亰舒 仄亠亟仍亠仆仆亠亠 从舒舒亠仍仆亠 亟仂仍亢仆 弍 亞仂亳亰仂仆舒仍仆仄亳 (仗亳于亠 仂 仆舒亠亞仂 亟亞舒 :) 舒从 从舒从舒, 仂仍从仂 仆舒仂弍仂仂 丕亞仂仍 亰亠仆亳; 仗仂弍仍亠仄舒 亞舒仄仄仂亶 仂舒. 丕亞仂仍 亰亠仆亳; 仗仂弍仍亠仄舒 亞舒仄仄仂亶 仂舒. 丕亞仂仍 亰亠仆亳; 仗仂弍仍亠仄舒 亞舒仄仄仂亶 仂舒.