Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * gdith.c --
3
 *
4
 *      Procedures dealing with grey-scale and mono dithering,
5
 *      as well as X Windows set up procedures.
6
 *
7
 */
8
 
9
/*
10
 * Copyright (c) 1995 The Regents of the University of California.
11
 * All rights reserved.
12
 *
13
 * Permission to use, copy, modify, and distribute this software and its
14
 * documentation for any purpose, without fee, and without written agreement is
15
 * hereby granted, provided that the above copyright notice and the following
16
 * two paragraphs appear in all copies of this software.
17
 *
18
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
19
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
20
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
21
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
 *
23
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
26
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
27
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
28
 */
29
 
30
/*
31
 * Portions of this software Copyright (c) 1995 Brown University.
32
 * All rights reserved.
33
 *
34
 * Permission to use, copy, modify, and distribute this software and its
35
 * documentation for any purpose, without fee, and without written agreement
36
 * is hereby granted, provided that the above copyright notice and the
37
 * following two paragraphs appear in all copies of this software.
38
 *
39
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
40
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
41
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
42
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 *
44
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
45
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
46
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
47
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
48
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49
 */
50
 
51
#include <math.h>
52
#include "video.h"
53
#include "proto.h"
54
#include "dither.h"
55
#ifndef NOCONTROLS
56
#include "ctrlbar.h"
57
#endif
58
#include <sys/time.h>
59
 
60
#ifdef __STDC__
61
#include <stdlib.h>
62
#include <string.h>
63
#endif
64
 
65
/*
66
   Changes to make the code reentrant:
67
      X variables now passed in xinfo: display, ximage,cmap,window, gc, etc
68
      De-globalized: ditherType, matched_depth, totNumFrames
69
      vid_stream->film_has_ended instead of FilmState
70
 
71
   Additional changes:
72
      Now can name and position each movie window individually
73
      DISABLE_DITHER cpp define - do not include dither code if defined
74
      NOFRAMECOUNT cpp define - do not count frames when running without
75
         controls
76
      Short circuit InitColorDisplay if not displaying anything
77
      ExistingWindow default now 0
78
   -lsh@cs.brown.edu (Loring Holden)
79
*/
80
 
81
/* Range values for lum, cr, cb. */
82
int LUM_RANGE;
83
int CR_RANGE;
84
int CB_RANGE;
85
 
86
/* Array that remaps color numbers to actual pixel values used by X server. */
87
 
88
unsigned char pixel[256];
89
unsigned long wpixel[256];
90
 
91
/* Arrays holding quantized value ranged for lum, cr, and cb. */
92
 
93
int *lum_values;
94
int *cr_values;
95
int *cb_values;
96
 
97
/* Declaration of global variable containing dither type. */
98
 
99
/* Frame Rate Info */
100
extern int framerate;
101
 
102
/* Video rates table */
103
/* Cheat on Vid rates, round to 30, and use 30 if illegal value
104
   Except for 9, where Xing means 15, and given their popularity, we'll
105
   be nice and do it */
106
static int VidRateNum[16]={30, 24, 24, 25, 30, 30, 50, 60,
107
                         60, 15, 30, 30, 30, 30, 30, 30};
108
 
109
/* Luminance and chrominance lookup tables */
110
static double *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
111
 
112
 
113
/*
114
 *--------------------------------------------------------------
115
 *
116
 * InitColor --
117
 *
118
 *      Initialize lum, cr, and cb quantized range value arrays.
119
 *      Also initializes the lookup tables for the possible
120
 *      values of lum, cr, and cb.
121
 *    Color values from ITU-R BT.470-2 System B, G and SMPTE 170M
122
 *    see InitColorDither in 16bits.c for more
123
 *
124
 * Results:
125
 *      None.
126
 *
127
 * Side effects:
128
 *      None.
129
 *
130
 *--------------------------------------------------------------
131
 */
132
 
133
void
134
InitColor()
135
{
136
  int i, CR, CB;
137
 
138
  L_tab    = (double *)malloc(LUM_RANGE*sizeof(double));
139
  Cr_r_tab = (double *)malloc(CR_RANGE*sizeof(double));
140
  Cr_g_tab = (double *)malloc(CR_RANGE*sizeof(double));
141
  Cb_g_tab = (double *)malloc(CB_RANGE*sizeof(double));
142
  Cb_b_tab = (double *)malloc(CB_RANGE*sizeof(double));
143
 
144
  if (L_tab == NULL    || Cr_r_tab == NULL ||
145
      Cr_g_tab == NULL || Cb_g_tab == NULL ||
146
      Cb_b_tab == NULL) {
147
    fprintf(stderr, "Could not alloc memory in InitColor\n");
148
    exit(1);
149
  }
150
 
151
  for (i=0; i<LUM_RANGE; i++) {
152
    lum_values[i]  = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
153
    L_tab[i] = lum_values[i];
154
    if (gammaCorrectFlag) {
155
      L_tab[i] = GAMMA_CORRECTION(L_tab[i]);
156
    }
157
  }
158
 
159
  for (i=0; i<CR_RANGE; i++) {
160
    register double tmp;
161
    if (chromaCorrectFlag) {
162
      tmp = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
163
      Cr_r_tab[i] = (int) (0.419/0.299) * CHROMA_CORRECTION128D(tmp - 128.0);
164
      Cr_g_tab[i] = (int) -(0.299/0.419) * CHROMA_CORRECTION128D(tmp - 128.0);
165
      cr_values[i] = CHROMA_CORRECTION256(tmp);
166
    } else {
167
      tmp = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
168
      Cr_r_tab[i] = (int)  (0.419/0.299) * (tmp - 128.0);
169
      Cr_g_tab[i] = (int) -(0.299/0.419) * (tmp - 128.0);
170
      cr_values[i] = (int) tmp;
171
    }
172
  }
173
 
174
  for (i=0; i<CB_RANGE; i++) {
175
    register double tmp;
176
    if (chromaCorrectFlag) {
177
      tmp = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
178
      Cb_g_tab[i] = (int) -(0.114/0.331) * CHROMA_CORRECTION128D(tmp - 128.0);
179
      Cb_b_tab[i] = (int)  (0.587/0.331) * CHROMA_CORRECTION128D(tmp - 128.0);
180
      cb_values[i] = CHROMA_CORRECTION256(tmp);
181
    } else {
182
      tmp = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
183
      Cb_g_tab[i] = (int) -(0.114/0.331) * (tmp - 128.0);
184
      Cb_b_tab[i] = (int)  (0.587/0.331) * (tmp - 128.0);
185
      cb_values[i] = (int) tmp;
186
    }
187
  }
188
 
189
}
190
 
191
 
192
/*
193
 *--------------------------------------------------------------
194
 *
195
 * ConvertColor --
196
 *
197
 *      Given a l, cr, cb tuple, converts it to r,g,b.
198
 *
199
 * Results:
200
 *      r,g,b values returned in pointers passed as parameters.
201
 *
202
 * Side effects:
203
 *      None.
204
 *
205
 *--------------------------------------------------------------
206
 */
207
 
208
static void
209
ConvertColor(l, cr, cb, r, g, b)
210
     unsigned int l, cr, cb;
211
     unsigned char *r, *g, *b;
212
{
213
  double fl, fcr, fcb, fr, fg, fb;
214
 
215
/*
216
 * Old method w/o lookup table
217
 *
218
 * fl = 1.164*(((double) l)-16.0);
219
 * fcr =  ((double) cr) - 128.0;
220
 * fcb =  ((double) cb) - 128.0;
221
 *
222
 * fr = fl + (1.366 * fcr);
223
 * fg = fl - (0.700 * fcr) - (0.334 * fcb);
224
 * fb = fl + (1.732 * fcb);
225
 */
226
 
227
  fl = L_tab[l];
228
 
229
  fr = fl + Cr_r_tab[cr];
230
  fg = fl + Cr_g_tab[cr] + Cb_g_tab[cb];
231
  fb = fl + Cb_b_tab[cb];
232
 
233
  if (fr < 0.0) fr = 0.0;
234
  else if (fr > 255.0) fr = 255.0;
235
 
236
  if (fg < 0.0) fg = 0.0;
237
  else if (fg > 255.0) fg = 255.0;
238
 
239
  if (fb < 0.0) fb = 0.0;
240
  else if (fb > 255.0) fb = 255.0;
241
 
242
  *r = (unsigned char) fr;
243
  *g = (unsigned char) fg;
244
  *b = (unsigned char) fb;
245
 
246
}