~ubuntu-branches/ubuntu/maverick/vice/maverick

« back to all changes in this revision

Viewing changes to src/video/video-color.c

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2005-02-01 11:30:26 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050201113026-3eyakzsmmheclvjg
Tags: 1.16-1
* New upstream version
* Fixes crash on 64-bit architectures (closes: #287640)
* x128 working again (closes: #286767)
* Works fine with /dev/dsp in use (not in the main changelog, but tested
  on my local machine as working).  Presumably, this also takes care of
  the issue with dsp being held.  I'm not sure if this is because I'm
  testing it on a 2.6 kernel now -- if you are still having problems
  with /dev/dsp, please reopen the bugs. (closes: #152952, #207942)
* Don't kill Makefile.in on clean

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <stdlib.h>
31
31
#include <math.h>       /* needed for pow function */
32
32
 
 
33
#include "lib.h"
33
34
#include "machine.h"
34
35
#include "palette.h"
35
36
#include "video-canvas.h"
57
58
DWORD color_grn[256];
58
59
DWORD color_blu[256];
59
60
 
 
61
/* YUV table for hardware rendering: (Y << 16) | (U << 8) | V */
 
62
DWORD yuv_table[128];
 
63
 
60
64
 
61
65
void video_render_setrawrgb(unsigned int index, DWORD r, DWORD g, DWORD b)
62
66
{
101
105
    float cr;
102
106
} video_ycbcr_color_t;
103
107
 
 
108
typedef struct video_ycbcr_palette_s {
 
109
    unsigned int num_entries;
 
110
    video_ycbcr_color_t *entries;
 
111
} video_ycbcr_palette_t;
 
112
 
 
113
static video_ycbcr_palette_t *video_ycbcr_palette_create(unsigned int num_entries)
 
114
{
 
115
    video_ycbcr_palette_t *p;
 
116
 
 
117
    p = (video_ycbcr_palette_t *)lib_malloc(sizeof(video_ycbcr_palette_t));
 
118
 
 
119
    p->num_entries = num_entries;
 
120
    p->entries = lib_calloc(num_entries, sizeof(video_ycbcr_color_t));
 
121
 
 
122
    return p;
 
123
}
 
124
 
 
125
static void video_ycbcr_palette_free(video_ycbcr_palette_t *p)
 
126
{
 
127
    if (p == NULL)
 
128
        return;
 
129
 
 
130
    lib_free(p->entries);
 
131
    lib_free(p);
 
132
}
 
133
 
 
134
 
104
135
/* variables needed for generating and activating a palette */
105
136
 
106
137
struct video_canvas_s *video_current_canvas = NULL;
207
238
    dst->name   = NULL;
208
239
}
209
240
 
 
241
/* conversion of RGB to YCbCr */
 
242
 
 
243
static void video_convert_rgb_to_ycbcr(const palette_entry_t *src,
 
244
                                       video_ycbcr_color_t *dst)
 
245
{
 
246
    float yf, cbf, crf;
 
247
    int y, cb, cr;
 
248
 
 
249
    /* convert RGB to YCbCr */
 
250
 
 
251
    yf = 0.2989f*src->red + 0.5866f*src->green + 0.1145f*src->blue;
 
252
    cbf = src->blue - yf;
 
253
    crf = src->red - yf;
 
254
 
 
255
    /* convert to int and clip to 8 bit boundaries */
 
256
 
 
257
    y  = (int)yf;
 
258
    cb = (int)cbf;
 
259
    cr = (int)crf;
 
260
 
 
261
    if (y  <   0) y  =   0;
 
262
    if (y  > 255) y  = 255;
 
263
    if (cb <   0) cb =   0;
 
264
    if (cb > 255) cb = 255;
 
265
    if (cr <   0) cr =   0;
 
266
    if (cr > 255) cr = 255;
 
267
 
 
268
    dst->y  = (BYTE)y;
 
269
    dst->cb = (BYTE)cb;
 
270
    dst->cr = (BYTE)cr;
 
271
}
 
272
 
210
273
/* gammatable calculation */
211
274
 
212
275
static void video_calc_gammatable(void)
242
305
 
243
306
/* ycbcr table calculation */
244
307
 
245
 
/* YUV table for hardware rendering: (Y << 16) | (U << 8) | V */
246
 
DWORD yuv_table[128];
247
 
 
248
 
static void video_calc_ycbcrtable(const video_cbm_palette_t *p)
 
308
static void video_calc_ycbcrtable(const video_ycbcr_palette_t *p)
249
309
{
250
 
    video_ycbcr_color_t primary;
 
310
    video_ycbcr_color_t *primary;
251
311
    unsigned int i, lf, hf;
252
312
    float sat;
253
313
 
254
 
        lf = 64*video_resources.pal_blur/1000;
255
 
        hf = 256 - (lf << 1);
 
314
    lf = 64*video_resources.pal_blur/1000;
 
315
    hf = 256 - (lf << 1);
256
316
    sat = ((float)(video_resources.color_saturation)) * (256.0f / 1000.0f);
257
317
    for (i = 0;i < p->num_entries; i++) {
258
 
        video_convert_cbm_to_ycbcr(&p->entries[i], p->saturation,
259
 
                                   p->phase,&primary);
260
 
        ytable[i] = (SDWORD)(primary.y * 256.0f);
 
318
        primary = &p->entries[i];
 
319
        ytable[i] = (SDWORD)(primary->y * 256.0f);
261
320
        ytablel[i] = ytable[i]*lf;
262
321
        ytableh[i] = ytable[i]*hf;
263
 
        cbtable[i] = (SDWORD)(primary.cb * sat);
264
 
        crtable[i] = (SDWORD)(primary.cr * sat);
 
322
        cbtable[i] = (SDWORD)(primary->cb * sat);
 
323
        crtable[i] = (SDWORD)(primary->cr * sat);
265
324
 
266
325
        /* YCbCr to YUV, scale [0, 256] to [0, 255] */
267
 
        yuv_table[i] = ((BYTE)(primary.y * 255 / 256 + 0.5) << 16)
268
 
            | ((BYTE)(0.493111 * primary.cb * 255 / 256 + 128.5) << 8)
269
 
            | (BYTE)(0.877283 * primary.cr * 255 / 256 + 128.5);
 
326
        yuv_table[i] = ((BYTE)(primary->y * 255 / 256 + 0.5) << 16)
 
327
            | ((BYTE)(0.493111 * primary->cb * 255 / 256 + 128.5) << 8)
 
328
            | (BYTE)(0.877283 * primary->cr * 255 / 256 + 128.5);
 
329
    }
 
330
}
 
331
 
 
332
/* Convert an RGB palette to YCbCr. */
 
333
static void video_palette_to_ycbcr(const palette_t *p,
 
334
                                   video_ycbcr_palette_t* ycbcr)
 
335
{
 
336
    unsigned int i;
 
337
 
 
338
    for (i = 0;i < p->num_entries; i++) {
 
339
        video_convert_rgb_to_ycbcr(&p->entries[i], &ycbcr->entries[i]);
 
340
    }
 
341
}
 
342
 
 
343
/* Convert a CBM palette to YCbCr. */
 
344
static void video_cbm_palette_to_ycbcr(const video_cbm_palette_t *p,
 
345
                                       video_ycbcr_palette_t* ycbcr)
 
346
{
 
347
    unsigned int i;
 
348
 
 
349
    for (i = 0;i < p->num_entries; i++) {
 
350
        video_convert_cbm_to_ycbcr(&p->entries[i], p->saturation,
 
351
                                   p->phase, &ycbcr->entries[i]);
270
352
    }
271
353
}
272
354
 
273
355
/* Calculate a RGB palette out of VIC/VIC-II/TED colors.  */
274
 
static palette_t *video_calc_palette(const video_cbm_palette_t *p)
 
356
static palette_t *video_calc_palette(const video_ycbcr_palette_t *p)
275
357
{
276
358
    palette_t *prgb;
277
359
    video_ycbcr_color_t primary;
290
372
            return NULL;
291
373
 
292
374
        for (i = 0; i <p->num_entries; i++) {
293
 
            video_convert_cbm_to_ycbcr(&p->entries[i], p->saturation,
294
 
                                       p->phase, &primary);
295
 
            video_convert_ycbcr_to_rgb(&primary, sat, bri, con, gam,
 
375
            video_convert_ycbcr_to_rgb(&p->entries[i], sat, bri, con, gam,
296
376
                                       &prgb->entries[i]);
297
377
        }
298
378
    } else {
305
385
 
306
386
        index = 0;
307
387
        for (j = 0; j < p->num_entries; j++) {
308
 
            video_convert_cbm_to_ycbcr(&p->entries[j], p->saturation,
309
 
                                       p->phase, &primary);
 
388
            primary = p->entries[j];
310
389
            cb = primary.cb;
311
390
            cr = primary.cr;
312
391
            for (i = 0; i < p->num_entries; i++) {
313
 
                video_convert_cbm_to_ycbcr(&p->entries[i], p->saturation,
314
 
                                           p->phase, &primary);
 
392
                primary = p->entries[i];
315
393
                primary.cb = (primary.cb + cb) * 0.5f;
316
394
                primary.cr = (primary.cr + cr) * 0.5f;
317
395
                video_convert_ycbcr_to_rgb(&primary, sat, bri, con, gam,
346
424
int video_color_update_palette(struct video_canvas_s *canvas)
347
425
{
348
426
    palette_t *palette;
 
427
    video_ycbcr_palette_t *ycbcr;
349
428
 
350
429
    if (canvas == NULL)
351
430
        return 0;
355
434
    if (canvas->videoconfig->external_palette) {
356
435
        palette = video_load_palette(canvas->videoconfig->cbm_palette,
357
436
                                     canvas->videoconfig->external_palette_name);
 
437
        video_calc_gammatable();
 
438
        ycbcr = video_ycbcr_palette_create(palette->num_entries);
 
439
        video_palette_to_ycbcr(palette, ycbcr);
 
440
        video_calc_ycbcrtable(ycbcr);
 
441
        if (video_resources.delayloop_emulation) {
 
442
            palette_free(palette);
 
443
            palette = video_calc_palette(ycbcr);
 
444
        }
358
445
    } else {
359
446
        video_calc_gammatable();
360
 
        video_calc_ycbcrtable(canvas->videoconfig->cbm_palette);
361
 
        palette = video_calc_palette(canvas->videoconfig->cbm_palette);
 
447
        ycbcr = video_ycbcr_palette_create(canvas->videoconfig->cbm_palette->num_entries);
 
448
        video_cbm_palette_to_ycbcr(canvas->videoconfig->cbm_palette, ycbcr);
 
449
        video_calc_ycbcrtable(ycbcr);
 
450
        palette = video_calc_palette(ycbcr);
362
451
    }
363
452
 
 
453
    video_ycbcr_palette_free(ycbcr);
 
454
 
364
455
    if (palette != NULL)
365
456
        return video_canvas_palette_set(canvas, palette);
366
457