summaryrefslogtreecommitdiff
path: root/dxr3colormanager.c
blob: 93311ba2bade2dc5975fbebe91c6aeafa6ae7356 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/***************************************************************************
                          dxr3colormanager.c -  description
                             -------------------
    begin                : Tue Oct 22 2002
    copyright            : (C) 2002 by Stefan Schluenss
    email                : vdr@schluenss.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/**
 * Background:
 * Each encoded pixel in the SPU could have one of the values 0,1,2,3.    *
 * These values

 	Pixelvalue	Maps
				to index
 		0		-->		4	-->


 * The SPU data definition allows highlighting of rectangular areas. They *
 * are defined by a starting and ending row. Whithin these rows one could *
 * define a starting column for using a new color palette mapping. This   *
 * mapping will be used til the end of the line or up to the next column  *
 * defintion.
 * Look at the picture below:
 *
	Row		   Col=5     Col=16
	n-2		...............................
	n-1		...............................
	n		.....-------------------------- <- Highligh region starts here
			.....| 6,3,1,2 | 0,2,8,9
			.....| 6,3,1,2 | 0,2,8,9
			.....| 6,3,1,2 | 0,2,8,9
	n+4		.....-------------------------- <- Highligh region ends here
	n+5		...............................
	n+6		...............................

	In the above example one region (from n to n+4) is defined with two    *
	highlight sections - one from column 5 up to 15 and the second from 16 *
	til the end of the line.
**/


#include <assert.h>

#include "dxr3colormanager.h"
#include "dxr3memcpy.h"
#include <stdio.h>
#include <string.h>

// ==================================
//! constructor
cColorManager::cColorManager()
{
	NrOfRegions = 0;
	isopen = false;
	for(int i = 0; i < MAX_NO_OF_REGIONS; i++)
		hlr[i] = NULL;
}

// ==================================
cColorManager::~cColorManager()
{
	for (int i = 0; i < NrOfRegions; i++)
	{
		if (hlr[i])
		{
			delete(hlr[i]);
		}
	}
}

// ==================================
// Opens a new highlight region
void cColorManager::OpenRegion(int y, int NrOfSecToCopy)
{
	hlr[NrOfRegions] = new yRegion();
	hlr[NrOfRegions]->Y1 = y;
	isopen = true;

    if (NrOfSecToCopy > 0)
    {
    	for (int i = 0; i < NrOfSecToCopy; i++)
        {
        	hlr[NrOfRegions]->Section[i] = hlr[NrOfRegions - 1]->Section[i];
        }
    }
}

// ==================================
// Closes the spu-highlight region
void cColorManager::CloseRegion(int y)
{

	hlr[NrOfRegions]->Y2 = y;
	isopen = false;

	if (hlr[NrOfRegions]->N != 0)		// skip this region if there is no section defined
	{
		if (NrOfRegions < MAX_NO_OF_SECTIONS -1)
        {
			NrOfRegions++;
   		}
	}

}

// ==================================    
void cColorManager::EncodeColors(int width, int height, unsigned char* map, unsigned char* dmap)
{
    unsigned char color = 0xFF, ccol = 0xFF;
    unsigned char ColorIndex = 0xFF;
    unsigned char buffer[1024] = {0};

    for (int y = 0; y < height; ++y) 
	{
        color = 0xFF;
        for(int x = 0; x < width; ++x) 
		{
            ccol = map[y * width + x];
            if (ccol != 0) MaxY = y;
            if (ccol != color)  
			{
                color = ccol; // save this color
                if (!AddColor(x,y,color, ColorIndex)) 
				{ 
					// add this color to highlight regions
                    color = 0xFF;
                    x = -1;
                } 
				else
				{ 
					// color successfully added
                    buffer[x] = ColorIndex;
                }
            } 
			else
			{
                buffer[x] = ColorIndex;//*(dmap+(y * width + x)) = ColorIndex;
            }
        }
        dxr3_memcpy(dmap+y*width, buffer,width);
    }
}

// ==================================
unsigned char cColorManager::AddColor(int x, int y, unsigned char color, unsigned char &ColorIndex) {
    static int yold = -1;
    xSection* Section = 0;
    int SectionIndex = 0;

    if (isopen) 
	{
		// there is an opened highlight-region
        Section = GetSection(x, SectionIndex);	
		
		// checks whether we have a section defined on the formerly line on this x-position
        if (Section != NULL) 
		{ 
			// there was a section
            if (!Section->HasColor(color, ColorIndex)) 
			{ 
				// this color is new for this section
                if (Section->AllColorsUsed()) 
				{ 
					// no more free colors
                    if (yold != y) 
					{
                        CloseRegion(y-1); 
						// terminate region
                        return(0);
                        yold = y;
						// open new region
                        OpenRegion(y,SectionIndex+1);
                    }
					// create new section
                    Section = NewSection(x);
                }
				// and add new color
                ColorIndex = Section->AddColor(color);
            }
        }
		else
		{ 
			// no section found (but region already open)

			// terminate region
            CloseRegion(y-1);
            yold = y;
			// open new region
            OpenRegion(y);
        	// create new section
			Section = NewSection(x);           
			// and add new color
			ColorIndex = Section->AddColor(color);
        }
    } 
	else
	{ 
		// currently no region open
        yold = y;

		// open new region
        OpenRegion(y);
		// create new section
        Section = NewSection(x);
		// and add new color
        ColorIndex = Section->AddColor(color);
    }
    return(1);
}

// ==================================
xSection *cColorManager::GetSection(int x, int &n)
{
	int i;
    n = 0;

	// for every section in the current region
    for (i = 0; i < hlr[NrOfRegions]->N; i++)	
    {
    	if ((x <= hlr[NrOfRegions]->Section[i]->X2) &&  (x >= hlr[NrOfRegions]->Section[i]->X1)) // x-pos is in section
        {
        	n = i;
        	return (hlr[NrOfRegions]->Section[i]);
        }
    }
    return(NULL);
}

/** Adds a new highlight region beginning from FIRST to LAST column */
/**
void cColorManager::AddRegion(int first, int last, unsigned int color, unsigned int opac)
{
	DIAG("AddRegion(%d %d %x %x)\n",first, last, color, opac);
	hlr[NrOfRegions]->AddSection(first, last, color, opac);
}
**/

// ==================================
// convert into SPU - hope is correct description
unsigned char* cColorManager::GetSpuData(int& len)
{
    if (isopen)		// there is an opened highlight-region
		CloseRegion(MaxY);

	if (NrOfRegions != 0)
    {
    int ptr = 0;
	spudata[ptr++] = 0x07;	// CHG_COLCON command
	spudata[ptr++] = 0x00;  // total size of parameter area
	spudata[ptr++] = 0x00;  // will be filled later


	for(int i = 0; i < NrOfRegions;i++)
	{
		spudata[ptr++] = (hlr[i]->Y1 >> 8) & 0x0f;
		spudata[ptr++] = (hlr[i]->Y1 & 0xff);
		spudata[ptr++] = ( ( (hlr[i]->N) & 0x0f) << 4)	| ((hlr[i]->Y2 >> 8) & 0x0f);
		spudata[ptr++] = (hlr[i]->Y2 & 0xff);

		for(int c = 0; c < hlr[i]->N; c++)
		{
			spudata[ptr++] = hlr[i]->Section[c]->X1 >> 8;
			spudata[ptr++] = hlr[i]->Section[c]->X1 & 0xff;
			spudata[ptr++] = (hlr[i]->Section[c]->Colors[3] << 4) | (hlr[i]->Section[c]->Colors[2] & 0x0F);
			spudata[ptr++] = (hlr[i]->Section[c]->Colors[1] << 4) | (hlr[i]->Section[c]->Colors[0] & 0x0F);

			spudata[ptr++] = (hlr[i]->Section[c]->Opac[3] << 4) | hlr[i]->Section[c]->Opac[2];
			spudata[ptr++] = (hlr[i]->Section[c]->Opac[1] << 4) | hlr[i]->Section[c]->Opac[0];
		}
	}
	spudata[ptr++] = 0x0f;	// termination of parameter block
	spudata[ptr++] = 0xff;
	spudata[ptr++] = 0xff;
	spudata[ptr++] = 0xff;
	int size = ptr - 1;
	spudata[1] = size >> 8;
	spudata[2] = size & 0xff;

	len = ptr;
    }
    else
    	len = 0;

#if OSD_SPU_CM_DUMP
	FILE *fp;
    fp = fopen("CM.dump","a+");
    fprintf(fp,"len:%03d ",len);
    for (int i = 0; i < len; i++)
    	fprintf(fp,"%02X",*(spudata+i));
    fprintf(fp,"\n");
    fclose(fp);
#endif

	return(spudata);
}

// ==================================
xSection *cColorManager::NewSection(int x)
{
	xSection* sec = new xSection(x);
    int N = hlr[NrOfRegions]->N;

	hlr[NrOfRegions]->Section[hlr[NrOfRegions]->N] = sec;
	if (N > 0)
    	hlr[NrOfRegions]->Section[hlr[NrOfRegions]->N - 1]->X2 = x-1;
	(hlr[NrOfRegions]->N)++;

    return(sec);
}

/**
// No descriptions */
/**
void HLRegion::AddSection(int first, int last, unsigned int color, unsigned int opac)
{
	DIAG("  AddSection %d,%d %X,%X\n",first, last, color, opac);
	Region[N] = new ColRegion(first, last, color, opac);
	assert(N<MAX_NO_OF_REGIONS -1);
	N++;
	DIAG("HL N:%d\n",N);
}
**/

// ==================================
xSection::xSection(int x)
{
    X1 = x;
    X2 = 0xFFF;
    NrOfColors = 0;
    for (int i = 0; i < 4; i++)
    {
		Opac[i] = 0xFF;
		Colors[i] = 0;
    }
}

// ==================================
unsigned char xSection::AddColor(unsigned int color)
{
	unsigned char ColorIndex = 0;

	if (NrOfColors <= 3)
    {
		Colors[NrOfColors] = color;
		Opac[NrOfColors] = color >> 4;
        ColorIndex = NrOfColors;
        NrOfColors++;
    }
    return(ColorIndex);
}

// ==================================
bool xSection::HasColor(unsigned int color, unsigned char &ColorIndex)
{
	for(int i = 0; i < NrOfColors; i++)
    {
    	if (Colors[i] == color)
        {
        	ColorIndex = i;
        	return (true);
        }
    }
    return(false);
}

/** No descriptions */
void cColorManager::SetBgColor(unsigned int bgColor)
{
//	cColorManager::BgColor = bgColor;
}

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// indent-tabs-mode: t
// End: