233 lines
5.5 KiB
C
233 lines
5.5 KiB
C
/*
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
*/
|
|
/*
|
|
* Graphical routines for setting the palette
|
|
*
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <conf.h>
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "pstypes.h"
|
|
#include "u_mem.h"
|
|
#include "gr.h"
|
|
#include "grdef.h"
|
|
#include "cfile.h"
|
|
#include "error.h"
|
|
#include "mono.h"
|
|
#include "fix.h"
|
|
//added/remove by dph on 1/9/99
|
|
//#include "key.h"
|
|
//end remove
|
|
|
|
extern int gr_installed;
|
|
|
|
ubyte gr_palette[256*3];
|
|
ubyte gr_current_pal[256*3];
|
|
ubyte gr_fade_table[256*34];
|
|
|
|
ubyte gr_palette_gamma = 0;
|
|
int gr_palette_gamma_param = 0;
|
|
ubyte gr_palette_faded_out = 1;
|
|
|
|
extern void gr_palette_load( ubyte * pal );
|
|
|
|
void gr_palette_set_gamma( int gamma )
|
|
{
|
|
if ( gamma < 0 ) gamma = 0;
|
|
//added/changed on 10/27/98 by Victor Rachels to increase brightness slider
|
|
if ( gamma > 16 ) gamma = 16; //was 8
|
|
//end this section change - Victor Rachels
|
|
|
|
if (gr_palette_gamma_param != gamma ) {
|
|
gr_palette_gamma_param = gamma;
|
|
gr_palette_gamma = gamma;
|
|
if (!gr_palette_faded_out)
|
|
gr_palette_load( gr_palette );
|
|
}
|
|
}
|
|
|
|
int gr_palette_get_gamma()
|
|
{
|
|
return gr_palette_gamma_param;
|
|
}
|
|
|
|
|
|
void gr_use_palette_table( char * filename )
|
|
{
|
|
CFILE *fp;
|
|
int i,fsize;
|
|
|
|
fp = cfopen( filename, "rb" );
|
|
if ( fp==NULL)
|
|
Error("Can't open palette file <%s>",filename);
|
|
|
|
fsize = cfilelength( fp );
|
|
Assert( fsize == 9472 );
|
|
cfread( gr_palette, 256*3, 1, fp );
|
|
cfread( gr_fade_table, 256*34, 1, fp );
|
|
cfclose(fp);
|
|
|
|
// This is the TRANSPARENCY COLOR
|
|
for (i=0; i<GR_FADE_LEVELS; i++ ) {
|
|
gr_fade_table[i*256+255] = 255;
|
|
}
|
|
|
|
}
|
|
|
|
#define SQUARE(x) ((x)*(x))
|
|
|
|
#define MAX_COMPUTED_COLORS 32
|
|
|
|
int Num_computed_colors=0;
|
|
|
|
typedef struct {
|
|
ubyte r,g,b,color_num;
|
|
} color_record;
|
|
|
|
color_record Computed_colors[MAX_COMPUTED_COLORS];
|
|
|
|
// Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
|
|
// If list wasn't full already, increment Num_computed_colors.
|
|
// If was full, replace a random one.
|
|
void add_computed_color(int r, int g, int b, int color_num)
|
|
{
|
|
int add_index;
|
|
|
|
if (Num_computed_colors < MAX_COMPUTED_COLORS) {
|
|
add_index = Num_computed_colors;
|
|
Num_computed_colors++;
|
|
} else
|
|
add_index = (d_rand() * MAX_COMPUTED_COLORS) >> 15;
|
|
|
|
Computed_colors[add_index].r = r;
|
|
Computed_colors[add_index].g = g;
|
|
Computed_colors[add_index].b = b;
|
|
Computed_colors[add_index].color_num = color_num;
|
|
}
|
|
|
|
void init_computed_colors(void)
|
|
{
|
|
int i;
|
|
|
|
for (i=0; i<MAX_COMPUTED_COLORS; i++)
|
|
Computed_colors[i].r = 255; // Make impossible to match.
|
|
}
|
|
|
|
int gr_find_closest_color( int r, int g, int b )
|
|
{
|
|
int i, j;
|
|
int best_value, best_index, value;
|
|
|
|
if (Num_computed_colors == 0)
|
|
init_computed_colors();
|
|
|
|
// If we've already computed this color, return it!
|
|
for (i=0; i<Num_computed_colors; i++)
|
|
if (r == Computed_colors[i].r)
|
|
if (g == Computed_colors[i].g)
|
|
if (b == Computed_colors[i].b) {
|
|
if (i > 4) {
|
|
color_record trec;
|
|
trec = Computed_colors[i-1];
|
|
Computed_colors[i-1] = Computed_colors[i];
|
|
Computed_colors[i] = trec;
|
|
return Computed_colors[i-1].color_num;
|
|
}
|
|
return Computed_colors[i].color_num;
|
|
}
|
|
|
|
// r &= 63;
|
|
// g &= 63;
|
|
// b &= 63;
|
|
|
|
best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
|
|
best_index = 0;
|
|
if (best_value==0) {
|
|
add_computed_color(r, g, b, best_index);
|
|
return best_index;
|
|
}
|
|
j=0;
|
|
// only go to 255, 'cause we dont want to check the transparent color.
|
|
for (i=1; i<254; i++ ) {
|
|
j += 3;
|
|
value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
|
|
if ( value < best_value ) {
|
|
if (value==0) {
|
|
add_computed_color(r, g, b, i);
|
|
return i;
|
|
}
|
|
best_value = value;
|
|
best_index = i;
|
|
}
|
|
}
|
|
add_computed_color(r, g, b, best_index);
|
|
return best_index;
|
|
}
|
|
|
|
int gr_find_closest_color_15bpp( int rgb )
|
|
{
|
|
return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
|
|
}
|
|
|
|
|
|
int gr_find_closest_color_current( int r, int g, int b )
|
|
{
|
|
int i, j;
|
|
int best_value, best_index, value;
|
|
|
|
// r &= 63;
|
|
// g &= 63;
|
|
// b &= 63;
|
|
|
|
best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
|
|
best_index = 0;
|
|
if (best_value==0)
|
|
return best_index;
|
|
|
|
j=0;
|
|
// only go to 255, 'cause we dont want to check the transparent color.
|
|
for (i=1; i<254; i++ ) {
|
|
j += 3;
|
|
value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
|
|
if ( value < best_value ) {
|
|
if (value==0)
|
|
return i;
|
|
best_value = value;
|
|
best_index = i;
|
|
}
|
|
}
|
|
return best_index;
|
|
}
|
|
|
|
void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
|
|
{
|
|
int i;
|
|
ubyte r1, g1, b1;
|
|
|
|
for (i=0; i<256; i++ ) {
|
|
r1 = gr_palette[i*3+0] + r;
|
|
if ( r1 > 63 ) r1 = 63;
|
|
g1 = gr_palette[i*3+1] + g;
|
|
if ( g1 > 63 ) g1 = 63;
|
|
b1 = gr_palette[i*3+2] + b;
|
|
if ( b1 > 63 ) b1 = 63;
|
|
table[i] = gr_find_closest_color( r1, g1, b1 );
|
|
}
|
|
}
|
|
|