summaryrefslogtreecommitdiff
path: root/src/corelib/render/software/ctrl/agg_gamma_spline.pas
blob: e061955cb5fb64550092975c22ebaaf222f12679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4 (Public License)
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Anti-Grain Geometry - Version 2.4 Release Milano 3 (AggPas 2.4 RM3)
// Pascal Port By: Milan Marusinec alias Milano
//                 milan@marusinec.sk
//                 http://www.aggpas.org
// Copyright (c) 2005-2006
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//
// [Pascal Port History] -----------------------------------------------------
//
// 30.01.2006-Milano: Unit port establishment
//
{ agg_gamma_spline.pas }
unit
 agg_gamma_spline ;

INTERFACE

{$I agg_mode.inc }

uses
 agg_basics ,
 agg_bspline ,
 agg_vertex_source ;

{ TYPES DEFINITION }
type
// Class-helper for calculation gamma-correction arrays. A gamma-correction
// array is an array of 256 unsigned chars that determine the actual values
// of Anti-Aliasing for each pixel coverage value from 0 to 255. If all the
// values in the array are equal to its index, i.e. 0,1,2,3,... there's
// no gamma-correction. Class agg::polyfill allows you to use custom
// gamma-correction arrays. You can calculate it using any approach, and
// class gamma_spline allows you to calculate almost any reasonable shape
// of the gamma-curve with using only 4 values - kx1, ky1, kx2, ky2.
//
//                                      kx2
//        +----------------------------------+
//        |                 |        |    .  |
//        |                 |        | .     | ky2
//        |                 |       .  ------|
//        |                 |    .           |
//        |                 | .              |
//        |----------------.|----------------|
//        |             .   |                |
//        |          .      |                |
//        |-------.         |                |
//    ky1 |    .   |        |                |
//        | .      |        |                |
//        +----------------------------------+
//            kx1
//
// Each value can be in range [0...2]. Value 1.0 means one quarter of the
// bounding rectangle. Function values() calculates the curve by these
// 4 values. After calling it one can get the gamma-array with call gamma().
// Class also supports the vertex source interface, i.e rewind() and
// vertex(). It's made for convinience and used in class gamma_ctrl.
// Before calling rewind/vertex one must set the bounding box
// box() using pixel coordinates.
 gamma_spline = object(vertex_source )
   m_gamma : array[0..255 ] of char;

   m_x  ,
   m_y  : array[0..3 ] of double;
   m_x1 ,
   m_y1 ,
   m_x2 ,
   m_y2 ,

   m_cur_x  : double;
   m_spline : bspline;

   constructor Construct;
   destructor  Destruct; virtual;

   procedure values(kx1 ,ky1 ,kx2 ,ky2 : double ); overload;
   procedure values(kx1 ,ky1 ,kx2 ,ky2 : double_ptr ); overload;

   function  gamma : char_ptr;
   function  _y (x : double ) : double;
   procedure box(x1 ,y1 ,x2 ,y2 : double );

   procedure rewind(path_id : unsigned ); virtual;
   function  vertex(x ,y : double_ptr ) : unsigned; virtual;

  end;

{ GLOBAL PROCEDURES }


IMPLEMENTATION
{ LOCAL VARIABLES & CONSTANTS }
{ UNIT IMPLEMENTATION }
{ CONSTRUCT }
constructor gamma_spline.Construct;
begin
 m_spline.Construct;

 m_x1:=0;
 m_y1:=0;
 m_x2:=10;
 m_y2:=10;

 m_cur_x:=0.0;

 values(1.0 ,1.0 ,1.0 ,1.0 );

end;

{ DESTRUCT }
destructor gamma_spline.Destruct;
begin
 m_spline.Destruct;

end;

{ VALUES }
procedure gamma_spline.values(kx1 ,ky1 ,kx2 ,ky2 : double );
var
 i : int;

begin
 if kx1 < 0.001 then
  kx1:=0.001;

 if kx1 > 1.999 then
  kx1:=1.999;

 if ky1 < 0.001 then
  ky1:=0.001;

 if ky1 > 1.999 then
  ky1:=1.999;

 if kx2 < 0.001 then
  kx2:=0.001;

 if kx2 > 1.999 then
  kx2:=1.999;

 if ky2 < 0.001 then
  ky2:=0.001;

 if ky2 > 1.999 then
  ky2:=1.999;

 m_x[0 ]:=0.0;
 m_y[0 ]:=0.0;
 m_x[1 ]:=kx1 * 0.25;
 m_y[1 ]:=ky1 * 0.25;
 m_x[2 ]:=1.0 - kx2 * 0.25;
 m_y[2 ]:=1.0 - ky2 * 0.25;
 m_x[3 ]:=1.0;
 m_y[3 ]:=1.0;

 m_spline.init(4 ,@m_x ,@m_y );

 for i:=0 to 255 do
  m_gamma[i ]:=char(trunc(_y(i / 255.0 ) * 255.0 ) );

end;

{ VALUES }
procedure gamma_spline.values(kx1 ,ky1 ,kx2 ,ky2 : double_ptr );
begin
 kx1^:=m_x[1 ] * 4.0;
 ky1^:=m_y[1 ] * 4.0;
 kx2^:=(1.0 - m_x[2 ] ) * 4.0;
 ky2^:=(1.0 - m_y[2 ] ) * 4.0;
 
end;

{ GAMMA }
function gamma_spline.gamma;
begin
 result:=@m_gamma[0 ];

end;

{ _Y }
function gamma_spline._y;
var
 val : double;

begin
 if x < 0.0 then
  x:=0.0;

 if x > 1.0 then
  x:=1.0;

 val:=m_spline.get(x );

 if val < 0.0 then
  val:=0.0;

 if val > 1.0 then
  val:=1.0;

 result:=val;

end;

{ BOX }
procedure gamma_spline.box;
begin
 m_x1:=x1;
 m_y1:=y1;
 m_x2:=x2;
 m_y2:=y2;

end;

{ REWIND }
procedure gamma_spline.rewind;
begin
 m_cur_x:=0.0;

end;

{ VERTEX }
function gamma_spline.vertex;
begin
 if m_cur_x = 0.0 then
  begin
   x^:=m_x1;
   y^:=m_y1;

   m_cur_x:=m_cur_x + (1.0 / (m_x2 - m_x1 ) );
   result :=path_cmd_move_to;

   exit;

  end;

 if m_cur_x > 1.0 then
  begin
   result:=path_cmd_stop;

   exit;

  end;

 x^:=m_x1 + m_cur_x * (m_x2 - m_x1 );
 y^:=m_y1 + _y(m_cur_x ) * (m_y2 - m_y1 );

 m_cur_x:=m_cur_x + (1.0 / (m_x2 - m_x1 ) );
 result :=path_cmd_line_to;

end;

END.