summaryrefslogtreecommitdiffstats
path: root/src/livarot/AlphaLigne.cpp
blob: 7ae72c17e32acbcf19a145e3f0ebc4662107d0b8 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors:
 * see git history
 * Fred
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "AlphaLigne.h"

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <glib.h>

AlphaLigne::AlphaLigne(int iMin,int iMax)
{
	min=iMin;
	max=iMax;
	if ( max < min+1 ) max=min+1;
	steps=nullptr;
	nbStep=maxStep=0;
	before.x=min-1;
	before.delta=0;
	after.x=max+1;
	after.delta=0;
}
AlphaLigne::~AlphaLigne()
{
	g_free(steps);
	steps=nullptr;
	nbStep=maxStep=0;
}
void						 AlphaLigne::Affiche()
{
	printf("%i steps\n",nbStep);
	for (int i=0;i<nbStep;i++) {
		printf("(%i %f) ",steps[i].x,steps[i].delta); // localization ok
	}
	printf("\n");
}


void             AlphaLigne::Reset()
{
  // reset to empty line
  // doesn't deallocate the steps array, to minimize memory operations
	curMin=max;
	curMax=min;
	nbStep=0;
	before.x=min-1;
	before.delta=0;
	after.x=max+1;
	after.delta=0;
}
int              AlphaLigne::AddBord(float spos,float sval,float epos,float eval,float tPente)
{
//	printf("%f %f -> %f %f / %f\n",spos,sval,epos,eval,tPente);
	if ( sval == eval ) return 0;
  // compute the footprint of [spos,epos] on the line of pixels
	float  curStF=floor(spos);
	float  curEnF=floor(epos);
	int   curSt=(int)curStF;
	int   curEn=(int)curEnF;
	
  // update curMin and curMax
	if ( curSt > max ) {
    // we're on the right of the visible portion of the line: bail out!
    if ( eval < sval ) curMax=max;
    return 0;
  }
	if ( curSt < curMin ) curMin=curSt;
	if ( ceil(epos) > curMax ) curMax=(int)ceil(epos);
	
  // clamp the changed portion to [min,max], no need for bigger
	if ( curMax > max ) curMax=max;
	if ( curMin < min ) curMin=min;
	
  // total amount of change in pixel coverage from before the right to after the run
	float    needed=eval-sval;
	float    needC=/*(int)ldexpf(*/needed/*,24)*/;
	
	if ( curEn < min ) {
    // the added portion is entirely on the left, so we only have to change the initial coverage for the line
    before.delta+=needC;
    return 0;
	}
  
  // add the steps
  // the pixels from [curSt..curEn] (included) intersect with [spos;epos]
  // since we're dealing with delta in the coverage, there is also a curEn+1 delta, since the curEn pixel intersect
  // with [spos;epos] and thus has some delta with respect to its next pixel
  // lots of different cases... ugly
	if ( curSt == curEn ) {
		if ( curSt+1 < min ) {
			before.delta+=needC;
		} else {
			if ( nbStep+2 >= maxStep ) {
				maxStep=2*nbStep+2;
				steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
			}
			float  stC=/*(int)ldexpf(*/(eval-sval)*(0.5*(epos-spos)+curStF+1-epos)/*,24)*/;
			steps[nbStep].x=curSt;
			steps[nbStep].delta=stC;
			nbStep++;
			steps[nbStep].x=curSt+1;
			steps[nbStep].delta=needC-stC; //  au final, on a toujours le bon delta, meme avec une arete completement verticale
			nbStep++;
		}
	} else if ( curEn == curSt+1 ) {
		if ( curSt+2 < min ) {
			before.delta+=needC;
		} else {
			if ( nbStep+3 >= maxStep ) {
				maxStep=2*nbStep+3;
				steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
			}
			float  stC=/*(int)ldexpf(*/0.5*tPente*(curEnF-spos)*(curEnF-spos)/*,24)*/;
			float  enC=/*(int)ldexpf(*/tPente-0.5*tPente*((spos-curStF)*(spos-curStF)+(curEnF+1.0-epos)*(curEnF+1.0-epos))/*,24)*/;
			steps[nbStep].x=curSt;
			steps[nbStep].delta=stC;
			nbStep++;
			steps[nbStep].x=curEn;
			steps[nbStep].delta=enC;
			nbStep++;
			steps[nbStep].x=curEn+1;
			steps[nbStep].delta=needC-stC-enC;
			nbStep++;
		}
	} else {
		float  stC=/*(int)ldexpf(*/0.5*tPente*(curStF+1-spos)*(curStF+1-spos)/*,24)*/;
		float  stFC=/*(int)ldexpf(*/tPente-0.5*tPente*(spos-curStF)*(spos-curStF)/*,24)*/;
		float  enC=/*(int)ldexpf(*/tPente-0.5*tPente*(curEnF+1.0-epos)*(curEnF+1.0-epos)/*,24)*/;
		float  miC=/*(int)ldexpf(*/tPente/*,24)*/;
		if ( curSt < min ) {
			if ( curEn > max ) {
				if ( nbStep+(max-min) >= maxStep ) {
					maxStep=2*nbStep+(max-min);
					steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
				}
				float  bfd=min-curSt-1;
				bfd*=miC;
				before.delta+=stC+bfd;
				for (int i=min;i<max;i++) {
					steps[nbStep].x=i;
					steps[nbStep].delta=miC;
					nbStep++;
				}
			} else {
				if ( nbStep+(curEn-min)+2 >= maxStep ) {
					maxStep=2*nbStep+(curEn-min)+2;
					steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
				}
				float  bfd=min-curSt-1;
				bfd*=miC;
				before.delta+=stC+bfd;
				for (int i=min;i<curEn;i++) {
					steps[nbStep].x=i;
					steps[nbStep].delta=miC;
					nbStep++;
				}
				steps[nbStep].x=curEn;
				steps[nbStep].delta=enC;
				nbStep++;
				steps[nbStep].x=curEn+1;
				steps[nbStep].delta=needC-stC-stFC-enC-(curEn-curSt-2)*miC;
				nbStep++;
			}
		} else {
			if ( curEn > max ) {
				if ( nbStep+3+(max-curSt) >= maxStep ) {
					maxStep=2*nbStep+3+(curEn-curSt);
					steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
				}
				steps[nbStep].x=curSt;
				steps[nbStep].delta=stC;
				nbStep++;
				steps[nbStep].x=curSt+1;
				steps[nbStep].delta=stFC;
				nbStep++;
				for (int i=curSt+2;i<max;i++) {
					steps[nbStep].x=i;
					steps[nbStep].delta=miC;
					nbStep++;
				}
			} else {
				if ( nbStep+3+(curEn-curSt) >= maxStep ) {
					maxStep=2*nbStep+3+(curEn-curSt);
					steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
				}
				steps[nbStep].x=curSt;
				steps[nbStep].delta=stC;
				nbStep++;
				steps[nbStep].x=curSt+1;
				steps[nbStep].delta=stFC;
				nbStep++;
				for (int i=curSt+2;i<curEn;i++) {
					steps[nbStep].x=i;
					steps[nbStep].delta=miC;
					nbStep++;
				}
				steps[nbStep].x=curEn;
				steps[nbStep].delta=enC;
				nbStep++;
				steps[nbStep].x=curEn+1;
				steps[nbStep].delta=needC-stC-stFC-enC-(curEn-curSt-2)*miC;
				nbStep++;
			}
		}
	}

	return 0;
}
int              AlphaLigne::AddBord(float spos,float sval,float epos,float eval)
{
	// pas de pente dans ce cas; on ajoute le delta au premier pixel
	float   tPente=(eval-sval);

	float curStF=floor(spos);
	float curEnF=floor(epos);
	int   curSt=(int)curStF;
	int   curEn=(int)curEnF;
	
	if ( curSt > max ) {
    if ( eval < sval ) curMax=max;
    return 0; // en dehors des limites (attention a ne pas faire ca avec curEn)
  }
	if ( curEn < min ) {
    before.delta+=eval-sval;
    return 0; // en dehors des limites (attention a ne pas faire ca avec curEn)
	}
	
	if ( curSt < curMin ) curMin=curSt;
//	int   curEn=(int)curEnF;
	if ( ceil(epos) > curMax-1 ) curMax=1+(int)ceil(epos);
	if ( curSt < min ) {
		before.delta+=eval-sval;
	} else {
		AddRun(curSt,/*(int)ldexpf(*/(((float)(curSt+1))-spos)*tPente/*,24)*/);
		AddRun(curSt+1,/*(int)ldexpf(*/(spos-((float)(curSt)))*tPente/*,24)*/);
	}
	return 0;
}

void             AlphaLigne::Flatten()
{
  // just sort
	if ( nbStep > 0 ) qsort(steps,nbStep,sizeof(alpha_step),CmpStep);
}
void             AlphaLigne::AddRun(int st,float pente)
{
	if ( nbStep >= maxStep ) {
		maxStep=2*nbStep+1;
		steps=(alpha_step*)g_realloc(steps,maxStep*sizeof(alpha_step));
	}
	int nStep=nbStep++;
	steps[nStep].x=st;
	steps[nStep].delta=pente;
}

void             AlphaLigne::Raster(raster_info &dest,void* color,RasterInRunFunc worker)
{
  // start by checking if there are actually pixels in need of rasterization
	if ( curMax <= curMin ) return;
	if ( dest.endPix <= curMin || dest.startPix >= curMax ) return;

	int    nMin=curMin,nMax=curMax;
	float  alpSum=before.delta; // alpSum will be the pixel coverage value, so we start at before.delta
	int    curStep=0;
  
  // first add all the deltas up to the first pixel in need of rasterization
  while ( curStep < nbStep && steps[curStep].x < nMin ) { 
    alpSum+=steps[curStep].delta;
    curStep++;
  }
  // just in case, if the line bounds are greater than the buffer bounds.
	if ( nMin < dest.startPix ) {
		for (;( curStep < nbStep && steps[curStep].x < dest.startPix) ;curStep++) alpSum+=steps[curStep].delta;
		nMin=dest.startPix;
	}
	if ( nMax > dest.endPix ) nMax=dest.endPix;

  // raster!
	int       curPos=dest.startPix;
	for (;curStep<nbStep;curStep++) {
		if ( alpSum > 0 && steps[curStep].x > curPos ) {
      // we're going to change the pixel position curPos, and alpSum is > 0: rasterization needed from
      // the last position (curPos) up to the pixel we're moving to (steps[curStep].x)
			int  nst=curPos,nen=steps[curStep].x;
//Buffer::RasterRun(dest,color,nst,alpSum,nen,alpSum);
      (worker)(dest,color,nst,alpSum,nen,alpSum);
		}
    // add coverage deltas
		alpSum+=steps[curStep].delta;
		curPos=steps[curStep].x;
		if ( curPos >= nMax ) break;
	}
  // if we ended the line with alpSum > 0, we need to raster from curPos to the right edge
	if ( alpSum > 0 && curPos < nMax ) {
		int  nst=curPos,nen=max;
    (worker)(dest,color,nst,alpSum,nen,alpSum);
//Buffer::RasterRun(dest,color,nst,alpSum,nen,alpSum);
	}
}