File: ppeffect.cpp

package info (click to toggle)
photoprint 0.3.8b-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,396 kB
  • ctags: 3,472
  • sloc: cpp: 25,818; sh: 9,132; ansic: 4,778; makefile: 491; sed: 16
file content (221 lines) | stat: -rw-r--r-- 4,476 bytes parent folder | download
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
#include <iostream>
#include <string.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ppeffect.h"

using namespace std;

PPEffectHeader::PPEffectHeader() : RWMutex(), firsteffect(NULL)
{
}


PPEffectHeader::~PPEffectHeader()
{
	ObtainMutex();
	while(firsteffect)
		delete firsteffect;
	ReleaseMutex();
}


ImageSource *PPEffectHeader::ApplyEffects(ImageSource *source,enum PPEFFECT_STAGE stage)
{
	cerr << "ApplyEffects - Obtain" << endl;
	ObtainMutexShared();
	PPEffect *effect=GetFirstEffect(stage);
	while(effect)
	{
		source=effect->Apply(source);
		effect=effect->Next(stage);	
	}
	cerr << "ApplyEffects - Release" << endl;
	ReleaseMutex();
	return(source);
}


int PPEffectHeader::EffectCount(enum PPEFFECT_STAGE stage)
{
	cerr << "EffectCount - obtain" << endl;
	ObtainMutexShared();
	PPEffect *effect=GetFirstEffect(stage);
	int count=0;
	while(effect)
	{
		count++;
		effect=effect->Next(stage);
	}
	cerr << "EffectCount - Release" << endl;
	ReleaseMutex();
	return(count);
}


PPEffect *PPEffectHeader::GetFirstEffect(enum PPEFFECT_STAGE stage)
{
	cerr << "GetFirstEffect - Obtain" << endl;
//	if(!AttemptMutex())
//		cerr << "GetFirstEffect - deadlock..." << endl;
	ObtainMutexShared();
	PPEffect *result=NULL;
	PPEffect *tmp=firsteffect;
	while(tmp)
	{
		if(tmp->stage & stage)
		{
			result=tmp;
			tmp=NULL;
		}
		else
			tmp=tmp->Next(stage);
	}
	cerr << "GetFirstEffect - Release" << endl;
	ReleaseMutex();
	return(result);
}


PPEffect *PPEffectHeader::Find(const char *id)
{
	if(!id)
		throw "PPEffectHeader::Find: No ID provided";
	cerr << "Find - Obtain" << endl;
//	if(!AttemptMutex())
//		cerr << "Find - deadlock..." << endl;
	ObtainMutexShared();
	PPEffect *result=NULL;
	PPEffect *tmp=firsteffect;
	while(tmp)
	{
		if(strcmp(tmp->GetID(),id)==0)
		{
			
			result=tmp;
			tmp=false;
		}
		else
			tmp=tmp->Next(PPEFFECT_DONTCARE);
	}
	cerr << "Find - Releasing" << endl;
	ReleaseMutex();
	return(result);
}


void PPEffectHeader::ObtainMutex()
{
	// We over-ride the ObtainMutex virtual function here

	RWMutex::ObtainMutex();
}


// We have a node list that works like this:
// Header  <->  Node 1  <->  Node 2  <->  Node 3...
// Each node contains a priority field, so we can slot newly inserted nodes
// into the correct point.

PPEffect::PPEffect(PPEffectHeader &header,int priority,enum PPEFFECT_STAGE stage)
	: priority(priority), stage(stage), header(header),prev(NULL),next(NULL)
{
	cerr << "Effect constructor - obtain" << endl;
	header.ObtainMutex();	// Exclusive lock needed
	PPEffect *node;
	if((node=header.firsteffect))
	{
		// Header  <->  node  <->  ...

		bool done=false;
		do
		{
			if(priority>node->priority)
			{
				// We've found a node with lower priority than this new one
				// So insert beforehand.

				next=node;

				// Header  <->  ...  <//>  this  </->  node... 

				if(node->prev==NULL)
				{
					// If the node being examined has no previous node, we're at the
					// head of the list, so we mark it in the header.
					node->prev=this;
					header.firsteffect=this;
					prev=NULL;
					done=true;
					// Header  <->  this  <->  node...
				}
				else
				{
					// Otherwise, slot this node between the prev node and the one
					// before that.
					node->prev->next=this;
					// Header  <->  ...  </->  this  </->  node  <->  ...
					prev=node->prev;
					// Header  <->  ...  <->  this  </->  node  <->  ...
					node->prev=this;
					// Header  <->  ...  <->  this  <->  node  <->  ...
					done=true;
				}
			}
			// Next node.
			if(node)
			{
				if(node->next)
					node=node->next;
				else if (!done)
				{
					// We've reached the end of the list, and not found any nodes of
					// lower priority - so we tack this node on the end of the list.
					node->next=this;
					prev=node;
					done=true;
				}
			}
		} while(!done);
	}
	else
	{
		// Situation is:
		// Header  <->  <NULL>  -  easy to deal with.
		header.firsteffect=this;
	}
	cerr << "effect constructor - release" << endl;
	header.ReleaseMutex();
}


PPEffect::~PPEffect()
{
	cerr << "Effect destructor - Obtain" << endl;
	header.ObtainMutex();	// Exclusive lock
	if(prev)
		prev->next=next;
	else
		header.firsteffect=next;
	if(next)
		next->prev=prev;
	cerr << "Effect destructor - Release" << endl;
	header.ReleaseMutex();
}


PPEffect *PPEffect::Next(enum PPEFFECT_STAGE stage)
{
	PPEffect *effect=next;
	while(effect)
	{
		if(effect->stage & stage)
			return(effect);
		effect=effect->next;
	}
	return(NULL);
}